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