blob: d7dad2738a05e3539b469b893cc80b545b5a66c5 [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) {
109 Found = true;
110 } else if (Name == ExpectedMatch ||
111 (FullLocation.isValid() &&
112 FullLocation.getSpellingLineNumber() == ExpectedLine &&
113 FullLocation.getSpellingColumnNumber() == ExpectedColumn)) {
114 // If we did not match, record information about partial matches.
115 llvm::raw_string_ostream Stream(PartialMatches);
116 Stream << ", partial match: \"" << Name << "\" at ";
Manuel Klimekdab28942012-04-20 14:07:01 +0000117 Location.print(Stream, this->Context->getSourceManager());
Manuel Klimekfad7f852012-04-19 08:48:53 +0000118 }
119 }
120
121 std::string ExpectedMatch;
122 unsigned ExpectedLine;
123 unsigned ExpectedColumn;
124 std::string PartialMatches;
125 bool Found;
126};
127
128class TypeLocVisitor : public ExpectedLocationVisitor<TypeLocVisitor> {
129public:
130 bool VisitTypeLoc(TypeLoc TypeLocation) {
131 Match(TypeLocation.getType().getAsString(), TypeLocation.getBeginLoc());
132 return true;
133 }
134};
135
136class DeclRefExprVisitor : public ExpectedLocationVisitor<DeclRefExprVisitor> {
137public:
138 bool VisitDeclRefExpr(DeclRefExpr *Reference) {
139 Match(Reference->getNameInfo().getAsString(), Reference->getLocation());
140 return true;
141 }
142};
143
144class CXXMemberCallVisitor
145 : public ExpectedLocationVisitor<CXXMemberCallVisitor> {
146public:
147 bool VisitCXXMemberCallExpr(CXXMemberCallExpr *Call) {
148 Match(Call->getMethodDecl()->getQualifiedNameAsString(),
149 Call->getLocStart());
150 return true;
151 }
152};
153
154TEST(RecursiveASTVisitor, VisitsBaseClassDeclarations) {
155 TypeLocVisitor Visitor;
156 Visitor.ExpectMatch("class X", 1, 30);
157 EXPECT_TRUE(Visitor.runOver("class X {}; class Y : public X {};"));
158}
159
160TEST(RecursiveASTVisitor, VisitsBaseClassTemplateArguments) {
161 DeclRefExprVisitor Visitor;
162 Visitor.ExpectMatch("x", 2, 3);
163 EXPECT_TRUE(Visitor.runOver(
164 "void x(); template <void (*T)()> class X {};\nX<x> y;"));
165}
166
167TEST(RecursiveASTVisitor, VisitsCallExpr) {
168 DeclRefExprVisitor Visitor;
169 Visitor.ExpectMatch("x", 1, 22);
170 EXPECT_TRUE(Visitor.runOver(
171 "void x(); void y() { x(); }"));
172}
173
174TEST(RecursiveASTVisitor, VisitsCallInTemplateInstantiation) {
175 CXXMemberCallVisitor Visitor;
176 Visitor.ExpectMatch("Y::x", 3, 3);
177 EXPECT_TRUE(Visitor.runOver(
178 "struct Y { void x(); };\n"
179 "template<typename T> void y(T t) {\n"
180 " t.x();\n"
181 "}\n"
182 "void foo() { y<Y>(Y()); }"));
183}
184
185/* FIXME:
186TEST(RecursiveASTVisitor, VisitsCallInNestedTemplateInstantiation) {
187 CXXMemberCallVisitor Visitor;
188 Visitor.ExpectMatch("Y::x", 4, 5);
189 EXPECT_TRUE(Visitor.runOver(
190 "struct Y { void x(); };\n"
191 "template<typename T> struct Z {\n"
192 " template<typename U> static void f() {\n"
193 " T().x();\n"
194 " }\n"
195 "};\n"
196 "void foo() { Z<Y>::f<int>(); }"));
197}
198*/
199
200/* FIXME: According to Richard Smith this is a bug in the AST.
201TEST(RecursiveASTVisitor, VisitsBaseClassTemplateArgumentsInInstantiation) {
202 DeclRefExprVisitor Visitor;
203 Visitor.ExpectMatch("x", 3, 43);
204 EXPECT_TRUE(Visitor.runOver(
205 "template <typename T> void x();\n"
206 "template <void (*T)()> class X {};\n"
207 "template <typename T> class Y : public X< x<T> > {};\n"
208 "Y<int> y;"));
209}
210*/
211
212} // end namespace clang
213