11 #include "../utils/OptionsUtils.h" 12 #include "clang/AST/ASTContext.h" 13 #include "clang/ASTMatchers/ASTMatchFinder.h" 27 AST_MATCHER_P(FunctionDecl, isInstantiatedFrom, Matcher<FunctionDecl>,
29 FunctionDecl *InstantiatedFrom = Node.getInstantiatedFromMemberFunction();
30 return InnerMatcher.matches(InstantiatedFrom ? *InstantiatedFrom : Node,
36 UnusedReturnValueCheck::UnusedReturnValueCheck(llvm::StringRef
Name,
39 CheckedFunctions(Options.get(
"CheckedFunctions",
45 "::std::unique_ptr::release;" 46 "::std::basic_string::empty;" 47 "::std::vector::empty")) {}
50 Options.
store(Opts,
"CheckedFunctions", CheckedFunctions);
55 auto MatchedCallExpr = expr(ignoringImplicit(ignoringParenImpCasts(
56 callExpr(callee(functionDecl(
58 unless(returns(voidType())),
59 isInstantiatedFrom(hasAnyName(
60 std::vector<StringRef>(FunVec.begin(), FunVec.end()))))))
63 auto UnusedInCompoundStmt =
64 compoundStmt(forEach(MatchedCallExpr),
69 unless(hasParent(stmtExpr())));
71 ifStmt(eachOf(hasThen(MatchedCallExpr), hasElse(MatchedCallExpr)));
72 auto UnusedInWhileStmt = whileStmt(hasBody(MatchedCallExpr));
73 auto UnusedInDoStmt = doStmt(hasBody(MatchedCallExpr));
74 auto UnusedInForStmt =
75 forStmt(eachOf(hasLoopInit(MatchedCallExpr),
76 hasIncrement(MatchedCallExpr), hasBody(MatchedCallExpr)));
77 auto UnusedInRangeForStmt = cxxForRangeStmt(hasBody(MatchedCallExpr));
78 auto UnusedInCaseStmt = switchCase(forEach(MatchedCallExpr));
81 stmt(anyOf(UnusedInCompoundStmt, UnusedInIfStmt, UnusedInWhileStmt,
82 UnusedInDoStmt, UnusedInForStmt, UnusedInRangeForStmt,
88 if (
const auto *Matched = Result.Nodes.getNodeAs<CallExpr>(
"match")) {
89 diag(Matched->getBeginLoc(),
90 "the value returned by this function should be used")
91 << Matched->getSourceRange();
92 diag(Matched->getBeginLoc(),
93 "cast the expression to void to silence this warning",
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.
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
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.
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++ -*-===//
AST_MATCHER_P(FunctionDecl, throws, internal::Matcher< Type >, InnerMatcher)
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.