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

via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 9 14:49:00 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Yaxun (Sam) Liu (yxsamliu)

<details>
<summary>Changes</summary>

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.


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


3 Files Affected:

- (modified) clang/lib/CodeGen/CGBuiltin.cpp (+7-1) 
- (modified) clang/test/DebugInfo/CXX/verbose-trap.cpp (+4-4) 
- (added) clang/test/DebugInfo/Generic/verbose-trap-line.c (+12) 


``````````diff
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");
+}

``````````

</details>


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


More information about the cfe-commits mailing list