balazske updated this revision to Diff 378113.
balazske added a comment.

Using `StringRef`, added documentation (untested).


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D111228/new/

https://reviews.llvm.org/D111228

Files:
  clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
  clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.h
  clang-tools-extra/docs/clang-tidy/checks/bugprone-unused-return-value.rst
  
clang-tools-extra/test/clang-tidy/checkers/bugprone-unused-return-value-custom.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-unused-return-value-custom.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/bugprone-unused-return-value-custom.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-unused-return-value-custom.cpp
@@ -1,7 +1,11 @@
 // RUN: %check_clang_tidy %s bugprone-unused-return-value %t \
 // RUN: -config='{CheckOptions: \
 // RUN:  [{key: bugprone-unused-return-value.CheckedFunctions, \
-// RUN:    value: "::fun;::ns::Outer::Inner::memFun;::ns::Type::staticFun;::ns::ClassTemplate::memFun;::ns::ClassTemplate::staticFun"}]}' \
+// RUN:    value: "::fun;::ns::Outer::Inner::memFun;::ns::Type::staticFun;::ns::ClassTemplate::memFun;::ns::ClassTemplate::staticFun;::excludedFun"}, \
+// RUN:  {key: bugprone-unused-return-value.IncludedFunctions, \
+// RUN:    value: "::includedFun;::excludedFun"}, \
+// RUN:  {key: bugprone-unused-return-value.ExcludedFunctions, \
+// RUN:    value: "::excludedFun"}]}' \
 // RUN: --
 
 namespace std {
@@ -44,6 +48,8 @@
 
 int fun();
 void fun(int);
+int includedFun();
+int excludedFun();
 
 void warning() {
   fun();
@@ -81,6 +87,10 @@
   ns::ClassTemplate<int>::staticFun();
   // CHECK-NOTES: [[@LINE-1]]:3: warning: the value {{.*}} should be used
   // CHECK-NOTES: [[@LINE-2]]:3: note: cast {{.*}} this warning
+
+  includedFun();
+  // CHECK-NOTES: [[@LINE-1]]:3: warning: the value {{.*}} should be used
+  // CHECK-NOTES: [[@LINE-2]]:3: note: cast {{.*}} this warning
 }
 
 void noWarning() {
@@ -105,4 +115,7 @@
 
   ns::Type ObjB3;
   ObjB3.memFun();
+
+  // no warning on function that is specified as excluded and as to be checked
+  excludedFun();
 }
Index: clang-tools-extra/docs/clang-tidy/checks/bugprone-unused-return-value.rst
===================================================================
--- clang-tools-extra/docs/clang-tidy/checks/bugprone-unused-return-value.rst
+++ clang-tools-extra/docs/clang-tidy/checks/bugprone-unused-return-value.rst
@@ -10,9 +10,25 @@
 
 .. option:: CheckedFunctions
 
-   Semicolon-separated list of functions to check. Defaults to
-   ``::std::async;::std::launder;::std::remove;::std::remove_if;::std::unique;::std::unique_ptr::release;::std::basic_string::empty;::std::vector::empty``.
-   This means that the calls to following functions are checked by default:
+   Semicolon-separated list of functions to check. The function is checked if
+   the name and scope matches, with any arguments.
+   By default the following functions are checked:
+   ``std::async, std::launder, std::remove, std::remove_if, std::unique,
+   std::unique_ptr::release, std::basic_string::empty, std::vector::empty,
+   std::back_inserter, std::distance, std::find, std::find_if, std::inserter,
+   std::lower_bound, std::make_pair, std::map::count, std::map::find,
+   std::map::lower_bound, std::multimap::equal_range,
+   std::multimap::upper_bound, std::set::count, std::set::find, std::setfill,
+   std::setprecision, std::setw, std::upper_bound, std::vector::at,
+   bsearch, ferror, feof, isalnum, isalpha, isblank, iscntrl, isdigit, isgraph,
+   islower, isprint, ispunct, isspace, isupper, iswalnum, iswprint, iswspace,
+   isxdigit, memchr, memcmp, strcmp, strcoll, strncmp, strpbrk, strrchr,
+   strspn, strstr, wcscmp, access, bind, connect, difftime, dlsym, fnmatch,
+   getaddrinfo, getopt, htonl, htons, iconv_open, inet_addr, isascii, isatty,
+   mmap, newlocale, openat, pathconf, pthread_equal, pthread_getspecific,
+   pthread_mutex_trylock, readdir, readlink, recvmsg, regexec, scandir,
+   semget, setjmp, shm_open, shmget, sigismember, strcasecmp, strsignal,
+   ttyname``
 
    - ``std::async()``. Not using the return value makes the call synchronous.
    - ``std::launder()``. Not using the return value usually means that the
@@ -29,3 +45,14 @@
    - ``std::basic_string::empty()`` and ``std::vector::empty()``. Not using the
      return value often indicates that the programmer confused the function with
      ``clear()``.
+
+.. option:: IncludedFunctions
+
+   Semicolon-separated list of additional functions to check. The list specified
+   with `CheckedFunctions` is extended by these functions.
+
+.. option:: ExcludedFunctions
+
+   Semicolon-separated list of functions to exclude from the check. These
+   functions are not checked even if included in the `CheckedFunctions` or
+   `IncludedFunctions` list.
Index: clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.h
===================================================================
--- clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.h
+++ clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.h
@@ -29,6 +29,8 @@
 
 private:
   std::string CheckedFunctions;
+  std::string IncludedFunctions;
+  std::string ExcludedFunctions;
 };
 
 } // namespace bugprone
Index: clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
@@ -30,116 +30,129 @@
                               Finder, Builder);
 }
 
+const llvm::StringRef DefaultCheckedFunctions = "::std::async;"
+                                                "::std::launder;"
+                                                "::std::remove;"
+                                                "::std::remove_if;"
+                                                "::std::unique;"
+                                                "::std::unique_ptr::release;"
+                                                "::std::basic_string::empty;"
+                                                "::std::vector::empty;"
+                                                "::std::back_inserter;"
+                                                "::std::distance;"
+                                                "::std::find;"
+                                                "::std::find_if;"
+                                                "::std::inserter;"
+                                                "::std::lower_bound;"
+                                                "::std::make_pair;"
+                                                "::std::map::count;"
+                                                "::std::map::find;"
+                                                "::std::map::lower_bound;"
+                                                "::std::multimap::equal_range;"
+                                                "::std::multimap::upper_bound;"
+                                                "::std::set::count;"
+                                                "::std::set::find;"
+                                                "::std::setfill;"
+                                                "::std::setprecision;"
+                                                "::std::setw;"
+                                                "::std::upper_bound;"
+                                                "::std::vector::at;"
+                                                // C standard library
+                                                "::bsearch;"
+                                                "::ferror;"
+                                                "::feof;"
+                                                "::isalnum;"
+                                                "::isalpha;"
+                                                "::isblank;"
+                                                "::iscntrl;"
+                                                "::isdigit;"
+                                                "::isgraph;"
+                                                "::islower;"
+                                                "::isprint;"
+                                                "::ispunct;"
+                                                "::isspace;"
+                                                "::isupper;"
+                                                "::iswalnum;"
+                                                "::iswprint;"
+                                                "::iswspace;"
+                                                "::isxdigit;"
+                                                "::memchr;"
+                                                "::memcmp;"
+                                                "::strcmp;"
+                                                "::strcoll;"
+                                                "::strncmp;"
+                                                "::strpbrk;"
+                                                "::strrchr;"
+                                                "::strspn;"
+                                                "::strstr;"
+                                                "::wcscmp;"
+                                                // POSIX
+                                                "::access;"
+                                                "::bind;"
+                                                "::connect;"
+                                                "::difftime;"
+                                                "::dlsym;"
+                                                "::fnmatch;"
+                                                "::getaddrinfo;"
+                                                "::getopt;"
+                                                "::htonl;"
+                                                "::htons;"
+                                                "::iconv_open;"
+                                                "::inet_addr;"
+                                                "::isascii;"
+                                                "::isatty;"
+                                                "::mmap;"
+                                                "::newlocale;"
+                                                "::openat;"
+                                                "::pathconf;"
+                                                "::pthread_equal;"
+                                                "::pthread_getspecific;"
+                                                "::pthread_mutex_trylock;"
+                                                "::readdir;"
+                                                "::readlink;"
+                                                "::recvmsg;"
+                                                "::regexec;"
+                                                "::scandir;"
+                                                "::semget;"
+                                                "::setjmp;"
+                                                "::shm_open;"
+                                                "::shmget;"
+                                                "::sigismember;"
+                                                "::strcasecmp;"
+                                                "::strsignal;"
+                                                "::ttyname";
+
 } // namespace
 
 UnusedReturnValueCheck::UnusedReturnValueCheck(llvm::StringRef Name,
                                                ClangTidyContext *Context)
     : ClangTidyCheck(Name, Context),
-      CheckedFunctions(Options.get("CheckedFunctions",
-                                   "::std::async;"
-                                   "::std::launder;"
-                                   "::std::remove;"
-                                   "::std::remove_if;"
-                                   "::std::unique;"
-                                   "::std::unique_ptr::release;"
-                                   "::std::basic_string::empty;"
-                                   "::std::vector::empty;"
-                                   "::std::back_inserter;"
-                                   "::std::distance;"
-                                   "::std::find;"
-                                   "::std::find_if;"
-                                   "::std::inserter;"
-                                   "::std::lower_bound;"
-                                   "::std::make_pair;"
-                                   "::std::map::count;"
-                                   "::std::map::find;"
-                                   "::std::map::lower_bound;"
-                                   "::std::multimap::equal_range;"
-                                   "::std::multimap::upper_bound;"
-                                   "::std::set::count;"
-                                   "::std::set::find;"
-                                   "::std::setfill;"
-                                   "::std::setprecision;"
-                                   "::std::setw;"
-                                   "::std::upper_bound;"
-                                   "::std::vector::at;"
-                                   // C standard library
-                                   "::bsearch;"
-                                   "::ferror;"
-                                   "::feof;"
-                                   "::isalnum;"
-                                   "::isalpha;"
-                                   "::isblank;"
-                                   "::iscntrl;"
-                                   "::isdigit;"
-                                   "::isgraph;"
-                                   "::islower;"
-                                   "::isprint;"
-                                   "::ispunct;"
-                                   "::isspace;"
-                                   "::isupper;"
-                                   "::iswalnum;"
-                                   "::iswprint;"
-                                   "::iswspace;"
-                                   "::isxdigit;"
-                                   "::memchr;"
-                                   "::memcmp;"
-                                   "::strcmp;"
-                                   "::strcoll;"
-                                   "::strncmp;"
-                                   "::strpbrk;"
-                                   "::strrchr;"
-                                   "::strspn;"
-                                   "::strstr;"
-                                   "::wcscmp;"
-                                   // POSIX
-                                   "::access;"
-                                   "::bind;"
-                                   "::connect;"
-                                   "::difftime;"
-                                   "::dlsym;"
-                                   "::fnmatch;"
-                                   "::getaddrinfo;"
-                                   "::getopt;"
-                                   "::htonl;"
-                                   "::htons;"
-                                   "::iconv_open;"
-                                   "::inet_addr;"
-                                   "::isascii;"
-                                   "::isatty;"
-                                   "::mmap;"
-                                   "::newlocale;"
-                                   "::openat;"
-                                   "::pathconf;"
-                                   "::pthread_equal;"
-                                   "::pthread_getspecific;"
-                                   "::pthread_mutex_trylock;"
-                                   "::readdir;"
-                                   "::readlink;"
-                                   "::recvmsg;"
-                                   "::regexec;"
-                                   "::scandir;"
-                                   "::semget;"
-                                   "::setjmp;"
-                                   "::shm_open;"
-                                   "::shmget;"
-                                   "::sigismember;"
-                                   "::strcasecmp;"
-                                   "::strsignal;"
-                                   "::ttyname")) {}
+      CheckedFunctions(
+          Options.get("CheckedFunctions", DefaultCheckedFunctions)),
+      IncludedFunctions(Options.get("IncludedFunctions", "")),
+      ExcludedFunctions(Options.get("ExcludedFunctions", "")) {}
 
 void UnusedReturnValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
   Options.store(Opts, "CheckedFunctions", CheckedFunctions);
+  Options.store(Opts, "IncludedFunctions", IncludedFunctions);
+  Options.store(Opts, "ExcludedFunctions", ExcludedFunctions);
 }
 
 void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) {
   auto FunVec = utils::options::parseStringList(CheckedFunctions);
+  std::set<std::string> Functions(FunVec.begin(), FunVec.end());
+  FunVec = utils::options::parseStringList(IncludedFunctions);
+  Functions.insert(FunVec.begin(), FunVec.end());
+  FunVec = utils::options::parseStringList(ExcludedFunctions);
+  for (const std::string &Str : FunVec)
+    Functions.erase(Str);
+
   auto MatchedCallExpr = expr(ignoringImplicit(ignoringParenImpCasts(
       callExpr(callee(functionDecl(
                    // Don't match void overloads of checked functions.
                    unless(returns(voidType())),
-                   isInstantiatedFrom(hasAnyName(
-                       std::vector<StringRef>(FunVec.begin(), FunVec.end()))))))
+                   isInstantiatedFrom(hasAnyName(std::vector<StringRef>(
+                       Functions.begin(), Functions.end()))))))
           .bind("match"))));
 
   auto UnusedInCompoundStmt =
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to