https://github.com/pvelesko created 
https://github.com/llvm/llvm-project/pull/243405

With -g, an alias to a function-local static whose function or the static 
itself is nodebug asserted "Region stack mismatch, stack empty!" in 
collectVarDeclProps. EmitGlobalAlias runs at module scope, and because the 
static never got debug info it is not in DeclCache, so 
getDeclarationOrDefinition tried to build a forward declaration that needs the 
enclosing lexical block. The assert came from #213153.

EmitGlobalAlias now skips the imported declaration when the aliasee is a static 
local with no DeclCache entry. Static locals that do have debug info are cached 
and still get their DIImportedEntity.

Fixes #243381

This PR was generated using AI

>From 1546d1e46ed5ab5c894f3b2f118c97119a7d2fbf Mon Sep 17 00:00:00 2001
From: Paulius Velesko <[email protected]>
Date: Tue, 17 Nov 2026 10:06:49 +0200
Subject: [PATCH] [clang][DebugInfo] Skip alias debug info for static locals
 without debug info

With -g, clang asserted "Region stack mismatch, stack empty!" in
CGDebugInfo::collectVarDeclProps for:

  __attribute__((nodebug)) void f() { static int s; }
  extern int a __attribute__((alias("_ZZ1fvE1s")));

and likewise for a static local that is itself marked nodebug.

EmitGlobalAlias runs at module level, where LexicalBlockStack is empty.
A static local that had debug info is already in DeclCache, so
getDeclarationOrDefinition returns early. A nodebug static local never
entered DeclCache, so getDeclarationOrDefinition fell through to
getGlobalVariableForwardDeclaration and collectVarDeclProps, which needs
the enclosing lexical block of a static local. The assert was added by
https://github.com/llvm/llvm-project/pull/213153.

Skip the imported declaration when the aliasee is a static local with no
DeclCache entry.

Fixes #243381
---
 clang/lib/CodeGen/CGDebugInfo.cpp                  |  8 ++++++--
 .../DebugInfo/CXX/alias-nodebug-static-local.cpp   | 14 ++++++++++++++
 2 files changed, 20 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/DebugInfo/CXX/alias-nodebug-static-local.cpp

diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp 
b/clang/lib/CodeGen/CGDebugInfo.cpp
index bc516406269c2c..28aa231306648d 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -6611,8 +6611,12 @@ void CGDebugInfo::EmitGlobalAlias(const 
llvm::GlobalValue *GV,
     // Fix compiler to generate "newname" as imported_declaration
     // pointing to the DIE of "oldname".
     return;
-  if (!(DI = getDeclarationOrDefinition(
-            AliaseeDecl.getCanonicalDecl().getDecl())))
+  const auto *AliaseeD = AliaseeDecl.getCanonicalDecl().getDecl();
+  // A static local without debug info has no scope to forward-declare it in.
+  if (const auto *VD = dyn_cast<VarDecl>(AliaseeD);
+      VD && VD->isStaticLocal() && !DeclCache.count(VD))
+    return;
+  if (!(DI = getDeclarationOrDefinition(AliaseeD)))
     return;
 
   llvm::DIScope *DContext = getDeclContextDescriptor(D);
diff --git a/clang/test/DebugInfo/CXX/alias-nodebug-static-local.cpp 
b/clang/test/DebugInfo/CXX/alias-nodebug-static-local.cpp
new file mode 100644
index 00000000000000..d0b02ba335a06a
--- /dev/null
+++ b/clang/test/DebugInfo/CXX/alias-nodebug-static-local.cpp
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm 
-debug-info-kind=limited %s -o - | FileCheck %s
+
+// Aliases of static locals without debug info must not crash or get an
+// imported declaration.
+
+// CHECK-DAG: @a = alias i32, ptr @_ZZ1fvE1s
+// CHECK-DAG: @b = alias i32, ptr @_ZZ1gvE1s
+// CHECK-NOT: DIImportedEntity
+
+__attribute__((nodebug)) void f() { static int s; }
+extern int a __attribute__((alias("_ZZ1fvE1s")));
+
+void g() { __attribute__((nodebug)) static int s; }
+extern int b __attribute__((alias("_ZZ1gvE1s")));

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to