blob: 247a5fe811897ae1adafeb60738256021d0f49e0 [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
Richard Smitha313b2f2012-04-25 22:57:25 +0000155class NamedDeclVisitor
156 : public ExpectedLocationVisitor<NamedDeclVisitor> {
157public:
158 bool VisitNamedDecl(NamedDecl *Decl) {
159 std::string NameWithTemplateArgs;
160 Decl->getNameForDiagnostic(NameWithTemplateArgs,
161 Decl->getASTContext().getPrintingPolicy(),
162 true);
163 Match(NameWithTemplateArgs, Decl->getLocation());
164 return true;
165 }
166};
167
Manuel Klimekfad7f852012-04-19 08:48:53 +0000168TEST(RecursiveASTVisitor, VisitsBaseClassDeclarations) {
169 TypeLocVisitor Visitor;
170 Visitor.ExpectMatch("class X", 1, 30);
171 EXPECT_TRUE(Visitor.runOver("class X {}; class Y : public X {};"));
172}
173
Manuel Klimek9f99d062012-04-23 16:40:40 +0000174TEST(RecursiveASTVisitor, VisitsCXXBaseSpecifiersOfForwardDeclaredClass) {
175 TypeLocVisitor Visitor;
176 Visitor.ExpectMatch("class X", 3, 18);
177 EXPECT_TRUE(Visitor.runOver(
178 "class Y;\n"
179 "class X {};\n"
180 "class Y : public X {};"));
181}
182
183TEST(RecursiveASTVisitor, VisitsCXXBaseSpecifiersWithIncompleteInnerClass) {
184 TypeLocVisitor Visitor;
185 Visitor.ExpectMatch("class X", 2, 18);
186 EXPECT_TRUE(Visitor.runOver(
187 "class X {};\n"
188 "class Y : public X { class Z; };"));
189}
190
191TEST(RecursiveASTVisitor, VisitsCXXBaseSpecifiersOfSelfReferentialType) {
192 TypeLocVisitor Visitor;
193 Visitor.ExpectMatch("X<class Y>", 2, 18);
194 EXPECT_TRUE(Visitor.runOver(
195 "template<typename T> class X {};\n"
196 "class Y : public X<Y> {};"));
197}
198
Manuel Klimekfad7f852012-04-19 08:48:53 +0000199TEST(RecursiveASTVisitor, VisitsBaseClassTemplateArguments) {
200 DeclRefExprVisitor Visitor;
201 Visitor.ExpectMatch("x", 2, 3);
202 EXPECT_TRUE(Visitor.runOver(
203 "void x(); template <void (*T)()> class X {};\nX<x> y;"));
204}
205
206TEST(RecursiveASTVisitor, VisitsCallExpr) {
207 DeclRefExprVisitor Visitor;
208 Visitor.ExpectMatch("x", 1, 22);
209 EXPECT_TRUE(Visitor.runOver(
210 "void x(); void y() { x(); }"));
211}
212
213TEST(RecursiveASTVisitor, VisitsCallInTemplateInstantiation) {
214 CXXMemberCallVisitor Visitor;
215 Visitor.ExpectMatch("Y::x", 3, 3);
216 EXPECT_TRUE(Visitor.runOver(
217 "struct Y { void x(); };\n"
218 "template<typename T> void y(T t) {\n"
219 " t.x();\n"
220 "}\n"
221 "void foo() { y<Y>(Y()); }"));
222}
223
Richard Smith5482dc32012-04-24 20:39:49 +0000224TEST(RecursiveASTVisitor, VisitsCallInNestedFunctionTemplateInstantiation) {
Manuel Klimekfad7f852012-04-19 08:48:53 +0000225 CXXMemberCallVisitor Visitor;
226 Visitor.ExpectMatch("Y::x", 4, 5);
227 EXPECT_TRUE(Visitor.runOver(
228 "struct Y { void x(); };\n"
229 "template<typename T> struct Z {\n"
230 " template<typename U> static void f() {\n"
231 " T().x();\n"
232 " }\n"
233 "};\n"
234 "void foo() { Z<Y>::f<int>(); }"));
235}
Richard Smith5482dc32012-04-24 20:39:49 +0000236
237TEST(RecursiveASTVisitor, VisitsCallInNestedClassTemplateInstantiation) {
238 CXXMemberCallVisitor Visitor;
239 Visitor.ExpectMatch("A::x", 5, 7);
240 EXPECT_TRUE(Visitor.runOver(
241 "template <typename T1> struct X {\n"
242 " template <typename T2> struct Y {\n"
243 " void f() {\n"
244 " T2 y;\n"
245 " y.x();\n"
246 " }\n"
247 " };\n"
248 "};\n"
249 "struct A { void x(); };\n"
250 "int main() {\n"
251 " (new X<A>::Y<A>())->f();\n"
252 "}"));
253}
Manuel Klimekfad7f852012-04-19 08:48:53 +0000254
255/* FIXME: According to Richard Smith this is a bug in the AST.
256TEST(RecursiveASTVisitor, VisitsBaseClassTemplateArgumentsInInstantiation) {
257 DeclRefExprVisitor Visitor;
258 Visitor.ExpectMatch("x", 3, 43);
259 EXPECT_TRUE(Visitor.runOver(
260 "template <typename T> void x();\n"
261 "template <void (*T)()> class X {};\n"
262 "template <typename T> class Y : public X< x<T> > {};\n"
263 "Y<int> y;"));
264}
265*/
266
Richard Smitha313b2f2012-04-25 22:57:25 +0000267TEST(RecursiveASTVisitor, VisitsCallInPartialTemplateSpecialization) {
268 CXXMemberCallVisitor Visitor;
269 Visitor.ExpectMatch("A::x", 6, 20);
270 EXPECT_TRUE(Visitor.runOver(
271 "template <typename T1> struct X {\n"
272 " template <typename T2, bool B> struct Y { void g(); };\n"
273 "};\n"
274 "template <typename T1> template <typename T2>\n"
275 "struct X<T1>::Y<T2, true> {\n"
276 " void f() { T2 y; y.x(); }\n"
277 "};\n"
278 "struct A { void x(); };\n"
279 "int main() {\n"
280 " (new X<A>::Y<A, true>())->f();\n"
281 "}\n"));
282}
283
284TEST(RecursiveASTVisitor, VisitsPartialTemplateSpecialization) {
285 // From cfe-commits/Week-of-Mon-20100830/033998.html
286 // Contrary to the approach sugggested in that email, we visit all
287 // specializations when we visit the primary template. Visiting them when we
288 // visit the associated specialization is problematic for specializations of
289 // template members of class templates.
290 NamedDeclVisitor Visitor;
291 Visitor.ExpectMatch("A<bool>", 1, 26);
292 Visitor.ExpectMatch("A<char *>", 2, 26);
293 EXPECT_TRUE(Visitor.runOver(
294 "template <class T> class A {};\n"
295 "template <class T> class A<T*> {};\n"
296 "A<bool> ab;\n"
297 "A<char*> acp;\n"));
298}
299
300TEST(RecursiveASTVisitor, VisitsUndefinedClassTemplateSpecialization) {
301 NamedDeclVisitor Visitor;
302 Visitor.ExpectMatch("A<int>", 1, 29);
303 EXPECT_TRUE(Visitor.runOver(
304 "template<typename T> struct A;\n"
305 "A<int> *p;\n"));
306}
307
308TEST(RecursiveASTVisitor, VisitsNestedUndefinedClassTemplateSpecialization) {
309 NamedDeclVisitor Visitor;
310 Visitor.ExpectMatch("A<int>::B<char>", 2, 31);
311 EXPECT_TRUE(Visitor.runOver(
312 "template<typename T> struct A {\n"
313 " template<typename U> struct B;\n"
314 "};\n"
315 "A<int>::B<char> *p;\n"));
316}
317
318TEST(RecursiveASTVisitor, VisitsUndefinedFunctionTemplateSpecialization) {
319 NamedDeclVisitor Visitor;
320 Visitor.ExpectMatch("A<int>", 1, 26);
321 EXPECT_TRUE(Visitor.runOver(
322 "template<typename T> int A();\n"
323 "int k = A<int>();\n"));
324}
325
326TEST(RecursiveASTVisitor, VisitsNestedUndefinedFunctionTemplateSpecialization) {
327 NamedDeclVisitor Visitor;
328 Visitor.ExpectMatch("A<int>::B<char>", 2, 35);
329 EXPECT_TRUE(Visitor.runOver(
330 "template<typename T> struct A {\n"
331 " template<typename U> static int B();\n"
332 "};\n"
333 "int k = A<int>::B<char>();\n"));
334}
335
336TEST(RecursiveASTVisitor, NoRecursionInSelfFriend) {
337 // From cfe-commits/Week-of-Mon-20100830/033977.html
338 NamedDeclVisitor Visitor;
339 Visitor.ExpectMatch("vector_iterator<int>", 2, 7);
340 EXPECT_TRUE(Visitor.runOver(
341 "template<typename Container>\n"
342 "class vector_iterator {\n"
343 " template <typename C> friend class vector_iterator;\n"
344 "};\n"
345 "vector_iterator<int> it_int;\n"));
346}
347
Manuel Klimekfad7f852012-04-19 08:48:53 +0000348} // end namespace clang