ImplicitCtor.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //===- unittest/Tooling/RecursiveASTVisitorTests/ImplicitCtor.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. using namespace clang;
  10. namespace {
  11. // A visitor that visits implicit declarations and matches constructors.
  12. class ImplicitCtorVisitor
  13. : public ExpectedLocationVisitor<ImplicitCtorVisitor> {
  14. public:
  15. bool shouldVisitImplicitCode() const { return true; }
  16. bool VisitCXXConstructorDecl(CXXConstructorDecl* Ctor) {
  17. if (Ctor->isImplicit()) { // Was not written in source code
  18. if (const CXXRecordDecl* Class = Ctor->getParent()) {
  19. Match(Class->getName(), Ctor->getLocation());
  20. }
  21. }
  22. return true;
  23. }
  24. };
  25. TEST(RecursiveASTVisitor, VisitsImplicitCopyConstructors) {
  26. ImplicitCtorVisitor Visitor;
  27. Visitor.ExpectMatch("Simple", 2, 8);
  28. // Note: Clang lazily instantiates implicit declarations, so we need
  29. // to use them in order to force them to appear in the AST.
  30. EXPECT_TRUE(Visitor.runOver(
  31. "struct WithCtor { WithCtor(); }; \n"
  32. "struct Simple { Simple(); WithCtor w; }; \n"
  33. "int main() { Simple s; Simple t(s); }\n"));
  34. }
  35. } // end anonymous namespace