Commit 514e3c36 authored by Stephen Kelly's avatar Stephen Kelly
Browse files

Add missing tests for parent traversal

parent 61b56340
Loading
Loading
Loading
Loading
+60 −0
Original line number Diff line number Diff line
@@ -1633,6 +1633,15 @@ void foo()
              traverse(ast_type_traits::TK_IgnoreImplicitCastsAndParentheses,
                       Matcher)));

  auto ParentMatcher = floatLiteral(hasParent(varDecl(hasName("i"))));

  EXPECT_TRUE(
      notMatches(VarDeclCode, traverse(ast_type_traits::TK_AsIs, ParentMatcher)));
  EXPECT_TRUE(
      matches(VarDeclCode,
              traverse(ast_type_traits::TK_IgnoreUnlessSpelledInSource,
                       ParentMatcher)));

  EXPECT_TRUE(
      matches(VarDeclCode, decl(traverse(ast_type_traits::TK_AsIs,
                                         anyOf(cxxRecordDecl(), varDecl())))));
@@ -1714,6 +1723,13 @@ void bar()
      functionDecl(hasName("foo"), traverse(ast_type_traits::TK_AsIs,
                                            hasDescendant(floatLiteral())))));

  EXPECT_TRUE(
      notMatches(Code, traverse(ast_type_traits::TK_AsIs, floatLiteral(hasParent(callExpr(callee(functionDecl(hasName("foo")))))))));
  EXPECT_TRUE(
      matches(Code,
              traverse(ast_type_traits::TK_IgnoreUnlessSpelledInSource,
                       floatLiteral(hasParent(callExpr(callee(functionDecl(hasName("foo")))))))));

  Code = R"cpp(
void foo()
{
@@ -1724,6 +1740,10 @@ void foo()
      matches(Code,
              traverse(ast_type_traits::TK_IgnoreUnlessSpelledInSource,
                       varDecl(hasInitializer(integerLiteral(equals(3)))))));
  EXPECT_TRUE(
      matches(Code,
              traverse(ast_type_traits::TK_IgnoreUnlessSpelledInSource,
                       integerLiteral(equals(3), hasParent(varDecl(hasName("i")))))));
}

template <typename MatcherT>
@@ -1905,12 +1925,21 @@ void func14() {
                     returnStmt(forFunction(functionDecl(hasName("func1"))),
                                hasReturnValue(integerLiteral(equals(42)))))));

  EXPECT_TRUE(matches(
      Code, traverse(ast_type_traits::TK_IgnoreUnlessSpelledInSource,
        integerLiteral(equals(42), hasParent(returnStmt(forFunction(functionDecl(hasName("func1"))))))
                     )));

  EXPECT_TRUE(matches(
      Code,
      traverse(ast_type_traits::TK_IgnoreUnlessSpelledInSource,
               returnStmt(forFunction(functionDecl(hasName("func2"))),
                          hasReturnValue(cxxTemporaryObjectExpr(
                              hasArgument(0, integerLiteral(equals(42)))))))));
  EXPECT_TRUE(matches(
      Code,
      traverse(ast_type_traits::TK_IgnoreUnlessSpelledInSource,
        integerLiteral(equals(42), hasParent(cxxTemporaryObjectExpr(hasParent(returnStmt(forFunction(functionDecl(hasName("func2")))))))))));

  EXPECT_TRUE(matches(
      Code, traverse(ast_type_traits::TK_IgnoreUnlessSpelledInSource,
@@ -1919,6 +1948,10 @@ void func14() {
                                    cxxFunctionalCastExpr(hasSourceExpression(
                                        integerLiteral(equals(42)))))))));

  EXPECT_TRUE(matches(
      Code, traverse(ast_type_traits::TK_IgnoreUnlessSpelledInSource,
        integerLiteral(equals(42), hasParent(cxxFunctionalCastExpr(hasParent(returnStmt(forFunction(functionDecl(hasName("func3")))) )))))));

  EXPECT_TRUE(matches(
      Code, traverse(ast_type_traits::TK_IgnoreUnlessSpelledInSource,
                     returnStmt(forFunction(functionDecl(hasName("func4"))),
@@ -1957,18 +1990,32 @@ void func14() {
                                hasReturnValue(
                                    declRefExpr(to(varDecl(hasName("a")))))))));

  EXPECT_TRUE(matches(
      Code, traverse(ast_type_traits::TK_IgnoreUnlessSpelledInSource,
        declRefExpr(to(varDecl(hasName("a"))), hasParent(returnStmt(forFunction(functionDecl(hasName("func10")))))))));

  EXPECT_TRUE(matches(
      Code, traverse(ast_type_traits::TK_IgnoreUnlessSpelledInSource,
                     returnStmt(forFunction(functionDecl(hasName("func11"))),
                                hasReturnValue(
                                    declRefExpr(to(varDecl(hasName("b")))))))));

  EXPECT_TRUE(matches(
      Code, traverse(ast_type_traits::TK_IgnoreUnlessSpelledInSource,
        declRefExpr(to(varDecl(hasName("b"))), hasParent(returnStmt(forFunction(functionDecl(hasName("func11"))))))
                     )));

  EXPECT_TRUE(matches(
      Code, traverse(ast_type_traits::TK_IgnoreUnlessSpelledInSource,
                     returnStmt(forFunction(functionDecl(hasName("func12"))),
                                hasReturnValue(
                                    declRefExpr(to(varDecl(hasName("c")))))))));

  EXPECT_TRUE(matches(
      Code, traverse(ast_type_traits::TK_IgnoreUnlessSpelledInSource,
        declRefExpr(to(varDecl(hasName("c"))), hasParent(returnStmt(forFunction(functionDecl(hasName("func12"))))))
                     )));

  EXPECT_TRUE(matches(
      Code,
      traverse(
@@ -1980,6 +2027,19 @@ void func14() {
                                                   varDecl(hasName("c"))))))),
                     has(parmVarDecl(hasName("d")))))));

  EXPECT_TRUE(matches(
      Code,
      traverse(
          ast_type_traits::TK_IgnoreUnlessSpelledInSource,
          declRefExpr(to(varDecl(hasName("a"))), hasParent( lambdaExpr(forFunction(functionDecl(hasName("func13")))) )))));

  EXPECT_TRUE(matches(
      Code,
      traverse(
          ast_type_traits::TK_IgnoreUnlessSpelledInSource,
          varDecl(hasName("b"), hasInitializer(declRefExpr(to(
                                                   varDecl(hasName("c"))))), hasParent( lambdaExpr(forFunction(functionDecl(hasName("func13")))) )))));

  EXPECT_TRUE(matches(
      Code, traverse(ast_type_traits::TK_IgnoreUnlessSpelledInSource,
                     lambdaExpr(