segoon updated this revision to Diff 303532.
segoon added a comment.

- use static instead of namespace {}
- don't use SourceRange()
- revert unrelated changes to .rst


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

https://reviews.llvm.org/D90944

Files:
  clang-tools-extra/clang-tidy/misc/CMakeLists.txt
  clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
  clang-tools-extra/clang-tidy/misc/MtUnsafeCheck.cpp
  clang-tools-extra/clang-tidy/misc/MtUnsafeCheck.h
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/docs/clang-tidy/checks/list.rst
  clang-tools-extra/docs/clang-tidy/checks/misc-mt-unsafe.rst
  clang-tools-extra/test/clang-tidy/checkers/misc-mt-unsafe.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/misc-mt-unsafe.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/misc-mt-unsafe.cpp
@@ -0,0 +1,17 @@
+// RUN: %check_clang_tidy %s misc-mt-unsafe %t
+
+extern unsigned int sleep (unsigned int __seconds);
+extern int *gmtime (const int *__timer);
+
+void foo() {
+  sleep(2);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: function is not thread safe [misc-mt-unsafe]
+
+  ::sleep(2);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: function is not thread safe [misc-mt-unsafe]
+
+  auto tm = gmtime(nullptr);
+  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: function is not thread safe [misc-mt-unsafe]
+  tm = ::gmtime(nullptr);
+  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: function is not thread safe [misc-mt-unsafe]
+}
Index: clang-tools-extra/docs/clang-tidy/checks/misc-mt-unsafe.rst
===================================================================
--- /dev/null
+++ clang-tools-extra/docs/clang-tidy/checks/misc-mt-unsafe.rst
@@ -0,0 +1,16 @@
+.. title:: clang-tidy - misc-mt-unsafe
+
+misc-mt-unsafe
+==============
+
+Checks for some thread-unsafe functions against a black list of
+known-to-be-unsafe functions. Usually they access static variables without
+synchronization (e.g. gmtime(3)) or utilize signals in a racy way.
+
+Examples:
+
+.. code-block:: c++
+
+    tm = gmtime(timep); // uses a global buffer
+
+    sleep(1); // implementation may use SIGALRM
Index: clang-tools-extra/docs/clang-tidy/checks/list.rst
===================================================================
--- clang-tools-extra/docs/clang-tidy/checks/list.rst
+++ clang-tools-extra/docs/clang-tidy/checks/list.rst
@@ -198,6 +198,7 @@
    `llvmlibc-restrict-system-libc-headers <llvmlibc-restrict-system-libc-headers.html>`_, "Yes"
    `misc-definitions-in-headers <misc-definitions-in-headers.html>`_, "Yes"
    `misc-misplaced-const <misc-misplaced-const.html>`_,
+   `misc-mt-unsafe <misc-mt-unsafe.html>`_,
    `misc-new-delete-overloads <misc-new-delete-overloads.html>`_,
    `misc-no-recursion <misc-no-recursion.html>`_,
    `misc-non-copyable-objects <misc-non-copyable-objects.html>`_,
Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -106,6 +106,10 @@
   Finds condition variables in nested ``if`` statements that were also checked
   in the outer ``if`` statement and were not changed.
 
+- New :doc:`misc-mt-unsafe <clang-tidy/checks/misc-mt-unsafe>` check.
+
+  Finds thread-unsafe functions usage.
+
 - New :doc:`readability-function-cognitive-complexity
   <clang-tidy/checks/readability-function-cognitive-complexity>` check.
 
Index: clang-tools-extra/clang-tidy/misc/MtUnsafeCheck.h
===================================================================
--- /dev/null
+++ clang-tools-extra/clang-tidy/misc/MtUnsafeCheck.h
@@ -0,0 +1,34 @@
+//===--- MtUnsafeCheck.h - clang-tidy ---------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MTUNSAFECHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MTUNSAFECHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang {
+namespace tidy {
+namespace misc {
+
+/// Checks that non-thread-safe functions are not used.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/misc-mt-unsafe.html
+class MtUnsafeCheck : public ClangTidyCheck {
+public:
+  MtUnsafeCheck(StringRef Name, ClangTidyContext *Context)
+      : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+} // namespace misc
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MTUNSAFECHECK_H
Index: clang-tools-extra/clang-tidy/misc/MtUnsafeCheck.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/clang-tidy/misc/MtUnsafeCheck.cpp
@@ -0,0 +1,199 @@
+//===--- MtUnsafeCheck.cpp - clang-tidy -----------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "MtUnsafeCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace misc {
+
+// Initial list was extracted from gcc documentation
+static const StringRef functions[] = {
+    "::argp_error",
+    "::argp_help",
+    "::argp_parse",
+    "::argp_state_help",
+    "::argp_usage",
+    "::asctime",
+    "::clearenv",
+    "::crypt",
+    "::ctime",
+    "::cuserid",
+    "::drand48",
+    "::ecvt",
+    "::encrypt",
+    "::endfsent",
+    "::endgrent",
+    "::endhostent",
+    "::endnetent",
+    "::endnetgrent",
+    "::endprotoent",
+    "::endpwent",
+    "::endservent",
+    "::endutent",
+    "::endutxent",
+    "::erand48",
+    "::error_at_line",
+    "::exit",
+    "::fcloseall",
+    "::fcvt",
+    "::fgetgrent",
+    "::fgetpwent",
+    "::gammal",
+    "::getchar_unlocked",
+    "::getdate",
+    "::getfsent",
+    "::getfsfile",
+    "::getfsspec",
+    "::getgrent",
+    "::getgrent_r",
+    "::getgrgid",
+    "::getgrnam",
+    "::gethostbyaddr",
+    "::gethostbyname",
+    "::gethostbyname2",
+    "::gethostent",
+    "::getlogin",
+    "::getmntent",
+    "::getnetbyaddr",
+    "::getnetbyname",
+    "::getnetent",
+    "::getnetgrent",
+    "::getnetgrent_r",
+    "::getopt",
+    "::getopt_long",
+    "::getopt_long_only",
+    "::getpass",
+    "::getprotobyname",
+    "::getprotobynumber",
+    "::getprotoent",
+    "::getpwent",
+    "::getpwent_r",
+    "::getpwnam",
+    "::getpwuid",
+    "::getservbyname",
+    "::getservbyport",
+    "::getservent",
+    "::getutent",
+    "::getutent_r",
+    "::getutid",
+    "::getutid_r",
+    "::getutline",
+    "::getutline_r",
+    "::getutxent",
+    "::getutxid",
+    "::getutxline",
+    "::getwchar_unlocked",
+    "::glob",
+    "::glob64",
+    "::gmtime",
+    "::hcreate",
+    "::hdestroy",
+    "::hsearch",
+    "::innetgr",
+    "::jrand48",
+    "::l64a",
+    "::lcong48",
+    "::lgammafNx",
+    "::localeconv",
+    "::localtime",
+    "::login",
+    "::login_tty",
+    "::logout",
+    "::logwtmp",
+    "::lrand48",
+    "::mallinfo",
+    "::mallopt",
+    "::mblen",
+    "::mbrlen",
+    "::mbrtowc",
+    "::mbsnrtowcs",
+    "::mbsrtowcs",
+    "::mbtowc",
+    "::mcheck",
+    "::mprobe",
+    "::mrand48",
+    "::mtrace",
+    "::muntrace",
+    "::nrand48",
+    "::__ppc_get_timebase_freq",
+    "::ptsname",
+    "::putchar_unlocked",
+    "::putenv",
+    "::pututline",
+    "::pututxline",
+    "::putwchar_unlocked",
+    "::qecvt",
+    "::qfcvt",
+    "::register_printf_function",
+    "::seed48",
+    "::setenv",
+    "::setfsent",
+    "::setgrent",
+    "::sethostent",
+    "::sethostid",
+    "::setkey",
+    "::setlocale",
+    "::setlogmask",
+    "::setnetent",
+    "::setnetgrent",
+    "::setprotoent",
+    "::setpwent",
+    "::setservent",
+    "::setutent",
+    "::setutxent",
+    "::siginterrupt",
+    "::sigpause",
+    "::sigprocmask",
+    "::sigsuspend",
+    "::sleep",
+    "::srand48",
+    "::strerror",
+    "::strsignal",
+    "::strtok",
+    "::tcflow",
+    "::tcsendbreak",
+    "::tmpnam",
+    "::ttyname",
+    "::unsetenv",
+    "::updwtmp",
+    "::utmpname",
+    "::utmpxname",
+    "::valloc",
+    "::vlimit",
+    "::wcrtomb",
+    "::wcsnrtombs",
+    "::wcsrtombs",
+    "::wctomb",
+    "::wordexp",
+};
+
+static ast_matchers::internal::Matcher<clang::NamedDecl> hasAnyMtUnsafeNames() {
+  return hasAnyName(functions);
+}
+
+void MtUnsafeCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+      callExpr(callee(functionDecl(hasAnyMtUnsafeNames()))).bind("mt-unsafe"),
+      this);
+}
+
+void MtUnsafeCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *Call = Result.Nodes.getNodeAs<CallExpr>("mt-unsafe");
+  assert(Call && "Unhandled binding in the Matcher");
+
+  diag(Call->getBeginLoc(), "function is not thread safe");
+}
+
+} // namespace misc
+} // namespace tidy
+} // namespace clang
Index: clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
===================================================================
--- clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
+++ clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
@@ -11,6 +11,7 @@
 #include "../ClangTidyModuleRegistry.h"
 #include "DefinitionsInHeadersCheck.h"
 #include "MisplacedConstCheck.h"
+#include "MtUnsafeCheck.h"
 #include "NewDeleteOverloadsCheck.h"
 #include "NoRecursionCheck.h"
 #include "NonCopyableObjects.h"
@@ -34,6 +35,8 @@
     CheckFactories.registerCheck<DefinitionsInHeadersCheck>(
         "misc-definitions-in-headers");
     CheckFactories.registerCheck<MisplacedConstCheck>("misc-misplaced-const");
+    CheckFactories.registerCheck<MtUnsafeCheck>(
+        "misc-mt-unsafe");
     CheckFactories.registerCheck<NewDeleteOverloadsCheck>(
         "misc-new-delete-overloads");
     CheckFactories.registerCheck<NoRecursionCheck>("misc-no-recursion");
Index: clang-tools-extra/clang-tidy/misc/CMakeLists.txt
===================================================================
--- clang-tools-extra/clang-tidy/misc/CMakeLists.txt
+++ clang-tools-extra/clang-tidy/misc/CMakeLists.txt
@@ -7,6 +7,7 @@
   DefinitionsInHeadersCheck.cpp
   MiscTidyModule.cpp
   MisplacedConstCheck.cpp
+  MtUnsafeCheck.cpp
   NewDeleteOverloadsCheck.cpp
   NoRecursionCheck.cpp
   NonCopyableObjects.cpp
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to