Commit 0a57d14a authored by Stephen Kelly's avatar Stephen Kelly
Browse files

[ASTMatchers] Fix parent traversal with InitListExpr

Children of InitListExpr are traversed twice by RAV, so this code
populates a vector to represent the possibly-multiple parents (in
reality in this situation the parent is the same and is therefore
de-duplicated).
parent b567ff2f
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -133,8 +133,8 @@ public:
    return getDynNodeFromMap(Node, OtherParents);
  }

  ast_type_traits::DynTypedNode
  AscendIgnoreUnlessSpelledInSource(const Expr *E, const Expr *Child) {
  DynTypedNodeList AscendIgnoreUnlessSpelledInSource(const Expr *E,
                                                     const Expr *Child) {

    auto ShouldSkip = [](const Expr *E, const Expr *Child) {
      if (isa<ImplicitCastExpr>(E))
@@ -179,8 +179,11 @@ public:
      if (It == PointerParents.end())
        break;
      const auto *S = It->second.dyn_cast<const Stmt *>();
      if (!S)
      if (!S) {
        if (auto *Vec = It->second.dyn_cast<ParentVector *>())
          return llvm::makeArrayRef(*Vec);
        return getSingleDynTypedNodeFromParentMap(It->second);
      }
      const auto *P = dyn_cast<Expr>(S);
      if (!P)
        return ast_type_traits::DynTypedNode::create(*S);
+10 −0
Original line number Diff line number Diff line
@@ -1744,6 +1744,16 @@ void foo()
      Code,
      traverse(ast_type_traits::TK_IgnoreUnlessSpelledInSource,
               integerLiteral(equals(3), hasParent(varDecl(hasName("i")))))));

  Code = R"cpp(
const char *SomeString{"str"};
)cpp";
  EXPECT_TRUE(matches(Code, traverse(ast_type_traits::TK_AsIs,
                                     stringLiteral(hasParent(implicitCastExpr(
                                         hasParent(initListExpr())))))));
  EXPECT_TRUE(
      matches(Code, traverse(ast_type_traits::TK_IgnoreUnlessSpelledInSource,
                             stringLiteral(hasParent(initListExpr())))));
}

template <typename MatcherT>