clang-tools 8.0.0

AbseilMatcher.h
Go to the documentation of this file.
1 //===- AbseilMatcher.h - clang-tidy ---------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include <algorithm>
13
14 namespace clang {
15 namespace ast_matchers {
16
17 /// Matches AST nodes that were found within Abseil files.
18 ///
19 /// Example matches Y but not X
20 /// (matcher = cxxRecordDecl(isInAbseilFile())
21 /// \code
22 /// #include "absl/strings/internal-file.h"
23 /// class X {};
24 /// \endcode
25 /// absl/strings/internal-file.h:
26 /// \code
27 /// class Y {};
28 /// \endcode
29 ///
30 /// Usable as: Matcher<Decl>, Matcher<Stmt>, Matcher<TypeLoc>,
31 /// Matcher<NestedNameSpecifierLoc>
32 AST_POLYMORPHIC_MATCHER(
33 isInAbseilFile, AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc,
34 NestedNameSpecifierLoc)) {
35 auto &SourceManager = Finder->getASTContext().getSourceManager();
36 SourceLocation Loc = SourceManager.getSpellingLoc(Node.getBeginLoc());
37 if (Loc.isInvalid())
38 return false;
39 const FileEntry *FileEntry =
40 SourceManager.getFileEntryForID(SourceManager.getFileID(Loc));
41 if (!FileEntry)
42 return false;
43 // Determine whether filepath contains "absl/[absl-library]" substring, where
44 // [absl-library] is AbseilLibraries list entry.
45 StringRef Path = FileEntry->getName();
46 static constexpr llvm::StringLiteral AbslPrefix("absl/");
47 size_t PrefixPosition = Path.find(AbslPrefix);
48 if (PrefixPosition == StringRef::npos)
49 return false;
50 Path = Path.drop_front(PrefixPosition + AbslPrefix.size());
51 static const char *AbseilLibraries[] = {
52 "algorithm", "base", "container", "debugging", "flags",
53 "hash", "iterator", "memory", "meta", "numeric",
54 "random", "strings", "synchronization", "time", "types",
55 "utility"};
56 return std::any_of(
57 std::begin(AbseilLibraries), std::end(AbseilLibraries),
58 [&](const char *Library) { return Path.startswith(Library); });
59 }
60
61 } // namespace ast_matchers
62 } // namespace clang
Loc
SourceLocation Loc
&#39;#&#39; location in the include directive
Definition: IncludeOrderCheck.cpp:37
clang::ast_matchers::AST_POLYMORPHIC_MATCHER
AST_POLYMORPHIC_MATCHER(isInAbseilFile, AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc, NestedNameSpecifierLoc))
Matches AST nodes that were found within Abseil files.
Definition: AbseilMatcher.h:32
Path
std::vector< HeaderHandle > Path
Definition: PreprocessorTracker.cpp:538
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18