Richard Smith | 87d8fb9 | 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 | 75c100b | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
| 11 | /// \brief Defines utility templates for RecursiveASTVisitor related tests. |
| 12 | /// |
Richard Smith | 87d8fb9 | 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 | 320d966 | 2012-12-04 09:45:34 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 20 | #include "clang/AST/RecursiveASTVisitor.h" |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/CompilerInstance.h" |
Chandler Carruth | 320d966 | 2012-12-04 09:45:34 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/FrontendAction.h" |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 23 | #include "clang/Tooling/Tooling.h" |
| 24 | #include "gtest/gtest.h" |
Chandler Carruth | 320d966 | 2012-12-04 09:45:34 +0000 | [diff] [blame] | 25 | #include <vector> |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 26 | |
| 27 | namespace clang { |
| 28 | |
Matt Beaumont-Gay | a0e2c04 | 2012-06-25 18:27:11 +0000 | [diff] [blame] | 29 | /// \brief Base class for simple RecursiveASTVisitor based tests. |
Richard Smith | 87d8fb9 | 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 | /// |
Michael Han | c90d12d | 2013-09-11 15:53:29 +0000 | [diff] [blame] | 34 | /// Visits template instantiations and implicit code by default. |
Richard Smith | 87d8fb9 | 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 | a0e2c04 | 2012-06-25 18:27:11 +0000 | [diff] [blame] | 40 | virtual ~TestVisitor() { } |
| 41 | |
Alp Toker | e3ad531 | 2014-06-26 01:42:24 +0000 | [diff] [blame] | 42 | enum Language { |
| 43 | Lang_C, |
| 44 | Lang_CXX98, |
| 45 | Lang_CXX11, |
| 46 | Lang_OBJC, |
| 47 | Lang_OBJCXX11, |
| 48 | Lang_CXX = Lang_CXX98 |
| 49 | }; |
Richard Smith | 87deab3 | 2012-08-17 21:23:17 +0000 | [diff] [blame] | 50 | |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 51 | /// \brief Runs the current AST visitor over the given code. |
Richard Smith | 87deab3 | 2012-08-17 21:23:17 +0000 | [diff] [blame] | 52 | bool runOver(StringRef Code, Language L = Lang_CXX) { |
Nico Weber | 077a53e | 2012-08-30 02:02:19 +0000 | [diff] [blame] | 53 | std::vector<std::string> Args; |
| 54 | switch (L) { |
| 55 | case Lang_C: Args.push_back("-std=c99"); break; |
James Dennett | eb576c0 | 2013-06-30 03:05:49 +0000 | [diff] [blame] | 56 | case Lang_CXX98: Args.push_back("-std=c++98"); break; |
| 57 | case Lang_CXX11: Args.push_back("-std=c++11"); break; |
Alp Toker | 62438da | 2014-06-06 15:05:09 +0000 | [diff] [blame] | 58 | case Lang_OBJC: Args.push_back("-ObjC"); break; |
Alp Toker | e3ad531 | 2014-06-26 01:42:24 +0000 | [diff] [blame] | 59 | case Lang_OBJCXX11: |
| 60 | Args.push_back("-ObjC++"); |
| 61 | Args.push_back("-std=c++11"); |
Alp Toker | 0843bff | 2014-06-26 02:07:06 +0000 | [diff] [blame] | 62 | Args.push_back("-fblocks"); |
Alp Toker | e3ad531 | 2014-06-26 01:42:24 +0000 | [diff] [blame] | 63 | break; |
Nico Weber | 077a53e | 2012-08-30 02:02:19 +0000 | [diff] [blame] | 64 | } |
| 65 | return tooling::runToolOnCodeWithArgs(CreateTestAction(), Code, Args); |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | bool shouldVisitTemplateInstantiations() const { |
| 69 | return true; |
| 70 | } |
| 71 | |
Michael Han | c90d12d | 2013-09-11 15:53:29 +0000 | [diff] [blame] | 72 | bool shouldVisitImplicitCode() const { |
| 73 | return true; |
| 74 | } |
| 75 | |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 76 | protected: |
| 77 | virtual ASTFrontendAction* CreateTestAction() { |
| 78 | return new TestAction(this); |
| 79 | } |
| 80 | |
| 81 | class FindConsumer : public ASTConsumer { |
| 82 | public: |
| 83 | FindConsumer(TestVisitor *Visitor) : Visitor(Visitor) {} |
| 84 | |
| 85 | virtual void HandleTranslationUnit(clang::ASTContext &Context) { |
Manuel Klimek | 5da9dcb | 2012-07-05 18:13:01 +0000 | [diff] [blame] | 86 | Visitor->Context = &Context; |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 87 | Visitor->TraverseDecl(Context.getTranslationUnitDecl()); |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | TestVisitor *Visitor; |
| 92 | }; |
| 93 | |
| 94 | class TestAction : public ASTFrontendAction { |
| 95 | public: |
| 96 | TestAction(TestVisitor *Visitor) : Visitor(Visitor) {} |
| 97 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 98 | virtual std::unique_ptr<clang::ASTConsumer> |
| 99 | CreateASTConsumer(CompilerInstance &, llvm::StringRef dummy) { |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 100 | /// TestConsumer will be deleted by the framework calling us. |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 101 | return llvm::make_unique<FindConsumer>(Visitor); |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | protected: |
| 105 | TestVisitor *Visitor; |
| 106 | }; |
| 107 | |
| 108 | ASTContext *Context; |
| 109 | }; |
| 110 | |
James Dennett | 75c100b | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 111 | /// \brief A RecursiveASTVisitor to check that certain matches are (or are |
| 112 | /// not) observed during visitation. |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 113 | /// |
James Dennett | 75c100b | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 114 | /// This is a RecursiveASTVisitor for testing the RecursiveASTVisitor itself, |
| 115 | /// and allows simple creation of test visitors running matches on only a small |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 116 | /// subset of the Visit* methods. |
| 117 | template <typename T, template <typename> class Visitor = TestVisitor> |
| 118 | class ExpectedLocationVisitor : public Visitor<T> { |
| 119 | public: |
James Dennett | 75c100b | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 120 | /// \brief Expect 'Match' *not* to occur at the given 'Line' and 'Column'. |
| 121 | /// |
| 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)); |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /// \brief Expect 'Match' to occur at the given 'Line' and 'Column'. |
James Dennett | 75c100b | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 128 | /// |
| 129 | /// Any number of expected matches can be set by calling this repeatedly. |
| 130 | /// Each is expected to be matched exactly once. |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 131 | void ExpectMatch(Twine Match, unsigned Line, unsigned Column) { |
James Dennett | 75c100b | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 132 | ExpectedMatches.push_back(ExpectedMatch(Match, Line, Column)); |
| 133 | } |
| 134 | |
| 135 | /// \brief Checks that all expected matches have been found. |
| 136 | virtual ~ExpectedLocationVisitor() { |
| 137 | for (typename std::vector<ExpectedMatch>::const_iterator |
| 138 | It = ExpectedMatches.begin(), End = ExpectedMatches.end(); |
| 139 | It != End; ++It) { |
| 140 | It->ExpectFound(); |
| 141 | } |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | protected: |
James Dennett | 75c100b | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 145 | /// \brief Checks an actual match against expected and disallowed matches. |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 146 | /// |
| 147 | /// Implementations are required to call this with appropriate values |
| 148 | /// for 'Name' during visitation. |
| 149 | void Match(StringRef Name, SourceLocation Location) { |
James Dennett | 75c100b | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 150 | const FullSourceLoc FullLocation = this->Context->getFullLoc(Location); |
| 151 | |
| 152 | for (typename std::vector<MatchCandidate>::const_iterator |
| 153 | It = DisallowedMatches.begin(), End = DisallowedMatches.end(); |
| 154 | It != End; ++It) { |
| 155 | EXPECT_FALSE(It->Matches(Name, FullLocation)) |
| 156 | << "Matched disallowed " << *It; |
| 157 | } |
| 158 | |
| 159 | for (typename std::vector<ExpectedMatch>::iterator |
| 160 | It = ExpectedMatches.begin(), End = ExpectedMatches.end(); |
| 161 | It != End; ++It) { |
| 162 | It->UpdateFor(Name, FullLocation, this->Context->getSourceManager()); |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
James Dennett | 75c100b | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 166 | private: |
| 167 | struct MatchCandidate { |
| 168 | std::string ExpectedName; |
| 169 | unsigned LineNumber; |
| 170 | unsigned ColumnNumber; |
| 171 | |
| 172 | MatchCandidate(Twine Name, unsigned LineNumber, unsigned ColumnNumber) |
| 173 | : ExpectedName(Name.str()), LineNumber(LineNumber), |
| 174 | ColumnNumber(ColumnNumber) { |
| 175 | } |
| 176 | |
| 177 | bool Matches(StringRef Name, FullSourceLoc const &Location) const { |
| 178 | return MatchesName(Name) && MatchesLocation(Location); |
| 179 | } |
| 180 | |
| 181 | bool PartiallyMatches(StringRef Name, FullSourceLoc const &Location) const { |
| 182 | return MatchesName(Name) || MatchesLocation(Location); |
| 183 | } |
| 184 | |
| 185 | bool MatchesName(StringRef Name) const { |
| 186 | return Name == ExpectedName; |
| 187 | } |
| 188 | |
| 189 | bool MatchesLocation(FullSourceLoc const &Location) const { |
| 190 | return Location.isValid() && |
| 191 | Location.getSpellingLineNumber() == LineNumber && |
| 192 | Location.getSpellingColumnNumber() == ColumnNumber; |
| 193 | } |
| 194 | |
| 195 | friend std::ostream &operator<<(std::ostream &Stream, |
| 196 | MatchCandidate const &Match) { |
| 197 | return Stream << Match.ExpectedName |
| 198 | << " at " << Match.LineNumber << ":" << Match.ColumnNumber; |
| 199 | } |
| 200 | }; |
| 201 | |
| 202 | struct ExpectedMatch { |
| 203 | ExpectedMatch(Twine Name, unsigned LineNumber, unsigned ColumnNumber) |
| 204 | : Candidate(Name, LineNumber, ColumnNumber), Found(false) {} |
| 205 | |
| 206 | void UpdateFor(StringRef Name, FullSourceLoc Location, SourceManager &SM) { |
| 207 | if (Candidate.Matches(Name, Location)) { |
| 208 | EXPECT_TRUE(!Found); |
| 209 | Found = true; |
| 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); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | void ExpectFound() const { |
| 218 | EXPECT_TRUE(Found) |
| 219 | << "Expected \"" << Candidate.ExpectedName |
| 220 | << "\" at " << Candidate.LineNumber |
| 221 | << ":" << Candidate.ColumnNumber << PartialMatches; |
| 222 | } |
| 223 | |
| 224 | MatchCandidate Candidate; |
| 225 | std::string PartialMatches; |
| 226 | bool Found; |
| 227 | }; |
| 228 | |
| 229 | std::vector<MatchCandidate> DisallowedMatches; |
| 230 | std::vector<ExpectedMatch> ExpectedMatches; |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 231 | }; |
| 232 | } |
| 233 | |
| 234 | #endif /* LLVM_CLANG_TEST_VISITOR_H */ |