1 //=------ unittest/Tooling/RecursiveASTVisitorTests/CXXMethodDecl.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 "TestVisitor.h" 10 #include "clang/AST/Expr.h" 11 12 using namespace clang; 13 14 namespace { 15 16 class CXXMethodDeclVisitor : public ExpectedLocationVisitor { 17 public: 18 CXXMethodDeclVisitor(bool VisitImplicitCode) { 19 ShouldVisitImplicitCode = VisitImplicitCode; 20 } 21 22 bool VisitDeclRefExpr(DeclRefExpr *D) override { 23 Match("declref", D->getLocation()); 24 return true; 25 } 26 27 bool VisitParmVarDecl(ParmVarDecl *P) override { 28 Match("parm", P->getLocation()); 29 return true; 30 } 31 }; 32 33 TEST(RecursiveASTVisitor, CXXMethodDeclNoDefaultBodyVisited) { 34 for (bool VisitImplCode : {false, true}) { 35 CXXMethodDeclVisitor Visitor(VisitImplCode); 36 if (VisitImplCode) 37 Visitor.ExpectMatch("declref", 8, 28); 38 else 39 Visitor.DisallowMatch("declref", 8, 28); 40 41 Visitor.ExpectMatch("parm", 8, 27); 42 llvm::StringRef Code = R"cpp( 43 struct B {}; 44 struct A { 45 B BB; 46 A &operator=(A &&O); 47 }; 48 49 A &A::operator=(A &&O) = default; 50 )cpp"; 51 EXPECT_TRUE(Visitor.runOver(Code, CXXMethodDeclVisitor::Lang_CXX11)); 52 } 53 } 54 55 TEST(RecursiveASTVisitor, FunctionDeclNoDefaultBodyVisited) { 56 for (bool VisitImplCode : {false, true}) { 57 CXXMethodDeclVisitor Visitor(VisitImplCode); 58 if (VisitImplCode) 59 Visitor.ExpectMatch("declref", 4, 58, /*Times=*/2); 60 else 61 Visitor.DisallowMatch("declref", 4, 58); 62 llvm::StringRef Code = R"cpp( 63 struct s { 64 int x; 65 friend auto operator==(s a, s b) -> bool = default; 66 }; 67 bool k = s() == s(); // make sure clang generates the "==" definition. 68 )cpp"; 69 EXPECT_TRUE(Visitor.runOver(Code, CXXMethodDeclVisitor::Lang_CXX2a)); 70 } 71 } 72 } // end anonymous namespace 73