10#include "../utils/Matchers.h"
11#include "../utils/OptionsUtils.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
16using namespace clang::ast_matchers::internal;
25AST_MATCHER_P(FunctionDecl, isInstantiatedFrom, Matcher<FunctionDecl>,
27 FunctionDecl *InstantiatedFrom =
Node.getInstantiatedFromMemberFunction();
28 return InnerMatcher.matches(InstantiatedFrom ? *InstantiatedFrom : Node,
36 CheckedFunctions(Options.get(
"CheckedFunctions",
42 "::std::unique_ptr::release;"
43 "::std::basic_string::empty;"
44 "::std::vector::empty;"
45 "::std::back_inserter;"
54 "::std::map::lower_bound;"
55 "::std::multimap::equal_range;"
56 "::std::multimap::upper_bound;"
60 "::std::setprecision;"
113 "::pthread_getspecific;"
114 "::pthread_mutex_trylock;"
128 CheckedReturnTypes(utils::options::parseStringList(
129 Options.get(
"CheckedReturnTypes",
"::std::error_code;"
130 "::std::error_condition;"
133 "::boost::system::error_code"))) {}
136 Options.
store(Opts,
"CheckedFunctions", CheckedFunctions);
144 auto MatchedCallExpr = expr(ignoringImplicit(ignoringParenImpCasts(
145 callExpr(callee(functionDecl(
147 unless(returns(voidType())),
148 anyOf(isInstantiatedFrom(hasAnyName(FunVec)),
149 returns(hasCanonicalType(hasDeclaration(
151 CheckedReturnTypes)))))))))
154 auto UnusedInCompoundStmt =
155 compoundStmt(forEach(MatchedCallExpr),
160 unless(hasParent(stmtExpr())));
161 auto UnusedInIfStmt =
162 ifStmt(eachOf(hasThen(MatchedCallExpr), hasElse(MatchedCallExpr)));
163 auto UnusedInWhileStmt = whileStmt(hasBody(MatchedCallExpr));
164 auto UnusedInDoStmt = doStmt(hasBody(MatchedCallExpr));
165 auto UnusedInForStmt =
166 forStmt(eachOf(hasLoopInit(MatchedCallExpr),
167 hasIncrement(MatchedCallExpr), hasBody(MatchedCallExpr)));
168 auto UnusedInRangeForStmt = cxxForRangeStmt(hasBody(MatchedCallExpr));
169 auto UnusedInCaseStmt = switchCase(forEach(MatchedCallExpr));
172 stmt(anyOf(UnusedInCompoundStmt, UnusedInIfStmt, UnusedInWhileStmt,
173 UnusedInDoStmt, UnusedInForStmt, UnusedInRangeForStmt,
179 if (
const auto *Matched = Result.Nodes.getNodeAs<CallExpr>(
"match")) {
180 diag(Matched->getBeginLoc(),
181 "the value returned by this function should be used")
182 << Matched->getSourceRange();
183 diag(Matched->getBeginLoc(),
184 "cast the expression to void to silence this warning",
185 DiagnosticIDs::Note);
CodeCompletionBuilder Builder
::clang::DynTypedNode Node
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.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
UnusedReturnValueCheck(StringRef Name, ClangTidyContext *Context)
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
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.
AST_MATCHER_P(FunctionDecl, parameterCountGE, unsigned, N)
Matches functions that have at least the specified amount of parameters.
inline ::clang::ast_matchers::internal::Matcher< NamedDecl > matchesAnyListedName(llvm::ArrayRef< StringRef > NameList)
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
std::vector< StringRef > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
llvm::StringMap< ClangTidyValue > OptionMap