diff options
author | Michael Buch <michaelbuch12@gmail.com> | 2023-11-06 09:22:20 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-06 09:22:20 +0000 |
commit | aa8a0c0e7b819e7183ca2008d0da46542a7d6ce2 (patch) | |
tree | 9e0de765a661cc055f0e25532c11b849fd84074f /clang/lib/CodeGen/CGDebugInfo.cpp | |
parent | a4fbe31f9ea8faa549b8f516b4a74a6074bf1308 (diff) | |
download | llvm-aa8a0c0e7b819e7183ca2008d0da46542a7d6ce2.zip llvm-aa8a0c0e7b819e7183ca2008d0da46542a7d6ce2.tar.gz llvm-aa8a0c0e7b819e7183ca2008d0da46542a7d6ce2.tar.bz2 |
[clang][DebugInfo][NFC] Add createConstantValueExpression helper (#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 https://github.com/llvm/llvm-project/pull/70639
Diffstat (limited to 'clang/lib/CodeGen/CGDebugInfo.cpp')
-rw-r--r-- | clang/lib/CodeGen/CGDebugInfo.cpp | 48 |
1 files changed, 30 insertions, 18 deletions
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index f4dde18..84a166d 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -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)) @@ -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; +} |