Skip to content

Commit 325003b

Browse files
committed
CodeView - add static data members to global variable debug info.
Summary: Add static data members to IR debug info's list of global variables so that they are emitted as S_CONSTANT records. Related to https://bugs.llvm.org/show_bug.cgi?id=41615. Reviewers: rnk Subscribers: aprantl, cfe-commits, llvm-commits, thakis Tags: #clang, #llvm Differential Revision: https://reviews.llvm.org/D62167 llvm-svn: 362038
1 parent 86bad3f commit 325003b

File tree

4 files changed

+79
-32
lines changed

4 files changed

+79
-32
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4361,9 +4361,13 @@ void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
43614361
return;
43624362
}
43634363

4364-
// Do not emit separate definitions for function local const/statics.
4364+
llvm::DIScope *DContext = nullptr;
4365+
4366+
// Do not emit separate definitions for function local consts.
43654367
if (isa<FunctionDecl>(VD->getDeclContext()))
43664368
return;
4369+
4370+
// Emit definition for static members in CodeView.
43674371
VD = cast<ValueDecl>(VD->getCanonicalDecl());
43684372
auto *VarD = cast<VarDecl>(VD);
43694373
if (VarD->isStaticDataMember()) {
@@ -4375,10 +4379,16 @@ void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
43754379
// through its scope.
43764380
RetainedTypes.push_back(
43774381
CGM.getContext().getRecordType(RD).getAsOpaquePtr());
4378-
return;
4379-
}
43804382

4381-
llvm::DIScope *DContext = getDeclContextDescriptor(VD);
4383+
if (!CGM.getCodeGenOpts().EmitCodeView)
4384+
return;
4385+
4386+
// Use the global scope for static members.
4387+
DContext = getContextDescriptor(
4388+
cast<Decl>(CGM.getContext().getTranslationUnitDecl()), TheCU);
4389+
} else {
4390+
DContext = getDeclContextDescriptor(VD);
4391+
}
43824392

43834393
auto &GV = DeclCache[VD];
43844394
if (GV)

clang/test/CodeGenCXX/debug-info-static-member.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// RUN: %clangxx -target x86_64-unknown-unknown -g %s -emit-llvm -S -o - | FileCheck %s
22
// RUN: %clangxx -target x86_64-unknown-unknown -g -std=c++98 %s -emit-llvm -S -o - | FileCheck %s
33
// RUN: %clangxx -target x86_64-unknown-unknown -g -std=c++11 %s -emit-llvm -S -o - | FileCheck %s
4+
// RUN: %clang_cc1 -triple x86_64-windows-msvc -gcodeview -debug-info-kind=limited %s -emit-llvm -o - | FileCheck --check-prefix MSVC %s
45
// PR14471
56

67
// CHECK: @_ZN1C1aE = dso_local global i32 4, align 4, !dbg [[A:![0-9]+]]
@@ -35,6 +36,7 @@ class C
3536
// CHECK: [[A]] = !DIGlobalVariableExpression(var: [[AV:.*]], expr: !DIExpression())
3637
// CHECK: [[AV]] = distinct !DIGlobalVariable(name: "a",
3738
// CHECK-SAME: declaration: ![[DECL_A:[0-9]+]])
39+
// MSVC: distinct !DIGlobalVariable(name: "a"
3840
//
3941
// CHECK: !DICompositeType(tag: DW_TAG_enumeration_type, name: "X"{{.*}}, identifier: "_ZTS1X")
4042
// CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "anon_static_decl_struct"
@@ -48,6 +50,7 @@ int C::a = 4;
4850
// CHECK: [[B]] = !DIGlobalVariableExpression(var: [[BV:.*]], expr: !DIExpression())
4951
// CHECK: [[BV]] = distinct !DIGlobalVariable(name: "b",
5052
// CHECK-SAME: declaration: ![[DECL_B:[0-9]+]])
53+
// MSVC: distinct !DIGlobalVariable(name: "b"
5154
// CHECK: ![[DECL_B]] = !DIDerivedType(tag: DW_TAG_member, name: "b"
5255
// CHECK-NOT: size:
5356
// CHECK-NOT: align:
@@ -95,6 +98,7 @@ int C::a = 4;
9598
int C::b = 2;
9699
// CHECK: [[C]] = !DIGlobalVariableExpression(var: [[CV:.*]], expr: !DIExpression())
97100
// CHECK: [[CV]] = distinct !DIGlobalVariable(name: "c", {{.*}} declaration: ![[DECL_C]])
101+
// MSVC: distinct !DIGlobalVariable(name: "c"
98102
int C::c = 1;
99103

100104
int main()
@@ -114,11 +118,18 @@ struct anon_static_decl_struct {
114118
};
115119
}
116120

117-
118121
int ref() {
119122
return anon_static_decl_struct::anon_static_decl_var;
120123
}
121124

125+
// In MSVC, static data members should be emitted as global variables when used.
126+
// MSVC: !DIGlobalVariableExpression(var: [[ANON_STATIC_DECL:![0-9]+]],
127+
// MSVC-SAME: !DIExpression(DW_OP_constu, 117, DW_OP_stack_value)
128+
// MSVC: [[ANON_STATIC_DECL]] = distinct !DIGlobalVariable(name: "anon_static_decl_var"
129+
// MSVC: !DIGlobalVariableExpression(var: [[STATIC_DECL_TEMPL:![0-9]+]]
130+
// MSVC-SAME: !DIExpression(DW_OP_constu, 7, DW_OP_stack_value)
131+
// MSVC: [[STATIC_DECL_TEMPL]] = distinct !DIGlobalVariable(name: "static_decl_templ_var"
132+
122133
template<typename T>
123134
struct static_decl_templ {
124135
static const int static_decl_templ_var = 7;

llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3069,7 +3069,12 @@ void CodeViewDebug::emitDebugInfoForGlobal(const CVGlobalVariable &CVGV) {
30693069
OS.EmitBinaryData(SRef);
30703070

30713071
OS.AddComment("Name");
3072-
emitNullTerminatedSymbolName(OS, DIGV->getDisplayName());
3072+
// Get fully qualified name if global is a static data member.
3073+
std::string Name = DIGV->getDisplayName();
3074+
if (const auto *MemberDecl = dyn_cast_or_null<DIDerivedType>(
3075+
DIGV->getRawStaticDataMemberDeclaration()))
3076+
Name = getFullyQualifiedName(MemberDecl->getScope(), Name);
3077+
emitNullTerminatedSymbolName(OS, Name);
30733078
endSymbolRecord(SConstantEnd);
30743079
}
30753080
}

llvm/test/DebugInfo/COFF/global-constants.ll

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,25 @@
33

44
; C++ source to regenerate:
55
; const int Test1 = 1;
6+
; struct Foo { static const int Test2 = 2; };
67
; int main() {
7-
; return Test1;
8+
; return Test1 + Foo::Test2;
89
; }
910
; $ clang t.cpp -S -emit-llvm -g -gcodeview -o t.ll
1011

11-
; ASM-LABEL: .long 241 # Symbol subsection for globals
12+
; ASM-LABEL: .long 241 # Symbol subsection for globals
1213

13-
; ASM: .short {{.*-.*}} # Record length
14-
; ASM: .short 4359 # Record kind: S_CONSTANT
15-
; ASM-NEXT: .long 4099 # Type
16-
; ASM-NEXT: .byte 0x01, 0x00 # Value
17-
; ASM-NEXT: .asciz "Test1" # Name
14+
; ASM: .short {{.*-.*}} # Record length
15+
; ASM: .short 4359 # Record kind: S_CONSTANT
16+
; ASM-NEXT: .long 4099 # Type
17+
; ASM-NEXT: .byte 0x01, 0x00 # Value
18+
; ASM-NEXT: .asciz "Test1" # Name
19+
20+
; ASM: .short {{.*-.*}} # Record length
21+
; ASM: .short 4359 # Record kind: S_CONSTANT
22+
; ASM: .long 4099 # Type
23+
; ASM: .byte 0x02, 0x00 # Value
24+
; ASM: .asciz "Foo::Test2" # Name
1825

1926
; OBJ: CodeViewDebugInfo [
2027
; OBJ: Section: .debug$S
@@ -27,38 +34,52 @@
2734
; OBJ-NEXT: Value: 1
2835
; OBJ-NEXT: Name: Test1
2936
; OBJ-NEXT: }
37+
; OBJ: ConstantSym {
38+
; OBJ-NEXT: Kind: S_CONSTANT (0x1107)
39+
; OBJ-NEXT: Type: const int (0x1003)
40+
; OBJ-NEXT: Value: 2
41+
; OBJ-NEXT: Name: Foo::Test2
42+
; OBJ-NEXT: }
3043

3144
; ModuleID = 't.cpp'
3245
source_filename = "t.cpp"
3346
target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
3447
target triple = "x86_64-pc-windows-msvc"
3548

3649
; Function Attrs: noinline norecurse nounwind optnone
37-
define dso_local i32 @main() #0 !dbg !13 {
50+
define dso_local i32 @main() #0 !dbg !19 {
3851
entry:
3952
%retval = alloca i32, align 4
4053
store i32 0, i32* %retval, align 4
41-
ret i32 1, !dbg !16
54+
ret i32 3, !dbg !22
4255
}
4356

57+
attributes #0 = { noinline norecurse nounwind optnone "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-features"="+cx8,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
58+
4459
!llvm.dbg.cu = !{!0}
45-
!llvm.module.flags = !{!9, !10, !11}
46-
!llvm.ident = !{!12}
60+
!llvm.module.flags = !{!15, !16, !17}
61+
!llvm.ident = !{!18}
4762

48-
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 9.0.0 (https://github.com/llvm/llvm-project.git 4a1902b6739e3087a03c0ac7ab85b640764e9335)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, globals: !3, nameTableKind: None)
49-
!1 = !DIFile(filename: "<stdin>", directory: "C:\5Csrc\5Ctest", checksumkind: CSK_MD5, checksum: "0d5ef00bdd80bdb409a3deac9938f20d")
63+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 9.0.0 (https://github.com/llvm/llvm-project.git 2b66a49044196d8b90d95d7d3b5246ccbe3abc05)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !3, globals: !10, nameTableKind: None)
64+
!1 = !DIFile(filename: "<stdin>", directory: "C:\5Csrc\5Ctest", checksumkind: CSK_MD5, checksum: "77cff5e1c7b260440ed03b23c18809c3")
5065
!2 = !{}
5166
!3 = !{!4}
52-
!4 = !DIGlobalVariableExpression(var: !5, expr: !DIExpression(DW_OP_constu, 1, DW_OP_stack_value))
53-
!5 = distinct !DIGlobalVariable(name: "Test1", scope: !0, file: !6, line: 1, type: !7, isLocal: true, isDefinition: true)
54-
!6 = !DIFile(filename: "t.cpp", directory: "C:\5Csrc\5Ctest", checksumkind: CSK_MD5, checksum: "0d5ef00bdd80bdb409a3deac9938f20d")
55-
!7 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !8)
56-
!8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
57-
!9 = !{i32 2, !"CodeView", i32 1}
58-
!10 = !{i32 2, !"Debug Info Version", i32 3}
59-
!11 = !{i32 1, !"wchar_size", i32 2}
60-
!12 = !{!"clang version 9.0.0 (https://github.com/llvm/llvm-project.git 4a1902b6739e3087a03c0ac7ab85b640764e9335)"}
61-
!13 = distinct !DISubprogram(name: "main", scope: !6, file: !6, line: 3, type: !14, scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
62-
!14 = !DISubroutineType(types: !15)
63-
!15 = !{!8}
64-
!16 = !DILocation(line: 4, scope: !13)
67+
!4 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "Foo", file: !5, line: 3, size: 8, flags: DIFlagTypePassByValue, elements: !6, identifier: ".?AUFoo@@")
68+
!5 = !DIFile(filename: "t.cpp", directory: "C:\5Csrc\5Ctest", checksumkind: CSK_MD5, checksum: "77cff5e1c7b260440ed03b23c18809c3")
69+
!6 = !{!7}
70+
!7 = !DIDerivedType(tag: DW_TAG_member, name: "Test2", scope: !4, file: !5, line: 4, baseType: !8, flags: DIFlagStaticMember, extraData: i32 2)
71+
!8 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !9)
72+
!9 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
73+
!10 = !{!11, !13}
74+
!11 = !DIGlobalVariableExpression(var: !12, expr: !DIExpression(DW_OP_constu, 1, DW_OP_stack_value))
75+
!12 = distinct !DIGlobalVariable(name: "Test1", scope: null, file: !5, line: 1, type: !8, isLocal: true, isDefinition: true)
76+
!13 = !DIGlobalVariableExpression(var: !14, expr: !DIExpression(DW_OP_constu, 2, DW_OP_stack_value))
77+
!14 = distinct !DIGlobalVariable(name: "Test2", scope: !0, file: !5, line: 4, type: !8, isLocal: true, isDefinition: true, declaration: !7)
78+
!15 = !{i32 2, !"CodeView", i32 1}
79+
!16 = !{i32 2, !"Debug Info Version", i32 3}
80+
!17 = !{i32 1, !"wchar_size", i32 2}
81+
!18 = !{!"clang version 9.0.0 (https://github.com/llvm/llvm-project.git 2b66a49044196d8b90d95d7d3b5246ccbe3abc05)"}
82+
!19 = distinct !DISubprogram(name: "main", scope: !5, file: !5, line: 7, type: !20, scopeLine: 7, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
83+
!20 = !DISubroutineType(types: !21)
84+
!21 = !{!9}
85+
!22 = !DILocation(line: 8, scope: !19)

0 commit comments

Comments
 (0)