[Lldb-commits] [lldb] ef3fa23 - [Formatters][NFCI] Replace 'is_regex' arguments with an enum.

Jorge Gorbe Moya via lldb-commits lldb-commits at lists.llvm.org
Tue Sep 13 13:00:42 PDT 2022


Author: Jorge Gorbe Moya
Date: 2022-09-13T12:50:55-07:00
New Revision: ef3fa232b338fd73475656ed61847295b0beef24

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

LOG: [Formatters][NFCI] Replace 'is_regex' arguments with an enum.

Modify `SBTypeNameSpecifier` and `lldb_private::TypeMatcher` so they
have an enum value for the type of matching to perform instead of an
`m_is_regex` boolean value.

This change paves the way for introducing formatter matching based on
the result of a python callback in addition to the existing name-based
matching. See the RFC thread at
https://discourse.llvm.org/t/rfc-python-callback-for-data-formatters-type-matching/64204
for more details.

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

Added: 
    

Modified: 
    lldb/bindings/interface/SBTypeNameSpecifier.i
    lldb/include/lldb/API/SBTypeNameSpecifier.h
    lldb/include/lldb/DataFormatters/FormatClasses.h
    lldb/include/lldb/DataFormatters/FormattersContainer.h
    lldb/include/lldb/lldb-enumerations.h
    lldb/source/API/SBTypeNameSpecifier.cpp
    lldb/source/Core/FormatEntity.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/bindings/interface/SBTypeNameSpecifier.i b/lldb/bindings/interface/SBTypeNameSpecifier.i
index 772f7c174093f..4bd5f205cfbc6 100644
--- a/lldb/bindings/interface/SBTypeNameSpecifier.i
+++ b/lldb/bindings/interface/SBTypeNameSpecifier.i
@@ -20,6 +20,9 @@ namespace lldb {
         SBTypeNameSpecifier (const char* name,
                              bool is_regex = false);
 
+        SBTypeNameSpecifier (const char* name,
+                             lldb::FormatterMatchType match_type);
+
         SBTypeNameSpecifier (SBType type);
 
         SBTypeNameSpecifier (const lldb::SBTypeNameSpecifier &rhs);
@@ -40,6 +43,9 @@ namespace lldb {
         lldb::SBType
         GetType ();
 
+        lldb::FormatterMatchType
+        GetMatchType();
+
         bool
         IsRegex();
 

diff  --git a/lldb/include/lldb/API/SBTypeNameSpecifier.h b/lldb/include/lldb/API/SBTypeNameSpecifier.h
index eee424c1710a4..56a85d72c723b 100644
--- a/lldb/include/lldb/API/SBTypeNameSpecifier.h
+++ b/lldb/include/lldb/API/SBTypeNameSpecifier.h
@@ -20,6 +20,9 @@ class LLDB_API SBTypeNameSpecifier {
 
   SBTypeNameSpecifier(const char *name, bool is_regex = false);
 
+  SBTypeNameSpecifier(const char *name,
+                      lldb::FormatterMatchType match_type);
+
   SBTypeNameSpecifier(SBType type);
 
   SBTypeNameSpecifier(const lldb::SBTypeNameSpecifier &rhs);
@@ -34,6 +37,8 @@ class LLDB_API SBTypeNameSpecifier {
 
   SBType GetType();
 
+  lldb::FormatterMatchType GetMatchType();
+
   bool IsRegex();
 
   bool GetDescription(lldb::SBStream &description,

diff  --git a/lldb/include/lldb/DataFormatters/FormatClasses.h b/lldb/include/lldb/DataFormatters/FormatClasses.h
index f8f17b6d5d8ae..ac2b070a55cdc 100644
--- a/lldb/include/lldb/DataFormatters/FormatClasses.h
+++ b/lldb/include/lldb/DataFormatters/FormatClasses.h
@@ -133,21 +133,23 @@ class TypeNameSpecifierImpl {
 public:
   TypeNameSpecifierImpl() = default;
 
-  TypeNameSpecifierImpl(llvm::StringRef name, bool is_regex)
-      : m_is_regex(is_regex) {
+  TypeNameSpecifierImpl(llvm::StringRef name,
+                        lldb::FormatterMatchType match_type)
+      : m_match_type(match_type) {
     m_type.m_type_name = std::string(name);
   }
 
-  // if constructing with a given type, is_regex cannot be true since we are
-  // giving an exact type to match
-  TypeNameSpecifierImpl(lldb::TypeSP type) : m_is_regex(false) {
+  // if constructing with a given type, we consider that a case of exact match.
+  TypeNameSpecifierImpl(lldb::TypeSP type)
+      : m_match_type(lldb::eFormatterMatchExact) {
     if (type) {
       m_type.m_type_name = std::string(type->GetName().GetStringRef());
       m_type.m_compiler_type = type->GetForwardCompilerType();
     }
   }
 
-  TypeNameSpecifierImpl(CompilerType type) : m_is_regex(false) {
+  TypeNameSpecifierImpl(CompilerType type)
+      : m_match_type(lldb::eFormatterMatchExact) {
     if (type.IsValid()) {
       m_type.m_type_name.assign(type.GetTypeName().GetCString());
       m_type.m_compiler_type = type;
@@ -166,10 +168,12 @@ class TypeNameSpecifierImpl {
     return CompilerType();
   }
 
-  bool IsRegex() { return m_is_regex; }
+  lldb::FormatterMatchType GetMatchType() { return m_match_type; }
+
+  bool IsRegex() { return m_match_type == lldb::eFormatterMatchRegex; }
 
 private:
-  bool m_is_regex = false;
+  lldb::FormatterMatchType m_match_type = lldb::eFormatterMatchExact;
   // TODO: Replace this with TypeAndOrName.
   struct TypeOrName {
     std::string m_type_name;

diff  --git a/lldb/include/lldb/DataFormatters/FormattersContainer.h b/lldb/include/lldb/DataFormatters/FormattersContainer.h
index 2f56218c43a7d..58df8b9610734 100644
--- a/lldb/include/lldb/DataFormatters/FormattersContainer.h
+++ b/lldb/include/lldb/DataFormatters/FormattersContainer.h
@@ -41,9 +41,11 @@ class IFormatChangeListener {
 class TypeMatcher {
   RegularExpression m_type_name_regex;
   ConstString m_type_name;
-  /// False if m_type_name_regex should be used for matching. False if this is
-  /// just matching by comparing with m_type_name string.
-  bool m_is_regex;
+  /// Indicates what kind of matching strategy should be used:
+  /// - eFormatterMatchExact: match the exact type name in m_type_name.
+  /// - eFormatterMatchRegex: match using the RegularExpression object
+  ///   `m_type_name_regex` instead.
+  lldb::FormatterMatchType m_match_type;
 
   // if the user tries to add formatters for, say, "struct Foo" those will not
   // match any type because of the way we strip qualifiers from typenames this
@@ -71,22 +73,25 @@ class TypeMatcher {
   TypeMatcher() = delete;
   /// Creates a matcher that accepts any type with exactly the given type name.
   TypeMatcher(ConstString type_name)
-      : m_type_name(type_name), m_is_regex(false) {}
+      : m_type_name(type_name), m_match_type(lldb::eFormatterMatchExact) {}
   /// Creates a matcher that accepts any type matching the given regex.
   TypeMatcher(RegularExpression regex)
-      : m_type_name_regex(std::move(regex)), m_is_regex(true) {}
+      : m_type_name_regex(std::move(regex)),
+        m_match_type(lldb::eFormatterMatchRegex) {}
 
   /// True iff this matches the given type name.
   bool Matches(ConstString type_name) const {
-    if (m_is_regex)
+    if (m_match_type == lldb::eFormatterMatchRegex)
       return m_type_name_regex.Execute(type_name.GetStringRef());
     return m_type_name == type_name ||
            StripTypeName(m_type_name) == StripTypeName(type_name);
   }
 
+  lldb::FormatterMatchType GetMatchType() const { return m_match_type; }
+
   /// Returns the underlying match string for this TypeMatcher.
   ConstString GetMatchString() const {
-    if (m_is_regex)
+    if (m_match_type == lldb::eFormatterMatchRegex)
       return ConstString(m_type_name_regex.GetText());
     return StripTypeName(m_type_name);
   }
@@ -176,7 +181,8 @@ template <typename ValueType> class FormattersContainer {
       return lldb::TypeNameSpecifierImplSP();
     TypeMatcher type_matcher = m_map[index].first;
     return std::make_shared<TypeNameSpecifierImpl>(
-        type_matcher.GetMatchString().GetStringRef(), true);
+        type_matcher.GetMatchString().GetStringRef(),
+        type_matcher.GetMatchType());
   }
 
   void Clear() {

diff  --git a/lldb/include/lldb/lldb-enumerations.h b/lldb/include/lldb/lldb-enumerations.h
index 264838e3c33ee..0f4fdfdf6f393 100644
--- a/lldb/include/lldb/lldb-enumerations.h
+++ b/lldb/include/lldb/lldb-enumerations.h
@@ -832,6 +832,15 @@ enum TemplateArgumentKind {
   eTemplateArgumentKindNullPtr,
 };
 
+/// Type of match to be performed when looking for a formatter for a data type.
+/// Used by classes like SBTypeNameSpecifier or lldb_private::TypeMatcher.
+enum FormatterMatchType {
+  eFormatterMatchExact,
+  eFormatterMatchRegex,
+
+  eLastFormatterMatchType = eFormatterMatchRegex,
+};
+
 /// Options that can be set for a formatter to alter its behavior. Not
 /// all of these are applicable to all formatter types.
 FLAGS_ENUM(TypeOptions){eTypeOptionNone = (0u),

diff  --git a/lldb/source/API/SBTypeNameSpecifier.cpp b/lldb/source/API/SBTypeNameSpecifier.cpp
index bc83a1d664d0d..d1dc2953c9b93 100644
--- a/lldb/source/API/SBTypeNameSpecifier.cpp
+++ b/lldb/source/API/SBTypeNameSpecifier.cpp
@@ -20,8 +20,15 @@ using namespace lldb_private;
 SBTypeNameSpecifier::SBTypeNameSpecifier() { LLDB_INSTRUMENT_VA(this); }
 
 SBTypeNameSpecifier::SBTypeNameSpecifier(const char *name, bool is_regex)
-    : m_opaque_sp(new TypeNameSpecifierImpl(name, is_regex)) {
+    : SBTypeNameSpecifier(name, is_regex ? eFormatterMatchRegex
+                                         : eFormatterMatchExact) {
   LLDB_INSTRUMENT_VA(this, name, is_regex);
+}
+
+SBTypeNameSpecifier::SBTypeNameSpecifier(const char *name,
+                                         FormatterMatchType match_type)
+    : m_opaque_sp(new TypeNameSpecifierImpl(name, match_type)) {
+  LLDB_INSTRUMENT_VA(this, name, match_type);
 
   if (name == nullptr || (*name) == 0)
     m_opaque_sp.reset();
@@ -72,13 +79,20 @@ SBType SBTypeNameSpecifier::GetType() {
   return SBType();
 }
 
+FormatterMatchType SBTypeNameSpecifier::GetMatchType() {
+  LLDB_INSTRUMENT_VA(this);
+  if (!IsValid())
+    return eFormatterMatchExact;
+  return m_opaque_sp->GetMatchType();
+}
+
 bool SBTypeNameSpecifier::IsRegex() {
   LLDB_INSTRUMENT_VA(this);
 
   if (!IsValid())
     return false;
 
-  return m_opaque_sp->IsRegex();
+  return m_opaque_sp->GetMatchType() == eFormatterMatchRegex;
 }
 
 bool SBTypeNameSpecifier::GetDescription(
@@ -116,7 +130,7 @@ bool SBTypeNameSpecifier::IsEqualTo(lldb::SBTypeNameSpecifier &rhs) {
   if (!IsValid())
     return !rhs.IsValid();
 
-  if (IsRegex() != rhs.IsRegex())
+  if (GetMatchType() != rhs.GetMatchType())
     return false;
   if (GetName() == nullptr || rhs.GetName() == nullptr)
     return false;

diff  --git a/lldb/source/Core/FormatEntity.cpp b/lldb/source/Core/FormatEntity.cpp
index 41c4aba63825e..3b0b569bd51ec 100644
--- a/lldb/source/Core/FormatEntity.cpp
+++ b/lldb/source/Core/FormatEntity.cpp
@@ -825,7 +825,7 @@ static bool DumpValue(Stream &s, const SymbolContext *sc,
     bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(),
                          target->GetBitfieldBitSize());
     auto type_sp = std::make_shared<TypeNameSpecifierImpl>(
-        bitfield_name.GetString(), false);
+        bitfield_name.GetString(), lldb::eFormatterMatchExact);
     if (val_obj_display ==
             ValueObject::eValueObjectRepresentationStyleSummary &&
         !DataVisualization::GetSummaryForType(type_sp))


        


More information about the lldb-commits mailing list