1 //===--- TestVisitor.h ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Defines utility templates for RecursiveASTVisitor related tests.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_UNITTESTS_TOOLING_TESTVISITOR_H
16 #define LLVM_CLANG_UNITTESTS_TOOLING_TESTVISITOR_H
18 #include "clang/AST/ASTConsumer.h"
19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/RecursiveASTVisitor.h"
21 #include "clang/Frontend/CompilerInstance.h"
22 #include "clang/Frontend/FrontendAction.h"
23 #include "clang/Tooling/Tooling.h"
24 #include "gtest/gtest.h"
29 /// \brief Base class for simple RecursiveASTVisitor based tests.
31 /// This is a drop-in replacement for RecursiveASTVisitor itself, with the
32 /// additional capability of running it over a snippet of code.
34 /// Visits template instantiations and implicit code by default.
36 class TestVisitor : public RecursiveASTVisitor<T> {
40 virtual ~TestVisitor() { }
51 /// \brief Runs the current AST visitor over the given code.
52 bool runOver(StringRef Code, Language L = Lang_CXX) {
53 std::vector<std::string> Args;
55 case Lang_C: Args.push_back("-std=c99"); break;
56 case Lang_CXX98: Args.push_back("-std=c++98"); break;
57 case Lang_CXX11: Args.push_back("-std=c++11"); break;
58 case Lang_OBJC: Args.push_back("-ObjC"); break;
60 Args.push_back("-ObjC++");
61 Args.push_back("-std=c++11");
62 Args.push_back("-fblocks");
65 return tooling::runToolOnCodeWithArgs(CreateTestAction(), Code, Args);
68 bool shouldVisitTemplateInstantiations() const {
72 bool shouldVisitImplicitCode() const {
77 virtual ASTFrontendAction* CreateTestAction() {
78 return new TestAction(this);
81 class FindConsumer : public ASTConsumer {
83 FindConsumer(TestVisitor *Visitor) : Visitor(Visitor) {}
85 void HandleTranslationUnit(clang::ASTContext &Context) override {
86 Visitor->Context = &Context;
87 Visitor->TraverseDecl(Context.getTranslationUnitDecl());
94 class TestAction : public ASTFrontendAction {
96 TestAction(TestVisitor *Visitor) : Visitor(Visitor) {}
98 std::unique_ptr<clang::ASTConsumer>
99 CreateASTConsumer(CompilerInstance &, llvm::StringRef dummy) override {
100 /// TestConsumer will be deleted by the framework calling us.
101 return llvm::make_unique<FindConsumer>(Visitor);
105 TestVisitor *Visitor;
111 /// \brief A RecursiveASTVisitor to check that certain matches are (or are
112 /// not) observed during visitation.
114 /// This is a RecursiveASTVisitor for testing the RecursiveASTVisitor itself,
115 /// and allows simple creation of test visitors running matches on only a small
116 /// subset of the Visit* methods.
117 template <typename T, template <typename> class Visitor = TestVisitor>
118 class ExpectedLocationVisitor : public Visitor<T> {
120 /// \brief Expect 'Match' *not* to occur at the given 'Line' and 'Column'.
122 /// Any number of matches can be disallowed.
123 void DisallowMatch(Twine Match, unsigned Line, unsigned Column) {
124 DisallowedMatches.push_back(MatchCandidate(Match, Line, Column));
127 /// \brief Expect 'Match' to occur at the given 'Line' and 'Column'.
129 /// Any number of expected matches can be set by calling this repeatedly.
130 /// Each is expected to be matched exactly once.
131 void ExpectMatch(Twine Match, unsigned Line, unsigned Column) {
132 ExpectedMatches.push_back(ExpectedMatch(Match, Line, Column));
135 /// \brief Checks that all expected matches have been found.
136 ~ExpectedLocationVisitor() override {
137 for (typename std::vector<ExpectedMatch>::const_iterator
138 It = ExpectedMatches.begin(), End = ExpectedMatches.end();
145 /// \brief Checks an actual match against expected and disallowed matches.
147 /// Implementations are required to call this with appropriate values
148 /// for 'Name' during visitation.
149 void Match(StringRef Name, SourceLocation Location) {
150 const FullSourceLoc FullLocation = this->Context->getFullLoc(Location);
152 for (typename std::vector<MatchCandidate>::const_iterator
153 It = DisallowedMatches.begin(), End = DisallowedMatches.end();
155 EXPECT_FALSE(It->Matches(Name, FullLocation))
156 << "Matched disallowed " << *It;
159 for (typename std::vector<ExpectedMatch>::iterator
160 It = ExpectedMatches.begin(), End = ExpectedMatches.end();
162 It->UpdateFor(Name, FullLocation, this->Context->getSourceManager());
167 struct MatchCandidate {
168 std::string ExpectedName;
170 unsigned ColumnNumber;
172 MatchCandidate(Twine Name, unsigned LineNumber, unsigned ColumnNumber)
173 : ExpectedName(Name.str()), LineNumber(LineNumber),
174 ColumnNumber(ColumnNumber) {
177 bool Matches(StringRef Name, FullSourceLoc const &Location) const {
178 return MatchesName(Name) && MatchesLocation(Location);
181 bool PartiallyMatches(StringRef Name, FullSourceLoc const &Location) const {
182 return MatchesName(Name) || MatchesLocation(Location);
185 bool MatchesName(StringRef Name) const {
186 return Name == ExpectedName;
189 bool MatchesLocation(FullSourceLoc const &Location) const {
190 return Location.isValid() &&
191 Location.getSpellingLineNumber() == LineNumber &&
192 Location.getSpellingColumnNumber() == ColumnNumber;
195 friend std::ostream &operator<<(std::ostream &Stream,
196 MatchCandidate const &Match) {
197 return Stream << Match.ExpectedName
198 << " at " << Match.LineNumber << ":" << Match.ColumnNumber;
202 struct ExpectedMatch {
203 ExpectedMatch(Twine Name, unsigned LineNumber, unsigned ColumnNumber)
204 : Candidate(Name, LineNumber, ColumnNumber), Found(false) {}
206 void UpdateFor(StringRef Name, FullSourceLoc Location, SourceManager &SM) {
207 if (Candidate.Matches(Name, Location)) {
210 } else if (!Found && Candidate.PartiallyMatches(Name, Location)) {
211 llvm::raw_string_ostream Stream(PartialMatches);
212 Stream << ", partial match: \"" << Name << "\" at ";
213 Location.print(Stream, SM);
217 void ExpectFound() const {
219 << "Expected \"" << Candidate.ExpectedName
220 << "\" at " << Candidate.LineNumber
221 << ":" << Candidate.ColumnNumber << PartialMatches;
224 MatchCandidate Candidate;
225 std::string PartialMatches;
229 std::vector<MatchCandidate> DisallowedMatches;
230 std::vector<ExpectedMatch> ExpectedMatches;