diff --git a/include/llvm/Support/GlobPattern.h b/include/llvm/Support/GlobPattern.h index 3e5989d0250..b79de6f41c4 100644 --- a/include/llvm/Support/GlobPattern.h +++ b/include/llvm/Support/GlobPattern.h @@ -31,6 +31,16 @@ public: static Expected 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 Pat, StringRef S) const; diff --git a/unittests/Support/GlobPatternTest.cpp b/unittests/Support/GlobPatternTest.cpp index 17d60b2b850..7acd311b0bb 100644 --- a/unittests/Support/GlobPatternTest.cpp +++ b/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 Pat1 = GlobPattern::create("*"); + EXPECT_TRUE((bool)Pat1); + EXPECT_TRUE(Pat1->isTrivialMatchAll()); + + const char *NegativeCases[] = {"a*", "*a", "?*", "*?", "**", "\\*"}; + for (auto *P : NegativeCases) { + Expected Pat2 = GlobPattern::create(P); + EXPECT_TRUE((bool)Pat2); + EXPECT_FALSE(Pat2->isTrivialMatchAll()); + } +} }