Skip to content

Commit 7f3ee3c

Browse files
authored
[clang][DebugInfo] Revert to attaching DW_AT_const_value on static member declarations (#73626)
In #71780 we started emitting definitions for all static data-members with constant initialisers, even if they were constants (i.e., didn't have a location). We also dropped the DW_AT_const_value from the declaration to [help resolve inconsistencies during type merging in the DWARFParallelLinker](#68721). However, for static data members that do have locations, we wouldn't emit a DW_AT_const_value on it, assuming that the consumer knows how to read the value using the location. This broke some consumers that really wanted to find a DW_AT_const_value. Ultimately we want to attach a DW_AT_const_value to definitions that have a location too. But to fix consumers broken by said change, this patch adds the constant back onto the declaration. This is what we used to do prior to #71780
1 parent f3a9dbe commit 7f3ee3c

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

+15-3
Original file line numberDiff line numberDiff line change
@@ -1680,14 +1680,26 @@ CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
16801680
unsigned LineNumber = getLineNumber(Var->getLocation());
16811681
StringRef VName = Var->getName();
16821682

1683+
// FIXME: to avoid complications with type merging we should
1684+
// emit the constant on the definition instead of the declaration.
1685+
llvm::Constant *C = nullptr;
1686+
if (Var->getInit()) {
1687+
const APValue *Value = Var->evaluateValue();
1688+
if (Value) {
1689+
if (Value->isInt())
1690+
C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
1691+
if (Value->isFloat())
1692+
C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
1693+
}
1694+
}
1695+
16831696
llvm::DINode::DIFlags Flags = getAccessFlag(Var->getAccess(), RD);
16841697
auto Tag = CGM.getCodeGenOpts().DwarfVersion >= 5
16851698
? llvm::dwarf::DW_TAG_variable
16861699
: llvm::dwarf::DW_TAG_member;
16871700
auto Align = getDeclAlignIfRequired(Var, CGM.getContext());
1688-
llvm::DIDerivedType *GV =
1689-
DBuilder.createStaticMemberType(RecordTy, VName, VUnit, LineNumber, VTy,
1690-
Flags, /* Val */ nullptr, Tag, Align);
1701+
llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
1702+
RecordTy, VName, VUnit, LineNumber, VTy, Flags, C, Tag, Align);
16911703
StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
16921704
StaticDataMemberDefinitionsToEmit.push_back(Var->getCanonicalDecl());
16931705
return GV;

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -49,35 +49,35 @@ int main() {
4949

5050
// CHECK: ![[INT_DECL]] = !DIDerivedType(tag: DW_TAG_member, name: "cexpr_int_with_addr",
5151
// CHECK-SAME: flags: DIFlagStaticMember
52-
// CHECK-NOT: extraData:
52+
// CHECK-SAME: extraData: i32 25
5353

5454
// CHECK: ![[INT_DECL2:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "cexpr_int2",
5555
// CHECK-SAME: flags: DIFlagStaticMember
56-
// CHECK-NOT: extraData:
56+
// CHECK-SAME: extraData: i32 26
5757

5858
// CHECK: ![[FLOAT_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "cexpr_float",
5959
// CHECK-SAME: flags: DIFlagStaticMember
60-
// CHECK-NOT: extraData:
60+
// CHECK-SAME: extraData: float
6161

6262
// CHECK: ![[ENUM_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "cexpr_enum",
6363
// CHECK-SAME: flags: DIFlagStaticMember
64-
// CHECK-NOT: extraData:
64+
// CHECK-SAME: extraData: i32 -1
6565

6666
// CHECK: ![[EMPTY_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "cexpr_struct_with_addr",
6767
// CHECK-SAME: flags: DIFlagStaticMember
6868
// CHECK-NOT: extraData:
6969

7070
// CHECK: ![[IENUM_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "inline_enum",
7171
// CHECK-SAME: flags: DIFlagStaticMember
72-
// CHECK-NOT: extraData:
72+
// CHECK-SAME: extraData: i32 -1
7373

7474
// CHECK: ![[EMPTY_TEMPLATED_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "empty_templated",
7575
// CHECK-SAME: flags: DIFlagStaticMember
76-
// CHECK-NOT: extraData:
76+
// CHECK-SAME: extraData: i32 1
7777

7878
// CHECK: ![[TEMPLATE_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "cexpr_template",
7979
// CHECK-SAME: flags: DIFlagStaticMember
80-
// CHECK-NOT: extraData:
80+
// CHECK-SAME: extraData: i32 1
8181

8282
// CHECK: !DIGlobalVariableExpression(var: ![[EMPTY_VAR:[0-9]+]], expr: !DIExpression())
8383
// CHECK: ![[EMPTY_VAR]] = distinct !DIGlobalVariable(name: "cexpr_struct_with_addr", linkageName:

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ int C::a = 4;
7474
// CHECK-NOT: align:
7575
// CHECK-NOT: offset:
7676
// CHECK-SAME: flags: DIFlagStaticMember
77-
// CHECK-NOT: extraData:
77+
// CHECK-SAME: extraData: i1 true
7878

7979
// DWARF4: ![[CONST_B_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "const_b"
8080
// DWARF5: ![[CONST_B_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_variable, name: "const_b"
8181
// CHECK-NOT: size:
8282
// CHECK-NOT: align:
8383
// CHECK-NOT: offset:
8484
// CHECK-SAME: flags: DIFlagProtected | DIFlagStaticMember
85-
// CHECK-NOT: extraData:
85+
// CHECK-SAME: extraData: float
8686

8787
// DWARF4: ![[DECL_C:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "c"
8888
// DWARF5: ![[DECL_C:[0-9]+]] = !DIDerivedType(tag: DW_TAG_variable, name: "c"
@@ -97,7 +97,7 @@ int C::a = 4;
9797
// CHECK-NOT: align:
9898
// CHECK-NOT: offset:
9999
// CHECK-SAME: flags: DIFlagPublic | DIFlagStaticMember
100-
// CHECK-NOT: extraData:
100+
// CHECK-SAME: extraData: i32 18
101101
//
102102
// DWARF4: !DIDerivedType(tag: DW_TAG_member, name: "x_a"
103103
// DWARF5: !DIDerivedType(tag: DW_TAG_variable, name: "x_a"
@@ -154,7 +154,7 @@ struct V {
154154
// const_va is not emitted for MS targets.
155155
// NOT-MS: !DIDerivedType(tag: DW_TAG_member, name: "const_va",
156156
// NOT-MS-SAME: line: [[@LINE-5]]
157-
// NOT-MS-NOT: extraData:
157+
// NOT-MS-SAME: extraData: i32 42
158158
const int V::const_va;
159159

160160
namespace x {

0 commit comments

Comments
 (0)