steveire created this revision. steveire added a reviewer: aaron.ballman. steveire requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D93988 Files: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
Index: clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp =================================================================== --- clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp +++ clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp @@ -26,13 +26,14 @@ } TEST(DeclarationMatcher, ClassDerivedFromDependentTemplateSpecialization) { - EXPECT_TRUE(matches( - "template <typename T> struct A {" - " template <typename T2> struct F {};" - "};" - "template <typename T> struct B : A<T>::template F<T> {};" - "B<int> b;", - cxxRecordDecl(hasName("B"), isDerivedFrom(recordDecl())))); + EXPECT_TRUE( + matches("template <typename T> struct A {" + " template <typename T2> struct F {};" + "};" + "template <typename T> struct B : A<T>::template F<T> {};" + "B<int> b;", + traverse(TK_AsIs, cxxRecordDecl(hasName("B"), + isDerivedFrom(recordDecl()))))); } TEST(DeclarationMatcher, hasDeclContext) { @@ -262,9 +263,9 @@ } TEST(HasDeclaration, HasDeclarationOfCXXNewExpr) { - EXPECT_TRUE( - matches("int *A = new int();", - cxxNewExpr(hasDeclaration(functionDecl(parameterCountIs(1)))))); + EXPECT_TRUE(matches("int *A = new int();", + traverse(TK_AsIs, cxxNewExpr(hasDeclaration(functionDecl( + parameterCountIs(1))))))); } TEST(HasDeclaration, HasDeclarationOfTypeAlias) { @@ -981,60 +982,63 @@ TEST(Matcher, MatchesTypeTemplateArgument) { EXPECT_TRUE(matches( - "template<typename T> struct B {};" + "template<typename T> struct B {};" "B<int> b;", - classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType( - asString("int")))))); + traverse(TK_AsIs, classTemplateSpecializationDecl(hasAnyTemplateArgument( + refersToType(asString("int"))))))); } TEST(Matcher, MatchesTemplateTemplateArgument) { - EXPECT_TRUE(matches("template<template <typename> class S> class X {};" - "template<typename T> class Y {};" - "X<Y> xi;", - classTemplateSpecializationDecl(hasAnyTemplateArgument( - refersToTemplate(templateName()))))); + EXPECT_TRUE(matches( + "template<template <typename> class S> class X {};" + "template<typename T> class Y {};" + "X<Y> xi;", + traverse(TK_AsIs, classTemplateSpecializationDecl(hasAnyTemplateArgument( + refersToTemplate(templateName())))))); } TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) { - EXPECT_TRUE(matches( - "struct B { int next; };" - "template<int(B::*next_ptr)> struct A {};" - "A<&B::next> a;", - classTemplateSpecializationDecl(hasAnyTemplateArgument( - refersToDeclaration(fieldDecl(hasName("next"))))))); + EXPECT_TRUE( + matches("struct B { int next; };" + "template<int(B::*next_ptr)> struct A {};" + "A<&B::next> a;", + traverse(TK_AsIs, + classTemplateSpecializationDecl(hasAnyTemplateArgument( + refersToDeclaration(fieldDecl(hasName("next")))))))); EXPECT_TRUE(notMatches( - "template <typename T> struct A {};" + "template <typename T> struct A {};" "A<int> a;", - classTemplateSpecializationDecl(hasAnyTemplateArgument( - refersToDeclaration(decl()))))); + traverse(TK_AsIs, classTemplateSpecializationDecl(hasAnyTemplateArgument( + refersToDeclaration(decl())))))); EXPECT_TRUE(matches( - "struct B { int next; };" + "struct B { int next; };" "template<int(B::*next_ptr)> struct A {};" "A<&B::next> a;", - templateSpecializationType(hasAnyTemplateArgument(isExpr( - hasDescendant(declRefExpr(to(fieldDecl(hasName("next")))))))))); + traverse(TK_AsIs, templateSpecializationType(hasAnyTemplateArgument( + isExpr(hasDescendant(declRefExpr( + to(fieldDecl(hasName("next"))))))))))); EXPECT_TRUE(notMatches( - "template <typename T> struct A {};" + "template <typename T> struct A {};" "A<int> a;", - templateSpecializationType(hasAnyTemplateArgument( - refersToDeclaration(decl()))))); + traverse(TK_AsIs, templateSpecializationType(hasAnyTemplateArgument( + refersToDeclaration(decl())))))); } TEST(Matcher, MatchesSpecificArgument) { EXPECT_TRUE(matches( - "template<typename T, typename U> class A {};" + "template<typename T, typename U> class A {};" "A<bool, int> a;", - classTemplateSpecializationDecl(hasTemplateArgument( - 1, refersToType(asString("int")))))); + traverse(TK_AsIs, classTemplateSpecializationDecl(hasTemplateArgument( + 1, refersToType(asString("int"))))))); EXPECT_TRUE(notMatches( - "template<typename T, typename U> class A {};" + "template<typename T, typename U> class A {};" "A<int, bool> a;", - classTemplateSpecializationDecl(hasTemplateArgument( - 1, refersToType(asString("int")))))); + traverse(TK_AsIs, classTemplateSpecializationDecl(hasTemplateArgument( + 1, refersToType(asString("int"))))))); EXPECT_TRUE(matches( "template<typename T, typename U> class A {};" @@ -1047,27 +1051,31 @@ templateSpecializationType(hasTemplateArgument( 1, refersToType(asString("int")))))); - EXPECT_TRUE(matches( - "template<typename T> void f() {};" - "void func() { f<int>(); }", - functionDecl(hasTemplateArgument(0, refersToType(asString("int")))))); - EXPECT_TRUE(notMatches( - "template<typename T> void f() {};", - functionDecl(hasTemplateArgument(0, refersToType(asString("int")))))); + EXPECT_TRUE( + matches("template<typename T> void f() {};" + "void func() { f<int>(); }", + traverse(TK_AsIs, functionDecl(hasTemplateArgument( + 0, refersToType(asString("int"))))))); + EXPECT_TRUE( + notMatches("template<typename T> void f() {};", + traverse(TK_AsIs, functionDecl(hasTemplateArgument( + 0, refersToType(asString("int"))))))); } TEST(TemplateArgument, Matches) { - EXPECT_TRUE(matches("template<typename T> struct C {}; C<int> c;", - classTemplateSpecializationDecl( - hasAnyTemplateArgument(templateArgument())))); EXPECT_TRUE(matches( - "template<typename T> struct C {}; C<int> c;", - templateSpecializationType(hasAnyTemplateArgument(templateArgument())))); - + "template<typename T> struct C {}; C<int> c;", + traverse(TK_AsIs, classTemplateSpecializationDecl( + hasAnyTemplateArgument(templateArgument()))))); EXPECT_TRUE(matches( - "template<typename T> void f() {};" - "void func() { f<int>(); }", - functionDecl(hasAnyTemplateArgument(templateArgument())))); + "template<typename T> struct C {}; C<int> c;", + traverse(TK_AsIs, templateSpecializationType( + hasAnyTemplateArgument(templateArgument()))))); + + EXPECT_TRUE(matches("template<typename T> void f() {};" + "void func() { f<int>(); }", + traverse(TK_AsIs, functionDecl(hasAnyTemplateArgument( + templateArgument()))))); } TEST(TemplateTypeParmDecl, CXXMethodDecl) { @@ -1158,13 +1166,14 @@ } TEST(RefersToIntegralType, Matches) { - EXPECT_TRUE(matches("template<int T> struct C {}; C<42> c;", - classTemplateSpecializationDecl( - hasAnyTemplateArgument(refersToIntegralType( - asString("int")))))); - EXPECT_TRUE(notMatches("template<unsigned T> struct C {}; C<42> c;", - classTemplateSpecializationDecl(hasAnyTemplateArgument( - refersToIntegralType(asString("int")))))); + EXPECT_TRUE(matches( + "template<int T> struct C {}; C<42> c;", + traverse(TK_AsIs, classTemplateSpecializationDecl(hasAnyTemplateArgument( + refersToIntegralType(asString("int"))))))); + EXPECT_TRUE(notMatches( + "template<unsigned T> struct C {}; C<42> c;", + traverse(TK_AsIs, classTemplateSpecializationDecl(hasAnyTemplateArgument( + refersToIntegralType(asString("int"))))))); } TEST(ConstructorDeclaration, SimpleCase) { @@ -1240,8 +1249,10 @@ allOf(forField(hasName("foo_")), isWritten()))))); EXPECT_TRUE(notMatches(Code, cxxConstructorDecl(hasAnyConstructorInitializer( allOf(forField(hasName("bar_")), isWritten()))))); - EXPECT_TRUE(matches(Code, cxxConstructorDecl(hasAnyConstructorInitializer( - allOf(forField(hasName("bar_")), unless(isWritten())))))); + EXPECT_TRUE(matches( + Code, traverse(TK_AsIs, + cxxConstructorDecl(hasAnyConstructorInitializer(allOf( + forField(hasName("bar_")), unless(isWritten()))))))); } TEST(HasAnyConstructorInitializer, IsBaseInitializer) { @@ -1533,13 +1544,13 @@ TEST(Matcher, VisitsTemplateInstantiations) { EXPECT_TRUE(matches( - "class A { public: void x(); };" + "class A { public: void x(); };" "template <typename T> class B { public: void y() { T t; t.x(); } };" "void f() { B<A> b; b.y(); }", - callExpr(callee(cxxMethodDecl(hasName("x")))))); + traverse(TK_AsIs, callExpr(callee(cxxMethodDecl(hasName("x"))))))); EXPECT_TRUE(matches( - "class A { public: void x(); };" + "class A { public: void x(); };" "class C {" " public:" " template <typename T> class B { public: void y() { T t; t.x(); } };" @@ -1547,8 +1558,9 @@ "void f() {" " C::B<A> b; b.y();" "}", - recordDecl(hasName("C"), hasDescendant(callExpr( - callee(cxxMethodDecl(hasName("x")))))))); + traverse(TK_AsIs, + recordDecl(hasName("C"), hasDescendant(callExpr(callee( + cxxMethodDecl(hasName("x"))))))))); } TEST(Matcher, HasCondition) { @@ -3431,19 +3443,20 @@ } )cpp"; EXPECT_TRUE(matchAndVerifyResultFalse( - Code, binaryOperator(forEach(declRefExpr().bind("dre"))), + Code, + traverse(TK_AsIs, binaryOperator(forEach(declRefExpr().bind("dre")))), std::make_unique<VerifyIdIsBoundTo<DeclRefExpr>>("dre", 0))); EXPECT_TRUE(matchAndVerifyResultTrue( Code, - binaryOperator(forEach( - implicitCastExpr(hasSourceExpression(declRefExpr().bind("dre"))))), + traverse(TK_AsIs, binaryOperator(forEach(implicitCastExpr( + hasSourceExpression(declRefExpr().bind("dre")))))), std::make_unique<VerifyIdIsBoundTo<DeclRefExpr>>("dre", 2))); EXPECT_TRUE(matchAndVerifyResultTrue( Code, - binaryOperator( - forEach(expr(ignoringImplicit(declRefExpr().bind("dre"))))), + traverse(TK_AsIs, binaryOperator(forEach(expr( + ignoringImplicit(declRefExpr().bind("dre")))))), std::make_unique<VerifyIdIsBoundTo<DeclRefExpr>>("dre", 2))); EXPECT_TRUE(matchAndVerifyResultTrue( @@ -3553,11 +3566,12 @@ anyOf(unless(anything()), anything())), std::make_unique<VerifyIdIsBoundTo<CXXRecordDecl>>("x", 1))); EXPECT_TRUE(matchAndVerifyResultTrue( - "template<typename T1, typename T2> class X {}; X<float, int> x;", - classTemplateSpecializationDecl( - decl().bind("x"), - hasAnyTemplateArgument(refersToType(asString("int")))), - std::make_unique<VerifyIdIsBoundTo<Decl>>("x", 1))); + "template<typename T1, typename T2> class X {}; X<float, int> x;", + traverse(TK_AsIs, + classTemplateSpecializationDecl( + decl().bind("x"), + hasAnyTemplateArgument(refersToType(asString("int"))))), + std::make_unique<VerifyIdIsBoundTo<Decl>>("x", 1))); EXPECT_TRUE(matchAndVerifyResultTrue( "class X { void f(); void g(); };", cxxRecordDecl(decl().bind("x"), hasMethod(hasName("g"))), @@ -3601,12 +3615,22 @@ functionDecl(decl().bind("x"), hasAncestor(recordDecl(hasName("::A")))), std::make_unique<VerifyIdIsBoundTo<Decl>>("x", 1))); EXPECT_TRUE(matchAndVerifyResultTrue( - "template <typename T> struct A { struct B {" + "template <typename T> struct A { struct B {" " void f() { if(true) {} }" "}; };" "void t() { A<int>::B b; b.f(); }", - ifStmt(stmt().bind("x"), hasAncestor(recordDecl(hasName("::A")))), - std::make_unique<VerifyIdIsBoundTo<Stmt>>("x", 2))); + traverse(TK_AsIs, ifStmt(stmt().bind("x"), + hasAncestor(recordDecl(hasName("::A"))))), + std::make_unique<VerifyIdIsBoundTo<Stmt>>("x", 2))); + EXPECT_TRUE(matchAndVerifyResultTrue( + "template <typename T> struct A { struct B {" + " void f() { if(true) {} }" + "}; };" + "void t() { A<int>::B b; b.f(); }", + traverse( + TK_IgnoreUnlessSpelledInSource, + ifStmt(stmt().bind("x"), hasAncestor(recordDecl(hasName("::A"))))), + std::make_unique<VerifyIdIsBoundTo<Stmt>>("x", 1))); EXPECT_TRUE(matchAndVerifyResultTrue( "class A {};", recordDecl(hasName("::A"), decl().bind("x"), unless(hasName("fooble"))), @@ -3709,32 +3733,36 @@ TEST(HasAncestor, MatchesClosestAncestor) { EXPECT_TRUE(matchAndVerifyResultTrue( - "template <typename T> struct C {" + "template <typename T> struct C {" " void f(int) {" " struct I { void g(T) { int x; } } i; i.g(42);" " }" "};" "template struct C<int>;", - varDecl(hasName("x"), - hasAncestor(functionDecl(hasParameter( - 0, varDecl(hasType(asString("int"))))).bind("f"))).bind("v"), - std::make_unique<VerifyIdIsBoundTo<FunctionDecl>>("f", "g", 2))); + traverse(TK_AsIs, + varDecl(hasName("x"), + hasAncestor( + functionDecl(hasParameter(0, varDecl(hasType( + asString("int"))))) + .bind("f"))) + .bind("v")), + std::make_unique<VerifyIdIsBoundTo<FunctionDecl>>("f", "g", 2))); } TEST(HasAncestor, MatchesInTemplateInstantiations) { EXPECT_TRUE(matches( - "template <typename T> struct A { struct B { struct C { T t; }; }; }; " + "template <typename T> struct A { struct B { struct C { T t; }; }; }; " "A<int>::B::C a;", - fieldDecl(hasType(asString("int")), - hasAncestor(recordDecl(hasName("A")))))); + traverse(TK_AsIs, fieldDecl(hasType(asString("int")), + hasAncestor(recordDecl(hasName("A"))))))); } TEST(HasAncestor, MatchesInImplicitCode) { EXPECT_TRUE(matches( - "struct X {}; struct A { A() {} X x; };", - cxxConstructorDecl( - hasAnyConstructorInitializer(withInitializer(expr( - hasAncestor(recordDecl(hasName("A"))))))))); + "struct X {}; struct A { A() {} X x; };", + traverse(TK_AsIs, + cxxConstructorDecl(hasAnyConstructorInitializer(withInitializer( + expr(hasAncestor(recordDecl(hasName("A")))))))))); } TEST(HasParent, MatchesOnlyParent) { @@ -3788,11 +3816,12 @@ } TEST(HasAncestor, ImplicitArrayCopyCtorDeclRefExpr) { - EXPECT_TRUE(matches("struct MyClass {\n" - " int c[1];\n" - " static MyClass Create() { return MyClass(); }\n" - "};", - declRefExpr(to(decl(hasAncestor(decl())))))); + EXPECT_TRUE( + matches("struct MyClass {\n" + " int c[1];\n" + " static MyClass Create() { return MyClass(); }\n" + "};", + traverse(TK_AsIs, declRefExpr(to(decl(hasAncestor(decl()))))))); } TEST(HasAncestor, AnonymousUnionMemberExpr) { @@ -4205,20 +4234,24 @@ "double F(T t);" "int i;" "double j = F(i);"; - EXPECT_TRUE(matches(Fragment, substTemplateTypeParmType(hasReplacementType( - qualType(asString("int")))))); - EXPECT_TRUE(notMatches(Fragment, substTemplateTypeParmType(hasReplacementType( - qualType(asString("double")))))); - EXPECT_TRUE( - notMatches("template<int N>" - "double F();" - "double j = F<5>();", - substTemplateTypeParmType(hasReplacementType(qualType())))); + EXPECT_TRUE(matches( + Fragment, traverse(TK_AsIs, substTemplateTypeParmType(hasReplacementType( + qualType(asString("int"))))))); + EXPECT_TRUE(notMatches( + Fragment, traverse(TK_AsIs, substTemplateTypeParmType(hasReplacementType( + qualType(asString("double"))))))); + EXPECT_TRUE(notMatches( + "template<int N>" + "double F();" + "double j = F<5>();", + traverse(TK_AsIs, + substTemplateTypeParmType(hasReplacementType(qualType()))))); } TEST(ClassTemplateSpecializationDecl, HasSpecializedTemplate) { - auto Matcher = classTemplateSpecializationDecl( - hasSpecializedTemplate(classTemplateDecl())); + auto Matcher = + traverse(TK_AsIs, classTemplateSpecializationDecl( + hasSpecializedTemplate(classTemplateDecl()))); EXPECT_TRUE( matches("template<typename T> class A {}; typedef A<int> B;", Matcher)); EXPECT_TRUE(notMatches("template<typename T> class A {};", Matcher)); Index: clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp =================================================================== --- clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp +++ clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp @@ -711,11 +711,11 @@ } EXPECT_TRUE(matches("template<typename T> struct A {};" "template<> struct A<int> {};", - classTemplateSpecializationDecl())); + traverse(TK_AsIs, classTemplateSpecializationDecl()))); EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;", - classTemplateSpecializationDecl())); + traverse(TK_AsIs, classTemplateSpecializationDecl()))); EXPECT_TRUE(notMatches("template<typename T> struct A {};", - classTemplateSpecializationDecl())); + traverse(TK_AsIs, classTemplateSpecializationDecl()))); } TEST_P(ASTMatchersTest, DeclaratorDecl) { @@ -1021,14 +1021,16 @@ "void foo() { struct point { double x; double y; };" " struct point ptarray[10] = " " { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; }", - initListExpr( - has(cxxConstructExpr(requiresZeroInitialization())), - has(initListExpr( - hasType(asString("struct point")), has(floatLiteral(equals(1.0))), - has(implicitValueInitExpr(hasType(asString("double")))))), - has(initListExpr(hasType(asString("struct point")), - has(floatLiteral(equals(2.0))), - has(floatLiteral(equals(1.0)))))))); + traverse( + TK_AsIs, + initListExpr(has(cxxConstructExpr(requiresZeroInitialization())), + has(initListExpr(hasType(asString("struct point")), + has(floatLiteral(equals(1.0))), + has(implicitValueInitExpr( + hasType(asString("double")))))), + has(initListExpr(hasType(asString("struct point")), + has(floatLiteral(equals(2.0))), + has(floatLiteral(equals(1.0))))))))); } TEST_P(ASTMatchersTest, ParenListExpr) { @@ -1189,19 +1191,20 @@ TEST_P(ASTMatchersTest, CastExpr_DoesNotMatchNonCasts) { if (GetParam().Language == Lang_C89 || GetParam().Language == Lang_C99) { // This does have a cast in C - EXPECT_TRUE(matches("char c = '0';", implicitCastExpr())); + EXPECT_TRUE( + matches("char c = '0';", traverse(TK_AsIs, implicitCastExpr()))); } else { - EXPECT_TRUE(notMatches("char c = '0';", castExpr())); + EXPECT_TRUE(notMatches("char c = '0';", traverse(TK_AsIs, castExpr()))); } - EXPECT_TRUE(notMatches("int i = (0);", castExpr())); - EXPECT_TRUE(notMatches("int i = 0;", castExpr())); + EXPECT_TRUE(notMatches("int i = (0);", traverse(TK_AsIs, castExpr()))); + EXPECT_TRUE(notMatches("int i = 0;", traverse(TK_AsIs, castExpr()))); } TEST_P(ASTMatchersTest, CastExpr_DoesNotMatchNonCasts_CXX) { if (!GetParam().isCXX()) { return; } - EXPECT_TRUE(notMatches("char c, &q = c;", castExpr())); + EXPECT_TRUE(notMatches("char c, &q = c;", traverse(TK_AsIs, castExpr()))); } TEST_P(ASTMatchersTest, CXXReinterpretCastExpr) { @@ -1233,7 +1236,7 @@ } StringRef foo_class = "class Foo { public: Foo(const char*); };"; EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }", - cxxFunctionalCastExpr())); + traverse(TK_AsIs, cxxFunctionalCastExpr()))); } TEST_P(ASTMatchersTest, CXXFunctionalCastExpr_DoesNotMatchOtherCasts) { @@ -1243,9 +1246,9 @@ StringRef FooClass = "class Foo { public: Foo(const char*); };"; EXPECT_TRUE( notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }", - cxxFunctionalCastExpr())); + traverse(TK_AsIs, cxxFunctionalCastExpr()))); EXPECT_TRUE(notMatches(FooClass + "void r() { Foo f = \"hello world\"; }", - cxxFunctionalCastExpr())); + traverse(TK_AsIs, cxxFunctionalCastExpr()))); } TEST_P(ASTMatchersTest, CXXDynamicCastExpr) { @@ -1508,7 +1511,8 @@ } TEST_P(ASTMatchersTest, QualType) { - EXPECT_TRUE(matches("struct S {};", qualType().bind("loc"))); + EXPECT_TRUE( + matches("struct S {};", traverse(TK_AsIs, qualType().bind("loc")))); } TEST_P(ASTMatchersTest, ConstantArrayType) { @@ -1569,7 +1573,7 @@ } EXPECT_TRUE(matches("auto i = 2;", autoType())); EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }", - autoType())); + traverse(TK_AsIs, autoType()))); EXPECT_TRUE(matches("auto i = 2;", varDecl(hasType(isInteger())))); EXPECT_TRUE(matches("struct X{}; auto x = X{};", @@ -1843,10 +1847,12 @@ " return 1 + T();" "}" "int i = F<int>();"; - EXPECT_FALSE(matches(code, binaryOperator(hasLHS( - expr(hasType(substTemplateTypeParmType())))))); - EXPECT_TRUE(matches(code, binaryOperator(hasRHS( - expr(hasType(substTemplateTypeParmType())))))); + EXPECT_FALSE( + matches(code, traverse(TK_AsIs, binaryOperator(hasLHS(expr(hasType( + substTemplateTypeParmType()))))))); + EXPECT_TRUE( + matches(code, traverse(TK_AsIs, binaryOperator(hasRHS(expr(hasType( + substTemplateTypeParmType()))))))); } TEST_P(ASTMatchersTest, NestedNameSpecifier) { Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp =================================================================== --- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp +++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp @@ -194,7 +194,7 @@ DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X")))); EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX)); - EXPECT_TRUE(matches("class X {};", HasClassX)); + EXPECT_TRUE(matches("class X {};", traverse(TK_AsIs, HasClassX))); DeclarationMatcher YHasClassX = recordDecl(hasName("Y"), has(recordDecl(hasName("X")))); @@ -265,12 +265,14 @@ return; } - DeclarationMatcher Recursive = recordDecl( - anyOf(has(recordDecl(anyOf(has(recordDecl(hasName("X"))), - has(recordDecl(hasName("Y"))), hasName("Z")))), - has(recordDecl(anyOf(hasName("C"), has(recordDecl(hasName("A"))), - has(recordDecl(hasName("B")))))), - hasName("F"))); + DeclarationMatcher Recursive = traverse( + TK_AsIs, + recordDecl(anyOf( + has(recordDecl(anyOf(has(recordDecl(hasName("X"))), + has(recordDecl(hasName("Y"))), hasName("Z")))), + has(recordDecl(anyOf(hasName("C"), has(recordDecl(hasName("A"))), + has(recordDecl(hasName("B")))))), + hasName("F")))); EXPECT_TRUE(matches("class F {};", Recursive)); EXPECT_TRUE(matches("class Z {};", Recursive)); @@ -540,26 +542,26 @@ EXPECT_TRUE( matches("template<class X> class A { public: class Z : public X {}; }; " "class X{}; void y() { A<X>::Z z; }", - ZIsDerivedFromX)); + traverse(TK_AsIs, ZIsDerivedFromX))); EXPECT_TRUE( matches("template <class T> class X {}; " "template<class Y> class A { class Z : public X<Y> {}; };", - ZIsDerivedFromX)); + traverse(TK_AsIs, ZIsDerivedFromX))); EXPECT_TRUE(notMatches("template<template<class T> class X> class A { " " class Z : public X<int> {}; };", - ZIsDerivedFromX)); + traverse(TK_AsIs, ZIsDerivedFromX))); EXPECT_TRUE(matches("template<template<class T> class X> class A { " " public: class Z : public X<int> {}; }; " "template<class T> class X {}; void y() { A<X>::Z z; }", - ZIsDerivedFromX)); + traverse(TK_AsIs, ZIsDerivedFromX))); EXPECT_TRUE( notMatches("template<class X> class A { class Z : public X::D {}; };", - ZIsDerivedFromX)); + traverse(TK_AsIs, ZIsDerivedFromX))); EXPECT_TRUE(matches("template<class X> class A { public: " " class Z : public X::D {}; }; " "class Y { public: class X {}; typedef X D; }; " "void y() { A<Y>::Z z; }", - ZIsDerivedFromX)); + traverse(TK_AsIs, ZIsDerivedFromX))); EXPECT_TRUE(matches("class X {}; typedef X Y; class Z : public Y {};", ZIsDerivedFromX)); EXPECT_TRUE(matches("template<class T> class Y { typedef typename T::U X; " @@ -574,10 +576,10 @@ matches("template<class T> class X { public: typedef X<T> D; }; " "template<class T> class A { public: " " class Z : public X<T>::D {}; }; void y() { A<int>::Z z; }", - ZIsDerivedFromX)); + traverse(TK_AsIs, ZIsDerivedFromX))); EXPECT_TRUE( notMatches("template<class X> class A { class Z : public X::D::E {}; };", - ZIsDerivedFromX)); + traverse(TK_AsIs, ZIsDerivedFromX))); EXPECT_TRUE( matches("class X {}; typedef X V; typedef V W; class Z : public W {};", ZIsDerivedFromX)); @@ -590,11 +592,11 @@ EXPECT_TRUE( matches("template<class T, class U> class X {}; " "template<class T> class A { class Z : public X<T, int> {}; };", - ZIsDerivedFromX)); + traverse(TK_AsIs, ZIsDerivedFromX))); EXPECT_TRUE( notMatches("template<class X> class D { typedef X A; typedef A B; " " typedef B C; class Z : public C {}; };", - ZIsDerivedFromX)); + traverse(TK_AsIs, ZIsDerivedFromX))); EXPECT_TRUE(matches("class X {}; typedef X A; typedef A B; " "class Z : public B {};", ZIsDerivedFromX)); @@ -619,36 +621,38 @@ EXPECT_TRUE(notMatches("template <typename T> class Z;" "template <> class Z<void> {};" "template <typename T> class Z : public Z<void> {};", - IsDerivedFromX)); + traverse(TK_AsIs, IsDerivedFromX))); EXPECT_TRUE(matches("template <typename T> class X;" "template <> class X<void> {};" "template <typename T> class X : public X<void> {};", - IsDerivedFromX)); + traverse(TK_AsIs, IsDerivedFromX))); EXPECT_TRUE( matches("class X {};" "template <typename T> class Z;" "template <> class Z<void> {};" "template <typename T> class Z : public Z<void>, public X {};", - ZIsDerivedFromX)); - EXPECT_TRUE( - notMatches("template<int> struct X;" - "template<int i> struct X : public X<i-1> {};", - cxxRecordDecl(isDerivedFrom(recordDecl(hasName("Some")))))); + traverse(TK_AsIs, ZIsDerivedFromX))); + EXPECT_TRUE(notMatches("template<int> struct X;" + "template<int i> struct X : public X<i-1> {};", + traverse(TK_AsIs, cxxRecordDecl(isDerivedFrom( + recordDecl(hasName("Some"))))))); EXPECT_TRUE(matches( "struct A {};" "template<int> struct X;" "template<int i> struct X : public X<i-1> {};" "template<> struct X<0> : public A {};" "struct B : public X<42> {};", - cxxRecordDecl(hasName("B"), isDerivedFrom(recordDecl(hasName("A")))))); + traverse(TK_AsIs, cxxRecordDecl(hasName("B"), isDerivedFrom(recordDecl( + hasName("A"))))))); EXPECT_TRUE(notMatches( "struct A {};" "template<int> struct X;" "template<int i> struct X : public X<i-1> {};" "template<> struct X<0> : public A {};" "struct B : public X<42> {};", - cxxRecordDecl(hasName("B"), - isDirectlyDerivedFrom(recordDecl(hasName("A")))))); + traverse(TK_AsIs, + cxxRecordDecl(hasName("B"), isDirectlyDerivedFrom( + recordDecl(hasName("A"))))))); // FIXME: Once we have better matchers for template type matching, // get rid of the Variable(...) matching and match the right template @@ -662,19 +666,22 @@ "template <> class Z<double> : public Z<int> {};" "template <typename T> class Z : public Z<float>, public Z<double> {};" "void f() { Z<float> z_float; Z<double> z_double; Z<char> z_char; }"; - EXPECT_TRUE(matches( - RecursiveTemplateOneParameter, - varDecl(hasName("z_float"), - hasInitializer(hasType(cxxRecordDecl(isDerivedFrom("Base1"))))))); - EXPECT_TRUE(notMatches( - RecursiveTemplateOneParameter, - varDecl(hasName("z_float"), - hasInitializer(hasType(cxxRecordDecl(isDerivedFrom("Base2"))))))); EXPECT_TRUE( matches(RecursiveTemplateOneParameter, - varDecl(hasName("z_char"), - hasInitializer(hasType(cxxRecordDecl( - isDerivedFrom("Base1"), isDerivedFrom("Base2"))))))); + traverse(TK_AsIs, varDecl(hasName("z_float"), + hasInitializer(hasType(cxxRecordDecl( + isDerivedFrom("Base1")))))))); + EXPECT_TRUE( + notMatches(RecursiveTemplateOneParameter, + traverse(TK_AsIs, varDecl(hasName("z_float"), + hasInitializer(hasType(cxxRecordDecl( + isDerivedFrom("Base2")))))))); + EXPECT_TRUE( + matches(RecursiveTemplateOneParameter, + traverse(TK_AsIs, varDecl(hasName("z_char"), + hasInitializer(hasType(cxxRecordDecl( + isDerivedFrom("Base1"), + isDerivedFrom("Base2")))))))); const char *RecursiveTemplateTwoParameters = "class Base1 {}; class Base2 {};" @@ -687,6 +694,8 @@ " public Z<float, T2>, public Z<double, T2> {};" "void f() { Z<float, void> z_float; Z<double, void> z_double; " " Z<char, void> z_char; }"; + + // TODO: No change? EXPECT_TRUE(matches( RecursiveTemplateTwoParameters, varDecl(hasName("z_float"), @@ -842,7 +851,8 @@ return; } - const auto IsLambda = cxxMethodDecl(ofClass(cxxRecordDecl(isLambda()))); + const auto IsLambda = + traverse(TK_AsIs, cxxMethodDecl(ofClass(cxxRecordDecl(isLambda())))); EXPECT_TRUE(matches("auto x = []{};", IsLambda)); EXPECT_TRUE(notMatches("struct S { void operator()() const; };", IsLambda)); } @@ -1231,10 +1241,12 @@ matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX)); // The match here is on the implicit copy constructor code for // class X, not on code 'X x = y'. - EXPECT_TRUE(matches("class X {}; void y(X y) { X x = y; }", ReferenceClassX)); - EXPECT_TRUE(notMatches("class X {}; extern X x;", ReferenceClassX)); - EXPECT_TRUE( - notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX)); + EXPECT_TRUE(matches("class X {}; void y(X y) { X x = y; }", + traverse(TK_AsIs, ReferenceClassX))); + EXPECT_TRUE(notMatches("class X {}; extern X x;", + traverse(TK_AsIs, ReferenceClassX))); + EXPECT_TRUE(notMatches("class X {}; void y(X *y) { X *&x = y; }", + traverse(TK_AsIs, ReferenceClassX))); } TEST_P(ASTMatchersTest, HasLocalQualifiers) { @@ -1405,18 +1417,19 @@ return; } - EXPECT_TRUE( - matches("template<typename T> struct C {}; C<int> c;", - classTemplateSpecializationDecl(templateArgumentCountIs(1)))); - EXPECT_TRUE( - notMatches("template<typename T> struct C {}; C<int> c;", - classTemplateSpecializationDecl(templateArgumentCountIs(2)))); + EXPECT_TRUE(matches("template<typename T> struct C {}; C<int> c;", + traverse(TK_AsIs, classTemplateSpecializationDecl( + templateArgumentCountIs(1))))); + EXPECT_TRUE(notMatches("template<typename T> struct C {}; C<int> c;", + traverse(TK_AsIs, classTemplateSpecializationDecl( + templateArgumentCountIs(2))))); EXPECT_TRUE(matches("template<typename T> struct C {}; C<int> c;", - templateSpecializationType(templateArgumentCountIs(1)))); - EXPECT_TRUE( - notMatches("template<typename T> struct C {}; C<int> c;", - templateSpecializationType(templateArgumentCountIs(2)))); + traverse(TK_AsIs, templateSpecializationType( + templateArgumentCountIs(1))))); + EXPECT_TRUE(notMatches("template<typename T> struct C {}; C<int> c;", + traverse(TK_AsIs, templateSpecializationType( + templateArgumentCountIs(2))))); } TEST_P(ASTMatchersTest, IsIntegral) { @@ -1424,12 +1437,14 @@ return; } - EXPECT_TRUE(matches( - "template<int T> struct C {}; C<42> c;", - classTemplateSpecializationDecl(hasAnyTemplateArgument(isIntegral())))); - EXPECT_TRUE(notMatches("template<typename T> struct C {}; C<int> c;", - classTemplateSpecializationDecl(hasAnyTemplateArgument( - templateArgument(isIntegral()))))); + EXPECT_TRUE( + matches("template<int T> struct C {}; C<42> c;", + traverse(TK_AsIs, classTemplateSpecializationDecl( + hasAnyTemplateArgument(isIntegral()))))); + EXPECT_TRUE(notMatches( + "template<typename T> struct C {}; C<int> c;", + traverse(TK_AsIs, classTemplateSpecializationDecl(hasAnyTemplateArgument( + templateArgument(isIntegral())))))); } TEST_P(ASTMatchersTest, EqualsIntegralValue) { @@ -1437,18 +1452,22 @@ return; } - EXPECT_TRUE(matches("template<int T> struct C {}; C<42> c;", - classTemplateSpecializationDecl( - hasAnyTemplateArgument(equalsIntegralValue("42"))))); - EXPECT_TRUE(matches("template<int T> struct C {}; C<-42> c;", - classTemplateSpecializationDecl( - hasAnyTemplateArgument(equalsIntegralValue("-42"))))); - EXPECT_TRUE(matches("template<int T> struct C {}; C<-0042> c;", - classTemplateSpecializationDecl( - hasAnyTemplateArgument(equalsIntegralValue("-34"))))); - EXPECT_TRUE(notMatches("template<int T> struct C {}; C<42> c;", - classTemplateSpecializationDecl(hasAnyTemplateArgument( - equalsIntegralValue("0042"))))); + EXPECT_TRUE(matches( + "template<int T> struct C {}; C<42> c;", + traverse(TK_AsIs, classTemplateSpecializationDecl(hasAnyTemplateArgument( + equalsIntegralValue("42")))))); + EXPECT_TRUE(matches( + "template<int T> struct C {}; C<-42> c;", + traverse(TK_AsIs, classTemplateSpecializationDecl(hasAnyTemplateArgument( + equalsIntegralValue("-42")))))); + EXPECT_TRUE(matches( + "template<int T> struct C {}; C<-0042> c;", + traverse(TK_AsIs, classTemplateSpecializationDecl(hasAnyTemplateArgument( + equalsIntegralValue("-34")))))); + EXPECT_TRUE(notMatches( + "template<int T> struct C {}; C<42> c;", + traverse(TK_AsIs, classTemplateSpecializationDecl(hasAnyTemplateArgument( + equalsIntegralValue("0042")))))); } TEST_P(ASTMatchersTest, AccessSpecDecl) { @@ -1757,15 +1776,18 @@ // This one doesn't match because the constructor is not added by the // compiler (it is not needed). - EXPECT_TRUE(notMatches("class Foo { };", cxxConstructorDecl(isImplicit()))); + EXPECT_TRUE(notMatches("class Foo { };", + traverse(TK_AsIs, cxxConstructorDecl(isImplicit())))); // The compiler added the implicit default constructor. EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();", - cxxConstructorDecl(isImplicit()))); - EXPECT_TRUE(matches("class Foo { Foo(){} };", - cxxConstructorDecl(unless(isImplicit())))); + traverse(TK_AsIs, cxxConstructorDecl(isImplicit())))); + EXPECT_TRUE( + matches("class Foo { Foo(){} };", + traverse(TK_AsIs, cxxConstructorDecl(unless(isImplicit()))))); // The compiler added an implicit assignment operator. - EXPECT_TRUE(matches("struct A { int x; } a = {0}, b = a; void f() { a = b; }", - cxxMethodDecl(isImplicit(), hasName("operator=")))); + EXPECT_TRUE(matches( + "struct A { int x; } a = {0}, b = a; void f() { a = b; }", + traverse(TK_AsIs, cxxMethodDecl(isImplicit(), hasName("operator="))))); } TEST_P(ASTMatchersTest, IsExplicit_CXXConstructorDecl) { @@ -2508,13 +2530,14 @@ EXPECT_TRUE( matches("template <typename T> class X {}; class A {}; X<A> x;", - cxxRecordDecl(hasName("::X"), isTemplateInstantiation()))); + traverse(TK_AsIs, cxxRecordDecl(hasName("::X"), + isTemplateInstantiation())))); EXPECT_TRUE(matches( "template <typename T> class X { T t; }; class A {}; X<A> x;", - cxxRecordDecl( - isTemplateInstantiation(), - hasDescendant(fieldDecl(hasType(recordDecl(hasName("A")))))))); + traverse(TK_AsIs, cxxRecordDecl(isTemplateInstantiation(), + hasDescendant(fieldDecl(hasType( + recordDecl(hasName("A"))))))))); } TEST_P(ASTMatchersTest, @@ -2525,8 +2548,9 @@ EXPECT_TRUE(matches( "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }", - functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))), - isTemplateInstantiation()))); + traverse(TK_AsIs, + functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))), + isTemplateInstantiation())))); } TEST_P(ASTMatchersTest, @@ -2535,19 +2559,21 @@ return; } - EXPECT_TRUE(matches("template <typename T> class X { T t; }; class A {};" - "template class X<A>;", - cxxRecordDecl(isTemplateInstantiation(), - hasDescendant(fieldDecl( - hasType(recordDecl(hasName("A")))))))); + EXPECT_TRUE(matches( + "template <typename T> class X { T t; }; class A {};" + "template class X<A>;", + traverse(TK_AsIs, cxxRecordDecl(isTemplateInstantiation(), + hasDescendant(fieldDecl(hasType( + recordDecl(hasName("A"))))))))); // Make sure that we match the instantiation instead of the template // definition by checking whether the member function is present. - EXPECT_TRUE( - matches("template <typename T> class X { void f() { T t; } };" - "extern template class X<int>;", - cxxRecordDecl(isTemplateInstantiation(), - unless(hasDescendant(varDecl(hasName("t"))))))); + EXPECT_TRUE(matches( + "template <typename T> class X { void f() { T t; } };" + "extern template class X<int>;", + traverse(TK_AsIs, + cxxRecordDecl(isTemplateInstantiation(), + unless(hasDescendant(varDecl(hasName("t")))))))); } TEST_P( @@ -2560,7 +2586,8 @@ EXPECT_TRUE( matches("template <typename T> class X {};" "template <typename T> class X<T*> {}; class A {}; X<A*> x;", - cxxRecordDecl(hasName("::X"), isTemplateInstantiation()))); + traverse(TK_AsIs, cxxRecordDecl(hasName("::X"), + isTemplateInstantiation())))); } TEST_P( @@ -2576,7 +2603,8 @@ " template <typename U> class Y { U u; };" " Y<A> y;" "};", - cxxRecordDecl(hasName("::X::Y"), isTemplateInstantiation()))); + traverse(TK_AsIs, cxxRecordDecl(hasName("::X::Y"), + isTemplateInstantiation())))); } TEST_P( @@ -2595,7 +2623,8 @@ " template <typename U> class Y { U u; };" " Y<T> y;" "}; X<A> x;", - cxxRecordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation())))); + traverse(TK_AsIs, cxxRecordDecl(hasName("::X<A>::Y"), + unless(isTemplateInstantiation()))))); } TEST_P( @@ -2627,7 +2656,7 @@ EXPECT_TRUE( matches("template<typename T> class A { T i; }; class Y { A<int> a; };", - cxxRecordDecl(isInstantiated()))); + traverse(TK_AsIs, cxxRecordDecl(isInstantiated())))); } TEST_P(ASTMatchersTest, IsInstantiated_NotMatchesDefinition) { @@ -2644,9 +2673,10 @@ return; } - EXPECT_TRUE(matches("template<typename T> struct A { A() { T i; } };" - "class Y { A<int> a; }; Y y;", - declStmt(isInTemplateInstantiation()))); + EXPECT_TRUE( + matches("template<typename T> struct A { A() { T i; } };" + "class Y { A<int> a; }; Y y;", + traverse(TK_AsIs, declStmt(isInTemplateInstantiation())))); } TEST_P(ASTMatchersTest, IsInTemplateInstantiation_NotMatchesDefinitionStmt) { @@ -2665,7 +2695,7 @@ EXPECT_TRUE( matches("template<typename T> void A(T t) { T i; } void x() { A(0); }", - functionDecl(isInstantiated()))); + traverse(TK_AsIs, functionDecl(isInstantiated())))); } TEST_P(ASTMatchersTest, IsInstantiated_NotMatchesFunctionDefinition) { @@ -2685,7 +2715,7 @@ EXPECT_TRUE( matches("template<typename T> void A(T t) { T i; } void x() { A(0); }", - declStmt(isInTemplateInstantiation()))); + traverse(TK_AsIs, declStmt(isInTemplateInstantiation())))); } TEST_P(ASTMatchersTest, @@ -3429,10 +3459,10 @@ EXPECT_TRUE(matches( "auto lambda2 = [](double x, double y) -> double {return x + y;};", - functionDecl(hasTrailingReturn()))); + traverse(TK_AsIs, functionDecl(hasTrailingReturn())))); EXPECT_TRUE( notMatches("auto lambda2 = [](double x, double y) {return x + y;};", - functionDecl(hasTrailingReturn()))); + traverse(TK_AsIs, functionDecl(hasTrailingReturn())))); } TEST_P(ASTMatchersTest, IsAssignmentOperator) {
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits