[clang] Fix incorrect array initialization with string literal in dependent settings (fixes #112189) (PR #172995)

Corentin Jabot via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 21 04:23:33 PST 2025


================
@@ -133,6 +133,82 @@ const Expr *Expr::skipRValueSubobjectAdjustments(
   return E;
 }
 
+static StringLiteral *CloneStringLiteral(const StringLiteral *SL,
+                                         ASTContext &C) {
+  SourceLocation *SLocs = new (C) SourceLocation[SL->getNumConcatenated()];
+  std::copy(SL->tokloc_begin(), SL->tokloc_end(), SLocs);
+  return StringLiteral::Create(
+      C, SL->getBytes(), SL->getKind(), SL->isPascal(), SL->getType(),
+      ArrayRef<SourceLocation>(SLocs, SL->getNumConcatenated()));
+}
+
+// Exactly follow `IgnoreParensSingleStep` (`AST/IgnoreExpr.h`)
+// We only recursively visit those subexpressions which `IgnoreParensSingleStep`
+// drills down to.
+Expr *Expr::CloneIfIAmAStringLiteral(ASTContext &C) {
+  if (auto *SL = dyn_cast<StringLiteral>(this)) {
+    return CloneStringLiteral(SL, C);
+  }
+
+  if (auto *PE = dyn_cast<ParenExpr>(this)) {
+    return new (C) ParenExpr(PE->getBeginLoc(), PE->getEndLoc(),
+                             PE->getSubExpr()->CloneIfIAmAStringLiteral(C));
+  }
+
+  if (auto *UO = dyn_cast<UnaryOperator>(this)) {
+    if (UO->getOpcode() == UO_Extension) {
+      return UnaryOperator::Create(
+          C, UO->getSubExpr()->CloneIfIAmAStringLiteral(C), UO_Extension,
+          UO->getType(), UO->getValueKind(), UO->getObjectKind(),
+          UO->getBeginLoc(), UO->canOverflow(), UO->getFPOptionsOverride());
+    }
+  }
+
+  else if (auto *GSE = dyn_cast<GenericSelectionExpr>(this)) {
+    if (!GSE->isResultDependent()) {
+      ArrayRef<Expr *> GSEAEs = GSE->getAssocExprs();
+      Expr **NewGSEAEs = new (C) Expr *[GSEAEs.size()];
----------------
cor3ntin wrote:

The data is copied in the trailing objects (in the constructor of `GenericSelectionExpr`) - so afaict this array is indeed dangling and SmallVec is a great suggestion

https://github.com/llvm/llvm-project/pull/172995


More information about the cfe-commits mailing list