11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 using namespace clang::ast_matchers;
25 Node.getLocStart(),
Finder->getASTContext().getSourceManager(),
26 HeaderFileExtensions);
31 DefinitionsInHeadersCheck::DefinitionsInHeadersCheck(StringRef
Name,
34 UseHeaderFileExtension(Options.get(
"UseHeaderFileExtension", true)),
35 RawStringHeaderFileExtensions(
36 Options.getLocalOrGlobal(
"HeaderFileExtensions",
",h,hh,hpp,hxx")) {
38 HeaderFileExtensions,
',')) {
41 llvm::errs() <<
"Invalid header file extension: "
42 << RawStringHeaderFileExtensions <<
"\n";
48 Options.
store(Opts,
"UseHeaderFileExtension", UseHeaderFileExtension);
49 Options.
store(Opts,
"HeaderFileExtensions", RawStringHeaderFileExtensions);
55 auto DefinitionMatcher =
56 anyOf(functionDecl(isDefinition(), unless(isDeleted())),
57 varDecl(isDefinition()));
58 if (UseHeaderFileExtension) {
59 Finder->addMatcher(namedDecl(DefinitionMatcher,
60 usesHeaderFileExtension(HeaderFileExtensions))
65 namedDecl(DefinitionMatcher,
66 anyOf(usesHeaderFileExtension(HeaderFileExtensions),
67 unless(isExpansionInMainFile())))
75 if (Result.Context->getDiagnostics().hasUncompilableErrorOccurred())
86 const auto *ND = Result.Nodes.getNodeAs<NamedDecl>(
"name-decl");
88 if (ND->isInvalidDecl())
97 if (ND->getLinkageInternal() == InternalLinkage)
100 if (
const auto *FD = dyn_cast<FunctionDecl>(ND)) {
105 if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate)
108 if (FD->isTemplateInstantiation())
112 if (
const auto *MD = dyn_cast<CXXMethodDecl>(FD)) {
113 const auto *DC = MD->getDeclContext();
114 while (DC->isRecord()) {
115 if (
const auto *RD = dyn_cast<CXXRecordDecl>(DC)) {
116 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
118 if (RD->getDescribedClassTemplate())
121 DC = DC->getParent();
125 bool is_full_spec = FD->getTemplateSpecializationKind() != TSK_Undeclared;
126 diag(FD->getLocation(),
127 "%select{function|full function template specialization}0 %1 defined "
128 "in a header file; function definitions in header files can lead to "
130 << is_full_spec << FD << FixItHint::CreateInsertion(
131 FD->getReturnTypeSourceRange().getBegin(),
"inline ");
132 }
else if (
const auto *VD = dyn_cast<VarDecl>(ND)) {
134 if (VD->getDeclContext()->isDependentContext() && VD->isStaticDataMember())
137 if (isTemplateInstantiation(VD->getTemplateSpecializationKind()))
140 if (VD->hasLocalStorage() || VD->isStaticLocal())
146 diag(VD->getLocation(),
147 "variable %0 defined in a header file; "
148 "variable definitions in header files can lead to ODR violations")
LangOptions getLangOpts() const
Returns the language options from the context.
bool parseHeaderFileExtensions(StringRef AllHeaderFileExtensions, HeaderFileExtensionsSet &HeaderFileExtensions, char delimiter)
Parses header file extensions from a semicolon-separated list.
std::unique_ptr< ast_matchers::MatchFinder > Finder
bool isExpansionLocInHeaderFile(SourceLocation Loc, const SourceManager &SM, const HeaderFileExtensionsSet &HeaderFileExtensions)
Checks whether expansion location of Loc is in header file.
AST_MATCHER_P(CXXForRangeStmt, hasRangeBeginEndStmt, ast_matchers::internal::Matcher< DeclStmt >, InnerMatcher)
Base class for all clang-tidy checks.
llvm::SmallSet< llvm::StringRef, 5 > HeaderFileExtensionsSet
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.
std::map< std::string, std::string > OptionMap
ClangTidyContext & Context
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.