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