1 //===- unittest/ASTMatchers/Dynamic/ParserTest.cpp - Parser unit tests -===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===-------------------------------------------------------------------===// 8 9 #include "../ASTMatchersTest.h" 10 #include "clang/ASTMatchers/Dynamic/Parser.h" 11 #include "clang/ASTMatchers/Dynamic/Registry.h" 12 #include "llvm/ADT/Optional.h" 13 #include "gtest/gtest.h" 14 #include <string> 15 #include <vector> 16 17 namespace clang { 18 namespace ast_matchers { 19 namespace dynamic { 20 namespace { 21 22 class MockSema : public Parser::Sema { 23 public: 24 ~MockSema() override {} 25 26 uint64_t expectMatcher(StringRef MatcherName) { 27 // Optimizations on the matcher framework make simple matchers like 28 // 'stmt()' to be all the same matcher. 29 // Use a more complex expression to prevent that. 30 ast_matchers::internal::Matcher<Stmt> M = stmt(stmt(), stmt()); 31 ExpectedMatchers.insert(std::make_pair(std::string(MatcherName), M)); 32 return M.getID().second; 33 } 34 35 void parse(StringRef Code) { 36 Diagnostics Error; 37 VariantValue Value; 38 Parser::parseExpression(Code, this, &Value, &Error); 39 Values.push_back(Value); 40 Errors.push_back(Error.toStringFull()); 41 } 42 43 llvm::Optional<MatcherCtor> 44 lookupMatcherCtor(StringRef MatcherName) override { 45 const ExpectedMatchersTy::value_type *Matcher = 46 &*ExpectedMatchers.find(std::string(MatcherName)); 47 return reinterpret_cast<MatcherCtor>(Matcher); 48 } 49 50 VariantMatcher actOnMatcherExpression(MatcherCtor Ctor, 51 SourceRange NameRange, 52 StringRef BindID, 53 ArrayRef<ParserValue> Args, 54 Diagnostics *Error) override { 55 const ExpectedMatchersTy::value_type *Matcher = 56 reinterpret_cast<const ExpectedMatchersTy::value_type *>(Ctor); 57 MatcherInfo ToStore = {Matcher->first, NameRange, Args, 58 std::string(BindID)}; 59 Matchers.push_back(ToStore); 60 return VariantMatcher::SingleMatcher(Matcher->second); 61 } 62 63 struct MatcherInfo { 64 StringRef MatcherName; 65 SourceRange NameRange; 66 std::vector<ParserValue> Args; 67 std::string BoundID; 68 }; 69 70 std::vector<std::string> Errors; 71 std::vector<VariantValue> Values; 72 std::vector<MatcherInfo> Matchers; 73 typedef std::map<std::string, ast_matchers::internal::Matcher<Stmt> > 74 ExpectedMatchersTy; 75 ExpectedMatchersTy ExpectedMatchers; 76 }; 77 78 TEST(ParserTest, ParseBoolean) { 79 MockSema Sema; 80 Sema.parse("true"); 81 Sema.parse("false"); 82 EXPECT_EQ(2U, Sema.Values.size()); 83 EXPECT_TRUE(Sema.Values[0].getBoolean()); 84 EXPECT_FALSE(Sema.Values[1].getBoolean()); 85 } 86 87 TEST(ParserTest, ParseDouble) { 88 MockSema Sema; 89 Sema.parse("1.0"); 90 Sema.parse("2.0f"); 91 Sema.parse("34.56e-78"); 92 Sema.parse("4.E+6"); 93 Sema.parse("1"); 94 EXPECT_EQ(5U, Sema.Values.size()); 95 EXPECT_EQ(1.0, Sema.Values[0].getDouble()); 96 EXPECT_EQ("1:1: Error parsing numeric literal: <2.0f>", Sema.Errors[1]); 97 EXPECT_EQ(34.56e-78, Sema.Values[2].getDouble()); 98 EXPECT_EQ(4e+6, Sema.Values[3].getDouble()); 99 EXPECT_FALSE(Sema.Values[4].isDouble()); 100 } 101 102 TEST(ParserTest, ParseUnsigned) { 103 MockSema Sema; 104 Sema.parse("0"); 105 Sema.parse("123"); 106 Sema.parse("0x1f"); 107 Sema.parse("12345678901"); 108 Sema.parse("1a1"); 109 EXPECT_EQ(5U, Sema.Values.size()); 110 EXPECT_EQ(0U, Sema.Values[0].getUnsigned()); 111 EXPECT_EQ(123U, Sema.Values[1].getUnsigned()); 112 EXPECT_EQ(31U, Sema.Values[2].getUnsigned()); 113 EXPECT_EQ("1:1: Error parsing numeric literal: <12345678901>", Sema.Errors[3]); 114 EXPECT_EQ("1:1: Error parsing numeric literal: <1a1>", Sema.Errors[4]); 115 } 116 117 TEST(ParserTest, ParseString) { 118 MockSema Sema; 119 Sema.parse("\"Foo\""); 120 Sema.parse("\"\""); 121 Sema.parse("\"Baz"); 122 EXPECT_EQ(3ULL, Sema.Values.size()); 123 EXPECT_EQ("Foo", Sema.Values[0].getString()); 124 EXPECT_EQ("", Sema.Values[1].getString()); 125 EXPECT_EQ("1:1: Error parsing string token: <\"Baz>", Sema.Errors[2]); 126 } 127 128 bool matchesRange(SourceRange Range, unsigned StartLine, 129 unsigned EndLine, unsigned StartColumn, unsigned EndColumn) { 130 EXPECT_EQ(StartLine, Range.Start.Line); 131 EXPECT_EQ(EndLine, Range.End.Line); 132 EXPECT_EQ(StartColumn, Range.Start.Column); 133 EXPECT_EQ(EndColumn, Range.End.Column); 134 return Range.Start.Line == StartLine && Range.End.Line == EndLine && 135 Range.Start.Column == StartColumn && Range.End.Column == EndColumn; 136 } 137 138 llvm::Optional<DynTypedMatcher> getSingleMatcher(const VariantValue &Value) { 139 llvm::Optional<DynTypedMatcher> Result = 140 Value.getMatcher().getSingleMatcher(); 141 EXPECT_TRUE(Result.hasValue()); 142 return Result; 143 } 144 145 TEST(ParserTest, ParseMatcher) { 146 MockSema Sema; 147 const uint64_t ExpectedFoo = Sema.expectMatcher("Foo"); 148 const uint64_t ExpectedBar = Sema.expectMatcher("Bar"); 149 const uint64_t ExpectedBaz = Sema.expectMatcher("Baz"); 150 Sema.parse(" Foo ( Bar ( 17), Baz( \n \"B A,Z\") ) .bind( \"Yo!\") "); 151 for (const auto &E : Sema.Errors) { 152 EXPECT_EQ("", E); 153 } 154 155 EXPECT_NE(ExpectedFoo, ExpectedBar); 156 EXPECT_NE(ExpectedFoo, ExpectedBaz); 157 EXPECT_NE(ExpectedBar, ExpectedBaz); 158 159 EXPECT_EQ(1ULL, Sema.Values.size()); 160 EXPECT_EQ(ExpectedFoo, getSingleMatcher(Sema.Values[0])->getID().second); 161 162 EXPECT_EQ(3ULL, Sema.Matchers.size()); 163 const MockSema::MatcherInfo Bar = Sema.Matchers[0]; 164 EXPECT_EQ("Bar", Bar.MatcherName); 165 EXPECT_TRUE(matchesRange(Bar.NameRange, 1, 1, 8, 17)); 166 EXPECT_EQ(1ULL, Bar.Args.size()); 167 EXPECT_EQ(17U, Bar.Args[0].Value.getUnsigned()); 168 169 const MockSema::MatcherInfo Baz = Sema.Matchers[1]; 170 EXPECT_EQ("Baz", Baz.MatcherName); 171 EXPECT_TRUE(matchesRange(Baz.NameRange, 1, 2, 19, 10)); 172 EXPECT_EQ(1ULL, Baz.Args.size()); 173 EXPECT_EQ("B A,Z", Baz.Args[0].Value.getString()); 174 175 const MockSema::MatcherInfo Foo = Sema.Matchers[2]; 176 EXPECT_EQ("Foo", Foo.MatcherName); 177 EXPECT_TRUE(matchesRange(Foo.NameRange, 1, 2, 2, 12)); 178 EXPECT_EQ(2ULL, Foo.Args.size()); 179 EXPECT_EQ(ExpectedBar, getSingleMatcher(Foo.Args[0].Value)->getID().second); 180 EXPECT_EQ(ExpectedBaz, getSingleMatcher(Foo.Args[1].Value)->getID().second); 181 EXPECT_EQ("Yo!", Foo.BoundID); 182 } 183 184 TEST(ParserTest, ParseComment) { 185 MockSema Sema; 186 Sema.expectMatcher("Foo"); 187 Sema.parse(" Foo() # Bar() "); 188 for (const auto &E : Sema.Errors) { 189 EXPECT_EQ("", E); 190 } 191 192 EXPECT_EQ(1ULL, Sema.Matchers.size()); 193 194 Sema.parse("Foo(#) "); 195 196 EXPECT_EQ("1:4: Error parsing matcher. Found end-of-code while looking for ')'.", Sema.Errors[1]); 197 } 198 199 using ast_matchers::internal::Matcher; 200 201 Parser::NamedValueMap getTestNamedValues() { 202 Parser::NamedValueMap Values; 203 Values["nameX"] = llvm::StringRef("x"); 204 Values["hasParamA"] = VariantMatcher::SingleMatcher( 205 functionDecl(hasParameter(0, hasName("a")))); 206 return Values; 207 } 208 209 TEST(ParserTest, FullParserTest) { 210 Diagnostics Error; 211 212 StringRef Code = 213 "varDecl(hasInitializer(binaryOperator(hasLHS(integerLiteral())," 214 " hasOperatorName(\"+\"))))"; 215 llvm::Optional<DynTypedMatcher> VarDecl( 216 Parser::parseMatcherExpression(Code, &Error)); 217 EXPECT_EQ("", Error.toStringFull()); 218 Matcher<Decl> M = VarDecl->unconditionalConvertTo<Decl>(); 219 EXPECT_TRUE(matches("int x = 1 + false;", M)); 220 EXPECT_FALSE(matches("int x = true + 1;", M)); 221 EXPECT_FALSE(matches("int x = 1 - false;", M)); 222 EXPECT_FALSE(matches("int x = true - 1;", M)); 223 224 Code = "implicitCastExpr(hasCastKind(\"CK_IntegralToBoolean\"))"; 225 llvm::Optional<DynTypedMatcher> implicitIntBooleanCast( 226 Parser::parseMatcherExpression(Code, nullptr, nullptr, &Error)); 227 EXPECT_EQ("", Error.toStringFull()); 228 Matcher<Stmt> MCastStmt = 229 implicitIntBooleanCast->unconditionalConvertTo<Stmt>(); 230 EXPECT_TRUE(matches("bool X = 1;", MCastStmt)); 231 EXPECT_FALSE(matches("bool X = true;", MCastStmt)); 232 233 Code = "functionDecl(hasParameter(1, hasName(\"x\")))"; 234 llvm::Optional<DynTypedMatcher> HasParameter( 235 Parser::parseMatcherExpression(Code, &Error)); 236 EXPECT_EQ("", Error.toStringFull()); 237 M = HasParameter->unconditionalConvertTo<Decl>(); 238 239 EXPECT_TRUE(matches("void f(int a, int x);", M)); 240 EXPECT_FALSE(matches("void f(int x, int a);", M)); 241 242 // Test named values. 243 auto NamedValues = getTestNamedValues(); 244 245 Code = "functionDecl(hasParamA, hasParameter(1, hasName(nameX)))"; 246 llvm::Optional<DynTypedMatcher> HasParameterWithNamedValues( 247 Parser::parseMatcherExpression(Code, nullptr, &NamedValues, &Error)); 248 EXPECT_EQ("", Error.toStringFull()); 249 M = HasParameterWithNamedValues->unconditionalConvertTo<Decl>(); 250 251 EXPECT_TRUE(matches("void f(int a, int x);", M)); 252 EXPECT_FALSE(matches("void f(int x, int a);", M)); 253 254 Code = "unaryExprOrTypeTraitExpr(ofKind(\"UETT_SizeOf\"))"; 255 llvm::Optional<DynTypedMatcher> UnaryExprSizeOf( 256 Parser::parseMatcherExpression(Code, nullptr, nullptr, &Error)); 257 EXPECT_EQ("", Error.toStringFull()); 258 Matcher<Stmt> MStmt = UnaryExprSizeOf->unconditionalConvertTo<Stmt>(); 259 EXPECT_TRUE(matches("unsigned X = sizeof(int);", MStmt)); 260 EXPECT_FALSE(matches("unsigned X = alignof(int);", MStmt)); 261 262 Code = "hasInitializer(\n binaryOperator(hasLHS(\"A\")))"; 263 EXPECT_TRUE(!Parser::parseMatcherExpression(Code, &Error).hasValue()); 264 EXPECT_EQ("1:1: Error parsing argument 1 for matcher hasInitializer.\n" 265 "2:5: Error parsing argument 1 for matcher binaryOperator.\n" 266 "2:20: Error building matcher hasLHS.\n" 267 "2:27: Incorrect type for arg 1. " 268 "(Expected = Matcher<Expr>) != (Actual = String)", 269 Error.toStringFull()); 270 } 271 272 TEST(ParserTest, VariadicMatchTest) { 273 Diagnostics Error; 274 275 StringRef Code = 276 "stmt(objcMessageExpr(hasAnySelector(\"methodA\", \"methodB:\")))"; 277 llvm::Optional<DynTypedMatcher> OM( 278 Parser::parseMatcherExpression(Code, &Error)); 279 EXPECT_EQ("", Error.toStringFull()); 280 auto M = OM->unconditionalConvertTo<Stmt>(); 281 EXPECT_TRUE(matchesObjC("@interface I @end " 282 "void foo(I* i) { [i methodA]; }", M)); 283 } 284 285 std::string ParseWithError(StringRef Code) { 286 Diagnostics Error; 287 VariantValue Value; 288 Parser::parseExpression(Code, &Value, &Error); 289 return Error.toStringFull(); 290 } 291 292 std::string ParseMatcherWithError(StringRef Code) { 293 Diagnostics Error; 294 Parser::parseMatcherExpression(Code, &Error); 295 return Error.toStringFull(); 296 } 297 298 TEST(ParserTest, Errors) { 299 EXPECT_EQ( 300 "1:5: Error parsing matcher. Found token <123> while looking for '('.", 301 ParseWithError("Foo 123")); 302 EXPECT_EQ( 303 "1:1: Matcher not found: Foo\n" 304 "1:9: Error parsing matcher. Found token <123> while looking for ','.", 305 ParseWithError("Foo(\"A\" 123)")); 306 EXPECT_EQ( 307 "1:1: Error parsing argument 1 for matcher stmt.\n" 308 "1:6: Value not found: someValue", 309 ParseWithError("stmt(someValue)")); 310 EXPECT_EQ( 311 "1:1: Matcher not found: Foo\n" 312 "1:4: Error parsing matcher. Found end-of-code while looking for ')'.", 313 ParseWithError("Foo(")); 314 EXPECT_EQ("1:1: End of code found while looking for token.", 315 ParseWithError("")); 316 EXPECT_EQ("Input value is not a matcher expression.", 317 ParseMatcherWithError("\"A\"")); 318 EXPECT_EQ("1:1: Matcher not found: Foo\n" 319 "1:1: Error parsing argument 1 for matcher Foo.\n" 320 "1:5: Invalid token <(> found when looking for a value.", 321 ParseWithError("Foo((")); 322 EXPECT_EQ("1:7: Expected end of code.", ParseWithError("expr()a")); 323 EXPECT_EQ("1:11: Malformed bind() expression.", 324 ParseWithError("isArrow().biind")); 325 EXPECT_EQ("1:15: Malformed bind() expression.", 326 ParseWithError("isArrow().bind")); 327 EXPECT_EQ("1:16: Malformed bind() expression.", 328 ParseWithError("isArrow().bind(foo")); 329 EXPECT_EQ("1:21: Malformed bind() expression.", 330 ParseWithError("isArrow().bind(\"foo\"")); 331 EXPECT_EQ("1:1: Error building matcher isArrow.\n" 332 "1:1: Matcher does not support binding.", 333 ParseWithError("isArrow().bind(\"foo\")")); 334 EXPECT_EQ("Input value has unresolved overloaded type: " 335 "Matcher<DoStmt|ForStmt|WhileStmt|CXXForRangeStmt|FunctionDecl>", 336 ParseMatcherWithError("hasBody(stmt())")); 337 EXPECT_EQ( 338 "1:1: Error parsing argument 1 for matcher decl.\n" 339 "1:6: Error building matcher hasAttr.\n" 340 "1:14: Unknown value 'attr::Fnal' for arg 1; did you mean 'attr::Final'", 341 ParseMatcherWithError(R"query(decl(hasAttr("attr::Fnal")))query")); 342 EXPECT_EQ("1:1: Error parsing argument 1 for matcher decl.\n" 343 "1:6: Error building matcher hasAttr.\n" 344 "1:14: Unknown value 'Final' for arg 1; did you mean 'attr::Final'", 345 ParseMatcherWithError(R"query(decl(hasAttr("Final")))query")); 346 EXPECT_EQ("1:1: Error parsing argument 1 for matcher decl.\n" 347 "1:6: Error building matcher hasAttr.\n" 348 "1:14: Incorrect type for arg 1. (Expected = string) != (Actual = " 349 "String)", 350 ParseMatcherWithError(R"query(decl(hasAttr("unrelated")))query")); 351 } 352 353 TEST(ParserTest, OverloadErrors) { 354 EXPECT_EQ("1:1: Error building matcher callee.\n" 355 "1:8: Candidate 1: Incorrect type for arg 1. " 356 "(Expected = Matcher<Stmt>) != (Actual = String)\n" 357 "1:8: Candidate 2: Incorrect type for arg 1. " 358 "(Expected = Matcher<Decl>) != (Actual = String)", 359 ParseWithError("callee(\"A\")")); 360 } 361 362 TEST(ParserTest, ParseMultiline) { 363 StringRef Code; 364 365 llvm::Optional<DynTypedMatcher> M; 366 { 367 Code = R"matcher(varDecl( 368 hasName("foo") 369 ) 370 )matcher"; 371 Diagnostics Error; 372 EXPECT_TRUE(Parser::parseMatcherExpression(Code, &Error).hasValue()); 373 } 374 375 { 376 Code = R"matcher(varDecl( 377 # Internal comment 378 hasName("foo") # Internal comment 379 # Internal comment 380 ) 381 )matcher"; 382 Diagnostics Error; 383 EXPECT_TRUE(Parser::parseMatcherExpression(Code, &Error).hasValue()); 384 } 385 386 { 387 Code = R"matcher(decl().bind( 388 "paramName") 389 )matcher"; 390 Diagnostics Error; 391 EXPECT_TRUE(Parser::parseMatcherExpression(Code, &Error).hasValue()); 392 } 393 394 { 395 Code = R"matcher(decl().bind( 396 "paramName" 397 ) 398 )matcher"; 399 Diagnostics Error; 400 EXPECT_TRUE(Parser::parseMatcherExpression(Code, &Error).hasValue()); 401 } 402 403 { 404 Code = R"matcher(decl(decl() 405 , decl()))matcher"; 406 Diagnostics Error; 407 EXPECT_TRUE(Parser::parseMatcherExpression(Code, &Error).hasValue()); 408 } 409 410 { 411 Code = R"matcher(decl(decl(), 412 decl()))matcher"; 413 Diagnostics Error; 414 EXPECT_TRUE(Parser::parseMatcherExpression(Code, &Error).hasValue()); 415 } 416 417 { 418 Code = "namedDecl(hasName(\"n\"\n))"; 419 Diagnostics Error; 420 EXPECT_TRUE(Parser::parseMatcherExpression(Code, &Error).hasValue()); 421 } 422 423 { 424 Diagnostics Error; 425 426 auto NamedValues = getTestNamedValues(); 427 428 Code = R"matcher(hasParamA.bind 429 ("paramName") 430 )matcher"; 431 M = Parser::parseMatcherExpression(Code, nullptr, &NamedValues, &Error); 432 EXPECT_FALSE(M.hasValue()); 433 EXPECT_EQ("1:15: Malformed bind() expression.", Error.toStringFull()); 434 } 435 436 { 437 Diagnostics Error; 438 439 auto NamedValues = getTestNamedValues(); 440 441 Code = R"matcher(hasParamA. 442 bind("paramName") 443 )matcher"; 444 M = Parser::parseMatcherExpression(Code, nullptr, &NamedValues, &Error); 445 EXPECT_FALSE(M.hasValue()); 446 EXPECT_EQ("1:11: Malformed bind() expression.", Error.toStringFull()); 447 } 448 449 { 450 Diagnostics Error; 451 452 Code = R"matcher(varDecl 453 () 454 )matcher"; 455 M = Parser::parseMatcherExpression(Code, nullptr, nullptr, &Error); 456 EXPECT_FALSE(M.hasValue()); 457 EXPECT_EQ("1:8: Error parsing matcher. Found token " 458 "<NewLine> while looking for '('.", 459 Error.toStringFull()); 460 } 461 462 // Correct line/column numbers 463 { 464 Diagnostics Error; 465 466 Code = R"matcher(varDecl( 467 doesNotExist() 468 ) 469 )matcher"; 470 M = Parser::parseMatcherExpression(Code, nullptr, nullptr, &Error); 471 EXPECT_FALSE(M.hasValue()); 472 StringRef Expected = R"error(1:1: Error parsing argument 1 for matcher varDecl. 473 2:3: Matcher not found: doesNotExist)error"; 474 EXPECT_EQ(Expected, Error.toStringFull()); 475 } 476 } 477 478 TEST(ParserTest, CompletionRegistry) { 479 StringRef Code = "while"; 480 std::vector<MatcherCompletion> Comps = Parser::completeExpression(Code, 5); 481 ASSERT_EQ(1u, Comps.size()); 482 EXPECT_EQ("Stmt(", Comps[0].TypedText); 483 EXPECT_EQ("Matcher<Stmt> whileStmt(Matcher<WhileStmt>...)", 484 Comps[0].MatcherDecl); 485 486 Code = "whileStmt()."; 487 Comps = Parser::completeExpression(Code, 12); 488 ASSERT_EQ(1u, Comps.size()); 489 EXPECT_EQ("bind(\"", Comps[0].TypedText); 490 EXPECT_EQ("bind", Comps[0].MatcherDecl); 491 } 492 493 TEST(ParserTest, CompletionNamedValues) { 494 // Can complete non-matcher types. 495 auto NamedValues = getTestNamedValues(); 496 StringRef Code = "functionDecl(hasName("; 497 std::vector<MatcherCompletion> Comps = 498 Parser::completeExpression(Code, Code.size(), nullptr, &NamedValues); 499 ASSERT_EQ(1u, Comps.size()); 500 EXPECT_EQ("nameX", Comps[0].TypedText); 501 EXPECT_EQ("String nameX", Comps[0].MatcherDecl); 502 503 // Can complete if there are names in the expression. 504 Code = "cxxMethodDecl(hasName(nameX), "; 505 Comps = Parser::completeExpression(Code, Code.size(), nullptr, &NamedValues); 506 EXPECT_LT(0u, Comps.size()); 507 508 // Can complete names and registry together. 509 Code = "functionDecl(hasP"; 510 Comps = Parser::completeExpression(Code, Code.size(), nullptr, &NamedValues); 511 ASSERT_EQ(3u, Comps.size()); 512 513 EXPECT_EQ("arameter(", Comps[0].TypedText); 514 EXPECT_EQ( 515 "Matcher<FunctionDecl> hasParameter(unsigned, Matcher<ParmVarDecl>)", 516 Comps[0].MatcherDecl); 517 518 EXPECT_EQ("aramA", Comps[1].TypedText); 519 EXPECT_EQ("Matcher<Decl> hasParamA", Comps[1].MatcherDecl); 520 521 EXPECT_EQ("arent(", Comps[2].TypedText); 522 EXPECT_EQ( 523 "Matcher<Decl> " 524 "hasParent(Matcher<NestedNameSpecifierLoc|TypeLoc|Decl|...>)", 525 Comps[2].MatcherDecl); 526 } 527 528 TEST(ParserTest, ParseBindOnLet) { 529 530 auto NamedValues = getTestNamedValues(); 531 532 Diagnostics Error; 533 534 { 535 StringRef Code = "hasParamA.bind(\"parmABinding\")"; 536 llvm::Optional<DynTypedMatcher> TopLevelLetBinding( 537 Parser::parseMatcherExpression(Code, nullptr, &NamedValues, &Error)); 538 EXPECT_EQ("", Error.toStringFull()); 539 auto M = TopLevelLetBinding->unconditionalConvertTo<Decl>(); 540 541 EXPECT_TRUE(matchAndVerifyResultTrue( 542 "void foo(int a);", M, 543 std::make_unique<VerifyIdIsBoundTo<FunctionDecl>>("parmABinding"))); 544 EXPECT_TRUE(matchAndVerifyResultFalse( 545 "void foo(int b);", M, 546 std::make_unique<VerifyIdIsBoundTo<FunctionDecl>>("parmABinding"))); 547 } 548 549 { 550 StringRef Code = "functionDecl(hasParamA.bind(\"parmABinding\"))"; 551 llvm::Optional<DynTypedMatcher> NestedLetBinding( 552 Parser::parseMatcherExpression(Code, nullptr, &NamedValues, &Error)); 553 EXPECT_EQ("", Error.toStringFull()); 554 auto M = NestedLetBinding->unconditionalConvertTo<Decl>(); 555 556 EXPECT_TRUE(matchAndVerifyResultTrue( 557 "void foo(int a);", M, 558 std::make_unique<VerifyIdIsBoundTo<FunctionDecl>>("parmABinding"))); 559 EXPECT_TRUE(matchAndVerifyResultFalse( 560 "void foo(int b);", M, 561 std::make_unique<VerifyIdIsBoundTo<FunctionDecl>>("parmABinding"))); 562 } 563 } 564 565 } // end anonymous namespace 566 } // end namespace dynamic 567 } // end namespace ast_matchers 568 } // end namespace clang 569