10 #include "../utils/OptionsUtils.h" 11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 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")) {}
49 Options.
store(Opts,
"CheckedFunctions", CheckedFunctions);
54 auto MatchedCallExpr = expr(ignoringImplicit(ignoringParenImpCasts(
55 callExpr(callee(functionDecl(
57 unless(returns(voidType())),
58 isInstantiatedFrom(hasAnyName(
59 std::vector<StringRef>(FunVec.begin(), FunVec.end()))))))
62 auto UnusedInCompoundStmt =
63 compoundStmt(forEach(MatchedCallExpr),
68 unless(hasParent(stmtExpr())));
70 ifStmt(eachOf(hasThen(MatchedCallExpr), hasElse(MatchedCallExpr)));
71 auto UnusedInWhileStmt = whileStmt(hasBody(MatchedCallExpr));
72 auto UnusedInDoStmt = doStmt(hasBody(MatchedCallExpr));
73 auto UnusedInForStmt =
74 forStmt(eachOf(hasLoopInit(MatchedCallExpr),
75 hasIncrement(MatchedCallExpr), hasBody(MatchedCallExpr)));
76 auto UnusedInRangeForStmt = cxxForRangeStmt(hasBody(MatchedCallExpr));
77 auto UnusedInCaseStmt = switchCase(forEach(MatchedCallExpr));
80 stmt(anyOf(UnusedInCompoundStmt, UnusedInIfStmt, UnusedInWhileStmt,
81 UnusedInDoStmt, UnusedInForStmt, UnusedInRangeForStmt,
87 if (
const auto *Matched = Result.Nodes.getNodeAs<CallExpr>(
"match")) {
88 diag(Matched->getBeginLoc(),
89 "the value returned by this function should be used")
90 << Matched->getSourceRange();
91 diag(Matched->getBeginLoc(),
92 "cast the expression to void to silence this warning",
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 store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
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
CodeCompletionBuilder Builder
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
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl, ast_matchers::internal::Matcher< CXXMethodDecl >, InnerMatcher)