From e6e8fef281d155094fae889cd0c9b7a5830f496b Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Thu, 30 Aug 2018 23:11:09 +0000 Subject: [PATCH] Allow binding to NamedValue resulting from let expression Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D51259 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@341142 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/ASTMatchers/Dynamic/Parser.cpp | 23 ++++++++++-- unittests/ASTMatchers/Dynamic/ParserTest.cpp | 38 ++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/lib/ASTMatchers/Dynamic/Parser.cpp b/lib/ASTMatchers/Dynamic/Parser.cpp index 1288b0897f..96362cd4bc 100644 --- a/lib/ASTMatchers/Dynamic/Parser.cpp +++ b/lib/ASTMatchers/Dynamic/Parser.cpp @@ -339,8 +339,27 @@ bool Parser::parseIdentifierPrefixImpl(VariantValue *Value) { 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; + if (!parseBindID(BindID)) + return false; + + assert(NamedValue.isMatcher()); + llvm::Optional Result = + NamedValue.getMatcher().getSingleMatcher(); + if (Result.hasValue()) { + llvm::Optional 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. diff --git a/unittests/ASTMatchers/Dynamic/ParserTest.cpp b/unittests/ASTMatchers/Dynamic/ParserTest.cpp index d670e3129b..fd7bbdde4a 100644 --- a/unittests/ASTMatchers/Dynamic/ParserTest.cpp +++ b/unittests/ASTMatchers/Dynamic/ParserTest.cpp @@ -359,6 +359,44 @@ TEST(ParserTest, CompletionNamedValues) { Comps[2].MatcherDecl); } +TEST(ParserTest, ParseBindOnLet) { + + auto NamedValues = getTestNamedValues(); + + Diagnostics Error; + + { + llvm::Optional TopLevelLetBinding( + Parser::parseMatcherExpression("hasParamA.bind(\"parmABinding\")", + nullptr, &NamedValues, &Error)); + EXPECT_EQ("", Error.toStringFull()); + auto M = TopLevelLetBinding->unconditionalConvertTo(); + + EXPECT_TRUE(matchAndVerifyResultTrue( + "void foo(int a);", M, + llvm::make_unique>("parmABinding"))); + EXPECT_TRUE(matchAndVerifyResultFalse( + "void foo(int b);", M, + llvm::make_unique>("parmABinding"))); + } + + { + llvm::Optional NestedLetBinding( + Parser::parseMatcherExpression( + "functionDecl(hasParamA.bind(\"parmABinding\"))", nullptr, + &NamedValues, &Error)); + EXPECT_EQ("", Error.toStringFull()); + auto M = NestedLetBinding->unconditionalConvertTo(); + + EXPECT_TRUE(matchAndVerifyResultTrue( + "void foo(int a);", M, + llvm::make_unique>("parmABinding"))); + EXPECT_TRUE(matchAndVerifyResultFalse( + "void foo(int b);", M, + llvm::make_unique>("parmABinding"))); + } +} + } // end anonymous namespace } // end namespace dynamic } // end namespace ast_matchers -- 2.40.0