10 #include "clang/AST/ASTContext.h" 11 #include "clang/AST/Decl.h" 12 #include "clang/AST/Type.h" 13 #include "clang/ASTMatchers/ASTMatchFinder.h" 22 const llvm::StringRef &MacroId) {
25 if (MacroId.startswith(
"[[") || MacroId.startswith(
"__attribute__"))
29 return Context.Idents.get(MacroId).hasMacroDefinition();
35 return Node.isOverloadedOperator();
40 return isa<CXXConversionDecl>(Node);
45 return Node.getParent()->hasMutableFields();
49 return Node.isParameterPack();
54 return Node.getReturnType()->isTemplateTypeParmType() ||
55 Node.getReturnType()->isInstantiationDependentType();
59 return !(Node.isThisDeclarationADefinition() && Node.isOutOfLine());
61 AST_MATCHER(QualType, isInstantiationDependentType) {
62 return Node->isInstantiationDependentType();
64 AST_MATCHER(QualType, isNonConstReferenceOrPointer) {
71 return (Node->isTemplateTypeParmType() || Node->isPointerType() ||
72 (Node->isReferenceType() &&
73 !Node.getNonReferenceType().isConstQualified()) ||
74 Node->isInstantiationDependentType());
80 NoDiscardMacro(Options.get(
"ReplacementString",
"[[nodiscard]]")) {}
83 Options.
store(Opts,
"ReplacementString", NoDiscardMacro);
90 if ((NoDiscardMacro ==
"[[nodiscard]]" && !
getLangOpts().CPlusPlus17) ||
95 cxxRecordDecl(hasAnyName(
"::std::function",
"::boost::function"));
101 allOf(isConst(), isDefinitionOrInline(),
104 returns(hasDeclaration(decl(hasAttr(clang::attr::WarnUnusedResult)))),
105 isNoReturn(), isOverloadedOperator(),
106 isVariadic(), hasTemplateReturnType(),
107 hasClassMutableFields(), isConversionOperator(),
108 hasAttr(clang::attr::WarnUnusedResult),
109 hasType(isInstantiationDependentType()),
110 hasAnyParameter(anyOf(
111 parmVarDecl(anyOf(hasType(FunctionObj),
112 hasType(references(FunctionObj)))),
113 hasType(isNonConstReferenceOrPointer()),
114 hasParameterPack()))))))
120 const auto *MatchedDecl = Result.Nodes.getNodeAs<CXXMethodDecl>(
"no_discard");
122 SourceLocation
Loc = MatchedDecl->getLocation();
123 if (Loc.isInvalid() || Loc.isMacroID())
126 SourceLocation RetLoc = MatchedDecl->getInnerLocStart();
128 ASTContext &Context = *Result.Context;
130 auto Diag =
diag(RetLoc,
"function %0 should be marked " + NoDiscardMacro)
141 Diag << FixItHint::CreateInsertion(RetLoc, NoDiscardMacro +
" ");
SourceLocation Loc
'#' location in the include directive
static bool doesNoDiscardMacroExist(ASTContext &Context, const llvm::StringRef &MacroId)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
AST_MATCHER(Expr, isMacroID)
Base class for all clang-tidy checks.
const LangOptions & getLangOpts() const
Returns the language options from the context.
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.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.