11#include "clang/AST/ASTContext.h"
12#include "clang/AST/Decl.h"
13#include "clang/AST/Type.h"
14#include "clang/ASTMatchers/ASTMatchFinder.h"
22 const StringRef &MacroId) {
25 if (MacroId.starts_with(
"[[") || MacroId.starts_with(
"__attribute__"))
29 return Context.Idents.get(MacroId).hasMacroDefinition();
36 return isa<CXXConversionDecl>(Node);
41 return Node.getParent()->hasMutableFields();
45 return Node.isParameterPack();
50 return Node.getReturnType()->isTemplateTypeParmType() ||
51 Node.getReturnType()->isInstantiationDependentType();
55 return !(Node.isThisDeclarationADefinition() && Node.isOutOfLine());
57AST_MATCHER(QualType, isInstantiationDependentType) {
58 return Node->isInstantiationDependentType();
60AST_MATCHER(QualType, isNonConstReferenceOrPointer) {
67 return (Node->isTemplateTypeParmType() || Node->isPointerType() ||
68 (Node->isReferenceType() &&
69 !Node.getNonReferenceType().isConstQualified()) ||
70 Node->isInstantiationDependentType());
76 NoDiscardMacro(Options.get(
"ReplacementString",
"[[nodiscard]]")) {}
79 Options.store(Opts,
"ReplacementString", NoDiscardMacro);
83 const auto FunctionObj =
84 cxxRecordDecl(hasAnyName(
"::std::function",
"::boost::function"));
85 const auto NoDiscardClassTemplateSpecialization =
86 classTemplateSpecializationDecl(hasSpecializedTemplate(classTemplateDecl(
87 has(cxxRecordDecl(hasAttr(attr::WarnUnusedResult))))));
93 isConst(), isDefinitionOrInline(),
96 returns(hasDeclaration(decl(hasAttr(attr::WarnUnusedResult)))),
97 returns(hasUnqualifiedDesugaredType(recordType(
98 hasDeclaration(NoDiscardClassTemplateSpecialization)))),
99 isNoReturn(), isOverloadedOperator(), isVariadic(),
100 hasTemplateReturnType(), hasClassMutableFields(),
101 isConversionOperator(), hasAttr(attr::WarnUnusedResult),
102 hasType(isInstantiationDependentType()),
104 anyOf(parmVarDecl(anyOf(hasType(FunctionObj),
105 hasType(references(FunctionObj)))),
106 hasType(isNonConstReferenceOrPointer()),
107 hasParameterPack())))))
113 const auto *MatchedDecl = Result.Nodes.getNodeAs<CXXMethodDecl>(
"no_discard");
115 const SourceLocation Loc = MatchedDecl->getLocation();
116 if (Loc.isInvalid() || Loc.isMacroID())
119 const SourceLocation RetLoc = MatchedDecl->getInnerLocStart();
121 ASTContext &Context = *Result.Context;
123 const auto Diag = diag(RetLoc,
"function %0 should be marked %1")
124 << MatchedDecl << NoDiscardMacro;
134 Diag << FixItHint::CreateInsertion(RetLoc, (NoDiscardMacro +
" ").str());
138 const LangOptions &LangOpts)
const {
143 if (NoDiscardMacro ==
"[[nodiscard]]")
144 return LangOpts.CPlusPlus17;
146 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