1 //=------ unittest/Tooling/RecursiveASTVisitorTests/CXXMethodDecl.cpp ------=//
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
7 //===----------------------------------------------------------------------===//
9 #include "TestVisitor.h"
10 #include "clang/AST/Expr.h"
12 using namespace clang
;
16 class CXXMethodDeclVisitor
17 : public ExpectedLocationVisitor
<CXXMethodDeclVisitor
> {
19 CXXMethodDeclVisitor(bool VisitImplicitCode
)
20 : VisitImplicitCode(VisitImplicitCode
) {}
22 bool shouldVisitImplicitCode() const { return VisitImplicitCode
; }
24 bool VisitDeclRefExpr(DeclRefExpr
*D
) {
25 Match("declref", D
->getLocation());
28 bool VisitParmVarDecl(ParmVarDecl
*P
) {
29 Match("parm", P
->getLocation());
34 bool VisitImplicitCode
;
37 TEST(RecursiveASTVisitor
, CXXMethodDeclNoDefaultBodyVisited
) {
38 for (bool VisitImplCode
: {false, true}) {
39 CXXMethodDeclVisitor
Visitor(VisitImplCode
);
41 Visitor
.ExpectMatch("declref", 8, 28);
43 Visitor
.DisallowMatch("declref", 8, 28);
45 Visitor
.ExpectMatch("parm", 8, 27);
46 llvm::StringRef Code
= R
"cpp(
53 A &A::operator=(A &&O) = default;
55 EXPECT_TRUE(Visitor
.runOver(Code
, CXXMethodDeclVisitor::Lang_CXX11
));
59 TEST(RecursiveASTVisitor
, FunctionDeclNoDefaultBodyVisited
) {
60 for (bool VisitImplCode
: {false, true}) {
61 CXXMethodDeclVisitor
Visitor(VisitImplCode
);
63 Visitor
.ExpectMatch("declref", 4, 58, /*Times=*/2);
65 Visitor
.DisallowMatch("declref", 4, 58);
66 llvm::StringRef Code
= R
"cpp(
69 friend auto operator==(s a, s b) -> bool = default;
71 bool k = s() == s(); // make sure clang generates the "==" definition.
73 EXPECT_TRUE(Visitor
.runOver(Code
, CXXMethodDeclVisitor::Lang_CXX2a
));
76 } // end anonymous namespace