|
| 1 | +//===- unittest/Tooling/EnterExitRecursiveASTVisitorTestDeclVisitor.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/RecursiveASTEnterExitVisitor.h" |
| 11 | + |
| 12 | +using namespace clang; |
| 13 | + |
| 14 | +namespace { |
| 15 | + |
| 16 | +class VarDeclVisitor : public ExpectedLocationVisitor { |
| 17 | +public: |
| 18 | + bool VisitVarDecl(VarDecl *Variable) override { |
| 19 | + Match(Variable->getNameAsString(), Variable->getBeginLoc()); |
| 20 | + return true; |
| 21 | + } |
| 22 | +}; |
| 23 | + |
| 24 | +TEST(EnterExitRecursiveASTVisitor, VisitsCXXForRangeStmtLoopVariable) { |
| 25 | + VarDeclVisitor Visitor; |
| 26 | + Visitor.ExpectMatch("i", 2, 17); |
| 27 | + EXPECT_TRUE(Visitor.runOver( |
| 28 | + "int x[5];\n" |
| 29 | + "void f() { for (int i : x) {} }", |
| 30 | + VarDeclVisitor::Lang_CXX11)); |
| 31 | +} |
| 32 | + |
| 33 | +class ParmVarDeclVisitorForImplicitCode : public ExpectedLocationVisitor { |
| 34 | +public: |
| 35 | + ParmVarDeclVisitorForImplicitCode() { ShouldVisitImplicitCode = true; } |
| 36 | + |
| 37 | + bool VisitParmVarDecl(ParmVarDecl *ParamVar) override { |
| 38 | + Match(ParamVar->getNameAsString(), ParamVar->getBeginLoc()); |
| 39 | + return true; |
| 40 | + } |
| 41 | +}; |
| 42 | + |
| 43 | +// Test RAV visits parameter variable declaration of the implicit |
| 44 | +// copy assignment operator and implicit copy constructor. |
| 45 | +TEST(EnterExitRecursiveASTVisitor, VisitsParmVarDeclForImplicitCode) { |
| 46 | + ParmVarDeclVisitorForImplicitCode Visitor; |
| 47 | + // Match parameter variable name of implicit copy assignment operator and |
| 48 | + // implicit copy constructor. |
| 49 | + // This parameter name does not have a valid IdentifierInfo, and shares |
| 50 | + // same SourceLocation with its class declaration, so we match an empty name |
| 51 | + // with the class' source location. |
| 52 | + Visitor.ExpectMatch("", 1, 7); |
| 53 | + Visitor.ExpectMatch("", 3, 7); |
| 54 | + EXPECT_TRUE(Visitor.runOver( |
| 55 | + "class X {};\n" |
| 56 | + "void foo(X a, X b) {a = b;}\n" |
| 57 | + "class Y {};\n" |
| 58 | + "void bar(Y a) {Y b = a;}")); |
| 59 | +} |
| 60 | + |
| 61 | +class NamedDeclVisitor : public ExpectedLocationVisitor { |
| 62 | +public: |
| 63 | + bool VisitNamedDecl(NamedDecl *Decl) override { |
| 64 | + std::string NameWithTemplateArgs; |
| 65 | + llvm::raw_string_ostream OS(NameWithTemplateArgs); |
| 66 | + Decl->getNameForDiagnostic(OS, |
| 67 | + Decl->getASTContext().getPrintingPolicy(), |
| 68 | + true); |
| 69 | + Match(NameWithTemplateArgs, Decl->getLocation()); |
| 70 | + return true; |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | +TEST(EnterExitRecursiveASTVisitor, VisitsPartialTemplateSpecialization) { |
| 75 | + // From cfe-commits/Week-of-Mon-20100830/033998.html |
| 76 | + // Contrary to the approach suggested in that email, we visit all |
| 77 | + // specializations when we visit the primary template. Visiting them when we |
| 78 | + // visit the associated specialization is problematic for specializations of |
| 79 | + // template members of class templates. |
| 80 | + NamedDeclVisitor Visitor; |
| 81 | + Visitor.ExpectMatch("A<bool>", 1, 26); |
| 82 | + Visitor.ExpectMatch("A<char *>", 2, 26); |
| 83 | + EXPECT_TRUE(Visitor.runOver( |
| 84 | + "template <class T> class A {};\n" |
| 85 | + "template <class T> class A<T*> {};\n" |
| 86 | + "A<bool> ab;\n" |
| 87 | + "A<char*> acp;\n")); |
| 88 | +} |
| 89 | + |
| 90 | +TEST(EnterExitRecursiveASTVisitor, VisitsUndefinedClassTemplateSpecialization) { |
| 91 | + NamedDeclVisitor Visitor; |
| 92 | + Visitor.ExpectMatch("A<int>", 1, 29); |
| 93 | + EXPECT_TRUE(Visitor.runOver( |
| 94 | + "template<typename T> struct A;\n" |
| 95 | + "A<int> *p;\n")); |
| 96 | +} |
| 97 | + |
| 98 | +TEST(EnterExitRecursiveASTVisitor, VisitsNestedUndefinedClassTemplateSpecialization) { |
| 99 | + NamedDeclVisitor Visitor; |
| 100 | + Visitor.ExpectMatch("A<int>::B<char>", 2, 31); |
| 101 | + EXPECT_TRUE(Visitor.runOver( |
| 102 | + "template<typename T> struct A {\n" |
| 103 | + " template<typename U> struct B;\n" |
| 104 | + "};\n" |
| 105 | + "A<int>::B<char> *p;\n")); |
| 106 | +} |
| 107 | + |
| 108 | +TEST(EnterExitRecursiveASTVisitor, VisitsUndefinedFunctionTemplateSpecialization) { |
| 109 | + NamedDeclVisitor Visitor; |
| 110 | + Visitor.ExpectMatch("A<int>", 1, 26); |
| 111 | + EXPECT_TRUE(Visitor.runOver( |
| 112 | + "template<typename T> int A();\n" |
| 113 | + "int k = A<int>();\n")); |
| 114 | +} |
| 115 | + |
| 116 | +TEST(EnterExitRecursiveASTVisitor, VisitsNestedUndefinedFunctionTemplateSpecialization) { |
| 117 | + NamedDeclVisitor Visitor; |
| 118 | + Visitor.ExpectMatch("A<int>::B<char>", 2, 35); |
| 119 | + EXPECT_TRUE(Visitor.runOver( |
| 120 | + "template<typename T> struct A {\n" |
| 121 | + " template<typename U> static int B();\n" |
| 122 | + "};\n" |
| 123 | + "int k = A<int>::B<char>();\n")); |
| 124 | +} |
| 125 | + |
| 126 | +TEST(EnterExitRecursiveASTVisitor, NoRecursionInSelfFriend) { |
| 127 | + // From cfe-commits/Week-of-Mon-20100830/033977.html |
| 128 | + NamedDeclVisitor Visitor; |
| 129 | + Visitor.ExpectMatch("vector_iterator<int>", 2, 7); |
| 130 | + EXPECT_TRUE(Visitor.runOver( |
| 131 | + "template<typename Container>\n" |
| 132 | + "class vector_iterator {\n" |
| 133 | + " template <typename C> friend class vector_iterator;\n" |
| 134 | + "};\n" |
| 135 | + "vector_iterator<int> it_int;\n")); |
| 136 | +} |
| 137 | + |
| 138 | +} // end anonymous namespace |
0 commit comments