[PATCH] D125694: [clang][DebugInfo] Allow function-local statics to be scoped within a lexical block (4/5)

Kristina Bessonova via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon May 16 08:42:27 PDT 2022


krisb created this revision.
krisb added reviewers: dblaikie, aprantl, probinson.
Herald added a project: All.
krisb requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This is a part from D113743 <https://reviews.llvm.org/D113743> split to make it easier to test and review.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D125694

Files:
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/test/CodeGenCXX/debug-info-static-locals.cpp


Index: clang/test/CodeGenCXX/debug-info-static-locals.cpp
===================================================================
--- /dev/null
+++ clang/test/CodeGenCXX/debug-info-static-locals.cpp
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -triple x86_64-none-linux-gnu -emit-llvm -debug-info-kind=limited %s -o - | FileCheck %s
+
+// Test that static local variables emitted in correct scopes.
+
+void test() {
+  static int bar = 2;
+  {
+    static int bar = 1;
+    {
+      static int bar = 0;
+    }
+  }
+}
+
+// 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:                                localDecls: [[FS_DECLS:![0-9]+]]
+// CHECK: [[FS_DECLS]] = !{[[FS_GVE]]}
+// CHECK: [[LB1_GVE:![0-9]+]] = !DIGlobalVariableExpression(var: [[LB1_GV:![0-9]+]]
+// CHECK: [[LB1_GV]] = distinct !DIGlobalVariable(name: "bar", scope: [[LB1SCOPE:![0-9]+]]
+// CHECK: [[LB1SCOPE]] = distinct !DILexicalBlock(scope: [[FSCOPE]]
+// CHECK-SAME:                                    localDecls: [[LB1_DECLS:![0-9]+]]
+// CHECK: [[LB1_DECLS]] = !{[[LB1_GVE]]}
+// CHECK: [[LB2_GVE:![0-9]+]] = !DIGlobalVariableExpression(var: [[LB2_GV:![0-9]+]]
+// CHECK: [[LB2_GV]] = distinct !DIGlobalVariable(name: "bar", scope: [[LB2SCOPE:![0-9]+]]
+// CHECK: [[LB2SCOPE]] = distinct !DILexicalBlock(scope: [[LB1SCOPE]]
+// CHECK-SAME:                                    localDecls: [[LB2_DECLS:![0-9]+]]
+// CHECK: [[LB2_DECLS]] = !{[[LB2_GVE]]}
Index: clang/lib/CodeGen/CGDebugInfo.cpp
===================================================================
--- clang/lib/CodeGen/CGDebugInfo.cpp
+++ clang/lib/CodeGen/CGDebugInfo.cpp
@@ -3746,6 +3746,14 @@
     TemplateParameters = nullptr;
   }
 
+  // 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!");
+    VDContext = LexicalBlockStack.back();
+    return;
+  }
+
   // Since we emit declarations (DW_AT_members) for static members, place the
   // definition of those static members in the namespace they were declared in
   // in the source code (the lexical decl context).


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125694.429728.patch
Type: text/x-patch
Size: 2391 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220516/6038bd6b/attachment.bin>


More information about the cfe-commits mailing list