Skip to content

Commit aa8a0c0

Browse files
authored
[clang][DebugInfo][NFC] Add createConstantValueExpression helper (llvm#70674)
This patch factors out the code to create a DIExpression from an APValue into a separate helper function. This will be useful in a follow-up patch where we re-use this logic elsewhere. Pre-requisite for llvm#70639
1 parent a4fbe31 commit aa8a0c0

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5580,25 +5580,8 @@ void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
55805580
auto &GV = DeclCache[VD];
55815581
if (GV)
55825582
return;
5583-
llvm::DIExpression *InitExpr = nullptr;
5584-
if (CGM.getContext().getTypeSize(VD->getType()) <= 64) {
5585-
// FIXME: Add a representation for integer constants wider than 64 bits.
5586-
if (Init.isInt()) {
5587-
const llvm::APSInt &InitInt = Init.getInt();
5588-
std::optional<uint64_t> InitIntOpt;
5589-
if (InitInt.isUnsigned())
5590-
InitIntOpt = InitInt.tryZExtValue();
5591-
else if (auto tmp = InitInt.trySExtValue(); tmp.has_value())
5592-
// Transform a signed optional to unsigned optional. When cpp 23 comes,
5593-
// use std::optional::transform
5594-
InitIntOpt = (uint64_t)tmp.value();
5595-
if (InitIntOpt)
5596-
InitExpr = DBuilder.createConstantValueExpression(InitIntOpt.value());
5597-
} else if (Init.isFloat())
5598-
InitExpr = DBuilder.createConstantValueExpression(
5599-
Init.getFloat().bitcastToAPInt().getZExtValue());
5600-
}
56015583

5584+
llvm::DIExpression *InitExpr = createConstantValueExpression(VD, Init);
56025585
llvm::MDTuple *TemplateParameters = nullptr;
56035586

56045587
if (isa<VarTemplateSpecializationDecl>(VD))
@@ -5935,3 +5918,32 @@ llvm::DINode::DIFlags CGDebugInfo::getCallSiteRelatedAttrs() const {
59355918

59365919
return llvm::DINode::FlagAllCallsDescribed;
59375920
}
5921+
5922+
llvm::DIExpression *
5923+
CGDebugInfo::createConstantValueExpression(const clang::ValueDecl *VD,
5924+
const APValue &Val) {
5925+
// FIXME: Add a representation for integer constants wider than 64 bits.
5926+
if (CGM.getContext().getTypeSize(VD->getType()) > 64)
5927+
return nullptr;
5928+
5929+
if (Val.isFloat())
5930+
return DBuilder.createConstantValueExpression(
5931+
Val.getFloat().bitcastToAPInt().getZExtValue());
5932+
5933+
if (!Val.isInt())
5934+
return nullptr;
5935+
5936+
llvm::APSInt const &ValInt = Val.getInt();
5937+
std::optional<uint64_t> ValIntOpt;
5938+
if (ValInt.isUnsigned())
5939+
ValIntOpt = ValInt.tryZExtValue();
5940+
else if (auto tmp = ValInt.trySExtValue())
5941+
// Transform a signed optional to unsigned optional. When cpp 23 comes,
5942+
// use std::optional::transform
5943+
ValIntOpt = static_cast<uint64_t>(*tmp);
5944+
5945+
if (ValIntOpt)
5946+
return DBuilder.createConstantValueExpression(ValIntOpt.value());
5947+
5948+
return nullptr;
5949+
}

clang/lib/CodeGen/CGDebugInfo.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,11 @@ class CGDebugInfo {
800800
llvm::MDTuple *&TemplateParameters,
801801
llvm::DIScope *&VDContext);
802802

803+
/// Create a DIExpression representing the constant corresponding
804+
/// to the specified 'Val'. Returns nullptr on failure.
805+
llvm::DIExpression *createConstantValueExpression(const clang::ValueDecl *VD,
806+
const APValue &Val);
807+
803808
/// Allocate a copy of \p A using the DebugInfoNames allocator
804809
/// and return a reference to it. If multiple arguments are given the strings
805810
/// are concatenated.

0 commit comments

Comments
 (0)