[Lldb-commits] [PATCH] D128126: Support expressions in the context of a reference

Emre Kultursay via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Sat Jun 18 15:18:41 PDT 2022


emrekultursay updated this revision to Diff 438156.
emrekultursay added a comment.

Rebased


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D128126/new/

https://reviews.llvm.org/D128126

Files:
  lldb/source/Expression/UserExpression.cpp
  lldb/test/API/commands/expression/context-object/TestContextObject.py
  lldb/test/API/commands/expression/context-object/main.cpp


Index: lldb/test/API/commands/expression/context-object/main.cpp
===================================================================
--- lldb/test/API/commands/expression/context-object/main.cpp
+++ lldb/test/API/commands/expression/context-object/main.cpp
@@ -31,6 +31,9 @@
   cpp_namespace::CppStruct cpp_struct = cpp_namespace::GetCppStruct();
   cpp_struct.function();
 
+  cpp_namespace::CppStruct& cpp_struct_ref = cpp_struct;
+  cpp_struct_ref.function();
+
   int field = 4444;
 
   cpp_namespace::CppUnion cpp_union;
Index: lldb/test/API/commands/expression/context-object/TestContextObject.py
===================================================================
--- lldb/test/API/commands/expression/context-object/TestContextObject.py
+++ lldb/test/API/commands/expression/context-object/TestContextObject.py
@@ -45,6 +45,36 @@
         self.assertSuccess(value.GetError())
         self.assertEqual(value.GetValueAsSigned(), 1111)
 
+        #
+        # Test C++ reference-to-struct variable
+        #
+
+        obj_val = frame.FindVariable("cpp_struct_ref")
+        self.assertTrue(obj_val.IsValid())
+
+        # Test an empty expression evaluation
+        value = obj_val.EvaluateExpression("")
+        self.assertFalse(value.IsValid())
+        self.assertFalse(value.GetError().Success())
+
+        # Test retrieveing of a field (not a local with the same name)
+        value = obj_val.EvaluateExpression("field")
+        self.assertTrue(value.IsValid())
+        self.assertSuccess(value.GetError())
+        self.assertEqual(value.GetValueAsSigned(), 1111)
+
+        # Test functions evaluation
+        value = obj_val.EvaluateExpression("function()")
+        self.assertTrue(value.IsValid())
+        self.assertSuccess(value.GetError())
+        self.assertEqual(value.GetValueAsSigned(), 2222)
+
+        # Test that we retrieve the right global
+        value = obj_val.EvaluateExpression("global.field")
+        self.assertTrue(value.IsValid())
+        self.assertSuccess(value.GetError())
+        self.assertEqual(value.GetValueAsSigned(), 1111)
+
         #
         # Test C++ union variable
         #
Index: lldb/source/Expression/UserExpression.cpp
===================================================================
--- lldb/source/Expression/UserExpression.cpp
+++ lldb/source/Expression/UserExpression.cpp
@@ -145,8 +145,9 @@
   Log *log(GetLog(LLDBLog::Expressions | LLDBLog::Step));
 
   if (ctx_obj) {
-    static unsigned const ctx_type_mask =
-        lldb::TypeFlags::eTypeIsClass | lldb::TypeFlags::eTypeIsStructUnion;
+    static unsigned const ctx_type_mask = lldb::TypeFlags::eTypeIsClass |
+                                          lldb::TypeFlags::eTypeIsStructUnion |
+                                          lldb::TypeFlags::eTypeIsReference;
     if (!(ctx_obj->GetTypeInfo() & ctx_type_mask)) {
       LLDB_LOG(log, "== [UserExpression::Evaluate] Passed a context object of "
                     "an invalid type, can't run expressions.");
@@ -155,6 +156,19 @@
     }
   }
 
+  if (ctx_obj && ctx_obj->GetTypeInfo() & lldb::TypeFlags::eTypeIsReference) {
+    Status error;
+    lldb::ValueObjectSP deref_ctx_sp = ctx_obj->Dereference(error);
+    if (!error.Success()) {
+      LLDB_LOG(log, "== [UserExpression::Evaluate] Passed a context object of "
+                    "a reference type that can't be dereferenced, can't run expressions.");
+      error.SetErrorString("passed context object of an reference type cannot be deferenced");
+      return lldb::eExpressionSetupError;
+    }
+
+    ctx_obj = deref_ctx_sp.get();
+  }
+
   lldb_private::ExecutionPolicy execution_policy = options.GetExecutionPolicy();
   lldb::LanguageType language = options.GetLanguage();
   const ResultType desired_type = options.DoesCoerceToId()


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128126.438156.patch
Type: text/x-patch
Size: 3801 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20220618/3bb12f7c/attachment-0001.bin>


More information about the lldb-commits mailing list