clang-tools  9.0.0
UnusedReturnValueCheck.cpp
Go to the documentation of this file.
1 //===--- UnusedReturnValueCheck.cpp - clang-tidy---------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "../utils/OptionsUtils.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 
14 using namespace clang::ast_matchers;
15 using namespace clang::ast_matchers::internal;
16 
17 namespace clang {
18 namespace tidy {
19 namespace bugprone {
20 
21 namespace {
22 
23 // Matches functions that are instantiated from a class template member function
24 // matching InnerMatcher. Functions not instantiated from a class template
25 // member function are matched directly with InnerMatcher.
26 AST_MATCHER_P(FunctionDecl, isInstantiatedFrom, Matcher<FunctionDecl>,
27  InnerMatcher) {
28  FunctionDecl *InstantiatedFrom = Node.getInstantiatedFromMemberFunction();
29  return InnerMatcher.matches(InstantiatedFrom ? *InstantiatedFrom : Node,
30  Finder, Builder);
31 }
32 
33 } // namespace
34 
35 UnusedReturnValueCheck::UnusedReturnValueCheck(llvm::StringRef Name,
36  ClangTidyContext *Context)
37  : ClangTidyCheck(Name, Context),
38  CheckedFunctions(Options.get("CheckedFunctions",
39  "::std::async;"
40  "::std::launder;"
41  "::std::remove;"
42  "::std::remove_if;"
43  "::std::unique;"
44  "::std::unique_ptr::release;"
45  "::std::basic_string::empty;"
46  "::std::vector::empty")) {}
47 
49  Options.store(Opts, "CheckedFunctions", CheckedFunctions);
50 }
51 
52 void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) {
53  auto FunVec = utils::options::parseStringList(CheckedFunctions);
54  auto MatchedCallExpr = expr(ignoringImplicit(ignoringParenImpCasts(
55  callExpr(callee(functionDecl(
56  // Don't match void overloads of checked functions.
57  unless(returns(voidType())),
58  isInstantiatedFrom(hasAnyName(
59  std::vector<StringRef>(FunVec.begin(), FunVec.end()))))))
60  .bind("match"))));
61 
62  auto UnusedInCompoundStmt =
63  compoundStmt(forEach(MatchedCallExpr),
64  // The checker can't currently differentiate between the
65  // return statement and other statements inside GNU statement
66  // expressions, so disable the checker inside them to avoid
67  // false positives.
68  unless(hasParent(stmtExpr())));
69  auto UnusedInIfStmt =
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));
78 
79  Finder->addMatcher(
80  stmt(anyOf(UnusedInCompoundStmt, UnusedInIfStmt, UnusedInWhileStmt,
81  UnusedInDoStmt, UnusedInForStmt, UnusedInRangeForStmt,
82  UnusedInCaseStmt)),
83  this);
84 }
85 
86 void UnusedReturnValueCheck::check(const MatchFinder::MatchResult &Result) {
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",
93  DiagnosticIDs::Note);
94  }
95 }
96 
97 } // namespace bugprone
98 } // namespace tidy
99 } // namespace clang
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
Definition: Rename.cpp:36
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl, ast_matchers::internal::Matcher< CXXMethodDecl >, InnerMatcher)