1d639f6dfSJohan Vikstrom //=------ unittest/Tooling/RecursiveASTVisitorTests/CXXMethodDecl.cpp ------=// 2d639f6dfSJohan Vikstrom // 3d639f6dfSJohan Vikstrom // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4d639f6dfSJohan Vikstrom // See https://llvm.org/LICENSE.txt for license information. 5d639f6dfSJohan Vikstrom // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6d639f6dfSJohan Vikstrom // 7d639f6dfSJohan Vikstrom //===----------------------------------------------------------------------===// 8d639f6dfSJohan Vikstrom 9d639f6dfSJohan Vikstrom #include "TestVisitor.h" 10d639f6dfSJohan Vikstrom #include "clang/AST/Expr.h" 11d639f6dfSJohan Vikstrom 12d639f6dfSJohan Vikstrom using namespace clang; 13d639f6dfSJohan Vikstrom 14d639f6dfSJohan Vikstrom namespace { 15d639f6dfSJohan Vikstrom 16*4e600751SSirraide class CXXMethodDeclVisitor : public ExpectedLocationVisitor { 17d639f6dfSJohan Vikstrom public: 18*4e600751SSirraide CXXMethodDeclVisitor(bool VisitImplicitCode) { 19*4e600751SSirraide ShouldVisitImplicitCode = VisitImplicitCode; 20*4e600751SSirraide } 21d639f6dfSJohan Vikstrom 22*4e600751SSirraide bool VisitDeclRefExpr(DeclRefExpr *D) override { 23d639f6dfSJohan Vikstrom Match("declref", D->getLocation()); 24d639f6dfSJohan Vikstrom return true; 25d639f6dfSJohan Vikstrom } 26*4e600751SSirraide 27*4e600751SSirraide bool VisitParmVarDecl(ParmVarDecl *P) override { 28d639f6dfSJohan Vikstrom Match("parm", P->getLocation()); 29d639f6dfSJohan Vikstrom return true; 30d639f6dfSJohan Vikstrom } 31d639f6dfSJohan Vikstrom }; 32d639f6dfSJohan Vikstrom 33d639f6dfSJohan Vikstrom TEST(RecursiveASTVisitor, CXXMethodDeclNoDefaultBodyVisited) { 34d639f6dfSJohan Vikstrom for (bool VisitImplCode : {false, true}) { 35d639f6dfSJohan Vikstrom CXXMethodDeclVisitor Visitor(VisitImplCode); 36d639f6dfSJohan Vikstrom if (VisitImplCode) 37d639f6dfSJohan Vikstrom Visitor.ExpectMatch("declref", 8, 28); 38d639f6dfSJohan Vikstrom else 39d639f6dfSJohan Vikstrom Visitor.DisallowMatch("declref", 8, 28); 40d639f6dfSJohan Vikstrom 41d639f6dfSJohan Vikstrom Visitor.ExpectMatch("parm", 8, 27); 42d639f6dfSJohan Vikstrom llvm::StringRef Code = R"cpp( 43d639f6dfSJohan Vikstrom struct B {}; 44d639f6dfSJohan Vikstrom struct A { 45d639f6dfSJohan Vikstrom B BB; 46d639f6dfSJohan Vikstrom A &operator=(A &&O); 47d639f6dfSJohan Vikstrom }; 48d639f6dfSJohan Vikstrom 49d639f6dfSJohan Vikstrom A &A::operator=(A &&O) = default; 50d639f6dfSJohan Vikstrom )cpp"; 51d639f6dfSJohan Vikstrom EXPECT_TRUE(Visitor.runOver(Code, CXXMethodDeclVisitor::Lang_CXX11)); 52d639f6dfSJohan Vikstrom } 53d639f6dfSJohan Vikstrom } 54447ea9b4Smydeveloperday 55447ea9b4Smydeveloperday TEST(RecursiveASTVisitor, FunctionDeclNoDefaultBodyVisited) { 56447ea9b4Smydeveloperday for (bool VisitImplCode : {false, true}) { 57447ea9b4Smydeveloperday CXXMethodDeclVisitor Visitor(VisitImplCode); 58447ea9b4Smydeveloperday if (VisitImplCode) 59447ea9b4Smydeveloperday Visitor.ExpectMatch("declref", 4, 58, /*Times=*/2); 60447ea9b4Smydeveloperday else 61447ea9b4Smydeveloperday Visitor.DisallowMatch("declref", 4, 58); 62447ea9b4Smydeveloperday llvm::StringRef Code = R"cpp( 63447ea9b4Smydeveloperday struct s { 64447ea9b4Smydeveloperday int x; 65447ea9b4Smydeveloperday friend auto operator==(s a, s b) -> bool = default; 66447ea9b4Smydeveloperday }; 67447ea9b4Smydeveloperday bool k = s() == s(); // make sure clang generates the "==" definition. 68447ea9b4Smydeveloperday )cpp"; 69447ea9b4Smydeveloperday EXPECT_TRUE(Visitor.runOver(Code, CXXMethodDeclVisitor::Lang_CXX2a)); 70447ea9b4Smydeveloperday } 71447ea9b4Smydeveloperday } 72d639f6dfSJohan Vikstrom } // end anonymous namespace 73