clang-tools  7.0.0
UnusedReturnValueCheck.cpp
Go to the documentation of this file.
1 //===--- UnusedReturnValueCheck.cpp - clang-tidy---------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "UnusedReturnValueCheck.h"
11 #include "../utils/OptionsUtils.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 
15 using namespace clang::ast_matchers;
16 using namespace clang::ast_matchers::internal;
17 
18 namespace clang {
19 namespace tidy {
20 namespace bugprone {
21 
22 namespace {
23 
24 // Matches functions that are instantiated from a class template member function
25 // matching InnerMatcher. Functions not instantiated from a class template
26 // member function are matched directly with InnerMatcher.
27 AST_MATCHER_P(FunctionDecl, isInstantiatedFrom, Matcher<FunctionDecl>,
28  InnerMatcher) {
29  FunctionDecl *InstantiatedFrom = Node.getInstantiatedFromMemberFunction();
30  return InnerMatcher.matches(InstantiatedFrom ? *InstantiatedFrom : Node,
31  Finder, Builder);
32 }
33 
34 } // namespace
35 
36 UnusedReturnValueCheck::UnusedReturnValueCheck(llvm::StringRef Name,
37  ClangTidyContext *Context)
38  : ClangTidyCheck(Name, Context),
39  CheckedFunctions(Options.get("CheckedFunctions",
40  "::std::async;"
41  "::std::launder;"
42  "::std::remove;"
43  "::std::remove_if;"
44  "::std::unique;"
45  "::std::unique_ptr::release;"
46  "::std::basic_string::empty;"
47  "::std::vector::empty")) {}
48 
50  Options.store(Opts, "CheckedFunctions", CheckedFunctions);
51 }
52 
53 void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) {
54  auto FunVec = utils::options::parseStringList(CheckedFunctions);
55  auto MatchedCallExpr = expr(ignoringImplicit(ignoringParenImpCasts(
56  callExpr(callee(functionDecl(
57  // Don't match void overloads of checked functions.
58  unless(returns(voidType())),
59  isInstantiatedFrom(hasAnyName(
60  std::vector<StringRef>(FunVec.begin(), FunVec.end()))))))
61  .bind("match"))));
62 
63  auto UnusedInCompoundStmt =
64  compoundStmt(forEach(MatchedCallExpr),
65  // The checker can't currently differentiate between the
66  // return statement and other statements inside GNU statement
67  // expressions, so disable the checker inside them to avoid
68  // false positives.
69  unless(hasParent(stmtExpr())));
70  auto UnusedInIfStmt =
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));
79 
80  Finder->addMatcher(
81  stmt(anyOf(UnusedInCompoundStmt, UnusedInIfStmt, UnusedInWhileStmt,
82  UnusedInDoStmt, UnusedInForStmt, UnusedInRangeForStmt,
83  UnusedInCaseStmt)),
84  this);
85 }
86 
87 void UnusedReturnValueCheck::check(const MatchFinder::MatchResult &Result) {
88  if (const auto *Matched = Result.Nodes.getNodeAs<CallExpr>("match")) {
89  diag(Matched->getLocStart(),
90  "the value returned by this function should be used")
91  << Matched->getSourceRange();
92  diag(Matched->getLocStart(),
93  "cast the expression to void to silence this warning",
94  DiagnosticIDs::Note);
95  }
96 }
97 
98 } // namespace bugprone
99 } // namespace tidy
100 } // namespace clang
llvm::StringRef Name
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.
Definition: ClangTidy.cpp:460
Base class for all clang-tidy checks.
Definition: ClangTidy.h:127
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.
std::map< std::string, std::string > OptionMap
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&#39;s name.
Definition: ClangTidy.cpp:427