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(); 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_EQ(1ULL, Sema.Values.size()); 129 EXPECT_EQ(ExpectedFoo, getSingleMatcher(Sema.Values[0])->getID()); 130 131 EXPECT_EQ(3ULL, Sema.Matchers.size()); 132 const MockSema::MatcherInfo Bar = Sema.Matchers[0]; 133 EXPECT_EQ("Bar", Bar.MatcherName); 134 EXPECT_TRUE(matchesRange(Bar.NameRange, 1, 1, 8, 17)); 135 EXPECT_EQ(1ULL, Bar.Args.size()); 136 EXPECT_EQ(17U, Bar.Args[0].Value.getUnsigned()); 137 138 const MockSema::MatcherInfo Baz = Sema.Matchers[1]; 139 EXPECT_EQ("Baz", Baz.MatcherName); 140 EXPECT_TRUE(matchesRange(Baz.NameRange, 1, 2, 19, 10)); 141 EXPECT_EQ(1ULL, Baz.Args.size()); 142 EXPECT_EQ("B A,Z", Baz.Args[0].Value.getString()); 143 144 const MockSema::MatcherInfo Foo = Sema.Matchers[2]; 145 EXPECT_EQ("Foo", Foo.MatcherName); 146 EXPECT_TRUE(matchesRange(Foo.NameRange, 1, 2, 2, 12)); 147 EXPECT_EQ(2ULL, Foo.Args.size()); 148 EXPECT_EQ(ExpectedBar, getSingleMatcher(Foo.Args[0].Value)->getID()); 149 EXPECT_EQ(ExpectedBaz, getSingleMatcher(Foo.Args[1].Value)->getID()); 150 EXPECT_EQ("Yo!", Foo.BoundID); 151 } 152 153 using ast_matchers::internal::Matcher; 154 155 Parser::NamedValueMap getTestNamedValues() { 156 Parser::NamedValueMap Values; 157 Values["nameX"] = std::string("x"); 158 Values["hasParamA"] = 159 VariantMatcher::SingleMatcher(hasParameter(0, hasName("a"))); 160 return Values; 161 } 162 163 TEST(ParserTest, FullParserTest) { 164 Diagnostics Error; 165 llvm::Optional<DynTypedMatcher> VarDecl(Parser::parseMatcherExpression( 166 "varDecl(hasInitializer(binaryOperator(hasLHS(integerLiteral())," 167 " hasOperatorName(\"+\"))))", 168 &Error)); 169 EXPECT_EQ("", Error.toStringFull()); 170 Matcher<Decl> M = VarDecl->unconditionalConvertTo<Decl>(); 171 EXPECT_TRUE(matches("int x = 1 + false;", M)); 172 EXPECT_FALSE(matches("int x = true + 1;", M)); 173 EXPECT_FALSE(matches("int x = 1 - false;", M)); 174 EXPECT_FALSE(matches("int x = true - 1;", M)); 175 176 llvm::Optional<DynTypedMatcher> HasParameter(Parser::parseMatcherExpression( 177 "functionDecl(hasParameter(1, hasName(\"x\")))", &Error)); 178 EXPECT_EQ("", Error.toStringFull()); 179 M = HasParameter->unconditionalConvertTo<Decl>(); 180 181 EXPECT_TRUE(matches("void f(int a, int x);", M)); 182 EXPECT_FALSE(matches("void f(int x, int a);", M)); 183 184 // Test named values. 185 auto NamedValues = getTestNamedValues(); 186 llvm::Optional<DynTypedMatcher> HasParameterWithNamedValues( 187 Parser::parseMatcherExpression( 188 "functionDecl(hasParamA, hasParameter(1, hasName(nameX)))", 189 nullptr, &NamedValues, &Error)); 190 EXPECT_EQ("", Error.toStringFull()); 191 M = HasParameterWithNamedValues->unconditionalConvertTo<Decl>(); 192 193 EXPECT_TRUE(matches("void f(int a, int x);", M)); 194 EXPECT_FALSE(matches("void f(int x, int a);", M)); 195 196 197 EXPECT_TRUE(!Parser::parseMatcherExpression( 198 "hasInitializer(\n binaryOperator(hasLHS(\"A\")))", 199 &Error).hasValue()); 200 EXPECT_EQ("1:1: Error parsing argument 1 for matcher hasInitializer.\n" 201 "2:5: Error parsing argument 1 for matcher binaryOperator.\n" 202 "2:20: Error building matcher hasLHS.\n" 203 "2:27: Incorrect type for arg 1. " 204 "(Expected = Matcher<Expr>) != (Actual = String)", 205 Error.toStringFull()); 206 } 207 208 std::string ParseWithError(StringRef Code) { 209 Diagnostics Error; 210 VariantValue Value; 211 Parser::parseExpression(Code, &Value, &Error); 212 return Error.toStringFull(); 213 } 214 215 std::string ParseMatcherWithError(StringRef Code) { 216 Diagnostics Error; 217 Parser::parseMatcherExpression(Code, &Error); 218 return Error.toStringFull(); 219 } 220 221 TEST(ParserTest, Errors) { 222 EXPECT_EQ( 223 "1:5: Error parsing matcher. Found token <123> while looking for '('.", 224 ParseWithError("Foo 123")); 225 EXPECT_EQ( 226 "1:1: Matcher not found: Foo\n" 227 "1:9: Error parsing matcher. Found token <123> while looking for ','.", 228 ParseWithError("Foo(\"A\" 123)")); 229 EXPECT_EQ( 230 "1:1: Error parsing argument 1 for matcher stmt.\n" 231 "1:6: Value not found: someValue", 232 ParseWithError("stmt(someValue)")); 233 EXPECT_EQ( 234 "1:1: Matcher not found: Foo\n" 235 "1:4: Error parsing matcher. Found end-of-code while looking for ')'.", 236 ParseWithError("Foo(")); 237 EXPECT_EQ("1:1: End of code found while looking for token.", 238 ParseWithError("")); 239 EXPECT_EQ("Input value is not a matcher expression.", 240 ParseMatcherWithError("\"A\"")); 241 EXPECT_EQ("1:1: Matcher not found: Foo\n" 242 "1:1: Error parsing argument 1 for matcher Foo.\n" 243 "1:5: Invalid token <(> found when looking for a value.", 244 ParseWithError("Foo((")); 245 EXPECT_EQ("1:7: Expected end of code.", ParseWithError("expr()a")); 246 EXPECT_EQ("1:11: Malformed bind() expression.", 247 ParseWithError("isArrow().biind")); 248 EXPECT_EQ("1:15: Malformed bind() expression.", 249 ParseWithError("isArrow().bind")); 250 EXPECT_EQ("1:16: Malformed bind() expression.", 251 ParseWithError("isArrow().bind(foo")); 252 EXPECT_EQ("1:21: Malformed bind() expression.", 253 ParseWithError("isArrow().bind(\"foo\"")); 254 EXPECT_EQ("1:1: Error building matcher isArrow.\n" 255 "1:1: Matcher does not support binding.", 256 ParseWithError("isArrow().bind(\"foo\")")); 257 EXPECT_EQ("Input value has unresolved overloaded type: " 258 "Matcher<DoStmt|ForStmt|WhileStmt|CXXForRangeStmt>", 259 ParseMatcherWithError("hasBody(stmt())")); 260 } 261 262 TEST(ParserTest, OverloadErrors) { 263 EXPECT_EQ("1:1: Error building matcher callee.\n" 264 "1:8: Candidate 1: Incorrect type for arg 1. " 265 "(Expected = Matcher<Stmt>) != (Actual = String)\n" 266 "1:8: Candidate 2: Incorrect type for arg 1. " 267 "(Expected = Matcher<Decl>) != (Actual = String)", 268 ParseWithError("callee(\"A\")")); 269 } 270 271 TEST(ParserTest, CompletionRegistry) { 272 std::vector<MatcherCompletion> Comps = 273 Parser::completeExpression("while", 5); 274 ASSERT_EQ(1u, Comps.size()); 275 EXPECT_EQ("Stmt(", Comps[0].TypedText); 276 EXPECT_EQ("Matcher<Stmt> whileStmt(Matcher<WhileStmt>...)", 277 Comps[0].MatcherDecl); 278 279 Comps = Parser::completeExpression("whileStmt().", 12); 280 ASSERT_EQ(1u, Comps.size()); 281 EXPECT_EQ("bind(\"", Comps[0].TypedText); 282 EXPECT_EQ("bind", Comps[0].MatcherDecl); 283 } 284 285 TEST(ParserTest, CompletionNamedValues) { 286 // Can complete non-matcher types. 287 auto NamedValues = getTestNamedValues(); 288 StringRef Code = "functionDecl(hasName("; 289 std::vector<MatcherCompletion> Comps = 290 Parser::completeExpression(Code, Code.size(), nullptr, &NamedValues); 291 ASSERT_EQ(1u, Comps.size()); 292 EXPECT_EQ("nameX", Comps[0].TypedText); 293 EXPECT_EQ("String nameX", Comps[0].MatcherDecl); 294 295 // Can complete if there are names in the expression. 296 Code = "methodDecl(hasName(nameX), "; 297 Comps = Parser::completeExpression(Code, Code.size(), nullptr, &NamedValues); 298 EXPECT_LT(0u, Comps.size()); 299 300 // Can complete names and registry together. 301 Code = "methodDecl(hasP"; 302 Comps = Parser::completeExpression(Code, Code.size(), nullptr, &NamedValues); 303 ASSERT_EQ(3u, Comps.size()); 304 EXPECT_EQ("aramA", Comps[0].TypedText); 305 EXPECT_EQ("Matcher<FunctionDecl> hasParamA", Comps[0].MatcherDecl); 306 307 EXPECT_EQ("arameter(", Comps[1].TypedText); 308 EXPECT_EQ( 309 "Matcher<FunctionDecl> hasParameter(unsigned, Matcher<ParmVarDecl>)", 310 Comps[1].MatcherDecl); 311 312 EXPECT_EQ("arent(", Comps[2].TypedText); 313 EXPECT_EQ("Matcher<Decl> hasParent(Matcher<Decl|Stmt>)", 314 Comps[2].MatcherDecl); 315 } 316 317 } // end anonymous namespace 318 } // end namespace dynamic 319 } // end namespace ast_matchers 320 } // end namespace clang 321