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 "llvm/ADT/STLExtras.h"
25 static llvm::ArrayRef<
26 std::pair<misc::UseInternalLinkageCheck::FixModeKind, StringRef>>
47static bool isInMainFile(SourceLocation L, SourceManager &SM,
52 if (SM.isInMainFile(L))
55 L = SM.getIncludeLoc(SM.getFileID(L));
64 HeaderFileExtensions) {
65 return llvm::all_of(
Node.redecls(), [&](
const Decl *D) {
66 return isInMainFile(D->getLocation(),
67 Finder->getASTContext().getSourceManager(),
68 HeaderFileExtensions);
73 AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
75 return Node.getStorageClass() == SC_Extern;
83 HeaderFileExtensions(Context->getHeaderFileExtensions()),
84 FixMode(Options.get(
"FixMode",
FixModeKind::UseStatic)) {}
92 allOf(isFirstDecl(), isAllRedeclsInMainFile(HeaderFileExtensions),
95 isStaticStorageClass(), isInAnonymousNamespace(),
97 isExternStorageClass(), isExternC(),
99 isExplicitTemplateSpecialization(),
101 hasAncestor(friendDecl()))));
103 functionDecl(Common, unless(cxxMethodDecl()), unless(isMain()))
106 Finder->addMatcher(varDecl(Common, hasGlobalStorage()).bind(
"var"),
this);
110 "%0 %1 can be made static or moved into an anonymous namespace "
111 "to enforce internal linkage";
114 if (
const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>(
"fn")) {
115 DiagnosticBuilder DB =
diag(FD->getLocation(),
Message) <<
"function" << FD;
116 SourceLocation FixLoc = FD->getTypeSpecStartLoc();
117 if (FixLoc.isInvalid() || FixLoc.isMacroID())
120 DB << FixItHint::CreateInsertion(FixLoc,
"static ");
123 if (
const auto *VD = Result.Nodes.getNodeAs<VarDecl>(
"var")) {
127 if (
getLangOpts().CPlusPlus && VD->getType().isConstQualified())
130 DiagnosticBuilder DB =
diag(VD->getLocation(),
Message) <<
"variable" << VD;
131 SourceLocation FixLoc = VD->getTypeSpecStartLoc();
132 if (FixLoc.isInvalid() || FixLoc.isMacroID())
135 DB << FixItHint::CreateInsertion(FixLoc,
"static ");
138 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_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)
AST_MATCHER(Decl, declHasNoReturnAttr)
matches a Decl if it has a "no return" attribute of any kind
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...