10#include "clang/AST/ASTContext.h"
11#include "clang/AST/Decl.h"
12#include "clang/AST/Type.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
20 const llvm::StringRef &MacroId) {
23 if (MacroId.starts_with(
"[[") || MacroId.starts_with(
"__attribute__"))
27 return Context.Idents.get(MacroId).hasMacroDefinition();
33 return Node.isOverloadedOperator();
38 return isa<CXXConversionDecl>(Node);
43 return Node.getParent()->hasMutableFields();
47 return Node.isParameterPack();
52 return Node.getReturnType()->isTemplateTypeParmType() ||
53 Node.getReturnType()->isInstantiationDependentType();
57 return !(
Node.isThisDeclarationADefinition() &&
Node.isOutOfLine());
59AST_MATCHER(QualType, isInstantiationDependentType) {
60 return Node->isInstantiationDependentType();
62AST_MATCHER(QualType, isNonConstReferenceOrPointer) {
69 return (
Node->isTemplateTypeParmType() ||
Node->isPointerType() ||
70 (
Node->isReferenceType() &&
71 !
Node.getNonReferenceType().isConstQualified()) ||
72 Node->isInstantiationDependentType());
78 NoDiscardMacro(Options.get(
"ReplacementString",
"[[nodiscard]]")) {}
81 Options.
store(Opts,
"ReplacementString", NoDiscardMacro);
86 cxxRecordDecl(hasAnyName(
"::std::function",
"::boost::function"));
92 isConst(), isDefinitionOrInline(),
96 hasDeclaration(decl(hasAttr(clang::attr::WarnUnusedResult)))),
97 isNoReturn(), isOverloadedOperator(), isVariadic(),
98 hasTemplateReturnType(), hasClassMutableFields(),
99 isConversionOperator(), hasAttr(clang::attr::WarnUnusedResult),
100 hasType(isInstantiationDependentType()),
102 anyOf(parmVarDecl(anyOf(hasType(FunctionObj),
103 hasType(references(FunctionObj)))),
104 hasType(isNonConstReferenceOrPointer()),
105 hasParameterPack())))))
111 const auto *MatchedDecl = Result.Nodes.getNodeAs<CXXMethodDecl>(
"no_discard");
113 SourceLocation
Loc = MatchedDecl->getLocation();
114 if (
Loc.isInvalid() ||
Loc.isMacroID())
117 SourceLocation RetLoc = MatchedDecl->getInnerLocStart();
119 ASTContext &Context = *Result.Context;
121 auto Diag =
diag(RetLoc,
"function %0 should be marked %1")
122 << MatchedDecl << NoDiscardMacro;
132 Diag << FixItHint::CreateInsertion(RetLoc, (NoDiscardMacro +
" ").str());
136 const LangOptions &LangOpts)
const {
141 if (NoDiscardMacro ==
"[[nodiscard]]")
142 return LangOpts.CPlusPlus17;
144 return LangOpts.CPlusPlus;
llvm::SmallString< 256U > Name
::clang::DynTypedNode Node
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.
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.
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
Override this to disable registering matchers and PP callbacks if an invalid language version is bein...
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
UseNodiscardCheck(StringRef Name, ClangTidyContext *Context)
AST_MATCHER(Decl, declHasNoReturnAttr)
matches a Decl if it has a "no return" attribute of any kind
static bool doesNoDiscardMacroExist(ASTContext &Context, const llvm::StringRef &MacroId)
llvm::StringMap< ClangTidyValue > OptionMap