[clang] [clang][CodeGen] Drop debug location on the if-then exit branch (PR #212942)

Jim Lin via cfe-commits cfe-commits at lists.llvm.org
Sun Aug 2 17:57:13 PDT 2026


https://github.com/tclin914 updated https://github.com/llvm/llvm-project/pull/212942

>From bd2615f602ed0a21fbda11339cc7e5a733d9b01f Mon Sep 17 00:00:00 2001
From: Jim Lin <jim at andestech.com>
Date: Tue, 28 Jul 2026 13:55:01 +0800
Subject: [PATCH 1/2] [clang][CodeGen] Drop debug location on the if-then exit
 branch

In EmitIfStmt, the branch from the 'then' block to the continuation
block inherited the debug location of the block's closing brace, adding
a spurious is_stmt line-table entry on the '}'. Guard it with
ApplyDebugLocation::CreateEmpty, as the 'else' branch already is.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply at anthropic.com>
---
 clang/lib/CodeGen/CGStmt.cpp | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index 27e74d966eca1..f937f739cc681 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -975,7 +975,11 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
     RunCleanupsScope ThenScope(*this);
     EmitStmt(S.getThen());
   }
-  EmitBranch(ContBlock);
+  {
+    // There is no need to emit line number for an unconditional branch.
+    auto NL = ApplyDebugLocation::CreateEmpty(*this);
+    EmitBranch(ContBlock);
+  }
 
   // Emit the 'else' code if present.
   if (Else) {

>From 1e4166b9f0849ef169e8e0792b7676e4150a6888 Mon Sep 17 00:00:00 2001
From: Jim Lin <jim at andestech.com>
Date: Fri, 31 Jul 2026 09:31:32 +0800
Subject: [PATCH 2/2] Add testcase

---
 .../DebugInfo/Generic/if-then-exit-branch.c   | 34 +++++++++++++++++++
 1 file changed, 34 insertions(+)
 create mode 100644 clang/test/DebugInfo/Generic/if-then-exit-branch.c

diff --git a/clang/test/DebugInfo/Generic/if-then-exit-branch.c b/clang/test/DebugInfo/Generic/if-then-exit-branch.c
new file mode 100644
index 0000000000000..260bee7064ade
--- /dev/null
+++ b/clang/test/DebugInfo/Generic/if-then-exit-branch.c
@@ -0,0 +1,34 @@
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -debug-info-kind=line-tables-only %s -o - | FileCheck %s
+
+// The unconditional branch from the 'then' (and 'else') block to the
+// continuation block is not a statement, so it must not carry a debug
+// location.
+
+int g;
+
+// CHECK-LABEL: define {{.*}}@no_else(
+// CHECK:       if.then:
+// CHECK:         store i32 1, ptr @g{{.*}}, !dbg
+// CHECK-NEXT:    br label %if.end{{$}}
+void no_else(int a) {
+  if (a) {
+    g = 1;
+  }
+  g = 2;
+}
+
+// CHECK-LABEL: define {{.*}}@with_else(
+// CHECK:       if.then:
+// CHECK:         store i32 1, ptr @g{{.*}}, !dbg
+// CHECK-NEXT:    br label %if.end{{$}}
+// CHECK:       if.else:
+// CHECK:         store i32 2, ptr @g{{.*}}, !dbg
+// CHECK-NEXT:    br label %if.end{{$}}
+void with_else(int a) {
+  if (a) {
+    g = 1;
+  } else {
+    g = 2;
+  }
+  g = 3;
+}



More information about the cfe-commits mailing list