blob: ce3246a902f13813f07563fc091cff8a08ca6236 [file] [log] [blame]
Richard Smithbc9e5582012-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 Dennett8268fe72012-08-24 06:59:51 +00009///
10/// \file
11/// \brief Defines utility templates for RecursiveASTVisitor related tests.
12///
Richard Smithbc9e5582012-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 Carruth1050e8b2012-12-04 09:45:34 +000019#include "clang/AST/ASTContext.h"
Richard Smithbc9e5582012-06-24 23:56:26 +000020#include "clang/AST/RecursiveASTVisitor.h"
Richard Smithbc9e5582012-06-24 23:56:26 +000021#include "clang/Frontend/CompilerInstance.h"
Chandler Carruth1050e8b2012-12-04 09:45:34 +000022#include "clang/Frontend/FrontendAction.h"
Richard Smithbc9e5582012-06-24 23:56:26 +000023#include "clang/Tooling/Tooling.h"
24#include "gtest/gtest.h"
Chandler Carruth1050e8b2012-12-04 09:45:34 +000025#include <vector>
Richard Smithbc9e5582012-06-24 23:56:26 +000026
27namespace clang {
28
Matt Beaumont-Gay8f35d232012-06-25 18:27:11 +000029/// \brief Base class for simple RecursiveASTVisitor based tests.
Richard Smithbc9e5582012-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///
James Dennett8268fe72012-08-24 06:59:51 +000034/// Visits template instantiations (but not implicit code) by default.
Richard Smithbc9e5582012-06-24 23:56:26 +000035template <typename T>
36class TestVisitor : public RecursiveASTVisitor<T> {
37public:
38 TestVisitor() { }
39
Matt Beaumont-Gay8f35d232012-06-25 18:27:11 +000040 virtual ~TestVisitor() { }
41
Richard Smith82b45502012-08-17 21:23:17 +000042 enum Language { Lang_C, Lang_CXX };
43
Richard Smithbc9e5582012-06-24 23:56:26 +000044 /// \brief Runs the current AST visitor over the given code.
Richard Smith82b45502012-08-17 21:23:17 +000045 bool runOver(StringRef Code, Language L = Lang_CXX) {
Nico Weber56669882012-08-30 02:02:19 +000046 std::vector<std::string> Args;
47 switch (L) {
48 case Lang_C: Args.push_back("-std=c99"); break;
49 case Lang_CXX: Args.push_back("-std=c++98"); break;
50 }
51 return tooling::runToolOnCodeWithArgs(CreateTestAction(), Code, Args);
Richard Smithbc9e5582012-06-24 23:56:26 +000052 }
53
54 bool shouldVisitTemplateInstantiations() const {
55 return true;
56 }
57
58protected:
59 virtual ASTFrontendAction* CreateTestAction() {
60 return new TestAction(this);
61 }
62
63 class FindConsumer : public ASTConsumer {
64 public:
65 FindConsumer(TestVisitor *Visitor) : Visitor(Visitor) {}
66
67 virtual void HandleTranslationUnit(clang::ASTContext &Context) {
Manuel Klimeke6df0ce2012-07-05 18:13:01 +000068 Visitor->Context = &Context;
Richard Smithbc9e5582012-06-24 23:56:26 +000069 Visitor->TraverseDecl(Context.getTranslationUnitDecl());
70 }
71
72 private:
73 TestVisitor *Visitor;
74 };
75
76 class TestAction : public ASTFrontendAction {
77 public:
78 TestAction(TestVisitor *Visitor) : Visitor(Visitor) {}
79
80 virtual clang::ASTConsumer* CreateASTConsumer(
Manuel Klimeke6df0ce2012-07-05 18:13:01 +000081 CompilerInstance&, llvm::StringRef dummy) {
Richard Smithbc9e5582012-06-24 23:56:26 +000082 /// TestConsumer will be deleted by the framework calling us.
83 return new FindConsumer(Visitor);
84 }
85
86 protected:
87 TestVisitor *Visitor;
88 };
89
90 ASTContext *Context;
91};
92
James Dennett8268fe72012-08-24 06:59:51 +000093/// \brief A RecursiveASTVisitor to check that certain matches are (or are
94/// not) observed during visitation.
Richard Smithbc9e5582012-06-24 23:56:26 +000095///
James Dennett8268fe72012-08-24 06:59:51 +000096/// This is a RecursiveASTVisitor for testing the RecursiveASTVisitor itself,
97/// and allows simple creation of test visitors running matches on only a small
Richard Smithbc9e5582012-06-24 23:56:26 +000098/// subset of the Visit* methods.
99template <typename T, template <typename> class Visitor = TestVisitor>
100class ExpectedLocationVisitor : public Visitor<T> {
101public:
James Dennett8268fe72012-08-24 06:59:51 +0000102 /// \brief Expect 'Match' *not* to occur at the given 'Line' and 'Column'.
103 ///
104 /// Any number of matches can be disallowed.
105 void DisallowMatch(Twine Match, unsigned Line, unsigned Column) {
106 DisallowedMatches.push_back(MatchCandidate(Match, Line, Column));
Richard Smithbc9e5582012-06-24 23:56:26 +0000107 }
108
109 /// \brief Expect 'Match' to occur at the given 'Line' and 'Column'.
James Dennett8268fe72012-08-24 06:59:51 +0000110 ///
111 /// Any number of expected matches can be set by calling this repeatedly.
112 /// Each is expected to be matched exactly once.
Richard Smithbc9e5582012-06-24 23:56:26 +0000113 void ExpectMatch(Twine Match, unsigned Line, unsigned Column) {
James Dennett8268fe72012-08-24 06:59:51 +0000114 ExpectedMatches.push_back(ExpectedMatch(Match, Line, Column));
115 }
116
117 /// \brief Checks that all expected matches have been found.
118 virtual ~ExpectedLocationVisitor() {
119 for (typename std::vector<ExpectedMatch>::const_iterator
120 It = ExpectedMatches.begin(), End = ExpectedMatches.end();
121 It != End; ++It) {
122 It->ExpectFound();
123 }
Richard Smithbc9e5582012-06-24 23:56:26 +0000124 }
125
126protected:
James Dennett8268fe72012-08-24 06:59:51 +0000127 /// \brief Checks an actual match against expected and disallowed matches.
Richard Smithbc9e5582012-06-24 23:56:26 +0000128 ///
129 /// Implementations are required to call this with appropriate values
130 /// for 'Name' during visitation.
131 void Match(StringRef Name, SourceLocation Location) {
James Dennett8268fe72012-08-24 06:59:51 +0000132 const FullSourceLoc FullLocation = this->Context->getFullLoc(Location);
133
134 for (typename std::vector<MatchCandidate>::const_iterator
135 It = DisallowedMatches.begin(), End = DisallowedMatches.end();
136 It != End; ++It) {
137 EXPECT_FALSE(It->Matches(Name, FullLocation))
138 << "Matched disallowed " << *It;
139 }
140
141 for (typename std::vector<ExpectedMatch>::iterator
142 It = ExpectedMatches.begin(), End = ExpectedMatches.end();
143 It != End; ++It) {
144 It->UpdateFor(Name, FullLocation, this->Context->getSourceManager());
Richard Smithbc9e5582012-06-24 23:56:26 +0000145 }
146 }
147
James Dennett8268fe72012-08-24 06:59:51 +0000148 private:
149 struct MatchCandidate {
150 std::string ExpectedName;
151 unsigned LineNumber;
152 unsigned ColumnNumber;
153
154 MatchCandidate(Twine Name, unsigned LineNumber, unsigned ColumnNumber)
155 : ExpectedName(Name.str()), LineNumber(LineNumber),
156 ColumnNumber(ColumnNumber) {
157 }
158
159 bool Matches(StringRef Name, FullSourceLoc const &Location) const {
160 return MatchesName(Name) && MatchesLocation(Location);
161 }
162
163 bool PartiallyMatches(StringRef Name, FullSourceLoc const &Location) const {
164 return MatchesName(Name) || MatchesLocation(Location);
165 }
166
167 bool MatchesName(StringRef Name) const {
168 return Name == ExpectedName;
169 }
170
171 bool MatchesLocation(FullSourceLoc const &Location) const {
172 return Location.isValid() &&
173 Location.getSpellingLineNumber() == LineNumber &&
174 Location.getSpellingColumnNumber() == ColumnNumber;
175 }
176
177 friend std::ostream &operator<<(std::ostream &Stream,
178 MatchCandidate const &Match) {
179 return Stream << Match.ExpectedName
180 << " at " << Match.LineNumber << ":" << Match.ColumnNumber;
181 }
182 };
183
184 struct ExpectedMatch {
185 ExpectedMatch(Twine Name, unsigned LineNumber, unsigned ColumnNumber)
186 : Candidate(Name, LineNumber, ColumnNumber), Found(false) {}
187
188 void UpdateFor(StringRef Name, FullSourceLoc Location, SourceManager &SM) {
189 if (Candidate.Matches(Name, Location)) {
190 EXPECT_TRUE(!Found);
191 Found = true;
192 } else if (!Found && Candidate.PartiallyMatches(Name, Location)) {
193 llvm::raw_string_ostream Stream(PartialMatches);
194 Stream << ", partial match: \"" << Name << "\" at ";
195 Location.print(Stream, SM);
196 }
197 }
198
199 void ExpectFound() const {
200 EXPECT_TRUE(Found)
201 << "Expected \"" << Candidate.ExpectedName
202 << "\" at " << Candidate.LineNumber
203 << ":" << Candidate.ColumnNumber << PartialMatches;
204 }
205
206 MatchCandidate Candidate;
207 std::string PartialMatches;
208 bool Found;
209 };
210
211 std::vector<MatchCandidate> DisallowedMatches;
212 std::vector<ExpectedMatch> ExpectedMatches;
Richard Smithbc9e5582012-06-24 23:56:26 +0000213};
214}
215
216#endif /* LLVM_CLANG_TEST_VISITOR_H */