10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
17AST_POLYMORPHIC_MATCHER_P(isInHeaderFile,
18 AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
22 Node.getBeginLoc(), Finder->getASTContext().getSourceManager(),
23 HeaderFileExtensions);
27 return llvm::isa<CXXMethodDecl>(&Node);
29AST_MATCHER(VarDecl, isStaticDataMember) {
return Node.isStaticDataMember(); }
35 std::optional<StringRef> HeaderFileExtensionsOption =
37 RawStringHeaderFileExtensions =
39 if (HeaderFileExtensionsOption) {
44 << RawStringHeaderFileExtensions;
47 HeaderFileExtensions = Context->getHeaderFileExtensions();
52 Options.
store(Opts,
"HeaderFileExtensions", RawStringHeaderFileExtensions);
57 functionDecl(isStaticStorageClass(),
58 unless(anyOf(isInHeaderFile(HeaderFileExtensions),
59 isInAnonymousNamespace(), isMemberFunction())))
63 varDecl(isStaticStorageClass(),
64 unless(anyOf(isInHeaderFile(HeaderFileExtensions),
65 isInAnonymousNamespace(), isStaticLocal(),
66 isStaticDataMember(), hasType(isConstQualified()))))
72 if (
const auto *MatchedDecl = Result.Nodes.getNodeAs<NamedDecl>(
"x")) {
73 StringRef
Type = llvm::isa<VarDecl>(MatchedDecl) ?
"variable" :
"function";
74 diag(MatchedDecl->getLocation(),
75 "%0 %1 declared 'static', move to anonymous namespace instead")
76 <<
Type << MatchedDecl;
std::optional< StringRef > get(StringRef LocalName) const
Read a named option from the Context.
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
DiagnosticBuilder configurationDiag(StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning) const
Adds a diagnostic to report errors in the check's configuration.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
UseAnonymousNamespaceCheck(StringRef Name, ClangTidyContext *Context)
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
AST_MATCHER(Decl, declHasNoReturnAttr)
matches a Decl if it has a "no return" attribute of any kind
bool isExpansionLocInHeaderFile(SourceLocation Loc, const SourceManager &SM, const FileExtensionsSet &HeaderFileExtensions)
Checks whether expansion location of Loc is in header file.
StringRef defaultHeaderFileExtensions()
Returns recommended default value for the list of header file extensions.
StringRef defaultFileExtensionDelimiters()
Returns recommended default value for the list of file extension delimiters.
bool parseFileExtensions(StringRef AllFileExtensions, FileExtensionsSet &FileExtensions, StringRef Delimiters)
Parses header file extensions from a semicolon-separated list.
llvm::SmallSet< llvm::StringRef, 5 > FileExtensionsSet
llvm::StringMap< ClangTidyValue > OptionMap