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 } 72 73 template <typename... T> void Visit(T...) {} 74 }; 75 76 class TestASTDumper : public ASTNodeTraverser<TestASTDumper, NodeTreePrinter> { 77 78 NodeTreePrinter MyNodeRecorder; 79 80 public: 81 TestASTDumper(llvm::raw_ostream &OS) : MyNodeRecorder(OS) {} 82 NodeTreePrinter &doGetNodeDelegate() { return MyNodeRecorder; } 83 }; 84 85 template <typename... NodeType> std::string dumpASTString(NodeType &&... N) { 86 std::string Buffer; 87 llvm::raw_string_ostream OS(Buffer); 88 89 TestASTDumper Dumper(OS); 90 91 OS << "\n"; 92 93 Dumper.Visit(std::forward<NodeType &&>(N)...); 94 95 return OS.str(); 96 } 97 98 template <typename... NodeType> 99 std::string dumpASTString(TraversalKind TK, NodeType &&... N) { 100 std::string Buffer; 101 llvm::raw_string_ostream OS(Buffer); 102 103 TestASTDumper Dumper(OS); 104 Dumper.SetTraversalKind(TK); 105 106 OS << "\n"; 107 108 Dumper.Visit(std::forward<NodeType &&>(N)...); 109 110 return OS.str(); 111 } 112 113 const FunctionDecl *getFunctionNode(clang::ASTUnit *AST, 114 const std::string &Name) { 115 auto Result = ast_matchers::match(functionDecl(hasName(Name)).bind("fn"), 116 AST->getASTContext()); 117 EXPECT_EQ(Result.size(), 1u); 118 return Result[0].getNodeAs<FunctionDecl>("fn"); 119 } 120 121 template <typename T> struct Verifier { 122 static void withDynNode(T Node, const std::string &DumpString) { 123 EXPECT_EQ(dumpASTString(DynTypedNode::create(Node)), DumpString); 124 } 125 }; 126 127 template <typename T> struct Verifier<T *> { 128 static void withDynNode(T *Node, const std::string &DumpString) { 129 EXPECT_EQ(dumpASTString(DynTypedNode::create(*Node)), DumpString); 130 } 131 }; 132 133 template <typename T> 134 void verifyWithDynNode(T Node, const std::string &DumpString) { 135 EXPECT_EQ(dumpASTString(Node), DumpString); 136 137 Verifier<T>::withDynNode(Node, DumpString); 138 } 139 140 TEST(Traverse, Dump) { 141 142 auto AST = buildASTFromCode(R"cpp( 143 struct A { 144 int m_number; 145 146 /// CTor 147 A() : m_number(42) {} 148 149 [[nodiscard]] const int func() { 150 return 42; 151 } 152 153 }; 154 155 template<typename T> 156 struct templ 157 { 158 }; 159 160 template<> 161 struct templ<int> 162 { 163 }; 164 165 void parmvardecl_attr(struct A __attribute__((address_space(19)))*); 166 167 )cpp"); 168 169 const FunctionDecl *Func = getFunctionNode(AST.get(), "func"); 170 171 verifyWithDynNode(Func, 172 R"cpp( 173 CXXMethodDecl 'func' 174 |-CompoundStmt 175 | `-ReturnStmt 176 | `-IntegerLiteral 177 `-WarnUnusedResultAttr 178 )cpp"); 179 180 Stmt *Body = Func->getBody(); 181 182 verifyWithDynNode(Body, 183 R"cpp( 184 CompoundStmt 185 `-ReturnStmt 186 `-IntegerLiteral 187 )cpp"); 188 189 QualType QT = Func->getType(); 190 191 verifyWithDynNode(QT, 192 R"cpp( 193 FunctionProtoType 194 `-QualType const 195 `-BuiltinType 196 )cpp"); 197 198 const FunctionDecl *CTorFunc = getFunctionNode(AST.get(), "A"); 199 200 verifyWithDynNode(CTorFunc->getType(), 201 R"cpp( 202 FunctionProtoType 203 `-BuiltinType 204 )cpp"); 205 206 Attr *A = *Func->attr_begin(); 207 208 { 209 std::string expectedString = R"cpp( 210 WarnUnusedResultAttr 211 )cpp"; 212 213 EXPECT_EQ(dumpASTString(A), expectedString); 214 } 215 216 auto *CTor = dyn_cast<CXXConstructorDecl>(CTorFunc); 217 const CXXCtorInitializer *Init = *CTor->init_begin(); 218 219 verifyWithDynNode(Init, 220 R"cpp( 221 CXXCtorInitializer 222 `-IntegerLiteral 223 )cpp"); 224 225 const comments::FullComment *Comment = 226 AST->getASTContext().getLocalCommentForDeclUncached(CTorFunc); 227 { 228 std::string expectedString = R"cpp( 229 FullComment 230 `-ParagraphComment 231 `-TextComment 232 )cpp"; 233 EXPECT_EQ(dumpASTString(Comment, Comment), expectedString); 234 } 235 236 auto Result = ast_matchers::match( 237 classTemplateSpecializationDecl(hasName("templ")).bind("fn"), 238 AST->getASTContext()); 239 EXPECT_EQ(Result.size(), 1u); 240 auto Templ = Result[0].getNodeAs<ClassTemplateSpecializationDecl>("fn"); 241 242 TemplateArgument TA = Templ->getTemplateArgs()[0]; 243 244 verifyWithDynNode(TA, 245 R"cpp( 246 TemplateArgument 247 `-BuiltinType 248 )cpp"); 249 250 Func = getFunctionNode(AST.get(), "parmvardecl_attr"); 251 252 const auto *Parm = Func->getParamDecl(0); 253 const auto TL = Parm->getTypeSourceInfo()->getTypeLoc(); 254 ASSERT_TRUE(TL.getType()->isPointerType()); 255 256 const auto ATL = TL.getNextTypeLoc().getAs<AttributedTypeLoc>(); 257 const auto *AS = cast<AddressSpaceAttr>(ATL.getAttr()); 258 EXPECT_EQ(toTargetAddressSpace(static_cast<LangAS>(AS->getAddressSpace())), 259 19u); 260 } 261 262 TEST(Traverse, IgnoreUnlessSpelledInSourceVars) { 263 264 auto AST = buildASTFromCode(R"cpp( 265 266 struct String 267 { 268 String(const char*, int = -1) {} 269 270 int overloaded() const; 271 int& overloaded(); 272 }; 273 274 void stringConstruct() 275 { 276 String s = "foo"; 277 s = "bar"; 278 } 279 280 void overloadCall() 281 { 282 String s = "foo"; 283 (s).overloaded(); 284 } 285 286 struct C1 {}; 287 struct C2 { operator C1(); }; 288 289 void conversionOperator() 290 { 291 C2* c2; 292 C1 c1 = (*c2); 293 } 294 295 template <unsigned alignment> 296 void template_test() { 297 static_assert(alignment, ""); 298 } 299 void actual_template_test() { 300 template_test<4>(); 301 } 302 )cpp"); 303 304 { 305 auto FN = 306 ast_matchers::match(functionDecl(hasName("stringConstruct")).bind("fn"), 307 AST->getASTContext()); 308 EXPECT_EQ(FN.size(), 1u); 309 310 EXPECT_EQ(dumpASTString(TK_AsIs, FN[0].getNodeAs<Decl>("fn")), 311 R"cpp( 312 FunctionDecl 'stringConstruct' 313 `-CompoundStmt 314 |-DeclStmt 315 | `-VarDecl 's' 316 | `-ExprWithCleanups 317 | `-CXXConstructExpr 318 | `-MaterializeTemporaryExpr 319 | `-ImplicitCastExpr 320 | `-CXXConstructExpr 321 | |-ImplicitCastExpr 322 | | `-StringLiteral 323 | `-CXXDefaultArgExpr 324 `-ExprWithCleanups 325 `-CXXOperatorCallExpr 326 |-ImplicitCastExpr 327 | `-DeclRefExpr 'operator=' 328 |-DeclRefExpr 's' 329 `-MaterializeTemporaryExpr 330 `-CXXConstructExpr 331 |-ImplicitCastExpr 332 | `-StringLiteral 333 `-CXXDefaultArgExpr 334 )cpp"); 335 336 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, 337 FN[0].getNodeAs<Decl>("fn")), 338 R"cpp( 339 FunctionDecl 'stringConstruct' 340 `-CompoundStmt 341 |-DeclStmt 342 | `-VarDecl 's' 343 | `-StringLiteral 344 `-CXXOperatorCallExpr 345 |-DeclRefExpr 'operator=' 346 |-DeclRefExpr 's' 347 `-StringLiteral 348 )cpp"); 349 } 350 351 { 352 auto FN = 353 ast_matchers::match(functionDecl(hasName("overloadCall")).bind("fn"), 354 AST->getASTContext()); 355 EXPECT_EQ(FN.size(), 1u); 356 357 EXPECT_EQ(dumpASTString(TK_AsIs, FN[0].getNodeAs<Decl>("fn")), 358 R"cpp( 359 FunctionDecl 'overloadCall' 360 `-CompoundStmt 361 |-DeclStmt 362 | `-VarDecl 's' 363 | `-ExprWithCleanups 364 | `-CXXConstructExpr 365 | `-MaterializeTemporaryExpr 366 | `-ImplicitCastExpr 367 | `-CXXConstructExpr 368 | |-ImplicitCastExpr 369 | | `-StringLiteral 370 | `-CXXDefaultArgExpr 371 `-CXXMemberCallExpr 372 `-MemberExpr 373 `-ParenExpr 374 `-DeclRefExpr 's' 375 )cpp"); 376 377 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, 378 FN[0].getNodeAs<Decl>("fn")), 379 R"cpp( 380 FunctionDecl 'overloadCall' 381 `-CompoundStmt 382 |-DeclStmt 383 | `-VarDecl 's' 384 | `-StringLiteral 385 `-CXXMemberCallExpr 386 `-MemberExpr 387 `-DeclRefExpr 's' 388 )cpp"); 389 } 390 391 { 392 auto FN = ast_matchers::match( 393 functionDecl(hasName("conversionOperator"), 394 hasDescendant(varDecl(hasName("c1")).bind("var"))), 395 AST->getASTContext()); 396 EXPECT_EQ(FN.size(), 1u); 397 398 EXPECT_EQ(dumpASTString(TK_AsIs, FN[0].getNodeAs<Decl>("var")), 399 R"cpp( 400 VarDecl 'c1' 401 `-ExprWithCleanups 402 `-CXXConstructExpr 403 `-MaterializeTemporaryExpr 404 `-ImplicitCastExpr 405 `-CXXMemberCallExpr 406 `-MemberExpr 407 `-ParenExpr 408 `-UnaryOperator 409 `-ImplicitCastExpr 410 `-DeclRefExpr 'c2' 411 )cpp"); 412 413 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, 414 FN[0].getNodeAs<Decl>("var")), 415 R"cpp( 416 VarDecl 'c1' 417 `-UnaryOperator 418 `-DeclRefExpr 'c2' 419 )cpp"); 420 } 421 422 { 423 auto FN = ast_matchers::match( 424 functionDecl(hasName("template_test"), 425 hasDescendant(staticAssertDecl().bind("staticAssert"))), 426 AST->getASTContext()); 427 EXPECT_EQ(FN.size(), 2u); 428 429 EXPECT_EQ(dumpASTString(TK_AsIs, FN[1].getNodeAs<Decl>("staticAssert")), 430 R"cpp( 431 StaticAssertDecl 432 |-ImplicitCastExpr 433 | `-SubstNonTypeTemplateParmExpr 434 | |-NonTypeTemplateParmDecl 'alignment' 435 | `-IntegerLiteral 436 `-StringLiteral 437 )cpp"); 438 439 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, 440 FN[1].getNodeAs<Decl>("staticAssert")), 441 R"cpp( 442 StaticAssertDecl 443 |-IntegerLiteral 444 `-StringLiteral 445 )cpp"); 446 } 447 } 448 449 TEST(Traverse, IgnoreUnlessSpelledInSourceStructs) { 450 auto AST = buildASTFromCode(R"cpp( 451 452 struct MyStruct { 453 MyStruct(); 454 MyStruct(int i) { 455 MyStruct(); 456 } 457 ~MyStruct(); 458 }; 459 460 )cpp"); 461 462 auto BN = ast_matchers::match( 463 cxxConstructorDecl(hasName("MyStruct"), 464 hasParameter(0, parmVarDecl(hasType(isInteger())))) 465 .bind("ctor"), 466 AST->getASTContext()); 467 EXPECT_EQ(BN.size(), 1u); 468 469 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, 470 BN[0].getNodeAs<Decl>("ctor")), 471 R"cpp( 472 CXXConstructorDecl 'MyStruct' 473 |-ParmVarDecl 'i' 474 `-CompoundStmt 475 `-CXXTemporaryObjectExpr 476 )cpp"); 477 478 EXPECT_EQ(dumpASTString(TK_AsIs, BN[0].getNodeAs<Decl>("ctor")), 479 R"cpp( 480 CXXConstructorDecl 'MyStruct' 481 |-ParmVarDecl 'i' 482 `-CompoundStmt 483 `-ExprWithCleanups 484 `-CXXBindTemporaryExpr 485 `-CXXTemporaryObjectExpr 486 )cpp"); 487 } 488 489 TEST(Traverse, IgnoreUnlessSpelledInSourceReturnStruct) { 490 491 auto AST = buildASTFromCode(R"cpp( 492 struct Retval { 493 Retval() {} 494 ~Retval() {} 495 }; 496 497 Retval someFun(); 498 499 void foo() 500 { 501 someFun(); 502 } 503 )cpp"); 504 505 auto BN = ast_matchers::match(functionDecl(hasName("foo")).bind("fn"), 506 AST->getASTContext()); 507 EXPECT_EQ(BN.size(), 1u); 508 509 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, 510 BN[0].getNodeAs<Decl>("fn")), 511 R"cpp( 512 FunctionDecl 'foo' 513 `-CompoundStmt 514 `-CallExpr 515 `-DeclRefExpr 'someFun' 516 )cpp"); 517 518 EXPECT_EQ(dumpASTString(TK_AsIs, BN[0].getNodeAs<Decl>("fn")), 519 R"cpp( 520 FunctionDecl 'foo' 521 `-CompoundStmt 522 `-ExprWithCleanups 523 `-CXXBindTemporaryExpr 524 `-CallExpr 525 `-ImplicitCastExpr 526 `-DeclRefExpr 'someFun' 527 )cpp"); 528 } 529 530 TEST(Traverse, IgnoreUnlessSpelledInSourceReturns) { 531 532 auto AST = buildASTFromCode(R"cpp( 533 534 struct A 535 { 536 }; 537 538 struct B 539 { 540 B(int); 541 B(A const& a); 542 B(); 543 }; 544 545 struct C 546 { 547 operator B(); 548 }; 549 550 B func1() { 551 return 42; 552 } 553 554 B func2() { 555 return B{42}; 556 } 557 558 B func3() { 559 return B(42); 560 } 561 562 B func4() { 563 return B(); 564 } 565 566 B func5() { 567 return B{}; 568 } 569 570 B func6() { 571 return C(); 572 } 573 574 B func7() { 575 return A(); 576 } 577 578 B func8() { 579 return C{}; 580 } 581 582 B func9() { 583 return A{}; 584 } 585 586 B func10() { 587 A a; 588 return a; 589 } 590 591 B func11() { 592 B b; 593 return b; 594 } 595 596 B func12() { 597 C c; 598 return c; 599 } 600 601 )cpp"); 602 603 auto getFunctionNode = [&AST](const std::string &name) { 604 auto BN = ast_matchers::match(functionDecl(hasName(name)).bind("fn"), 605 AST->getASTContext()); 606 EXPECT_EQ(BN.size(), 1u); 607 return BN[0].getNodeAs<Decl>("fn"); 608 }; 609 610 { 611 auto FN = getFunctionNode("func1"); 612 llvm::StringRef Expected = R"cpp( 613 FunctionDecl 'func1' 614 `-CompoundStmt 615 `-ReturnStmt 616 `-ExprWithCleanups 617 `-CXXConstructExpr 618 `-MaterializeTemporaryExpr 619 `-ImplicitCastExpr 620 `-CXXConstructExpr 621 `-IntegerLiteral 622 )cpp"; 623 624 EXPECT_EQ(dumpASTString(TK_AsIs, FN), Expected); 625 626 Expected = R"cpp( 627 FunctionDecl 'func1' 628 `-CompoundStmt 629 `-ReturnStmt 630 `-IntegerLiteral 631 )cpp"; 632 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, FN), Expected); 633 } 634 635 llvm::StringRef Expected = R"cpp( 636 FunctionDecl 'func2' 637 `-CompoundStmt 638 `-ReturnStmt 639 `-CXXTemporaryObjectExpr 640 `-IntegerLiteral 641 )cpp"; 642 EXPECT_EQ( 643 dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func2")), 644 Expected); 645 646 Expected = R"cpp( 647 FunctionDecl 'func3' 648 `-CompoundStmt 649 `-ReturnStmt 650 `-CXXFunctionalCastExpr 651 `-IntegerLiteral 652 )cpp"; 653 EXPECT_EQ( 654 dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func3")), 655 Expected); 656 657 Expected = R"cpp( 658 FunctionDecl 'func4' 659 `-CompoundStmt 660 `-ReturnStmt 661 `-CXXTemporaryObjectExpr 662 )cpp"; 663 EXPECT_EQ( 664 dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func4")), 665 Expected); 666 667 Expected = R"cpp( 668 FunctionDecl 'func5' 669 `-CompoundStmt 670 `-ReturnStmt 671 `-CXXTemporaryObjectExpr 672 )cpp"; 673 EXPECT_EQ( 674 dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func5")), 675 Expected); 676 677 Expected = R"cpp( 678 FunctionDecl 'func6' 679 `-CompoundStmt 680 `-ReturnStmt 681 `-CXXTemporaryObjectExpr 682 )cpp"; 683 EXPECT_EQ( 684 dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func6")), 685 Expected); 686 687 Expected = R"cpp( 688 FunctionDecl 'func7' 689 `-CompoundStmt 690 `-ReturnStmt 691 `-CXXTemporaryObjectExpr 692 )cpp"; 693 EXPECT_EQ( 694 dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func7")), 695 Expected); 696 697 Expected = R"cpp( 698 FunctionDecl 'func8' 699 `-CompoundStmt 700 `-ReturnStmt 701 `-CXXFunctionalCastExpr 702 `-InitListExpr 703 )cpp"; 704 EXPECT_EQ( 705 dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func8")), 706 Expected); 707 708 Expected = R"cpp( 709 FunctionDecl 'func9' 710 `-CompoundStmt 711 `-ReturnStmt 712 `-CXXFunctionalCastExpr 713 `-InitListExpr 714 )cpp"; 715 EXPECT_EQ( 716 dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func9")), 717 Expected); 718 719 Expected = R"cpp( 720 FunctionDecl 'func10' 721 `-CompoundStmt 722 |-DeclStmt 723 | `-VarDecl 'a' 724 | `-CXXConstructExpr 725 `-ReturnStmt 726 `-DeclRefExpr 'a' 727 )cpp"; 728 EXPECT_EQ( 729 dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func10")), 730 Expected); 731 732 Expected = R"cpp( 733 FunctionDecl 'func11' 734 `-CompoundStmt 735 |-DeclStmt 736 | `-VarDecl 'b' 737 | `-CXXConstructExpr 738 `-ReturnStmt 739 `-DeclRefExpr 'b' 740 )cpp"; 741 EXPECT_EQ( 742 dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func11")), 743 Expected); 744 745 Expected = R"cpp( 746 FunctionDecl 'func12' 747 `-CompoundStmt 748 |-DeclStmt 749 | `-VarDecl 'c' 750 | `-CXXConstructExpr 751 `-ReturnStmt 752 `-DeclRefExpr 'c' 753 )cpp"; 754 EXPECT_EQ( 755 dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func12")), 756 Expected); 757 } 758 759 TEST(Traverse, LambdaUnlessSpelledInSource) { 760 761 auto AST = 762 buildASTFromCodeWithArgs(R"cpp( 763 764 void captures() { 765 int a = 0; 766 int b = 0; 767 int d = 0; 768 int f = 0; 769 770 [a, &b, c = d, &e = f](int g, int h = 42) {}; 771 } 772 773 void templated() { 774 int a = 0; 775 [a]<typename T>(T t) {}; 776 } 777 778 struct SomeStruct { 779 int a = 0; 780 void capture_this() { 781 [this]() {}; 782 } 783 void capture_this_copy() { 784 [self = *this]() {}; 785 } 786 }; 787 )cpp", 788 {"-Wno-unused-value", "-Wno-c++2a-extensions"}); 789 790 auto getLambdaNode = [&AST](const std::string &name) { 791 auto BN = ast_matchers::match( 792 lambdaExpr(hasAncestor(functionDecl(hasName(name)))).bind("lambda"), 793 AST->getASTContext()); 794 EXPECT_EQ(BN.size(), 1u); 795 return BN[0].getNodeAs<LambdaExpr>("lambda"); 796 }; 797 798 { 799 auto L = getLambdaNode("captures"); 800 801 llvm::StringRef Expected = R"cpp( 802 LambdaExpr 803 |-DeclRefExpr 'a' 804 |-DeclRefExpr 'b' 805 |-VarDecl 'c' 806 | `-DeclRefExpr 'd' 807 |-VarDecl 'e' 808 | `-DeclRefExpr 'f' 809 |-ParmVarDecl 'g' 810 |-ParmVarDecl 'h' 811 | `-IntegerLiteral 812 `-CompoundStmt 813 )cpp"; 814 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, L), Expected); 815 816 Expected = R"cpp( 817 LambdaExpr 818 |-CXXRecordDecl '' 819 | |-CXXMethodDecl 'operator()' 820 | | |-ParmVarDecl 'g' 821 | | |-ParmVarDecl 'h' 822 | | | `-IntegerLiteral 823 | | `-CompoundStmt 824 | |-FieldDecl '' 825 | |-FieldDecl '' 826 | |-FieldDecl '' 827 | |-FieldDecl '' 828 | `-CXXDestructorDecl '~' 829 |-ImplicitCastExpr 830 | `-DeclRefExpr 'a' 831 |-DeclRefExpr 'b' 832 |-ImplicitCastExpr 833 | `-DeclRefExpr 'd' 834 |-DeclRefExpr 'f' 835 `-CompoundStmt 836 )cpp"; 837 EXPECT_EQ(dumpASTString(TK_AsIs, L), Expected); 838 } 839 840 { 841 auto L = getLambdaNode("templated"); 842 843 llvm::StringRef Expected = R"cpp( 844 LambdaExpr 845 |-DeclRefExpr 'a' 846 |-TemplateTypeParmDecl 'T' 847 |-ParmVarDecl 't' 848 `-CompoundStmt 849 )cpp"; 850 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, L), Expected); 851 } 852 853 { 854 auto L = getLambdaNode("capture_this"); 855 856 llvm::StringRef Expected = R"cpp( 857 LambdaExpr 858 |-CXXThisExpr 859 `-CompoundStmt 860 )cpp"; 861 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, L), Expected); 862 } 863 864 { 865 auto L = getLambdaNode("capture_this_copy"); 866 867 llvm::StringRef Expected = R"cpp( 868 LambdaExpr 869 |-VarDecl 'self' 870 | `-UnaryOperator 871 | `-CXXThisExpr 872 `-CompoundStmt 873 )cpp"; 874 EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, L), Expected); 875 } 876 } 877 878 } // namespace clang 879