[PATCH] D91033: [clang-tidy][NFC] Tweak GlobList to iterate backwards

Nathan James via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 10 06:27:39 PST 2020


This revision was automatically updated to reflect the committed changes.
Closed by commit rGe076fee63dd3: [clang-tidy][NFC] Tweak GlobList to iterate backwards (authored by njames93).

Changed prior to commit:
  https://reviews.llvm.org/D91033?vs=303725&id=304166#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D91033/new/

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
@@ -34,7 +34,7 @@
   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 @@
 }
 
 GlobList::GlobList(StringRef Globs) {
+  Items.reserve(Globs.count(',') + 1);
   do {
     GlobListItem Item;
     Item.IsPositive = !ConsumeNegativeIndicator(Globs);
@@ -52,10 +53,11 @@
 }
 
 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;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D91033.304166.patch
Type: text/x-patch
Size: 1784 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201110/158d67c0/attachment.bin>


More information about the cfe-commits mailing list