Debian Codegraph @

llvm-project/clang/unittests/Analysis/FlowSensitive/TestingSupportTest.cpp

raw
1 #include "TestingSupport.h"
2 #include "clang/AST/ASTContext.h"
3 #include "clang/ASTMatchers/ASTMatchFinder.h"
4 #include "clang/ASTMatchers/ASTMatchers.h"
5 #include "clang/Analysis/FlowSensitive/NoopAnalysis.h"
6 #include "clang/Tooling/Tooling.h"
7 #include "llvm/Testing/ADT/StringMapEntry.h"
8 #include "llvm/Testing/Support/Error.h"
9 #include "gmock/gmock.h"
10 #include "gtest/gtest.h"
11
12 using namespace clang;
13 using namespace dataflow;
14
15 namespace {
16
17 using ::clang::ast_matchers::functionDecl;
18 using ::clang::ast_matchers::hasAnyName;
19 using ::clang::ast_matchers::hasName;
20 using ::clang::ast_matchers::isDefinition;
21 using ::clang::dataflow::test::AnalysisInputs;
22 using ::clang::dataflow::test::AnalysisOutputs;
23 using ::clang::dataflow::test::checkDataflow;
24 using ::llvm::IsStringMapEntry;
25 using ::testing::_;
26 using ::testing::IsEmpty;
27 using ::testing::UnorderedElementsAre;
28
29 template <typename T>
30 const FunctionDecl *findTargetFunc(ASTContext &Context, T FunctionMatcher) {
31 auto TargetMatcher =
32 functionDecl(FunctionMatcher, isDefinition()).bind("target");
33 for (const auto &Node : ast_matchers::match(TargetMatcher, Context)) {
34 const auto *Func = Node.template getNodeAs<FunctionDecl>("target");
35 if (Func == nullptr)
36 continue;
37 if (Func->isTemplated())
38 continue;
39 return Func;
40 }
41 return nullptr;
42 }
43
44 void runTest(
45 llvm::StringRef Code, llvm::StringRef TargetName,
46 std::function<void(const llvm::DenseMap<const Stmt *, std::string> &)>
47 RunChecks) {
48 llvm::Annotations AnnotatedCode(Code);
49 auto Unit = tooling::buildASTFromCodeWithArgs(
50 AnnotatedCode.code(), {"-fsyntax-only", "-std=c++17"});
51 auto &Context = Unit->getASTContext();
52 const FunctionDecl *Func = findTargetFunc(Context, hasName(TargetName));
53 ASSERT_NE(Func, nullptr);
54
55 llvm::Expected<llvm::DenseMap<const Stmt *, std::string>> Mapping =
56 test::buildStatementToAnnotationMapping(Func, AnnotatedCode);
57 ASSERT_TRUE(static_cast<bool>(Mapping));
58
59 RunChecks(Mapping.get());
60 }
61
62 TEST(BuildStatementToAnnotationMappingTest, ReturnStmt) {
63 runTest(R"(
64 int target() {
65 return 42;
66 /*[[ok]]*/
67 }
68 )",
69 "target",
70 [](const llvm::DenseMap<const Stmt *, std::string> &Annotations) {
71 ASSERT_EQ(Annotations.size(), static_cast<unsigned int>(1));
72 EXPECT_TRUE(isa<ReturnStmt>(Annotations.begin()->first));
73 EXPECT_EQ(Annotations.begin()->second, "ok");
74 });
75 }
76
77 void checkDataflow(
78 llvm::StringRef Code,
79 ast_matchers::internal::Matcher<FunctionDecl> TargetFuncMatcher,
80 std::function<
81 void(const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &,
82 const AnalysisOutputs &)>
83 Expectations) {
84 ASSERT_THAT_ERROR(checkDataflow<NoopAnalysis>(
85 AnalysisInputs<NoopAnalysis>(
86 Code, std::move(TargetFuncMatcher),
87 [](ASTContext &Context, Environment &) {
88 return NoopAnalysis(
89 Context, /*ApplyBuiltinTransfer=*/false);
90 })
91 .withASTBuildArgs({"-fsyntax-only", "-std=c++17"}),
92 /*VerifyResults=*/std::move(Expectations)),
93 llvm::Succeeded());
94 }
95
96 TEST(ProgramPointAnnotations, NoAnnotations) {
97 ::testing::MockFunction<void(
98 const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &,
99 const AnalysisOutputs &)>
100 Expectations;
101
102 EXPECT_CALL(Expectations, Call(IsEmpty(), _)).Times(1);
103
104 checkDataflow("void target() {}", hasName("target"),
105 Expectations.AsStdFunction());
106 }
107
108 TEST(ProgramPointAnnotations, NoAnnotationsDifferentTarget) {
109 ::testing::MockFunction<void(
110 const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &,
111 const AnalysisOutputs &)>
112 Expectations;
113
114 EXPECT_CALL(Expectations, Call(IsEmpty(), _)).Times(1);
115
116 checkDataflow("void target() {}", hasName("target"),
117 Expectations.AsStdFunction());
118 }
119
120 TEST(ProgramPointAnnotations, WithProgramPoint) {
121 ::testing::MockFunction<void(
122 const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &,
123 const AnalysisOutputs &)>
124 Expectations;
125
126 EXPECT_CALL(
127 Expectations,
128 Call(UnorderedElementsAre(IsStringMapEntry("program-point", _)), _))
129 .Times(1);
130
131 checkDataflow(R"cc(void target() {
132 int n;
133 // [[program-point]]
134 })cc",
135 hasName("target"), Expectations.AsStdFunction());
136 }
137
138 TEST(ProgramPointAnnotations, MultipleProgramPoints) {
139 ::testing::MockFunction<void(
140 const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &,
141 const AnalysisOutputs &)>
142 Expectations;
143
144 EXPECT_CALL(Expectations,
145 Call(UnorderedElementsAre(IsStringMapEntry("program-point-1", _),
146 IsStringMapEntry("program-point-2", _)),
147 _))
148 .Times(1);
149
150 checkDataflow(R"cc(void target(bool b) {
151 if (b) {
152 int n;
153 // [[program-point-1]]
154 } else {
155 int m;
156 // [[program-point-2]]
157 }
158 })cc",
159 hasName("target"), Expectations.AsStdFunction());
160 }
161
162 TEST(ProgramPointAnnotations, MultipleFunctionsMultipleProgramPoints) {
163 ::testing::MockFunction<void(
164 const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &,
165 const AnalysisOutputs &)>
166 Expectations;
167
168 EXPECT_CALL(Expectations, Call(UnorderedElementsAre(
169 IsStringMapEntry("program-point-1a", _),
170 IsStringMapEntry("program-point-1b", _)),
171 _))
172 .Times(1);
173
174 EXPECT_CALL(Expectations, Call(UnorderedElementsAre(
175 IsStringMapEntry("program-point-2a", _),
176 IsStringMapEntry("program-point-2b", _)),
177 _))
178 .Times(1);
179
180 checkDataflow(
181 R"cc(
182 void target1(bool b) {
183 if (b) {
184 int n;
185 // [[program-point-1a]]
186 } else {
187 int m;
188 // [[program-point-1b]]
189 }
190 }
191
192 void target2(bool b) {
193 if (b) {
194 int n;
195 // [[program-point-2a]]
196 } else {
197 int m;
198 // [[program-point-2b]]
199 }
200 }
201 )cc",
202 functionDecl(hasAnyName("target1", "target2")),
203 Expectations.AsStdFunction());
204 }
205
206 } // namespace