[clang] [clang][DebugInfo] Fix verbose trap source line (PR #222456)

Yaxun Liu via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 9 18:16:31 PDT 2026


https://github.com/yxsamliu updated https://github.com/llvm/llvm-project/pull/222456

>From e0f76162e57627fcc98b3e28b36830ef8975a884 Mon Sep 17 00:00:00 2001
From: "Yaxun (Sam) Liu" <yaxun.liu at amd.com>
Date: Wed, 9 Sep 2026 14:57:47 -0400
Subject: [PATCH 1/2] [clang][DebugInfo] Fix verbose trap source line

The artificial inline location for `__builtin_verbose_trap` used line
zero. As a result, the emitted trap could inherit the preceding source
line in DWARF line tables.

Keep the artificial trap-message frame while assigning the builtin
call's line and column to the trap instruction.
---
 clang/lib/CodeGen/CGBuiltin.cpp                  |  8 +++++++-
 clang/test/DebugInfo/CXX/verbose-trap.cpp        |  8 ++++----
 clang/test/DebugInfo/Generic/verbose-trap-line.c | 12 ++++++++++++
 3 files changed, 23 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/DebugInfo/Generic/verbose-trap-line.c

diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index f4598a8a54e1b..1e65a6a30c35e 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -4146,11 +4146,17 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
     EmitTrapCall(Intrinsic::trap);
     return RValue::get(nullptr);
   case Builtin::BI__builtin_verbose_trap: {
-    llvm::DILocation *TrapLocation = Builder.getCurrentDebugLocation();
+    llvm::DebugLoc CallLocation = Builder.getCurrentDebugLocation();
+    llvm::DILocation *TrapLocation = CallLocation;
     if (getDebugInfo()) {
       TrapLocation = getDebugInfo()->CreateTrapFailureMessageFor(
           TrapLocation, *E->getArg(0)->tryEvaluateString(getContext()),
           *E->getArg(1)->tryEvaluateString(getContext()));
+      // Keep the trap on the builtin's source line. A line-zero location would
+      // leave the trap attributed to the preceding line in the line table.
+      TrapLocation = llvm::DILocation::get(
+          getLLVMContext(), CallLocation.getLine(), CallLocation.getCol(),
+          TrapLocation->getScope(), TrapLocation->getInlinedAt());
     }
     ApplyDebugLocation ApplyTrapDI(*this, TrapLocation);
     // Currently no attempt is made to prevent traps from being merged.
diff --git a/clang/test/DebugInfo/CXX/verbose-trap.cpp b/clang/test/DebugInfo/CXX/verbose-trap.cpp
index af5bd4119532a..bae945ad4fda4 100644
--- a/clang/test/DebugInfo/CXX/verbose-trap.cpp
+++ b/clang/test/DebugInfo/CXX/verbose-trap.cpp
@@ -25,7 +25,7 @@ char const constCat[] = "category2";
 char const constMsg[] = "hello";
 
 // CHECK: ![[SUBPROG14:.*]] = distinct !DISubprogram(name: "f0", linkageName: "_Z2f0v",
-// CHECK: ![[LOC17]] = !DILocation(line: 0, scope: ![[SUBPROG18:.*]], inlinedAt: ![[LOC20:.*]])
+// CHECK: ![[LOC17]] = !DILocation(line: [[@LINE+4]], column: 3, scope: ![[SUBPROG18:.*]], inlinedAt: ![[LOC20:.*]])
 // CHECK: ![[SUBPROG18]] = distinct !DISubprogram(name: "__clang_trap_msg$category1$Argument_must_not_be_null", scope: ![[FILESCOPE]], file: ![[FILESCOPE]], type: !{{.*}}, flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: !{{.*}})
 // CHECK: ![[LOC20]] = !DILocation(line: [[@LINE+2]], column: 3, scope: ![[SUBPROG14]])
 void f0() {
@@ -33,10 +33,10 @@ void f0() {
 }
 
 // CHECK: ![[SUBPROG22:.*]] = distinct !DISubprogram(name: "f1", linkageName: "_Z2f1v",
-// CHECK: ![[LOC23]] = !DILocation(line: 0, scope: ![[SUBPROG18]], inlinedAt: ![[LOC24:.*]])
+// CHECK: ![[LOC23]] = !DILocation(line: [[@LINE+7]], column: 3, scope: ![[SUBPROG18]], inlinedAt: ![[LOC24:.*]])
 // CHECK: ![[LOC24]] = !DILocation(line: [[@LINE+6]], column: 3, scope: ![[SUBPROG22]])
 // CHECK: ![[SUBPROG_F1B:.*]] = distinct !DISubprogram(name: "f1_b", linkageName: "_Z4f1_bv",
-// CHECK: ![[LOC25]] = !DILocation(line: 0, scope: ![[SUBPROG26:.*]], inlinedAt: ![[LOC27:.*]])
+// CHECK: ![[LOC25]] = !DILocation(line: [[@LINE+7]], column: 3, scope: ![[SUBPROG26:.*]], inlinedAt: ![[LOC27:.*]])
 // CHECK: ![[SUBPROG26]] = distinct !DISubprogram(name: "__clang_trap_msg$category2$hello", scope: ![[FILESCOPE]], file: ![[FILESCOPE]], type: !{{.*}}, flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: !{{.*}})
 // CHECK: ![[LOC27]] = !DILocation(line: [[@LINE+5]], column: 3, scope: ![[SUBPROG_F1B]])
 void f1() {
@@ -47,7 +47,7 @@ void f1_b() {
 }
 
 // CHECK: ![[SUBPROG32:.*]] = distinct !DISubprogram(name: "f2<constCat, constMsg>", linkageName: "_Z2f2IXadsoKcL_ZL8constCatEEEXadsoS0_L_ZL8constMsgEEEEvv",
-// CHECK: ![[LOC36]] = !DILocation(line: 0, scope: ![[SUBPROG26]], inlinedAt: ![[LOC37:.*]])
+// CHECK: ![[LOC36]] = !DILocation(line: [[@LINE+4]], column: 3, scope: ![[SUBPROG26]], inlinedAt: ![[LOC37:.*]])
 // CHECK: ![[LOC37]] = !DILocation(line: [[@LINE+3]], column: 3, scope: ![[SUBPROG32]])
 template <const char * const category, const char * const reason>
 void f2() {
diff --git a/clang/test/DebugInfo/Generic/verbose-trap-line.c b/clang/test/DebugInfo/Generic/verbose-trap-line.c
new file mode 100644
index 0000000000000..8fcef6c9fd051
--- /dev/null
+++ b/clang/test/DebugInfo/Generic/verbose-trap-line.c
@@ -0,0 +1,12 @@
+// REQUIRES: x86-registered-target
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-obj -debug-info-kind=limited %s -o %t
+// RUN: llvm-objdump -d -l %t | FileCheck %s
+
+void test_trap_function(void) {
+  int x = 1;
+
+// CHECK-LABEL: <test_trap_function>:
+// CHECK: verbose-trap-line.c:[[# @LINE+2]]
+// CHECK-NEXT: {{.*}}ud2
+  __builtin_verbose_trap("category", "message");
+}

>From fae22e313df60a8319c36f9b72c4db5aa6d427b1 Mon Sep 17 00:00:00 2001
From: "Yaxun (Sam) Liu" <yaxun.liu at amd.com>
Date: Wed, 9 Sep 2026 21:15:51 -0400
Subject: [PATCH 2/2] Remove redundant verbose trap object test

---
 clang/test/DebugInfo/Generic/verbose-trap-line.c | 12 ------------
 1 file changed, 12 deletions(-)
 delete mode 100644 clang/test/DebugInfo/Generic/verbose-trap-line.c

diff --git a/clang/test/DebugInfo/Generic/verbose-trap-line.c b/clang/test/DebugInfo/Generic/verbose-trap-line.c
deleted file mode 100644
index 8fcef6c9fd051..0000000000000
--- a/clang/test/DebugInfo/Generic/verbose-trap-line.c
+++ /dev/null
@@ -1,12 +0,0 @@
-// REQUIRES: x86-registered-target
-// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-obj -debug-info-kind=limited %s -o %t
-// RUN: llvm-objdump -d -l %t | FileCheck %s
-
-void test_trap_function(void) {
-  int x = 1;
-
-// CHECK-LABEL: <test_trap_function>:
-// CHECK: verbose-trap-line.c:[[# @LINE+2]]
-// CHECK-NEXT: {{.*}}ud2
-  __builtin_verbose_trap("category", "message");
-}



More information about the cfe-commits mailing list