blob: ec751c350e068be7bb347aed3541a02e6c654dc2 [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
James Dennetteb576c02013-06-30 03:05:49 +000042 enum Language { Lang_C, Lang_CXX98, Lang_CXX11, Lang_CXX=Lang_CXX98 };
Richard Smith87deab32012-08-17 21:23:17 +000043
Richard Smith87d8fb92012-06-24 23:56:26 +000044 /// \brief Runs the current AST visitor over the given code.
Richard Smith87deab32012-08-17 21:23:17 +000045 bool runOver(StringRef Code, Language L = Lang_CXX) {
Nico Weber077a53e2012-08-30 02:02:19 +000046 std::vector<std::string> Args;
47 switch (L) {
48 case Lang_C: Args.push_back("-std=c99"); break;
James Dennetteb576c02013-06-30 03:05:49 +000049 case Lang_CXX98: Args.push_back("-std=c++98"); break;
50 case Lang_CXX11: Args.push_back("-std=c++11"); break;
Nico Weber077a53e2012-08-30 02:02:19 +000051 }
52 return tooling::runToolOnCodeWithArgs(CreateTestAction(), Code, Args);
Richard Smith87d8fb92012-06-24 23:56:26 +000053 }
54
55 bool shouldVisitTemplateInstantiations() const {
56 return true;
57 }
58
Michael Hanc90d12d2013-09-11 15:53:29 +000059 bool shouldVisitImplicitCode() const {
60 return true;
61 }
62
Richard Smith87d8fb92012-06-24 23:56:26 +000063protected:
64 virtual ASTFrontendAction* CreateTestAction() {
65 return new TestAction(this);
66 }
67
68 class FindConsumer : public ASTConsumer {
69 public:
70 FindConsumer(TestVisitor *Visitor) : Visitor(Visitor) {}
71
72 virtual void HandleTranslationUnit(clang::ASTContext &Context) {
Manuel Klimek5da9dcb2012-07-05 18:13:01 +000073 Visitor->Context = &Context;
Richard Smith87d8fb92012-06-24 23:56:26 +000074 Visitor->TraverseDecl(Context.getTranslationUnitDecl());
75 }
76
77 private:
78 TestVisitor *Visitor;
79 };
80
81 class TestAction : public ASTFrontendAction {
82 public:
83 TestAction(TestVisitor *Visitor) : Visitor(Visitor) {}
84
85 virtual clang::ASTConsumer* CreateASTConsumer(
Manuel Klimek5da9dcb2012-07-05 18:13:01 +000086 CompilerInstance&, llvm::StringRef dummy) {
Richard Smith87d8fb92012-06-24 23:56:26 +000087 /// TestConsumer will be deleted by the framework calling us.
88 return new FindConsumer(Visitor);
89 }
90
91 protected:
92 TestVisitor *Visitor;
93 };
94
95 ASTContext *Context;
96};
97
James Dennett75c100b2012-08-24 06:59:51 +000098/// \brief A RecursiveASTVisitor to check that certain matches are (or are
99/// not) observed during visitation.
Richard Smith87d8fb92012-06-24 23:56:26 +0000100///
James Dennett75c100b2012-08-24 06:59:51 +0000101/// This is a RecursiveASTVisitor for testing the RecursiveASTVisitor itself,
102/// and allows simple creation of test visitors running matches on only a small
Richard Smith87d8fb92012-06-24 23:56:26 +0000103/// subset of the Visit* methods.
104template <typename T, template <typename> class Visitor = TestVisitor>
105class ExpectedLocationVisitor : public Visitor<T> {
106public:
James Dennett75c100b2012-08-24 06:59:51 +0000107 /// \brief Expect 'Match' *not* to occur at the given 'Line' and 'Column'.
108 ///
109 /// Any number of matches can be disallowed.
110 void DisallowMatch(Twine Match, unsigned Line, unsigned Column) {
111 DisallowedMatches.push_back(MatchCandidate(Match, Line, Column));
Richard Smith87d8fb92012-06-24 23:56:26 +0000112 }
113
114 /// \brief Expect 'Match' to occur at the given 'Line' and 'Column'.
James Dennett75c100b2012-08-24 06:59:51 +0000115 ///
116 /// Any number of expected matches can be set by calling this repeatedly.
117 /// Each is expected to be matched exactly once.
Richard Smith87d8fb92012-06-24 23:56:26 +0000118 void ExpectMatch(Twine Match, unsigned Line, unsigned Column) {
James Dennett75c100b2012-08-24 06:59:51 +0000119 ExpectedMatches.push_back(ExpectedMatch(Match, Line, Column));
120 }
121
122 /// \brief Checks that all expected matches have been found.
123 virtual ~ExpectedLocationVisitor() {
124 for (typename std::vector<ExpectedMatch>::const_iterator
125 It = ExpectedMatches.begin(), End = ExpectedMatches.end();
126 It != End; ++It) {
127 It->ExpectFound();
128 }
Richard Smith87d8fb92012-06-24 23:56:26 +0000129 }
130
131protected:
James Dennett75c100b2012-08-24 06:59:51 +0000132 /// \brief Checks an actual match against expected and disallowed matches.
Richard Smith87d8fb92012-06-24 23:56:26 +0000133 ///
134 /// Implementations are required to call this with appropriate values
135 /// for 'Name' during visitation.
136 void Match(StringRef Name, SourceLocation Location) {
James Dennett75c100b2012-08-24 06:59:51 +0000137 const FullSourceLoc FullLocation = this->Context->getFullLoc(Location);
138
139 for (typename std::vector<MatchCandidate>::const_iterator
140 It = DisallowedMatches.begin(), End = DisallowedMatches.end();
141 It != End; ++It) {
142 EXPECT_FALSE(It->Matches(Name, FullLocation))
143 << "Matched disallowed " << *It;
144 }
145
146 for (typename std::vector<ExpectedMatch>::iterator
147 It = ExpectedMatches.begin(), End = ExpectedMatches.end();
148 It != End; ++It) {
149 It->UpdateFor(Name, FullLocation, this->Context->getSourceManager());
Richard Smith87d8fb92012-06-24 23:56:26 +0000150 }
151 }
152
James Dennett75c100b2012-08-24 06:59:51 +0000153 private:
154 struct MatchCandidate {
155 std::string ExpectedName;
156 unsigned LineNumber;
157 unsigned ColumnNumber;
158
159 MatchCandidate(Twine Name, unsigned LineNumber, unsigned ColumnNumber)
160 : ExpectedName(Name.str()), LineNumber(LineNumber),
161 ColumnNumber(ColumnNumber) {
162 }
163
164 bool Matches(StringRef Name, FullSourceLoc const &Location) const {
165 return MatchesName(Name) && MatchesLocation(Location);
166 }
167
168 bool PartiallyMatches(StringRef Name, FullSourceLoc const &Location) const {
169 return MatchesName(Name) || MatchesLocation(Location);
170 }
171
172 bool MatchesName(StringRef Name) const {
173 return Name == ExpectedName;
174 }
175
176 bool MatchesLocation(FullSourceLoc const &Location) const {
177 return Location.isValid() &&
178 Location.getSpellingLineNumber() == LineNumber &&
179 Location.getSpellingColumnNumber() == ColumnNumber;
180 }
181
182 friend std::ostream &operator<<(std::ostream &Stream,
183 MatchCandidate const &Match) {
184 return Stream << Match.ExpectedName
185 << " at " << Match.LineNumber << ":" << Match.ColumnNumber;
186 }
187 };
188
189 struct ExpectedMatch {
190 ExpectedMatch(Twine Name, unsigned LineNumber, unsigned ColumnNumber)
191 : Candidate(Name, LineNumber, ColumnNumber), Found(false) {}
192
193 void UpdateFor(StringRef Name, FullSourceLoc Location, SourceManager &SM) {
194 if (Candidate.Matches(Name, Location)) {
195 EXPECT_TRUE(!Found);
196 Found = true;
197 } else if (!Found && Candidate.PartiallyMatches(Name, Location)) {
198 llvm::raw_string_ostream Stream(PartialMatches);
199 Stream << ", partial match: \"" << Name << "\" at ";
200 Location.print(Stream, SM);
201 }
202 }
203
204 void ExpectFound() const {
205 EXPECT_TRUE(Found)
206 << "Expected \"" << Candidate.ExpectedName
207 << "\" at " << Candidate.LineNumber
208 << ":" << Candidate.ColumnNumber << PartialMatches;
209 }
210
211 MatchCandidate Candidate;
212 std::string PartialMatches;
213 bool Found;
214 };
215
216 std::vector<MatchCandidate> DisallowedMatches;
217 std::vector<ExpectedMatch> ExpectedMatches;
Richard Smith87d8fb92012-06-24 23:56:26 +0000218};
219}
220
221#endif /* LLVM_CLANG_TEST_VISITOR_H */