[Lldb-commits] [lldb] Make result variables obey their dynamic values in subsequent expressions (PR #168611)

via lldb-commits lldb-commits at lists.llvm.org
Tue Nov 18 13:55:15 PST 2025


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff origin/main HEAD --extensions cpp,h -- lldb/test/API/functionalities/expr-result-var/two-bases.cpp lldb/include/lldb/Expression/ExpressionVariable.h lldb/source/Expression/ExpressionVariable.cpp lldb/source/Expression/LLVMUserExpression.cpp lldb/source/Expression/Materializer.cpp lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionVariable.cpp lldb/source/Target/ABI.cpp --diff_from_common_commit
``````````

:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/lldb/include/lldb/Expression/ExpressionVariable.h b/lldb/include/lldb/Expression/ExpressionVariable.h
index 8b7eee63f..705263a37 100644
--- a/lldb/include/lldb/Expression/ExpressionVariable.h
+++ b/lldb/include/lldb/Expression/ExpressionVariable.h
@@ -33,20 +33,19 @@ public:
 
   virtual ~ExpressionVariable() = default;
 
-  llvm::Expected<uint64_t> GetByteSize() { 
-    return GetValueObject()->GetByteSize(); 
-  
+  llvm::Expected<uint64_t> GetByteSize() {
+    return GetValueObject()->GetByteSize();
   }
 
   ConstString GetName() { return m_frozen_sp->GetName(); }
 
   lldb::ValueObjectSP GetValueObject() {
-    lldb::ValueObjectSP dyn_sp = 
+    lldb::ValueObjectSP dyn_sp =
         m_frozen_sp->GetDynamicValue(lldb::eDynamicDontRunTarget);
     if (dyn_sp && dyn_sp->UpdateValueIfNeeded())
       return dyn_sp;
     else
-      return m_frozen_sp; 
+      return m_frozen_sp;
   }
 
   uint8_t *GetValueBytes();
@@ -91,12 +90,10 @@ public:
   }
   // We don't try to get the dynamic value of the live object when we fetch
   // it here.  The live object describes the container of the value in the
-  // target, but it's type is of the object for convenience.  So it can't 
+  // target, but it's type is of the object for convenience.  So it can't
   // produce the dynamic value.  Instead, we use TransferAddress to adjust the
   // value held by the LiveObject.
-  lldb::ValueObjectSP GetLiveObject() {
-    return m_live_sp;
-  }
+  lldb::ValueObjectSP GetLiveObject() { return m_live_sp; }
 
   enum Flags {
     EVNone = 0,
@@ -133,7 +130,7 @@ public:
   /// The m_frozen_sp holds the data & type of the expression variable or result
   /// in the host.  The m_frozen_sp also can present a dynamic value if one is
   /// available.
-  /// The m_frozen_sp manages the copy of this value in m_frozen_sp that we 
+  /// The m_frozen_sp manages the copy of this value in m_frozen_sp that we
   /// insert in the target so that it can be referred to in future expressions.
   /// We don't actually use the contents of the live_sp to create the value in
   /// the target, that comes from the frozen sp.  The live_sp is mostly to track
@@ -148,7 +145,7 @@ public:
   lldb::ValueObjectSP m_live_sp;
   /// @}
 
-  //LLVMCastKind m_kind;
+  // LLVMCastKind m_kind;
   lldb::DynamicValueType m_dyn_option = lldb::eNoDynamicValues;
 };
 
diff --git a/lldb/source/Expression/ExpressionVariable.cpp b/lldb/source/Expression/ExpressionVariable.cpp
index 9d361bc85..7c9c99f0a 100644
--- a/lldb/source/Expression/ExpressionVariable.cpp
+++ b/lldb/source/Expression/ExpressionVariable.cpp
@@ -28,8 +28,7 @@ uint8_t *ExpressionVariable::GetValueBytes() {
       valobj_sp->GetValue().ResizeData(*byte_size);
       valobj_sp->GetValue().GetData(valobj_sp->GetDataExtractor());
     }
-    return const_cast<uint8_t *>(
-        valobj_sp->GetDataExtractor().GetDataStart());
+    return const_cast<uint8_t *>(valobj_sp->GetDataExtractor().GetDataStart());
   }
   return nullptr;
 }
@@ -49,23 +48,23 @@ void ExpressionVariable::TransferAddress(bool force) {
     lldb::addr_t live_addr = m_live_sp->GetLiveAddress();
     m_frozen_sp->SetLiveAddress(live_addr);
     // One more detail, if there's an offset_to_top in the frozen_sp, then we
-    // need to appy that offset by hand.  The live_sp can't compute this 
+    // need to appy that offset by hand.  The live_sp can't compute this
     // itself as its type is the type of the contained object which confuses
     // the dynamic type calculation.  So we have to update the contents of the
     // m_live_sp with the dynamic value.
     // Note: We could get this right when we originally write the address, but
-    // that happens in different ways for the various flavors of 
+    // that happens in different ways for the various flavors of
     // Entity*::Materialize, but everything comes through here, and it's just
     // one extra memory write.
-    
+
     // You can only have an "offset_to_top" with pointers or references:
     if (!m_frozen_sp->GetCompilerType().IsPointerOrReferenceType())
       return;
 
     lldb::ProcessSP process_sp = m_frozen_sp->GetProcessSP();
     // If there's no dynamic value, then there can't be an offset_to_top:
-    if (!process_sp 
-        || !process_sp->IsPossibleDynamicValue(*(m_frozen_sp.get())))
+    if (!process_sp ||
+        !process_sp->IsPossibleDynamicValue(*(m_frozen_sp.get())))
       return;
 
     lldb::ValueObjectSP dyn_sp = m_frozen_sp->GetDynamicValue(m_dyn_option);
@@ -75,27 +74,30 @@ void ExpressionVariable::TransferAddress(bool force) {
     if (static_addr.type != eAddressTypeLoad)
       return;
 
-    ValueObject::AddrAndType dynamic_addr= dyn_sp->GetPointerValue();
-    if (dynamic_addr.type != eAddressTypeLoad || static_addr.address == dynamic_addr.address)
-        return;
-      
+    ValueObject::AddrAndType dynamic_addr = dyn_sp->GetPointerValue();
+    if (dynamic_addr.type != eAddressTypeLoad ||
+        static_addr.address == dynamic_addr.address)
+      return;
+
     Status error;
     Log *log = GetLog(LLDBLog::Expressions);
-    lldb::addr_t cur_value 
-        = process_sp->ReadPointerFromMemory(live_addr, error);
+    lldb::addr_t cur_value =
+        process_sp->ReadPointerFromMemory(live_addr, error);
     if (error.Fail())
       return;
-    
+
     if (cur_value != static_addr.address) {
-      LLDB_LOG(log, "Stored value: {0} read from {1} doesn't "
+      LLDB_LOG(log,
+               "Stored value: {0} read from {1} doesn't "
                "match static addr: {2}",
                cur_value, live_addr, static_addr.address);
       return;
     }
-    
-    if (!process_sp->WritePointerToMemory(live_addr, dynamic_addr.address, error)) {
-      LLDB_LOG(log, "Got error: {0} writing dynamic value: {1} to {2}",
-               error, dynamic_addr.address, live_addr);
+
+    if (!process_sp->WritePointerToMemory(live_addr, dynamic_addr.address,
+                                          error)) {
+      LLDB_LOG(log, "Got error: {0} writing dynamic value: {1} to {2}", error,
+               dynamic_addr.address, live_addr);
       return;
     }
   }
diff --git a/lldb/source/Expression/LLVMUserExpression.cpp b/lldb/source/Expression/LLVMUserExpression.cpp
index dac572e81..e78543c4a 100644
--- a/lldb/source/Expression/LLVMUserExpression.cpp
+++ b/lldb/source/Expression/LLVMUserExpression.cpp
@@ -252,11 +252,12 @@ LLVMUserExpression::DoExecute(DiagnosticManager &diagnostic_manager,
 
   if (FinalizeJITExecution(diagnostic_manager, exe_ctx, result_sp,
                            function_stack_bottom, function_stack_top)) {
-//    if (result_sp) {
-//      // This is a bit of a hack, replace with a real API.  Trying to force 
-//      // fetching all the dynamic info at this point.
-//      result_sp->GetValueObject();
-//    }
+    //    if (result_sp) {
+    //      // This is a bit of a hack, replace with a real API.  Trying to
+    //      force
+    //      // fetching all the dynamic info at this point.
+    //      result_sp->GetValueObject();
+    //    }
     return lldb::eExpressionCompleted;
   }
 
diff --git a/lldb/source/Expression/Materializer.cpp b/lldb/source/Expression/Materializer.cpp
index 6561aff39..cbc197e3d 100644
--- a/lldb/source/Expression/Materializer.cpp
+++ b/lldb/source/Expression/Materializer.cpp
@@ -76,10 +76,11 @@ public:
 
     const bool zero_memory = false;
     IRMemoryMap::AllocationPolicy used_policy;
-    uint64_t malloc_size = llvm::expectedToOptional(
-        m_persistent_variable_sp->GetByteSize()).value_or(0);
-    auto address_or_error = map.Malloc(malloc_size,
-        8, lldb::ePermissionsReadable | lldb::ePermissionsWritable,
+    uint64_t malloc_size =
+        llvm::expectedToOptional(m_persistent_variable_sp->GetByteSize())
+            .value_or(0);
+    auto address_or_error = map.Malloc(
+        malloc_size, 8, lldb::ePermissionsReadable | lldb::ePermissionsWritable,
         IRMemoryMap::eAllocationPolicyMirror, zero_memory, &used_policy);
     if (!address_or_error) {
       err = Status::FromErrorStringWithFormat(
@@ -90,9 +91,9 @@ public:
     }
     lldb::addr_t mem = *address_or_error;
 
-    LLDB_LOGF(log, "Allocated 0x%" PRIx64 "bytes for %s (0x%" PRIx64 
-              ") successfully", malloc_size, 
-              m_persistent_variable_sp->GetName().GetCString(), mem);
+    LLDB_LOGF(
+        log, "Allocated 0x%" PRIx64 "bytes for %s (0x%" PRIx64 ") successfully",
+        malloc_size, m_persistent_variable_sp->GetName().GetCString(), mem);
 
     // Put the location of the spare memory into the live data of the
     // ValueObject.
@@ -144,11 +145,9 @@ public:
   void DestroyAllocation(IRMemoryMap &map, Status &err) {
     Status deallocate_error;
 
-    lldb::ValueObjectSP live_valobj_sp 
-        = m_persistent_variable_sp->GetLiveObject();
-    map.Free((lldb::addr_t)live_valobj_sp->GetValue()
-                 .GetScalar()
-                 .ULongLong(),
+    lldb::ValueObjectSP live_valobj_sp =
+        m_persistent_variable_sp->GetLiveObject();
+    map.Free((lldb::addr_t)live_valobj_sp->GetValue().GetScalar().ULongLong(),
              deallocate_error);
 
     live_valobj_sp.reset();
@@ -186,18 +185,17 @@ public:
         return;
     }
 
-    lldb::ValueObjectSP live_valobj_sp 
-        = m_persistent_variable_sp->GetLiveObject();
+    lldb::ValueObjectSP live_valobj_sp =
+        m_persistent_variable_sp->GetLiveObject();
     if ((m_persistent_variable_sp->m_flags &
-             ExpressionVariable::EVIsProgramReference && live_valobj_sp) ||
+             ExpressionVariable::EVIsProgramReference &&
+         live_valobj_sp) ||
         m_persistent_variable_sp->m_flags &
             ExpressionVariable::EVIsLLDBAllocated) {
       Status write_error;
 
-      map.WriteScalarToMemory(
-          load_addr,
-          live_valobj_sp->GetValue().GetScalar(),
-          map.GetAddressByteSize(), write_error);
+      map.WriteScalarToMemory(load_addr, live_valobj_sp->GetValue().GetScalar(),
+                              map.GetAddressByteSize(), write_error);
 
       if (!write_error.Success()) {
         err = Status::FromErrorStringWithFormat(
@@ -233,8 +231,8 @@ public:
       m_delegate->DidDematerialize(m_persistent_variable_sp);
     }
 
-    lldb::ValueObjectSP live_valobj_sp 
-          = m_persistent_variable_sp->GetLiveObject();
+    lldb::ValueObjectSP live_valobj_sp =
+        m_persistent_variable_sp->GetLiveObject();
     if ((m_persistent_variable_sp->m_flags &
          ExpressionVariable::EVIsLLDBAllocated) ||
         (m_persistent_variable_sp->m_flags &
@@ -246,7 +244,7 @@ public:
         // ClangExpressionVariable's live variable data hasn't been set up yet.
         // Do this now.
 
-       lldb::addr_t location;
+        lldb::addr_t location;
         Status read_error;
 
         map.ReadPointerFromMemory(&location, load_addr, read_error);
@@ -290,12 +288,10 @@ public:
         return;
       }
 
-      lldb::addr_t mem = live_valobj_sp->GetValue()
-                             .GetScalar()
-                             .ULongLong();
+      lldb::addr_t mem = live_valobj_sp->GetValue().GetScalar().ULongLong();
 
-      if (live_valobj_sp->GetValue().GetValueAddressType() 
-          != eAddressTypeLoad) {
+      if (live_valobj_sp->GetValue().GetValueAddressType() !=
+          eAddressTypeLoad) {
         err = Status::FromErrorStringWithFormat(
             "the address of the memory area for %s is in an incorrect format",
             m_persistent_variable_sp->GetName().GetCString());
diff --git a/lldb/test/API/functionalities/expr-result-var/two-bases.cpp b/lldb/test/API/functionalities/expr-result-var/two-bases.cpp
index fa6fab0b4..21e277cd8 100644
--- a/lldb/test/API/functionalities/expr-result-var/two-bases.cpp
+++ b/lldb/test/API/functionalities/expr-result-var/two-bases.cpp
@@ -1,5 +1,5 @@
-#include <stdio.h>
 #include <stdint.h>
+#include <stdio.h>
 
 struct Base {
   virtual ~Base() = default;
@@ -9,56 +9,48 @@ struct Base {
 
 struct Base_1 : public VIRTUAL Base {
   virtual ~Base_1() = default;
-  int base_1_arr[10] = { 100, 101, 102, 103, 104, 105, 106, 107, 108, 109 };
+  int base_1_arr[10] = {100, 101, 102, 103, 104, 105, 106, 107, 108, 109};
   Base *return_base_1() { return return_me(); }
 };
 
-struct Base_2 :public VIRTUAL Base {
+struct Base_2 : public VIRTUAL Base {
   virtual ~Base_2() = default;
-  int base_2_arr[10] = { 200, 201, 202, 203, 204, 205, 206, 207, 208, 209 };
+  int base_2_arr[10] = {200, 201, 202, 203, 204, 205, 206, 207, 208, 209};
   Base *return_base_2() { return return_me(); }
 };
 
-struct Derived : public Base_1, Base_2
-{
+struct Derived : public Base_1, Base_2 {
   virtual ~Derived() = default;
   int derived_int = 1000;
-  int method_of_derived() {
-    return 500;
-  }
+  int method_of_derived() { return 500; }
 };
 
-Base *MakeADerivedReportABase() {
-  return (Base *) ((Base_1 *) new Derived());
-}
+Base *MakeADerivedReportABase() { return (Base *)((Base_1 *)new Derived()); }
 
-int
-main()
-{
+int main() {
   Derived my_derived;
   int call_it = my_derived.method_of_derived();
 
-  Base_1 *base_1_ptr = (Base_1 *) &my_derived;
+  Base_1 *base_1_ptr = (Base_1 *)&my_derived;
 
-  Base_2 *base_2_ptr = (Base_2 *) &my_derived;
+  Base_2 *base_2_ptr = (Base_2 *)&my_derived;
 
   Base *base_through_1 = my_derived.return_base_1();
-  Base *base_through_2 = my_derived.return_base_2();;
+  Base *base_through_2 = my_derived.return_base_2();
+  ;
 
-  // Call this to make sure the compiler makes it. 
+  // Call this to make sure the compiler makes it.
   Base *fake_base = MakeADerivedReportABase();
 
-  uint64_t base_through_1_addr = (uint64_t) base_through_1;
-  uint64_t base_through_2_addr = (uint64_t) base_through_2;
+  uint64_t base_through_1_addr = (uint64_t)base_through_1;
+  uint64_t base_through_2_addr = (uint64_t)base_through_2;
   int64_t base_offset = base_through_2_addr - base_through_1_addr;
   printf("Base offset (should be 0): 0x%llx.\n", base_offset);
-  uint64_t base_1_addr = (uint64_t) base_1_ptr;
-  uint64_t base_2_addr = (uint64_t) base_2_ptr;
+  uint64_t base_1_addr = (uint64_t)base_1_ptr;
+  uint64_t base_2_addr = (uint64_t)base_2_ptr;
   int64_t offset = base_2_addr - base_1_addr;
 
   // Set a breakpoint here
-  return my_derived.derived_int + base_1_ptr->base_1_arr[0]
-    + base_2_ptr->base_2_arr[0] + my_derived.return_base_1()->base_int;
-  
+  return my_derived.derived_int + base_1_ptr->base_1_arr[0] +
+         base_2_ptr->base_2_arr[0] + my_derived.return_base_1()->base_int;
 }
-

``````````

</details>


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


More information about the lldb-commits mailing list