kbobyrev created this revision.
kbobyrev added a reviewer: hokein.
kbobyrev added a project: clang-tools-extra.
Herald added a subscriber: xazax.hun.
Instead of parsing and compiling the `llvm::Regex` each time, it's faster to 
use basic string matching for filename prefix check.


https://reviews.llvm.org/D51360

Files:
  clang-tools-extra/clang-tidy/abseil/AbseilMatcher.h


Index: clang-tools-extra/clang-tidy/abseil/AbseilMatcher.h
===================================================================
--- clang-tools-extra/clang-tidy/abseil/AbseilMatcher.h
+++ clang-tools-extra/clang-tidy/abseil/AbseilMatcher.h
@@ -6,9 +6,10 @@
 // License. See LICENSE.TXT for details.
 //
 
//===----------------------------------------------------------------------===//
-//
+
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
+#include <algorithm>
 
 namespace clang {
 namespace ast_matchers {
@@ -28,10 +29,9 @@
 ///
 /// Usable as: Matcher<Decl>, Matcher<Stmt>, Matcher<TypeLoc>,
 /// Matcher<NestedNameSpecifierLoc>
-
-AST_POLYMORPHIC_MATCHER(isInAbseilFile,
-                        AST_POLYMORPHIC_SUPPORTED_TYPES(
-                            Decl, Stmt, TypeLoc, NestedNameSpecifierLoc)) {
+AST_POLYMORPHIC_MATCHER(
+    isInAbseilFile, AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc,
+                                                    NestedNameSpecifierLoc)) {
   auto &SourceManager = Finder->getASTContext().getSourceManager();
   SourceLocation Loc = Node.getBeginLoc();
   if (Loc.isInvalid())
@@ -41,10 +41,16 @@
   if (!FileEntry)
     return false;
   StringRef Filename = FileEntry->getName();
-  llvm::Regex RE(
-      "absl/(algorithm|base|container|debugging|memory|meta|numeric|strings|"
-      "synchronization|time|types|utility)");
-  return RE.match(Filename);
+  const llvm::SmallString<5> AbslPrefix("absl/");
+  if (!Filename.startswith(AbslPrefix))
+    return false;
+  Filename = Filename.drop_front(AbslPrefix.size());
+  static const char *AbseilLibraries[] = {
+      "algorithm",       "base", "container", "debugging",
+      "memory",          "meta", "numeric",   "strings",
+      "synchronization", "time", "types",     "utility"};
+  return std::any_of(std::begin(AbseilLibraries), std::end(AbseilLibraries),
+                     [&](const char *Library) { return Library == Filename; });
 }
 
 } // namespace ast_matchers


Index: clang-tools-extra/clang-tidy/abseil/AbseilMatcher.h
===================================================================
--- clang-tools-extra/clang-tidy/abseil/AbseilMatcher.h
+++ clang-tools-extra/clang-tidy/abseil/AbseilMatcher.h
@@ -6,9 +6,10 @@
 // License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
-//
+
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
+#include <algorithm>
 
 namespace clang {
 namespace ast_matchers {
@@ -28,10 +29,9 @@
 ///
 /// Usable as: Matcher<Decl>, Matcher<Stmt>, Matcher<TypeLoc>,
 /// Matcher<NestedNameSpecifierLoc>
-
-AST_POLYMORPHIC_MATCHER(isInAbseilFile,
-                        AST_POLYMORPHIC_SUPPORTED_TYPES(
-                            Decl, Stmt, TypeLoc, NestedNameSpecifierLoc)) {
+AST_POLYMORPHIC_MATCHER(
+    isInAbseilFile, AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc,
+                                                    NestedNameSpecifierLoc)) {
   auto &SourceManager = Finder->getASTContext().getSourceManager();
   SourceLocation Loc = Node.getBeginLoc();
   if (Loc.isInvalid())
@@ -41,10 +41,16 @@
   if (!FileEntry)
     return false;
   StringRef Filename = FileEntry->getName();
-  llvm::Regex RE(
-      "absl/(algorithm|base|container|debugging|memory|meta|numeric|strings|"
-      "synchronization|time|types|utility)");
-  return RE.match(Filename);
+  const llvm::SmallString<5> AbslPrefix("absl/");
+  if (!Filename.startswith(AbslPrefix))
+    return false;
+  Filename = Filename.drop_front(AbslPrefix.size());
+  static const char *AbseilLibraries[] = {
+      "algorithm",       "base", "container", "debugging",
+      "memory",          "meta", "numeric",   "strings",
+      "synchronization", "time", "types",     "utility"};
+  return std::any_of(std::begin(AbseilLibraries), std::end(AbseilLibraries),
+                     [&](const char *Library) { return Library == Filename; });
 }
 
 } // namespace ast_matchers
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to