[Lldb-commits] [lldb] fa3a2e6 - [lldb][NFCI] Refactor regex filtering logic in CommandObjectTypeFormatterList

Jorge Gorbe Moya via lldb-commits lldb-commits at lists.llvm.org
Thu Jul 21 09:22:54 PDT 2022


Author: Jorge Gorbe Moya
Date: 2022-07-21T09:22:40-07:00
New Revision: fa3a2e611d84c29ddb3b48afea0ac64ef85b2de9

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

LOG: [lldb][NFCI] Refactor regex filtering logic in CommandObjectTypeFormatterList

Extract a bit of copy/pasted regex filtering logic into a separate
function and simplify it a little bit.

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

Added: 
    

Modified: 
    lldb/source/Commands/CommandObjectType.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/source/Commands/CommandObjectType.cpp b/lldb/source/Commands/CommandObjectType.cpp
index e74d1188ee4b1..11acbb5c627f6 100644
--- a/lldb/source/Commands/CommandObjectType.cpp
+++ b/lldb/source/Commands/CommandObjectType.cpp
@@ -1055,6 +1055,15 @@ class CommandObjectTypeFormatterList : public CommandObjectParsed {
     return false;
   }
 
+  static bool ShouldListItem(llvm::StringRef s, RegularExpression *regex) {
+    // If we have a regex, it can match two kinds of results:
+    //   - An item created with that same regex string (exact string match), so
+    //     the user can list it using the same string it used at creation time.
+    //   - Items that match the regex.
+    // No regex means list everything.
+    return regex == nullptr || s == regex->GetText() || regex->Execute(s);
+  }
+
   bool DoExecute(Args &command, CommandReturnObject &result) override {
     const size_t argc = command.GetArgumentCount();
 
@@ -1096,24 +1105,13 @@ class CommandObjectTypeFormatterList : public CommandObjectParsed {
         .SetExact([&result, &formatter_regex, &any_printed](
                       const TypeMatcher &type_matcher,
                       const FormatterSharedPointer &format_sp) -> bool {
-          if (formatter_regex) {
-            bool escape = true;
-            if (type_matcher.CreatedBySameMatchString(
-                    ConstString(formatter_regex->GetText()))) {
-              escape = false;
-            } else if (formatter_regex->Execute(
-                           type_matcher.GetMatchString().GetStringRef())) {
-              escape = false;
-            }
-
-            if (escape)
-              return true;
+          if (ShouldListItem(type_matcher.GetMatchString().GetStringRef(),
+                             formatter_regex.get())) {
+            any_printed = true;
+            result.GetOutputStream().Printf(
+                "%s: %s\n", type_matcher.GetMatchString().GetCString(),
+                format_sp->GetDescription().c_str());
           }
-
-          any_printed = true;
-          result.GetOutputStream().Printf(
-              "%s: %s\n", type_matcher.GetMatchString().GetCString(),
-              format_sp->GetDescription().c_str());
           return true;
         });
 
@@ -1121,24 +1119,13 @@ class CommandObjectTypeFormatterList : public CommandObjectParsed {
         .SetWithRegex([&result, &formatter_regex, &any_printed](
                           const TypeMatcher &type_matcher,
                           const FormatterSharedPointer &format_sp) -> bool {
-          if (formatter_regex) {
-            bool escape = true;
-            if (type_matcher.CreatedBySameMatchString(
-                    ConstString(formatter_regex->GetText()))) {
-              escape = false;
-            } else if (formatter_regex->Execute(
-                           type_matcher.GetMatchString().GetStringRef())) {
-              escape = false;
-            }
-
-            if (escape)
-              return true;
+          if (ShouldListItem(type_matcher.GetMatchString().GetStringRef(),
+                             formatter_regex.get())) {
+            any_printed = true;
+            result.GetOutputStream().Printf(
+                "%s: %s\n", type_matcher.GetMatchString().GetCString(),
+                format_sp->GetDescription().c_str());
           }
-
-          any_printed = true;
-          result.GetOutputStream().Printf(
-              "%s: %s\n", type_matcher.GetMatchString().GetCString(),
-              format_sp->GetDescription().c_str());
           return true;
         });
 
@@ -1155,20 +1142,9 @@ class CommandObjectTypeFormatterList : public CommandObjectParsed {
       DataVisualization::Categories::ForEach(
           [&category_regex, &category_closure](
               const lldb::TypeCategoryImplSP &category) -> bool {
-            if (category_regex) {
-              bool escape = true;
-              if (category->GetName() == category_regex->GetText()) {
-                escape = false;
-              } else if (category_regex->Execute(category->GetName())) {
-                escape = false;
-              }
-
-              if (escape)
-                return true;
+            if (ShouldListItem(category->GetName(), category_regex.get())) {
+              category_closure(category);
             }
-
-            category_closure(category);
-
             return true;
           });
 


        


More information about the lldb-commits mailing list