1 //===- unittests/AST/ASTTraverserTest.h------------------------------------===// 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/AST/ASTContext.h" 10 #include "clang/AST/ASTNodeTraverser.h" 11 #include "clang/AST/TextNodeDumper.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "clang/ASTMatchers/ASTMatchers.h" 14 #include "clang/Tooling/Tooling.h" 15 #include "gmock/gmock.h" 16 #include "gtest/gtest.h" 17 18 using namespace clang::tooling; 19 using namespace clang::ast_matchers; 20 21 namespace clang { 22 23 class NodeTreePrinter : public TextTreeStructure { 24 llvm::raw_ostream &OS; 25 26 public: 27 NodeTreePrinter(llvm::raw_ostream &OS) 28 : TextTreeStructure(OS, /* showColors */ false), OS(OS) {} 29 30 void Visit(const Decl *D) { 31 OS << D->getDeclKindName() << "Decl"; 32 if (auto *ND = dyn_cast<NamedDecl>(D)) { 33 OS << " '" << ND->getDeclName() << "'"; 34 } 35 } 36 37 void Visit(const Stmt *S) { 38 OS << S->getStmtClassName(); 39 if (auto *E = dyn_cast<DeclRefExpr>(S)) { 40 OS << " '" << E->getDecl()->getDeclName() << "'"; 41 } 42 } 43 44 void Visit(QualType QT) { 45 OS << "QualType " << QT.split().Quals.getAsString(); 46 } 47 48 void Visit(const Type *T) { OS << T->getTypeClassName() << "Type"; } 49 50 void Visit(const comments::Comment *C, const comments::FullComment *FC) { 51 OS << C->getCommentKindName(); 52 } 53 54 void Visit(const CXXCtorInitializer *Init) { OS << "CXXCtorInitializer"; } 55 56 void Visit(const Attr *A) { 57 switch (A->getKind()) { 58 #define ATTR(X) \ 59 case attr::X: \ 60 OS << #X; \ 61 break; 62 #include "clang/Basic/AttrList.inc" 63 } 64 OS << "Attr"; 65 } 66 67 void Visit(const OMPClause *C) { OS << "OMPClause"; } 68 void Visit(const TemplateArgument &A, SourceRange R = {}, 69 const Decl *From = nullptr, const char *Label = nullptr) { 70 OS << "TemplateArgument"; 71 switch (A.getKind()) { 72 case TemplateArgument::Type: { 73 OS << " type " << A.getAsType().getAsString(); 74 break; 75 } 76 default: 77 break; 78 } 79 } 80 81 template <typename... T> void Visit(T...) {} 82 }; 83 84 class TestASTDumper : public ASTNodeTraverser<TestASTDumper, NodeTreePrinter> { 85 86 NodeTreePrinter MyNodeRecorder; 87 88 public: 89 TestASTDumper(llvm::raw_ostream &OS) : MyNodeRecorder(OS) {} 90 NodeTreePrinter &doGetNodeDelegate() { return MyNodeRecorder; } 91 }; 92 93 template <typename... NodeType> std::string dumpASTString(NodeType &&... N) { 94 std::string Buffer; 95 llvm::raw_string_ostream OS(Buffer); 96 97 TestASTDumper Dumper(OS); 98 99 OS << "\n"; 100 101 Dumper.Visit(std::forward<NodeType &&>(N)...); 102 103 return OS.str(); 104 } 105 106 template <typename... NodeType> 107 std::string dumpASTString(TraversalKind TK, NodeType &&... N) { 108 std::string Buffer; 109 llvm::raw_string_ostream OS(Buffer); 110 111 TestASTDumper Dumper(OS); 112 Dumper.SetTraversalKind(TK); 113 114 OS << "\n"; 115 116 Dumper.Visit(std::forward<NodeType &&>(N)...); 117 118 return OS.str(); 119 } 120 121 const FunctionDecl *getFunctionNode(clang::ASTUnit *AST, 122 const std::string &Name) { 123 auto Result = ast_matchers::match(functionDecl(hasName(Name)).bind("fn"), 124 AST->getASTContext()); 125 EXPECT_EQ(Result.size(), 1u); 126 return Result[0].getNodeAs<FunctionDecl>("fn"); 127 } 128 129 template <typename T> struct Verifier { 130 static void withDynNode(T Node, const std::string &DumpString) { 131 EXPECT_EQ(dumpASTString(DynTypedNode::create(Node)), DumpString); 132 } 133 }; 134 135 template <typename T> struct Verifier<T *> { 136 static void withDynNode(T *Node, const std::string &DumpString) { 137 EXPECT_EQ(dumpASTString(DynTypedNode::create(*Node)), DumpString); 138 } 139 }; 140 141 template <typename T> 142 void verifyWithDynNode(T Node, const std::string &DumpString) { 143 EXPECT_EQ(dumpASTString(Node), DumpString); 144 145 Verifier<T>::withDynNode(Node, DumpString); 146 } 147 148 TEST(Traverse, Dump) { 149 150 auto AST = buildASTFromCode(R"cpp( 151 struct A { 152 int m_number; 153 154 /// CTor 155 A() : m_number(42) {} 156 157 [[nodiscard]] const int func() { 158 return 42; 159 } 160 161 }; 162 163 template<typename T> 164 struct templ 165 { 166 }; 167 168 template<> 169 struct templ<int> 170 { 171 }; 172 173 void parmvardecl_attr(struct A __attribute__((address_space(19)))*); 174 175 )cpp"); 176 177 const FunctionDecl *Func = getFunctionNode(AST.get(), "func"); 178 179 verifyWithDynNode(Func, 180 R"cpp( 181 CXXMethodDecl 'func' 182 |-CompoundStmt 183 | `-ReturnStmt 184 | `-IntegerLiteral 185 `-WarnUnusedResultAttr 186 )cpp"); 187 188 Stmt *Body = Func->getBody(); 189 190 verifyWithDynNode(Body, 191 R"cpp( 192 CompoundStmt 193 `-ReturnStmt 194 `-IntegerLiteral 195 )cpp"); 196 197 QualType QT = Func->getType(); 198 199 verifyWithDynNode(QT, 200 R"cpp( 201 FunctionProtoType 202 `-QualType const 203 `-BuiltinType 204 )cpp"); 205 206 const FunctionDecl *CTorFunc = getFunctionNode(AST.get(), "A"); 207 208 verifyWithDynNode(CTorFunc->getType(), 209 R"cpp( 210 FunctionProtoType 211 `-BuiltinType 212 )cpp"); 213 214 Attr *A = *Func->attr_begin(); 215 216 { 217 std::string expectedString = R"cpp( 218 WarnUnusedResultAttr 219 )cpp"; 220 221 EXPECT_EQ(dumpASTString(A), expectedString); 222 } 223 224 auto *CTor = dyn_cast<CXXConstructorDecl>(CTorFunc); 225 const CXXCtorInitializer *Init = *CTor->init_begin(); 226 227 verifyWithDynNode(Init, 228 R"cpp( 229 CXXCtorInitializer 230 `-IntegerLiteral 231 )cpp"); 232 233 const comments::FullComment *Comment = 234 AST->getASTContext().getLocalCommentForDeclUncached(CTorFunc); 235 { 236 std::string expectedString = R"cpp( 237 FullComment 238 `-ParagraphComment 239 `-TextComment 240 )cpp"; 241 EXPECT_EQ(dumpASTString(Comment, Comment), expectedString); 242 } 243 244 auto Result = ast_matchers::match( 245 classTemplateSpecializationDecl(hasName("templ")).bind("fn"), 246 AST->getASTContext()); 247 EXPECT_EQ(Result.size(), 1u); 248 auto Templ = Result[0].getNodeAs<ClassTemplateSpecializationDecl>("fn"); 249 250 TemplateArgument TA = Templ->getTemplateArgs()[0]; 251 252 verifyWithDynNode(TA, 253 R"cpp( 254 TemplateArgument type int 255 `-BuiltinType 256 )cpp"); 257 258 Func = getFunctionNode(AST.get(), "parmvardecl_attr"); 259 260 const auto *Parm = Func->getParamDecl(0); 261 const auto TL = Parm->getTypeSourceInfo()->getTypeLoc(); 262 ASSERT_TRUE(TL.getType()->isPointerType()); 263 264 const auto ATL = TL.getNextTypeLoc().getAs<AttributedTypeLoc>(); 265 const auto *AS = cast<AddressSpaceAttr>(ATL.getAttr()); 266 EXPECT_EQ(toTargetAddressSpace(static_cast<LangAS>(AS->getAddressSpace())), 267 19u); 268 } 269 270 TEST(Traverse, IgnoreUnlessSpelledInSourceVars) { 271 272 auto AST = buildASTFromCode(R"cpp( 273 274 struct String 275 { 276 String(const char*, int = -1) {} 277 278 int overloaded() const; 279 int& overloaded(); 280 }; 281 282 void stringConstruct() 283 { 284 String s = "foo"; 285 s = "bar"; 286 } 287 288 void overloadCall() 289 { 290 String s = "foo"; 291 (s).overloaded(); 292 } 293 294 struct C1 {}; 295 struct C2 { operator C1(); }; 296 297 void conversionOperator() 298 { 299 C2* c2; 300 C1 c1 = (*c2); 301 } 302 303 template <unsigned alignment> 304 void template_test() { 305 static_assert(alignment, ""); 306 } 307 void actual_template_test() { 308 template_test<4>(); 309 } 310 311 struct OneParamCtor { 312 explicit OneParamCtor(int); 313 }; 314 struct TwoParamCtor { 315 explicit TwoParamCtor(int, int); 316 }; 317 318 void varDeclCtors() { 319 { 320 auto var1 = OneParamCtor(5); 321 auto var2 = TwoParamCtor(6, 7); 322 } 323 { 324 OneParamCtor var3(5); 325 TwoParamCtor var4(6, 7); 326 } 327 int i = 0; 328 { 329 auto var5 = OneParamCtor(i); 330 auto var6 = TwoParamCtor(i, 7); 331 } 332 { 333 OneParamCtor var7(i); 334 TwoParamCtor var8(i, 7); 335 } 336 } 337 338 )cpp"); 339 340 { 341 auto FN = 342 ast_matchers::match(functionDecl(hasName("stringConstruct")).bind("fn"), 343 AST->getASTContext()); 344 EXPECT_EQ(FN.size(), 1u); 345 346 EXPECT_EQ(dumpASTString(TK_AsIs, FN[0].getNodeAs<Decl>("fn")), 347 R"cpp( 348 FunctionDecl 'stringConstruct' 349 `-CompoundStmt 350 |-DeclStmt 351 | `-VarDecl 's' 352 | `-ExprWithCleanups 353 | `-CXXConstructExpr 354 | `-MaterializeTemporaryExpr 355 | `-ImplicitCastExpr 356 | `-CXXConstructExpr 357 | |-ImplicitCastExpr 358 | | `-StringLiteral 359 | `-CXXDefaultArgExpr 360 `-ExprWithCleanups 361 `-CXXOperatorCallExpr 362 |-ImplicitCastExpr 363 | `-DeclRefExpr 'operator=' 364 |-DeclRefExpr 's' 365 `-MaterializeTemporaryExpr 366 `-CXXConstructExpr 367 |-ImplicitCastExpr 368 | `-StringLiteral 369 `-CXXDefaultArgExpr 370 )cpp"); 371 372 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, 373 FN[0].getNodeAs<Decl>("fn")), 374 R"cpp( 375 FunctionDecl 'stringConstruct' 376 `-CompoundStmt 377 |-DeclStmt 378 | `-VarDecl 's' 379 | `-StringLiteral 380 `-CXXOperatorCallExpr 381 |-DeclRefExpr 'operator=' 382 |-DeclRefExpr 's' 383 `-StringLiteral 384 )cpp"); 385 } 386 387 { 388 auto FN = 389 ast_matchers::match(functionDecl(hasName("overloadCall")).bind("fn"), 390 AST->getASTContext()); 391 EXPECT_EQ(FN.size(), 1u); 392 393 EXPECT_EQ(dumpASTString(TK_AsIs, FN[0].getNodeAs<Decl>("fn")), 394 R"cpp( 395 FunctionDecl 'overloadCall' 396 `-CompoundStmt 397 |-DeclStmt 398 | `-VarDecl 's' 399 | `-ExprWithCleanups 400 | `-CXXConstructExpr 401 | `-MaterializeTemporaryExpr 402 | `-ImplicitCastExpr 403 | `-CXXConstructExpr 404 | |-ImplicitCastExpr 405 | | `-StringLiteral 406 | `-CXXDefaultArgExpr 407 `-CXXMemberCallExpr 408 `-MemberExpr 409 `-ParenExpr 410 `-DeclRefExpr 's' 411 )cpp"); 412 413 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, 414 FN[0].getNodeAs<Decl>("fn")), 415 R"cpp( 416 FunctionDecl 'overloadCall' 417 `-CompoundStmt 418 |-DeclStmt 419 | `-VarDecl 's' 420 | `-StringLiteral 421 `-CXXMemberCallExpr 422 `-MemberExpr 423 `-DeclRefExpr 's' 424 )cpp"); 425 } 426 427 { 428 auto FN = ast_matchers::match( 429 functionDecl(hasName("conversionOperator"), 430 hasDescendant(varDecl(hasName("c1")).bind("var"))), 431 AST->getASTContext()); 432 EXPECT_EQ(FN.size(), 1u); 433 434 EXPECT_EQ(dumpASTString(TK_AsIs, FN[0].getNodeAs<Decl>("var")), 435 R"cpp( 436 VarDecl 'c1' 437 `-ExprWithCleanups 438 `-CXXConstructExpr 439 `-MaterializeTemporaryExpr 440 `-ImplicitCastExpr 441 `-CXXMemberCallExpr 442 `-MemberExpr 443 `-ParenExpr 444 `-UnaryOperator 445 `-ImplicitCastExpr 446 `-DeclRefExpr 'c2' 447 )cpp"); 448 449 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, 450 FN[0].getNodeAs<Decl>("var")), 451 R"cpp( 452 VarDecl 'c1' 453 `-UnaryOperator 454 `-DeclRefExpr 'c2' 455 )cpp"); 456 } 457 458 { 459 auto FN = ast_matchers::match( 460 functionDecl(hasName("template_test"), 461 hasDescendant(staticAssertDecl().bind("staticAssert"))), 462 AST->getASTContext()); 463 EXPECT_EQ(FN.size(), 2u); 464 465 EXPECT_EQ(dumpASTString(TK_AsIs, FN[1].getNodeAs<Decl>("staticAssert")), 466 R"cpp( 467 StaticAssertDecl 468 |-ImplicitCastExpr 469 | `-SubstNonTypeTemplateParmExpr 470 | |-NonTypeTemplateParmDecl 'alignment' 471 | `-IntegerLiteral 472 `-StringLiteral 473 )cpp"); 474 475 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, 476 FN[1].getNodeAs<Decl>("staticAssert")), 477 R"cpp( 478 StaticAssertDecl 479 |-IntegerLiteral 480 `-StringLiteral 481 )cpp"); 482 } 483 484 auto varChecker = [&AST](StringRef varName, StringRef SemanticDump, 485 StringRef SyntacticDump) { 486 auto FN = ast_matchers::match( 487 functionDecl( 488 hasName("varDeclCtors"), 489 forEachDescendant(varDecl(hasName(varName)).bind("varDeclCtor"))), 490 AST->getASTContext()); 491 EXPECT_EQ(FN.size(), 1u); 492 493 EXPECT_EQ(dumpASTString(TK_AsIs, FN[0].getNodeAs<Decl>("varDeclCtor")), 494 SemanticDump); 495 496 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, 497 FN[0].getNodeAs<Decl>("varDeclCtor")), 498 SyntacticDump); 499 }; 500 501 varChecker("var1", 502 R"cpp( 503 VarDecl 'var1' 504 `-ExprWithCleanups 505 `-CXXConstructExpr 506 `-MaterializeTemporaryExpr 507 `-CXXFunctionalCastExpr 508 `-CXXConstructExpr 509 `-IntegerLiteral 510 )cpp", 511 R"cpp( 512 VarDecl 'var1' 513 `-CXXConstructExpr 514 `-IntegerLiteral 515 )cpp"); 516 517 varChecker("var2", 518 R"cpp( 519 VarDecl 'var2' 520 `-ExprWithCleanups 521 `-CXXConstructExpr 522 `-MaterializeTemporaryExpr 523 `-CXXTemporaryObjectExpr 524 |-IntegerLiteral 525 `-IntegerLiteral 526 )cpp", 527 R"cpp( 528 VarDecl 'var2' 529 `-CXXTemporaryObjectExpr 530 |-IntegerLiteral 531 `-IntegerLiteral 532 )cpp"); 533 534 varChecker("var3", 535 R"cpp( 536 VarDecl 'var3' 537 `-CXXConstructExpr 538 `-IntegerLiteral 539 )cpp", 540 R"cpp( 541 VarDecl 'var3' 542 `-CXXConstructExpr 543 `-IntegerLiteral 544 )cpp"); 545 546 varChecker("var4", 547 R"cpp( 548 VarDecl 'var4' 549 `-CXXConstructExpr 550 |-IntegerLiteral 551 `-IntegerLiteral 552 )cpp", 553 R"cpp( 554 VarDecl 'var4' 555 `-CXXConstructExpr 556 |-IntegerLiteral 557 `-IntegerLiteral 558 )cpp"); 559 560 varChecker("var5", 561 R"cpp( 562 VarDecl 'var5' 563 `-ExprWithCleanups 564 `-CXXConstructExpr 565 `-MaterializeTemporaryExpr 566 `-CXXFunctionalCastExpr 567 `-CXXConstructExpr 568 `-ImplicitCastExpr 569 `-DeclRefExpr 'i' 570 )cpp", 571 R"cpp( 572 VarDecl 'var5' 573 `-CXXConstructExpr 574 `-DeclRefExpr 'i' 575 )cpp"); 576 577 varChecker("var6", 578 R"cpp( 579 VarDecl 'var6' 580 `-ExprWithCleanups 581 `-CXXConstructExpr 582 `-MaterializeTemporaryExpr 583 `-CXXTemporaryObjectExpr 584 |-ImplicitCastExpr 585 | `-DeclRefExpr 'i' 586 `-IntegerLiteral 587 )cpp", 588 R"cpp( 589 VarDecl 'var6' 590 `-CXXTemporaryObjectExpr 591 |-DeclRefExpr 'i' 592 `-IntegerLiteral 593 )cpp"); 594 595 varChecker("var7", 596 R"cpp( 597 VarDecl 'var7' 598 `-CXXConstructExpr 599 `-ImplicitCastExpr 600 `-DeclRefExpr 'i' 601 )cpp", 602 R"cpp( 603 VarDecl 'var7' 604 `-CXXConstructExpr 605 `-DeclRefExpr 'i' 606 )cpp"); 607 608 varChecker("var8", 609 R"cpp( 610 VarDecl 'var8' 611 `-CXXConstructExpr 612 |-ImplicitCastExpr 613 | `-DeclRefExpr 'i' 614 `-IntegerLiteral 615 )cpp", 616 R"cpp( 617 VarDecl 'var8' 618 `-CXXConstructExpr 619 |-DeclRefExpr 'i' 620 `-IntegerLiteral 621 )cpp"); 622 } 623 624 TEST(Traverse, IgnoreUnlessSpelledInSourceStructs) { 625 auto AST = buildASTFromCode(R"cpp( 626 627 struct MyStruct { 628 MyStruct(); 629 MyStruct(int i) { 630 MyStruct(); 631 } 632 ~MyStruct(); 633 }; 634 635 )cpp"); 636 637 auto BN = ast_matchers::match( 638 cxxConstructorDecl(hasName("MyStruct"), 639 hasParameter(0, parmVarDecl(hasType(isInteger())))) 640 .bind("ctor"), 641 AST->getASTContext()); 642 EXPECT_EQ(BN.size(), 1u); 643 644 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, 645 BN[0].getNodeAs<Decl>("ctor")), 646 R"cpp( 647 CXXConstructorDecl 'MyStruct' 648 |-ParmVarDecl 'i' 649 `-CompoundStmt 650 `-CXXTemporaryObjectExpr 651 )cpp"); 652 653 EXPECT_EQ(dumpASTString(TK_AsIs, BN[0].getNodeAs<Decl>("ctor")), 654 R"cpp( 655 CXXConstructorDecl 'MyStruct' 656 |-ParmVarDecl 'i' 657 `-CompoundStmt 658 `-ExprWithCleanups 659 `-CXXBindTemporaryExpr 660 `-CXXTemporaryObjectExpr 661 )cpp"); 662 } 663 664 TEST(Traverse, IgnoreUnlessSpelledInSourceReturnStruct) { 665 666 auto AST = buildASTFromCode(R"cpp( 667 struct Retval { 668 Retval() {} 669 ~Retval() {} 670 }; 671 672 Retval someFun(); 673 674 void foo() 675 { 676 someFun(); 677 } 678 )cpp"); 679 680 auto BN = ast_matchers::match(functionDecl(hasName("foo")).bind("fn"), 681 AST->getASTContext()); 682 EXPECT_EQ(BN.size(), 1u); 683 684 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, 685 BN[0].getNodeAs<Decl>("fn")), 686 R"cpp( 687 FunctionDecl 'foo' 688 `-CompoundStmt 689 `-CallExpr 690 `-DeclRefExpr 'someFun' 691 )cpp"); 692 693 EXPECT_EQ(dumpASTString(TK_AsIs, BN[0].getNodeAs<Decl>("fn")), 694 R"cpp( 695 FunctionDecl 'foo' 696 `-CompoundStmt 697 `-ExprWithCleanups 698 `-CXXBindTemporaryExpr 699 `-CallExpr 700 `-ImplicitCastExpr 701 `-DeclRefExpr 'someFun' 702 )cpp"); 703 } 704 705 TEST(Traverse, IgnoreUnlessSpelledInSourceReturns) { 706 707 auto AST = buildASTFromCode(R"cpp( 708 709 struct A 710 { 711 }; 712 713 struct B 714 { 715 B(int); 716 B(A const& a); 717 B(); 718 }; 719 720 struct C 721 { 722 operator B(); 723 }; 724 725 B func1() { 726 return 42; 727 } 728 729 B func2() { 730 return B{42}; 731 } 732 733 B func3() { 734 return B(42); 735 } 736 737 B func4() { 738 return B(); 739 } 740 741 B func5() { 742 return B{}; 743 } 744 745 B func6() { 746 return C(); 747 } 748 749 B func7() { 750 return A(); 751 } 752 753 B func8() { 754 return C{}; 755 } 756 757 B func9() { 758 return A{}; 759 } 760 761 B func10() { 762 A a; 763 return a; 764 } 765 766 B func11() { 767 B b; 768 return b; 769 } 770 771 B func12() { 772 C c; 773 return c; 774 } 775 776 )cpp"); 777 778 auto getFunctionNode = [&AST](const std::string &name) { 779 auto BN = ast_matchers::match(functionDecl(hasName(name)).bind("fn"), 780 AST->getASTContext()); 781 EXPECT_EQ(BN.size(), 1u); 782 return BN[0].getNodeAs<Decl>("fn"); 783 }; 784 785 { 786 auto FN = getFunctionNode("func1"); 787 llvm::StringRef Expected = R"cpp( 788 FunctionDecl 'func1' 789 `-CompoundStmt 790 `-ReturnStmt 791 `-ExprWithCleanups 792 `-CXXConstructExpr 793 `-MaterializeTemporaryExpr 794 `-ImplicitCastExpr 795 `-CXXConstructExpr 796 `-IntegerLiteral 797 )cpp"; 798 799 EXPECT_EQ(dumpASTString(TK_AsIs, FN), Expected); 800 801 Expected = R"cpp( 802 FunctionDecl 'func1' 803 `-CompoundStmt 804 `-ReturnStmt 805 `-IntegerLiteral 806 )cpp"; 807 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, FN), Expected); 808 } 809 810 llvm::StringRef Expected = R"cpp( 811 FunctionDecl 'func2' 812 `-CompoundStmt 813 `-ReturnStmt 814 `-CXXTemporaryObjectExpr 815 `-IntegerLiteral 816 )cpp"; 817 EXPECT_EQ( 818 dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func2")), 819 Expected); 820 821 Expected = R"cpp( 822 FunctionDecl 'func3' 823 `-CompoundStmt 824 `-ReturnStmt 825 `-CXXConstructExpr 826 `-IntegerLiteral 827 )cpp"; 828 EXPECT_EQ( 829 dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func3")), 830 Expected); 831 832 Expected = R"cpp( 833 FunctionDecl 'func4' 834 `-CompoundStmt 835 `-ReturnStmt 836 `-CXXTemporaryObjectExpr 837 )cpp"; 838 EXPECT_EQ( 839 dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func4")), 840 Expected); 841 842 Expected = R"cpp( 843 FunctionDecl 'func5' 844 `-CompoundStmt 845 `-ReturnStmt 846 `-CXXTemporaryObjectExpr 847 )cpp"; 848 EXPECT_EQ( 849 dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func5")), 850 Expected); 851 852 Expected = R"cpp( 853 FunctionDecl 'func6' 854 `-CompoundStmt 855 `-ReturnStmt 856 `-CXXTemporaryObjectExpr 857 )cpp"; 858 EXPECT_EQ( 859 dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func6")), 860 Expected); 861 862 Expected = R"cpp( 863 FunctionDecl 'func7' 864 `-CompoundStmt 865 `-ReturnStmt 866 `-CXXTemporaryObjectExpr 867 )cpp"; 868 EXPECT_EQ( 869 dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func7")), 870 Expected); 871 872 Expected = R"cpp( 873 FunctionDecl 'func8' 874 `-CompoundStmt 875 `-ReturnStmt 876 `-CXXFunctionalCastExpr 877 `-InitListExpr 878 )cpp"; 879 EXPECT_EQ( 880 dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func8")), 881 Expected); 882 883 Expected = R"cpp( 884 FunctionDecl 'func9' 885 `-CompoundStmt 886 `-ReturnStmt 887 `-CXXFunctionalCastExpr 888 `-InitListExpr 889 )cpp"; 890 EXPECT_EQ( 891 dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func9")), 892 Expected); 893 894 Expected = R"cpp( 895 FunctionDecl 'func10' 896 `-CompoundStmt 897 |-DeclStmt 898 | `-VarDecl 'a' 899 | `-CXXConstructExpr 900 `-ReturnStmt 901 `-DeclRefExpr 'a' 902 )cpp"; 903 EXPECT_EQ( 904 dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func10")), 905 Expected); 906 907 Expected = R"cpp( 908 FunctionDecl 'func11' 909 `-CompoundStmt 910 |-DeclStmt 911 | `-VarDecl 'b' 912 | `-CXXConstructExpr 913 `-ReturnStmt 914 `-DeclRefExpr 'b' 915 )cpp"; 916 EXPECT_EQ( 917 dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func11")), 918 Expected); 919 920 Expected = R"cpp( 921 FunctionDecl 'func12' 922 `-CompoundStmt 923 |-DeclStmt 924 | `-VarDecl 'c' 925 | `-CXXConstructExpr 926 `-ReturnStmt 927 `-DeclRefExpr 'c' 928 )cpp"; 929 EXPECT_EQ( 930 dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func12")), 931 Expected); 932 } 933 934 TEST(Traverse, LambdaUnlessSpelledInSource) { 935 936 auto AST = 937 buildASTFromCodeWithArgs(R"cpp( 938 939 void captures() { 940 int a = 0; 941 int b = 0; 942 int d = 0; 943 int f = 0; 944 945 [a, &b, c = d, &e = f](int g, int h = 42) {}; 946 } 947 948 void templated() { 949 int a = 0; 950 [a]<typename T>(T t) {}; 951 } 952 953 struct SomeStruct { 954 int a = 0; 955 void capture_this() { 956 [this]() {}; 957 } 958 void capture_this_copy() { 959 [self = *this]() {}; 960 } 961 }; 962 )cpp", 963 {"-Wno-unused-value", "-Wno-c++2a-extensions"}); 964 965 auto getLambdaNode = [&AST](const std::string &name) { 966 auto BN = ast_matchers::match( 967 lambdaExpr(hasAncestor(functionDecl(hasName(name)))).bind("lambda"), 968 AST->getASTContext()); 969 EXPECT_EQ(BN.size(), 1u); 970 return BN[0].getNodeAs<LambdaExpr>("lambda"); 971 }; 972 973 { 974 auto L = getLambdaNode("captures"); 975 976 llvm::StringRef Expected = R"cpp( 977 LambdaExpr 978 |-DeclRefExpr 'a' 979 |-DeclRefExpr 'b' 980 |-VarDecl 'c' 981 | `-DeclRefExpr 'd' 982 |-VarDecl 'e' 983 | `-DeclRefExpr 'f' 984 |-ParmVarDecl 'g' 985 |-ParmVarDecl 'h' 986 | `-IntegerLiteral 987 `-CompoundStmt 988 )cpp"; 989 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, L), Expected); 990 991 Expected = R"cpp( 992 LambdaExpr 993 |-CXXRecordDecl '' 994 | |-CXXMethodDecl 'operator()' 995 | | |-ParmVarDecl 'g' 996 | | |-ParmVarDecl 'h' 997 | | | `-IntegerLiteral 998 | | `-CompoundStmt 999 | |-FieldDecl '' 1000 | |-FieldDecl '' 1001 | |-FieldDecl '' 1002 | |-FieldDecl '' 1003 | `-CXXDestructorDecl '~' 1004 |-ImplicitCastExpr 1005 | `-DeclRefExpr 'a' 1006 |-DeclRefExpr 'b' 1007 |-ImplicitCastExpr 1008 | `-DeclRefExpr 'd' 1009 |-DeclRefExpr 'f' 1010 `-CompoundStmt 1011 )cpp"; 1012 EXPECT_EQ(dumpASTString(TK_AsIs, L), Expected); 1013 } 1014 1015 { 1016 auto L = getLambdaNode("templated"); 1017 1018 llvm::StringRef Expected = R"cpp( 1019 LambdaExpr 1020 |-DeclRefExpr 'a' 1021 |-TemplateTypeParmDecl 'T' 1022 |-ParmVarDecl 't' 1023 `-CompoundStmt 1024 )cpp"; 1025 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, L), Expected); 1026 } 1027 1028 { 1029 auto L = getLambdaNode("capture_this"); 1030 1031 llvm::StringRef Expected = R"cpp( 1032 LambdaExpr 1033 |-CXXThisExpr 1034 `-CompoundStmt 1035 )cpp"; 1036 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, L), Expected); 1037 } 1038 1039 { 1040 auto L = getLambdaNode("capture_this_copy"); 1041 1042 llvm::StringRef Expected = R"cpp( 1043 LambdaExpr 1044 |-VarDecl 'self' 1045 | `-UnaryOperator 1046 | `-CXXThisExpr 1047 `-CompoundStmt 1048 )cpp"; 1049 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, L), Expected); 1050 } 1051 } 1052 1053 TEST(Traverse, IgnoreUnlessSpelledInSourceTemplateInstantiations) { 1054 1055 auto AST = buildASTFromCode(R"cpp( 1056 template<typename T> 1057 struct TemplStruct { 1058 TemplStruct() {} 1059 ~TemplStruct() {} 1060 1061 private: 1062 T m_t; 1063 }; 1064 1065 template<typename T> 1066 T timesTwo(T input) 1067 { 1068 return input * 2; 1069 } 1070 1071 void instantiate() 1072 { 1073 TemplStruct<int> ti; 1074 TemplStruct<double> td; 1075 (void)timesTwo<int>(2); 1076 (void)timesTwo<double>(2); 1077 } 1078 1079 template class TemplStruct<float>; 1080 1081 extern template class TemplStruct<long>; 1082 1083 template<> class TemplStruct<bool> { 1084 TemplStruct() {} 1085 ~TemplStruct() {} 1086 1087 void foo() {} 1088 private: 1089 bool m_t; 1090 }; 1091 1092 // Explicit instantiation of template functions do not appear in the AST 1093 template float timesTwo(float); 1094 1095 template<> bool timesTwo<bool>(bool) { 1096 return true; 1097 } 1098 )cpp"); 1099 { 1100 auto BN = ast_matchers::match( 1101 classTemplateDecl(hasName("TemplStruct")).bind("rec"), 1102 AST->getASTContext()); 1103 EXPECT_EQ(BN.size(), 1u); 1104 1105 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, 1106 BN[0].getNodeAs<Decl>("rec")), 1107 R"cpp( 1108 ClassTemplateDecl 'TemplStruct' 1109 |-TemplateTypeParmDecl 'T' 1110 `-CXXRecordDecl 'TemplStruct' 1111 |-CXXRecordDecl 'TemplStruct' 1112 |-CXXConstructorDecl 'TemplStruct<T>' 1113 | `-CompoundStmt 1114 |-CXXDestructorDecl '~TemplStruct<T>' 1115 | `-CompoundStmt 1116 |-AccessSpecDecl 1117 `-FieldDecl 'm_t' 1118 )cpp"); 1119 1120 EXPECT_EQ(dumpASTString(TK_AsIs, BN[0].getNodeAs<Decl>("rec")), 1121 R"cpp( 1122 ClassTemplateDecl 'TemplStruct' 1123 |-TemplateTypeParmDecl 'T' 1124 |-CXXRecordDecl 'TemplStruct' 1125 | |-CXXRecordDecl 'TemplStruct' 1126 | |-CXXConstructorDecl 'TemplStruct<T>' 1127 | | `-CompoundStmt 1128 | |-CXXDestructorDecl '~TemplStruct<T>' 1129 | | `-CompoundStmt 1130 | |-AccessSpecDecl 1131 | `-FieldDecl 'm_t' 1132 |-ClassTemplateSpecializationDecl 'TemplStruct' 1133 | |-TemplateArgument type int 1134 | | `-BuiltinType 1135 | |-CXXRecordDecl 'TemplStruct' 1136 | |-CXXConstructorDecl 'TemplStruct' 1137 | | `-CompoundStmt 1138 | |-CXXDestructorDecl '~TemplStruct' 1139 | | `-CompoundStmt 1140 | |-AccessSpecDecl 1141 | |-FieldDecl 'm_t' 1142 | `-CXXConstructorDecl 'TemplStruct' 1143 | `-ParmVarDecl '' 1144 |-ClassTemplateSpecializationDecl 'TemplStruct' 1145 | |-TemplateArgument type double 1146 | | `-BuiltinType 1147 | |-CXXRecordDecl 'TemplStruct' 1148 | |-CXXConstructorDecl 'TemplStruct' 1149 | | `-CompoundStmt 1150 | |-CXXDestructorDecl '~TemplStruct' 1151 | | `-CompoundStmt 1152 | |-AccessSpecDecl 1153 | |-FieldDecl 'm_t' 1154 | `-CXXConstructorDecl 'TemplStruct' 1155 | `-ParmVarDecl '' 1156 |-ClassTemplateSpecializationDecl 'TemplStruct' 1157 | |-TemplateArgument type float 1158 | | `-BuiltinType 1159 | |-CXXRecordDecl 'TemplStruct' 1160 | |-CXXConstructorDecl 'TemplStruct' 1161 | | `-CompoundStmt 1162 | |-CXXDestructorDecl '~TemplStruct' 1163 | | `-CompoundStmt 1164 | |-AccessSpecDecl 1165 | `-FieldDecl 'm_t' 1166 |-ClassTemplateSpecializationDecl 'TemplStruct' 1167 | |-TemplateArgument type long 1168 | | `-BuiltinType 1169 | |-CXXRecordDecl 'TemplStruct' 1170 | |-CXXConstructorDecl 'TemplStruct' 1171 | |-CXXDestructorDecl '~TemplStruct' 1172 | |-AccessSpecDecl 1173 | `-FieldDecl 'm_t' 1174 `-ClassTemplateSpecializationDecl 'TemplStruct' 1175 |-TemplateArgument type _Bool 1176 | `-BuiltinType 1177 |-CXXRecordDecl 'TemplStruct' 1178 |-CXXConstructorDecl 'TemplStruct' 1179 | `-CompoundStmt 1180 |-CXXDestructorDecl '~TemplStruct' 1181 | `-CompoundStmt 1182 |-CXXMethodDecl 'foo' 1183 | `-CompoundStmt 1184 |-AccessSpecDecl 1185 `-FieldDecl 'm_t' 1186 )cpp"); 1187 } 1188 { 1189 auto BN = ast_matchers::match( 1190 classTemplateSpecializationDecl( 1191 hasTemplateArgument( 1192 0, templateArgument(refersToType(asString("_Bool"))))) 1193 .bind("templSpec"), 1194 AST->getASTContext()); 1195 EXPECT_EQ(BN.size(), 1u); 1196 1197 EXPECT_EQ(dumpASTString(TK_AsIs, BN[0].getNodeAs<Decl>("templSpec")), 1198 R"cpp( 1199 ClassTemplateSpecializationDecl 'TemplStruct' 1200 |-TemplateArgument type _Bool 1201 | `-BuiltinType 1202 |-CXXRecordDecl 'TemplStruct' 1203 |-CXXConstructorDecl 'TemplStruct' 1204 | `-CompoundStmt 1205 |-CXXDestructorDecl '~TemplStruct' 1206 | `-CompoundStmt 1207 |-CXXMethodDecl 'foo' 1208 | `-CompoundStmt 1209 |-AccessSpecDecl 1210 `-FieldDecl 'm_t' 1211 )cpp"); 1212 1213 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, 1214 BN[0].getNodeAs<Decl>("templSpec")), 1215 R"cpp( 1216 ClassTemplateSpecializationDecl 'TemplStruct' 1217 |-TemplateArgument type _Bool 1218 | `-BuiltinType 1219 |-CXXRecordDecl 'TemplStruct' 1220 |-CXXConstructorDecl 'TemplStruct' 1221 | `-CompoundStmt 1222 |-CXXDestructorDecl '~TemplStruct' 1223 | `-CompoundStmt 1224 |-CXXMethodDecl 'foo' 1225 | `-CompoundStmt 1226 |-AccessSpecDecl 1227 `-FieldDecl 'm_t' 1228 )cpp"); 1229 } 1230 { 1231 auto BN = ast_matchers::match( 1232 functionTemplateDecl(hasName("timesTwo")).bind("fn"), 1233 AST->getASTContext()); 1234 EXPECT_EQ(BN.size(), 1u); 1235 1236 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, 1237 BN[0].getNodeAs<Decl>("fn")), 1238 R"cpp( 1239 FunctionTemplateDecl 'timesTwo' 1240 |-TemplateTypeParmDecl 'T' 1241 `-FunctionDecl 'timesTwo' 1242 |-ParmVarDecl 'input' 1243 `-CompoundStmt 1244 `-ReturnStmt 1245 `-BinaryOperator 1246 |-DeclRefExpr 'input' 1247 `-IntegerLiteral 1248 )cpp"); 1249 1250 EXPECT_EQ(dumpASTString(TK_AsIs, BN[0].getNodeAs<Decl>("fn")), 1251 R"cpp( 1252 FunctionTemplateDecl 'timesTwo' 1253 |-TemplateTypeParmDecl 'T' 1254 |-FunctionDecl 'timesTwo' 1255 | |-ParmVarDecl 'input' 1256 | `-CompoundStmt 1257 | `-ReturnStmt 1258 | `-BinaryOperator 1259 | |-DeclRefExpr 'input' 1260 | `-IntegerLiteral 1261 |-FunctionDecl 'timesTwo' 1262 | |-TemplateArgument type int 1263 | | `-BuiltinType 1264 | |-ParmVarDecl 'input' 1265 | `-CompoundStmt 1266 | `-ReturnStmt 1267 | `-BinaryOperator 1268 | |-ImplicitCastExpr 1269 | | `-DeclRefExpr 'input' 1270 | `-IntegerLiteral 1271 |-FunctionDecl 'timesTwo' 1272 | |-TemplateArgument type double 1273 | | `-BuiltinType 1274 | |-ParmVarDecl 'input' 1275 | `-CompoundStmt 1276 | `-ReturnStmt 1277 | `-BinaryOperator 1278 | |-ImplicitCastExpr 1279 | | `-DeclRefExpr 'input' 1280 | `-ImplicitCastExpr 1281 | `-IntegerLiteral 1282 |-FunctionDecl 'timesTwo' 1283 | |-TemplateArgument type float 1284 | | `-BuiltinType 1285 | |-ParmVarDecl 'input' 1286 | `-CompoundStmt 1287 | `-ReturnStmt 1288 | `-BinaryOperator 1289 | |-ImplicitCastExpr 1290 | | `-DeclRefExpr 'input' 1291 | `-ImplicitCastExpr 1292 | `-IntegerLiteral 1293 |-FunctionDecl 'timesTwo' 1294 | |-TemplateArgument type _Bool 1295 | | `-BuiltinType 1296 | |-ParmVarDecl '' 1297 | `-CompoundStmt 1298 | `-ReturnStmt 1299 | `-CXXBoolLiteralExpr 1300 `-FunctionDecl 'timesTwo' 1301 |-TemplateArgument type _Bool 1302 | `-BuiltinType 1303 `-ParmVarDecl 'input' 1304 )cpp"); 1305 } 1306 { 1307 auto BN = ast_matchers::match( 1308 classTemplateSpecializationDecl( 1309 hasName("TemplStruct"), 1310 hasTemplateArgument( 1311 0, templateArgument(refersToType(asString("float")))), 1312 hasParent(translationUnitDecl())) 1313 .bind("rec"), 1314 AST->getASTContext()); 1315 EXPECT_EQ(BN.size(), 1u); 1316 1317 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, 1318 BN[0].getNodeAs<Decl>("rec")), 1319 R"cpp( 1320 ClassTemplateSpecializationDecl 'TemplStruct' 1321 `-TemplateArgument type float 1322 `-BuiltinType 1323 )cpp"); 1324 1325 EXPECT_EQ(dumpASTString(TK_AsIs, BN[0].getNodeAs<Decl>("rec")), 1326 R"cpp( 1327 ClassTemplateSpecializationDecl 'TemplStruct' 1328 |-TemplateArgument type float 1329 | `-BuiltinType 1330 |-CXXRecordDecl 'TemplStruct' 1331 |-CXXConstructorDecl 'TemplStruct' 1332 | `-CompoundStmt 1333 |-CXXDestructorDecl '~TemplStruct' 1334 | `-CompoundStmt 1335 |-AccessSpecDecl 1336 `-FieldDecl 'm_t' 1337 )cpp"); 1338 } 1339 } 1340 1341 } // namespace clang 1342