Skip to content

Commit 53abbce

Browse files
authored
[DebugInfo] Correct the line attribution for IF branches (#108300)
An 'if' statement introduces a scope, but in some cases the conditional branch to the then/else blocks had a debug-info attribution that did not include the scope. This led to some inefficiency in the DWARF line table.
1 parent be0b114 commit 53abbce

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

clang/lib/CodeGen/CGStmt.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,7 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
815815
// C99 6.8.4.1: The first substatement is executed if the expression compares
816816
// unequal to 0. The condition must be a scalar type.
817817
LexicalScope ConditionScope(*this, S.getCond()->getSourceRange());
818+
ApplyDebugLocation DL(*this, S.getCond());
818819

819820
if (S.getInit())
820821
EmitStmt(S.getInit());
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// RUN: %clang_cc1 -debug-info-kind=limited -gno-column-info -triple=x86_64-pc-linux -emit-llvm %s -o - | FileCheck %s
2+
3+
// The important thing is that the compare and the conditional branch have
4+
// locs with the same scope (the lexical block for the 'if'). By turning off
5+
// column info, they end up with the same !dbg record, which halves the number
6+
// of checks to verify the scope.
7+
8+
int c = 2;
9+
10+
int f() {
11+
#line 100
12+
if (int a = 5; a > c)
13+
return 1;
14+
return 0;
15+
}
16+
// CHECK-LABEL: define {{.*}} @_Z1fv()
17+
// CHECK: = icmp {{.*}} !dbg [[F_CMP:![0-9]+]]
18+
// CHECK-NEXT: br i1 {{.*}} !dbg [[F_CMP]]
19+
20+
int g() {
21+
#line 200
22+
if (int a = f())
23+
return 2;
24+
return 3;
25+
}
26+
// CHECK-LABEL: define {{.*}} @_Z1gv()
27+
// CHECK: = icmp {{.*}} !dbg [[G_CMP:![0-9]+]]
28+
// CHECK-NEXT: br i1 {{.*}} !dbg [[G_CMP]]
29+
30+
int h() {
31+
#line 300
32+
if (c > 3)
33+
return 4;
34+
return 5;
35+
}
36+
// CHECK-LABEL: define {{.*}} @_Z1hv()
37+
// CHECK: = icmp {{.*}} !dbg [[H_CMP:![0-9]+]]
38+
// CHECK-NEXT: br i1 {{.*}} !dbg [[H_CMP]]
39+
40+
// CHECK-DAG: [[F_CMP]] = !DILocation(line: 100, scope: [[F_SCOPE:![0-9]+]]
41+
// CHECK-DAG: [[F_SCOPE]] = distinct !DILexicalBlock({{.*}} line: 100)
42+
// CHECK-DAG: [[G_CMP]] = !DILocation(line: 200, scope: [[G_SCOPE:![0-9]+]]
43+
// CHECK-DAG: [[G_SCOPE]] = distinct !DILexicalBlock({{.*}} line: 200)
44+
// CHECK-DAG: [[H_CMP]] = !DILocation(line: 300, scope: [[H_SCOPE:![0-9]+]]
45+
// CHECK-DAG: [[H_SCOPE]] = distinct !DILexicalBlock({{.*}} line: 300)

0 commit comments

Comments
 (0)