llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Longjun Luo (anatasluo)

<details>
<summary>Changes</summary>

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 -&gt; sibling blocks
- Declaration in a constant-folded arm -&gt; a block under the condition

Assisted-by: OpenAI Codex


---
Full diff: https://github.com/llvm/llvm-project/pull/243767.diff


2 Files Affected:

- (modified) clang/lib/CodeGen/CGStmt.cpp (+20-14) 
- (modified) clang/test/DebugInfo/X86/debug-info-static-locals.cpp (+17-7) 


``````````diff
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]],

``````````

</details>


https://github.com/llvm/llvm-project/pull/243767
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to