blob: 6ef2786210f84348d073d942bc3683b949bbab20 [file] [log] [blame]
Manuel Klimekfad7f852012-04-19 08:48:53 +00001//===- unittest/AST/RecursiveASTMatcherTest.cpp ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/AST/ASTConsumer.h"
11#include "clang/AST/RecursiveASTVisitor.h"
12#include "clang/Frontend/FrontendAction.h"
13#include "clang/Frontend/CompilerInstance.h"
14#include "clang/Tooling/Tooling.h"
15#include "gtest/gtest.h"
16
17namespace clang {
18
19/// \brief Base class for sipmle RecursiveASTVisitor based tests.
20///
21/// This is a drop-in replacement for RecursiveASTVisitor itself, with the
22/// additional capability of running it over a snippet of code.
23///
24/// Visits template instantiations by default.
25///
26/// FIXME: Put into a common location.
27template <typename T>
28class TestVisitor : public clang::RecursiveASTVisitor<T> {
29public:
30 /// \brief Runs the current AST visitor over the given code.
31 bool runOver(StringRef Code) {
32 return tooling::runToolOnCode(new TestAction(this), Code);
33 }
34
35 bool shouldVisitTemplateInstantiations() const {
36 return true;
37 }
38
39protected:
40 clang::ASTContext *Context;
Manuel Klimekfad7f852012-04-19 08:48:53 +000041
42private:
43 class FindConsumer : public clang::ASTConsumer {
44 public:
45 FindConsumer(TestVisitor *Visitor) : Visitor(Visitor) {}
46
47 virtual void HandleTranslationUnit(clang::ASTContext &Context) {
48 Visitor->TraverseDecl(Context.getTranslationUnitDecl());
49 }
50
51 private:
52 TestVisitor *Visitor;
53 };
54
55 class TestAction : public clang::ASTFrontendAction {
56 public:
57 TestAction(TestVisitor *Visitor) : Visitor(Visitor) {}
58
59 virtual clang::ASTConsumer* CreateASTConsumer(
60 clang::CompilerInstance& compiler, llvm::StringRef dummy) {
Manuel Klimekfad7f852012-04-19 08:48:53 +000061 Visitor->Context = &compiler.getASTContext();
62 /// TestConsumer will be deleted by the framework calling us.
63 return new FindConsumer(Visitor);
64 }
65
66 private:
67 TestVisitor *Visitor;
68 };
69};
70
71/// \brief A RecursiveASTVisitor for testing the RecursiveASTVisitor itself.
72///
73/// Allows simple creation of test visitors running matches on only a small
74/// subset of the Visit* methods.
75template <typename T>
76class ExpectedLocationVisitor : public TestVisitor<T> {
77public:
78 ExpectedLocationVisitor()
79 : ExpectedLine(0), ExpectedColumn(0), Found(false) {}
80
81 ~ExpectedLocationVisitor() {
82 EXPECT_TRUE(Found)
83 << "Expected \"" << ExpectedMatch << "\" at " << ExpectedLine
84 << ":" << ExpectedColumn << PartialMatches;
85 }
86
87 /// \brief Expect 'Match' to occur at the given 'Line' and 'Column'.
88 void ExpectMatch(Twine Match, unsigned Line, unsigned Column) {
89 ExpectedMatch = Match.str();
90 ExpectedLine = Line;
91 ExpectedColumn = Column;
92 }
93
94protected:
95 /// \brief Convenience method to simplify writing test visitors.
96 ///
97 /// Sets 'Found' to true if 'Name' and 'Location' match the expected
98 /// values. If only a partial match is found, record the information
99 /// to produce nice error output when a test fails.
100 ///
101 /// Implementations are required to call this with appropriate values
102 /// for 'Name' during visitation.
103 void Match(StringRef Name, SourceLocation Location) {
104 FullSourceLoc FullLocation = this->Context->getFullLoc(Location);
105 if (Name == ExpectedMatch &&
106 FullLocation.isValid() &&
107 FullLocation.getSpellingLineNumber() == ExpectedLine &&
108 FullLocation.getSpellingColumnNumber() == ExpectedColumn) {
Manuel Klimek9f99d062012-04-23 16:40:40 +0000109 EXPECT_TRUE(!Found);
Manuel Klimekfad7f852012-04-19 08:48:53 +0000110 Found = true;
111 } else if (Name == ExpectedMatch ||
112 (FullLocation.isValid() &&
113 FullLocation.getSpellingLineNumber() == ExpectedLine &&
114 FullLocation.getSpellingColumnNumber() == ExpectedColumn)) {
115 // If we did not match, record information about partial matches.
116 llvm::raw_string_ostream Stream(PartialMatches);
117 Stream << ", partial match: \"" << Name << "\" at ";
Manuel Klimekdab28942012-04-20 14:07:01 +0000118 Location.print(Stream, this->Context->getSourceManager());
Manuel Klimekfad7f852012-04-19 08:48:53 +0000119 }
120 }
121
122 std::string ExpectedMatch;
123 unsigned ExpectedLine;
124 unsigned ExpectedColumn;
125 std::string PartialMatches;
126 bool Found;
127};
128
129class TypeLocVisitor : public ExpectedLocationVisitor<TypeLocVisitor> {
130public:
131 bool VisitTypeLoc(TypeLoc TypeLocation) {
132 Match(TypeLocation.getType().getAsString(), TypeLocation.getBeginLoc());
133 return true;
134 }
135};
136
137class DeclRefExprVisitor : public ExpectedLocationVisitor<DeclRefExprVisitor> {
138public:
139 bool VisitDeclRefExpr(DeclRefExpr *Reference) {
140 Match(Reference->getNameInfo().getAsString(), Reference->getLocation());
141 return true;
142 }
143};
144
145class CXXMemberCallVisitor
146 : public ExpectedLocationVisitor<CXXMemberCallVisitor> {
147public:
148 bool VisitCXXMemberCallExpr(CXXMemberCallExpr *Call) {
149 Match(Call->getMethodDecl()->getQualifiedNameAsString(),
150 Call->getLocStart());
151 return true;
152 }
153};
154
155TEST(RecursiveASTVisitor, VisitsBaseClassDeclarations) {
156 TypeLocVisitor Visitor;
157 Visitor.ExpectMatch("class X", 1, 30);
158 EXPECT_TRUE(Visitor.runOver("class X {}; class Y : public X {};"));
159}
160
Manuel Klimek9f99d062012-04-23 16:40:40 +0000161TEST(RecursiveASTVisitor, VisitsCXXBaseSpecifiersOfForwardDeclaredClass) {
162 TypeLocVisitor Visitor;
163 Visitor.ExpectMatch("class X", 3, 18);
164 EXPECT_TRUE(Visitor.runOver(
165 "class Y;\n"
166 "class X {};\n"
167 "class Y : public X {};"));
168}
169
170TEST(RecursiveASTVisitor, VisitsCXXBaseSpecifiersWithIncompleteInnerClass) {
171 TypeLocVisitor Visitor;
172 Visitor.ExpectMatch("class X", 2, 18);
173 EXPECT_TRUE(Visitor.runOver(
174 "class X {};\n"
175 "class Y : public X { class Z; };"));
176}
177
178TEST(RecursiveASTVisitor, VisitsCXXBaseSpecifiersOfSelfReferentialType) {
179 TypeLocVisitor Visitor;
180 Visitor.ExpectMatch("X<class Y>", 2, 18);
181 EXPECT_TRUE(Visitor.runOver(
182 "template<typename T> class X {};\n"
183 "class Y : public X<Y> {};"));
184}
185
Manuel Klimekfad7f852012-04-19 08:48:53 +0000186TEST(RecursiveASTVisitor, VisitsBaseClassTemplateArguments) {
187 DeclRefExprVisitor Visitor;
188 Visitor.ExpectMatch("x", 2, 3);
189 EXPECT_TRUE(Visitor.runOver(
190 "void x(); template <void (*T)()> class X {};\nX<x> y;"));
191}
192
193TEST(RecursiveASTVisitor, VisitsCallExpr) {
194 DeclRefExprVisitor Visitor;
195 Visitor.ExpectMatch("x", 1, 22);
196 EXPECT_TRUE(Visitor.runOver(
197 "void x(); void y() { x(); }"));
198}
199
200TEST(RecursiveASTVisitor, VisitsCallInTemplateInstantiation) {
201 CXXMemberCallVisitor Visitor;
202 Visitor.ExpectMatch("Y::x", 3, 3);
203 EXPECT_TRUE(Visitor.runOver(
204 "struct Y { void x(); };\n"
205 "template<typename T> void y(T t) {\n"
206 " t.x();\n"
207 "}\n"
208 "void foo() { y<Y>(Y()); }"));
209}
210
Richard Smith5482dc32012-04-24 20:39:49 +0000211TEST(RecursiveASTVisitor, VisitsCallInNestedFunctionTemplateInstantiation) {
Manuel Klimekfad7f852012-04-19 08:48:53 +0000212 CXXMemberCallVisitor Visitor;
213 Visitor.ExpectMatch("Y::x", 4, 5);
214 EXPECT_TRUE(Visitor.runOver(
215 "struct Y { void x(); };\n"
216 "template<typename T> struct Z {\n"
217 " template<typename U> static void f() {\n"
218 " T().x();\n"
219 " }\n"
220 "};\n"
221 "void foo() { Z<Y>::f<int>(); }"));
222}
Richard Smith5482dc32012-04-24 20:39:49 +0000223
224TEST(RecursiveASTVisitor, VisitsCallInNestedClassTemplateInstantiation) {
225 CXXMemberCallVisitor Visitor;
226 Visitor.ExpectMatch("A::x", 5, 7);
227 EXPECT_TRUE(Visitor.runOver(
228 "template <typename T1> struct X {\n"
229 " template <typename T2> struct Y {\n"
230 " void f() {\n"
231 " T2 y;\n"
232 " y.x();\n"
233 " }\n"
234 " };\n"
235 "};\n"
236 "struct A { void x(); };\n"
237 "int main() {\n"
238 " (new X<A>::Y<A>())->f();\n"
239 "}"));
240}
Manuel Klimekfad7f852012-04-19 08:48:53 +0000241
242/* FIXME: According to Richard Smith this is a bug in the AST.
243TEST(RecursiveASTVisitor, VisitsBaseClassTemplateArgumentsInInstantiation) {
244 DeclRefExprVisitor Visitor;
245 Visitor.ExpectMatch("x", 3, 43);
246 EXPECT_TRUE(Visitor.runOver(
247 "template <typename T> void x();\n"
248 "template <void (*T)()> class X {};\n"
249 "template <typename T> class Y : public X< x<T> > {};\n"
250 "Y<int> y;"));
251}
252*/
253
254} // end namespace clang