File: TransferBranchTest.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (114 lines) | stat: -rw-r--r-- 3,776 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//===- unittests/Analysis/FlowSensitive/SignAnalysisTest.cpp --===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
//  This file defines a test for the transferBranch function of the
//  TypeErasedDataflowAnalysis.
//
//===----------------------------------------------------------------------===//

#include "TestingSupport.h"
#include "clang/Analysis/FlowSensitive/DataflowAnalysis.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/Error.h"
#include "llvm/Testing/Annotations/Annotations.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"
#include <optional>

namespace clang::dataflow::test {
namespace {

using namespace ast_matchers;

struct TestLattice {
  std::optional<bool> Branch;
  static TestLattice bottom() { return {}; }

  // Does not matter for this test, but we must provide some definition of join.
  LatticeJoinEffect join(const TestLattice &Other) {
    return LatticeJoinEffect::Unchanged;
  }
  friend bool operator==(const TestLattice &Lhs, const TestLattice &Rhs) {
    return Lhs.Branch == Rhs.Branch;
  }
};

class TestPropagationAnalysis
    : public DataflowAnalysis<TestPropagationAnalysis, TestLattice> {
public:
  explicit TestPropagationAnalysis(ASTContext &Context)
      : DataflowAnalysis<TestPropagationAnalysis, TestLattice>(Context) {}
  static TestLattice initialElement() { return TestLattice::bottom(); }
  void transfer(const CFGElement &, TestLattice &, Environment &) {}
  void transferBranch(bool Branch, const Stmt *S, TestLattice &L,
                      Environment &Env) {
    L.Branch = Branch;
  }
};

using ::testing::UnorderedElementsAre;

template <typename Matcher>
void runDataflow(llvm::StringRef Code, Matcher VerifyResults,
                 LangStandard::Kind Std = LangStandard::lang_cxx17,
                 llvm::StringRef TargetFun = "fun") {
  using ast_matchers::hasName;
  ASSERT_THAT_ERROR(
      checkDataflow<TestPropagationAnalysis>(
          AnalysisInputs<TestPropagationAnalysis>(
              Code, hasName(TargetFun),
              [](ASTContext &C, Environment &) {
                return TestPropagationAnalysis(C);
              })
              .withASTBuildArgs(
                  {"-fsyntax-only", "-fno-delayed-template-parsing",
                   "-std=" +
                       std::string(LangStandard::getLangStandardForKind(Std)
                                       .getName())}),
          VerifyResults),
      llvm::Succeeded());
}

template <typename LatticeT>
const LatticeT &getLatticeAtAnnotation(
    const llvm::StringMap<DataflowAnalysisState<LatticeT>> &AnnotationStates,
    llvm::StringRef Annotation) {
  auto It = AnnotationStates.find(Annotation);
  assert(It != AnnotationStates.end());
  return It->getValue().Lattice;
}

TEST(TransferBranchTest, IfElse) {
  std::string Code = R"(
    void fun(int a) {
      if (a > 0) {
        (void)1;
        // [[p]]
      } else {
        (void)0;
        // [[q]]
      }
    }
  )";
  runDataflow(
      Code,
      [](const llvm::StringMap<DataflowAnalysisState<TestLattice>> &Results,
         const AnalysisOutputs &) {
        ASSERT_THAT(Results.keys(), UnorderedElementsAre("p", "q"));

        const TestLattice &LP = getLatticeAtAnnotation(Results, "p");
        EXPECT_THAT(LP.Branch, std::make_optional(true));

        const TestLattice &LQ = getLatticeAtAnnotation(Results, "q");
        EXPECT_THAT(LQ.Branch, std::make_optional(false));
      },
      LangStandard::lang_cxx17);
}

} // namespace
} // namespace clang::dataflow::test