10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
20 HeaderFileExtensions) {
22 Node.getBeginLoc(), Finder->getASTContext().getSourceManager(),
23 HeaderFileExtensions);
31 HeaderFileExtensions(Context->getHeaderFileExtensions()) {}
34 auto DefinitionMatcher =
35 anyOf(functionDecl(isDefinition(), unless(isDeleted())),
36 varDecl(isDefinition()));
37 Finder->addMatcher(namedDecl(DefinitionMatcher,
38 usesHeaderFileExtension(HeaderFileExtensions))
45 if (Result.Context->getDiagnostics().hasUncompilableErrorOccurred())
56 const auto *ND = Result.Nodes.getNodeAs<NamedDecl>(
"name-decl");
58 if (ND->isInvalidDecl())
68 if (!ND->hasExternalFormalLinkage() || ND->isInAnonymousNamespace())
71 if (
const auto *FD = dyn_cast<FunctionDecl>(ND)) {
76 if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate)
79 if (FD->isTemplateInstantiation())
83 if (
const auto *MD = dyn_cast<CXXMethodDecl>(FD)) {
84 const auto *DC = MD->getDeclContext();
85 while (DC->isRecord()) {
86 if (
const auto *RD = dyn_cast<CXXRecordDecl>(DC)) {
87 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
89 if (RD->getDescribedClassTemplate())
96 bool IsFullSpec = FD->getTemplateSpecializationKind() != TSK_Undeclared;
97 diag(FD->getLocation(),
98 "%select{function|full function template specialization}0 %1 defined "
99 "in a header file; function definitions in header files can lead to "
105 diag(FD->getLocation(),
"make as 'inline'",
107 << FixItHint::CreateInsertion(FD->getInnerLocStart(),
"inline ");
108 }
else if (
const auto *VD = dyn_cast<VarDecl>(ND)) {
110 if (VD->getDescribedVarTemplate())
113 if (VD->getDeclContext()->isDependentContext() && VD->isStaticDataMember())
116 if (isTemplateInstantiation(VD->getTemplateSpecializationKind()))
119 if (VD->hasLocalStorage() || VD->isStaticLocal())
125 if (isa<VarTemplatePartialSpecializationDecl>(VD))
128 diag(VD->getLocation(),
129 "variable %0 defined in a header file; "
130 "variable definitions in header files can lead to ODR violations")
llvm::SmallString< 256U > Name
::clang::DynTypedNode Node
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.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
AST_MATCHER_P(UserDefinedLiteral, hasLiteral, clang::ast_matchers::internal::Matcher< Expr >, InnerMatcher)
bool isExpansionLocInHeaderFile(SourceLocation Loc, const SourceManager &SM, const FileExtensionsSet &HeaderFileExtensions)
Checks whether expansion location of Loc is in header file.
llvm::SmallSet< llvm::StringRef, 5 > FileExtensionsSet