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 "llvm/ADT/StringMap.h" 15 #include "gtest/gtest.h" 16 #include <string> 17 #include <vector> 18 19 namespace clang { 20 namespace ast_matchers { 21 namespace dynamic { 22 namespace { 23 24 class MockSema : public Parser::Sema { 25 public: 26 virtual ~MockSema() {} 27 28 uint64_t expectMatcher(StringRef MatcherName) { 29 ast_matchers::internal::Matcher<Stmt> M = stmt(); 30 ExpectedMatchers.insert(std::make_pair(MatcherName, M)); 31 return M.getID().second; 32 } 33 34 void parse(StringRef Code) { 35 Diagnostics Error; 36 VariantValue Value; 37 Parser::parseExpression(Code, this, &Value, &Error); 38 Values.push_back(Value); 39 Errors.push_back(Error.toStringFull()); 40 } 41 42 llvm::Optional<MatcherCtor> lookupMatcherCtor(StringRef MatcherName) { 43 const ExpectedMatchersTy::value_type *Matcher = 44 &*ExpectedMatchers.find(MatcherName); 45 return reinterpret_cast<MatcherCtor>(Matcher); 46 } 47 48 VariantMatcher actOnMatcherExpression(MatcherCtor Ctor, 49 const SourceRange &NameRange, 50 StringRef BindID, 51 ArrayRef<ParserValue> Args, 52 Diagnostics *Error) { 53 const ExpectedMatchersTy::value_type *Matcher = 54 reinterpret_cast<const ExpectedMatchersTy::value_type *>(Ctor); 55 MatcherInfo ToStore = { Matcher->first, NameRange, Args, BindID }; 56 Matchers.push_back(ToStore); 57 return VariantMatcher::SingleMatcher(Matcher->second); 58 } 59 60 struct MatcherInfo { 61 StringRef MatcherName; 62 SourceRange NameRange; 63 std::vector<ParserValue> Args; 64 std::string BoundID; 65 }; 66 67 std::vector<std::string> Errors; 68 std::vector<VariantValue> Values; 69 std::vector<MatcherInfo> Matchers; 70 typedef std::map<std::string, ast_matchers::internal::Matcher<Stmt> > 71 ExpectedMatchersTy; 72 ExpectedMatchersTy ExpectedMatchers; 73 }; 74 75 TEST(ParserTest, ParseUnsigned) { 76 MockSema Sema; 77 Sema.parse("0"); 78 Sema.parse("123"); 79 Sema.parse("0x1f"); 80 Sema.parse("12345678901"); 81 Sema.parse("1a1"); 82 EXPECT_EQ(5U, Sema.Values.size()); 83 EXPECT_EQ(0U, Sema.Values[0].getUnsigned()); 84 EXPECT_EQ(123U, Sema.Values[1].getUnsigned()); 85 EXPECT_EQ(31U, Sema.Values[2].getUnsigned()); 86 EXPECT_EQ("1:1: Error parsing unsigned token: <12345678901>", Sema.Errors[3]); 87 EXPECT_EQ("1:1: Error parsing unsigned token: <1a1>", Sema.Errors[4]); 88 } 89 90 TEST(ParserTest, ParseString) { 91 MockSema Sema; 92 Sema.parse("\"Foo\""); 93 Sema.parse("\"\""); 94 Sema.parse("\"Baz"); 95 EXPECT_EQ(3ULL, Sema.Values.size()); 96 EXPECT_EQ("Foo", Sema.Values[0].getString()); 97 EXPECT_EQ("", Sema.Values[1].getString()); 98 EXPECT_EQ("1:1: Error parsing string token: <\"Baz>", Sema.Errors[2]); 99 } 100 101 bool matchesRange(const SourceRange &Range, unsigned StartLine, 102 unsigned EndLine, unsigned StartColumn, unsigned EndColumn) { 103 EXPECT_EQ(StartLine, Range.Start.Line); 104 EXPECT_EQ(EndLine, Range.End.Line); 105 EXPECT_EQ(StartColumn, Range.Start.Column); 106 EXPECT_EQ(EndColumn, Range.End.Column); 107 return Range.Start.Line == StartLine && Range.End.Line == EndLine && 108 Range.Start.Column == StartColumn && Range.End.Column == EndColumn; 109 } 110 111 llvm::Optional<DynTypedMatcher> getSingleMatcher(const VariantValue &Value) { 112 llvm::Optional<DynTypedMatcher> Result = 113 Value.getMatcher().getSingleMatcher(); 114 EXPECT_TRUE(Result.hasValue()); 115 return Result; 116 } 117 118 TEST(ParserTest, ParseMatcher) { 119 MockSema Sema; 120 const uint64_t ExpectedFoo = Sema.expectMatcher("Foo"); 121 const uint64_t ExpectedBar = Sema.expectMatcher("Bar"); 122 const uint64_t ExpectedBaz = Sema.expectMatcher("Baz"); 123 Sema.parse(" Foo ( Bar ( 17), Baz( \n \"B A,Z\") ) .bind( \"Yo!\") "); 124 for (size_t i = 0, e = Sema.Errors.size(); i != e; ++i) { 125 EXPECT_EQ("", Sema.Errors[i]); 126 } 127 128 EXPECT_NE(ExpectedFoo, ExpectedBar); 129 EXPECT_NE(ExpectedFoo, ExpectedBaz); 130 EXPECT_NE(ExpectedBar, ExpectedBaz); 131 132 EXPECT_EQ(1ULL, Sema.Values.size()); 133 EXPECT_EQ(ExpectedFoo, getSingleMatcher(Sema.Values[0])->getID().second); 134 135 EXPECT_EQ(3ULL, Sema.Matchers.size()); 136 const MockSema::MatcherInfo Bar = Sema.Matchers[0]; 137 EXPECT_EQ("Bar", Bar.MatcherName); 138 EXPECT_TRUE(matchesRange(Bar.NameRange, 1, 1, 8, 17)); 139 EXPECT_EQ(1ULL, Bar.Args.size()); 140 EXPECT_EQ(17U, Bar.Args[0].Value.getUnsigned()); 141 142 const MockSema::MatcherInfo Baz = Sema.Matchers[1]; 143 EXPECT_EQ("Baz", Baz.MatcherName); 144 EXPECT_TRUE(matchesRange(Baz.NameRange, 1, 2, 19, 10)); 145 EXPECT_EQ(1ULL, Baz.Args.size()); 146 EXPECT_EQ("B A,Z", Baz.Args[0].Value.getString()); 147 148 const MockSema::MatcherInfo Foo = Sema.Matchers[2]; 149 EXPECT_EQ("Foo", Foo.MatcherName); 150 EXPECT_TRUE(matchesRange(Foo.NameRange, 1, 2, 2, 12)); 151 EXPECT_EQ(2ULL, Foo.Args.size()); 152 EXPECT_EQ(ExpectedBar, getSingleMatcher(Foo.Args[0].Value)->getID().second); 153 EXPECT_EQ(ExpectedBaz, getSingleMatcher(Foo.Args[1].Value)->getID().second); 154 EXPECT_EQ("Yo!", Foo.BoundID); 155 } 156 157 using ast_matchers::internal::Matcher; 158 159 Parser::NamedValueMap getTestNamedValues() { 160 Parser::NamedValueMap Values; 161 Values["nameX"] = std::string("x"); 162 Values["hasParamA"] = 163 VariantMatcher::SingleMatcher(hasParameter(0, hasName("a"))); 164 return Values; 165 } 166 167 TEST(ParserTest, FullParserTest) { 168 Diagnostics Error; 169 llvm::Optional<DynTypedMatcher> VarDecl(Parser::parseMatcherExpression( 170 "varDecl(hasInitializer(binaryOperator(hasLHS(integerLiteral())," 171 " hasOperatorName(\"+\"))))", 172 &Error)); 173 EXPECT_EQ("", Error.toStringFull()); 174 Matcher<Decl> M = VarDecl->unconditionalConvertTo<Decl>(); 175 EXPECT_TRUE(matches("int x = 1 + false;", M)); 176 EXPECT_FALSE(matches("int x = true + 1;", M)); 177 EXPECT_FALSE(matches("int x = 1 - false;", M)); 178 EXPECT_FALSE(matches("int x = true - 1;", M)); 179 180 llvm::Optional<DynTypedMatcher> HasParameter(Parser::parseMatcherExpression( 181 "functionDecl(hasParameter(1, hasName(\"x\")))", &Error)); 182 EXPECT_EQ("", Error.toStringFull()); 183 M = HasParameter->unconditionalConvertTo<Decl>(); 184 185 EXPECT_TRUE(matches("void f(int a, int x);", M)); 186 EXPECT_FALSE(matches("void f(int x, int a);", M)); 187 188 // Test named values. 189 auto NamedValues = getTestNamedValues(); 190 llvm::Optional<DynTypedMatcher> HasParameterWithNamedValues( 191 Parser::parseMatcherExpression( 192 "functionDecl(hasParamA, hasParameter(1, hasName(nameX)))", 193 nullptr, &NamedValues, &Error)); 194 EXPECT_EQ("", Error.toStringFull()); 195 M = HasParameterWithNamedValues->unconditionalConvertTo<Decl>(); 196 197 EXPECT_TRUE(matches("void f(int a, int x);", M)); 198 EXPECT_FALSE(matches("void f(int x, int a);", M)); 199 200 201 EXPECT_TRUE(!Parser::parseMatcherExpression( 202 "hasInitializer(\n binaryOperator(hasLHS(\"A\")))", 203 &Error).hasValue()); 204 EXPECT_EQ("1:1: Error parsing argument 1 for matcher hasInitializer.\n" 205 "2:5: Error parsing argument 1 for matcher binaryOperator.\n" 206 "2:20: Error building matcher hasLHS.\n" 207 "2:27: Incorrect type for arg 1. " 208 "(Expected = Matcher<Expr>) != (Actual = String)", 209 Error.toStringFull()); 210 } 211 212 std::string ParseWithError(StringRef Code) { 213 Diagnostics Error; 214 VariantValue Value; 215 Parser::parseExpression(Code, &Value, &Error); 216 return Error.toStringFull(); 217 } 218 219 std::string ParseMatcherWithError(StringRef Code) { 220 Diagnostics Error; 221 Parser::parseMatcherExpression(Code, &Error); 222 return Error.toStringFull(); 223 } 224 225 TEST(ParserTest, Errors) { 226 EXPECT_EQ( 227 "1:5: Error parsing matcher. Found token <123> while looking for '('.", 228 ParseWithError("Foo 123")); 229 EXPECT_EQ( 230 "1:1: Matcher not found: Foo\n" 231 "1:9: Error parsing matcher. Found token <123> while looking for ','.", 232 ParseWithError("Foo(\"A\" 123)")); 233 EXPECT_EQ( 234 "1:1: Error parsing argument 1 for matcher stmt.\n" 235 "1:6: Value not found: someValue", 236 ParseWithError("stmt(someValue)")); 237 EXPECT_EQ( 238 "1:1: Matcher not found: Foo\n" 239 "1:4: Error parsing matcher. Found end-of-code while looking for ')'.", 240 ParseWithError("Foo(")); 241 EXPECT_EQ("1:1: End of code found while looking for token.", 242 ParseWithError("")); 243 EXPECT_EQ("Input value is not a matcher expression.", 244 ParseMatcherWithError("\"A\"")); 245 EXPECT_EQ("1:1: Matcher not found: Foo\n" 246 "1:1: Error parsing argument 1 for matcher Foo.\n" 247 "1:5: Invalid token <(> found when looking for a value.", 248 ParseWithError("Foo((")); 249 EXPECT_EQ("1:7: Expected end of code.", ParseWithError("expr()a")); 250 EXPECT_EQ("1:11: Malformed bind() expression.", 251 ParseWithError("isArrow().biind")); 252 EXPECT_EQ("1:15: Malformed bind() expression.", 253 ParseWithError("isArrow().bind")); 254 EXPECT_EQ("1:16: Malformed bind() expression.", 255 ParseWithError("isArrow().bind(foo")); 256 EXPECT_EQ("1:21: Malformed bind() expression.", 257 ParseWithError("isArrow().bind(\"foo\"")); 258 EXPECT_EQ("1:1: Error building matcher isArrow.\n" 259 "1:1: Matcher does not support binding.", 260 ParseWithError("isArrow().bind(\"foo\")")); 261 EXPECT_EQ("Input value has unresolved overloaded type: " 262 "Matcher<DoStmt|ForStmt|WhileStmt|CXXForRangeStmt>", 263 ParseMatcherWithError("hasBody(stmt())")); 264 } 265 266 TEST(ParserTest, OverloadErrors) { 267 EXPECT_EQ("1:1: Error building matcher callee.\n" 268 "1:8: Candidate 1: Incorrect type for arg 1. " 269 "(Expected = Matcher<Stmt>) != (Actual = String)\n" 270 "1:8: Candidate 2: Incorrect type for arg 1. " 271 "(Expected = Matcher<Decl>) != (Actual = String)", 272 ParseWithError("callee(\"A\")")); 273 } 274 275 TEST(ParserTest, CompletionRegistry) { 276 std::vector<MatcherCompletion> Comps = 277 Parser::completeExpression("while", 5); 278 ASSERT_EQ(1u, Comps.size()); 279 EXPECT_EQ("Stmt(", Comps[0].TypedText); 280 EXPECT_EQ("Matcher<Stmt> whileStmt(Matcher<WhileStmt>...)", 281 Comps[0].MatcherDecl); 282 283 Comps = Parser::completeExpression("whileStmt().", 12); 284 ASSERT_EQ(1u, Comps.size()); 285 EXPECT_EQ("bind(\"", Comps[0].TypedText); 286 EXPECT_EQ("bind", Comps[0].MatcherDecl); 287 } 288 289 TEST(ParserTest, CompletionNamedValues) { 290 // Can complete non-matcher types. 291 auto NamedValues = getTestNamedValues(); 292 StringRef Code = "functionDecl(hasName("; 293 std::vector<MatcherCompletion> Comps = 294 Parser::completeExpression(Code, Code.size(), nullptr, &NamedValues); 295 ASSERT_EQ(1u, Comps.size()); 296 EXPECT_EQ("nameX", Comps[0].TypedText); 297 EXPECT_EQ("String nameX", Comps[0].MatcherDecl); 298 299 // Can complete if there are names in the expression. 300 Code = "methodDecl(hasName(nameX), "; 301 Comps = Parser::completeExpression(Code, Code.size(), nullptr, &NamedValues); 302 EXPECT_LT(0u, Comps.size()); 303 304 // Can complete names and registry together. 305 Code = "methodDecl(hasP"; 306 Comps = Parser::completeExpression(Code, Code.size(), nullptr, &NamedValues); 307 ASSERT_EQ(3u, Comps.size()); 308 EXPECT_EQ("aramA", Comps[0].TypedText); 309 EXPECT_EQ("Matcher<FunctionDecl> hasParamA", Comps[0].MatcherDecl); 310 311 EXPECT_EQ("arameter(", Comps[1].TypedText); 312 EXPECT_EQ( 313 "Matcher<FunctionDecl> hasParameter(unsigned, Matcher<ParmVarDecl>)", 314 Comps[1].MatcherDecl); 315 316 EXPECT_EQ("arent(", Comps[2].TypedText); 317 EXPECT_EQ("Matcher<Decl> hasParent(Matcher<Decl|Stmt>)", 318 Comps[2].MatcherDecl); 319 } 320 321 } // end anonymous namespace 322 } // end namespace dynamic 323 } // end namespace ast_matchers 324 } // end namespace clang 325