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 StringRef &MacroId) {
23 if (MacroId.starts_with(
"[[") || MacroId.starts_with(
"__attribute__"))
27 return Context.Idents.get(MacroId).hasMacroDefinition();
31AST_MATCHER(CXXMethodDecl, isOverloadedOperator) {
33 return Node.isOverloadedOperator();
35AST_MATCHER(CXXMethodDecl, isConversionOperator) {
38 return isa<CXXConversionDecl>(Node);
40AST_MATCHER(CXXMethodDecl, hasClassMutableFields) {
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"));
87 auto NoDiscardClassTemplateSpecialization =
88 classTemplateSpecializationDecl(hasSpecializedTemplate(classTemplateDecl(
89 has(cxxRecordDecl(hasAttr(attr::WarnUnusedResult))))));
95 isConst(), isDefinitionOrInline(),
98 returns(hasDeclaration(decl(hasAttr(attr::WarnUnusedResult)))),
99 returns(hasUnqualifiedDesugaredType(recordType(
100 hasDeclaration(NoDiscardClassTemplateSpecialization)))),
101 isNoReturn(), isOverloadedOperator(), isVariadic(),
102 hasTemplateReturnType(), hasClassMutableFields(),
103 isConversionOperator(), hasAttr(attr::WarnUnusedResult),
104 hasType(isInstantiationDependentType()),
106 anyOf(parmVarDecl(anyOf(hasType(FunctionObj),
107 hasType(references(FunctionObj)))),
108 hasType(isNonConstReferenceOrPointer()),
109 hasParameterPack())))))
115 const auto *MatchedDecl = Result.Nodes.getNodeAs<CXXMethodDecl>(
"no_discard");
117 const SourceLocation Loc = MatchedDecl->getLocation();
118 if (Loc.isInvalid() || Loc.isMacroID())
121 const SourceLocation RetLoc = MatchedDecl->getInnerLocStart();
123 ASTContext &Context = *Result.Context;
125 auto Diag = diag(RetLoc,
"function %0 should be marked %1")
126 << MatchedDecl << NoDiscardMacro;
136 Diag << FixItHint::CreateInsertion(RetLoc, (NoDiscardMacro +
" ").str());
140 const LangOptions &LangOpts)
const {
145 if (NoDiscardMacro ==
"[[nodiscard]]")
146 return LangOpts.CPlusPlus17;
148 return LangOpts.CPlusPlus;
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
UseNodiscardCheck(StringRef Name, ClangTidyContext *Context)
AST_MATCHER(BinaryOperator, isRelationalOperator)
static bool doesNoDiscardMacroExist(ASTContext &Context, const StringRef &MacroId)
llvm::StringMap< ClangTidyValue > OptionMap