Debian Codegraph @

clang/unittests/Analysis/Scalable/ASTEntityMappingTest.cpp

raw
1 //===- unittests/Analysis/Scalable/ASTEntityMappingTest.cpp --------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "clang/Analysis/Scalable/ASTEntityMapping.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/AST/Decl.h"
12 #include "clang/AST/DeclCXX.h"
13 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 #include "clang/ASTMatchers/ASTMatchers.h"
15 #include "clang/Tooling/Tooling.h"
16 #include "gtest/gtest.h"
17
18 using namespace clang::ast_matchers;
19
20 namespace clang::ssaf {
21 namespace {
22
23 // Helper function to find a declaration by name
24 template <typename DeclType>
25 const DeclType *findDecl(ASTContext &Ctx, StringRef Name) {
26 auto Matcher = namedDecl(hasName(Name)).bind("decl");
27 auto Matches = match(Matcher, Ctx);
28 if (Matches.empty())
29 return nullptr;
30 if (auto Result = Matches[0].getNodeAs<DeclType>("decl"))
31 return dyn_cast<DeclType>(Result->getCanonicalDecl());
32 return nullptr;
33 }
34
35 TEST(ASTEntityMappingTest, FunctionDecl) {
36 auto AST = tooling::buildASTFromCode(R"cpp(void foo() {})cpp");
37 auto &Ctx = AST->getASTContext();
38
39 const auto *FD = findDecl<FunctionDecl>(Ctx, "foo");
40 ASSERT_NE(FD, nullptr);
41
42 auto EntityName = getEntityName(FD);
43 EXPECT_TRUE(EntityName.has_value());
44 }
45
46 TEST(ASTEntityMappingTest, VarDecl) {
47 auto AST = tooling::buildASTFromCode(R"cpp(int x = 42;)cpp");
48 auto &Ctx = AST->getASTContext();
49
50 const auto *VD = findDecl<VarDecl>(Ctx, "x");
51 ASSERT_NE(VD, nullptr);
52
53 auto EntityName = getEntityName(VD);
54 EXPECT_TRUE(EntityName.has_value());
55 }
56
57 TEST(ASTEntityMappingTest, ParmVarDecl) {
58 auto AST = tooling::buildASTFromCode(R"cpp(void foo(int x) {})cpp");
59 auto &Ctx = AST->getASTContext();
60
61 const auto *FD = findDecl<FunctionDecl>(Ctx, "foo");
62 ASSERT_NE(FD, nullptr);
63 ASSERT_EQ(FD->param_size(), 1u);
64
65 const auto *PVD = FD->getParamDecl(0);
66 ASSERT_NE(PVD, nullptr);
67
68 auto EntityName = getEntityName(PVD);
69 EXPECT_TRUE(EntityName.has_value());
70 }
71
72 TEST(ASTEntityMappingTest, RecordDecl) {
73 auto AST = tooling::buildASTFromCode(R"cpp(struct S {};)cpp");
74 auto &Ctx = AST->getASTContext();
75
76 const auto *RD = findDecl<RecordDecl>(Ctx, "S");
77 ASSERT_NE(RD, nullptr);
78
79 auto EntityName = getEntityName(RD);
80 EXPECT_TRUE(EntityName.has_value());
81 }
82
83 TEST(ASTEntityMappingTest, FieldDecl) {
84 auto AST = tooling::buildASTFromCode(R"cpp(struct S { int field; };)cpp");
85 auto &Ctx = AST->getASTContext();
86
87 const auto *FD = findDecl<FieldDecl>(Ctx, "field");
88 ASSERT_NE(FD, nullptr);
89
90 auto EntityName = getEntityName(FD);
91 EXPECT_TRUE(EntityName.has_value());
92 }
93
94 TEST(ASTEntityMappingTest, NullDecl) {
95 auto EntityName = getEntityName(nullptr);
96 EXPECT_FALSE(EntityName.has_value());
97 }
98
99 TEST(ASTEntityMappingTest, ImplicitDeclLambda) {
100 auto AST = tooling::buildASTFromCode(
101 R"cpp(
102 auto L = [](){};
103 )cpp",
104 "test.cpp", std::make_shared<PCHContainerOperations>());
105 auto &Ctx = AST->getASTContext();
106
107 auto Matcher = cxxRecordDecl(isImplicit()).bind("decl");
108 auto Matches = match(Matcher, Ctx);
109 ASSERT_GT(Matches.size(), 0u);
110
111 const auto *ImplCXXRD = Matches[0].getNodeAs<CXXRecordDecl>("decl");
112 ASSERT_NE(ImplCXXRD, nullptr);
113
114 auto EntityName = getEntityName(ImplCXXRD);
115 EXPECT_FALSE(EntityName.has_value());
116 }
117
118 TEST(ASTEntityMappingTest, BuiltinFunction) {
119 auto AST = tooling::buildASTFromCode(R"cpp(
120 void test() {
121 __builtin_memcpy(0, 0, 0);
122 }
123 )cpp");
124 auto &Ctx = AST->getASTContext();
125
126 // Find the builtin call
127 auto Matcher = callExpr().bind("call");
128 auto Matches = match(Matcher, Ctx);
129 ASSERT_EQ(Matches.size(), 1ul);
130
131 const auto *CE = Matches[0].getNodeAs<CallExpr>("call");
132 ASSERT_NE(CE, nullptr);
133
134 const auto *Callee = CE->getDirectCallee();
135 ASSERT_NE(Callee, nullptr);
136 ASSERT_NE(Callee->getBuiltinID(), 0u /* not a built-in */);
137
138 auto EntityName = getEntityName(Callee);
139 EXPECT_FALSE(EntityName.has_value());
140 }
141
142 TEST(ASTEntityMappingTest, UnsupportedDecl) {
143 auto AST = tooling::buildASTFromCode(R"cpp(namespace N {})cpp");
144 auto &Ctx = AST->getASTContext();
145
146 const auto *ND = findDecl<NamespaceDecl>(Ctx, "N");
147 ASSERT_NE(ND, nullptr);
148
149 auto EntityName = getEntityName(ND);
150 EXPECT_FALSE(EntityName.has_value());
151 }
152
153 TEST(ASTEntityMappingTest, FunctionReturn) {
154 auto AST = tooling::buildASTFromCode(R"cpp(int foo() { return 42; })cpp");
155 auto &Ctx = AST->getASTContext();
156
157 const auto *FD = findDecl<FunctionDecl>(Ctx, "foo");
158 ASSERT_NE(FD, nullptr);
159
160 auto EntityName = getEntityNameForReturn(FD);
161 EXPECT_TRUE(EntityName.has_value());
162 }
163
164 TEST(ASTEntityMappingTest, FunctionReturnNull) {
165 auto EntityName = getEntityNameForReturn(nullptr);
166 EXPECT_FALSE(EntityName.has_value());
167 }
168
169 TEST(ASTEntityMappingTest, FunctionReturnBuiltin) {
170 auto AST = tooling::buildASTFromCode(R"cpp(
171 void test() {
172 __builtin_memcpy(0, 0, 0);
173 }
174 )cpp");
175 auto &Ctx = AST->getASTContext();
176
177 // Find the builtin call
178 auto Matcher = callExpr().bind("call");
179 auto Matches = match(Matcher, Ctx);
180 ASSERT_FALSE(Matches.empty());
181
182 const auto *CE = Matches[0].getNodeAs<CallExpr>("call");
183 ASSERT_NE(CE, nullptr);
184
185 const auto *Callee = CE->getDirectCallee();
186 if (Callee && Callee->getBuiltinID()) {
187 auto EntityName = getEntityNameForReturn(Callee);
188 EXPECT_FALSE(EntityName.has_value());
189 }
190 }
191
192 TEST(ASTEntityMappingTest, DifferentFunctionsDifferentNames) {
193 auto AST = tooling::buildASTFromCode(R"cpp(
194 void foo() {}
195 void bar() {}
196 )cpp");
197 auto &Ctx = AST->getASTContext();
198
199 const auto *Foo = findDecl<FunctionDecl>(Ctx, "foo");
200 const auto *Bar = findDecl<FunctionDecl>(Ctx, "bar");
201 ASSERT_NE(Foo, nullptr);
202 ASSERT_NE(Bar, nullptr);
203
204 auto FooName = getEntityName(Foo);
205 auto BarName = getEntityName(Bar);
206 ASSERT_TRUE(FooName.has_value());
207 ASSERT_TRUE(BarName.has_value());
208
209 EXPECT_NE(*FooName, *BarName);
210 }
211
212 // Redeclaration tests
213
214 TEST(ASTEntityMappingTest, FunctionRedeclaration) {
215 auto AST = tooling::buildASTFromCode(R"cpp(
216 void foo();
217 void foo() {}
218 )cpp");
219 auto &Ctx = AST->getASTContext();
220
221 auto Matcher = functionDecl(hasName("foo")).bind("decl");
222 auto Matches = match(Matcher, Ctx);
223 ASSERT_GE(Matches.size(), 2u);
224
225 const auto *FirstDecl = Matches[0].getNodeAs<FunctionDecl>("decl");
226 ASSERT_NE(FirstDecl, nullptr);
227
228 auto FirstName = getEntityName(FirstDecl);
229 ASSERT_TRUE(FirstName.has_value());
230
231 for (size_t I = 1; I < Matches.size(); ++I) {
232 const auto *Decl = Matches[I].getNodeAs<FunctionDecl>("decl");
233 ASSERT_NE(Decl, nullptr);
234
235 auto Name = getEntityName(Decl);
236 ASSERT_TRUE(Name.has_value());
237 EXPECT_EQ(*FirstName, *Name);
238 }
239 }
240
241 TEST(ASTEntityMappingTest, VarRedeclaration) {
242 auto AST = tooling::buildASTFromCode(R"cpp(
243 extern int x;
244 int x = 42;
245 )cpp");
246 auto &Ctx = AST->getASTContext();
247
248 auto Matcher = varDecl(hasName("x")).bind("decl");
249 auto Matches = match(Matcher, Ctx);
250 ASSERT_EQ(Matches.size(), 2u);
251
252 const auto *FirstDecl = Matches[0].getNodeAs<VarDecl>("decl");
253 ASSERT_NE(FirstDecl, nullptr);
254
255 auto FirstName = getEntityName(FirstDecl);
256 ASSERT_TRUE(FirstName.has_value());
257
258 for (size_t I = 1; I < Matches.size(); ++I) {
259 const auto *Decl = Matches[I].getNodeAs<VarDecl>("decl");
260 ASSERT_NE(Decl, nullptr);
261
262 auto Name = getEntityName(Decl);
263 ASSERT_TRUE(Name.has_value());
264 EXPECT_EQ(*FirstName, *Name);
265 }
266 }
267
268 TEST(ASTEntityMappingTest, RecordRedeclaration) {
269 auto AST = tooling::buildASTFromCode(R"cpp(
270 struct S;
271 struct S {};
272 )cpp");
273 auto &Ctx = AST->getASTContext();
274
275 auto Matcher = recordDecl(hasName("S"), unless(isImplicit())).bind("decl");
276 auto Matches = match(Matcher, Ctx);
277 ASSERT_GE(Matches.size(), 2u);
278
279 const auto *FirstDecl = Matches[0].getNodeAs<RecordDecl>("decl");
280 ASSERT_NE(FirstDecl, nullptr);
281
282 auto FirstName = getEntityName(FirstDecl);
283 ASSERT_TRUE(FirstName.has_value());
284
285 for (size_t I = 1; I < Matches.size(); ++I) {
286 const auto *Decl = Matches[I].getNodeAs<RecordDecl>("decl");
287 ASSERT_NE(Decl, nullptr);
288
289 auto Name = getEntityName(Decl);
290 ASSERT_TRUE(Name.has_value());
291 EXPECT_EQ(*FirstName, *Name);
292 }
293 }
294
295 TEST(ASTEntityMappingTest, ParmVarDeclRedeclaration) {
296 auto AST = tooling::buildASTFromCode(R"cpp(
297 void foo(int);
298 void foo(int x);
299 void foo(int y);
300 void foo(int x) {}
301 )cpp");
302 auto &Ctx = AST->getASTContext();
303
304 auto Matcher = functionDecl(hasName("foo")).bind("decl");
305 auto Matches = match(Matcher, Ctx);
306 ASSERT_GE(Matches.size(), 2u);
307
308 const auto *FirstFuncDecl = Matches[0].getNodeAs<FunctionDecl>("decl");
309 ASSERT_NE(FirstFuncDecl, nullptr);
310 ASSERT_GT(FirstFuncDecl->param_size(), 0u);
311
312 auto ParamEName = getEntityName(FirstFuncDecl->getParamDecl(0));
313 ASSERT_TRUE(ParamEName.has_value());
314
315 for (size_t I = 1; I < Matches.size(); ++I) {
316 const auto *FDecl = Matches[I].getNodeAs<FunctionDecl>("decl");
317 ASSERT_NE(FDecl, nullptr);
318 ASSERT_GT(FDecl->param_size(), 0u);
319
320 auto ParamRedeclEName = getEntityName(FDecl->getParamDecl(0));
321 EXPECT_EQ(*ParamEName, *ParamRedeclEName);
322 }
323 }
324
325 TEST(ASTEntityMappingTest, FunctionReturnRedeclaration) {
326 auto AST = tooling::buildASTFromCode(R"cpp(
327 int foo();
328 int foo() { return 42; }
329 )cpp");
330 auto &Ctx = AST->getASTContext();
331
332 auto Matcher = functionDecl(hasName("foo")).bind("decl");
333 auto Matches = match(Matcher, Ctx);
334 ASSERT_EQ(Matches.size(), 2u);
335
336 const auto *Decl1 = Matches[0].getNodeAs<FunctionDecl>("decl");
337 ASSERT_NE(Decl1, nullptr);
338 auto Name1 = getEntityNameForReturn(Decl1);
339 ASSERT_TRUE(Name1.has_value());
340
341 for (size_t I = 1; I < Matches.size(); ++I) {
342 const auto *FDecl = Matches[I].getNodeAs<FunctionDecl>("decl");
343 ASSERT_NE(FDecl, nullptr);
344
345 auto Name = getEntityNameForReturn(Decl1);
346 ASSERT_TRUE(Name.has_value());
347 EXPECT_EQ(*Name1, *Name);
348 }
349 }
350
351 } // namespace
352 } // namespace clang::ssaf