From e076fee63dd385dace3ce2036e4f64fa1b74d3f3 Mon Sep 17 00:00:00 2001 From: Nathan James Date: Tue, 10 Nov 2020 14:27:22 +0000 Subject: [PATCH] [clang-tidy][NFC] Tweak GlobList to iterate backwards 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. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D91033 --- clang-tools-extra/clang-tidy/GlobList.cpp | 12 +++++++----- clang-tools-extra/clang-tidy/GlobList.h | 6 +++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/clang-tools-extra/clang-tidy/GlobList.cpp b/clang-tools-extra/clang-tidy/GlobList.cpp index fad3616..88e6ea2 100644 --- a/clang-tools-extra/clang-tidy/GlobList.cpp +++ b/clang-tools-extra/clang-tidy/GlobList.cpp @@ -34,7 +34,7 @@ static llvm::Regex ConsumeGlob(StringRef &GlobList) { for (char C : Glob) { if (C == '*') RegexText.push_back('.'); - else if (MetaChars.find(C) != StringRef::npos) + else if (MetaChars.contains(C)) RegexText.push_back('\\'); RegexText.push_back(C); } @@ -43,6 +43,7 @@ static llvm::Regex ConsumeGlob(StringRef &GlobList) { } GlobList::GlobList(StringRef Globs) { + Items.reserve(Globs.count(',') + 1); do { GlobListItem Item; Item.IsPositive = !ConsumeNegativeIndicator(Globs); @@ -52,10 +53,11 @@ GlobList::GlobList(StringRef Globs) { } bool GlobList::contains(StringRef S) { - bool Contains = false; - for (const GlobListItem &Item : Items) { + // Iterating the container backwards as the last match determins if S is in + // the list. + for (const GlobListItem &Item : llvm::reverse(Items)) { if (Item.Regex.match(S)) - Contains = Item.IsPositive; + return Item.IsPositive; } - return Contains; + return false; } diff --git a/clang-tools-extra/clang-tidy/GlobList.h b/clang-tools-extra/clang-tidy/GlobList.h index 4edb03b..5acb6a5 100644 --- a/clang-tools-extra/clang-tidy/GlobList.h +++ b/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 namespace clang { namespace tidy { @@ -39,9 +39,9 @@ private: struct GlobListItem { bool IsPositive; - mutable llvm::Regex Regex; + llvm::Regex Regex; }; - std::vector Items; + SmallVector Items; }; } // end namespace tidy -- 2.7.4