1 //===--- 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 //===----------------------------------------------------------------------===// 9 /// 10 /// \file 11 /// \brief Defines utility templates for RecursiveASTVisitor related tests. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_CLANG_TEST_VISITOR_H 16 #define LLVM_CLANG_TEST_VISITOR_H 17 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" 25 #include <vector> 26 27 namespace clang { 28 29 /// \brief Base class for simple RecursiveASTVisitor based tests. 30 /// 31 /// This is a drop-in replacement for RecursiveASTVisitor itself, with the 32 /// additional capability of running it over a snippet of code. 33 /// 34 /// Visits template instantiations and implicit code by default. 35 template <typename T> 36 class TestVisitor : public RecursiveASTVisitor<T> { 37 public: 38 TestVisitor() { } 39 40 virtual ~TestVisitor() { } 41 42 enum Language { Lang_C, Lang_CXX98, Lang_CXX11, Lang_CXX=Lang_CXX98 }; 43 44 /// \brief Runs the current AST visitor over the given code. 45 bool runOver(StringRef Code, Language L = Lang_CXX) { 46 std::vector<std::string> Args; 47 switch (L) { 48 case Lang_C: Args.push_back("-std=c99"); break; 49 case Lang_CXX98: Args.push_back("-std=c++98"); break; 50 case Lang_CXX11: Args.push_back("-std=c++11"); break; 51 } 52 return tooling::runToolOnCodeWithArgs(CreateTestAction(), Code, Args); 53 } 54 55 bool shouldVisitTemplateInstantiations() const { 56 return true; 57 } 58 59 bool shouldVisitImplicitCode() const { 60 return true; 61 } 62 63 protected: 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) { 73 Visitor->Context = &Context; 74 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( 86 CompilerInstance&, llvm::StringRef dummy) { 87 /// 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 98 /// \brief A RecursiveASTVisitor to check that certain matches are (or are 99 /// not) observed during visitation. 100 /// 101 /// This is a RecursiveASTVisitor for testing the RecursiveASTVisitor itself, 102 /// and allows simple creation of test visitors running matches on only a small 103 /// subset of the Visit* methods. 104 template <typename T, template <typename> class Visitor = TestVisitor> 105 class ExpectedLocationVisitor : public Visitor<T> { 106 public: 107 /// \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)); 112 } 113 114 /// \brief Expect 'Match' to occur at the given 'Line' and 'Column'. 115 /// 116 /// Any number of expected matches can be set by calling this repeatedly. 117 /// Each is expected to be matched exactly once. 118 void ExpectMatch(Twine Match, unsigned Line, unsigned Column) { 119 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 } 129 } 130 131 protected: 132 /// \brief Checks an actual match against expected and disallowed matches. 133 /// 134 /// Implementations are required to call this with appropriate values 135 /// for 'Name' during visitation. 136 void Match(StringRef Name, SourceLocation Location) { 137 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()); 150 } 151 } 152 153 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; 218 }; 219 } 220 221 #endif /* LLVM_CLANG_TEST_VISITOR_H */ 222