Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:18

0001 //===- AbseilMatcher.h - clang-tidy ---------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #include "clang/AST/ASTContext.h"
0010 #include "clang/ASTMatchers/ASTMatchFinder.h"
0011 #include <algorithm>
0012 
0013 namespace clang::ast_matchers {
0014 
0015 /// Matches AST nodes that were found within Abseil files.
0016 ///
0017 /// Example matches Y but not X
0018 ///     (matcher = cxxRecordDecl(isInAbseilFile())
0019 /// \code
0020 ///   #include "absl/strings/internal-file.h"
0021 ///   class X {};
0022 /// \endcode
0023 /// absl/strings/internal-file.h:
0024 /// \code
0025 ///   class Y {};
0026 /// \endcode
0027 ///
0028 /// Usable as: Matcher<Decl>, Matcher<Stmt>, Matcher<TypeLoc>,
0029 /// Matcher<NestedNameSpecifierLoc>
0030 AST_POLYMORPHIC_MATCHER(
0031     isInAbseilFile, AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc,
0032                                                     NestedNameSpecifierLoc)) {
0033   auto &SourceManager = Finder->getASTContext().getSourceManager();
0034   SourceLocation Loc = SourceManager.getSpellingLoc(Node.getBeginLoc());
0035   if (Loc.isInvalid())
0036     return false;
0037   OptionalFileEntryRef FileEntry =
0038       SourceManager.getFileEntryRefForID(SourceManager.getFileID(Loc));
0039   if (!FileEntry)
0040     return false;
0041   // Determine whether filepath contains "absl/[absl-library]" substring, where
0042   // [absl-library] is AbseilLibraries list entry.
0043   StringRef Path = FileEntry->getName();
0044   static constexpr llvm::StringLiteral AbslPrefix("absl/");
0045   size_t PrefixPosition = Path.find(AbslPrefix);
0046   if (PrefixPosition == StringRef::npos)
0047     return false;
0048   Path = Path.drop_front(PrefixPosition + AbslPrefix.size());
0049   static const char *AbseilLibraries[] = {
0050       "algorithm", "base",     "container", "debugging", "flags",
0051       "hash",      "iterator", "memory",    "meta",      "numeric",
0052       "profiling", "random",   "status",    "strings",   "synchronization",
0053       "time",      "types",    "utility"};
0054   return llvm::any_of(AbseilLibraries, [&](const char *Library) {
0055     return Path.starts_with(Library);
0056   });
0057 }
0058 
0059 } // namespace clang::ast_matchers