diff options
Diffstat (limited to 'clang/unittests/ASTMatchers/Dynamic/ParserTest.cpp')
-rw-r--r-- | clang/unittests/ASTMatchers/Dynamic/ParserTest.cpp | 75 |
1 files changed, 56 insertions, 19 deletions
diff --git a/clang/unittests/ASTMatchers/Dynamic/ParserTest.cpp b/clang/unittests/ASTMatchers/Dynamic/ParserTest.cpp index f19ec51ad70..9116ab8a9f3 100644 --- a/clang/unittests/ASTMatchers/Dynamic/ParserTest.cpp +++ b/clang/unittests/ASTMatchers/Dynamic/ParserTest.cpp @@ -21,14 +21,51 @@ namespace ast_matchers { namespace dynamic { namespace { +class DummyDynTypedMatcher : public DynTypedMatcher { +public: + DummyDynTypedMatcher(uint64_t ID) : ID(ID) {} + DummyDynTypedMatcher(uint64_t ID, StringRef BoundID) + : ID(ID), BoundID(BoundID) {} + + typedef ast_matchers::internal::ASTMatchFinder ASTMatchFinder; + typedef ast_matchers::internal::BoundNodesTreeBuilder BoundNodesTreeBuilder; + virtual bool matches(const ast_type_traits::DynTypedNode DynNode, + ASTMatchFinder *Finder, + BoundNodesTreeBuilder *Builder) const { + return false; + } + + /// \brief Makes a copy of this matcher object. + virtual DynTypedMatcher *clone() const { + return new DummyDynTypedMatcher(*this); + } + + /// \brief Returns a unique ID for the matcher. + virtual uint64_t getID() const { return ID; } + + virtual DynTypedMatcher* tryBind(StringRef BoundID) const { + return new DummyDynTypedMatcher(ID, BoundID); + } + + StringRef boundID() const { return BoundID; } + + virtual ast_type_traits::ASTNodeKind getSupportedKind() const { + return ast_type_traits::ASTNodeKind(); + } + +private: + uint64_t ID; + std::string BoundID; +}; + class MockSema : public Parser::Sema { public: virtual ~MockSema() {} uint64_t expectMatcher(StringRef MatcherName) { - ast_matchers::internal::Matcher<Stmt> M = stmt(); - ExpectedMatchers.insert(std::make_pair(MatcherName, M)); - return M.getID(); + uint64_t ID = ExpectedMatchers.size() + 1; + ExpectedMatchers[MatcherName] = ID; + return ID; } void parse(StringRef Code) { @@ -46,8 +83,9 @@ public: Diagnostics *Error) { MatcherInfo ToStore = { MatcherName, NameRange, Args, BindID }; Matchers.push_back(ToStore); - return VariantMatcher::SingleMatcher( - ExpectedMatchers.find(MatcherName)->second); + DummyDynTypedMatcher Matcher(ExpectedMatchers[MatcherName]); + OwningPtr<DynTypedMatcher> Out(Matcher.tryBind(BindID)); + return VariantMatcher::SingleMatcher(*Out); } struct MatcherInfo { @@ -60,8 +98,7 @@ public: std::vector<std::string> Errors; std::vector<VariantValue> Values; std::vector<MatcherInfo> Matchers; - std::map<std::string, ast_matchers::internal::Matcher<Stmt> > - ExpectedMatchers; + llvm::StringMap<uint64_t> ExpectedMatchers; }; TEST(ParserTest, ParseUnsigned) { @@ -100,11 +137,10 @@ bool matchesRange(const SourceRange &Range, unsigned StartLine, Range.Start.Column == StartColumn && Range.End.Column == EndColumn; } -llvm::Optional<DynTypedMatcher> getSingleMatcher(const VariantValue &Value) { - llvm::Optional<DynTypedMatcher> Result = - Value.getMatcher().getSingleMatcher(); - EXPECT_TRUE(Result.hasValue()); - return Result; +const DynTypedMatcher *getSingleMatcher(const VariantValue &Value) { + const DynTypedMatcher *Out; + EXPECT_TRUE(Value.getMatcher().getSingleMatcher(Out)); + return Out; } TEST(ParserTest, ParseMatcher) { @@ -119,6 +155,8 @@ TEST(ParserTest, ParseMatcher) { EXPECT_EQ(1ULL, Sema.Values.size()); EXPECT_EQ(ExpectedFoo, getSingleMatcher(Sema.Values[0])->getID()); + EXPECT_EQ("Yo!", static_cast<const DummyDynTypedMatcher *>( + getSingleMatcher(Sema.Values[0]))->boundID()); EXPECT_EQ(3ULL, Sema.Matchers.size()); const MockSema::MatcherInfo Bar = Sema.Matchers[0]; @@ -146,28 +184,27 @@ using ast_matchers::internal::Matcher; TEST(ParserTest, FullParserTest) { Diagnostics Error; - llvm::Optional<DynTypedMatcher> VarDecl(Parser::parseMatcherExpression( + OwningPtr<DynTypedMatcher> VarDecl(Parser::parseMatcherExpression( "varDecl(hasInitializer(binaryOperator(hasLHS(integerLiteral())," " hasOperatorName(\"+\"))))", &Error)); EXPECT_EQ("", Error.toStringFull()); - Matcher<Decl> M = VarDecl->unconditionalConvertTo<Decl>(); + Matcher<Decl> M = Matcher<Decl>::constructFrom(*VarDecl); EXPECT_TRUE(matches("int x = 1 + false;", M)); EXPECT_FALSE(matches("int x = true + 1;", M)); EXPECT_FALSE(matches("int x = 1 - false;", M)); EXPECT_FALSE(matches("int x = true - 1;", M)); - llvm::Optional<DynTypedMatcher> HasParameter(Parser::parseMatcherExpression( + OwningPtr<DynTypedMatcher> HasParameter(Parser::parseMatcherExpression( "functionDecl(hasParameter(1, hasName(\"x\")))", &Error)); EXPECT_EQ("", Error.toStringFull()); - M = HasParameter->unconditionalConvertTo<Decl>(); + M = Matcher<Decl>::constructFrom(*HasParameter); EXPECT_TRUE(matches("void f(int a, int x);", M)); EXPECT_FALSE(matches("void f(int x, int a);", M)); - EXPECT_TRUE(!Parser::parseMatcherExpression( - "hasInitializer(\n binaryOperator(hasLHS(\"A\")))", - &Error).hasValue()); + EXPECT_TRUE(Parser::parseMatcherExpression( + "hasInitializer(\n binaryOperator(hasLHS(\"A\")))", &Error) == NULL); EXPECT_EQ("1:1: Error parsing argument 1 for matcher hasInitializer.\n" "2:5: Error parsing argument 1 for matcher binaryOperator.\n" "2:20: Error building matcher hasLHS.\n" |