@@ -69,17 +69,27 @@ static uint32_t getDeclAlignIfRequired(const Decl *D, const ASTContext &Ctx) {
69
69
return D->hasAttr <AlignedAttr>() ? D->getMaxAlignment () : 0 ;
70
70
}
71
71
72
- static APValue const *evaluateConstantInitializer (clang::VarDecl const *VD) {
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 on failure.
76
+ static std::optional<APValue>
77
+ evaluateConstantInitializer (const clang::VarDecl *VD,
78
+ const clang::ASTContext &Ctx) {
73
79
assert (VD != nullptr );
74
80
75
- VD = VD->getCanonicalDecl ();
76
- if (!VD)
77
- return nullptr ;
81
+ if (!VD->isStaticDataMember ())
82
+ return std::nullopt;
78
83
79
- if (!VD->hasConstantInitialization () || !VD->hasInit ())
80
- return nullptr ;
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;
81
91
82
- return VD-> evaluateValue () ;
92
+ return Result. Val ;
83
93
}
84
94
85
95
CGDebugInfo::CGDebugInfo (CodeGenModule &CGM)
@@ -5520,7 +5530,7 @@ void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
5520
5530
5521
5531
llvm::DIExpression *E = nullptr ;
5522
5532
if (Expr.empty ()) {
5523
- if (auto const * InitVal = evaluateConstantInitializer (D))
5533
+ if (const auto InitVal = evaluateConstantInitializer (D, CGM. getContext () ))
5524
5534
E = createConstantValueExpression (D, *InitVal);
5525
5535
} else {
5526
5536
E = DBuilder.createExpression (Expr);
@@ -5622,7 +5632,7 @@ void CGDebugInfo::EmitGlobalVariable(const VarDecl *VD) {
5622
5632
if (CacheIt != DeclCache.end ())
5623
5633
return ;
5624
5634
5625
- auto const * InitVal = evaluateConstantInitializer (VD);
5635
+ const auto InitVal = evaluateConstantInitializer (VD, CGM. getContext () );
5626
5636
if (!InitVal)
5627
5637
return ;
5628
5638
0 commit comments