diff options
author | Samuel Benzaquen <sbenza@google.com> | 2013-08-13 14:54:51 +0000 |
---|---|---|
committer | Samuel Benzaquen <sbenza@google.com> | 2013-08-13 14:54:51 +0000 |
commit | 0239b691674cdcfdba1e4f821cd7e31177032fac (patch) | |
tree | 9ffbc0255bb9c4d7b05d78212cc5a37919589523 /clang/unittests | |
parent | 68443b0a1487c6f7e583de387fb76a778f4ececf (diff) | |
download | bcm5719-llvm-0239b691674cdcfdba1e4f821cd7e31177032fac.tar.gz bcm5719-llvm-0239b691674cdcfdba1e4f821cd7e31177032fac.zip |
Refactor "MatcherList" into "VariantMatcher" and abstract the notion of a list of matchers for the polymorphic case.
Summary:
Refactor "MatcherList" into "VariantMatcher" and abstract the notion of a list of matchers for the polymorphic case.
This work is to support future changes needed for eachOf/allOf/anyOf matchers. We will add a new type on VariantMatcher.
Reviewers: klimek
CC: cfe-commits, revane
Differential Revision: http://llvm-reviews.chandlerc.com/D1365
llvm-svn: 188272
Diffstat (limited to 'clang/unittests')
-rw-r--r-- | clang/unittests/ASTMatchers/Dynamic/ParserTest.cpp | 28 | ||||
-rw-r--r-- | clang/unittests/ASTMatchers/Dynamic/RegistryTest.cpp | 59 | ||||
-rw-r--r-- | clang/unittests/ASTMatchers/Dynamic/VariantValueTest.cpp | 53 |
3 files changed, 80 insertions, 60 deletions
diff --git a/clang/unittests/ASTMatchers/Dynamic/ParserTest.cpp b/clang/unittests/ASTMatchers/Dynamic/ParserTest.cpp index d9b2d3c87f4..71b0f87e027 100644 --- a/clang/unittests/ASTMatchers/Dynamic/ParserTest.cpp +++ b/clang/unittests/ASTMatchers/Dynamic/ParserTest.cpp @@ -76,16 +76,16 @@ public: Errors.push_back(Error.toStringFull()); } - MatcherList actOnMatcherExpression(StringRef MatcherName, - const SourceRange &NameRange, - StringRef BindID, - ArrayRef<ParserValue> Args, - Diagnostics *Error) { + VariantMatcher actOnMatcherExpression(StringRef MatcherName, + const SourceRange &NameRange, + StringRef BindID, + ArrayRef<ParserValue> Args, + Diagnostics *Error) { MatcherInfo ToStore = { MatcherName, NameRange, Args, BindID }; Matchers.push_back(ToStore); DummyDynTypedMatcher Matcher(ExpectedMatchers[MatcherName]); OwningPtr<DynTypedMatcher> Out(Matcher.tryBind(BindID)); - return *Out; + return VariantMatcher::SingleMatcher(*Out); } struct MatcherInfo { @@ -137,6 +137,12 @@ bool matchesRange(const SourceRange &Range, unsigned StartLine, Range.Start.Column == StartColumn && Range.End.Column == EndColumn; } +const DynTypedMatcher *getSingleMatcher(const VariantValue &value) { + const DynTypedMatcher *Out; + EXPECT_TRUE(value.getMatcher().getSingleMatcher(Out)); + return Out; +} + TEST(ParserTest, ParseMatcher) { MockSema Sema; const uint64_t ExpectedFoo = Sema.expectMatcher("Foo"); @@ -148,9 +154,9 @@ TEST(ParserTest, ParseMatcher) { } EXPECT_EQ(1ULL, Sema.Values.size()); - EXPECT_EQ(ExpectedFoo, Sema.Values[0].getMatchers().matchers()[0]->getID()); + EXPECT_EQ(ExpectedFoo, getSingleMatcher(Sema.Values[0])->getID()); EXPECT_EQ("Yo!", static_cast<const DummyDynTypedMatcher *>( - Sema.Values[0].getMatchers().matchers()[0])->boundID()); + getSingleMatcher(Sema.Values[0]))->boundID()); EXPECT_EQ(3ULL, Sema.Matchers.size()); const MockSema::MatcherInfo Bar = Sema.Matchers[0]; @@ -169,10 +175,8 @@ TEST(ParserTest, ParseMatcher) { EXPECT_EQ("Foo", Foo.MatcherName); EXPECT_TRUE(matchesRange(Foo.NameRange, 1, 2, 2, 12)); EXPECT_EQ(2ULL, Foo.Args.size()); - EXPECT_EQ(ExpectedBar, - Foo.Args[0].Value.getMatchers().matchers()[0]->getID()); - EXPECT_EQ(ExpectedBaz, - Foo.Args[1].Value.getMatchers().matchers()[0]->getID()); + EXPECT_EQ(ExpectedBar, getSingleMatcher(Foo.Args[0].Value)->getID()); + EXPECT_EQ(ExpectedBaz, getSingleMatcher(Foo.Args[1].Value)->getID()); EXPECT_EQ("Yo!", Foo.BoundID); } diff --git a/clang/unittests/ASTMatchers/Dynamic/RegistryTest.cpp b/clang/unittests/ASTMatchers/Dynamic/RegistryTest.cpp index 7f49b6a4ac7..55490a5bab9 100644 --- a/clang/unittests/ASTMatchers/Dynamic/RegistryTest.cpp +++ b/clang/unittests/ASTMatchers/Dynamic/RegistryTest.cpp @@ -36,32 +36,34 @@ public: return Out; } - MatcherList constructMatcher(StringRef MatcherName, - Diagnostics *Error = NULL) { + VariantMatcher constructMatcher(StringRef MatcherName, + Diagnostics *Error = NULL) { Diagnostics DummyError; if (!Error) Error = &DummyError; - const MatcherList Out = + const VariantMatcher Out = Registry::constructMatcher(MatcherName, SourceRange(), Args(), Error); EXPECT_EQ("", DummyError.toStringFull()); return Out; } - MatcherList constructMatcher(StringRef MatcherName, const VariantValue &Arg1, - Diagnostics *Error = NULL) { + VariantMatcher constructMatcher(StringRef MatcherName, + const VariantValue &Arg1, + Diagnostics *Error = NULL) { Diagnostics DummyError; if (!Error) Error = &DummyError; - const MatcherList Out = Registry::constructMatcher( + const VariantMatcher Out = Registry::constructMatcher( MatcherName, SourceRange(), Args(Arg1), Error); EXPECT_EQ("", DummyError.toStringFull()); return Out; } - MatcherList constructMatcher(StringRef MatcherName, const VariantValue &Arg1, - const VariantValue &Arg2, - Diagnostics *Error = NULL) { + VariantMatcher constructMatcher(StringRef MatcherName, + const VariantValue &Arg1, + const VariantValue &Arg2, + Diagnostics *Error = NULL) { Diagnostics DummyError; if (!Error) Error = &DummyError; - const MatcherList Out = Registry::constructMatcher( + const VariantMatcher Out = Registry::constructMatcher( MatcherName, SourceRange(), Args(Arg1, Arg2), Error); EXPECT_EQ("", DummyError.toStringFull()); return Out; @@ -99,11 +101,12 @@ TEST_F(RegistryTest, ConstructWithSimpleArgs) { } TEST_F(RegistryTest, ConstructWithMatcherArgs) { - Matcher<Decl> HasInitializerSimple = - constructMatcher("varDecl", constructMatcher("hasInitializer", stmt())) - .getTypedMatcher<Decl>(); + Matcher<Decl> HasInitializerSimple = constructMatcher( + "varDecl", constructMatcher("hasInitializer", constructMatcher("stmt"))) + .getTypedMatcher<Decl>(); Matcher<Decl> HasInitializerComplex = constructMatcher( - "varDecl", constructMatcher("hasInitializer", callExpr())) + "varDecl", + constructMatcher("hasInitializer", constructMatcher("callExpr"))) .getTypedMatcher<Decl>(); std::string code = "int i;"; @@ -118,8 +121,10 @@ TEST_F(RegistryTest, ConstructWithMatcherArgs) { EXPECT_TRUE(matches(code, HasInitializerSimple)); EXPECT_TRUE(matches(code, HasInitializerComplex)); - Matcher<Decl> HasParameter = functionDecl(constructMatcher( - "hasParameter", 1, hasName("x")).getTypedMatcher<FunctionDecl>()); + Matcher<Decl> HasParameter = + functionDecl(constructMatcher( + "hasParameter", 1, constructMatcher("hasName", std::string("x"))) + .getTypedMatcher<FunctionDecl>()); EXPECT_TRUE(matches("void f(int a, int x);", HasParameter)); EXPECT_FALSE(matches("void f(int x, int a);", HasParameter)); } @@ -149,7 +154,7 @@ TEST_F(RegistryTest, OverloadedMatchers) { } TEST_F(RegistryTest, PolymorphicMatchers) { - const MatcherList IsDefinition = constructMatcher("isDefinition"); + const VariantMatcher IsDefinition = constructMatcher("isDefinition"); Matcher<Decl> Var = constructMatcher("varDecl", IsDefinition).getTypedMatcher<Decl>(); Matcher<Decl> Class = @@ -165,7 +170,8 @@ TEST_F(RegistryTest, PolymorphicMatchers) { Matcher<Decl> Anything = constructMatcher("anything").getTypedMatcher<Decl>(); Matcher<Decl> RecordDecl = - constructMatcher("recordDecl", Anything).getTypedMatcher<Decl>(); + constructMatcher("recordDecl", VariantMatcher::SingleMatcher(Anything)) + .getTypedMatcher<Decl>(); EXPECT_TRUE(matches("int a;", Anything)); EXPECT_TRUE(matches("class A {};", Anything)); @@ -214,8 +220,10 @@ TEST_F(RegistryTest, TypeTraversal) { TEST_F(RegistryTest, CXXCtorInitializer) { Matcher<Decl> CtorDecl = constructMatcher( "constructorDecl", - constructMatcher("hasAnyConstructorInitializer", - constructMatcher("forField", hasName("foo")))) + constructMatcher( + "hasAnyConstructorInitializer", + constructMatcher("forField", + constructMatcher("hasName", std::string("foo"))))) .getTypedMatcher<Decl>(); EXPECT_TRUE(matches("struct Foo { Foo() : foo(1) {} int foo; };", CtorDecl)); EXPECT_FALSE(matches("struct Foo { Foo() {} int foo; };", CtorDecl)); @@ -256,23 +264,24 @@ TEST_F(RegistryTest, Adaptative) { TEST_F(RegistryTest, Errors) { // Incorrect argument count. OwningPtr<Diagnostics> Error(new Diagnostics()); - EXPECT_TRUE(constructMatcher("hasInitializer", Error.get()).empty()); + EXPECT_TRUE(constructMatcher("hasInitializer", Error.get()).isNull()); EXPECT_EQ("Incorrect argument count. (Expected = 1) != (Actual = 0)", Error->toString()); Error.reset(new Diagnostics()); - EXPECT_TRUE(constructMatcher("isArrow", std::string(), Error.get()).empty()); + EXPECT_TRUE(constructMatcher("isArrow", std::string(), Error.get()).isNull()); EXPECT_EQ("Incorrect argument count. (Expected = 0) != (Actual = 1)", Error->toString()); // Bad argument type Error.reset(new Diagnostics()); - EXPECT_TRUE(constructMatcher("ofClass", std::string(), Error.get()).empty()); + EXPECT_TRUE(constructMatcher("ofClass", std::string(), Error.get()).isNull()); EXPECT_EQ("Incorrect type for arg 1. (Expected = Matcher<CXXRecordDecl>) != " "(Actual = String)", Error->toString()); Error.reset(new Diagnostics()); - EXPECT_TRUE(constructMatcher("recordDecl", recordDecl(), parameterCountIs(3), - Error.get()).empty()); + EXPECT_TRUE(constructMatcher("recordDecl", constructMatcher("recordDecl"), + constructMatcher("parameterCountIs", 3), + Error.get()).isNull()); EXPECT_EQ("Incorrect type for arg 2. (Expected = Matcher<CXXRecordDecl>) != " "(Actual = Matcher<FunctionDecl>)", Error->toString()); diff --git a/clang/unittests/ASTMatchers/Dynamic/VariantValueTest.cpp b/clang/unittests/ASTMatchers/Dynamic/VariantValueTest.cpp index 625f70bd465..168741ca4d9 100644 --- a/clang/unittests/ASTMatchers/Dynamic/VariantValueTest.cpp +++ b/clang/unittests/ASTMatchers/Dynamic/VariantValueTest.cpp @@ -27,7 +27,7 @@ TEST(VariantValueTest, Unsigned) { EXPECT_EQ(kUnsigned, Value.getUnsigned()); EXPECT_FALSE(Value.isString()); - EXPECT_FALSE(Value.isMatchers()); + EXPECT_FALSE(Value.isMatcher()); EXPECT_FALSE(Value.hasTypedMatcher<Decl>()); EXPECT_FALSE(Value.hasTypedMatcher<UnaryOperator>()); } @@ -41,29 +41,29 @@ TEST(VariantValueTest, String) { EXPECT_EQ("String", Value.getTypeAsString()); EXPECT_FALSE(Value.isUnsigned()); - EXPECT_FALSE(Value.isMatchers()); + EXPECT_FALSE(Value.isMatcher()); } TEST(VariantValueTest, DynTypedMatcher) { - VariantValue Value = stmt(); + VariantValue Value = VariantMatcher::SingleMatcher(stmt()); EXPECT_FALSE(Value.isUnsigned()); EXPECT_FALSE(Value.isString()); - EXPECT_TRUE(Value.isMatchers()); + EXPECT_TRUE(Value.isMatcher()); EXPECT_FALSE(Value.hasTypedMatcher<Decl>()); EXPECT_TRUE(Value.hasTypedMatcher<UnaryOperator>()); EXPECT_EQ("Matcher<Stmt>", Value.getTypeAsString()); // Can only convert to compatible matchers. - Value = recordDecl(); - EXPECT_TRUE(Value.isMatchers()); + Value = VariantMatcher::SingleMatcher(recordDecl()); + EXPECT_TRUE(Value.isMatcher()); EXPECT_TRUE(Value.hasTypedMatcher<Decl>()); EXPECT_FALSE(Value.hasTypedMatcher<UnaryOperator>()); EXPECT_EQ("Matcher<Decl>", Value.getTypeAsString()); - Value = ignoringImpCasts(expr()); - EXPECT_TRUE(Value.isMatchers()); + Value = VariantMatcher::SingleMatcher(ignoringImpCasts(expr())); + EXPECT_TRUE(Value.isMatcher()); EXPECT_FALSE(Value.hasTypedMatcher<Decl>()); EXPECT_FALSE(Value.hasTypedMatcher<Stmt>()); EXPECT_TRUE(Value.hasTypedMatcher<Expr>()); @@ -77,13 +77,13 @@ TEST(VariantValueTest, Assignment) { EXPECT_TRUE(Value.isString()); EXPECT_EQ("A", Value.getString()); EXPECT_FALSE(Value.isUnsigned()); - EXPECT_FALSE(Value.isMatchers()); + EXPECT_FALSE(Value.isMatcher()); EXPECT_EQ("String", Value.getTypeAsString()); - Value = recordDecl(); + Value = VariantMatcher::SingleMatcher(recordDecl()); EXPECT_FALSE(Value.isUnsigned()); EXPECT_FALSE(Value.isString()); - EXPECT_TRUE(Value.isMatchers()); + EXPECT_TRUE(Value.isMatcher()); EXPECT_TRUE(Value.hasTypedMatcher<Decl>()); EXPECT_FALSE(Value.hasTypedMatcher<UnaryOperator>()); EXPECT_EQ("Matcher<Decl>", Value.getTypeAsString()); @@ -91,39 +91,46 @@ TEST(VariantValueTest, Assignment) { Value = 17; EXPECT_TRUE(Value.isUnsigned()); EXPECT_EQ(17U, Value.getUnsigned()); - EXPECT_FALSE(Value.isMatchers()); + EXPECT_FALSE(Value.isMatcher()); EXPECT_FALSE(Value.isString()); Value = VariantValue(); EXPECT_FALSE(Value.isUnsigned()); EXPECT_FALSE(Value.isString()); - EXPECT_FALSE(Value.isMatchers()); + EXPECT_FALSE(Value.isMatcher()); EXPECT_EQ("Nothing", Value.getTypeAsString()); } TEST(VariantValueTest, Matcher) { - EXPECT_TRUE(matches("class X {};", VariantValue(recordDecl(hasName("X"))) + EXPECT_TRUE(matches("class X {};", VariantValue(VariantMatcher::SingleMatcher( + recordDecl(hasName("X")))) .getTypedMatcher<Decl>())); + EXPECT_TRUE(matches("int x;", + VariantValue(VariantMatcher::SingleMatcher(varDecl())) + .getTypedMatcher<Decl>())); EXPECT_TRUE( - matches("int x;", VariantValue(varDecl()).getTypedMatcher<Decl>())); - EXPECT_TRUE(matches("int foo() { return 1 + 1; }", - VariantValue(functionDecl()).getTypedMatcher<Decl>())); + matches("int foo() { return 1 + 1; }", + VariantValue(VariantMatcher::SingleMatcher(functionDecl())) + .getTypedMatcher<Decl>())); // Can't get the wrong matcher. - EXPECT_FALSE(VariantValue(varDecl()).hasTypedMatcher<Stmt>()); + EXPECT_FALSE(VariantValue(VariantMatcher::SingleMatcher(varDecl())) + .hasTypedMatcher<Stmt>()); #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST && !defined(_MSC_VER) // Trying to get the wrong matcher fails an assertion in Matcher<T>. We don't // do this test when building with MSVC because its debug C runtime prints the // assertion failure message as a wide string, which gtest doesn't understand. - EXPECT_DEATH(VariantValue(varDecl()).getTypedMatcher<Stmt>(), + EXPECT_DEATH(VariantValue(VariantMatcher::SingleMatcher(varDecl())) + .getTypedMatcher<Stmt>(), "hasTypedMatcher"); #endif - EXPECT_FALSE( - matches("int x;", VariantValue(functionDecl()).getTypedMatcher<Decl>())); + EXPECT_FALSE(matches( + "int x;", VariantValue(VariantMatcher::SingleMatcher(functionDecl())) + .getTypedMatcher<Decl>())); EXPECT_FALSE( matches("int foo() { return 1 + 1; }", - - VariantValue(declRefExpr()).getTypedMatcher<Stmt>())); + VariantValue(VariantMatcher::SingleMatcher(declRefExpr())) + .getTypedMatcher<Stmt>())); } } // end anonymous namespace |