Skip to content

Commit

Permalink
[clang][DebugInfo][NFC] Add createConstantValueExpression helper (#70674
Browse files Browse the repository at this point in the history
)

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 #70639
  • Loading branch information
Michael137 committed Nov 6, 2023
1 parent a4fbe31 commit aa8a0c0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
48 changes: 30 additions & 18 deletions clang/lib/CodeGen/CGDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5580,25 +5580,8 @@ void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
auto &GV = DeclCache[VD];
if (GV)
return;
llvm::DIExpression *InitExpr = nullptr;
if (CGM.getContext().getTypeSize(VD->getType()) <= 64) {
// FIXME: Add a representation for integer constants wider than 64 bits.
if (Init.isInt()) {
const llvm::APSInt &InitInt = Init.getInt();
std::optional<uint64_t> InitIntOpt;
if (InitInt.isUnsigned())
InitIntOpt = InitInt.tryZExtValue();
else if (auto tmp = InitInt.trySExtValue(); tmp.has_value())
// Transform a signed optional to unsigned optional. When cpp 23 comes,
// use std::optional::transform
InitIntOpt = (uint64_t)tmp.value();
if (InitIntOpt)
InitExpr = DBuilder.createConstantValueExpression(InitIntOpt.value());
} else if (Init.isFloat())
InitExpr = DBuilder.createConstantValueExpression(
Init.getFloat().bitcastToAPInt().getZExtValue());
}

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

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

return llvm::DINode::FlagAllCallsDescribed;
}

llvm::DIExpression *
CGDebugInfo::createConstantValueExpression(const clang::ValueDecl *VD,
const APValue &Val) {
// FIXME: Add a representation for integer constants wider than 64 bits.
if (CGM.getContext().getTypeSize(VD->getType()) > 64)
return nullptr;

if (Val.isFloat())
return DBuilder.createConstantValueExpression(
Val.getFloat().bitcastToAPInt().getZExtValue());

if (!Val.isInt())
return nullptr;

llvm::APSInt const &ValInt = Val.getInt();
std::optional<uint64_t> ValIntOpt;
if (ValInt.isUnsigned())
ValIntOpt = ValInt.tryZExtValue();
else if (auto tmp = ValInt.trySExtValue())
// Transform a signed optional to unsigned optional. When cpp 23 comes,
// use std::optional::transform
ValIntOpt = static_cast<uint64_t>(*tmp);

if (ValIntOpt)
return DBuilder.createConstantValueExpression(ValIntOpt.value());

return nullptr;
}
5 changes: 5 additions & 0 deletions clang/lib/CodeGen/CGDebugInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,11 @@ class CGDebugInfo {
llvm::MDTuple *&TemplateParameters,
llvm::DIScope *&VDContext);

/// Create a DIExpression representing the constant corresponding
/// to the specified 'Val'. Returns nullptr on failure.
llvm::DIExpression *createConstantValueExpression(const clang::ValueDecl *VD,
const APValue &Val);

/// Allocate a copy of \p A using the DebugInfoNames allocator
/// and return a reference to it. If multiple arguments are given the strings
/// are concatenated.
Expand Down

0 comments on commit aa8a0c0

Please sign in to comment.