11 #include "clang/AST/ASTContext.h" 12 #include "clang/AST/Decl.h" 13 #include "clang/AST/Type.h" 14 #include "clang/ASTMatchers/ASTMatchFinder.h" 23 const llvm::StringRef &MacroId) {
26 if (MacroId.startswith(
"[[") || MacroId.startswith(
"__attribute__"))
30 return Context.Idents.get(MacroId).hasMacroDefinition();
36 return Node.isOverloadedOperator();
41 return isa<CXXConversionDecl>(Node);
46 return Node.getParent()->hasMutableFields();
50 return Node.isParameterPack();
55 return Node.getReturnType()->isTemplateTypeParmType() ||
56 Node.getReturnType()->isInstantiationDependentType();
60 return !(Node.isThisDeclarationADefinition() && Node.isOutOfLine());
62 AST_MATCHER(QualType, isInstantiationDependentType) {
63 return Node->isInstantiationDependentType();
65 AST_MATCHER(QualType, isNonConstReferenceOrPointer) {
72 return (Node->isTemplateTypeParmType() || Node->isPointerType() ||
73 (Node->isReferenceType() &&
74 !Node.getNonReferenceType().isConstQualified()) ||
75 Node->isInstantiationDependentType());
81 NoDiscardMacro(Options.get(
"ReplacementString",
"[[nodiscard]]")) {}
84 Options.
store(Opts,
"ReplacementString", NoDiscardMacro);
91 if ((NoDiscardMacro ==
"[[nodiscard]]" && !
getLangOpts().CPlusPlus17) ||
96 cxxRecordDecl(hasAnyName(
"::std::function",
"::boost::function"));
102 allOf(isConst(), isDefinitionOrInline(),
104 returns(voidType()), isNoReturn(), isOverloadedOperator(),
105 isVariadic(), hasTemplateReturnType(),
106 hasClassMutableFields(), isConversionOperator(),
107 hasAttr(clang::attr::WarnUnusedResult),
108 hasType(isInstantiationDependentType()),
109 hasAnyParameter(anyOf(
110 parmVarDecl(anyOf(hasType(FunctionObj),
111 hasType(references(FunctionObj)))),
112 hasType(isNonConstReferenceOrPointer()),
113 hasParameterPack()))))))
119 const auto *MatchedDecl = Result.Nodes.getNodeAs<CXXMethodDecl>(
"no_discard");
121 SourceLocation
Loc = MatchedDecl->getLocation();
122 if (Loc.isInvalid() || Loc.isMacroID())
125 SourceLocation RetLoc = MatchedDecl->getInnerLocStart();
127 ASTContext &Context = *Result.Context;
129 auto Diag =
diag(RetLoc,
"function %0 should be marked " + NoDiscardMacro)
140 Diag << FixItHint::CreateInsertion(RetLoc, NoDiscardMacro +
" ");
SourceLocation Loc
'#' location in the include directive
AST_MATCHER(BinaryOperator, isAssignmentOperator)
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 bool doesNoDiscardMacroExist(ASTContext &Context, const llvm::StringRef &MacroId)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
LangOptions getLangOpts() const
Returns the language options from the context.
Base class for all clang-tidy checks.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
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.