https://github.com/anatasluo updated https://github.com/llvm/llvm-project/pull/243767
>From b1850055f79b2d32579d5b00b05664ccc1c34dc9 Mon Sep 17 00:00:00 2001 From: Longjun Luo <[email protected]> Date: Wed, 18 Nov 2026 03:48:44 +0000 Subject: [PATCH 1/2] [clang][DebugInfo] Preserve scopes for declarations in unbraced if arms GDB can resolve a static local in an unbraced else arm to the variable of the same name in the then arm. Clang uses the current lexical block for static locals (#213153). For both declarations, that block is the condition's debug scope. Give an unbraced declaration its own lexical block to match the scope of an if substatement in C++ [stmt.select]. Extend the existing static-locals debug info test to cover both cases. - Static and automatic variables in opposite arms -> sibling blocks - Declaration in a constant-folded arm -> a block under the condition Assisted-by: OpenAI Codex Signed-off-by: Longjun Luo <[email protected]> --- clang/lib/CodeGen/CGStmt.cpp | 34 +++++++++++-------- .../X86/debug-info-static-locals.cpp | 24 +++++++++---- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp index 455c4352661043..a8c0e4fbc8d5b9 100644 --- a/clang/lib/CodeGen/CGStmt.cpp +++ b/clang/lib/CodeGen/CGStmt.cpp @@ -904,14 +904,27 @@ void CodeGenFunction::EmitIndirectGotoStmt(const IndirectGotoStmt &S) { void CodeGenFunction::EmitIfStmt(const IfStmt &S) { const Stmt *Else = S.getElse(); + auto EmitArm = [this](const Stmt *Arm) { + RunCleanupsScope Scope(*this); + CGDebugInfo *DI = getDebugInfo(); + + // A compound statement already has its own lexical block, so only add + // one here for an unbraced declaration. + bool EmitDebugScope = DI && CGM.getCodeGenOpts().hasReducedDebugInfo() && + isa<DeclStmt>(Arm->stripLabelLikeStatements()); + if (EmitDebugScope) + DI->EmitLexicalBlockStart(Builder, Arm->getBeginLoc()); + EmitStmt(Arm); + if (EmitDebugScope) + DI->EmitLexicalBlockEnd(Builder, Arm->getEndLoc()); + }; + // The else branch of a consteval if statement is always the only branch that // can be runtime evaluated. if (S.isConsteval()) { const Stmt *Executed = S.isNegatedConsteval() ? S.getThen() : Else; - if (Executed) { - RunCleanupsScope ExecutedScope(*this); - EmitStmt(Executed); - } + if (Executed) + EmitArm(Executed); return; } @@ -949,8 +962,7 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) { /*UseBoth=*/true); if (Executed) { MaybeEmitDeferredVarDeclInit(S.getConditionVariable()); - RunCleanupsScope ExecutedScope(*this); - EmitStmt(Executed); + EmitArm(Executed); } PGO->markStmtMaybeUsed(Skipped); return; @@ -1001,10 +1013,7 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) { // Emit the 'then' code. EmitBlock(ThenBlock); incrementProfileCounter(UseExecPath, &S); - { - RunCleanupsScope ThenScope(*this); - EmitStmt(S.getThen()); - } + EmitArm(S.getThen()); EmitBranch(ContBlock); // Emit the 'else' code if present. @@ -1017,10 +1026,7 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) { // Add a counter to else block unless it has CounterExpr. if (HasSkip) incrementProfileCounter(UseSkipPath, &S); - { - RunCleanupsScope ElseScope(*this); - EmitStmt(Else); - } + EmitArm(Else); { // There is no need to emit line number for an unconditional branch. auto NL = ApplyDebugLocation::CreateEmpty(*this); diff --git a/clang/test/DebugInfo/X86/debug-info-static-locals.cpp b/clang/test/DebugInfo/X86/debug-info-static-locals.cpp index 208e845c664f4e..bd1d82ef43b9ed 100644 --- a/clang/test/DebugInfo/X86/debug-info-static-locals.cpp +++ b/clang/test/DebugInfo/X86/debug-info-static-locals.cpp @@ -13,7 +13,7 @@ void test() { } // Automatic local variables in braceless arms of -// if statement are placed in the same DILexicalBlock. +// an if statement are placed in separate DILexicalBlocks. // Test that the behavior for static locals is the same as // for automatic variables in that case. void test_braceless(int x) { @@ -27,6 +27,8 @@ void test_braceless(int x) { else #line 200 int foobar = 60; + if (true) + int folded = x; } // CHECK: ![[FS_GVE:[0-9]+]] = !DIGlobalVariableExpression(var: ![[FS_GV:[0-9]+]] @@ -42,16 +44,24 @@ void test_braceless(int x) { // 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]+]] -// CHECK: ![[B_LBSCOPE]] = distinct !DILexicalBlock(scope: ![[BSCOPE:[0-9]+]] +// CHECK: ![[B_LB1_GV]] = distinct !DIGlobalVariable(name: "foo", scope: ![[THEN_SCOPE:[0-9]+]], +// CHECK: ![[THEN_SCOPE]] = distinct !DILexicalBlock(scope: ![[STATIC_IF:[0-9]+]], +// CHECK: ![[STATIC_IF]] = distinct !DILexicalBlock(scope: ![[BSCOPE:[0-9]+]], // CHECK: ![[BSCOPE]] = distinct !DISubprogram(name: "test_braceless" // CHECK-SAME: retainedNodes: ![[B_DECLS:[0-9]+]] // CHECK: ![[B_DECLS]] = !{![[B_LB1_GVE]], ![[B_LB2_GVE:[0-9]+]]} // CHECK: ![[B_LB2_GVE]] = !DIGlobalVariableExpression(var: ![[B_LB2_GV:[0-9]+]] -// CHECK: ![[B_LB2_GV]] = distinct !DIGlobalVariable(name: "foo", scope: ![[B_LBSCOPE:[0-9]+]] +// CHECK: ![[B_LB2_GV]] = distinct !DIGlobalVariable(name: "foo", scope: ![[ELSE_SCOPE:[0-9]+]], +// CHECK: ![[ELSE_SCOPE]] = distinct !DILexicalBlock(scope: ![[STATIC_IF]], -// CHECK: ![[LOCAL_VAR_SCOPE:[0-9]+]] = distinct !DILexicalBlock(scope: ![[BSCOPE]] -// CHECK: ![[LOCAL_VAR1:[0-9]+]] = !DILocalVariable(name: "foobar", scope: ![[LOCAL_VAR_SCOPE]] +// CHECK: ![[AUTO_IF:[0-9]+]] = distinct !DILexicalBlock(scope: ![[BSCOPE]], +// CHECK: !DILocalVariable(name: "foobar", scope: ![[AUTO_THEN:[0-9]+]], // CHECK-SAME: line: 100 -// CHECK: ![[LOCAL_VAR2:[0-9]+]] = !DILocalVariable(name: "foobar", scope: ![[LOCAL_VAR_SCOPE]] +// CHECK: ![[AUTO_THEN]] = distinct !DILexicalBlock(scope: ![[AUTO_IF]], +// CHECK: !DILocalVariable(name: "foobar", scope: ![[AUTO_ELSE:[0-9]+]], // CHECK-SAME: line: 200 +// CHECK: ![[AUTO_ELSE]] = distinct !DILexicalBlock(scope: ![[AUTO_IF]], + +// CHECK: !DILocalVariable(name: "folded", scope: ![[FOLDED_ARM:[0-9]+]], +// CHECK: ![[FOLDED_ARM]] = distinct !DILexicalBlock(scope: ![[FOLDED_IF:[0-9]+]], +// CHECK: ![[FOLDED_IF]] = distinct !DILexicalBlock(scope: ![[BSCOPE]], >From c8e259ff4fd0ea91f788a96cf32ec47adede3660 Mon Sep 17 00:00:00 2001 From: Longjun Luo <[email protected]> Date: Thu, 19 Nov 2026 02:53:08 +0000 Subject: [PATCH 2/2] [clang][DebugInfo] Align debug level checks for lexical blocks (NFC) - Use hasReducedDebugInfo() in EmitLexicalBlockStart and EmitLexicalBlockEnd. - Cover a labeled declaration in an unbraced else arm. Signed-off-by: Longjun Luo <[email protected]> --- clang/lib/CodeGen/CGDebugInfo.cpp | 4 ++-- clang/test/DebugInfo/X86/debug-info-static-locals.cpp | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index bc516406269c2c..3261369fa6ccb8 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -5399,7 +5399,7 @@ void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder, CGM.getLLVMContext(), getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back(), CurInlinedAt)); - if (DebugKind <= llvm::codegenoptions::DebugLineTablesOnly) + if (!CGM.getCodeGenOpts().hasReducedDebugInfo()) return; // Create a new lexical block and push it on the stack. @@ -5413,7 +5413,7 @@ void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder, // Provide an entry in the line table for the end of the block. EmitLocation(Builder, Loc); - if (DebugKind <= llvm::codegenoptions::DebugLineTablesOnly) + if (!CGM.getCodeGenOpts().hasReducedDebugInfo()) return; LexicalBlockStack.pop_back(); diff --git a/clang/test/DebugInfo/X86/debug-info-static-locals.cpp b/clang/test/DebugInfo/X86/debug-info-static-locals.cpp index bd1d82ef43b9ed..9d757e39f7c070 100644 --- a/clang/test/DebugInfo/X86/debug-info-static-locals.cpp +++ b/clang/test/DebugInfo/X86/debug-info-static-locals.cpp @@ -20,6 +20,7 @@ void test_braceless(int x) { if (x) static int foo = 30; else + arm_label: static int foo = 40; if (x) #line 100 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
