1 //===- unittest/ASTMatchers/Dynamic/ParserTest.cpp - Parser unit tests -===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===-------------------------------------------------------------------===// 9 10 #include "../ASTMatchersTest.h" 11 #include "clang/ASTMatchers/Dynamic/Parser.h" 12 #include "clang/ASTMatchers/Dynamic/Registry.h" 13 #include "llvm/ADT/Optional.h" 14 #include "gtest/gtest.h" 15 #include <string> 16 #include <vector> 17 18 namespace clang { 19 namespace ast_matchers { 20 namespace dynamic { 21 namespace { 22 23 class MockSema : public Parser::Sema { 24 public: 25 ~MockSema() override {} 26 27 uint64_t expectMatcher(StringRef MatcherName) { 28 // Optimizations on the matcher framework make simple matchers like 29 // 'stmt()' to be all the same matcher. 30 // Use a more complex expression to prevent that. 31 ast_matchers::internal::Matcher<Stmt> M = stmt(stmt(), stmt()); 32 ExpectedMatchers.insert(std::make_pair(MatcherName, M)); 33 return M.getID().second; 34 } 35 36 void parse(StringRef Code) { 37 Diagnostics Error; 38 VariantValue Value; 39 Parser::parseExpression(Code, this, &Value, &Error); 40 Values.push_back(Value); 41 Errors.push_back(Error.toStringFull()); 42 } 43 44 llvm::Optional<MatcherCtor> 45 lookupMatcherCtor(StringRef MatcherName) override { 46 const ExpectedMatchersTy::value_type *Matcher = 47 &*ExpectedMatchers.find(MatcherName); 48 return reinterpret_cast<MatcherCtor>(Matcher); 49 } 50 51 VariantMatcher actOnMatcherExpression(MatcherCtor Ctor, 52 SourceRange NameRange, 53 StringRef BindID, 54 ArrayRef<ParserValue> Args, 55 Diagnostics *Error) override { 56 const ExpectedMatchersTy::value_type *Matcher = 57 reinterpret_cast<const ExpectedMatchersTy::value_type *>(Ctor); 58 MatcherInfo ToStore = { Matcher->first, NameRange, Args, 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 (size_t i = 0, e = Sema.Errors.size(); i != e; ++i) { 152 EXPECT_EQ("", Sema.Errors[i]); 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 using ast_matchers::internal::Matcher; 185 186 Parser::NamedValueMap getTestNamedValues() { 187 Parser::NamedValueMap Values; 188 Values["nameX"] = llvm::StringRef("x"); 189 Values["hasParamA"] = 190 VariantMatcher::SingleMatcher(hasParameter(0, hasName("a"))); 191 return Values; 192 } 193 194 TEST(ParserTest, FullParserTest) { 195 Diagnostics Error; 196 llvm::Optional<DynTypedMatcher> VarDecl(Parser::parseMatcherExpression( 197 "varDecl(hasInitializer(binaryOperator(hasLHS(integerLiteral())," 198 " hasOperatorName(\"+\"))))", 199 &Error)); 200 EXPECT_EQ("", Error.toStringFull()); 201 Matcher<Decl> M = VarDecl->unconditionalConvertTo<Decl>(); 202 EXPECT_TRUE(matches("int x = 1 + false;", M)); 203 EXPECT_FALSE(matches("int x = true + 1;", M)); 204 EXPECT_FALSE(matches("int x = 1 - false;", M)); 205 EXPECT_FALSE(matches("int x = true - 1;", M)); 206 207 llvm::Optional<DynTypedMatcher> HasParameter(Parser::parseMatcherExpression( 208 "functionDecl(hasParameter(1, hasName(\"x\")))", &Error)); 209 EXPECT_EQ("", Error.toStringFull()); 210 M = HasParameter->unconditionalConvertTo<Decl>(); 211 212 EXPECT_TRUE(matches("void f(int a, int x);", M)); 213 EXPECT_FALSE(matches("void f(int x, int a);", M)); 214 215 // Test named values. 216 auto NamedValues = getTestNamedValues(); 217 llvm::Optional<DynTypedMatcher> HasParameterWithNamedValues( 218 Parser::parseMatcherExpression( 219 "functionDecl(hasParamA, hasParameter(1, hasName(nameX)))", 220 nullptr, &NamedValues, &Error)); 221 EXPECT_EQ("", Error.toStringFull()); 222 M = HasParameterWithNamedValues->unconditionalConvertTo<Decl>(); 223 224 EXPECT_TRUE(matches("void f(int a, int x);", M)); 225 EXPECT_FALSE(matches("void f(int x, int a);", M)); 226 227 228 EXPECT_TRUE(!Parser::parseMatcherExpression( 229 "hasInitializer(\n binaryOperator(hasLHS(\"A\")))", 230 &Error).hasValue()); 231 EXPECT_EQ("1:1: Error parsing argument 1 for matcher hasInitializer.\n" 232 "2:5: Error parsing argument 1 for matcher binaryOperator.\n" 233 "2:20: Error building matcher hasLHS.\n" 234 "2:27: Incorrect type for arg 1. " 235 "(Expected = Matcher<Expr>) != (Actual = String)", 236 Error.toStringFull()); 237 } 238 239 std::string ParseWithError(StringRef Code) { 240 Diagnostics Error; 241 VariantValue Value; 242 Parser::parseExpression(Code, &Value, &Error); 243 return Error.toStringFull(); 244 } 245 246 std::string ParseMatcherWithError(StringRef Code) { 247 Diagnostics Error; 248 Parser::parseMatcherExpression(Code, &Error); 249 return Error.toStringFull(); 250 } 251 252 TEST(ParserTest, Errors) { 253 EXPECT_EQ( 254 "1:5: Error parsing matcher. Found token <123> while looking for '('.", 255 ParseWithError("Foo 123")); 256 EXPECT_EQ( 257 "1:1: Matcher not found: Foo\n" 258 "1:9: Error parsing matcher. Found token <123> while looking for ','.", 259 ParseWithError("Foo(\"A\" 123)")); 260 EXPECT_EQ( 261 "1:1: Error parsing argument 1 for matcher stmt.\n" 262 "1:6: Value not found: someValue", 263 ParseWithError("stmt(someValue)")); 264 EXPECT_EQ( 265 "1:1: Matcher not found: Foo\n" 266 "1:4: Error parsing matcher. Found end-of-code while looking for ')'.", 267 ParseWithError("Foo(")); 268 EXPECT_EQ("1:1: End of code found while looking for token.", 269 ParseWithError("")); 270 EXPECT_EQ("Input value is not a matcher expression.", 271 ParseMatcherWithError("\"A\"")); 272 EXPECT_EQ("1:1: Matcher not found: Foo\n" 273 "1:1: Error parsing argument 1 for matcher Foo.\n" 274 "1:5: Invalid token <(> found when looking for a value.", 275 ParseWithError("Foo((")); 276 EXPECT_EQ("1:7: Expected end of code.", ParseWithError("expr()a")); 277 EXPECT_EQ("1:11: Malformed bind() expression.", 278 ParseWithError("isArrow().biind")); 279 EXPECT_EQ("1:15: Malformed bind() expression.", 280 ParseWithError("isArrow().bind")); 281 EXPECT_EQ("1:16: Malformed bind() expression.", 282 ParseWithError("isArrow().bind(foo")); 283 EXPECT_EQ("1:21: Malformed bind() expression.", 284 ParseWithError("isArrow().bind(\"foo\"")); 285 EXPECT_EQ("1:1: Error building matcher isArrow.\n" 286 "1:1: Matcher does not support binding.", 287 ParseWithError("isArrow().bind(\"foo\")")); 288 EXPECT_EQ("Input value has unresolved overloaded type: " 289 "Matcher<DoStmt|ForStmt|WhileStmt|CXXForRangeStmt|FunctionDecl>", 290 ParseMatcherWithError("hasBody(stmt())")); 291 } 292 293 TEST(ParserTest, OverloadErrors) { 294 EXPECT_EQ("1:1: Error building matcher callee.\n" 295 "1:8: Candidate 1: Incorrect type for arg 1. " 296 "(Expected = Matcher<Stmt>) != (Actual = String)\n" 297 "1:8: Candidate 2: Incorrect type for arg 1. " 298 "(Expected = Matcher<Decl>) != (Actual = String)", 299 ParseWithError("callee(\"A\")")); 300 } 301 302 TEST(ParserTest, CompletionRegistry) { 303 std::vector<MatcherCompletion> Comps = 304 Parser::completeExpression("while", 5); 305 ASSERT_EQ(1u, Comps.size()); 306 EXPECT_EQ("Stmt(", Comps[0].TypedText); 307 EXPECT_EQ("Matcher<Stmt> whileStmt(Matcher<WhileStmt>...)", 308 Comps[0].MatcherDecl); 309 310 Comps = Parser::completeExpression("whileStmt().", 12); 311 ASSERT_EQ(1u, Comps.size()); 312 EXPECT_EQ("bind(\"", Comps[0].TypedText); 313 EXPECT_EQ("bind", Comps[0].MatcherDecl); 314 } 315 316 TEST(ParserTest, CompletionNamedValues) { 317 // Can complete non-matcher types. 318 auto NamedValues = getTestNamedValues(); 319 StringRef Code = "functionDecl(hasName("; 320 std::vector<MatcherCompletion> Comps = 321 Parser::completeExpression(Code, Code.size(), nullptr, &NamedValues); 322 ASSERT_EQ(1u, Comps.size()); 323 EXPECT_EQ("nameX", Comps[0].TypedText); 324 EXPECT_EQ("String nameX", Comps[0].MatcherDecl); 325 326 // Can complete if there are names in the expression. 327 Code = "cxxMethodDecl(hasName(nameX), "; 328 Comps = Parser::completeExpression(Code, Code.size(), nullptr, &NamedValues); 329 EXPECT_LT(0u, Comps.size()); 330 331 // Can complete names and registry together. 332 Code = "cxxMethodDecl(hasP"; 333 Comps = Parser::completeExpression(Code, Code.size(), nullptr, &NamedValues); 334 ASSERT_EQ(3u, Comps.size()); 335 EXPECT_EQ("aramA", Comps[0].TypedText); 336 EXPECT_EQ("Matcher<FunctionDecl> hasParamA", Comps[0].MatcherDecl); 337 338 EXPECT_EQ("arameter(", Comps[1].TypedText); 339 EXPECT_EQ( 340 "Matcher<FunctionDecl> hasParameter(unsigned, Matcher<ParmVarDecl>)", 341 Comps[1].MatcherDecl); 342 343 EXPECT_EQ("arent(", Comps[2].TypedText); 344 EXPECT_EQ( 345 "Matcher<Decl> " 346 "hasParent(Matcher<NestedNameSpecifierLoc|TypeLoc|Decl|...>)", 347 Comps[2].MatcherDecl); 348 } 349 350 } // end anonymous namespace 351 } // end namespace dynamic 352 } // end namespace ast_matchers 353 } // end namespace clang 354