| 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 | |
| 9 | #include "UnusedReturnValueCheck.h" |
| 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::tidy::bugprone { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | // Matches functions that are instantiated from a class template member function |
| 22 | // matching InnerMatcher. Functions not instantiated from a class template |
| 23 | // member function are matched directly with InnerMatcher. |
| 24 | AST_MATCHER_P(FunctionDecl, isInstantiatedFrom, Matcher<FunctionDecl>, |
| 25 | InnerMatcher) { |
| 26 | FunctionDecl *InstantiatedFrom = Node.getInstantiatedFromMemberFunction(); |
| 27 | return InnerMatcher.matches(InstantiatedFrom ? *InstantiatedFrom : Node, |
| 28 | Finder, Builder); |
| 29 | } |
| 30 | |
| 31 | } // namespace |
| 32 | |
| 33 | UnusedReturnValueCheck::UnusedReturnValueCheck(llvm::StringRef Name, |
| 34 | ClangTidyContext *Context) |
| 35 | : ClangTidyCheck(Name, Context), |
| 36 | CheckedFunctions(Options.get("CheckedFunctions", |
| 37 | "::std::async;" |
| 38 | "::std::launder;" |
| 39 | "::std::remove;" |
| 40 | "::std::remove_if;" |
| 41 | "::std::unique;" |
| 42 | "::std::unique_ptr::release;" |
| 43 | "::std::basic_string::empty;" |
| 44 | "::std::vector::empty;" |
| 45 | "::std::back_inserter;" |
| 46 | "::std::distance;" |
| 47 | "::std::find;" |
| 48 | "::std::find_if;" |
| 49 | "::std::inserter;" |
| 50 | "::std::lower_bound;" |
| 51 | "::std::make_pair;" |
| 52 | "::std::map::count;" |
| 53 | "::std::map::find;" |
| 54 | "::std::map::lower_bound;" |
| 55 | "::std::multimap::equal_range;" |
| 56 | "::std::multimap::upper_bound;" |
| 57 | "::std::set::count;" |
| 58 | "::std::set::find;" |
| 59 | "::std::setfill;" |
| 60 | "::std::setprecision;" |
| 61 | "::std::setw;" |
| 62 | "::std::upper_bound;" |
| 63 | "::std::vector::at;" |
| 64 | // C standard library |
| 65 | "::bsearch;" |
| 66 | "::ferror;" |
| 67 | "::feof;" |
| 68 | "::isalnum;" |
| 69 | "::isalpha;" |
| 70 | "::isblank;" |
| 71 | "::iscntrl;" |
| 72 | "::isdigit;" |
| 73 | "::isgraph;" |
| 74 | "::islower;" |
| 75 | "::isprint;" |
| 76 | "::ispunct;" |
| 77 | "::isspace;" |
| 78 | "::isupper;" |
| 79 | "::iswalnum;" |
| 80 | "::iswprint;" |
| 81 | "::iswspace;" |
| 82 | "::isxdigit;" |
| 83 | "::memchr;" |
| 84 | "::memcmp;" |
| 85 | "::strcmp;" |
| 86 | "::strcoll;" |
| 87 | "::strncmp;" |
| 88 | "::strpbrk;" |
| 89 | "::strrchr;" |
| 90 | "::strspn;" |
| 91 | "::strstr;" |
| 92 | "::wcscmp;" |
| 93 | // POSIX |
| 94 | "::access;" |
| 95 | "::bind;" |
| 96 | "::connect;" |
| 97 | "::difftime;" |
| 98 | "::dlsym;" |
| 99 | "::fnmatch;" |
| 100 | "::getaddrinfo;" |
| 101 | "::getopt;" |
| 102 | "::htonl;" |
| 103 | "::htons;" |
| 104 | "::iconv_open;" |
| 105 | "::inet_addr;" |
| 106 | "::isascii;" |
| 107 | "::isatty;" |
| 108 | "::mmap;" |
| 109 | "::newlocale;" |
| 110 | "::openat;" |
| 111 | "::pathconf;" |
| 112 | "::pthread_equal;" |
| 113 | "::pthread_getspecific;" |
| 114 | "::pthread_mutex_trylock;" |
| 115 | "::readdir;" |
| 116 | "::readlink;" |
| 117 | "::recvmsg;" |
| 118 | "::regexec;" |
| 119 | "::scandir;" |
| 120 | "::semget;" |
| 121 | "::setjmp;" |
| 122 | "::shm_open;" |
| 123 | "::shmget;" |
| 124 | "::sigismember;" |
| 125 | "::strcasecmp;" |
| 126 | "::strsignal;" |
| 127 | "::ttyname")) {} |
| 128 | |
| 129 | void UnusedReturnValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
| 130 | Options.store(Opts, "CheckedFunctions", CheckedFunctions); |
| 131 | } |
| 132 | |
| 133 | void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) { |
| 134 | auto FunVec = utils::options::parseStringList(CheckedFunctions); |
| 135 | auto MatchedCallExpr = expr(ignoringImplicit(ignoringParenImpCasts( |
| 136 | callExpr(callee(functionDecl( |
| 137 | // Don't match void overloads of checked functions. |
| 138 | unless(returns(voidType())), |
| 139 | isInstantiatedFrom(hasAnyName(FunVec))))) |
| 140 | .bind("match")))); |
| 141 | |
| 142 | auto UnusedInCompoundStmt = |
| 143 | compoundStmt(forEach(MatchedCallExpr), |
| 144 | // The checker can't currently differentiate between the |
| 145 | // return statement and other statements inside GNU statement |
| 146 | // expressions, so disable the checker inside them to avoid |
| 147 | // false positives. |
| 148 | unless(hasParent(stmtExpr()))); |
| 149 | auto UnusedInIfStmt = |
| 150 | ifStmt(eachOf(hasThen(MatchedCallExpr), hasElse(MatchedCallExpr))); |
| 151 | auto UnusedInWhileStmt = whileStmt(hasBody(MatchedCallExpr)); |
| 152 | auto UnusedInDoStmt = doStmt(hasBody(MatchedCallExpr)); |
| 153 | auto UnusedInForStmt = |
| 154 | forStmt(eachOf(hasLoopInit(MatchedCallExpr), |
| 155 | hasIncrement(MatchedCallExpr), hasBody(MatchedCallExpr))); |
| 156 | auto UnusedInRangeForStmt = cxxForRangeStmt(hasBody(MatchedCallExpr)); |
| 157 | auto UnusedInCaseStmt = switchCase(forEach(MatchedCallExpr)); |
| 158 | |
| 159 | Finder->addMatcher( |
| 160 | stmt(anyOf(UnusedInCompoundStmt, UnusedInIfStmt, UnusedInWhileStmt, |
| 161 | UnusedInDoStmt, UnusedInForStmt, UnusedInRangeForStmt, |
| 162 | UnusedInCaseStmt)), |
| 163 | this); |
| 164 | } |
| 165 | |
| 166 | void UnusedReturnValueCheck::check(const MatchFinder::MatchResult &Result) { |
| 167 | if (const auto *Matched = Result.Nodes.getNodeAs<CallExpr>("match")) { |
| 168 | diag(Matched->getBeginLoc(), |
| 169 | "the value returned by this function should be used") |
| 170 | << Matched->getSourceRange(); |
| 171 | diag(Matched->getBeginLoc(), |
| 172 | "cast the expression to void to silence this warning", |
| 173 | DiagnosticIDs::Note); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | } // namespace clang::tidy::bugprone |