blob: ae02fb5ebbfd376066b2e4ee41fa28e83ca36c75 [file] [log] [blame]
Richard Smith87d8fb92012-06-24 23:56:26 +00001//===--- TestVisitor.h ------------------------------------------*- C++ -*-===//
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//===----------------------------------------------------------------------===//
James Dennett75c100b2012-08-24 06:59:51 +00009///
10/// \file
11/// \brief Defines utility templates for RecursiveASTVisitor related tests.
12///
Richard Smith87d8fb92012-06-24 23:56:26 +000013//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_TEST_VISITOR_H
16#define LLVM_CLANG_TEST_VISITOR_H
17
18#include "clang/AST/ASTConsumer.h"
Chandler Carruth320d9662012-12-04 09:45:34 +000019#include "clang/AST/ASTContext.h"
Richard Smith87d8fb92012-06-24 23:56:26 +000020#include "clang/AST/RecursiveASTVisitor.h"
Richard Smith87d8fb92012-06-24 23:56:26 +000021#include "clang/Frontend/CompilerInstance.h"
Chandler Carruth320d9662012-12-04 09:45:34 +000022#include "clang/Frontend/FrontendAction.h"
Richard Smith87d8fb92012-06-24 23:56:26 +000023#include "clang/Tooling/Tooling.h"
24#include "gtest/gtest.h"
Chandler Carruth320d9662012-12-04 09:45:34 +000025#include <vector>
Richard Smith87d8fb92012-06-24 23:56:26 +000026
27namespace clang {
28
Matt Beaumont-Gaya0e2c042012-06-25 18:27:11 +000029/// \brief Base class for simple RecursiveASTVisitor based tests.
Richard Smith87d8fb92012-06-24 23:56:26 +000030///
31/// This is a drop-in replacement for RecursiveASTVisitor itself, with the
32/// additional capability of running it over a snippet of code.
33///
Michael Hanc90d12d2013-09-11 15:53:29 +000034/// Visits template instantiations and implicit code by default.
Richard Smith87d8fb92012-06-24 23:56:26 +000035template <typename T>
36class TestVisitor : public RecursiveASTVisitor<T> {
37public:
38 TestVisitor() { }
39
Matt Beaumont-Gaya0e2c042012-06-25 18:27:11 +000040 virtual ~TestVisitor() { }
41
Alp Tokere3ad5312014-06-26 01:42:24 +000042 enum Language {
43 Lang_C,
44 Lang_CXX98,
45 Lang_CXX11,
46 Lang_OBJC,
47 Lang_OBJCXX11,
48 Lang_CXX = Lang_CXX98
49 };
Richard Smith87deab32012-08-17 21:23:17 +000050
Richard Smith87d8fb92012-06-24 23:56:26 +000051 /// \brief Runs the current AST visitor over the given code.
Richard Smith87deab32012-08-17 21:23:17 +000052 bool runOver(StringRef Code, Language L = Lang_CXX) {
Nico Weber077a53e2012-08-30 02:02:19 +000053 std::vector<std::string> Args;
54 switch (L) {
55 case Lang_C: Args.push_back("-std=c99"); break;
James Dennetteb576c02013-06-30 03:05:49 +000056 case Lang_CXX98: Args.push_back("-std=c++98"); break;
57 case Lang_CXX11: Args.push_back("-std=c++11"); break;
Alp Toker62438da2014-06-06 15:05:09 +000058 case Lang_OBJC: Args.push_back("-ObjC"); break;
Alp Tokere3ad5312014-06-26 01:42:24 +000059 case Lang_OBJCXX11:
60 Args.push_back("-ObjC++");
61 Args.push_back("-std=c++11");
62 break;
Nico Weber077a53e2012-08-30 02:02:19 +000063 }
64 return tooling::runToolOnCodeWithArgs(CreateTestAction(), Code, Args);
Richard Smith87d8fb92012-06-24 23:56:26 +000065 }
66
67 bool shouldVisitTemplateInstantiations() const {
68 return true;
69 }
70
Michael Hanc90d12d2013-09-11 15:53:29 +000071 bool shouldVisitImplicitCode() const {
72 return true;
73 }
74
Richard Smith87d8fb92012-06-24 23:56:26 +000075protected:
76 virtual ASTFrontendAction* CreateTestAction() {
77 return new TestAction(this);
78 }
79
80 class FindConsumer : public ASTConsumer {
81 public:
82 FindConsumer(TestVisitor *Visitor) : Visitor(Visitor) {}
83
84 virtual void HandleTranslationUnit(clang::ASTContext &Context) {
Manuel Klimek5da9dcb2012-07-05 18:13:01 +000085 Visitor->Context = &Context;
Richard Smith87d8fb92012-06-24 23:56:26 +000086 Visitor->TraverseDecl(Context.getTranslationUnitDecl());
87 }
88
89 private:
90 TestVisitor *Visitor;
91 };
92
93 class TestAction : public ASTFrontendAction {
94 public:
95 TestAction(TestVisitor *Visitor) : Visitor(Visitor) {}
96
97 virtual clang::ASTConsumer* CreateASTConsumer(
Manuel Klimek5da9dcb2012-07-05 18:13:01 +000098 CompilerInstance&, llvm::StringRef dummy) {
Richard Smith87d8fb92012-06-24 23:56:26 +000099 /// TestConsumer will be deleted by the framework calling us.
100 return new FindConsumer(Visitor);
101 }
102
103 protected:
104 TestVisitor *Visitor;
105 };
106
107 ASTContext *Context;
108};
109
James Dennett75c100b2012-08-24 06:59:51 +0000110/// \brief A RecursiveASTVisitor to check that certain matches are (or are
111/// not) observed during visitation.
Richard Smith87d8fb92012-06-24 23:56:26 +0000112///
James Dennett75c100b2012-08-24 06:59:51 +0000113/// This is a RecursiveASTVisitor for testing the RecursiveASTVisitor itself,
114/// and allows simple creation of test visitors running matches on only a small
Richard Smith87d8fb92012-06-24 23:56:26 +0000115/// subset of the Visit* methods.
116template <typename T, template <typename> class Visitor = TestVisitor>
117class ExpectedLocationVisitor : public Visitor<T> {
118public:
James Dennett75c100b2012-08-24 06:59:51 +0000119 /// \brief Expect 'Match' *not* to occur at the given 'Line' and 'Column'.
120 ///
121 /// Any number of matches can be disallowed.
122 void DisallowMatch(Twine Match, unsigned Line, unsigned Column) {
123 DisallowedMatches.push_back(MatchCandidate(Match, Line, Column));
Richard Smith87d8fb92012-06-24 23:56:26 +0000124 }
125
126 /// \brief Expect 'Match' to occur at the given 'Line' and 'Column'.
James Dennett75c100b2012-08-24 06:59:51 +0000127 ///
128 /// Any number of expected matches can be set by calling this repeatedly.
129 /// Each is expected to be matched exactly once.
Richard Smith87d8fb92012-06-24 23:56:26 +0000130 void ExpectMatch(Twine Match, unsigned Line, unsigned Column) {
James Dennett75c100b2012-08-24 06:59:51 +0000131 ExpectedMatches.push_back(ExpectedMatch(Match, Line, Column));
132 }
133
134 /// \brief Checks that all expected matches have been found.
135 virtual ~ExpectedLocationVisitor() {
136 for (typename std::vector<ExpectedMatch>::const_iterator
137 It = ExpectedMatches.begin(), End = ExpectedMatches.end();
138 It != End; ++It) {
139 It->ExpectFound();
140 }
Richard Smith87d8fb92012-06-24 23:56:26 +0000141 }
142
143protected:
James Dennett75c100b2012-08-24 06:59:51 +0000144 /// \brief Checks an actual match against expected and disallowed matches.
Richard Smith87d8fb92012-06-24 23:56:26 +0000145 ///
146 /// Implementations are required to call this with appropriate values
147 /// for 'Name' during visitation.
148 void Match(StringRef Name, SourceLocation Location) {
James Dennett75c100b2012-08-24 06:59:51 +0000149 const FullSourceLoc FullLocation = this->Context->getFullLoc(Location);
150
151 for (typename std::vector<MatchCandidate>::const_iterator
152 It = DisallowedMatches.begin(), End = DisallowedMatches.end();
153 It != End; ++It) {
154 EXPECT_FALSE(It->Matches(Name, FullLocation))
155 << "Matched disallowed " << *It;
156 }
157
158 for (typename std::vector<ExpectedMatch>::iterator
159 It = ExpectedMatches.begin(), End = ExpectedMatches.end();
160 It != End; ++It) {
161 It->UpdateFor(Name, FullLocation, this->Context->getSourceManager());
Richard Smith87d8fb92012-06-24 23:56:26 +0000162 }
163 }
164
James Dennett75c100b2012-08-24 06:59:51 +0000165 private:
166 struct MatchCandidate {
167 std::string ExpectedName;
168 unsigned LineNumber;
169 unsigned ColumnNumber;
170
171 MatchCandidate(Twine Name, unsigned LineNumber, unsigned ColumnNumber)
172 : ExpectedName(Name.str()), LineNumber(LineNumber),
173 ColumnNumber(ColumnNumber) {
174 }
175
176 bool Matches(StringRef Name, FullSourceLoc const &Location) const {
177 return MatchesName(Name) && MatchesLocation(Location);
178 }
179
180 bool PartiallyMatches(StringRef Name, FullSourceLoc const &Location) const {
181 return MatchesName(Name) || MatchesLocation(Location);
182 }
183
184 bool MatchesName(StringRef Name) const {
185 return Name == ExpectedName;
186 }
187
188 bool MatchesLocation(FullSourceLoc const &Location) const {
189 return Location.isValid() &&
190 Location.getSpellingLineNumber() == LineNumber &&
191 Location.getSpellingColumnNumber() == ColumnNumber;
192 }
193
194 friend std::ostream &operator<<(std::ostream &Stream,
195 MatchCandidate const &Match) {
196 return Stream << Match.ExpectedName
197 << " at " << Match.LineNumber << ":" << Match.ColumnNumber;
198 }
199 };
200
201 struct ExpectedMatch {
202 ExpectedMatch(Twine Name, unsigned LineNumber, unsigned ColumnNumber)
203 : Candidate(Name, LineNumber, ColumnNumber), Found(false) {}
204
205 void UpdateFor(StringRef Name, FullSourceLoc Location, SourceManager &SM) {
206 if (Candidate.Matches(Name, Location)) {
207 EXPECT_TRUE(!Found);
208 Found = true;
209 } else if (!Found && Candidate.PartiallyMatches(Name, Location)) {
210 llvm::raw_string_ostream Stream(PartialMatches);
211 Stream << ", partial match: \"" << Name << "\" at ";
212 Location.print(Stream, SM);
213 }
214 }
215
216 void ExpectFound() const {
217 EXPECT_TRUE(Found)
218 << "Expected \"" << Candidate.ExpectedName
219 << "\" at " << Candidate.LineNumber
220 << ":" << Candidate.ColumnNumber << PartialMatches;
221 }
222
223 MatchCandidate Candidate;
224 std::string PartialMatches;
225 bool Found;
226 };
227
228 std::vector<MatchCandidate> DisallowedMatches;
229 std::vector<ExpectedMatch> ExpectedMatches;
Richard Smith87d8fb92012-06-24 23:56:26 +0000230};
231}
232
233#endif /* LLVM_CLANG_TEST_VISITOR_H */