[PATCH] D51259: Allow binding to NamedValue resulting from let expression

Stephen Kelly via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Aug 25 09:15:38 PDT 2018


steveire created this revision.
Herald added a subscriber: cfe-commits.

Repository:
  rC Clang

https://reviews.llvm.org/D51259

Files:
  lib/ASTMatchers/Dynamic/Parser.cpp
  unittests/ASTMatchers/Dynamic/ParserTest.cpp


Index: unittests/ASTMatchers/Dynamic/ParserTest.cpp
===================================================================
--- unittests/ASTMatchers/Dynamic/ParserTest.cpp
+++ unittests/ASTMatchers/Dynamic/ParserTest.cpp
@@ -359,6 +359,44 @@
       Comps[2].MatcherDecl);
 }
 
+TEST(ParserTest, ParseBindOnLet) {
+
+  auto NamedValues = getTestNamedValues();
+
+  Diagnostics Error;
+
+  {
+    llvm::Optional<DynTypedMatcher> topLevelLetBinding(
+        Parser::parseMatcherExpression("hasParamA.bind(\"parmABinding\")",
+                                       nullptr, &NamedValues, &Error));
+    EXPECT_EQ("", Error.toStringFull());
+    auto M = topLevelLetBinding->unconditionalConvertTo<Decl>();
+
+    EXPECT_TRUE(matchAndVerifyResultTrue(
+        "void foo(int a);", M,
+        llvm::make_unique<VerifyIdIsBoundTo<FunctionDecl>>("parmABinding")));
+    EXPECT_TRUE(matchAndVerifyResultFalse(
+        "void foo(int b);", M,
+        llvm::make_unique<VerifyIdIsBoundTo<FunctionDecl>>("parmABinding")));
+  }
+
+  {
+    llvm::Optional<DynTypedMatcher> nestedLetBinding(
+        Parser::parseMatcherExpression(
+            "functionDecl(hasParamA.bind(\"parmABinding\"))", nullptr,
+            &NamedValues, &Error));
+    EXPECT_EQ("", Error.toStringFull());
+    auto M = nestedLetBinding->unconditionalConvertTo<Decl>();
+
+    EXPECT_TRUE(matchAndVerifyResultTrue(
+        "void foo(int a);", M,
+        llvm::make_unique<VerifyIdIsBoundTo<FunctionDecl>>("parmABinding")));
+    EXPECT_TRUE(matchAndVerifyResultFalse(
+        "void foo(int b);", M,
+        llvm::make_unique<VerifyIdIsBoundTo<FunctionDecl>>("parmABinding")));
+  }
+}
+
 }  // end anonymous namespace
 }  // end namespace dynamic
 }  // end namespace ast_matchers
Index: lib/ASTMatchers/Dynamic/Parser.cpp
===================================================================
--- lib/ASTMatchers/Dynamic/Parser.cpp
+++ lib/ASTMatchers/Dynamic/Parser.cpp
@@ -339,8 +339,28 @@
     if (const VariantValue NamedValue =
             NamedValues ? NamedValues->lookup(NameToken.Text)
                         : VariantValue()) {
-      *Value = NamedValue;
-      return true;
+
+      if (Tokenizer->nextTokenKind() != TokenInfo::TK_Period) {
+        *Value = NamedValue;
+        return true;
+      }
+
+      std::string BindID;
+      TokenInfo EndToken;
+      if (!parseBindID(BindID, EndToken))
+        return false;
+
+      assert(NamedValue.isMatcher());
+      llvm::Optional<DynTypedMatcher> Result =
+          NamedValue.getMatcher().getSingleMatcher();
+      if (Result.hasValue()) {
+        llvm::Optional<DynTypedMatcher> Bound = Result->tryBind(BindID);
+        if (Bound.hasValue()) {
+          *Value = VariantMatcher::SingleMatcher(*Bound);
+          return true;
+        }
+      }
+      return false;
     }
     // If the syntax is correct and the name is not a matcher either, report
     // unknown named value.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D51259.162553.patch
Type: text/x-patch
Size: 2927 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180825/72fe123a/attachment.bin>


More information about the cfe-commits mailing list