11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 25 Node.getLocStart(), Finder->getASTContext().getSourceManager(),
26 HeaderFileExtensions);
31 DefinitionsInHeadersCheck::DefinitionsInHeadersCheck(StringRef
Name,
34 UseHeaderFileExtension(Options.get(
"UseHeaderFileExtension", true)),
35 RawStringHeaderFileExtensions(Options.getLocalOrGlobal(
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())
100 if (!ND->hasExternalFormalLinkage() && !ND->isInAnonymousNamespace())
103 if (
const auto *FD = dyn_cast<FunctionDecl>(ND)) {
108 if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate)
111 if (FD->isTemplateInstantiation())
115 if (
const auto *MD = dyn_cast<CXXMethodDecl>(FD)) {
116 const auto *DC = MD->getDeclContext();
117 while (DC->isRecord()) {
118 if (
const auto *RD = dyn_cast<CXXRecordDecl>(DC)) {
119 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
121 if (RD->getDescribedClassTemplate())
124 DC = DC->getParent();
128 bool is_full_spec = FD->getTemplateSpecializationKind() != TSK_Undeclared;
129 diag(FD->getLocation(),
130 "%select{function|full function template specialization}0 %1 defined " 131 "in a header file; function definitions in header files can lead to " 133 << is_full_spec << FD << FixItHint::CreateInsertion(
134 FD->getReturnTypeSourceRange().getBegin(),
"inline ");
135 }
else if (
const auto *VD = dyn_cast<VarDecl>(ND)) {
137 if (VD->getDeclContext()->isDependentContext() && VD->isStaticDataMember())
140 if (isTemplateInstantiation(VD->getTemplateSpecializationKind()))
143 if (VD->hasLocalStorage() || VD->isStaticLocal())
149 diag(VD->getLocation(),
150 "variable %0 defined in a header file; " 151 "variable definitions in header files can lead to ODR violations")
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.
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.
bool isExpansionLocInHeaderFile(SourceLocation Loc, const SourceManager &SM, const HeaderFileExtensionsSet &HeaderFileExtensions)
Checks whether expansion location of Loc is in header file.
Base class for all clang-tidy checks.
StringRef defaultHeaderFileExtensions()
Returns recommended default value for the list of header file extensions.
llvm::SmallSet< llvm::StringRef, 5 > HeaderFileExtensionsSet
std::map< std::string, std::string > OptionMap
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
AST_MATCHER_P(FunctionDecl, throws, internal::Matcher< Type >, InnerMatcher)
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.