clang-tools 8.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
49 void UnusedReturnValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
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->getBeginLoc(),
90 "the value returned by this function should be used")
91 << Matched->getSourceRange();
92 diag(Matched->getBeginLoc(),
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
clang::ast_matchers
Definition: AbseilMatcher.h:15
clang::tidy::OptionsView::store
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:473
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidy.h:127
clang::tidy::utils::options::parseStringList
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
Definition: OptionsUtils.cpp:19
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidy.h:183
clang::tidy::bugprone::UnusedReturnValueCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: UnusedReturnValueCheck.cpp:87
clang::tidy::bugprone::UnusedReturnValueCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: UnusedReturnValueCheck.cpp:53
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:98
Result
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: ClangdServer.cpp:60
clang::tidy::bugprone::UnusedReturnValueCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: UnusedReturnValueCheck.cpp:49
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
clang::ast_matchers::AST_MATCHER_P
AST_MATCHER_P(FunctionDecl, throws, internal::Matcher< Type >, InnerMatcher)
Definition: ExceptionEscapeCheck.cpp:139
internal
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:100
UnusedReturnValueCheck.h
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidy.cpp:438