Manuel Klimek | aa58b97 | 2013-01-31 13:10:40 +0000 | [diff] [blame] | 1 | //===- unittest/AST/MatchVerifier.h - AST unit test support ---------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Manuel Klimek | aa58b97 | 2013-01-31 13:10:40 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Provides MatchVerifier, a base class to implement gtest matchers that |
| 10 | // verify things that can be matched on the AST. |
| 11 | // |
| 12 | // Also implements matchers based on MatchVerifier: |
| 13 | // LocationVerifier and RangeVerifier to verify whether a matched node has |
| 14 | // the expected source location or source range. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Benjamin Kramer | 12152ab | 2014-08-08 13:24:19 +0000 | [diff] [blame] | 18 | #ifndef LLVM_CLANG_UNITTESTS_AST_MATCHVERIFIER_H |
| 19 | #define LLVM_CLANG_UNITTESTS_AST_MATCHVERIFIER_H |
| 20 | |
Manuel Klimek | aa58b97 | 2013-01-31 13:10:40 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
| 22 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 23 | #include "clang/ASTMatchers/ASTMatchers.h" |
| 24 | #include "clang/Tooling/Tooling.h" |
Gabor Marton | 1f66753 | 2018-05-24 08:41:07 +0000 | [diff] [blame] | 25 | #include "Language.h" |
Manuel Klimek | aa58b97 | 2013-01-31 13:10:40 +0000 | [diff] [blame] | 26 | #include "gtest/gtest.h" |
| 27 | |
| 28 | namespace clang { |
| 29 | namespace ast_matchers { |
| 30 | |
Manuel Klimek | aa58b97 | 2013-01-31 13:10:40 +0000 | [diff] [blame] | 31 | /// \brief Base class for verifying some property of nodes found by a matcher. |
| 32 | template <typename NodeType> |
| 33 | class MatchVerifier : public MatchFinder::MatchCallback { |
| 34 | public: |
| 35 | template <typename MatcherType> |
| 36 | testing::AssertionResult match(const std::string &Code, |
| 37 | const MatcherType &AMatcher) { |
Enea Zaffanella | 4c40949 | 2013-07-08 14:50:30 +0000 | [diff] [blame] | 38 | std::vector<std::string> Args; |
| 39 | return match(Code, AMatcher, Args, Lang_CXX); |
Manuel Klimek | aa58b97 | 2013-01-31 13:10:40 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | template <typename MatcherType> |
| 43 | testing::AssertionResult match(const std::string &Code, |
Enea Zaffanella | 4c40949 | 2013-07-08 14:50:30 +0000 | [diff] [blame] | 44 | const MatcherType &AMatcher, |
| 45 | Language L) { |
| 46 | std::vector<std::string> Args; |
| 47 | return match(Code, AMatcher, Args, L); |
| 48 | } |
| 49 | |
| 50 | template <typename MatcherType> |
| 51 | testing::AssertionResult match(const std::string &Code, |
| 52 | const MatcherType &AMatcher, |
| 53 | std::vector<std::string>& Args, |
| 54 | Language L); |
Manuel Klimek | aa58b97 | 2013-01-31 13:10:40 +0000 | [diff] [blame] | 55 | |
Aaron Ballman | a35b8fc | 2016-03-09 17:11:51 +0000 | [diff] [blame] | 56 | template <typename MatcherType> |
| 57 | testing::AssertionResult match(const Decl *D, const MatcherType &AMatcher); |
| 58 | |
Manuel Klimek | aa58b97 | 2013-01-31 13:10:40 +0000 | [diff] [blame] | 59 | protected: |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 60 | void run(const MatchFinder::MatchResult &Result) override; |
Manuel Klimek | aa58b97 | 2013-01-31 13:10:40 +0000 | [diff] [blame] | 61 | virtual void verify(const MatchFinder::MatchResult &Result, |
Manuel Klimek | d4be408 | 2013-02-28 13:21:39 +0000 | [diff] [blame] | 62 | const NodeType &Node) {} |
Manuel Klimek | aa58b97 | 2013-01-31 13:10:40 +0000 | [diff] [blame] | 63 | |
| 64 | void setFailure(const Twine &Result) { |
| 65 | Verified = false; |
| 66 | VerifyResult = Result.str(); |
| 67 | } |
| 68 | |
| 69 | void setSuccess() { |
| 70 | Verified = true; |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | bool Verified; |
| 75 | std::string VerifyResult; |
| 76 | }; |
| 77 | |
| 78 | /// \brief Runs a matcher over some code, and returns the result of the |
| 79 | /// verifier for the matched node. |
| 80 | template <typename NodeType> template <typename MatcherType> |
| 81 | testing::AssertionResult MatchVerifier<NodeType>::match( |
Enea Zaffanella | 4c40949 | 2013-07-08 14:50:30 +0000 | [diff] [blame] | 82 | const std::string &Code, const MatcherType &AMatcher, |
| 83 | std::vector<std::string>& Args, Language L) { |
Manuel Klimek | aa58b97 | 2013-01-31 13:10:40 +0000 | [diff] [blame] | 84 | MatchFinder Finder; |
| 85 | Finder.addMatcher(AMatcher.bind(""), this); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 86 | std::unique_ptr<tooling::FrontendActionFactory> Factory( |
Manuel Klimek | aa58b97 | 2013-01-31 13:10:40 +0000 | [diff] [blame] | 87 | tooling::newFrontendActionFactory(&Finder)); |
| 88 | |
Manuel Klimek | aa58b97 | 2013-01-31 13:10:40 +0000 | [diff] [blame] | 89 | StringRef FileName; |
| 90 | switch (L) { |
| 91 | case Lang_C: |
| 92 | Args.push_back("-std=c99"); |
| 93 | FileName = "input.c"; |
| 94 | break; |
| 95 | case Lang_C89: |
| 96 | Args.push_back("-std=c89"); |
| 97 | FileName = "input.c"; |
| 98 | break; |
| 99 | case Lang_CXX: |
| 100 | Args.push_back("-std=c++98"); |
| 101 | FileName = "input.cc"; |
| 102 | break; |
Enea Zaffanella | 8421a06 | 2013-07-07 06:41:54 +0000 | [diff] [blame] | 103 | case Lang_CXX11: |
| 104 | Args.push_back("-std=c++11"); |
| 105 | FileName = "input.cc"; |
| 106 | break; |
Gabor Marton | 1f66753 | 2018-05-24 08:41:07 +0000 | [diff] [blame] | 107 | case Lang_CXX14: |
| 108 | Args.push_back("-std=c++14"); |
| 109 | FileName = "input.cc"; |
| 110 | break; |
Richard Smith | 9ca9101 | 2013-02-05 05:55:57 +0000 | [diff] [blame] | 111 | case Lang_OpenCL: |
| 112 | FileName = "input.cl"; |
Fariborz Jahanian | a1db7df | 2014-07-31 17:39:50 +0000 | [diff] [blame] | 113 | break; |
| 114 | case Lang_OBJCXX: |
| 115 | FileName = "input.mm"; |
| 116 | break; |
Manuel Klimek | aa58b97 | 2013-01-31 13:10:40 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // Default to failure in case callback is never called |
| 120 | setFailure("Could not find match"); |
| 121 | if (!tooling::runToolOnCodeWithArgs(Factory->create(), Code, Args, FileName)) |
| 122 | return testing::AssertionFailure() << "Parsing error"; |
| 123 | if (!Verified) |
| 124 | return testing::AssertionFailure() << VerifyResult; |
| 125 | return testing::AssertionSuccess(); |
| 126 | } |
| 127 | |
Aaron Ballman | a35b8fc | 2016-03-09 17:11:51 +0000 | [diff] [blame] | 128 | /// \brief Runs a matcher over some AST, and returns the result of the |
| 129 | /// verifier for the matched node. |
| 130 | template <typename NodeType> template <typename MatcherType> |
| 131 | testing::AssertionResult MatchVerifier<NodeType>::match( |
| 132 | const Decl *D, const MatcherType &AMatcher) { |
| 133 | MatchFinder Finder; |
| 134 | Finder.addMatcher(AMatcher.bind(""), this); |
| 135 | |
| 136 | setFailure("Could not find match"); |
| 137 | Finder.match(*D, D->getASTContext()); |
| 138 | |
| 139 | if (!Verified) |
| 140 | return testing::AssertionFailure() << VerifyResult; |
| 141 | return testing::AssertionSuccess(); |
| 142 | } |
| 143 | |
Manuel Klimek | aa58b97 | 2013-01-31 13:10:40 +0000 | [diff] [blame] | 144 | template <typename NodeType> |
| 145 | void MatchVerifier<NodeType>::run(const MatchFinder::MatchResult &Result) { |
| 146 | const NodeType *Node = Result.Nodes.getNodeAs<NodeType>(""); |
| 147 | if (!Node) { |
| 148 | setFailure("Matched node has wrong type"); |
| 149 | } else { |
| 150 | // Callback has been called, default to success. |
| 151 | setSuccess(); |
| 152 | verify(Result, *Node); |
| 153 | } |
| 154 | } |
| 155 | |
Peter Collingbourne | b23b39d | 2013-11-06 00:27:12 +0000 | [diff] [blame] | 156 | template <> |
| 157 | inline void MatchVerifier<ast_type_traits::DynTypedNode>::run( |
| 158 | const MatchFinder::MatchResult &Result) { |
| 159 | BoundNodes::IDToNodeMap M = Result.Nodes.getMap(); |
| 160 | BoundNodes::IDToNodeMap::const_iterator I = M.find(""); |
| 161 | if (I == M.end()) { |
| 162 | setFailure("Node was not bound"); |
| 163 | } else { |
| 164 | // Callback has been called, default to success. |
| 165 | setSuccess(); |
| 166 | verify(Result, I->second); |
| 167 | } |
| 168 | } |
| 169 | |
Manuel Klimek | aa58b97 | 2013-01-31 13:10:40 +0000 | [diff] [blame] | 170 | /// \brief Verify whether a node has the correct source location. |
| 171 | /// |
| 172 | /// By default, Node.getSourceLocation() is checked. This can be changed |
| 173 | /// by overriding getLocation(). |
| 174 | template <typename NodeType> |
| 175 | class LocationVerifier : public MatchVerifier<NodeType> { |
| 176 | public: |
| 177 | void expectLocation(unsigned Line, unsigned Column) { |
| 178 | ExpectLine = Line; |
| 179 | ExpectColumn = Column; |
| 180 | } |
| 181 | |
| 182 | protected: |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 183 | void verify(const MatchFinder::MatchResult &Result, |
| 184 | const NodeType &Node) override { |
Manuel Klimek | aa58b97 | 2013-01-31 13:10:40 +0000 | [diff] [blame] | 185 | SourceLocation Loc = getLocation(Node); |
| 186 | unsigned Line = Result.SourceManager->getSpellingLineNumber(Loc); |
| 187 | unsigned Column = Result.SourceManager->getSpellingColumnNumber(Loc); |
| 188 | if (Line != ExpectLine || Column != ExpectColumn) { |
| 189 | std::string MsgStr; |
| 190 | llvm::raw_string_ostream Msg(MsgStr); |
| 191 | Msg << "Expected location <" << ExpectLine << ":" << ExpectColumn |
| 192 | << ">, found <"; |
| 193 | Loc.print(Msg, *Result.SourceManager); |
| 194 | Msg << '>'; |
| 195 | this->setFailure(Msg.str()); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | virtual SourceLocation getLocation(const NodeType &Node) { |
| 200 | return Node.getLocation(); |
| 201 | } |
| 202 | |
| 203 | private: |
| 204 | unsigned ExpectLine, ExpectColumn; |
| 205 | }; |
| 206 | |
| 207 | /// \brief Verify whether a node has the correct source range. |
| 208 | /// |
| 209 | /// By default, Node.getSourceRange() is checked. This can be changed |
| 210 | /// by overriding getRange(). |
| 211 | template <typename NodeType> |
| 212 | class RangeVerifier : public MatchVerifier<NodeType> { |
| 213 | public: |
| 214 | void expectRange(unsigned BeginLine, unsigned BeginColumn, |
| 215 | unsigned EndLine, unsigned EndColumn) { |
| 216 | ExpectBeginLine = BeginLine; |
| 217 | ExpectBeginColumn = BeginColumn; |
| 218 | ExpectEndLine = EndLine; |
| 219 | ExpectEndColumn = EndColumn; |
| 220 | } |
| 221 | |
| 222 | protected: |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 223 | void verify(const MatchFinder::MatchResult &Result, |
| 224 | const NodeType &Node) override { |
Manuel Klimek | aa58b97 | 2013-01-31 13:10:40 +0000 | [diff] [blame] | 225 | SourceRange R = getRange(Node); |
| 226 | SourceLocation Begin = R.getBegin(); |
| 227 | SourceLocation End = R.getEnd(); |
| 228 | unsigned BeginLine = Result.SourceManager->getSpellingLineNumber(Begin); |
| 229 | unsigned BeginColumn = Result.SourceManager->getSpellingColumnNumber(Begin); |
| 230 | unsigned EndLine = Result.SourceManager->getSpellingLineNumber(End); |
| 231 | unsigned EndColumn = Result.SourceManager->getSpellingColumnNumber(End); |
| 232 | if (BeginLine != ExpectBeginLine || BeginColumn != ExpectBeginColumn || |
| 233 | EndLine != ExpectEndLine || EndColumn != ExpectEndColumn) { |
| 234 | std::string MsgStr; |
| 235 | llvm::raw_string_ostream Msg(MsgStr); |
| 236 | Msg << "Expected range <" << ExpectBeginLine << ":" << ExpectBeginColumn |
| 237 | << '-' << ExpectEndLine << ":" << ExpectEndColumn << ">, found <"; |
| 238 | Begin.print(Msg, *Result.SourceManager); |
| 239 | Msg << '-'; |
| 240 | End.print(Msg, *Result.SourceManager); |
| 241 | Msg << '>'; |
| 242 | this->setFailure(Msg.str()); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | virtual SourceRange getRange(const NodeType &Node) { |
| 247 | return Node.getSourceRange(); |
| 248 | } |
| 249 | |
| 250 | private: |
| 251 | unsigned ExpectBeginLine, ExpectBeginColumn, ExpectEndLine, ExpectEndColumn; |
| 252 | }; |
| 253 | |
Peter Collingbourne | b23b39d | 2013-11-06 00:27:12 +0000 | [diff] [blame] | 254 | /// \brief Verify whether a node's dump contains a given substring. |
| 255 | class DumpVerifier : public MatchVerifier<ast_type_traits::DynTypedNode> { |
| 256 | public: |
| 257 | void expectSubstring(const std::string &Str) { |
| 258 | ExpectSubstring = Str; |
| 259 | } |
| 260 | |
| 261 | protected: |
| 262 | void verify(const MatchFinder::MatchResult &Result, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 263 | const ast_type_traits::DynTypedNode &Node) override { |
Peter Collingbourne | b23b39d | 2013-11-06 00:27:12 +0000 | [diff] [blame] | 264 | std::string DumpStr; |
| 265 | llvm::raw_string_ostream Dump(DumpStr); |
| 266 | Node.dump(Dump, *Result.SourceManager); |
| 267 | |
| 268 | if (Dump.str().find(ExpectSubstring) == std::string::npos) { |
| 269 | std::string MsgStr; |
| 270 | llvm::raw_string_ostream Msg(MsgStr); |
| 271 | Msg << "Expected dump substring <" << ExpectSubstring << ">, found <" |
| 272 | << Dump.str() << '>'; |
| 273 | this->setFailure(Msg.str()); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | private: |
| 278 | std::string ExpectSubstring; |
| 279 | }; |
| 280 | |
| 281 | /// \brief Verify whether a node's pretty print matches a given string. |
| 282 | class PrintVerifier : public MatchVerifier<ast_type_traits::DynTypedNode> { |
| 283 | public: |
| 284 | void expectString(const std::string &Str) { |
| 285 | ExpectString = Str; |
| 286 | } |
| 287 | |
| 288 | protected: |
| 289 | void verify(const MatchFinder::MatchResult &Result, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 290 | const ast_type_traits::DynTypedNode &Node) override { |
Peter Collingbourne | b23b39d | 2013-11-06 00:27:12 +0000 | [diff] [blame] | 291 | std::string PrintStr; |
| 292 | llvm::raw_string_ostream Print(PrintStr); |
| 293 | Node.print(Print, Result.Context->getPrintingPolicy()); |
| 294 | |
| 295 | if (Print.str() != ExpectString) { |
| 296 | std::string MsgStr; |
| 297 | llvm::raw_string_ostream Msg(MsgStr); |
| 298 | Msg << "Expected pretty print <" << ExpectString << ">, found <" |
| 299 | << Print.str() << '>'; |
| 300 | this->setFailure(Msg.str()); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | private: |
| 305 | std::string ExpectString; |
| 306 | }; |
| 307 | |
Manuel Klimek | aa58b97 | 2013-01-31 13:10:40 +0000 | [diff] [blame] | 308 | } // end namespace ast_matchers |
| 309 | } // end namespace clang |
Benjamin Kramer | 12152ab | 2014-08-08 13:24:19 +0000 | [diff] [blame] | 310 | |
| 311 | #endif |