[Lldb-commits] [lldb] fe01292 - Move FormattersMatchCandidate flags to a struct.

Jorge Gorbe Moya via lldb-commits lldb-commits at lists.llvm.org
Tue Aug 9 10:49:24 PDT 2022


Author: Jorge Gorbe Moya
Date: 2022-08-09T10:48:49-07:00
New Revision: fe01292457fc04532c5d2eccc9d0674df4582fa6

URL: https://github.com/llvm/llvm-project/commit/fe01292457fc04532c5d2eccc9d0674df4582fa6
DIFF: https://github.com/llvm/llvm-project/commit/fe01292457fc04532c5d2eccc9d0674df4582fa6.diff

LOG: Move FormattersMatchCandidate flags to a struct.

This removes some error-prone repetition in
FormatManager::GetPossibleMatches, where the same three boolean flags
are passed in a row multiple times as arguments to recursive calls to
GetPossibleMatches.

Instead of:
```
  // same flags, but with did_strip_typedef set to true.
  GetPossibleMatches(..., did_strip_ptr, did_strip_ref, true);
```
we can now say
```
  GetPossibleMatches(..., current_flags.WithStrippedTypedef());
```
which hopefully makes the intent clearer, and more readable in case we
add another flag.

Reviewed by: DavidSpickett, labath

Differential Revision: https://reviews.llvm.org/D131459

Added: 
    

Modified: 
    lldb/include/lldb/DataFormatters/FormatClasses.h
    lldb/include/lldb/DataFormatters/FormatManager.h
    lldb/source/DataFormatters/FormatManager.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/DataFormatters/FormatClasses.h b/lldb/include/lldb/DataFormatters/FormatClasses.h
index 158d2531d0dab..f8f17b6d5d8ae 100644
--- a/lldb/include/lldb/DataFormatters/FormatClasses.h
+++ b/lldb/include/lldb/DataFormatters/FormatClasses.h
@@ -43,20 +43,48 @@ class HardcodedFormatters {
 
 class FormattersMatchCandidate {
 public:
-  FormattersMatchCandidate(ConstString name, bool strip_ptr,
-                           bool strip_ref, bool strip_tydef)
-      : m_type_name(name), m_stripped_pointer(strip_ptr),
-        m_stripped_reference(strip_ref), m_stripped_typedef(strip_tydef) {}
+  // Contains flags to indicate how this candidate was generated (e.g. if
+  // typedefs were stripped, or pointers were skipped). These are later compared
+  // to flags in formatters to confirm a string match.
+  struct Flags {
+    bool stripped_pointer = false;
+    bool stripped_reference = false;
+    bool stripped_typedef = false;
+
+    // Returns a copy of this with the "stripped pointer" flag set.
+    Flags WithStrippedPointer() {
+      Flags result(*this);
+      result.stripped_pointer = true;
+      return result;
+    }
+
+    // Returns a copy of this with the "stripped reference" flag set.
+    Flags WithStrippedReference() {
+      Flags result(*this);
+      result.stripped_reference = true;
+      return result;
+    }
+
+    // Returns a copy of this with the "stripped typedef" flag set.
+    Flags WithStrippedTypedef() {
+      Flags result(*this);
+      result.stripped_typedef = true;
+      return result;
+    }
+  };
+
+  FormattersMatchCandidate(ConstString name, Flags flags)
+      : m_type_name(name), m_flags(flags) {}
 
   ~FormattersMatchCandidate() = default;
 
   ConstString GetTypeName() const { return m_type_name; }
 
-  bool DidStripPointer() const { return m_stripped_pointer; }
+  bool DidStripPointer() const { return m_flags.stripped_pointer; }
 
-  bool DidStripReference() const { return m_stripped_reference; }
+  bool DidStripReference() const { return m_flags.stripped_reference; }
 
-  bool DidStripTypedef() const { return m_stripped_typedef; }
+  bool DidStripTypedef() const { return m_flags.stripped_typedef; }
 
   template <class Formatter>
   bool IsMatch(const std::shared_ptr<Formatter> &formatter_sp) const {
@@ -73,9 +101,7 @@ class FormattersMatchCandidate {
 
 private:
   ConstString m_type_name;
-  bool m_stripped_pointer;
-  bool m_stripped_reference;
-  bool m_stripped_typedef;
+  Flags m_flags;
 };
 
 typedef std::vector<FormattersMatchCandidate> FormattersMatchVector;

diff  --git a/lldb/include/lldb/DataFormatters/FormatManager.h b/lldb/include/lldb/DataFormatters/FormatManager.h
index 978ad148d6c49..594addd1f083f 100644
--- a/lldb/include/lldb/DataFormatters/FormatManager.h
+++ b/lldb/include/lldb/DataFormatters/FormatManager.h
@@ -162,8 +162,8 @@ class FormatManager : public IFormatChangeListener {
   static FormattersMatchVector
   GetPossibleMatches(ValueObject &valobj, lldb::DynamicValueType use_dynamic) {
     FormattersMatchVector matches;
-    GetPossibleMatches(valobj, valobj.GetCompilerType(),
-                       use_dynamic, matches, false, false, false, true);
+    GetPossibleMatches(valobj, valobj.GetCompilerType(), use_dynamic, matches,
+                       FormattersMatchCandidate::Flags(), true);
     return matches;
   }
 
@@ -179,8 +179,7 @@ class FormatManager : public IFormatChangeListener {
                                  CompilerType compiler_type,
                                  lldb::DynamicValueType use_dynamic,
                                  FormattersMatchVector &entries,
-                                 bool did_strip_ptr, bool did_strip_ref,
-                                 bool did_strip_typedef,
+                                 FormattersMatchCandidate::Flags current_flags,
                                  bool root_level = false);
 
   std::atomic<uint32_t> m_last_revision;

diff  --git a/lldb/source/DataFormatters/FormatManager.cpp b/lldb/source/DataFormatters/FormatManager.cpp
index a8390c5d79c6a..a81597f10c46a 100644
--- a/lldb/source/DataFormatters/FormatManager.cpp
+++ b/lldb/source/DataFormatters/FormatManager.cpp
@@ -175,60 +175,51 @@ void FormatManager::DisableAllCategories() {
 void FormatManager::GetPossibleMatches(
     ValueObject &valobj, CompilerType compiler_type,
     lldb::DynamicValueType use_dynamic, FormattersMatchVector &entries,
-    bool did_strip_ptr, bool did_strip_ref, bool did_strip_typedef,
-    bool root_level) {
+    FormattersMatchCandidate::Flags current_flags, bool root_level) {
   compiler_type = compiler_type.GetTypeForFormatters();
   ConstString type_name(compiler_type.GetTypeName());
   if (valobj.GetBitfieldBitSize() > 0) {
     StreamString sstring;
     sstring.Printf("%s:%d", type_name.AsCString(), valobj.GetBitfieldBitSize());
     ConstString bitfieldname(sstring.GetString());
-    entries.push_back(
-        {bitfieldname, did_strip_ptr, did_strip_ref, did_strip_typedef});
+    entries.push_back({bitfieldname, current_flags});
   }
 
   if (!compiler_type.IsMeaninglessWithoutDynamicResolution()) {
-    entries.push_back(
-        {type_name, did_strip_ptr, did_strip_ref, did_strip_typedef});
+    entries.push_back({type_name, current_flags});
 
     ConstString display_type_name(compiler_type.GetTypeName());
     if (display_type_name != type_name)
-      entries.push_back({display_type_name, did_strip_ptr,
-                         did_strip_ref, did_strip_typedef});
+      entries.push_back({display_type_name, current_flags});
   }
 
   for (bool is_rvalue_ref = true, j = true;
        j && compiler_type.IsReferenceType(nullptr, &is_rvalue_ref); j = false) {
     CompilerType non_ref_type = compiler_type.GetNonReferenceType();
-    GetPossibleMatches(
-        valobj, non_ref_type,
-        use_dynamic, entries, did_strip_ptr, true, did_strip_typedef);
+    GetPossibleMatches(valobj, non_ref_type, use_dynamic, entries,
+                       current_flags.WithStrippedReference());
     if (non_ref_type.IsTypedefType()) {
       CompilerType deffed_referenced_type = non_ref_type.GetTypedefedType();
       deffed_referenced_type =
           is_rvalue_ref ? deffed_referenced_type.GetRValueReferenceType()
                         : deffed_referenced_type.GetLValueReferenceType();
+      // this is not exactly the usual meaning of stripping typedefs
       GetPossibleMatches(
           valobj, deffed_referenced_type,
-          use_dynamic, entries, did_strip_ptr, did_strip_ref,
-          true); // this is not exactly the usual meaning of stripping typedefs
+          use_dynamic, entries, current_flags.WithStrippedTypedef());
     }
   }
 
   if (compiler_type.IsPointerType()) {
     CompilerType non_ptr_type = compiler_type.GetPointeeType();
-    GetPossibleMatches(
-        valobj, non_ptr_type,
-        use_dynamic, entries, true, did_strip_ref, did_strip_typedef);
+    GetPossibleMatches(valobj, non_ptr_type, use_dynamic, entries,
+                       current_flags.WithStrippedPointer());
     if (non_ptr_type.IsTypedefType()) {
       CompilerType deffed_pointed_type =
           non_ptr_type.GetTypedefedType().GetPointerType();
-      const bool stripped_typedef = true;
-      GetPossibleMatches(
-          valobj, deffed_pointed_type,
-          use_dynamic, entries, did_strip_ptr, did_strip_ref,
-          stripped_typedef); // this is not exactly the usual meaning of
-                             // stripping typedefs
+      // this is not exactly the usual meaning of stripping typedefs
+      GetPossibleMatches(valobj, deffed_pointed_type, use_dynamic, entries,
+                         current_flags.WithStrippedTypedef());
     }
   }
 
@@ -244,12 +235,10 @@ void FormatManager::GetPossibleMatches(
       // from it.
       CompilerType deffed_array_type =
           element_type.GetTypedefedType().GetArrayType(array_size);
-      const bool stripped_typedef = true;
+      // this is not exactly the usual meaning of stripping typedefs
       GetPossibleMatches(
           valobj, deffed_array_type,
-          use_dynamic, entries, did_strip_ptr, did_strip_ref,
-          stripped_typedef); // this is not exactly the usual meaning of
-                             // stripping typedefs
+          use_dynamic, entries, current_flags.WithStrippedTypedef());
     }
   }
 
@@ -258,9 +247,7 @@ void FormatManager::GetPossibleMatches(
     if (Language *language = Language::FindPlugin(language_type)) {
       for (ConstString candidate :
            language->GetPossibleFormattersMatches(valobj, use_dynamic)) {
-        entries.push_back(
-            {candidate,
-             did_strip_ptr, did_strip_ref, did_strip_typedef});
+        entries.push_back({candidate, current_flags});
       }
     }
   }
@@ -268,9 +255,8 @@ void FormatManager::GetPossibleMatches(
   // try to strip typedef chains
   if (compiler_type.IsTypedefType()) {
     CompilerType deffed_type = compiler_type.GetTypedefedType();
-    GetPossibleMatches(
-        valobj, deffed_type,
-        use_dynamic, entries, did_strip_ptr, did_strip_ref, true);
+    GetPossibleMatches(valobj, deffed_type, use_dynamic, entries,
+                       current_flags.WithStrippedTypedef());
   }
 
   if (root_level) {
@@ -284,19 +270,17 @@ void FormatManager::GetPossibleMatches(
         break;
       if (unqual_compiler_ast_type.GetOpaqueQualType() !=
           compiler_type.GetOpaqueQualType())
-        GetPossibleMatches(valobj, unqual_compiler_ast_type,
-                           use_dynamic, entries, did_strip_ptr, did_strip_ref,
-                           did_strip_typedef);
+        GetPossibleMatches(valobj, unqual_compiler_ast_type, use_dynamic,
+                           entries, current_flags);
     } while (false);
 
     // if all else fails, go to static type
     if (valobj.IsDynamic()) {
       lldb::ValueObjectSP static_value_sp(valobj.GetStaticValue());
       if (static_value_sp)
-        GetPossibleMatches(
-            *static_value_sp.get(), static_value_sp->GetCompilerType(),
-            use_dynamic, entries, did_strip_ptr, did_strip_ref,
-            did_strip_typedef, true);
+        GetPossibleMatches(*static_value_sp.get(),
+                           static_value_sp->GetCompilerType(), use_dynamic,
+                           entries, current_flags, true);
     }
   }
 }


        


More information about the lldb-commits mailing list