Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame^] | 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 <string> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "../ASTMatchersTest.h" |
| 14 | #include "clang/ASTMatchers/Dynamic/Parser.h" |
| 15 | #include "clang/ASTMatchers/Dynamic/Registry.h" |
| 16 | #include "gtest/gtest.h" |
| 17 | #include "llvm/ADT/StringMap.h" |
| 18 | |
| 19 | namespace clang { |
| 20 | namespace ast_matchers { |
| 21 | namespace dynamic { |
| 22 | namespace { |
| 23 | |
| 24 | class DummyDynTypedMatcher : public DynTypedMatcher { |
| 25 | public: |
| 26 | DummyDynTypedMatcher(uint64_t ID) : ID(ID) {} |
| 27 | |
| 28 | typedef ast_matchers::internal::ASTMatchFinder ASTMatchFinder; |
| 29 | typedef ast_matchers::internal::BoundNodesTreeBuilder BoundNodesTreeBuilder; |
| 30 | virtual bool matches(const ast_type_traits::DynTypedNode DynNode, |
| 31 | ASTMatchFinder *Finder, |
| 32 | BoundNodesTreeBuilder *Builder) const { |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | /// \brief Makes a copy of this matcher object. |
| 37 | virtual DynTypedMatcher *clone() const { |
| 38 | return new DummyDynTypedMatcher(ID); |
| 39 | } |
| 40 | |
| 41 | /// \brief Returns a unique ID for the matcher. |
| 42 | virtual uint64_t getID() const { return ID; } |
| 43 | |
| 44 | private: |
| 45 | uint64_t ID; |
| 46 | }; |
| 47 | |
| 48 | class MockSema : public Parser::Sema { |
| 49 | public: |
| 50 | virtual ~MockSema() {} |
| 51 | |
| 52 | uint64_t expectMatcher(StringRef MatcherName) { |
| 53 | uint64_t ID = ExpectedMatchers.size() + 1; |
| 54 | ExpectedMatchers[MatcherName] = ID; |
| 55 | return ID; |
| 56 | } |
| 57 | |
| 58 | void parse(StringRef Code) { |
| 59 | Diagnostics Error; |
| 60 | VariantValue Value; |
| 61 | Parser::parseExpression(Code, this, &Value, &Error); |
| 62 | Values.push_back(Value); |
| 63 | Errors.push_back(Error.ToStringFull()); |
| 64 | } |
| 65 | |
| 66 | DynTypedMatcher *actOnMatcherExpression(StringRef MatcherName, |
| 67 | const SourceRange &NameRange, |
| 68 | ArrayRef<ParserValue> Args, |
| 69 | Diagnostics *Error) { |
| 70 | MatcherInfo ToStore = { MatcherName, NameRange, Args }; |
| 71 | Matchers.push_back(ToStore); |
| 72 | return new DummyDynTypedMatcher(ExpectedMatchers[MatcherName]); |
| 73 | } |
| 74 | |
| 75 | struct MatcherInfo { |
| 76 | StringRef MatcherName; |
| 77 | SourceRange NameRange; |
| 78 | std::vector<ParserValue> Args; |
| 79 | }; |
| 80 | |
| 81 | std::vector<std::string> Errors; |
| 82 | std::vector<VariantValue> Values; |
| 83 | std::vector<MatcherInfo> Matchers; |
| 84 | llvm::StringMap<uint64_t> ExpectedMatchers; |
| 85 | }; |
| 86 | |
| 87 | TEST(ParserTest, ParseString) { |
| 88 | MockSema Sema; |
| 89 | Sema.parse("\"Foo\""); |
| 90 | Sema.parse("\"\""); |
| 91 | Sema.parse("\"Baz"); |
| 92 | EXPECT_EQ(3ULL, Sema.Values.size()); |
| 93 | EXPECT_EQ("Foo", Sema.Values[0].getString()); |
| 94 | EXPECT_EQ("", Sema.Values[1].getString()); |
| 95 | EXPECT_EQ("1:1: Error parsing string token: <\"Baz>", Sema.Errors[2]); |
| 96 | } |
| 97 | |
| 98 | bool matchesRange(const SourceRange &Range, unsigned StartLine, |
| 99 | unsigned EndLine, unsigned StartColumn, unsigned EndColumn) { |
| 100 | EXPECT_EQ(StartLine, Range.Start.Line); |
| 101 | EXPECT_EQ(EndLine, Range.End.Line); |
| 102 | EXPECT_EQ(StartColumn, Range.Start.Column); |
| 103 | EXPECT_EQ(EndColumn, Range.End.Column); |
| 104 | return Range.Start.Line == StartLine && Range.End.Line == EndLine && |
| 105 | Range.Start.Column == StartColumn && Range.End.Column == EndColumn; |
| 106 | } |
| 107 | |
| 108 | TEST(ParserTest, ParseMatcher) { |
| 109 | MockSema Sema; |
| 110 | const uint64_t ExpectedFoo = Sema.expectMatcher("Foo"); |
| 111 | const uint64_t ExpectedBar = Sema.expectMatcher("Bar"); |
| 112 | const uint64_t ExpectedBaz = Sema.expectMatcher("Baz"); |
| 113 | Sema.parse(" Foo ( Bar (), Baz( \n \"B A,Z\") ) "); |
| 114 | for (size_t i = 0, e = Sema.Errors.size(); i != e; ++i) { |
| 115 | EXPECT_EQ("", Sema.Errors[i]); |
| 116 | } |
| 117 | |
| 118 | EXPECT_EQ(1ULL, Sema.Values.size()); |
| 119 | EXPECT_EQ(ExpectedFoo, Sema.Values[0].getMatcher().getID()); |
| 120 | |
| 121 | EXPECT_EQ(3ULL, Sema.Matchers.size()); |
| 122 | const MockSema::MatcherInfo Bar = Sema.Matchers[0]; |
| 123 | EXPECT_EQ("Bar", Bar.MatcherName); |
| 124 | EXPECT_TRUE(matchesRange(Bar.NameRange, 1, 1, 8, 14)); |
| 125 | EXPECT_EQ(0ULL, Bar.Args.size()); |
| 126 | |
| 127 | const MockSema::MatcherInfo Baz = Sema.Matchers[1]; |
| 128 | EXPECT_EQ("Baz", Baz.MatcherName); |
| 129 | EXPECT_TRUE(matchesRange(Baz.NameRange, 1, 2, 16, 10)); |
| 130 | EXPECT_EQ(1ULL, Baz.Args.size()); |
| 131 | EXPECT_EQ("B A,Z", Baz.Args[0].Value.getString()); |
| 132 | |
| 133 | const MockSema::MatcherInfo Foo = Sema.Matchers[2]; |
| 134 | EXPECT_EQ("Foo", Foo.MatcherName); |
| 135 | EXPECT_TRUE(matchesRange(Foo.NameRange, 1, 2, 2, 12)); |
| 136 | EXPECT_EQ(2ULL, Foo.Args.size()); |
| 137 | EXPECT_EQ(ExpectedBar, Foo.Args[0].Value.getMatcher().getID()); |
| 138 | EXPECT_EQ(ExpectedBaz, Foo.Args[1].Value.getMatcher().getID()); |
| 139 | } |
| 140 | |
| 141 | using ast_matchers::internal::Matcher; |
| 142 | |
| 143 | TEST(ParserTest, FullParserTest) { |
| 144 | OwningPtr<DynTypedMatcher> Matcher(Parser::parseMatcherExpression( |
| 145 | "hasInitializer(binaryOperator(hasLHS(integerLiteral())))", NULL)); |
| 146 | EXPECT_TRUE(matchesDynamic("int x = 1 + false;", *Matcher)); |
| 147 | EXPECT_FALSE(matchesDynamic("int x = true + 1;", *Matcher)); |
| 148 | |
| 149 | Diagnostics Error; |
| 150 | EXPECT_TRUE(Parser::parseMatcherExpression( |
| 151 | "hasInitializer(\n binaryOperator(hasLHS(\"A\")))", &Error) == NULL); |
| 152 | EXPECT_EQ("1:1: Error parsing argument 1 for matcher hasInitializer.\n" |
| 153 | "2:5: Error parsing argument 1 for matcher binaryOperator.\n" |
| 154 | "2:20: Error building matcher hasLHS.\n" |
| 155 | "2:27: Incorrect type on function hasLHS for arg 1.", |
| 156 | Error.ToStringFull()); |
| 157 | } |
| 158 | |
| 159 | std::string ParseWithError(StringRef Code) { |
| 160 | Diagnostics Error; |
| 161 | VariantValue Value; |
| 162 | Parser::parseExpression(Code, &Value, &Error); |
| 163 | return Error.ToStringFull(); |
| 164 | } |
| 165 | |
| 166 | std::string ParseMatcherWithError(StringRef Code) { |
| 167 | Diagnostics Error; |
| 168 | Parser::parseMatcherExpression(Code, &Error); |
| 169 | return Error.ToStringFull(); |
| 170 | } |
| 171 | |
| 172 | TEST(ParserTest, Errors) { |
| 173 | EXPECT_EQ( |
| 174 | "1:5: Error parsing matcher. Found token <123> while looking for '('.", |
| 175 | ParseWithError("Foo 123")); |
| 176 | EXPECT_EQ( |
| 177 | "1:9: Error parsing matcher. Found token <123> while looking for ','.", |
| 178 | ParseWithError("Foo(\"A\" 123)")); |
| 179 | EXPECT_EQ( |
| 180 | "1:4: Error parsing matcher. Found end-of-code while looking for ')'.", |
| 181 | ParseWithError("Foo(")); |
| 182 | EXPECT_EQ("1:1: End of code found while looking for token.", |
| 183 | ParseWithError("")); |
| 184 | EXPECT_EQ("Input value is not a matcher expression.", |
| 185 | ParseMatcherWithError("\"A\"")); |
| 186 | EXPECT_EQ("1:1: Error parsing argument 1 for matcher Foo.\n" |
| 187 | "1:5: Invalid token <(> found when looking for a value.", |
| 188 | ParseWithError("Foo((")); |
| 189 | } |
| 190 | |
| 191 | } // end anonymous namespace |
| 192 | } // end namespace dynamic |
| 193 | } // end namespace ast_matchers |
| 194 | } // end namespace clang |