kbobyrev updated this revision to Diff 163302.
kbobyrev marked an inline comment as done.
kbobyrev added a comment.

Ah, correct. I totally forgot about the `^string$` for the exact match.

This should not change behavior now. I believe what you propose would make the 
code easier (there'd only be `AbseilLibraries = {"absl/algorithm", ...}` and 
the final `return std::any_of(..., [](...) { return Path.find(Library) != 
StringRef::npos; });` but the performance would be worse (because of the 
presence of the `absl/` prefix in each entry). Would you consider this less 
efficient but easier-to-follow approach better?


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,23 +29,30 @@
 ///
 /// 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())
     return false;
   const FileEntry *FileEntry =
       SourceManager.getFileEntryForID(SourceManager.getFileID(Loc));
   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);
+  StringRef Path = FileEntry->getName();
+  const static llvm::SmallString<5> AbslPrefix("absl/");
+  size_t PrefixPosition = Path.find(AbslPrefix);
+  if (PrefixPosition == StringRef::npos)
+    return false;
+  Path = Path.drop_front(PrefixPosition + 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 Path.startswith(Library); });
 }
 
 } // 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,23 +29,30 @@
 ///
 /// 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())
     return false;
   const FileEntry *FileEntry =
       SourceManager.getFileEntryForID(SourceManager.getFileID(Loc));
   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);
+  StringRef Path = FileEntry->getName();
+  const static llvm::SmallString<5> AbslPrefix("absl/");
+  size_t PrefixPosition = Path.find(AbslPrefix);
+  if (PrefixPosition == StringRef::npos)
+    return false;
+  Path = Path.drop_front(PrefixPosition + 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 Path.startswith(Library); });
 }
 
 } // 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