Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
James Dennett | 8268fe7 | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
| 11 | /// \brief Defines utility templates for RecursiveASTVisitor related tests. |
| 12 | /// |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_CLANG_TEST_VISITOR_H |
| 16 | #define LLVM_CLANG_TEST_VISITOR_H |
| 17 | |
| 18 | #include "clang/AST/ASTConsumer.h" |
Chandler Carruth | 1050e8b | 2012-12-04 09:45:34 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 20 | #include "clang/AST/RecursiveASTVisitor.h" |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/CompilerInstance.h" |
Chandler Carruth | 1050e8b | 2012-12-04 09:45:34 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/FrontendAction.h" |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 23 | #include "clang/Tooling/Tooling.h" |
| 24 | #include "gtest/gtest.h" |
Chandler Carruth | 1050e8b | 2012-12-04 09:45:34 +0000 | [diff] [blame] | 25 | #include <vector> |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 26 | |
| 27 | namespace clang { |
| 28 | |
Matt Beaumont-Gay | 8f35d23 | 2012-06-25 18:27:11 +0000 | [diff] [blame] | 29 | /// \brief Base class for simple RecursiveASTVisitor based tests. |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 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 | /// |
James Dennett | 8268fe7 | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 34 | /// Visits template instantiations (but not implicit code) by default. |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 35 | template <typename T> |
| 36 | class TestVisitor : public RecursiveASTVisitor<T> { |
| 37 | public: |
| 38 | TestVisitor() { } |
| 39 | |
Matt Beaumont-Gay | 8f35d23 | 2012-06-25 18:27:11 +0000 | [diff] [blame] | 40 | virtual ~TestVisitor() { } |
| 41 | |
James Dennett | 17aa33f | 2013-06-30 03:05:49 +0000 | [diff] [blame] | 42 | enum Language { Lang_C, Lang_CXX98, Lang_CXX11, Lang_CXX=Lang_CXX98 }; |
Richard Smith | 82b4550 | 2012-08-17 21:23:17 +0000 | [diff] [blame] | 43 | |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 44 | /// \brief Runs the current AST visitor over the given code. |
Richard Smith | 82b4550 | 2012-08-17 21:23:17 +0000 | [diff] [blame] | 45 | bool runOver(StringRef Code, Language L = Lang_CXX) { |
Nico Weber | 5666988 | 2012-08-30 02:02:19 +0000 | [diff] [blame] | 46 | std::vector<std::string> Args; |
| 47 | switch (L) { |
| 48 | case Lang_C: Args.push_back("-std=c99"); break; |
James Dennett | 17aa33f | 2013-06-30 03:05:49 +0000 | [diff] [blame] | 49 | case Lang_CXX98: Args.push_back("-std=c++98"); break; |
| 50 | case Lang_CXX11: Args.push_back("-std=c++11"); break; |
Nico Weber | 5666988 | 2012-08-30 02:02:19 +0000 | [diff] [blame] | 51 | } |
| 52 | return tooling::runToolOnCodeWithArgs(CreateTestAction(), Code, Args); |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | bool shouldVisitTemplateInstantiations() const { |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | protected: |
| 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 Klimek | e6df0ce | 2012-07-05 18:13:01 +0000 | [diff] [blame] | 69 | Visitor->Context = &Context; |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 70 | 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 Klimek | e6df0ce | 2012-07-05 18:13:01 +0000 | [diff] [blame] | 82 | CompilerInstance&, llvm::StringRef dummy) { |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 83 | /// 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 Dennett | 8268fe7 | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 94 | /// \brief A RecursiveASTVisitor to check that certain matches are (or are |
| 95 | /// not) observed during visitation. |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 96 | /// |
James Dennett | 8268fe7 | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 97 | /// This is a RecursiveASTVisitor for testing the RecursiveASTVisitor itself, |
| 98 | /// and allows simple creation of test visitors running matches on only a small |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 99 | /// subset of the Visit* methods. |
| 100 | template <typename T, template <typename> class Visitor = TestVisitor> |
| 101 | class ExpectedLocationVisitor : public Visitor<T> { |
| 102 | public: |
James Dennett | 8268fe7 | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 103 | /// \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 Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /// \brief Expect 'Match' to occur at the given 'Line' and 'Column'. |
James Dennett | 8268fe7 | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 111 | /// |
| 112 | /// Any number of expected matches can be set by calling this repeatedly. |
| 113 | /// Each is expected to be matched exactly once. |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 114 | void ExpectMatch(Twine Match, unsigned Line, unsigned Column) { |
James Dennett | 8268fe7 | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 115 | 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 Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | protected: |
James Dennett | 8268fe7 | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 128 | /// \brief Checks an actual match against expected and disallowed matches. |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 129 | /// |
| 130 | /// Implementations are required to call this with appropriate values |
| 131 | /// for 'Name' during visitation. |
| 132 | void Match(StringRef Name, SourceLocation Location) { |
James Dennett | 8268fe7 | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 133 | 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 Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
James Dennett | 8268fe7 | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 149 | 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 Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 214 | }; |
| 215 | } |
| 216 | |
| 217 | #endif /* LLVM_CLANG_TEST_VISITOR_H */ |