[Lldb-commits] [lldb] [LLDB] Modify CreateValueObjectFrom* to take an ExecutionContext (PR #185547)

via lldb-commits lldb-commits at lists.llvm.org
Mon Mar 9 17:46:20 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lldb

Author: Adrian Prantl (adrian-prantl)

<details>
<summary>Changes</summary>

Currently these functions take a target pointer. In Cross-language projects this means it's down to change what typesystem the resulting value will be in, since the implementation returns the first scratch type system in the target, which depends on the order of images and their implementation language.

By passing in an execution context the selected frame is used to determine the typesystem, which is usually the expected outcome when using DIL.

This should be entirely NFC for Clang-only LLDBs, but is a necessity for LLDBs with additional type system plugins such as the Swift plugin.

Assisted by Claude to patch the call sites.

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


5 Files Affected:

- (modified) lldb/include/lldb/ValueObject/ValueObject.h (+16-16) 
- (modified) lldb/source/API/SBValue.cpp (+4-2) 
- (modified) lldb/source/ValueObject/DILEval.cpp (+12-10) 
- (modified) lldb/source/ValueObject/ValueObject.cpp (+49-38) 
- (modified) lldb/unittests/DataFormatter/FormatterSectionTest.cpp (+1-1) 


``````````diff
diff --git a/lldb/include/lldb/ValueObject/ValueObject.h b/lldb/include/lldb/ValueObject/ValueObject.h
index 020b4a97cfc92..f4b83614f974c 100644
--- a/lldb/include/lldb/ValueObject/ValueObject.h
+++ b/lldb/include/lldb/ValueObject/ValueObject.h
@@ -727,32 +727,32 @@ class ValueObject {
                             const ExecutionContext &exe_ctx, CompilerType type);
 
   /// Create a value object containing the given APInt value.
-  static lldb::ValueObjectSP CreateValueObjectFromAPInt(lldb::TargetSP target,
-                                                        const llvm::APInt &v,
-                                                        CompilerType type,
-                                                        llvm::StringRef name);
+  static lldb::ValueObjectSP
+  CreateValueObjectFromAPInt(const ExecutionContext &exe_ctx,
+                             const llvm::APInt &v, CompilerType type,
+                             llvm::StringRef name);
 
   /// Create a value object containing the given APFloat value.
   static lldb::ValueObjectSP
-  CreateValueObjectFromAPFloat(lldb::TargetSP target, const llvm::APFloat &v,
-                               CompilerType type, llvm::StringRef name);
+  CreateValueObjectFromAPFloat(const ExecutionContext &exe_ctx,
+                               const llvm::APFloat &v, CompilerType type,
+                               llvm::StringRef name);
 
   /// Create a value object containing the given Scalar value.
-  static lldb::ValueObjectSP CreateValueObjectFromScalar(lldb::TargetSP target,
-                                                         Scalar &s,
-                                                         CompilerType type,
-                                                         llvm::StringRef name);
+  static lldb::ValueObjectSP
+  CreateValueObjectFromScalar(const ExecutionContext &exe_ctx, Scalar &s,
+                              CompilerType type, llvm::StringRef name);
 
   /// Create a value object containing the given boolean value.
-  static lldb::ValueObjectSP CreateValueObjectFromBool(lldb::TargetSP target,
-                                                       bool value,
-                                                       llvm::StringRef name);
+  static lldb::ValueObjectSP
+  CreateValueObjectFromBool(const ExecutionContext &exe_ctx, bool value,
+                            llvm::StringRef name);
 
   /// Create a nullptr value object with the specified type (must be a
   /// nullptr type).
-  static lldb::ValueObjectSP CreateValueObjectFromNullptr(lldb::TargetSP target,
-                                                          CompilerType type,
-                                                          llvm::StringRef name);
+  static lldb::ValueObjectSP
+  CreateValueObjectFromNullptr(const ExecutionContext &exe_ctx,
+                               CompilerType type, llvm::StringRef name);
 
   lldb::ValueObjectSP Persist();
 
diff --git a/lldb/source/API/SBValue.cpp b/lldb/source/API/SBValue.cpp
index adc03785602e1..594bbd1a5097c 100644
--- a/lldb/source/API/SBValue.cpp
+++ b/lldb/source/API/SBValue.cpp
@@ -491,8 +491,10 @@ lldb::SBValue SBValue::CreateBoolValue(const char *name, bool value) {
   lldb::ValueObjectSP value_sp(GetSP(locker));
   lldb::TargetSP target_sp = m_opaque_sp->GetTargetSP();
   if (value_sp && target_sp) {
-    new_value_sp =
-        ValueObject::CreateValueObjectFromBool(target_sp, value, name);
+    new_value_sp = ValueObject::CreateValueObjectFromBool(
+        ExecutionContext(target_sp.get(),
+                         /*fill_current_process_thread_frame=*/true),
+        value, name);
   }
   sb_value.SetSP(new_value_sp);
   return sb_value;
diff --git a/lldb/source/ValueObject/DILEval.cpp b/lldb/source/ValueObject/DILEval.cpp
index 1b841d39a034e..b3354cd6fbc9f 100644
--- a/lldb/source/ValueObject/DILEval.cpp
+++ b/lldb/source/ValueObject/DILEval.cpp
@@ -105,8 +105,8 @@ Interpreter::UnaryConversion(lldb::ValueObjectSP valobj, uint32_t location) {
       bool resolved = valobj->ResolveValue(scalar);
       if (!resolved)
         return llvm::createStringError("invalid scalar value");
-      return ValueObject::CreateValueObjectFromScalar(m_target, scalar, in_type,
-                                                      "result");
+      return ValueObject::CreateValueObjectFromScalar(
+          ExecutionContext(m_exe_ctx_scope.get()), scalar, in_type, "result");
     }
   }
 
@@ -507,7 +507,8 @@ Interpreter::Visit(const UnaryOpNode &node) {
     bool negated = scalar.UnaryNegate();
     if (negated)
       return ValueObject::CreateValueObjectFromScalar(
-          m_target, scalar, operand->GetCompilerType(), "result");
+          ExecutionContext(m_exe_ctx_scope.get()), scalar,
+          operand->GetCompilerType(), "result");
     break;
   }
   case UnaryOpKind::Plus: {
@@ -551,8 +552,8 @@ Interpreter::EvaluateScalarOp(BinaryOpKind kind, lldb::ValueObjectSP lhs,
                                                 location);
 
   auto value_object = [this, result_type](Scalar scalar) {
-    return ValueObject::CreateValueObjectFromScalar(m_target, scalar,
-                                                    result_type, "result");
+    return ValueObject::CreateValueObjectFromScalar(
+        ExecutionContext(m_exe_ctx_scope.get()), scalar, result_type, "result");
   };
 
   switch (kind) {
@@ -1010,8 +1011,8 @@ Interpreter::Visit(const IntegerLiteralNode &node) {
   if (!type_bitsize)
     return type_bitsize.takeError();
   scalar.TruncOrExtendTo(*type_bitsize, false);
-  return ValueObject::CreateValueObjectFromScalar(m_target, scalar, *type,
-                                                  "result");
+  return ValueObject::CreateValueObjectFromScalar(
+      ExecutionContext(m_exe_ctx_scope.get()), scalar, *type, "result");
 }
 
 llvm::Expected<lldb::ValueObjectSP>
@@ -1032,14 +1033,15 @@ Interpreter::Visit(const FloatLiteralNode &node) {
         m_expr, "unable to create a const literal", node.GetLocation());
 
   Scalar scalar = node.GetValue();
-  return ValueObject::CreateValueObjectFromScalar(m_target, scalar, type,
-                                                  "result");
+  return ValueObject::CreateValueObjectFromScalar(
+      ExecutionContext(m_exe_ctx_scope.get()), scalar, type, "result");
 }
 
 llvm::Expected<lldb::ValueObjectSP>
 Interpreter::Visit(const BooleanLiteralNode &node) {
   bool value = node.GetValue();
-  return ValueObject::CreateValueObjectFromBool(m_target, value, "result");
+  return ValueObject::CreateValueObjectFromBool(
+      ExecutionContext(m_exe_ctx_scope.get()), value, "result");
 }
 
 llvm::Expected<CastKind>
diff --git a/lldb/source/ValueObject/ValueObject.cpp b/lldb/source/ValueObject/ValueObject.cpp
index da9c84a4fa221..66ad12ca1e118 100644
--- a/lldb/source/ValueObject/ValueObject.cpp
+++ b/lldb/source/ValueObject/ValueObject.cpp
@@ -3188,12 +3188,12 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) {
   if (type.IsBoolean()) {
     if (!is_scalar || is_integer)
       return ValueObject::CreateValueObjectFromBool(
-          target, GetValueAsUnsigned(0) != 0, "result");
+          exe_ctx, GetValueAsUnsigned(0) != 0, "result");
     else if (is_scalar && is_float) {
       auto float_value_or_err = GetValueAsAPFloat();
       if (float_value_or_err)
         return ValueObject::CreateValueObjectFromBool(
-            target, !float_value_or_err->isZero(), "result");
+            exe_ctx, !float_value_or_err->isZero(), "result");
       else
         return ValueObjectConstResult::Create(
             exe_ctx.GetBestExecutionContextScope(),
@@ -3211,7 +3211,7 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) {
         // size.
         llvm::APSInt ext =
             int_value_or_err->extOrTrunc(type_byte_size * CHAR_BIT);
-        return ValueObject::CreateValueObjectFromAPInt(target, ext, type,
+        return ValueObject::CreateValueObjectFromAPInt(exe_ctx, ext, type,
                                                        "result");
       } else
         return ValueObjectConstResult::Create(
@@ -3236,7 +3236,7 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) {
               Status::FromErrorStringWithFormat(
                   "invalid type cast detected: %s",
                   llvm::toString(float_value_or_err.takeError()).c_str()));
-        return ValueObject::CreateValueObjectFromAPInt(target, integer, type,
+        return ValueObject::CreateValueObjectFromAPInt(exe_ctx, integer, type,
                                                        "result");
       }
     }
@@ -3251,7 +3251,7 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) {
         Scalar scalar_int(ext);
         llvm::APFloat f =
             scalar_int.CreateAPFloatFromAPSInt(type.GetBasicTypeEnumeration());
-        return ValueObject::CreateValueObjectFromAPFloat(target, f, type,
+        return ValueObject::CreateValueObjectFromAPFloat(exe_ctx, f, type,
                                                          "result");
       } else {
         return ValueObjectConstResult::Create(
@@ -3267,7 +3267,7 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) {
           Scalar scalar_int(*int_value_or_err);
           llvm::APFloat f = scalar_int.CreateAPFloatFromAPSInt(
               type.GetBasicTypeEnumeration());
-          return ValueObject::CreateValueObjectFromAPFloat(target, f, type,
+          return ValueObject::CreateValueObjectFromAPFloat(exe_ctx, f, type,
                                                            "result");
         } else {
           return ValueObjectConstResult::Create(
@@ -3283,7 +3283,7 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) {
           Scalar scalar_float(*float_value_or_err);
           llvm::APFloat f = scalar_float.CreateAPFloatFromAPFloat(
               type.GetBasicTypeEnumeration());
-          return ValueObject::CreateValueObjectFromAPFloat(target, f, type,
+          return ValueObject::CreateValueObjectFromAPFloat(exe_ctx, f, type,
                                                            "result");
         } else {
           return ValueObjectConstResult::Create(
@@ -3339,7 +3339,7 @@ lldb::ValueObjectSP ValueObject::CastToEnumType(CompilerType type) {
             Status::FromErrorStringWithFormat(
                 "invalid type cast detected: %s",
                 llvm::toString(value_or_err.takeError()).c_str()));
-      return ValueObject::CreateValueObjectFromAPInt(target, integer, type,
+      return ValueObject::CreateValueObjectFromAPInt(exe_ctx, integer, type,
                                                      "result");
     } else
       return ValueObjectConstResult::Create(
@@ -3350,7 +3350,7 @@ lldb::ValueObjectSP ValueObject::CastToEnumType(CompilerType type) {
     auto value_or_err = GetValueAsAPSInt();
     if (value_or_err) {
       llvm::APSInt ext = value_or_err->extOrTrunc(byte_size * CHAR_BIT);
-      return ValueObject::CreateValueObjectFromAPInt(target, ext, type,
+      return ValueObject::CreateValueObjectFromAPInt(exe_ctx, ext, type,
                                                      "result");
     } else
       return ValueObjectConstResult::Create(
@@ -3591,12 +3591,12 @@ lldb::ValueObjectSP ValueObject::CreateValueObjectFromData(
 }
 
 lldb::ValueObjectSP
-ValueObject::CreateValueObjectFromAPInt(lldb::TargetSP target,
+ValueObject::CreateValueObjectFromAPInt(const ExecutionContext &exe_ctx,
                                         const llvm::APInt &v, CompilerType type,
                                         llvm::StringRef name) {
-  ExecutionContext exe_ctx(target.get(), false);
   uint64_t byte_size = 0;
-  if (auto temp = llvm::expectedToOptional(type.GetByteSize(target.get())))
+  if (auto temp = llvm::expectedToOptional(
+          type.GetByteSize(exe_ctx.GetBestExecutionContextScope())))
     byte_size = temp.value();
   lldb::DataExtractorSP data_sp = std::make_shared<DataExtractor>(
       reinterpret_cast<const void *>(v.getRawData()), byte_size,
@@ -3605,52 +3605,63 @@ ValueObject::CreateValueObjectFromAPInt(lldb::TargetSP target,
 }
 
 lldb::ValueObjectSP ValueObject::CreateValueObjectFromAPFloat(
-    lldb::TargetSP target, const llvm::APFloat &v, CompilerType type,
+    const ExecutionContext &exe_ctx, const llvm::APFloat &v, CompilerType type,
     llvm::StringRef name) {
-  return CreateValueObjectFromAPInt(target, v.bitcastToAPInt(), type, name);
+  return CreateValueObjectFromAPInt(exe_ctx, v.bitcastToAPInt(), type, name);
 }
 
-lldb::ValueObjectSP ValueObject::CreateValueObjectFromScalar(
-    lldb::TargetSP target, Scalar &s, CompilerType type, llvm::StringRef name) {
-  ExecutionContext exe_ctx(target.get(), false);
+lldb::ValueObjectSP
+ValueObject::CreateValueObjectFromScalar(const ExecutionContext &exe_ctx,
+                                         Scalar &s, CompilerType type,
+                                         llvm::StringRef name) {
   return ValueObjectConstResult::Create(exe_ctx.GetBestExecutionContextScope(),
                                         type, s, ConstString(name));
 }
 
-lldb::ValueObjectSP
-ValueObject::CreateValueObjectFromBool(lldb::TargetSP target, bool value,
-                                       llvm::StringRef name) {
-  CompilerType target_type;
-  if (target) {
-    for (auto type_system_sp : target->GetScratchTypeSystems())
-      if (auto compiler_type =
-              type_system_sp->GetBasicTypeFromAST(lldb::eBasicTypeBool)) {
-        target_type = compiler_type;
-        break;
-      }
+static TypeSystemSP
+GetTypeSystemForExecutionContext(const ExecutionContext &exe_ctx) {
+  Target *target = exe_ctx.GetTargetPtr();
+  if (!target)
+    return {};
+  lldb::LanguageType language = lldb::eLanguageTypeC;
+  if (StackFrame *frame = exe_ctx.GetFramePtr())
+    language = frame->GetLanguage().AsLanguageType();
+  auto type_system_or_err = target->GetScratchTypeSystemForLanguage(language);
+  if (!type_system_or_err || !*type_system_or_err) {
+    LLDB_LOG_ERROR(
+        GetLog(LLDBLog::Types), type_system_or_err.takeError(),
+        "cannot find a matching type system for execution context: {0}");
+    return {};
   }
-  ExecutionContext exe_ctx(target.get(), false);
-  uint64_t byte_size = 0;
-  if (auto temp =
-          llvm::expectedToOptional(target_type.GetByteSize(target.get())))
-    byte_size = temp.value();
+  return *type_system_or_err;
+}
+
+lldb::ValueObjectSP
+ValueObject::CreateValueObjectFromBool(const ExecutionContext &exe_ctx,
+                                       bool value, llvm::StringRef name) {
+  TypeSystemSP type_system_sp = GetTypeSystemForExecutionContext(exe_ctx);
+  if (!type_system_sp)
+    return {};
+  CompilerType type = type_system_sp->GetBasicTypeFromAST(lldb::eBasicTypeBool);
+  ExecutionContextScope *exe_scope = exe_ctx.GetBestExecutionContextScope();
+  uint64_t byte_size =
+      llvm::expectedToOptional(type.GetByteSize(exe_scope)).value_or(0);
   lldb::DataExtractorSP data_sp = std::make_shared<DataExtractor>(
       reinterpret_cast<const void *>(&value), byte_size, exe_ctx.GetByteOrder(),
       exe_ctx.GetAddressByteSize());
-  return ValueObject::CreateValueObjectFromData(name, *data_sp, exe_ctx,
-                                                target_type);
+  return ValueObject::CreateValueObjectFromData(name, *data_sp, exe_ctx, type);
 }
 
 lldb::ValueObjectSP ValueObject::CreateValueObjectFromNullptr(
-    lldb::TargetSP target, CompilerType type, llvm::StringRef name) {
+    const ExecutionContext &exe_ctx, CompilerType type, llvm::StringRef name) {
   if (!type.IsNullPtrType()) {
     lldb::ValueObjectSP ret_val;
     return ret_val;
   }
   uintptr_t zero = 0;
-  ExecutionContext exe_ctx(target.get(), false);
   uint64_t byte_size = 0;
-  if (auto temp = llvm::expectedToOptional(type.GetByteSize(target.get())))
+  if (auto temp = llvm::expectedToOptional(
+          type.GetByteSize(exe_ctx.GetBestExecutionContextScope())))
     byte_size = temp.value();
   lldb::DataExtractorSP data_sp = std::make_shared<DataExtractor>(
       reinterpret_cast<const void *>(zero), byte_size, exe_ctx.GetByteOrder(),
diff --git a/lldb/unittests/DataFormatter/FormatterSectionTest.cpp b/lldb/unittests/DataFormatter/FormatterSectionTest.cpp
index 344b299023d03..7a65a9846cd04 100644
--- a/lldb/unittests/DataFormatter/FormatterSectionTest.cpp
+++ b/lldb/unittests/DataFormatter/FormatterSectionTest.cpp
@@ -132,7 +132,7 @@ TEST_F(FormatterSectionTest, LoadFormattersForModule) {
 
   std::string dest;
   ValueObjectSP valobj = ValueObjectConstResult::CreateValueObjectFromBool(
-      m_target_sp, false, "mock");
+      ExecutionContext(m_target_sp.get(), false), false, "mock");
   ASSERT_TRUE(
       point_summary_sp->FormatObject(valobj.get(), dest, TypeSummaryOptions()));
   ASSERT_EQ(dest, "AAAAA");

``````````

</details>


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


More information about the lldb-commits mailing list