blob: 8333c24a68807870d922c73b14daab756fb3e1a2 [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
James Dennett8268fe72012-08-24 06:59:51 +000018#include <vector>
19
Benjamin Kramera13d2bc2012-07-04 20:33:53 +000020#include "clang/AST/ASTContext.h"
Richard Smithbc9e5582012-06-24 23:56:26 +000021#include "clang/AST/ASTConsumer.h"
22#include "clang/AST/RecursiveASTVisitor.h"
23#include "clang/Frontend/FrontendAction.h"
24#include "clang/Frontend/CompilerInstance.h"
25#include "clang/Tooling/Tooling.h"
26#include "gtest/gtest.h"
27
28namespace clang {
29
Matt Beaumont-Gay8f35d232012-06-25 18:27:11 +000030/// \brief Base class for simple RecursiveASTVisitor based tests.
Richard Smithbc9e5582012-06-24 23:56:26 +000031///
32/// This is a drop-in replacement for RecursiveASTVisitor itself, with the
33/// additional capability of running it over a snippet of code.
34///
James Dennett8268fe72012-08-24 06:59:51 +000035/// Visits template instantiations (but not implicit code) by default.
Richard Smithbc9e5582012-06-24 23:56:26 +000036template <typename T>
37class TestVisitor : public RecursiveASTVisitor<T> {
38public:
39 TestVisitor() { }
40
Matt Beaumont-Gay8f35d232012-06-25 18:27:11 +000041 virtual ~TestVisitor() { }
42
Richard Smith82b45502012-08-17 21:23:17 +000043 enum Language { Lang_C, Lang_CXX };
44
Richard Smithbc9e5582012-06-24 23:56:26 +000045 /// \brief Runs the current AST visitor over the given code.
Richard Smith82b45502012-08-17 21:23:17 +000046 bool runOver(StringRef Code, Language L = Lang_CXX) {
Nico Weber56669882012-08-30 02:02:19 +000047 std::vector<std::string> Args;
48 switch (L) {
49 case Lang_C: Args.push_back("-std=c99"); break;
50 case Lang_CXX: Args.push_back("-std=c++98"); break;
51 }
52 return tooling::runToolOnCodeWithArgs(CreateTestAction(), Code, Args);
Richard Smithbc9e5582012-06-24 23:56:26 +000053 }
54
55 bool shouldVisitTemplateInstantiations() const {
56 return true;
57 }
58
59protected:
60 virtual ASTFrontendAction* CreateTestAction() {
61 return new TestAction(this);
62 }
63
64 class FindConsumer : public ASTConsumer {
65 public:
66 FindConsumer(TestVisitor *Visitor) : Visitor(Visitor) {}
67
68 virtual void HandleTranslationUnit(clang::ASTContext &Context) {
Manuel Klimeke6df0ce2012-07-05 18:13:01 +000069 Visitor->Context = &Context;
Richard Smithbc9e5582012-06-24 23:56:26 +000070 Visitor->TraverseDecl(Context.getTranslationUnitDecl());
71 }
72
73 private:
74 TestVisitor *Visitor;
75 };
76
77 class TestAction : public ASTFrontendAction {
78 public:
79 TestAction(TestVisitor *Visitor) : Visitor(Visitor) {}
80
81 virtual clang::ASTConsumer* CreateASTConsumer(
Manuel Klimeke6df0ce2012-07-05 18:13:01 +000082 CompilerInstance&, llvm::StringRef dummy) {
Richard Smithbc9e5582012-06-24 23:56:26 +000083 /// TestConsumer will be deleted by the framework calling us.
84 return new FindConsumer(Visitor);
85 }
86
87 protected:
88 TestVisitor *Visitor;
89 };
90
91 ASTContext *Context;
92};
93
James Dennett8268fe72012-08-24 06:59:51 +000094/// \brief A RecursiveASTVisitor to check that certain matches are (or are
95/// not) observed during visitation.
Richard Smithbc9e5582012-06-24 23:56:26 +000096///
James Dennett8268fe72012-08-24 06:59:51 +000097/// This is a RecursiveASTVisitor for testing the RecursiveASTVisitor itself,
98/// and allows simple creation of test visitors running matches on only a small
Richard Smithbc9e5582012-06-24 23:56:26 +000099/// subset of the Visit* methods.
100template <typename T, template <typename> class Visitor = TestVisitor>
101class ExpectedLocationVisitor : public Visitor<T> {
102public:
James Dennett8268fe72012-08-24 06:59:51 +0000103 /// \brief Expect 'Match' *not* to occur at the given 'Line' and 'Column'.
104 ///
105 /// Any number of matches can be disallowed.
106 void DisallowMatch(Twine Match, unsigned Line, unsigned Column) {
107 DisallowedMatches.push_back(MatchCandidate(Match, Line, Column));
Richard Smithbc9e5582012-06-24 23:56:26 +0000108 }
109
110 /// \brief Expect 'Match' to occur at the given 'Line' and 'Column'.
James Dennett8268fe72012-08-24 06:59:51 +0000111 ///
112 /// Any number of expected matches can be set by calling this repeatedly.
113 /// Each is expected to be matched exactly once.
Richard Smithbc9e5582012-06-24 23:56:26 +0000114 void ExpectMatch(Twine Match, unsigned Line, unsigned Column) {
James Dennett8268fe72012-08-24 06:59:51 +0000115 ExpectedMatches.push_back(ExpectedMatch(Match, Line, Column));
116 }
117
118 /// \brief Checks that all expected matches have been found.
119 virtual ~ExpectedLocationVisitor() {
120 for (typename std::vector<ExpectedMatch>::const_iterator
121 It = ExpectedMatches.begin(), End = ExpectedMatches.end();
122 It != End; ++It) {
123 It->ExpectFound();
124 }
Richard Smithbc9e5582012-06-24 23:56:26 +0000125 }
126
127protected:
James Dennett8268fe72012-08-24 06:59:51 +0000128 /// \brief Checks an actual match against expected and disallowed matches.
Richard Smithbc9e5582012-06-24 23:56:26 +0000129 ///
130 /// Implementations are required to call this with appropriate values
131 /// for 'Name' during visitation.
132 void Match(StringRef Name, SourceLocation Location) {
James Dennett8268fe72012-08-24 06:59:51 +0000133 const FullSourceLoc FullLocation = this->Context->getFullLoc(Location);
134
135 for (typename std::vector<MatchCandidate>::const_iterator
136 It = DisallowedMatches.begin(), End = DisallowedMatches.end();
137 It != End; ++It) {
138 EXPECT_FALSE(It->Matches(Name, FullLocation))
139 << "Matched disallowed " << *It;
140 }
141
142 for (typename std::vector<ExpectedMatch>::iterator
143 It = ExpectedMatches.begin(), End = ExpectedMatches.end();
144 It != End; ++It) {
145 It->UpdateFor(Name, FullLocation, this->Context->getSourceManager());
Richard Smithbc9e5582012-06-24 23:56:26 +0000146 }
147 }
148
James Dennett8268fe72012-08-24 06:59:51 +0000149 private:
150 struct MatchCandidate {
151 std::string ExpectedName;
152 unsigned LineNumber;
153 unsigned ColumnNumber;
154
155 MatchCandidate(Twine Name, unsigned LineNumber, unsigned ColumnNumber)
156 : ExpectedName(Name.str()), LineNumber(LineNumber),
157 ColumnNumber(ColumnNumber) {
158 }
159
160 bool Matches(StringRef Name, FullSourceLoc const &Location) const {
161 return MatchesName(Name) && MatchesLocation(Location);
162 }
163
164 bool PartiallyMatches(StringRef Name, FullSourceLoc const &Location) const {
165 return MatchesName(Name) || MatchesLocation(Location);
166 }
167
168 bool MatchesName(StringRef Name) const {
169 return Name == ExpectedName;
170 }
171
172 bool MatchesLocation(FullSourceLoc const &Location) const {
173 return Location.isValid() &&
174 Location.getSpellingLineNumber() == LineNumber &&
175 Location.getSpellingColumnNumber() == ColumnNumber;
176 }
177
178 friend std::ostream &operator<<(std::ostream &Stream,
179 MatchCandidate const &Match) {
180 return Stream << Match.ExpectedName
181 << " at " << Match.LineNumber << ":" << Match.ColumnNumber;
182 }
183 };
184
185 struct ExpectedMatch {
186 ExpectedMatch(Twine Name, unsigned LineNumber, unsigned ColumnNumber)
187 : Candidate(Name, LineNumber, ColumnNumber), Found(false) {}
188
189 void UpdateFor(StringRef Name, FullSourceLoc Location, SourceManager &SM) {
190 if (Candidate.Matches(Name, Location)) {
191 EXPECT_TRUE(!Found);
192 Found = true;
193 } else if (!Found && Candidate.PartiallyMatches(Name, Location)) {
194 llvm::raw_string_ostream Stream(PartialMatches);
195 Stream << ", partial match: \"" << Name << "\" at ";
196 Location.print(Stream, SM);
197 }
198 }
199
200 void ExpectFound() const {
201 EXPECT_TRUE(Found)
202 << "Expected \"" << Candidate.ExpectedName
203 << "\" at " << Candidate.LineNumber
204 << ":" << Candidate.ColumnNumber << PartialMatches;
205 }
206
207 MatchCandidate Candidate;
208 std::string PartialMatches;
209 bool Found;
210 };
211
212 std::vector<MatchCandidate> DisallowedMatches;
213 std::vector<ExpectedMatch> ExpectedMatches;
Richard Smithbc9e5582012-06-24 23:56:26 +0000214};
215}
216
217#endif /* LLVM_CLANG_TEST_VISITOR_H */