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")) {
42 llvm::errs() <<
"Invalid header file extension: "
43 << RawStringHeaderFileExtensions <<
"\n";
49 Options.
store(Opts,
"UseHeaderFileExtension", UseHeaderFileExtension);
50 Options.
store(Opts,
"HeaderFileExtensions", RawStringHeaderFileExtensions);
56 auto DefinitionMatcher =
57 anyOf(functionDecl(isDefinition(), unless(isDeleted())),
58 varDecl(isDefinition()));
59 if (UseHeaderFileExtension) {
60 Finder->addMatcher(namedDecl(DefinitionMatcher,
61 usesHeaderFileExtension(HeaderFileExtensions))
66 namedDecl(DefinitionMatcher,
67 anyOf(usesHeaderFileExtension(HeaderFileExtensions),
68 unless(isExpansionInMainFile())))
76 if (Result.Context->getDiagnostics().hasErrorOccurred())
87 const auto *ND = Result.Nodes.getNodeAs<NamedDecl>(
"name-decl");
89 if (ND->isInvalidDecl())
98 if (ND->getLinkageInternal() == InternalLinkage)
101 if (
const auto *FD = dyn_cast<FunctionDecl>(ND)) {
106 if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate)
109 if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
113 if (
const auto *MD = dyn_cast<CXXMethodDecl>(FD)) {
114 const auto *DC = MD->getDeclContext();
115 while (DC->isRecord()) {
116 if (
const auto *RD = dyn_cast<CXXRecordDecl>(DC)) {
117 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
119 if (RD->getDescribedClassTemplate())
122 DC = DC->getParent();
126 diag(FD->getLocation(),
127 "function %0 defined in a header file; "
128 "function definitions in header files can lead to ODR violations")
129 << FD << FixItHint::CreateInsertion(
130 FD->getReturnTypeSourceRange().getBegin(),
"inline ");
131 }
else if (
const auto *VD = dyn_cast<VarDecl>(ND)) {
133 if (VD->getDeclContext()->isDependentContext() && VD->isStaticDataMember())
135 if (VD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
138 if (VD->hasLocalStorage() || VD->isStaticLocal())
141 diag(VD->getLocation(),
142 "variable %0 defined in a header file; "
143 "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.