Skip to content

Commit 0120962

Browse files
committed
[clang][DebugInfo] Create evaluateConstantInitializer helper function
This patch extracts the logic to evaluate a C++ static data-member's constant initializer such that it can be used by 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.
1 parent c95d581 commit 0120962

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 24 additions & 4 deletions
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),
@@ -5596,14 +5619,11 @@ void CGDebugInfo::EmitGlobalVariable(const VarDecl *VD) {
55965619
if (VD->hasAttr<NoDebugAttr>())
55975620
return;
55985621

5599-
if (!VD->hasInit())
5600-
return;
5601-
56025622
const auto CacheIt = DeclCache.find(VD);
56035623
if (CacheIt != DeclCache.end())
56045624
return;
56055625

5606-
auto const *InitVal = VD->evaluateValue();
5626+
const auto InitVal = evaluateConstantInitializer(VD, CGM.getContext());
56075627
if (!InitVal)
56085628
return;
56095629

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

Lines changed: 0 additions & 9 deletions
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-NOT: extraData:
73-
7470
// CHECK: ![[EMPTY_TEMPLATED_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "empty_templated",
7571
// CHECK-SAME: flags: DIFlagStaticMember
7672
// CHECK-NOT: extraData:
@@ -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

Lines changed: 7 additions & 7 deletions
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)