[clang] [AST] Avoid singleton ParentVectors for repeated parents (PR #238710)

via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 4 00:32:56 PST 2026


https://github.com/relaxcn updated https://github.com/llvm/llvm-project/pull/238710

>From ea88db6eee3a283ea04bce021411c21776a78839 Mon Sep 17 00:00:00 2001
From: relaxcn <irelaxcn at outlook.com>
Date: Wed, 28 Oct 2026 18:14:10 +0800
Subject: [PATCH] [AST] Avoid singleton ParentVectors for repeated parents

Some nodes are reachable through more than one traversal path, so
`addParent()` can record the same child-to-parent edge repeatedly. It
promoted a single parent to `ParentVector` before checking for an
existing copy, so a repeated edge turned into a one-element vector.
The contents were unchanged, but the storage form was different.

That distinction matters during queries. Ignored-node traversal treats
a `ParentVector` as "more than one parent, no single path" and stops
ascending. A parent that merely appeared twice could therefore cut the
walk short before all wrappers were skipped.

I now compare the candidate parent `ParentStack.back()` with the stored
`Single` before allocating a vector, and return when they refer to the
same node. The comparison is guarded by `getMemoizationData()`, since
`DynTypedNode::operator==` only supports nodes with pointer identity.

Genuine multiple parents still use `ParentVector`, and entries that are
already vectors keep the existing dedup logic. The regression test
exercises both `TK_AsIs` and `TK_IgnoreUnlessSpelledInSource`.
---
 clang/lib/AST/ParentMapContext.cpp            |  8 ++++--
 .../unittests/AST/ASTContextParentMapTest.cpp | 26 +++++++++++++++++--
 2 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/clang/lib/AST/ParentMapContext.cpp b/clang/lib/AST/ParentMapContext.cpp
index 7607cf27bfe60f..48e86578b371ba 100644
--- a/clang/lib/AST/ParentMapContext.cpp
+++ b/clang/lib/AST/ParentMapContext.cpp
@@ -392,8 +392,12 @@ class ParentMapContext::ParentMap::ASTVisitor
         NodeOrVector = new DynTypedNode(ParentStack.back());
     } else {
       if (!isa<ParentVector *>(NodeOrVector)) {
-        auto *Vector = new ParentVector(
-            1, getSingleDynTypedNodeFromParentMap(NodeOrVector));
+        const auto Single = getSingleDynTypedNodeFromParentMap(NodeOrVector);
+        // Preserve single-parent storage for ignored-node traversal.
+        if (ParentStack.back().getMemoizationData() &&
+            Single == ParentStack.back())
+          return;
+        auto *Vector = new ParentVector(1, Single);
         delete dyn_cast<DynTypedNode *>(NodeOrVector);
         NodeOrVector = Vector;
       }
diff --git a/clang/unittests/AST/ASTContextParentMapTest.cpp b/clang/unittests/AST/ASTContextParentMapTest.cpp
index 545eaf36191717..41773051683bc1 100644
--- a/clang/unittests/AST/ASTContextParentMapTest.cpp
+++ b/clang/unittests/AST/ASTContextParentMapTest.cpp
@@ -10,13 +10,14 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "clang/AST/ASTContext.h"
 #include "MatchVerifier.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/ParentMapContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/ASTMatchers/ASTMatchers.h"
 #include "clang/Tooling/Tooling.h"
-#include "gtest/gtest.h"
 #include "gmock/gmock.h"
+#include "gtest/gtest.h"
 
 using testing::ElementsAre;
 
@@ -147,6 +148,27 @@ TEST(GetParents, FriendTypeLoc) {
               ElementsAre(DynTypedNode::create(FrA)));
 }
 
+TEST(GetParents, IgnoresRepeatedInitializerWrappers) {
+  auto AST = tooling::buildASTFromCodeWithArgs("int values[] = {((0))};",
+                                               {"-std=c11"}, "input.c");
+  ASSERT_TRUE(AST);
+  auto &Ctx = AST->getASTContext();
+  auto *VD = cast<VarDecl>(
+      Ctx.getTranslationUnitDecl()->lookup(&Ctx.Idents.get("values")).front());
+  auto *Init = cast<InitListExpr>(VD->getInit());
+  ASSERT_TRUE(Init->getSyntacticForm());
+  ASSERT_EQ(Init->getInit(0), Init->getSyntacticForm()->getInit(0));
+  auto *Outer = cast<ParenExpr>(Init->getInit(0));
+  auto *Inner = cast<ParenExpr>(Outer->getSubExpr());
+  auto *Literal = cast<IntegerLiteral>(Inner->getSubExpr());
+  EXPECT_THAT(Ctx.getParents(*Literal),
+              ElementsAre(DynTypedNode::create(*Inner)));
+
+  Ctx.getParentMapContext().setTraversalKind(TK_IgnoreUnlessSpelledInSource);
+  EXPECT_THAT(Ctx.getParents(*Literal),
+              ElementsAre(DynTypedNode::create(*Init)));
+}
+
 TEST(GetParents, UserDefinedTupleLikeTypes) {
   MatchVerifier<VarDecl> Verifier;
   EXPECT_TRUE(Verifier.match(



More information about the cfe-commits mailing list