1 //===- unittest/Tooling/ASTMatchersTest.h - Matcher tests helpers ------===//
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 #ifndef LLVM_CLANG_UNITTESTS_ASTMATCHERS_ASTMATCHERSTEST_H
10 #define LLVM_CLANG_UNITTESTS_ASTMATCHERS_ASTMATCHERSTEST_H
11 
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Frontend/ASTUnit.h"
14 #include "clang/Testing/CommandLineArgs.h"
15 #include "clang/Testing/TestClangConfig.h"
16 #include "clang/Tooling/Tooling.h"
17 #include "gtest/gtest.h"
18 
19 namespace clang {
20 namespace ast_matchers {
21 
22 using clang::tooling::buildASTFromCodeWithArgs;
23 using clang::tooling::FileContentMappings;
24 using clang::tooling::FrontendActionFactory;
25 using clang::tooling::newFrontendActionFactory;
26 using clang::tooling::runToolOnCodeWithArgs;
27 
28 class BoundNodesCallback {
29 public:
~BoundNodesCallback()30   virtual ~BoundNodesCallback() {}
31   virtual bool run(const BoundNodes *BoundNodes) = 0;
32   virtual bool run(const BoundNodes *BoundNodes, ASTContext *Context) = 0;
onEndOfTranslationUnit()33   virtual void onEndOfTranslationUnit() {}
34 };
35 
36 // If 'FindResultVerifier' is not NULL, sets *Verified to the result of
37 // running 'FindResultVerifier' with the bound nodes as argument.
38 // If 'FindResultVerifier' is NULL, sets *Verified to true when Run is called.
39 class VerifyMatch : public MatchFinder::MatchCallback {
40 public:
VerifyMatch(std::unique_ptr<BoundNodesCallback> FindResultVerifier,bool * Verified)41   VerifyMatch(std::unique_ptr<BoundNodesCallback> FindResultVerifier,
42               bool *Verified)
43       : Verified(Verified), FindResultReviewer(std::move(FindResultVerifier)) {}
44 
run(const MatchFinder::MatchResult & Result)45   void run(const MatchFinder::MatchResult &Result) override {
46     if (FindResultReviewer != nullptr) {
47       *Verified |= FindResultReviewer->run(&Result.Nodes, Result.Context);
48     } else {
49       *Verified = true;
50     }
51   }
52 
onEndOfTranslationUnit()53   void onEndOfTranslationUnit() override {
54     if (FindResultReviewer)
55       FindResultReviewer->onEndOfTranslationUnit();
56   }
57 
58 private:
59   bool *const Verified;
60   const std::unique_ptr<BoundNodesCallback> FindResultReviewer;
61 };
62 
langCxx11OrLater()63 inline ArrayRef<TestLanguage> langCxx11OrLater() {
64   static const TestLanguage Result[] = {Lang_CXX11, Lang_CXX14, Lang_CXX17,
65                                         Lang_CXX20};
66   return ArrayRef<TestLanguage>(Result);
67 }
68 
langCxx14OrLater()69 inline ArrayRef<TestLanguage> langCxx14OrLater() {
70   static const TestLanguage Result[] = {Lang_CXX14, Lang_CXX17, Lang_CXX20};
71   return ArrayRef<TestLanguage>(Result);
72 }
73 
langCxx17OrLater()74 inline ArrayRef<TestLanguage> langCxx17OrLater() {
75   static const TestLanguage Result[] = {Lang_CXX17, Lang_CXX20};
76   return ArrayRef<TestLanguage>(Result);
77 }
78 
langCxx20OrLater()79 inline ArrayRef<TestLanguage> langCxx20OrLater() {
80   static const TestLanguage Result[] = {Lang_CXX20};
81   return ArrayRef<TestLanguage>(Result);
82 }
83 
84 template <typename T>
85 testing::AssertionResult matchesConditionally(
86     const Twine &Code, const T &AMatcher, bool ExpectMatch,
87     ArrayRef<std::string> CompileArgs,
88     const FileContentMappings &VirtualMappedFiles = FileContentMappings(),
89     StringRef Filename = "input.cc") {
90   bool Found = false, DynamicFound = false;
91   MatchFinder Finder;
92   VerifyMatch VerifyFound(nullptr, &Found);
93   Finder.addMatcher(AMatcher, &VerifyFound);
94   VerifyMatch VerifyDynamicFound(nullptr, &DynamicFound);
95   if (!Finder.addDynamicMatcher(AMatcher, &VerifyDynamicFound))
96     return testing::AssertionFailure() << "Could not add dynamic matcher";
97   std::unique_ptr<FrontendActionFactory> Factory(
98       newFrontendActionFactory(&Finder));
99   std::vector<std::string> Args = {
100       // Some tests need rtti/exceptions on.
101       "-frtti", "-fexceptions",
102       // Ensure that tests specify the C++ standard version that they need.
103       "-Werror=c++14-extensions", "-Werror=c++17-extensions",
104       "-Werror=c++20-extensions"};
105   // Append additional arguments at the end to allow overriding the default
106   // choices that we made above.
107   llvm::copy(CompileArgs, std::back_inserter(Args));
108   if (llvm::find(Args, "-target") == Args.end()) {
109     // Use an unknown-unknown triple so we don't instantiate the full system
110     // toolchain.  On Linux, instantiating the toolchain involves stat'ing
111     // large portions of /usr/lib, and this slows down not only this test, but
112     // all other tests, via contention in the kernel.
113     //
114     // FIXME: This is a hack to work around the fact that there's no way to do
115     // the equivalent of runToolOnCodeWithArgs without instantiating a full
116     // Driver.  We should consider having a function, at least for tests, that
117     // invokes cc1.
118     Args.push_back("-target");
119     Args.push_back("i386-unknown-unknown");
120   }
121 
122   if (!runToolOnCodeWithArgs(
123           Factory->create(), Code, Args, Filename, "clang-tool",
124           std::make_shared<PCHContainerOperations>(), VirtualMappedFiles)) {
125     return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
126   }
127   if (Found != DynamicFound) {
128     return testing::AssertionFailure()
129            << "Dynamic match result (" << DynamicFound
130            << ") does not match static result (" << Found << ")";
131   }
132   if (!Found && ExpectMatch) {
133     return testing::AssertionFailure()
134            << "Could not find match in \"" << Code << "\"";
135   } else if (Found && !ExpectMatch) {
136     return testing::AssertionFailure()
137            << "Found unexpected match in \"" << Code << "\"";
138   }
139   return testing::AssertionSuccess();
140 }
141 
142 template <typename T>
143 testing::AssertionResult
matchesConditionally(const Twine & Code,const T & AMatcher,bool ExpectMatch,ArrayRef<TestLanguage> TestLanguages)144 matchesConditionally(const Twine &Code, const T &AMatcher, bool ExpectMatch,
145                      ArrayRef<TestLanguage> TestLanguages) {
146   for (auto Lang : TestLanguages) {
147     auto Result = matchesConditionally(
148         Code, AMatcher, ExpectMatch, getCommandLineArgsForTesting(Lang),
149         FileContentMappings(), getFilenameForTesting(Lang));
150     if (!Result)
151       return Result;
152   }
153 
154   return testing::AssertionSuccess();
155 }
156 
157 template <typename T>
158 testing::AssertionResult
159 matches(const Twine &Code, const T &AMatcher,
160         ArrayRef<TestLanguage> TestLanguages = {Lang_CXX11}) {
161   return matchesConditionally(Code, AMatcher, true, TestLanguages);
162 }
163 
164 template <typename T>
165 testing::AssertionResult
166 notMatches(const Twine &Code, const T &AMatcher,
167            ArrayRef<TestLanguage> TestLanguages = {Lang_CXX11}) {
168   return matchesConditionally(Code, AMatcher, false, TestLanguages);
169 }
170 
171 template <typename T>
172 testing::AssertionResult matchesObjC(const Twine &Code, const T &AMatcher,
173                                      bool ExpectMatch = true) {
174   return matchesConditionally(Code, AMatcher, ExpectMatch,
175                               {"-fobjc-nonfragile-abi", "-Wno-objc-root-class",
176                                "-fblocks", "-Wno-incomplete-implementation"},
177                               FileContentMappings(), "input.m");
178 }
179 
180 template <typename T>
matchesC(const Twine & Code,const T & AMatcher)181 testing::AssertionResult matchesC(const Twine &Code, const T &AMatcher) {
182   return matchesConditionally(Code, AMatcher, true, {}, FileContentMappings(),
183                               "input.c");
184 }
185 
186 template <typename T>
notMatchesObjC(const Twine & Code,const T & AMatcher)187 testing::AssertionResult notMatchesObjC(const Twine &Code, const T &AMatcher) {
188   return matchesObjC(Code, AMatcher, false);
189 }
190 
191 // Function based on matchesConditionally with "-x cuda" argument added and
192 // small CUDA header prepended to the code string.
193 template <typename T>
194 testing::AssertionResult
matchesConditionallyWithCuda(const Twine & Code,const T & AMatcher,bool ExpectMatch,llvm::StringRef CompileArg)195 matchesConditionallyWithCuda(const Twine &Code, const T &AMatcher,
196                              bool ExpectMatch, llvm::StringRef CompileArg) {
197   const std::string CudaHeader =
198       "typedef unsigned int size_t;\n"
199       "#define __constant__ __attribute__((constant))\n"
200       "#define __device__ __attribute__((device))\n"
201       "#define __global__ __attribute__((global))\n"
202       "#define __host__ __attribute__((host))\n"
203       "#define __shared__ __attribute__((shared))\n"
204       "struct dim3 {"
205       "  unsigned x, y, z;"
206       "  __host__ __device__ dim3(unsigned x, unsigned y = 1, unsigned z = 1)"
207       "      : x(x), y(y), z(z) {}"
208       "};"
209       "typedef struct cudaStream *cudaStream_t;"
210       "int cudaConfigureCall(dim3 gridSize, dim3 blockSize,"
211       "                      size_t sharedSize = 0,"
212       "                      cudaStream_t stream = 0);"
213       "extern \"C\" unsigned __cudaPushCallConfiguration("
214       "    dim3 gridDim, dim3 blockDim, size_t sharedMem = 0, void *stream = "
215       "0);";
216 
217   bool Found = false, DynamicFound = false;
218   MatchFinder Finder;
219   VerifyMatch VerifyFound(nullptr, &Found);
220   Finder.addMatcher(AMatcher, &VerifyFound);
221   VerifyMatch VerifyDynamicFound(nullptr, &DynamicFound);
222   if (!Finder.addDynamicMatcher(AMatcher, &VerifyDynamicFound))
223     return testing::AssertionFailure() << "Could not add dynamic matcher";
224   std::unique_ptr<FrontendActionFactory> Factory(
225       newFrontendActionFactory(&Finder));
226   // Some tests use typeof, which is a gnu extension.  Using an explicit
227   // unknown-unknown triple is good for a large speedup, because it lets us
228   // avoid constructing a full system triple.
229   std::vector<std::string> Args = {
230       "-xcuda",  "-fno-ms-extensions",     "--cuda-host-only",     "-nocudainc",
231       "-target", "x86_64-unknown-unknown", std::string(CompileArg)};
232   if (!runToolOnCodeWithArgs(Factory->create(), CudaHeader + Code, Args)) {
233     return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
234   }
235   if (Found != DynamicFound) {
236     return testing::AssertionFailure()
237            << "Dynamic match result (" << DynamicFound
238            << ") does not match static result (" << Found << ")";
239   }
240   if (!Found && ExpectMatch) {
241     return testing::AssertionFailure()
242            << "Could not find match in \"" << Code << "\"";
243   } else if (Found && !ExpectMatch) {
244     return testing::AssertionFailure()
245            << "Found unexpected match in \"" << Code << "\"";
246   }
247   return testing::AssertionSuccess();
248 }
249 
250 template <typename T>
matchesWithCuda(const Twine & Code,const T & AMatcher)251 testing::AssertionResult matchesWithCuda(const Twine &Code, const T &AMatcher) {
252   return matchesConditionallyWithCuda(Code, AMatcher, true, "-std=c++11");
253 }
254 
255 template <typename T>
notMatchesWithCuda(const Twine & Code,const T & AMatcher)256 testing::AssertionResult notMatchesWithCuda(const Twine &Code,
257                                             const T &AMatcher) {
258   return matchesConditionallyWithCuda(Code, AMatcher, false, "-std=c++11");
259 }
260 
261 template <typename T>
matchesWithOpenMP(const Twine & Code,const T & AMatcher)262 testing::AssertionResult matchesWithOpenMP(const Twine &Code,
263                                            const T &AMatcher) {
264   return matchesConditionally(Code, AMatcher, true, {"-fopenmp=libomp"});
265 }
266 
267 template <typename T>
notMatchesWithOpenMP(const Twine & Code,const T & AMatcher)268 testing::AssertionResult notMatchesWithOpenMP(const Twine &Code,
269                                               const T &AMatcher) {
270   return matchesConditionally(Code, AMatcher, false, {"-fopenmp=libomp"});
271 }
272 
273 template <typename T>
matchesWithOpenMP51(const Twine & Code,const T & AMatcher)274 testing::AssertionResult matchesWithOpenMP51(const Twine &Code,
275                                              const T &AMatcher) {
276   return matchesConditionally(Code, AMatcher, true,
277                               {"-fopenmp=libomp", "-fopenmp-version=51"});
278 }
279 
280 template <typename T>
notMatchesWithOpenMP51(const Twine & Code,const T & AMatcher)281 testing::AssertionResult notMatchesWithOpenMP51(const Twine &Code,
282                                                 const T &AMatcher) {
283   return matchesConditionally(Code, AMatcher, false,
284                               {"-fopenmp=libomp", "-fopenmp-version=51"});
285 }
286 
287 template <typename T>
matchAndVerifyResultConditionally(const Twine & Code,const T & AMatcher,std::unique_ptr<BoundNodesCallback> FindResultVerifier,bool ExpectResult)288 testing::AssertionResult matchAndVerifyResultConditionally(
289     const Twine &Code, const T &AMatcher,
290     std::unique_ptr<BoundNodesCallback> FindResultVerifier, bool ExpectResult) {
291   bool VerifiedResult = false;
292   MatchFinder Finder;
293   VerifyMatch VerifyVerifiedResult(std::move(FindResultVerifier),
294                                    &VerifiedResult);
295   Finder.addMatcher(AMatcher, &VerifyVerifiedResult);
296   std::unique_ptr<FrontendActionFactory> Factory(
297       newFrontendActionFactory(&Finder));
298   // Some tests use typeof, which is a gnu extension.  Using an explicit
299   // unknown-unknown triple is good for a large speedup, because it lets us
300   // avoid constructing a full system triple.
301   std::vector<std::string> Args = {"-std=gnu++11", "-target",
302                                    "i386-unknown-unknown"};
303   if (!runToolOnCodeWithArgs(Factory->create(), Code, Args)) {
304     return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
305   }
306   if (!VerifiedResult && ExpectResult) {
307     return testing::AssertionFailure()
308            << "Could not verify result in \"" << Code << "\"";
309   } else if (VerifiedResult && !ExpectResult) {
310     return testing::AssertionFailure()
311            << "Verified unexpected result in \"" << Code << "\"";
312   }
313 
314   VerifiedResult = false;
315   SmallString<256> Buffer;
316   std::unique_ptr<ASTUnit> AST(
317       buildASTFromCodeWithArgs(Code.toStringRef(Buffer), Args));
318   if (!AST.get())
319     return testing::AssertionFailure()
320            << "Parsing error in \"" << Code << "\" while building AST";
321   Finder.matchAST(AST->getASTContext());
322   if (!VerifiedResult && ExpectResult) {
323     return testing::AssertionFailure()
324            << "Could not verify result in \"" << Code << "\" with AST";
325   } else if (VerifiedResult && !ExpectResult) {
326     return testing::AssertionFailure()
327            << "Verified unexpected result in \"" << Code << "\" with AST";
328   }
329 
330   return testing::AssertionSuccess();
331 }
332 
333 // FIXME: Find better names for these functions (or document what they
334 // do more precisely).
335 template <typename T>
matchAndVerifyResultTrue(const Twine & Code,const T & AMatcher,std::unique_ptr<BoundNodesCallback> FindResultVerifier)336 testing::AssertionResult matchAndVerifyResultTrue(
337     const Twine &Code, const T &AMatcher,
338     std::unique_ptr<BoundNodesCallback> FindResultVerifier) {
339   return matchAndVerifyResultConditionally(Code, AMatcher,
340                                            std::move(FindResultVerifier), true);
341 }
342 
343 template <typename T>
matchAndVerifyResultFalse(const Twine & Code,const T & AMatcher,std::unique_ptr<BoundNodesCallback> FindResultVerifier)344 testing::AssertionResult matchAndVerifyResultFalse(
345     const Twine &Code, const T &AMatcher,
346     std::unique_ptr<BoundNodesCallback> FindResultVerifier) {
347   return matchAndVerifyResultConditionally(
348       Code, AMatcher, std::move(FindResultVerifier), false);
349 }
350 
351 // Implements a run method that returns whether BoundNodes contains a
352 // Decl bound to Id that can be dynamically cast to T.
353 // Optionally checks that the check succeeded a specific number of times.
354 template <typename T> class VerifyIdIsBoundTo : public BoundNodesCallback {
355 public:
356   // Create an object that checks that a node of type \c T was bound to \c Id.
357   // Does not check for a certain number of matches.
VerifyIdIsBoundTo(llvm::StringRef Id)358   explicit VerifyIdIsBoundTo(llvm::StringRef Id)
359       : Id(std::string(Id)), ExpectedCount(-1), Count(0) {}
360 
361   // Create an object that checks that a node of type \c T was bound to \c Id.
362   // Checks that there were exactly \c ExpectedCount matches.
VerifyIdIsBoundTo(llvm::StringRef Id,int ExpectedCount)363   VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
364       : Id(std::string(Id)), ExpectedCount(ExpectedCount), Count(0) {}
365 
366   // Create an object that checks that a node of type \c T was bound to \c Id.
367   // Checks that there was exactly one match with the name \c ExpectedName.
368   // Note that \c T must be a NamedDecl for this to work.
369   VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName,
370                     int ExpectedCount = 1)
Id(std::string (Id))371       : Id(std::string(Id)), ExpectedCount(ExpectedCount), Count(0),
372         ExpectedName(std::string(ExpectedName)) {}
373 
onEndOfTranslationUnit()374   void onEndOfTranslationUnit() override {
375     if (ExpectedCount != -1) {
376       EXPECT_EQ(ExpectedCount, Count);
377     }
378     if (!ExpectedName.empty()) {
379       EXPECT_EQ(ExpectedName, Name);
380     }
381     Count = 0;
382     Name.clear();
383   }
384 
~VerifyIdIsBoundTo()385   ~VerifyIdIsBoundTo() override {
386     EXPECT_EQ(0, Count);
387     EXPECT_EQ("", Name);
388   }
389 
run(const BoundNodes * Nodes)390   bool run(const BoundNodes *Nodes) override {
391     const BoundNodes::IDToNodeMap &M = Nodes->getMap();
392     if (Nodes->getNodeAs<T>(Id)) {
393       ++Count;
394       if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
395         Name = Named->getNameAsString();
396       } else if (const NestedNameSpecifier *NNS =
397                      Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
398         llvm::raw_string_ostream OS(Name);
399         NNS->print(OS, PrintingPolicy(LangOptions()));
400       }
401       BoundNodes::IDToNodeMap::const_iterator I = M.find(Id);
402       EXPECT_NE(M.end(), I);
403       if (I != M.end()) {
404         EXPECT_EQ(Nodes->getNodeAs<T>(Id), I->second.get<T>());
405       }
406       return true;
407     }
408     EXPECT_TRUE(M.count(Id) == 0 ||
409                 M.find(Id)->second.template get<T>() == nullptr);
410     return false;
411   }
412 
run(const BoundNodes * Nodes,ASTContext * Context)413   bool run(const BoundNodes *Nodes, ASTContext *Context) override {
414     return run(Nodes);
415   }
416 
417 private:
418   const std::string Id;
419   const int ExpectedCount;
420   int Count;
421   const std::string ExpectedName;
422   std::string Name;
423 };
424 
425 class ASTMatchersTest : public ::testing::Test,
426                         public ::testing::WithParamInterface<TestClangConfig> {
427 protected:
428   template <typename T>
matches(const Twine & Code,const T & AMatcher)429   testing::AssertionResult matches(const Twine &Code, const T &AMatcher) {
430     const TestClangConfig &TestConfig = GetParam();
431     return clang::ast_matchers::matchesConditionally(
432         Code, AMatcher, /*ExpectMatch=*/true, TestConfig.getCommandLineArgs(),
433         FileContentMappings(), getFilenameForTesting(TestConfig.Language));
434   }
435 
436   template <typename T>
notMatches(const Twine & Code,const T & AMatcher)437   testing::AssertionResult notMatches(const Twine &Code, const T &AMatcher) {
438     const TestClangConfig &TestConfig = GetParam();
439     return clang::ast_matchers::matchesConditionally(
440         Code, AMatcher, /*ExpectMatch=*/false, TestConfig.getCommandLineArgs(),
441         FileContentMappings(), getFilenameForTesting(TestConfig.Language));
442   }
443 };
444 
445 } // namespace ast_matchers
446 } // namespace clang
447 
448 #endif // LLVM_CLANG_UNITTESTS_AST_MATCHERS_AST_MATCHERS_TEST_H
449