Skip to content

Commit 81bd540

Browse files
committed
[clang][DebugInfo] Emit global variable definitions for static data members with constant initializers
When an LLDB user asks for the value of a static data member, LLDB starts by searching the Names accelerator table for the corresponding variable definition DIE. For static data members with out-of-class definitions that works fine, because those get represented as global variables with a location and making them eligible to be added to the Names table. However, in-class definitions won’t get indexed because we usually don't emit global variables for them. So in DWARF we end up with a single `DW_TAG_member` that usually holds the constant initializer. But we don't get a corresponding CU-level `DW_TAG_variable` like we do for out-of-class definitions. To make it more convenient for debuggers to get to the value of inline static data members, this patch makes sure we emit definitions for static variables with constant initializers the same way we do for other static variables. This also aligns Clang closer to GCC, which produces CU-level definitions for inline statics and also emits these into `.debug_pubnames`. The implementation keeps track of newly created static data members. Then in `CGDebugInfo::finalize`, we emit a global `DW_TAG_variable` with a `DW_AT_const_value` for any of those declarations that didn't end up with a definition in the `DeclCache`. The newly emitted `DW_TAG_variable` will look as follows: ``` 0x0000007b: DW_TAG_structure_type DW_AT_calling_convention (DW_CC_pass_by_value) DW_AT_name ("Foo") ... 0x0000008d: DW_TAG_member DW_AT_name ("i") DW_AT_type (0x00000062 "const int") DW_AT_external (true) DW_AT_declaration (true) DW_AT_const_value (4) Newly added vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv 0x0000009a: DW_TAG_variable DW_AT_specification (0x0000008d "i") DW_AT_const_value (4) DW_AT_linkage_name ("_ZN2t2IiE1iIfEE") ```
1 parent aa8a0c0 commit 81bd540

File tree

5 files changed

+144
-7
lines changed

5 files changed

+144
-7
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,6 +1693,7 @@ CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
16931693
llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
16941694
RecordTy, VName, VUnit, LineNumber, VTy, Flags, C, Align);
16951695
StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
1696+
StaticDataMemberDefinitionsToEmit.push_back(Var->getCanonicalDecl());
16961697
return GV;
16971698
}
16981699

@@ -5596,6 +5597,39 @@ void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
55965597
TemplateParameters, Align));
55975598
}
55985599

5600+
void CGDebugInfo::EmitGlobalVariable(const VarDecl *VD) {
5601+
assert(VD->hasInit());
5602+
assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
5603+
if (VD->hasAttr<NoDebugAttr>())
5604+
return;
5605+
5606+
auto &GV = DeclCache[VD];
5607+
if (GV)
5608+
return;
5609+
5610+
auto const *InitVal = VD->evaluateValue();
5611+
if (!InitVal)
5612+
return;
5613+
5614+
llvm::DIFile *Unit = nullptr;
5615+
llvm::DIScope *DContext = nullptr;
5616+
unsigned LineNo;
5617+
StringRef DeclName, LinkageName;
5618+
QualType T;
5619+
llvm::MDTuple *TemplateParameters = nullptr;
5620+
collectVarDeclProps(VD, Unit, LineNo, T, DeclName, LinkageName,
5621+
TemplateParameters, DContext);
5622+
5623+
auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
5624+
llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(VD);
5625+
llvm::DIExpression *InitExpr = createConstantValueExpression(VD, *InitVal);
5626+
5627+
GV.reset(DBuilder.createGlobalVariableExpression(
5628+
TheCU, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
5629+
true, true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VD),
5630+
TemplateParameters, Align, Annotations));
5631+
}
5632+
55995633
void CGDebugInfo::EmitExternalVariable(llvm::GlobalVariable *Var,
56005634
const VarDecl *D) {
56015635
assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
@@ -5866,6 +5900,18 @@ void CGDebugInfo::finalize() {
58665900
DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl));
58675901
}
58685902

5903+
for (auto const *VD : StaticDataMemberDefinitionsToEmit) {
5904+
assert (VD->isStaticDataMember());
5905+
5906+
if (auto It = DeclCache.find(VD); It != DeclCache.end())
5907+
continue;
5908+
5909+
if (!VD->hasInit())
5910+
continue;
5911+
5912+
EmitGlobalVariable(VD);
5913+
}
5914+
58695915
// We keep our own list of retained types, because we need to look
58705916
// up the final type in the type cache.
58715917
for (auto &RT : RetainedTypes)

clang/lib/CodeGen/CGDebugInfo.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ class CGDebugInfo {
161161
llvm::DenseMap<const Decl *, llvm::TypedTrackingMDRef<llvm::DIDerivedType>>
162162
StaticDataMemberCache;
163163

164+
/// Keeps track of static data members for which we should emit a definition.
165+
std::vector<const VarDecl *> StaticDataMemberDefinitionsToEmit;
166+
164167
using ParamDecl2StmtTy = llvm::DenseMap<const ParmVarDecl *, const Stmt *>;
165168
using Param2DILocTy =
166169
llvm::DenseMap<const ParmVarDecl *, llvm::DILocalVariable *>;
@@ -526,6 +529,9 @@ class CGDebugInfo {
526529
/// Emit a constant global variable's debug info.
527530
void EmitGlobalVariable(const ValueDecl *VD, const APValue &Init);
528531

532+
/// Emit debug-info for a variable with a constant initializer.
533+
void EmitGlobalVariable(const VarDecl *VD);
534+
529535
/// Emit information about an external variable.
530536
void EmitExternalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
531537

clang/test/CodeGenCXX/debug-info-class.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,19 @@ int main(int argc, char **argv) {
116116
// CHECK-SAME: DIFlagFwdDecl
117117
// CHECK-NOT: identifier:
118118
// CHECK-SAME: ){{$}}
119+
120+
// CHECK: !DIGlobalVariableExpression(var: ![[HDR_VAR:[0-9]+]], expr: !DIExpression(DW_OP_constu, 52, DW_OP_stack_value))
121+
// CHECK: ![[HDR_VAR]] = distinct !DIGlobalVariable(name: "HdrSize",
122+
// CHECK-SAME: isLocal: true, isDefinition: true, declaration: ![[HDR_VAR_DECL:[0-9]+]])
123+
// CHECK: ![[INT:[0-9]+]] = !DIBasicType(name: "int"
124+
// CHECK: ![[HDR_VAR_DECL]] = !DIDerivedType(tag: DW_TAG_member, name: "HdrSize"
125+
126+
// CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "A"
127+
119128
// CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "I"
120129
// CHECK-NOT: DIFlagFwdDecl
121130
// CHECK-SAME: ){{$}}
122131

123-
// CHECK: ![[INT:[0-9]+]] = !DIBasicType(name: "int"
124132
// CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "foo"
125133
// CHECK: !DICompositeType(tag: DW_TAG_class_type, name: "bar"
126134
// CHECK: !DICompositeType(tag: DW_TAG_union_type, name: "baz"
@@ -186,8 +194,5 @@ int main(int argc, char **argv) {
186194
// CHECK: [[G_INNER_I]] = !DIDerivedType(tag: DW_TAG_member, name: "j"
187195
// CHECK-SAME: baseType: ![[INT]]
188196

189-
// CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "A"
190-
// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "HdrSize"
191-
//
192197
// CHECK: ![[EXCEPTLOC]] = !DILocation(line: 100,
193198
// CHECK: ![[RETLOC]] = !DILocation(line: 99,
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// RUN: %clangxx -target arm64-apple-macosx11.0.0 -g %s -emit-llvm -S -o - | FileCheck --check-prefixes=CHECK %s
2+
3+
enum class Enum : int {
4+
VAL = -1
5+
};
6+
7+
struct Empty {};
8+
9+
constexpr auto func() { return 25; }
10+
11+
struct Foo {
12+
static constexpr int cexpr_int = func();
13+
static constexpr int cexpr_int2 = func() + 1;
14+
static constexpr float cexpr_float = 2.0 + 1.0;
15+
static constexpr Enum cexpr_enum = Enum::VAL;
16+
static constexpr Empty cexpr_empty{};
17+
18+
template<typename T>
19+
static constexpr T cexpr_template{};
20+
};
21+
22+
int main() {
23+
Foo f;
24+
25+
// Force global variable definitions to be emitted.
26+
(void)&Foo::cexpr_int;
27+
(void)&Foo::cexpr_empty;
28+
29+
return Foo::cexpr_int + Foo::cexpr_float
30+
+ (int)Foo::cexpr_enum + Foo::cexpr_template<short>;
31+
}
32+
33+
// CHECK: @{{.*}}cexpr_int{{.*}} =
34+
// CHECK-SAME: !dbg ![[INT_GLOBAL:[0-9]+]]
35+
36+
// CHECK: @{{.*}}cexpr_empty{{.*}} =
37+
// CHECK-SAME !dbg ![[EMPTY_GLOBAL:[0-9]+]]
38+
39+
// CHECK: !DIGlobalVariableExpression(var: ![[INT_VAR:[0-9]+]], expr: !DIExpression())
40+
// CHECK: ![[INT_VAR]] = distinct !DIGlobalVariable(name: "cexpr_int", linkageName:
41+
// CHECK-SAME: isLocal: false, isDefinition: true, declaration: ![[INT_DECL:[0-9]+]])
42+
43+
// CHECK: ![[INT_DECL]] = !DIDerivedType(tag: DW_TAG_member, name: "cexpr_int",
44+
// CHECK-SAME: flags: DIFlagStaticMember, extraData: i32 25)
45+
46+
// CHECK: ![[INT_DECL2:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "cexpr_int2",
47+
// CHECK-SAME: flags: DIFlagStaticMember, extraData: i32 26)
48+
49+
// CHECK: ![[FLOAT_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "cexpr_float",
50+
// CHECK-SAME: flags: DIFlagStaticMember, extraData: float
51+
52+
// CHECK: ![[ENUM_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "cexpr_enum",
53+
// CHECK-SAME: flags: DIFlagStaticMember, extraData: i32 -1)
54+
55+
// CHECK: ![[EMPTY_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "cexpr_empty",
56+
// CHECK-SAME: flags: DIFlagStaticMember)
57+
58+
// CHECK: ![[TEMPLATE_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "cexpr_template",
59+
// CHECK-SAME: flags: DIFlagStaticMember, extraData: i16 0)
60+
61+
// CHECK: !DIGlobalVariableExpression(var: ![[EMPTY_VAR:[0-9]+]], expr: !DIExpression())
62+
// CHECK: ![[EMPTY_VAR]] = distinct !DIGlobalVariable(name: "cexpr_empty", linkageName:
63+
// CHECK-SAME: isLocal: false, isDefinition: true, declaration: ![[EMPTY_DECL]])
64+
65+
// CHECK: !DIGlobalVariableExpression(var: ![[INT_VAR2:[0-9]+]], expr: !DIExpression(DW_OP_constu, 26, DW_OP_stack_value))
66+
// CHECK: ![[INT_VAR2]] = distinct !DIGlobalVariable(name: "cexpr_int2", linkageName:
67+
// CHECK-SAME: isLocal: true, isDefinition: true, declaration: ![[INT_DECL2]])
68+
69+
// CHECK: !DIGlobalVariableExpression(var: ![[FLOAT_VAR:[0-9]+]], expr: !DIExpression(DW_OP_constu, {{.*}}, DW_OP_stack_value))
70+
// CHECK: ![[FLOAT_VAR]] = distinct !DIGlobalVariable(name: "cexpr_float", linkageName:
71+
// CHECK-SAME: isLocal: true, isDefinition: true, declaration: ![[FLOAT_DECL]])
72+
73+
// CHECK: !DIGlobalVariableExpression(var: ![[ENUM_VAR:[0-9]+]], expr: !DIExpression(DW_OP_constu, {{.*}}, DW_OP_stack_value))
74+
// CHECK: ![[ENUM_VAR]] = distinct !DIGlobalVariable(name: "cexpr_enum", linkageName:
75+
// CHECK-SAME: isLocal: true, isDefinition: true, declaration: ![[ENUM_DECL]])
76+
77+
// CHECK: !DIGlobalVariableExpression(var: ![[TEMPLATE_VAR:[0-9]+]], expr: !DIExpression(DW_OP_constu, 0, DW_OP_stack_value))
78+
// CHECK: ![[TEMPLATE_VAR]] = distinct !DIGlobalVariable(name: "cexpr_template", linkageName:
79+
// CHECK-SAME: isLocal: true, isDefinition: true, declaration: ![[TEMPLATE_DECL]], templateParams: ![[TEMPLATE_PARMS:[0-9]+]])

lldb/test/API/lang/cpp/const_static_integral_member/TestConstStaticIntegralMember.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,10 @@ def test(self):
102102
# it does not crash.
103103
self.expect("image lookup -t A")
104104

105-
# dsymutil strips the debug info for classes that only have const static
106-
# data members without a definition namespace scope.
107-
@expectedFailureAll(debug_info=["dsym"])
105+
# For debug-info produced by older versions of clang, dsymutil strips the
106+
# debug info for classes that only have const static data members without
107+
# definitions.
108+
@expectedFailureAll(compiler=["clang"], compiler_version=["<", "18.0"])
108109
def test_class_with_only_const_static(self):
109110
self.build()
110111
lldbutil.run_to_source_breakpoint(

0 commit comments

Comments
 (0)