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