diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/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). diff --git a/clang/test/CodeGenCXX/debug-info-static-locals.cpp b/clang/test/CodeGenCXX/debug-info-static-locals.cpp new file mode 100644 --- /dev/null +++ b/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]]}