Skip to content

[clang][DebugInfo][NFC] Add createConstantValueExpression helper #70674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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