njames93 created this revision. njames93 added reviewers: aaron.ballman, alexfh, gribozavr2. Herald added subscribers: cfe-commits, xazax.hun. Herald added a project: clang. njames93 requested review of this revision.
By iterating backwards over the globs we can exit the loop as soon as we find a match. While we're here: - Regex doesn't need to be mutable. - We can reserve the amount of Globs needed ahead of time. - Using a SmallVector with size 0 is slightly more space efficient than a std::vector. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D91033 Files: clang-tools-extra/clang-tidy/GlobList.cpp clang-tools-extra/clang-tidy/GlobList.h Index: clang-tools-extra/clang-tidy/GlobList.h =================================================================== --- clang-tools-extra/clang-tidy/GlobList.h +++ clang-tools-extra/clang-tidy/GlobList.h @@ -10,9 +10,9 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GLOBLIST_H #include "clang/Basic/LLVM.h" +#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Regex.h" -#include <vector> namespace clang { namespace tidy { @@ -39,9 +39,9 @@ struct GlobListItem { bool IsPositive; - mutable llvm::Regex Regex; + llvm::Regex Regex; }; - std::vector<GlobListItem> Items; + SmallVector<GlobListItem, 0> Items; }; } // end namespace tidy Index: clang-tools-extra/clang-tidy/GlobList.cpp =================================================================== --- clang-tools-extra/clang-tidy/GlobList.cpp +++ clang-tools-extra/clang-tidy/GlobList.cpp @@ -43,6 +43,7 @@ } GlobList::GlobList(StringRef Globs) { + Items.reserve(Globs.count(',') + 1); do { GlobListItem Item; Item.IsPositive = !ConsumeNegativeIndicator(Globs); @@ -52,10 +53,9 @@ } bool GlobList::contains(StringRef S) { - bool Contains = false; - for (const GlobListItem &Item : Items) { + for (const GlobListItem &Item : llvm::reverse(Items)) { if (Item.Regex.match(S)) - Contains = Item.IsPositive; + return Item.IsPositive; } - return Contains; + return false; }
Index: clang-tools-extra/clang-tidy/GlobList.h =================================================================== --- clang-tools-extra/clang-tidy/GlobList.h +++ clang-tools-extra/clang-tidy/GlobList.h @@ -10,9 +10,9 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GLOBLIST_H #include "clang/Basic/LLVM.h" +#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Regex.h" -#include <vector> namespace clang { namespace tidy { @@ -39,9 +39,9 @@ struct GlobListItem { bool IsPositive; - mutable llvm::Regex Regex; + llvm::Regex Regex; }; - std::vector<GlobListItem> Items; + SmallVector<GlobListItem, 0> Items; }; } // end namespace tidy Index: clang-tools-extra/clang-tidy/GlobList.cpp =================================================================== --- clang-tools-extra/clang-tidy/GlobList.cpp +++ clang-tools-extra/clang-tidy/GlobList.cpp @@ -43,6 +43,7 @@ } GlobList::GlobList(StringRef Globs) { + Items.reserve(Globs.count(',') + 1); do { GlobListItem Item; Item.IsPositive = !ConsumeNegativeIndicator(Globs); @@ -52,10 +53,9 @@ } bool GlobList::contains(StringRef S) { - bool Contains = false; - for (const GlobListItem &Item : Items) { + for (const GlobListItem &Item : llvm::reverse(Items)) { if (Item.Regex.match(S)) - Contains = Item.IsPositive; + return Item.IsPositive; } - return Contains; + return false; }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits