Skip to content

Commit 53a24c3

Browse files
authored
[clang][DebugInfo] Improve heuristic to determine whether to evaluate a static variable's initializer (#72974)
This patch extracts the logic to evaluate a C++ static data-member's constant initializer. This logic will be re-used in an upcoming patch. It also makes the check for whether we are dealing with a constant initializer more robust/idiomatic, which revealed a bug in the `debug-info-static-inline-member` test (which existed since its introduction in #71780) **Test changes** * `debug-info-static-member.cpp`: * We added the check for `const_b` as part of the patch series in `638a8393615e911b729d5662096f60ef49f1c65e`. The check for `isUsableAsConstantExpression` added in the current patch doesn't support constant inline floats (since they are neither constexpr nor integrals). This isn't a regression since before said patch series we wouldn't ever emit the definition for `const_b` anyway. Now we just don't do it for `inline const float`s. This is consistent with GCC's behaviour starting with C++11. * `debug-info-static-inline-member`: * This was just a bug which is now fixed. We shouldn't emit a `DW_AT_const_value` for a non-const static.
1 parent e620035 commit 53a24c3

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

+24-4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,29 @@ static uint32_t getDeclAlignIfRequired(const Decl *D, const ASTContext &Ctx) {
6969
return D->hasAttr<AlignedAttr>() ? D->getMaxAlignment() : 0;
7070
}
7171

72+
/// Given a VarDecl corresponding to either the definition or
73+
/// declaration of a C++ static data member, if it has a constant
74+
/// initializer and is evaluatable, return the evaluated value.
75+
/// Returns std::nullopt otherwise.
76+
static std::optional<APValue>
77+
evaluateConstantInitializer(const clang::VarDecl *VD,
78+
const clang::ASTContext &Ctx) {
79+
assert(VD != nullptr);
80+
81+
if (!VD->isStaticDataMember())
82+
return std::nullopt;
83+
84+
if (!VD->isUsableInConstantExpressions(Ctx))
85+
return std::nullopt;
86+
87+
auto const *InitExpr = VD->getAnyInitializer();
88+
Expr::EvalResult Result;
89+
if (!InitExpr->EvaluateAsConstantExpr(Result, Ctx))
90+
return std::nullopt;
91+
92+
return Result.Val;
93+
}
94+
7295
CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
7396
: CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
7497
DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
@@ -5610,14 +5633,11 @@ void CGDebugInfo::EmitGlobalVariable(const VarDecl *VD) {
56105633
if (VD->hasAttr<NoDebugAttr>())
56115634
return;
56125635

5613-
if (!VD->hasInit())
5614-
return;
5615-
56165636
const auto CacheIt = DeclCache.find(VD);
56175637
if (CacheIt != DeclCache.end())
56185638
return;
56195639

5620-
auto const *InitVal = VD->evaluateValue();
5640+
const auto InitVal = evaluateConstantInitializer(VD, CGM.getContext());
56215641
if (!InitVal)
56225642
return;
56235643

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

-9
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ int main() {
6767
// CHECK-SAME: flags: DIFlagStaticMember
6868
// CHECK-NOT: extraData:
6969

70-
// CHECK: ![[IENUM_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "inline_enum",
71-
// CHECK-SAME: flags: DIFlagStaticMember
72-
// CHECK-SAME: extraData: i32 -1
73-
7470
// CHECK: ![[EMPTY_TEMPLATED_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "empty_templated",
7571
// CHECK-SAME: flags: DIFlagStaticMember
7672
// CHECK-SAME: extraData: i32 1
@@ -98,11 +94,6 @@ int main() {
9894
// CHECK-NOT: linkageName:
9995
// CHECK-SAME: isLocal: true, isDefinition: true, declaration: ![[ENUM_DECL]])
10096

101-
// CHECK: !DIGlobalVariableExpression(var: ![[IENUM_VAR:[0-9]+]], expr: !DIExpression(DW_OP_constu, {{.*}}, DW_OP_stack_value))
102-
// CHECK: ![[IENUM_VAR]] = distinct !DIGlobalVariable(name: "inline_enum"
103-
// CHECK-NOT: linkageName:
104-
// CHECK-SAME: isLocal: true, isDefinition: true, declaration: ![[IENUM_DECL]])
105-
10697
// CHECK: !DIGlobalVariableExpression(var: ![[EMPTY_TEMPLATED_VAR:[0-9]+]], expr: !DIExpression(DW_OP_constu, 1, DW_OP_stack_value))
10798
// CHECK: ![[EMPTY_TEMPLATED_VAR]] = distinct !DIGlobalVariable(name: "empty_templated"
10899
// CHECK-NOT: linkageName:

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// RUN: %clangxx -target x86_64-unknown-unknown -g -gdwarf-4 %s -emit-llvm -S -o - | FileCheck --check-prefixes=CHECK,DWARF4,NOT-MS %s
1+
// RUN: %clangxx -target x86_64-unknown-unknown -g -gdwarf-4 %s -emit-llvm -S -o - | FileCheck --check-prefixes=CHECK,DWARF4,CPP11,NOT-MS %s
22
// RUN: %clangxx -target x86_64-unknown-unknown -g -gdwarf-4 -std=c++98 %s -emit-llvm -S -o - | FileCheck --check-prefixes=CHECK,DWARF4,NOT-MS %s
3-
// RUN: %clangxx -target x86_64-unknown-unknown -g -gdwarf-4 -std=c++11 %s -emit-llvm -S -o - | FileCheck --check-prefixes=CHECK,DWARF4,NOT-MS %s
4-
// RUN: %clangxx -target x86_64-unknown-unknown -g -gdwarf-5 -std=c++11 %s -emit-llvm -S -o - | FileCheck --check-prefixes=CHECK,DWARF5 %s
5-
// RUN: %clangxx -target x86_64-windows-msvc -g -gdwarf-4 %s -emit-llvm -S -o - | FileCheck --check-prefixes=CHECK,DWARF4 %s
3+
// RUN: %clangxx -target x86_64-unknown-unknown -g -gdwarf-4 -std=c++11 %s -emit-llvm -S -o - | FileCheck --check-prefixes=CHECK,DWARF4,CPP11,NOT-MS %s
4+
// RUN: %clangxx -target x86_64-unknown-unknown -g -gdwarf-5 -std=c++11 %s -emit-llvm -S -o - | FileCheck --check-prefixes=CHECK,DWARF5,CPP11 %s
5+
// RUN: %clangxx -target x86_64-windows-msvc -g -gdwarf-4 %s -emit-llvm -S -o - | FileCheck --check-prefixes=CHECK,DWARF4,CPP11 %s
66
// PR14471
77

88
// CHECK: @{{.*}}a{{.*}} = dso_local global i32 4, align 4, !dbg [[A:![0-9]+]]
@@ -171,9 +171,9 @@ int y::z;
171171
// CHECK: ![[CONST_A_VAR]] = distinct !DIGlobalVariable(name: "const_a"
172172
// CHECK-SAME: isLocal: true, isDefinition: true, declaration: ![[CONST_A_DECL]])
173173

174-
// CHECK: !DIGlobalVariableExpression(var: ![[CONST_B_VAR:[0-9]+]], expr: !DIExpression(DW_OP_constu, {{.*}}, DW_OP_stack_value))
175-
// CHECK: ![[CONST_B_VAR]] = distinct !DIGlobalVariable(name: "const_b"
176-
// CHECK-SAME: isLocal: true, isDefinition: true, declaration: ![[CONST_B_DECL]])
174+
// CPP11: !DIGlobalVariableExpression(var: ![[CONST_B_VAR:[0-9]+]], expr: !DIExpression(DW_OP_constu, {{.*}}, DW_OP_stack_value))
175+
// CPP11: ![[CONST_B_VAR]] = distinct !DIGlobalVariable(name: "const_b"
176+
// CPP11-SAME: isLocal: true, isDefinition: true, declaration: ![[CONST_B_DECL]])
177177

178178
// CHECK: !DIGlobalVariableExpression(var: ![[CONST_C_VAR:[0-9]+]], expr: !DIExpression(DW_OP_constu, 18, DW_OP_stack_value))
179179
// CHECK: ![[CONST_C_VAR]] = distinct !DIGlobalVariable(name: "const_c"

0 commit comments

Comments
 (0)