10 #include "../utils/OptionsUtils.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
15 using namespace clang::ast_matchers::internal;
26 AST_MATCHER_P(FunctionDecl, isInstantiatedFrom, Matcher<FunctionDecl>,
28 FunctionDecl *InstantiatedFrom = Node.getInstantiatedFromMemberFunction();
29 return InnerMatcher.matches(InstantiatedFrom ? *InstantiatedFrom : Node,
35 UnusedReturnValueCheck::UnusedReturnValueCheck(llvm::StringRef
Name,
38 CheckedFunctions(Options.get(
"CheckedFunctions",
44 "::std::unique_ptr::release;"
45 "::std::basic_string::empty;"
46 "::std::vector::empty;"
47 "::std::back_inserter;"
56 "::std::map::lower_bound;"
57 "::std::multimap::equal_range;"
58 "::std::multimap::upper_bound;"
62 "::std::setprecision;"
115 "::pthread_getspecific;"
116 "::pthread_mutex_trylock;"
132 Options.
store(Opts,
"CheckedFunctions", CheckedFunctions);
137 auto MatchedCallExpr = expr(ignoringImplicit(ignoringParenImpCasts(
138 callExpr(callee(functionDecl(
140 unless(returns(voidType())),
141 isInstantiatedFrom(hasAnyName(
142 std::vector<StringRef>(FunVec.begin(), FunVec.end()))))))
145 auto UnusedInCompoundStmt =
146 compoundStmt(forEach(MatchedCallExpr),
151 unless(hasParent(stmtExpr())));
152 auto UnusedInIfStmt =
153 ifStmt(eachOf(hasThen(MatchedCallExpr), hasElse(MatchedCallExpr)));
154 auto UnusedInWhileStmt = whileStmt(hasBody(MatchedCallExpr));
155 auto UnusedInDoStmt = doStmt(hasBody(MatchedCallExpr));
156 auto UnusedInForStmt =
157 forStmt(eachOf(hasLoopInit(MatchedCallExpr),
158 hasIncrement(MatchedCallExpr), hasBody(MatchedCallExpr)));
159 auto UnusedInRangeForStmt = cxxForRangeStmt(hasBody(MatchedCallExpr));
160 auto UnusedInCaseStmt = switchCase(forEach(MatchedCallExpr));
163 stmt(anyOf(UnusedInCompoundStmt, UnusedInIfStmt, UnusedInWhileStmt,
164 UnusedInDoStmt, UnusedInForStmt, UnusedInRangeForStmt,
170 if (
const auto *Matched = Result.Nodes.getNodeAs<CallExpr>(
"match")) {
171 diag(Matched->getBeginLoc(),
172 "the value returned by this function should be used")
173 << Matched->getSourceRange();
174 diag(Matched->getBeginLoc(),
175 "cast the expression to void to silence this warning",
176 DiagnosticIDs::Note);