[Lldb-commits] [PATCH] D157022: Fix the NSIndexSet formatter for macOS Sonoma

Jim Ingham via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Thu Aug 3 11:26:50 PDT 2023


jingham created this revision.
jingham added reviewers: JDevlieghere, bulbazord, mib.
Herald added a subscriber: arphaman.
Herald added a project: All.
jingham requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

Just mutatis mutandis for the Foundation changes.  The current NSIndexSet test fails on Sonoma w/o this patch and exercise the different forms, so no new tests are required.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157022

Files:
  lldb/source/Plugins/Language/ObjC/Cocoa.cpp


Index: lldb/source/Plugins/Language/ObjC/Cocoa.cpp
===================================================================
--- lldb/source/Plugins/Language/ObjC/Cocoa.cpp
+++ lldb/source/Plugins/Language/ObjC/Cocoa.cpp
@@ -237,7 +237,8 @@
   if (!process_sp)
     return false;
 
-  ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);
+  AppleObjCRuntime *runtime = llvm::dyn_cast_or_null<AppleObjCRuntime>(
+      ObjCLanguageRuntime::Get(*process_sp));
 
   if (!runtime)
     return false;
@@ -264,20 +265,61 @@
 
   do {
     if (class_name == "NSIndexSet" || class_name == "NSMutableIndexSet") {
+      // Foundation version 2000 added a bitmask if the index set fit in 64 bits
+      // and a Tagged Pointer version if the bitmask is small enough to fit in
+      // the tagged pointer payload.  
+      // It also changed the layout (but not the size) of the set descriptor.
+
+      // First check whether this is a tagged pointer.  The bitmask will be in
+      // the payload of the tagged pointer.
+      uint64_t payload;
+      if (runtime->GetFoundationVersion() >= 2000  
+          && descriptor->GetTaggedPointerInfo(nullptr, nullptr, &payload)) {
+        count = __builtin_popcountll(payload);
+        break;
+      }
+      // The first 32 bits describe the index set in all cases:
       Status error;
       uint32_t mode = process_sp->ReadUnsignedIntegerFromMemory(
-          valobj_addr + ptr_size, 4, 0, error);
+            valobj_addr + ptr_size, 4, 0, error);
       if (error.Fail())
         return false;
-      // this means the set is empty - count = 0
-      if ((mode & 1) == 1) {
-        count = 0;
-        break;
+      // Now check if the index is held in a bitmask in the object:
+      if (runtime->GetFoundationVersion() >= 2000) {
+        // The first two bits are "isSingleRange" and "isBitfield".  If this is
+        // a bitfield we handle it here, otherwise set mode appropriately and
+        // the rest of the treatment is in common.
+        if ((mode & 2) == 2) {
+          // This is the bitfield case.  The bitfield is a uint64_t:
+          count = process_sp->ReadUnsignedIntegerFromMemory(
+              valobj_addr + 2 * ptr_size, 8, 0, error);
+          if (error.Fail())
+            return false;
+          // The bitfield is a 64 bit uint at the beginning of the data var.
+          uint64_t bitfield = process_sp->ReadUnsignedIntegerFromMemory(
+            valobj_addr + 2 * ptr_size, 8, 0, error);
+          if (error.Fail())
+            return false;
+          count = __builtin_popcountll(bitfield);
+          break;
+        }
+        // It wasn't a bitfield, so read the isSingleRange from its new loc:
+        if ((mode & 1) == 1)
+          mode = 1; // this means the set only has one range
+        else
+          mode = 2; // this means the set has multiple ranges
+      } else {
+        // this means the set is empty - count = 0
+        if ((mode & 1) == 1) {
+          count = 0;
+          break;
+        }
+      
+        if ((mode & 2) == 2)
+          mode = 1; // this means the set only has one range
+        else
+          mode = 2; // this means the set has multiple ranges
       }
-      if ((mode & 2) == 2)
-        mode = 1; // this means the set only has one range
-      else
-        mode = 2; // this means the set has multiple ranges
       if (mode == 1) {
         count = process_sp->ReadUnsignedIntegerFromMemory(
             valobj_addr + 3 * ptr_size, ptr_size, 0, error);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157022.546958.patch
Type: text/x-patch
Size: 3514 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20230803/0da19e95/attachment.bin>


More information about the lldb-commits mailing list