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