[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
This commit is contained in:
Kirill Bobyrev 2018-08-30 12:42:19 +00:00
parent bf3bc7117e
commit 29925890d9
1 changed files with 20 additions and 10 deletions

View File

@ -6,9 +6,10 @@
// License. See LICENSE.TXT for details. // License. See LICENSE.TXT for details.
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
//
#include "clang/AST/ASTContext.h" #include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/ASTMatchers/ASTMatchFinder.h"
#include <algorithm>
namespace clang { namespace clang {
namespace ast_matchers { namespace ast_matchers {
@ -28,10 +29,9 @@ namespace ast_matchers {
/// ///
/// Usable as: Matcher<Decl>, Matcher<Stmt>, Matcher<TypeLoc>, /// Usable as: Matcher<Decl>, Matcher<Stmt>, Matcher<TypeLoc>,
/// Matcher<NestedNameSpecifierLoc> /// Matcher<NestedNameSpecifierLoc>
AST_POLYMORPHIC_MATCHER(
AST_POLYMORPHIC_MATCHER(isInAbseilFile, isInAbseilFile, AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc,
AST_POLYMORPHIC_SUPPORTED_TYPES( NestedNameSpecifierLoc)) {
Decl, Stmt, TypeLoc, NestedNameSpecifierLoc)) {
auto &SourceManager = Finder->getASTContext().getSourceManager(); auto &SourceManager = Finder->getASTContext().getSourceManager();
SourceLocation Loc = Node.getBeginLoc(); SourceLocation Loc = Node.getBeginLoc();
if (Loc.isInvalid()) if (Loc.isInvalid())
@ -40,11 +40,21 @@ AST_POLYMORPHIC_MATCHER(isInAbseilFile,
SourceManager.getFileEntryForID(SourceManager.getFileID(Loc)); SourceManager.getFileEntryForID(SourceManager.getFileID(Loc));
if (!FileEntry) if (!FileEntry)
return false; return false;
StringRef Filename = FileEntry->getName(); // Determine whether filepath contains "absl/[absl-library]" substring, where
llvm::Regex RE( // [absl-library] is AbseilLibraries list entry.
"absl/(algorithm|base|container|debugging|memory|meta|numeric|strings|" StringRef Path = FileEntry->getName();
"synchronization|time|types|utility)"); const static llvm::SmallString<5> AbslPrefix("absl/");
return RE.match(Filename); 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 } // namespace ast_matchers