CXXMethodDecl.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. #include "TestVisitor.h"
  9. #include "clang/AST/Expr.h"
  10. using namespace clang;
  11. namespace {
  12. class CXXMethodDeclVisitor
  13. : public ExpectedLocationVisitor<CXXMethodDeclVisitor> {
  14. public:
  15. CXXMethodDeclVisitor(bool VisitImplicitCode)
  16. : VisitImplicitCode(VisitImplicitCode) {}
  17. bool shouldVisitImplicitCode() const { return VisitImplicitCode; }
  18. bool VisitDeclRefExpr(DeclRefExpr *D) {
  19. Match("declref", D->getLocation());
  20. return true;
  21. }
  22. bool VisitParmVarDecl(ParmVarDecl *P) {
  23. Match("parm", P->getLocation());
  24. return true;
  25. }
  26. private:
  27. bool VisitImplicitCode;
  28. };
  29. TEST(RecursiveASTVisitor, CXXMethodDeclNoDefaultBodyVisited) {
  30. for (bool VisitImplCode : {false, true}) {
  31. CXXMethodDeclVisitor Visitor(VisitImplCode);
  32. if (VisitImplCode)
  33. Visitor.ExpectMatch("declref", 8, 28);
  34. else
  35. Visitor.DisallowMatch("declref", 8, 28);
  36. Visitor.ExpectMatch("parm", 8, 27);
  37. llvm::StringRef Code = R"cpp(
  38. struct B {};
  39. struct A {
  40. B BB;
  41. A &operator=(A &&O);
  42. };
  43. A &A::operator=(A &&O) = default;
  44. )cpp";
  45. EXPECT_TRUE(Visitor.runOver(Code, CXXMethodDeclVisitor::Lang_CXX11));
  46. }
  47. }
  48. } // end anonymous namespace