Resubmit "Refactor DynTypedMatcher into a value type class, just like Matcher<T>."

Summary: This resubmits r193100, plus a fix for a breakage with MSVC.

Reviewers: klimek, rnk

CC: cfe-commits, revane

Differential Revision: http://llvm-reviews.chandlerc.com/D2005

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@193613 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/unittests/ASTMatchers/Dynamic/ParserTest.cpp b/unittests/ASTMatchers/Dynamic/ParserTest.cpp
index 9116ab8..f19ec51 100644
--- a/unittests/ASTMatchers/Dynamic/ParserTest.cpp
+++ b/unittests/ASTMatchers/Dynamic/ParserTest.cpp
@@ -21,51 +21,14 @@
 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) {
-    uint64_t ID = ExpectedMatchers.size() + 1;
-    ExpectedMatchers[MatcherName] = ID;
-    return ID;
+    ast_matchers::internal::Matcher<Stmt> M = stmt();
+    ExpectedMatchers.insert(std::make_pair(MatcherName, M));
+    return M.getID();
   }
 
   void parse(StringRef Code) {
@@ -83,9 +46,8 @@
                                         Diagnostics *Error) {
     MatcherInfo ToStore = { MatcherName, NameRange, Args, BindID };
     Matchers.push_back(ToStore);
-    DummyDynTypedMatcher Matcher(ExpectedMatchers[MatcherName]);
-    OwningPtr<DynTypedMatcher> Out(Matcher.tryBind(BindID));
-    return VariantMatcher::SingleMatcher(*Out);
+    return VariantMatcher::SingleMatcher(
+        ExpectedMatchers.find(MatcherName)->second);
   }
 
   struct MatcherInfo {
@@ -98,7 +60,8 @@
   std::vector<std::string> Errors;
   std::vector<VariantValue> Values;
   std::vector<MatcherInfo> Matchers;
-  llvm::StringMap<uint64_t> ExpectedMatchers;
+  std::map<std::string, ast_matchers::internal::Matcher<Stmt> >
+  ExpectedMatchers;
 };
 
 TEST(ParserTest, ParseUnsigned) {
@@ -137,10 +100,11 @@
          Range.Start.Column == StartColumn && Range.End.Column == EndColumn;
 }
 
-const DynTypedMatcher *getSingleMatcher(const VariantValue &Value) {
-  const DynTypedMatcher *Out;
-  EXPECT_TRUE(Value.getMatcher().getSingleMatcher(Out));
-  return Out;
+llvm::Optional<DynTypedMatcher> getSingleMatcher(const VariantValue &Value) {
+  llvm::Optional<DynTypedMatcher> Result =
+      Value.getMatcher().getSingleMatcher();
+  EXPECT_TRUE(Result.hasValue());
+  return Result;
 }
 
 TEST(ParserTest, ParseMatcher) {
@@ -155,8 +119,6 @@
 
   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];
@@ -184,27 +146,28 @@
 
 TEST(ParserTest, FullParserTest) {
   Diagnostics Error;
-  OwningPtr<DynTypedMatcher> VarDecl(Parser::parseMatcherExpression(
+  llvm::Optional<DynTypedMatcher> VarDecl(Parser::parseMatcherExpression(
       "varDecl(hasInitializer(binaryOperator(hasLHS(integerLiteral()),"
       "                                      hasOperatorName(\"+\"))))",
       &Error));
   EXPECT_EQ("", Error.toStringFull());
-  Matcher<Decl> M = Matcher<Decl>::constructFrom(*VarDecl);
+  Matcher<Decl> M = VarDecl->unconditionalConvertTo<Decl>();
   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));
 
-  OwningPtr<DynTypedMatcher> HasParameter(Parser::parseMatcherExpression(
+  llvm::Optional<DynTypedMatcher> HasParameter(Parser::parseMatcherExpression(
       "functionDecl(hasParameter(1, hasName(\"x\")))", &Error));
   EXPECT_EQ("", Error.toStringFull());
-  M = Matcher<Decl>::constructFrom(*HasParameter);
+  M = HasParameter->unconditionalConvertTo<Decl>();
 
   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) == NULL);
+  EXPECT_TRUE(!Parser::parseMatcherExpression(
+                   "hasInitializer(\n    binaryOperator(hasLHS(\"A\")))",
+                   &Error).hasValue());
   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"