Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / unittests / Tooling / RecursiveASTVisitorTests / CXXMethodDecl.cpp
blob90fa84bd448124c93cb5404f7b86bfe61b28b894
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 //===----------------------------------------------------------------------===//
9 #include "TestVisitor.h"
10 #include "clang/AST/Expr.h"
12 using namespace clang;
14 namespace {
16 class CXXMethodDeclVisitor
17 : public ExpectedLocationVisitor<CXXMethodDeclVisitor> {
18 public:
19 CXXMethodDeclVisitor(bool VisitImplicitCode)
20 : VisitImplicitCode(VisitImplicitCode) {}
22 bool shouldVisitImplicitCode() const { return VisitImplicitCode; }
24 bool VisitDeclRefExpr(DeclRefExpr *D) {
25 Match("declref", D->getLocation());
26 return true;
28 bool VisitParmVarDecl(ParmVarDecl *P) {
29 Match("parm", P->getLocation());
30 return true;
33 private:
34 bool VisitImplicitCode;
37 TEST(RecursiveASTVisitor, CXXMethodDeclNoDefaultBodyVisited) {
38 for (bool VisitImplCode : {false, true}) {
39 CXXMethodDeclVisitor Visitor(VisitImplCode);
40 if (VisitImplCode)
41 Visitor.ExpectMatch("declref", 8, 28);
42 else
43 Visitor.DisallowMatch("declref", 8, 28);
45 Visitor.ExpectMatch("parm", 8, 27);
46 llvm::StringRef Code = R"cpp(
47 struct B {};
48 struct A {
49 B BB;
50 A &operator=(A &&O);
53 A &A::operator=(A &&O) = default;
54 )cpp";
55 EXPECT_TRUE(Visitor.runOver(Code, CXXMethodDeclVisitor::Lang_CXX11));
59 TEST(RecursiveASTVisitor, FunctionDeclNoDefaultBodyVisited) {
60 for (bool VisitImplCode : {false, true}) {
61 CXXMethodDeclVisitor Visitor(VisitImplCode);
62 if (VisitImplCode)
63 Visitor.ExpectMatch("declref", 4, 58, /*Times=*/2);
64 else
65 Visitor.DisallowMatch("declref", 4, 58);
66 llvm::StringRef Code = R"cpp(
67 struct s {
68 int x;
69 friend auto operator==(s a, s b) -> bool = default;
71 bool k = s() == s(); // make sure clang generates the "==" definition.
72 )cpp";
73 EXPECT_TRUE(Visitor.runOver(Code, CXXMethodDeclVisitor::Lang_CXX2a));
76 } // end anonymous namespace