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 1 commit
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
44 changes: 26 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,28 @@ llvm::DINode::DIFlags CGDebugInfo::getCallSiteRelatedAttrs() const {

return llvm::DINode::FlagAllCallsDescribed;
}

llvm::DIExpression *
CGDebugInfo::createConstantValueExpression(clang::ValueDecl const *VD,
const APValue &Val) {
llvm::DIExpression *ValExpr = nullptr;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is how the original code was written, but since we are making a separate function we are now in a position where we can delete this line, early-return on the if and replace all assignments to ValExpr with return statements.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

if (CGM.getContext().getTypeSize(VD->getType()) <= 64) {
// FIXME: Add a representation for integer constants wider than 64 bits.
if (Val.isInt()) {
const llvm::APSInt &ValInt = Val.getInt();
std::optional<uint64_t> ValIntOpt;
if (ValInt.isUnsigned())
ValIntOpt = ValInt.tryZExtValue();
else if (auto tmp = ValInt.trySExtValue(); tmp.has_value())
// Transform a signed optional to unsigned optional. When cpp 23 comes,
// use std::optional::transform
ValIntOpt = (uint64_t)tmp.value();
if (ValIntOpt)
ValExpr = DBuilder.createConstantValueExpression(ValIntOpt.value());
} else if (Val.isFloat())
ValExpr = DBuilder.createConstantValueExpression(
Val.getFloat().bitcastToAPInt().getZExtValue());
}

return ValExpr;
}
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