summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Buch <michaelbuch12@gmail.com>2023-11-28 15:40:43 +0000
committerGitHub <noreply@github.com>2023-11-28 15:40:43 +0000
commit7f3ee3ca1d09daf0f7d350ed9d62796640a59bf4 (patch)
tree6299f9799261680fda786923533464f18dc26390
parentf3a9dbe7fcabb10d81a56d3757fb1b32fa37972e (diff)
[clang][DebugInfo] Revert to attaching DW_AT_const_value on static member declarations (#73626)
In https://github.com/llvm/llvm-project/pull/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](https://github.com/llvm/llvm-project/pull/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 https://github.com/llvm/llvm-project/pull/71780
-rw-r--r--clang/lib/CodeGen/CGDebugInfo.cpp18
-rw-r--r--clang/test/CodeGenCXX/debug-info-static-inline-member.cpp14
-rw-r--r--clang/test/CodeGenCXX/debug-info-static-member.cpp8
3 files changed, 26 insertions, 14 deletions
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index 3b4932cc4a30..f1f4a1b79b7b 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -1680,14 +1680,26 @@ CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
unsigned LineNumber = getLineNumber(Var->getLocation());
StringRef VName = Var->getName();
+ // FIXME: to avoid complications with type merging we should
+ // emit the constant on the definition instead of the declaration.
+ llvm::Constant *C = nullptr;
+ if (Var->getInit()) {
+ const APValue *Value = Var->evaluateValue();
+ if (Value) {
+ if (Value->isInt())
+ C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
+ if (Value->isFloat())
+ C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
+ }
+ }
+
llvm::DINode::DIFlags Flags = getAccessFlag(Var->getAccess(), RD);
auto Tag = CGM.getCodeGenOpts().DwarfVersion >= 5
? llvm::dwarf::DW_TAG_variable
: llvm::dwarf::DW_TAG_member;
auto Align = getDeclAlignIfRequired(Var, CGM.getContext());
- llvm::DIDerivedType *GV =
- DBuilder.createStaticMemberType(RecordTy, VName, VUnit, LineNumber, VTy,
- Flags, /* Val */ nullptr, Tag, Align);
+ llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
+ RecordTy, VName, VUnit, LineNumber, VTy, Flags, C, Tag, Align);
StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
StaticDataMemberDefinitionsToEmit.push_back(Var->getCanonicalDecl());
return GV;
diff --git a/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp b/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp
index f2d4d9408a82..d3b6a363c5bd 100644
--- a/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp
+++ b/clang/test/CodeGenCXX/debug-info-static-inline-member.cpp
@@ -49,19 +49,19 @@ int main() {
// CHECK: ![[INT_DECL]] = !DIDerivedType(tag: DW_TAG_member, name: "cexpr_int_with_addr",
// CHECK-SAME: flags: DIFlagStaticMember
-// CHECK-NOT: extraData:
+// CHECK-SAME: extraData: i32 25
// CHECK: ![[INT_DECL2:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "cexpr_int2",
// CHECK-SAME: flags: DIFlagStaticMember
-// CHECK-NOT: extraData:
+// CHECK-SAME: extraData: i32 26
// CHECK: ![[FLOAT_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "cexpr_float",
// CHECK-SAME: flags: DIFlagStaticMember
-// CHECK-NOT: extraData:
+// CHECK-SAME: extraData: float
// CHECK: ![[ENUM_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "cexpr_enum",
// CHECK-SAME: flags: DIFlagStaticMember
-// CHECK-NOT: extraData:
+// CHECK-SAME: extraData: i32 -1
// CHECK: ![[EMPTY_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "cexpr_struct_with_addr",
// CHECK-SAME: flags: DIFlagStaticMember
@@ -69,15 +69,15 @@ int main() {
// CHECK: ![[IENUM_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "inline_enum",
// CHECK-SAME: flags: DIFlagStaticMember
-// CHECK-NOT: extraData:
+// CHECK-SAME: extraData: i32 -1
// CHECK: ![[EMPTY_TEMPLATED_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "empty_templated",
// CHECK-SAME: flags: DIFlagStaticMember
-// CHECK-NOT: extraData:
+// CHECK-SAME: extraData: i32 1
// CHECK: ![[TEMPLATE_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "cexpr_template",
// CHECK-SAME: flags: DIFlagStaticMember
-// CHECK-NOT: extraData:
+// CHECK-SAME: extraData: i32 1
// CHECK: !DIGlobalVariableExpression(var: ![[EMPTY_VAR:[0-9]+]], expr: !DIExpression())
// CHECK: ![[EMPTY_VAR]] = distinct !DIGlobalVariable(name: "cexpr_struct_with_addr", linkageName:
diff --git a/clang/test/CodeGenCXX/debug-info-static-member.cpp b/clang/test/CodeGenCXX/debug-info-static-member.cpp
index a2d25e98ed1c..096bfa0271e0 100644
--- a/clang/test/CodeGenCXX/debug-info-static-member.cpp
+++ b/clang/test/CodeGenCXX/debug-info-static-member.cpp
@@ -74,7 +74,7 @@ int C::a = 4;
// CHECK-NOT: align:
// CHECK-NOT: offset:
// CHECK-SAME: flags: DIFlagStaticMember
-// CHECK-NOT: extraData:
+// CHECK-SAME: extraData: i1 true
// DWARF4: ![[CONST_B_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "const_b"
// DWARF5: ![[CONST_B_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_variable, name: "const_b"
@@ -82,7 +82,7 @@ int C::a = 4;
// CHECK-NOT: align:
// CHECK-NOT: offset:
// CHECK-SAME: flags: DIFlagProtected | DIFlagStaticMember
-// CHECK-NOT: extraData:
+// CHECK-SAME: extraData: float
// DWARF4: ![[DECL_C:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "c"
// DWARF5: ![[DECL_C:[0-9]+]] = !DIDerivedType(tag: DW_TAG_variable, name: "c"
@@ -97,7 +97,7 @@ int C::a = 4;
// CHECK-NOT: align:
// CHECK-NOT: offset:
// CHECK-SAME: flags: DIFlagPublic | DIFlagStaticMember
-// CHECK-NOT: extraData:
+// CHECK-SAME: extraData: i32 18
//
// DWARF4: !DIDerivedType(tag: DW_TAG_member, name: "x_a"
// DWARF5: !DIDerivedType(tag: DW_TAG_variable, name: "x_a"
@@ -154,7 +154,7 @@ struct V {
// const_va is not emitted for MS targets.
// NOT-MS: !DIDerivedType(tag: DW_TAG_member, name: "const_va",
// NOT-MS-SAME: line: [[@LINE-5]]
-// NOT-MS-NOT: extraData:
+// NOT-MS-SAME: extraData: i32 42
const int V::const_va;
namespace x {