blob: 44ae63a8f557dbeb3d90071876039634ace690a6 [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) {
47 // FIXME: The input language is determined based on the provided filename.
48 static const StringRef Filenames[] = { "input.c", "input.cc" };
49 return tooling::runToolOnCode(CreateTestAction(), Code, Filenames[L]);
Richard Smithbc9e5582012-06-24 23:56:26 +000050 }
51
52 bool shouldVisitTemplateInstantiations() const {
53 return true;
54 }
55
56protected:
57 virtual ASTFrontendAction* CreateTestAction() {
58 return new TestAction(this);
59 }
60
61 class FindConsumer : public ASTConsumer {
62 public:
63 FindConsumer(TestVisitor *Visitor) : Visitor(Visitor) {}
64
65 virtual void HandleTranslationUnit(clang::ASTContext &Context) {
Manuel Klimeke6df0ce2012-07-05 18:13:01 +000066 Visitor->Context = &Context;
Richard Smithbc9e5582012-06-24 23:56:26 +000067 Visitor->TraverseDecl(Context.getTranslationUnitDecl());
68 }
69
70 private:
71 TestVisitor *Visitor;
72 };
73
74 class TestAction : public ASTFrontendAction {
75 public:
76 TestAction(TestVisitor *Visitor) : Visitor(Visitor) {}
77
78 virtual clang::ASTConsumer* CreateASTConsumer(
Manuel Klimeke6df0ce2012-07-05 18:13:01 +000079 CompilerInstance&, llvm::StringRef dummy) {
Richard Smithbc9e5582012-06-24 23:56:26 +000080 /// TestConsumer will be deleted by the framework calling us.
81 return new FindConsumer(Visitor);
82 }
83
84 protected:
85 TestVisitor *Visitor;
86 };
87
88 ASTContext *Context;
89};
90
James Dennett8268fe72012-08-24 06:59:51 +000091/// \brief A RecursiveASTVisitor to check that certain matches are (or are
92/// not) observed during visitation.
Richard Smithbc9e5582012-06-24 23:56:26 +000093///
James Dennett8268fe72012-08-24 06:59:51 +000094/// This is a RecursiveASTVisitor for testing the RecursiveASTVisitor itself,
95/// and allows simple creation of test visitors running matches on only a small
Richard Smithbc9e5582012-06-24 23:56:26 +000096/// subset of the Visit* methods.
97template <typename T, template <typename> class Visitor = TestVisitor>
98class ExpectedLocationVisitor : public Visitor<T> {
99public:
James Dennett8268fe72012-08-24 06:59:51 +0000100 /// \brief Expect 'Match' *not* to occur at the given 'Line' and 'Column'.
101 ///
102 /// Any number of matches can be disallowed.
103 void DisallowMatch(Twine Match, unsigned Line, unsigned Column) {
104 DisallowedMatches.push_back(MatchCandidate(Match, Line, Column));
Richard Smithbc9e5582012-06-24 23:56:26 +0000105 }
106
107 /// \brief Expect 'Match' to occur at the given 'Line' and 'Column'.
James Dennett8268fe72012-08-24 06:59:51 +0000108 ///
109 /// Any number of expected matches can be set by calling this repeatedly.
110 /// Each is expected to be matched exactly once.
Richard Smithbc9e5582012-06-24 23:56:26 +0000111 void ExpectMatch(Twine Match, unsigned Line, unsigned Column) {
James Dennett8268fe72012-08-24 06:59:51 +0000112 ExpectedMatches.push_back(ExpectedMatch(Match, Line, Column));
113 }
114
115 /// \brief Checks that all expected matches have been found.
116 virtual ~ExpectedLocationVisitor() {
117 for (typename std::vector<ExpectedMatch>::const_iterator
118 It = ExpectedMatches.begin(), End = ExpectedMatches.end();
119 It != End; ++It) {
120 It->ExpectFound();
121 }
Richard Smithbc9e5582012-06-24 23:56:26 +0000122 }
123
124protected:
James Dennett8268fe72012-08-24 06:59:51 +0000125 /// \brief Checks an actual match against expected and disallowed matches.
Richard Smithbc9e5582012-06-24 23:56:26 +0000126 ///
127 /// Implementations are required to call this with appropriate values
128 /// for 'Name' during visitation.
129 void Match(StringRef Name, SourceLocation Location) {
James Dennett8268fe72012-08-24 06:59:51 +0000130 const FullSourceLoc FullLocation = this->Context->getFullLoc(Location);
131
132 for (typename std::vector<MatchCandidate>::const_iterator
133 It = DisallowedMatches.begin(), End = DisallowedMatches.end();
134 It != End; ++It) {
135 EXPECT_FALSE(It->Matches(Name, FullLocation))
136 << "Matched disallowed " << *It;
137 }
138
139 for (typename std::vector<ExpectedMatch>::iterator
140 It = ExpectedMatches.begin(), End = ExpectedMatches.end();
141 It != End; ++It) {
142 It->UpdateFor(Name, FullLocation, this->Context->getSourceManager());
Richard Smithbc9e5582012-06-24 23:56:26 +0000143 }
144 }
145
James Dennett8268fe72012-08-24 06:59:51 +0000146 private:
147 struct MatchCandidate {
148 std::string ExpectedName;
149 unsigned LineNumber;
150 unsigned ColumnNumber;
151
152 MatchCandidate(Twine Name, unsigned LineNumber, unsigned ColumnNumber)
153 : ExpectedName(Name.str()), LineNumber(LineNumber),
154 ColumnNumber(ColumnNumber) {
155 }
156
157 bool Matches(StringRef Name, FullSourceLoc const &Location) const {
158 return MatchesName(Name) && MatchesLocation(Location);
159 }
160
161 bool PartiallyMatches(StringRef Name, FullSourceLoc const &Location) const {
162 return MatchesName(Name) || MatchesLocation(Location);
163 }
164
165 bool MatchesName(StringRef Name) const {
166 return Name == ExpectedName;
167 }
168
169 bool MatchesLocation(FullSourceLoc const &Location) const {
170 return Location.isValid() &&
171 Location.getSpellingLineNumber() == LineNumber &&
172 Location.getSpellingColumnNumber() == ColumnNumber;
173 }
174
175 friend std::ostream &operator<<(std::ostream &Stream,
176 MatchCandidate const &Match) {
177 return Stream << Match.ExpectedName
178 << " at " << Match.LineNumber << ":" << Match.ColumnNumber;
179 }
180 };
181
182 struct ExpectedMatch {
183 ExpectedMatch(Twine Name, unsigned LineNumber, unsigned ColumnNumber)
184 : Candidate(Name, LineNumber, ColumnNumber), Found(false) {}
185
186 void UpdateFor(StringRef Name, FullSourceLoc Location, SourceManager &SM) {
187 if (Candidate.Matches(Name, Location)) {
188 EXPECT_TRUE(!Found);
189 Found = true;
190 } else if (!Found && Candidate.PartiallyMatches(Name, Location)) {
191 llvm::raw_string_ostream Stream(PartialMatches);
192 Stream << ", partial match: \"" << Name << "\" at ";
193 Location.print(Stream, SM);
194 }
195 }
196
197 void ExpectFound() const {
198 EXPECT_TRUE(Found)
199 << "Expected \"" << Candidate.ExpectedName
200 << "\" at " << Candidate.LineNumber
201 << ":" << Candidate.ColumnNumber << PartialMatches;
202 }
203
204 MatchCandidate Candidate;
205 std::string PartialMatches;
206 bool Found;
207 };
208
209 std::vector<MatchCandidate> DisallowedMatches;
210 std::vector<ExpectedMatch> ExpectedMatches;
Richard Smithbc9e5582012-06-24 23:56:26 +0000211};
212}
213
214#endif /* LLVM_CLANG_TEST_VISITOR_H */