diff options
Diffstat (limited to 'unittests/AST/MatchVerifier.h')
-rw-r--r-- | unittests/AST/MatchVerifier.h | 93 |
1 files changed, 88 insertions, 5 deletions
diff --git a/unittests/AST/MatchVerifier.h b/unittests/AST/MatchVerifier.h index 7aa7886..5a29cde 100644 --- a/unittests/AST/MatchVerifier.h +++ b/unittests/AST/MatchVerifier.h @@ -25,7 +25,7 @@ namespace clang { namespace ast_matchers { -enum Language { Lang_C, Lang_C89, Lang_CXX, Lang_OpenCL }; +enum Language { Lang_C, Lang_C89, Lang_CXX, Lang_CXX11, Lang_OpenCL }; /// \brief Base class for verifying some property of nodes found by a matcher. template <typename NodeType> @@ -34,12 +34,23 @@ public: template <typename MatcherType> testing::AssertionResult match(const std::string &Code, const MatcherType &AMatcher) { - return match(Code, AMatcher, Lang_CXX); + std::vector<std::string> Args; + return match(Code, AMatcher, Args, Lang_CXX); } template <typename MatcherType> testing::AssertionResult match(const std::string &Code, - const MatcherType &AMatcher, Language L); + const MatcherType &AMatcher, + Language L) { + std::vector<std::string> Args; + return match(Code, AMatcher, Args, L); + } + + template <typename MatcherType> + testing::AssertionResult match(const std::string &Code, + const MatcherType &AMatcher, + std::vector<std::string>& Args, + Language L); protected: virtual void run(const MatchFinder::MatchResult &Result); @@ -64,13 +75,13 @@ private: /// verifier for the matched node. template <typename NodeType> template <typename MatcherType> testing::AssertionResult MatchVerifier<NodeType>::match( - const std::string &Code, const MatcherType &AMatcher, Language L) { + const std::string &Code, const MatcherType &AMatcher, + std::vector<std::string>& Args, Language L) { MatchFinder Finder; Finder.addMatcher(AMatcher.bind(""), this); OwningPtr<tooling::FrontendActionFactory> Factory( tooling::newFrontendActionFactory(&Finder)); - std::vector<std::string> Args; StringRef FileName; switch (L) { case Lang_C: @@ -85,6 +96,10 @@ testing::AssertionResult MatchVerifier<NodeType>::match( Args.push_back("-std=c++98"); FileName = "input.cc"; break; + case Lang_CXX11: + Args.push_back("-std=c++11"); + FileName = "input.cc"; + break; case Lang_OpenCL: FileName = "input.cl"; } @@ -110,6 +125,20 @@ void MatchVerifier<NodeType>::run(const MatchFinder::MatchResult &Result) { } } +template <> +inline void MatchVerifier<ast_type_traits::DynTypedNode>::run( + const MatchFinder::MatchResult &Result) { + BoundNodes::IDToNodeMap M = Result.Nodes.getMap(); + BoundNodes::IDToNodeMap::const_iterator I = M.find(""); + if (I == M.end()) { + setFailure("Node was not bound"); + } else { + // Callback has been called, default to success. + setSuccess(); + verify(Result, I->second); + } +} + /// \brief Verify whether a node has the correct source location. /// /// By default, Node.getSourceLocation() is checked. This can be changed @@ -192,5 +221,59 @@ private: unsigned ExpectBeginLine, ExpectBeginColumn, ExpectEndLine, ExpectEndColumn; }; +/// \brief Verify whether a node's dump contains a given substring. +class DumpVerifier : public MatchVerifier<ast_type_traits::DynTypedNode> { +public: + void expectSubstring(const std::string &Str) { + ExpectSubstring = Str; + } + +protected: + void verify(const MatchFinder::MatchResult &Result, + const ast_type_traits::DynTypedNode &Node) { + std::string DumpStr; + llvm::raw_string_ostream Dump(DumpStr); + Node.dump(Dump, *Result.SourceManager); + + if (Dump.str().find(ExpectSubstring) == std::string::npos) { + std::string MsgStr; + llvm::raw_string_ostream Msg(MsgStr); + Msg << "Expected dump substring <" << ExpectSubstring << ">, found <" + << Dump.str() << '>'; + this->setFailure(Msg.str()); + } + } + +private: + std::string ExpectSubstring; +}; + +/// \brief Verify whether a node's pretty print matches a given string. +class PrintVerifier : public MatchVerifier<ast_type_traits::DynTypedNode> { +public: + void expectString(const std::string &Str) { + ExpectString = Str; + } + +protected: + void verify(const MatchFinder::MatchResult &Result, + const ast_type_traits::DynTypedNode &Node) { + std::string PrintStr; + llvm::raw_string_ostream Print(PrintStr); + Node.print(Print, Result.Context->getPrintingPolicy()); + + if (Print.str() != ExpectString) { + std::string MsgStr; + llvm::raw_string_ostream Msg(MsgStr); + Msg << "Expected pretty print <" << ExpectString << ">, found <" + << Print.str() << '>'; + this->setFailure(Msg.str()); + } + } + +private: + std::string ExpectString; +}; + } // end namespace ast_matchers } // end namespace clang |