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

source code of clang/unittests/Tooling/RecursiveASTVisitorTests/CXXMethodDecl.cpp