[clang] [llvm] [clang][DebugInfo] Allow function-local statics to be scoped within a lexical block (PR #213153)
Orlando Cazalet-Hyams via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 3 02:50:25 PDT 2026
================
@@ -0,0 +1,56 @@
+// RUN: %clang_cc1 -triple x86_64-none-linux-gnu -emit-llvm -debug-info-kind=limited %s -o - -O0 | FileCheck %s
+
+// Test that static local variables are emitted in correct scopes.
+
+void test() {
+ static int bar = 2;
+ {
+ static int bar = 1;
+ {
+ static int bar = 0;
+ }
+ }
+}
+
+// Automatic local variables in braceless arms of
+// if statement are placed in the same DILexicalBlock.
+// Test that the behavior for static locals is the same as
+// for automatic variables in that case.
+void test_braceless(int x) {
+ if (x)
+ static int foo = 30;
+ else
+ static int foo = 40;
+ if (x)
+#line 100
+ int foobar = 50;
+ else
+#line 200
+ int foobar = 60;
+}
+
+// CHECK: ![[FS_GVE:[0-9]+]] = !DIGlobalVariableExpression(var: ![[FS_GV:[0-9]+]]
+// CHECK: ![[FS_GV]] = distinct !DIGlobalVariable(name: "bar", scope: ![[FSCOPE:[0-9]+]]
+// CHECK: ![[FSCOPE]] = distinct !DISubprogram(name: "test"
+// CHECK-SAME: retainedNodes: ![[FS_DECLS:[0-9]+]]
+// CHECK: ![[FS_DECLS]] = !{![[FS_GVE]], ![[LB1_GVE:[0-9]+]], ![[LB2_GVE:[0-9]+]]}
+// CHECK: ![[LB1_GVE]] = !DIGlobalVariableExpression(var: ![[LB1_GV:[0-9]+]]
+// CHECK: ![[LB1_GV]] = distinct !DIGlobalVariable(name: "bar", scope: ![[LB1SCOPE:[0-9]+]]
+// CHECK: ![[LB1SCOPE]] = distinct !DILexicalBlock(scope: ![[FSCOPE]]
+// CHECK: ![[LB2_GVE]] = !DIGlobalVariableExpression(var: ![[LB2_GV:[0-9]+]]
+// CHECK: ![[LB2_GV]] = distinct !DIGlobalVariable(name: "bar", scope: ![[LB2SCOPE:[0-9]+]]
+// CHECK: ![[LB2SCOPE]] = distinct !DILexicalBlock(scope: ![[LB1SCOPE]]
+
+// CHECK: ![[B_LB1_GVE:[0-9]+]] = !DIGlobalVariableExpression(var: ![[B_LB1_GV:[0-9]+]]
+// CHECK: ![[B_LB1_GV]] = distinct !DIGlobalVariable(name: "foo", scope: ![[B_LBSCOPE:[0-9]+]]
----------------
OCHyams wrote:
Shouldn't there be two "foo" DIGlobalVariables?
https://github.com/llvm/llvm-project/pull/213153
More information about the cfe-commits
mailing list