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 | |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 15 | #ifndef LLVM_CLANG_UNITTESTS_TOOLING_TESTVISITOR_H |
| 16 | #define LLVM_CLANG_UNITTESTS_TOOLING_TESTVISITOR_H |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 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: |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 38 | TestVisitor() { } |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 39 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 40 | virtual ~TestVisitor() { } |
Matt Beaumont-Gay | a0e2c04 | 2012-06-25 18:27:11 +0000 | [diff] [blame] | 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, |
Martin Bohme | d5f94a6 | 2016-08-17 14:59:53 +0000 | [diff] [blame] | 46 | Lang_CXX14, |
Alp Toker | e3ad531 | 2014-06-26 01:42:24 +0000 | [diff] [blame] | 47 | Lang_OBJC, |
| 48 | Lang_OBJCXX11, |
| 49 | Lang_CXX = Lang_CXX98 |
| 50 | }; |
Richard Smith | 87deab3 | 2012-08-17 21:23:17 +0000 | [diff] [blame] | 51 | |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 52 | /// \brief Runs the current AST visitor over the given code. |
Richard Smith | 87deab3 | 2012-08-17 21:23:17 +0000 | [diff] [blame] | 53 | bool runOver(StringRef Code, Language L = Lang_CXX) { |
Nico Weber | 077a53e | 2012-08-30 02:02:19 +0000 | [diff] [blame] | 54 | std::vector<std::string> Args; |
| 55 | switch (L) { |
Stephan Bergmann | 83e6a82 | 2017-06-27 07:59:56 +0000 | [diff] [blame^] | 56 | case Lang_C: |
| 57 | Args.push_back("-x"); |
| 58 | Args.push_back("c"); |
| 59 | break; |
James Dennett | eb576c0 | 2013-06-30 03:05:49 +0000 | [diff] [blame] | 60 | case Lang_CXX98: Args.push_back("-std=c++98"); break; |
| 61 | case Lang_CXX11: Args.push_back("-std=c++11"); break; |
Martin Bohme | d5f94a6 | 2016-08-17 14:59:53 +0000 | [diff] [blame] | 62 | case Lang_CXX14: Args.push_back("-std=c++14"); break; |
Alp Toker | 62438da | 2014-06-06 15:05:09 +0000 | [diff] [blame] | 63 | case Lang_OBJC: Args.push_back("-ObjC"); break; |
Alp Toker | e3ad531 | 2014-06-26 01:42:24 +0000 | [diff] [blame] | 64 | case Lang_OBJCXX11: |
| 65 | Args.push_back("-ObjC++"); |
| 66 | Args.push_back("-std=c++11"); |
Alp Toker | 0843bff | 2014-06-26 02:07:06 +0000 | [diff] [blame] | 67 | Args.push_back("-fblocks"); |
Alp Toker | e3ad531 | 2014-06-26 01:42:24 +0000 | [diff] [blame] | 68 | break; |
Nico Weber | 077a53e | 2012-08-30 02:02:19 +0000 | [diff] [blame] | 69 | } |
| 70 | return tooling::runToolOnCodeWithArgs(CreateTestAction(), Code, Args); |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | bool shouldVisitTemplateInstantiations() const { |
| 74 | return true; |
| 75 | } |
| 76 | |
Michael Han | c90d12d | 2013-09-11 15:53:29 +0000 | [diff] [blame] | 77 | bool shouldVisitImplicitCode() const { |
| 78 | return true; |
| 79 | } |
| 80 | |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 81 | protected: |
| 82 | virtual ASTFrontendAction* CreateTestAction() { |
| 83 | return new TestAction(this); |
| 84 | } |
| 85 | |
| 86 | class FindConsumer : public ASTConsumer { |
| 87 | public: |
| 88 | FindConsumer(TestVisitor *Visitor) : Visitor(Visitor) {} |
| 89 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 90 | void HandleTranslationUnit(clang::ASTContext &Context) override { |
Manuel Klimek | 5da9dcb | 2012-07-05 18:13:01 +0000 | [diff] [blame] | 91 | Visitor->Context = &Context; |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 92 | Visitor->TraverseDecl(Context.getTranslationUnitDecl()); |
| 93 | } |
| 94 | |
| 95 | private: |
| 96 | TestVisitor *Visitor; |
| 97 | }; |
| 98 | |
| 99 | class TestAction : public ASTFrontendAction { |
| 100 | public: |
| 101 | TestAction(TestVisitor *Visitor) : Visitor(Visitor) {} |
| 102 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 103 | std::unique_ptr<clang::ASTConsumer> |
| 104 | CreateASTConsumer(CompilerInstance &, llvm::StringRef dummy) override { |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 105 | /// TestConsumer will be deleted by the framework calling us. |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 106 | return llvm::make_unique<FindConsumer>(Visitor); |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | protected: |
| 110 | TestVisitor *Visitor; |
| 111 | }; |
| 112 | |
| 113 | ASTContext *Context; |
| 114 | }; |
| 115 | |
James Dennett | 75c100b | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 116 | /// \brief A RecursiveASTVisitor to check that certain matches are (or are |
| 117 | /// not) observed during visitation. |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 118 | /// |
James Dennett | 75c100b | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 119 | /// This is a RecursiveASTVisitor for testing the RecursiveASTVisitor itself, |
| 120 | /// 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] | 121 | /// subset of the Visit* methods. |
| 122 | template <typename T, template <typename> class Visitor = TestVisitor> |
| 123 | class ExpectedLocationVisitor : public Visitor<T> { |
| 124 | public: |
James Dennett | 75c100b | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 125 | /// \brief Expect 'Match' *not* to occur at the given 'Line' and 'Column'. |
| 126 | /// |
| 127 | /// Any number of matches can be disallowed. |
| 128 | void DisallowMatch(Twine Match, unsigned Line, unsigned Column) { |
| 129 | DisallowedMatches.push_back(MatchCandidate(Match, Line, Column)); |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /// \brief Expect 'Match' to occur at the given 'Line' and 'Column'. |
James Dennett | 75c100b | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 133 | /// |
| 134 | /// Any number of expected matches can be set by calling this repeatedly. |
Martin Bohme | d5f94a6 | 2016-08-17 14:59:53 +0000 | [diff] [blame] | 135 | /// Each is expected to be matched 'Times' number of times. (This is useful in |
| 136 | /// cases in which different AST nodes can match at the same source code |
| 137 | /// location.) |
| 138 | void ExpectMatch(Twine Match, unsigned Line, unsigned Column, |
| 139 | unsigned Times = 1) { |
| 140 | ExpectedMatches.push_back(ExpectedMatch(Match, Line, Column, Times)); |
James Dennett | 75c100b | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /// \brief Checks that all expected matches have been found. |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 144 | ~ExpectedLocationVisitor() override { |
James Dennett | 75c100b | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 145 | for (typename std::vector<ExpectedMatch>::const_iterator |
| 146 | It = ExpectedMatches.begin(), End = ExpectedMatches.end(); |
| 147 | It != End; ++It) { |
| 148 | It->ExpectFound(); |
| 149 | } |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | protected: |
James Dennett | 75c100b | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 153 | /// \brief Checks an actual match against expected and disallowed matches. |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 154 | /// |
| 155 | /// Implementations are required to call this with appropriate values |
| 156 | /// for 'Name' during visitation. |
| 157 | void Match(StringRef Name, SourceLocation Location) { |
James Dennett | 75c100b | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 158 | const FullSourceLoc FullLocation = this->Context->getFullLoc(Location); |
| 159 | |
| 160 | for (typename std::vector<MatchCandidate>::const_iterator |
| 161 | It = DisallowedMatches.begin(), End = DisallowedMatches.end(); |
| 162 | It != End; ++It) { |
| 163 | EXPECT_FALSE(It->Matches(Name, FullLocation)) |
| 164 | << "Matched disallowed " << *It; |
| 165 | } |
| 166 | |
| 167 | for (typename std::vector<ExpectedMatch>::iterator |
| 168 | It = ExpectedMatches.begin(), End = ExpectedMatches.end(); |
| 169 | It != End; ++It) { |
| 170 | It->UpdateFor(Name, FullLocation, this->Context->getSourceManager()); |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
James Dennett | 75c100b | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 174 | private: |
| 175 | struct MatchCandidate { |
| 176 | std::string ExpectedName; |
| 177 | unsigned LineNumber; |
| 178 | unsigned ColumnNumber; |
| 179 | |
| 180 | MatchCandidate(Twine Name, unsigned LineNumber, unsigned ColumnNumber) |
| 181 | : ExpectedName(Name.str()), LineNumber(LineNumber), |
| 182 | ColumnNumber(ColumnNumber) { |
| 183 | } |
| 184 | |
| 185 | bool Matches(StringRef Name, FullSourceLoc const &Location) const { |
| 186 | return MatchesName(Name) && MatchesLocation(Location); |
| 187 | } |
| 188 | |
| 189 | bool PartiallyMatches(StringRef Name, FullSourceLoc const &Location) const { |
| 190 | return MatchesName(Name) || MatchesLocation(Location); |
| 191 | } |
| 192 | |
| 193 | bool MatchesName(StringRef Name) const { |
| 194 | return Name == ExpectedName; |
| 195 | } |
| 196 | |
| 197 | bool MatchesLocation(FullSourceLoc const &Location) const { |
| 198 | return Location.isValid() && |
| 199 | Location.getSpellingLineNumber() == LineNumber && |
| 200 | Location.getSpellingColumnNumber() == ColumnNumber; |
| 201 | } |
| 202 | |
| 203 | friend std::ostream &operator<<(std::ostream &Stream, |
| 204 | MatchCandidate const &Match) { |
| 205 | return Stream << Match.ExpectedName |
| 206 | << " at " << Match.LineNumber << ":" << Match.ColumnNumber; |
| 207 | } |
| 208 | }; |
| 209 | |
| 210 | struct ExpectedMatch { |
Martin Bohme | d5f94a6 | 2016-08-17 14:59:53 +0000 | [diff] [blame] | 211 | ExpectedMatch(Twine Name, unsigned LineNumber, unsigned ColumnNumber, |
| 212 | unsigned Times) |
| 213 | : Candidate(Name, LineNumber, ColumnNumber), TimesExpected(Times), |
| 214 | TimesSeen(0) {} |
James Dennett | 75c100b | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 215 | |
| 216 | void UpdateFor(StringRef Name, FullSourceLoc Location, SourceManager &SM) { |
| 217 | if (Candidate.Matches(Name, Location)) { |
Martin Bohme | d5f94a6 | 2016-08-17 14:59:53 +0000 | [diff] [blame] | 218 | EXPECT_LT(TimesSeen, TimesExpected); |
| 219 | ++TimesSeen; |
| 220 | } else if (TimesSeen < TimesExpected && |
| 221 | Candidate.PartiallyMatches(Name, Location)) { |
James Dennett | 75c100b | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 222 | llvm::raw_string_ostream Stream(PartialMatches); |
| 223 | Stream << ", partial match: \"" << Name << "\" at "; |
| 224 | Location.print(Stream, SM); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | void ExpectFound() const { |
Martin Bohme | d5f94a6 | 2016-08-17 14:59:53 +0000 | [diff] [blame] | 229 | EXPECT_EQ(TimesExpected, TimesSeen) |
James Dennett | 75c100b | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 230 | << "Expected \"" << Candidate.ExpectedName |
| 231 | << "\" at " << Candidate.LineNumber |
| 232 | << ":" << Candidate.ColumnNumber << PartialMatches; |
| 233 | } |
| 234 | |
| 235 | MatchCandidate Candidate; |
| 236 | std::string PartialMatches; |
Martin Bohme | d5f94a6 | 2016-08-17 14:59:53 +0000 | [diff] [blame] | 237 | unsigned TimesExpected; |
| 238 | unsigned TimesSeen; |
James Dennett | 75c100b | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 239 | }; |
| 240 | |
| 241 | std::vector<MatchCandidate> DisallowedMatches; |
| 242 | std::vector<ExpectedMatch> ExpectedMatches; |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 243 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 244 | } |
Richard Smith | 87d8fb9 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 245 | |
Manuel Klimek | 20c1c89 | 2014-10-09 15:02:06 +0000 | [diff] [blame] | 246 | #endif |