[clang] [clang][DebugInfo] Don't assume static locals are emitted inside their function (PR #236525)

Kristina Bessonova via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 1 02:04:31 PST 2026


================
@@ -4759,8 +4759,8 @@ void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
 
   // Get context for static locals (that are technically globals) the same way
   // we do for "local" locals -- by using current lexical block.
-  if (VD->isStaticLocal()) {
-    assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
+  // Outside a function (aliases, CUDA/HIP host-used statics): DeclContext.
+  if (VD->isStaticLocal() && !LexicalBlockStack.empty()) {
----------------
chbessonova wrote:

@efriedma-quic Thanks for raising this. This `if` committed by #213153 originally came from 2016, lots changed since thien. You're right: nothing guarantees that the scope on top of the stack belongs to the static being emitted. Apart from the CUDA/HIP empty-stack cases, other examples look a bit weird, but I agree that making this guarantee explicit is a right way to go.

So a better fix would be to use the current lexical block only when the static's declaration is being emitted inside the function it belongs to and fall back to the declaration context, otherwise, as before #213153. This would handle the empty-stack cases as well.

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


More information about the cfe-commits mailing list