[clang] [clang][DebugInfo][NFC] Add createConstantValueExpression helper (PR #70674)

via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 30 08:53:19 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-codegen

Author: Michael Buch (Michael137)

<details>
<summary>Changes</summary>

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

---
Full diff: https://github.com/llvm/llvm-project/pull/70674.diff


2 Files Affected:

- (modified) clang/lib/CodeGen/CGDebugInfo.cpp (+26-18) 
- (modified) clang/lib/CodeGen/CGDebugInfo.h (+5) 


``````````diff
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index 0aaf678bf287c6e..9de55e41f885d26 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,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;
+  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;
+}
diff --git a/clang/lib/CodeGen/CGDebugInfo.h b/clang/lib/CodeGen/CGDebugInfo.h
index ae12485850ca775..7b60e94555d0608 100644
--- a/clang/lib/CodeGen/CGDebugInfo.h
+++ b/clang/lib/CodeGen/CGDebugInfo.h
@@ -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.

``````````

</details>


https://github.com/llvm/llvm-project/pull/70674


More information about the cfe-commits mailing list