summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKirill Bobyrev <kbobyrev.opensource@gmail.com>2018-08-30 12:42:19 +0000
committerKirill Bobyrev <kbobyrev.opensource@gmail.com>2018-08-30 12:42:19 +0000
commit29925890d916a03b15be78067716f338afd5a9e1 (patch)
treeb6e3ba42bdb49ff3386b67f3c9612cbdc9c6349b
parentbf3bc7117eb66d3fbc1a93f35536c8a0120b274c (diff)
downloadbcm5719-llvm-29925890d916a03b15be78067716f338afd5a9e1.tar.gz
bcm5719-llvm-29925890d916a03b15be78067716f338afd5a9e1.zip
[clang-tidy] Use simple string matching instead of Regex
Instead of parsing and compiling the `llvm::Regex` each time, it's faster to use basic string matching for filename prefix check. Reviewed by: hokein Differential Revision: https://reviews.llvm.org/D51360 llvm-svn: 341061
-rw-r--r--clang-tools-extra/clang-tidy/abseil/AbseilMatcher.h30
1 files changed, 20 insertions, 10 deletions
diff --git a/clang-tools-extra/clang-tidy/abseil/AbseilMatcher.h b/clang-tools-extra/clang-tidy/abseil/AbseilMatcher.h
index ef08bb86bc4..17b3517e173 100644
--- a/clang-tools-extra/clang-tidy/abseil/AbseilMatcher.h
+++ b/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 @@ namespace ast_matchers {
///
/// 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())
@@ -40,11 +40,21 @@ AST_POLYMORPHIC_MATCHER(isInAbseilFile,
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);
+ // Determine whether filepath contains "absl/[absl-library]" substring, where
+ // [absl-library] is AbseilLibraries list entry.
+ 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
OpenPOWER on IntegriCloud