10#include "../utils/FileExtensionsUtils.h"
11#include "clang/AST/Decl.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
14#include "clang/ASTMatchers/ASTMatchersMacros.h"
15#include "clang/Basic/SourceLocation.h"
16#include "clang/Basic/Specifiers.h"
17#include "clang/Lex/Token.h"
18#include "llvm/ADT/DenseSet.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/SmallVector.h"
28 static llvm::ArrayRef<
29 std::pair<misc::UseInternalLinkageCheck::FixModeKind, StringRef>>
52static bool isInMainFile(SourceLocation L, SourceManager &SM,
57 if (SM.isInMainFile(L))
60 L = SM.getIncludeLoc(SM.getFileID(L));
69 HeaderFileExtensions) {
70 return llvm::all_of(
Node.redecls(), [&](
const Decl *D) {
71 return isInMainFile(D->getLocation(),
72 Finder->getASTContext().getSourceManager(),
73 HeaderFileExtensions);
78 AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
80 return Node.getStorageClass() == SC_Extern;
83AST_MATCHER(FunctionDecl, isAllocationOrDeallocationOverloadedFunction) {
90 static const llvm::DenseSet<OverloadedOperatorKind> OverloadedOperators{
91 OverloadedOperatorKind::OO_New,
92 OverloadedOperatorKind::OO_Array_New,
93 OverloadedOperatorKind::OO_Delete,
94 OverloadedOperatorKind::OO_Array_Delete,
96 return OverloadedOperators.contains(
Node.getOverloadedOperator());
104 HeaderFileExtensions(Context->getHeaderFileExtensions()),
105 FixMode(Options.get(
"FixMode",
FixModeKind::UseStatic)) {}
113 allOf(isFirstDecl(), isAllRedeclsInMainFile(HeaderFileExtensions),
116 isStaticStorageClass(), isInAnonymousNamespace(),
118 isExternStorageClass(), isExternC(),
120 isExplicitTemplateSpecialization(),
121 hasAncestor(decl(anyOf(
127 functionDecl(Common, hasBody(),
128 unless(anyOf(cxxMethodDecl(),
129 isAllocationOrDeallocationOverloadedFunction(),
133 Finder->addMatcher(varDecl(Common, hasGlobalStorage()).bind(
"var"),
this);
137 "%0 %1 can be made static or moved into an anonymous namespace "
138 "to enforce internal linkage";
141 if (
const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>(
"fn")) {
142 DiagnosticBuilder DB =
diag(FD->getLocation(),
Message) <<
"function" << FD;
143 const SourceLocation FixLoc = FD->getInnerLocStart();
144 if (FixLoc.isInvalid() || FixLoc.isMacroID())
147 DB << FixItHint::CreateInsertion(FixLoc,
"static ");
150 if (
const auto *VD = Result.Nodes.getNodeAs<VarDecl>(
"var")) {
154 if (
getLangOpts().CPlusPlus && VD->getType().isConstQualified())
157 DiagnosticBuilder DB =
diag(VD->getLocation(),
Message) <<
"variable" << VD;
158 const SourceLocation FixLoc = VD->getInnerLocStart();
159 if (FixLoc.isInvalid() || FixLoc.isMacroID())
162 DB << FixItHint::CreateInsertion(FixLoc,
"static ");
165 llvm_unreachable(
"");
const FunctionDecl * Decl
llvm::SmallString< 256U > Name
::clang::DynTypedNode Node
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.
const LangOptions & getLangOpts() const
Returns the language options from the context.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
UseInternalLinkageCheck(StringRef Name, ClangTidyContext *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.
AST_MATCHER(Expr, isMacroID)
AST_POLYMORPHIC_MATCHER(isInAbseilFile, AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc, NestedNameSpecifierLoc))
Matches AST nodes that were found within Abseil files.
AST_MATCHER_P(UserDefinedLiteral, hasLiteral, clang::ast_matchers::internal::Matcher< Expr >, InnerMatcher)
static constexpr StringRef Message
bool isSpellingLocInHeaderFile(SourceLocation Loc, SourceManager &SM, const FileExtensionsSet &HeaderFileExtensions)
Checks whether spelling location of Loc is in header file.
llvm::SmallSet< llvm::StringRef, 5 > FileExtensionsSet
llvm::StringMap< ClangTidyValue > OptionMap
static llvm::ArrayRef< std::pair< misc::UseInternalLinkageCheck::FixModeKind, StringRef > > getEnumMapping()
This class should be specialized by any enum type that needs to be converted to and from an llvm::Str...