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(),
103 returns(voidType()), isNoReturn(), isOverloadedOperator(),
104 isVariadic(), hasTemplateReturnType(),
105 hasClassMutableFields(), isConversionOperator(),
106 hasAttr(clang::attr::WarnUnusedResult),
107 hasType(isInstantiationDependentType()),
108 hasAnyParameter(anyOf(
109 parmVarDecl(anyOf(hasType(FunctionObj),
110 hasType(references(FunctionObj)))),
111 hasType(isNonConstReferenceOrPointer()),
112 hasParameterPack()))))))
118 const auto *MatchedDecl = Result.Nodes.getNodeAs<CXXMethodDecl>(
"no_discard");
120 SourceLocation
Loc = MatchedDecl->getLocation();
121 if (Loc.isInvalid() || Loc.isMacroID())
124 SourceLocation RetLoc = MatchedDecl->getInnerLocStart();
126 ASTContext &Context = *Result.Context;
128 auto Diag =
diag(RetLoc,
"function %0 should be marked " + NoDiscardMacro)
139 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.
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
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.