blob: d749f76a22bc16f29993feb10e9ee7f024a29765 [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;
41 clang::SourceManager *SM;
42
43private:
44 class FindConsumer : public clang::ASTConsumer {
45 public:
46 FindConsumer(TestVisitor *Visitor) : Visitor(Visitor) {}
47
48 virtual void HandleTranslationUnit(clang::ASTContext &Context) {
49 Visitor->TraverseDecl(Context.getTranslationUnitDecl());
50 }
51
52 private:
53 TestVisitor *Visitor;
54 };
55
56 class TestAction : public clang::ASTFrontendAction {
57 public:
58 TestAction(TestVisitor *Visitor) : Visitor(Visitor) {}
59
60 virtual clang::ASTConsumer* CreateASTConsumer(
61 clang::CompilerInstance& compiler, llvm::StringRef dummy) {
62 Visitor->SM = &compiler.getSourceManager();
63 Visitor->Context = &compiler.getASTContext();
64 /// TestConsumer will be deleted by the framework calling us.
65 return new FindConsumer(Visitor);
66 }
67
68 private:
69 TestVisitor *Visitor;
70 };
71};
72
73/// \brief A RecursiveASTVisitor for testing the RecursiveASTVisitor itself.
74///
75/// Allows simple creation of test visitors running matches on only a small
76/// subset of the Visit* methods.
77template <typename T>
78class ExpectedLocationVisitor : public TestVisitor<T> {
79public:
80 ExpectedLocationVisitor()
81 : ExpectedLine(0), ExpectedColumn(0), Found(false) {}
82
83 ~ExpectedLocationVisitor() {
84 EXPECT_TRUE(Found)
85 << "Expected \"" << ExpectedMatch << "\" at " << ExpectedLine
86 << ":" << ExpectedColumn << PartialMatches;
87 }
88
89 /// \brief Expect 'Match' to occur at the given 'Line' and 'Column'.
90 void ExpectMatch(Twine Match, unsigned Line, unsigned Column) {
91 ExpectedMatch = Match.str();
92 ExpectedLine = Line;
93 ExpectedColumn = Column;
94 }
95
96protected:
97 /// \brief Convenience method to simplify writing test visitors.
98 ///
99 /// Sets 'Found' to true if 'Name' and 'Location' match the expected
100 /// values. If only a partial match is found, record the information
101 /// to produce nice error output when a test fails.
102 ///
103 /// Implementations are required to call this with appropriate values
104 /// for 'Name' during visitation.
105 void Match(StringRef Name, SourceLocation Location) {
106 FullSourceLoc FullLocation = this->Context->getFullLoc(Location);
107 if (Name == ExpectedMatch &&
108 FullLocation.isValid() &&
109 FullLocation.getSpellingLineNumber() == ExpectedLine &&
110 FullLocation.getSpellingColumnNumber() == ExpectedColumn) {
111 Found = true;
112 } else if (Name == ExpectedMatch ||
113 (FullLocation.isValid() &&
114 FullLocation.getSpellingLineNumber() == ExpectedLine &&
115 FullLocation.getSpellingColumnNumber() == ExpectedColumn)) {
116 // If we did not match, record information about partial matches.
117 llvm::raw_string_ostream Stream(PartialMatches);
118 Stream << ", partial match: \"" << Name << "\" at ";
119 Location.print(Stream, *this->SM);
120 }
121 }
122
123 std::string ExpectedMatch;
124 unsigned ExpectedLine;
125 unsigned ExpectedColumn;
126 std::string PartialMatches;
127 bool Found;
128};
129
130class TypeLocVisitor : public ExpectedLocationVisitor<TypeLocVisitor> {
131public:
132 bool VisitTypeLoc(TypeLoc TypeLocation) {
133 Match(TypeLocation.getType().getAsString(), TypeLocation.getBeginLoc());
134 return true;
135 }
136};
137
138class DeclRefExprVisitor : public ExpectedLocationVisitor<DeclRefExprVisitor> {
139public:
140 bool VisitDeclRefExpr(DeclRefExpr *Reference) {
141 Match(Reference->getNameInfo().getAsString(), Reference->getLocation());
142 return true;
143 }
144};
145
146class CXXMemberCallVisitor
147 : public ExpectedLocationVisitor<CXXMemberCallVisitor> {
148public:
149 bool VisitCXXMemberCallExpr(CXXMemberCallExpr *Call) {
150 Match(Call->getMethodDecl()->getQualifiedNameAsString(),
151 Call->getLocStart());
152 return true;
153 }
154};
155
156TEST(RecursiveASTVisitor, VisitsBaseClassDeclarations) {
157 TypeLocVisitor Visitor;
158 Visitor.ExpectMatch("class X", 1, 30);
159 EXPECT_TRUE(Visitor.runOver("class X {}; class Y : public X {};"));
160}
161
162TEST(RecursiveASTVisitor, VisitsBaseClassTemplateArguments) {
163 DeclRefExprVisitor Visitor;
164 Visitor.ExpectMatch("x", 2, 3);
165 EXPECT_TRUE(Visitor.runOver(
166 "void x(); template <void (*T)()> class X {};\nX<x> y;"));
167}
168
169TEST(RecursiveASTVisitor, VisitsCallExpr) {
170 DeclRefExprVisitor Visitor;
171 Visitor.ExpectMatch("x", 1, 22);
172 EXPECT_TRUE(Visitor.runOver(
173 "void x(); void y() { x(); }"));
174}
175
176TEST(RecursiveASTVisitor, VisitsCallInTemplateInstantiation) {
177 CXXMemberCallVisitor Visitor;
178 Visitor.ExpectMatch("Y::x", 3, 3);
179 EXPECT_TRUE(Visitor.runOver(
180 "struct Y { void x(); };\n"
181 "template<typename T> void y(T t) {\n"
182 " t.x();\n"
183 "}\n"
184 "void foo() { y<Y>(Y()); }"));
185}
186
187/* FIXME:
188TEST(RecursiveASTVisitor, VisitsCallInNestedTemplateInstantiation) {
189 CXXMemberCallVisitor Visitor;
190 Visitor.ExpectMatch("Y::x", 4, 5);
191 EXPECT_TRUE(Visitor.runOver(
192 "struct Y { void x(); };\n"
193 "template<typename T> struct Z {\n"
194 " template<typename U> static void f() {\n"
195 " T().x();\n"
196 " }\n"
197 "};\n"
198 "void foo() { Z<Y>::f<int>(); }"));
199}
200*/
201
202/* FIXME: According to Richard Smith this is a bug in the AST.
203TEST(RecursiveASTVisitor, VisitsBaseClassTemplateArgumentsInInstantiation) {
204 DeclRefExprVisitor Visitor;
205 Visitor.ExpectMatch("x", 3, 43);
206 EXPECT_TRUE(Visitor.runOver(
207 "template <typename T> void x();\n"
208 "template <void (*T)()> class X {};\n"
209 "template <typename T> class Y : public X< x<T> > {};\n"
210 "Y<int> y;"));
211}
212*/
213
214} // end namespace clang
215