[Lldb-commits] [lldb] [lldb][DataFormatter][NFC] Use GetFirstValueOfLibCXXCompressedPair throughout formatters (PR #80133)

Michael Buch via lldb-commits lldb-commits at lists.llvm.org
Wed Jan 31 07:07:42 PST 2024


https://github.com/Michael137 updated https://github.com/llvm/llvm-project/pull/80133

>From da7c51b254087fc45fab7133e63d44782899d57f Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Wed, 31 Jan 2024 12:14:20 +0000
Subject: [PATCH 1/4] [lldb][DataFormatter][NFC] Use
 GetFirstValueOfLibCXXCompressedPair throughout formatters

This avoids duplicating the logic to get the first
element of a libc++ `__compressed_pair`. This will
be useful in supporting upcoming changes to the layout
of `__compressed_pair`.

Drive-by changes:
* Renamed `m_item` to `size_node` for readability;
  `m_item` suggests it's a member variable, which it
  is not.
---
 .../Plugins/Language/CPlusPlus/LibCxxMap.cpp  | 26 ++++------
 .../Language/CPlusPlus/LibCxxUnorderedMap.cpp | 48 +++++--------------
 .../Language/CPlusPlus/LibCxxVector.cpp       | 21 ++------
 3 files changed, 22 insertions(+), 73 deletions(-)

diff --git a/lldb/source/Plugins/Language/CPlusPlus/LibCxxMap.cpp b/lldb/source/Plugins/Language/CPlusPlus/LibCxxMap.cpp
index 092a4120376b7..d3ee63a35e107 100644
--- a/lldb/source/Plugins/Language/CPlusPlus/LibCxxMap.cpp
+++ b/lldb/source/Plugins/Language/CPlusPlus/LibCxxMap.cpp
@@ -213,30 +213,20 @@ size_t lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::
     CalculateNumChildren() {
   if (m_count != UINT32_MAX)
     return m_count;
+
   if (m_tree == nullptr)
     return 0;
-  ValueObjectSP m_item(m_tree->GetChildMemberWithName("__pair3_"));
-  if (!m_item)
+
+  ValueObjectSP size_node(m_tree->GetChildMemberWithName("__pair3_"));
+  if (!size_node)
     return 0;
 
-  switch (m_item->GetCompilerType().GetNumDirectBaseClasses()) {
-  case 1:
-    // Assume a pre llvm r300140 __compressed_pair implementation:
-    m_item = m_item->GetChildMemberWithName("__first_");
-    break;
-  case 2: {
-    // Assume a post llvm r300140 __compressed_pair implementation:
-    ValueObjectSP first_elem_parent = m_item->GetChildAtIndex(0);
-    m_item = first_elem_parent->GetChildMemberWithName("__value_");
-    break;
-  }
-  default:
-    return false;
-  }
+  size_node = GetFirstValueOfLibCXXCompressedPair(*size_node);
 
-  if (!m_item)
+  if (!size_node)
     return 0;
-  m_count = m_item->GetValueAsUnsigned(0);
+
+  m_count = size_node->GetValueAsUnsigned(0);
   return m_count;
 }
 
diff --git a/lldb/source/Plugins/Language/CPlusPlus/LibCxxUnorderedMap.cpp b/lldb/source/Plugins/Language/CPlusPlus/LibCxxUnorderedMap.cpp
index ff7043bdf97ff..25b4ea5e95df3 100644
--- a/lldb/source/Plugins/Language/CPlusPlus/LibCxxUnorderedMap.cpp
+++ b/lldb/source/Plugins/Language/CPlusPlus/LibCxxUnorderedMap.cpp
@@ -116,25 +116,10 @@ lldb::ValueObjectSP lldb_private::formatters::
         if (!p1_sp)
           return nullptr;
 
-        ValueObjectSP first_sp = nullptr;
-        switch (p1_sp->GetCompilerType().GetNumDirectBaseClasses()) {
-        case 1:
-          // Assume a pre llvm r300140 __compressed_pair implementation:
-          first_sp = p1_sp->GetChildMemberWithName("__first_");
-          break;
-        case 2: {
-          // Assume a post llvm r300140 __compressed_pair implementation:
-          ValueObjectSP first_elem_parent_sp =
-            p1_sp->GetChildAtIndex(0);
-          first_sp = p1_sp->GetChildMemberWithName("__value_");
-          break;
-        }
-        default:
-          return nullptr;
-        }
-
+        ValueObjectSP first_sp = GetFirstValueOfLibCXXCompressedPair(*p1_sp);
         if (!first_sp)
           return nullptr;
+
         m_element_type = first_sp->GetCompilerType();
         m_element_type = m_element_type.GetTypeTemplateArgument(0);
         m_element_type = m_element_type.GetPointeeType();
@@ -218,37 +203,26 @@ bool lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::
     return false;
 
   ValueObjectSP p2_sp = table_sp->GetChildMemberWithName("__p2_");
-  ValueObjectSP num_elements_sp = nullptr;
-  llvm::SmallVector<llvm::StringRef, 3> next_path;
-  switch (p2_sp->GetCompilerType().GetNumDirectBaseClasses()) {
-  case 1:
-    // Assume a pre llvm r300140 __compressed_pair implementation:
-    num_elements_sp = p2_sp->GetChildMemberWithName("__first_");
-    next_path.append({"__p1_", "__first_", "__next_"});
-    break;
-  case 2: {
-    // Assume a post llvm r300140 __compressed_pair implementation:
-    ValueObjectSP first_elem_parent = p2_sp->GetChildAtIndex(0);
-    num_elements_sp = first_elem_parent->GetChildMemberWithName("__value_");
-    next_path.append({"__p1_", "__value_", "__next_"});
-    break;
-  }
-  default:
+  if (!p2_sp)
     return false;
-  }
 
+  ValueObjectSP num_elements_sp = GetFirstValueOfLibCXXCompressedPair(*p2_sp);
   if (!num_elements_sp)
     return false;
 
-  m_tree = table_sp->GetChildAtNamePath(next_path).get();
+  ValueObjectSP p1_sp = table_sp->GetChildMemberWithName("__p1_");
+  if (!p1_sp)
+    return false;
+
+  m_tree = GetFirstValueOfLibCXXCompressedPair(*p1_sp).get();
   if (m_tree == nullptr)
     return false;
 
   m_num_elements = num_elements_sp->GetValueAsUnsigned(0);
 
   if (m_num_elements > 0)
-    m_next_element =
-        table_sp->GetChildAtNamePath(next_path).get();
+    m_next_element = m_tree;
+
   return false;
 }
 
diff --git a/lldb/source/Plugins/Language/CPlusPlus/LibCxxVector.cpp b/lldb/source/Plugins/Language/CPlusPlus/LibCxxVector.cpp
index db7cc5bce26ed..2a2c88fdb4195 100644
--- a/lldb/source/Plugins/Language/CPlusPlus/LibCxxVector.cpp
+++ b/lldb/source/Plugins/Language/CPlusPlus/LibCxxVector.cpp
@@ -11,6 +11,7 @@
 #include "lldb/Core/ValueObject.h"
 #include "lldb/DataFormatters/FormattersHelpers.h"
 #include "lldb/Utility/ConstString.h"
+#include <iterator>
 #include <optional>
 
 using namespace lldb;
@@ -123,26 +124,10 @@ bool lldb_private::formatters::LibcxxStdVectorSyntheticFrontEnd::Update() {
   if (!data_type_finder_sp)
     return false;
 
-  switch (data_type_finder_sp->GetCompilerType().GetNumDirectBaseClasses()) {
-  case 1:
-    // Assume a pre llvm r300140 __compressed_pair implementation:
-    data_type_finder_sp =
-        data_type_finder_sp->GetChildMemberWithName("__first_");
-    break;
-  case 2: {
-    // Assume a post llvm r300140 __compressed_pair implementation:
-    ValueObjectSP first_elem_parent_sp =
-      data_type_finder_sp->GetChildAtIndex(0);
-    data_type_finder_sp =
-        first_elem_parent_sp->GetChildMemberWithName("__value_");
-    break;
-  }
-  default:
-    return false;
-  }
-
+  data_type_finder_sp = GetFirstValueOfLibCXXCompressedPair(*data_type_finder_sp);
   if (!data_type_finder_sp)
     return false;
+
   m_element_type = data_type_finder_sp->GetCompilerType().GetPointeeType();
   if (std::optional<uint64_t> size = m_element_type.GetByteSize(nullptr)) {
     m_element_size = *size;

>From 5a9230476c571b4374431c5f9014ce8cb465e522 Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Wed, 31 Jan 2024 14:00:06 +0000
Subject: [PATCH 2/4] fixup! remove redundant header

---
 lldb/source/Plugins/Language/CPlusPlus/LibCxxVector.cpp | 1 -
 1 file changed, 1 deletion(-)

diff --git a/lldb/source/Plugins/Language/CPlusPlus/LibCxxVector.cpp b/lldb/source/Plugins/Language/CPlusPlus/LibCxxVector.cpp
index 2a2c88fdb4195..4e379c3e46731 100644
--- a/lldb/source/Plugins/Language/CPlusPlus/LibCxxVector.cpp
+++ b/lldb/source/Plugins/Language/CPlusPlus/LibCxxVector.cpp
@@ -11,7 +11,6 @@
 #include "lldb/Core/ValueObject.h"
 #include "lldb/DataFormatters/FormattersHelpers.h"
 #include "lldb/Utility/ConstString.h"
-#include <iterator>
 #include <optional>
 
 using namespace lldb;

>From 09d3ab75a33612705dd0a0f7efbe84756cb17a2b Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Wed, 31 Jan 2024 15:07:01 +0000
Subject: [PATCH 3/4] fixup! fix unordered_map formatter

---
 .../Plugins/Language/CPlusPlus/LibCxxUnorderedMap.cpp       | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/lldb/source/Plugins/Language/CPlusPlus/LibCxxUnorderedMap.cpp b/lldb/source/Plugins/Language/CPlusPlus/LibCxxUnorderedMap.cpp
index 25b4ea5e95df3..1a85d37ebf0cc 100644
--- a/lldb/source/Plugins/Language/CPlusPlus/LibCxxUnorderedMap.cpp
+++ b/lldb/source/Plugins/Language/CPlusPlus/LibCxxUnorderedMap.cpp
@@ -214,7 +214,11 @@ bool lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::
   if (!p1_sp)
     return false;
 
-  m_tree = GetFirstValueOfLibCXXCompressedPair(*p1_sp).get();
+  ValueObjectSP value_sp = GetFirstValueOfLibCXXCompressedPair(*p1_sp);
+  if (!value_sp)
+    return false;
+
+  m_tree = value_sp->GetChildMemberWithName("__next_").get();
   if (m_tree == nullptr)
     return false;
 

>From 5640a4dcf9732cfaf5b4cb404ba0891a55cc3d5b Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Wed, 31 Jan 2024 15:07:29 +0000
Subject: [PATCH 4/4] fixup! clang-format

---
 lldb/source/Plugins/Language/CPlusPlus/LibCxxVector.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/lldb/source/Plugins/Language/CPlusPlus/LibCxxVector.cpp b/lldb/source/Plugins/Language/CPlusPlus/LibCxxVector.cpp
index 4e379c3e46731..9d88fcf995309 100644
--- a/lldb/source/Plugins/Language/CPlusPlus/LibCxxVector.cpp
+++ b/lldb/source/Plugins/Language/CPlusPlus/LibCxxVector.cpp
@@ -123,7 +123,8 @@ bool lldb_private::formatters::LibcxxStdVectorSyntheticFrontEnd::Update() {
   if (!data_type_finder_sp)
     return false;
 
-  data_type_finder_sp = GetFirstValueOfLibCXXCompressedPair(*data_type_finder_sp);
+  data_type_finder_sp =
+      GetFirstValueOfLibCXXCompressedPair(*data_type_finder_sp);
   if (!data_type_finder_sp)
     return false;
 



More information about the lldb-commits mailing list