Revert "Refactor DynTypedMatcher into a value type class, just like Matcher<T>."
This reverts commit r193100.
It was failing to compile with MSVC 2012 while instantiating
llvm::Optional<DynTypedMatcher>.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@193123 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/unittests/ASTMatchers/Dynamic/ParserTest.cpp b/unittests/ASTMatchers/Dynamic/ParserTest.cpp
index f19ec51..9116ab8 100644
--- a/unittests/ASTMatchers/Dynamic/ParserTest.cpp
+++ b/unittests/ASTMatchers/Dynamic/ParserTest.cpp
@@ -21,14 +21,51 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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"