[llvm] 6040e2a - [Support] Add GlobPattern::isTrivialMatchAll()

Andrew Ng via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 16 02:27:46 PDT 2020


Author: Andrew Ng
Date: 2020-09-16T10:26:11+01:00
New Revision: 6040e2a6d97d9f9445715dfc468c3112f40e2588

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

LOG: [Support] Add GlobPattern::isTrivialMatchAll()

GlobPattern::isTrivialMatchAll() returns true for the GlobPattern "*"
which will match all inputs.

This can be used to avoid performing expensive preparation of the input
for match() when the result of the match will always be true.

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

Added: 
    

Modified: 
    llvm/include/llvm/Support/GlobPattern.h
    llvm/unittests/Support/GlobPatternTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/GlobPattern.h b/llvm/include/llvm/Support/GlobPattern.h
index 3e5989d025007..b79de6f41c494 100644
--- a/llvm/include/llvm/Support/GlobPattern.h
+++ b/llvm/include/llvm/Support/GlobPattern.h
@@ -31,6 +31,16 @@ class GlobPattern {
   static Expected<GlobPattern> create(StringRef Pat);
   bool match(StringRef S) const;
 
+  // Returns true for glob pattern "*". Can be used to avoid expensive
+  // preparation/acquisition of the input for match().
+  bool isTrivialMatchAll() const {
+    if (Prefix && Prefix->empty()) {
+      assert(!Suffix);
+      return true;
+    }
+    return false;
+  }
+
 private:
   bool matchOne(ArrayRef<BitVector> Pat, StringRef S) const;
 

diff  --git a/llvm/unittests/Support/GlobPatternTest.cpp b/llvm/unittests/Support/GlobPatternTest.cpp
index 17d60b2b85087..7acd311b0bb92 100644
--- a/llvm/unittests/Support/GlobPatternTest.cpp
+++ b/llvm/unittests/Support/GlobPatternTest.cpp
@@ -133,4 +133,17 @@ TEST_F(GlobPatternTest, ExtSym) {
   EXPECT_TRUE((bool)Pat2);
   EXPECT_TRUE(Pat2->match("\xFF"));
 }
+
+TEST_F(GlobPatternTest, IsTrivialMatchAll) {
+  Expected<GlobPattern> Pat1 = GlobPattern::create("*");
+  EXPECT_TRUE((bool)Pat1);
+  EXPECT_TRUE(Pat1->isTrivialMatchAll());
+
+  const char *NegativeCases[] = {"a*", "*a", "?*", "*?", "**", "\\*"};
+  for (auto *P : NegativeCases) {
+    Expected<GlobPattern> Pat2 = GlobPattern::create(P);
+    EXPECT_TRUE((bool)Pat2);
+    EXPECT_FALSE(Pat2->isTrivialMatchAll());
+  }
+}
 }


        


More information about the llvm-commits mailing list