Index: clang-tidy/modernize/CMakeLists.txt =================================================================== --- clang-tidy/modernize/CMakeLists.txt +++ clang-tidy/modernize/CMakeLists.txt @@ -27,6 +27,7 @@ UseNullptrCheck.cpp UseOverrideCheck.cpp UseTransparentFunctorsCheck.cpp + UseUncaughtExceptionsCheck.cpp UseUsingCheck.cpp LINK_LIBS Index: clang-tidy/modernize/ModernizeTidyModule.cpp =================================================================== --- clang-tidy/modernize/ModernizeTidyModule.cpp +++ clang-tidy/modernize/ModernizeTidyModule.cpp @@ -33,6 +33,7 @@ #include "UseNullptrCheck.h" #include "UseOverrideCheck.h" #include "UseTransparentFunctorsCheck.h" +#include "UseUncaughtExceptionsCheck.h" #include "UseUsingCheck.h" using namespace clang::ast_matchers; @@ -76,6 +77,8 @@ CheckFactories.registerCheck("modernize-use-noexcept"); CheckFactories.registerCheck("modernize-use-nullptr"); CheckFactories.registerCheck("modernize-use-override"); + CheckFactories.registerCheck( + "modernize-use-uncaught-exceptions"); CheckFactories.registerCheck( "modernize-use-transparent-functors"); CheckFactories.registerCheck("modernize-use-using"); Index: clang-tidy/modernize/UseUncaughtExceptionsCheck.h =================================================================== --- /dev/null +++ clang-tidy/modernize/UseUncaughtExceptionsCheck.h @@ -0,0 +1,37 @@ +//===--- UseUncaughtExceptionsCheck.h - clang-tidy------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USE_UNCAUGHT_EXCEPTIONS_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USE_UNCAUGHT_EXCEPTIONS_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace modernize { + +/// This check will warn for the occurrences of std::uncaught_exception and replace it with +/// std::uncaught_exceptions. Since C++17 std::uncaught_exception is deprecated. In case of +/// macro ID there will be only a warning without fixits. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/modernize-use-uncaught-exceptions.html +class UseUncaughtExceptionsCheck : public ClangTidyCheck { +public: + UseUncaughtExceptionsCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace modernize +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USE_UNCAUGHT_EXCEPTIONS_H Index: clang-tidy/modernize/UseUncaughtExceptionsCheck.cpp =================================================================== --- /dev/null +++ clang-tidy/modernize/UseUncaughtExceptionsCheck.cpp @@ -0,0 +1,73 @@ +//===--- UseUncaughtExceptionsCheck.cpp - clang-tidy--------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "UseUncaughtExceptionsCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace modernize { + +void UseUncaughtExceptionsCheck::registerMatchers(MatchFinder *Finder) { + if (!getLangOpts().CPlusPlus1z) + return; + + const char *MatchText = "::std::uncaught_exception"; + + Finder->addMatcher( + usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(hasName(MatchText)))) + .bind("using_decl"), + this); + + Finder->addMatcher( + declRefExpr(to(functionDecl(hasName(MatchText)))).bind("call_expr"), + this); +} + +void UseUncaughtExceptionsCheck::check(const MatchFinder::MatchResult &Result) { + SourceLocation BeginLoc; + SourceLocation EndLoc; + + if (const auto *D = Result.Nodes.getNodeAs("call_expr")) { + BeginLoc = D->getLocStart(); + EndLoc = D->getLocEnd(); + } else { + const auto *U = Result.Nodes.getNodeAs("using_decl"); + assert(U && "Null pointer, no node provided"); + BeginLoc = U->getNameInfo().getBeginLoc(); + EndLoc = U->getNameInfo().getEndLoc(); + } + + auto Diag = diag(BeginLoc, "'std::uncaught_exception' is deprecated, use " + "'std::uncaught_exceptions' instead"); + + if (!BeginLoc.isMacroID()) { + StringRef Text = + Lexer::getSourceText(CharSourceRange::getTokenRange(BeginLoc, EndLoc), + *Result.SourceManager, getLangOpts()); + + int TextLength = Text.size(); + StringRef checkText = "uncaught_exception"; + + // we don't want to modify template definitions + Text.consume_front("std::"); + if (!Text.equals(checkText)) { + return; + } + Diag << FixItHint::CreateInsertion(BeginLoc.getLocWithOffset(TextLength), + "s"); + } +} + +} // namespace modernize +} // namespace tidy +} // namespace clang Index: docs/ReleaseNotes.rst =================================================================== --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -132,6 +132,11 @@ Checks if the required file flag ``IN_CLOEXEC`` is present in the argument of ``inotify_init1()``. +- New `modernize-use-uncaught-exceptions + `_ check + + Finds and replaces deprecated uses of std::uncaught_exception to std::uncaught_exceptions() + - New `readability-static-accessed-through-instance `_ check Index: docs/clang-tidy/checks/list.rst =================================================================== --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -158,6 +158,7 @@ modernize-use-nullptr modernize-use-override modernize-use-transparent-functors + modernize-use-uncaught-exceptions modernize-use-using mpi-buffer-deref mpi-type-mismatch Index: docs/clang-tidy/checks/modernize-use-uncaught-exceptions.rst =================================================================== --- /dev/null +++ docs/clang-tidy/checks/modernize-use-uncaught-exceptions.rst @@ -0,0 +1,64 @@ +.. title:: clang-tidy - modernize-use-uncaught-exceptions + +modernize-use-uncaught-exceptions +==================================== + +This check will warn for the occurrences of ``std::uncaught_exception`` and +replace it with ``std::uncaught_exceptions``. Since C++17 +``std::uncaught_exception`` is deprecated. + +Below are a few examples of what kind of occurrences will be found and what +they will be replaced with. + +.. code-block:: c++ + + #define MACRO1 std::uncaught_exception + #define MACRO2 std::uncaught_exception + + int uncaught_exception() { + return 0; + } + + int main() { + int res; + + res = uncaught_exception(); + // No warning, since it is not the deprecated function from namespace std + + res = MACRO2(); + // Warning, but will not be replaced + + res = std::uncaught_exception(); + // Warning and replaced + + using std::uncaught_exception; + // Warning and replaced + + res = uncaught_exception(); + // Warning and replaced + } + +After applying the fixes the code will look like the following: + +.. code-block:: c++ + + #define MACRO1 std::uncaught_exception + #define MACRO2 std::uncaught_exception + + int uncaught_exception() { + return 0; + } + + int main() { + int res; + + res = uncaught_exception(); + + res = MACRO2(); + + res = std::uncaught_exceptions(); + + using std::uncaught_exceptions; + + res = uncaught_exceptions(); + } \ No newline at end of file Index: test/clang-tidy/modernize-use-uncaught-exceptions.cpp =================================================================== --- /dev/null +++ test/clang-tidy/modernize-use-uncaught-exceptions.cpp @@ -0,0 +1,71 @@ +// RUN: %check_clang_tidy %s modernize-use-uncaught-exceptions %t -- -- -std=c++1z +#define MACRO std::uncaught_exception +// CHECK-FIXES: #define MACRO std::uncaught_exception + +int uncaught_exception() { + return 0; +} + +namespace std { + int uncaught_exception() { + return 0; + } +} + +template +int doSomething(T t) { + return t(); + // CHECK-FIXES: return t(); +} + +template +int doSomething2() { + return T(); + // CHECK-MESSAGES: [[@LINE-1]]:12: warning: 'std::uncaught_exception' is deprecated, use 'std::uncaught_exceptions' instead + // CHECK-FIXES: return T(); +} + +int main() { + int res; + + res = uncaught_exception(); + // CHECK-FIXES: res = uncaught_exception(); + + res = doSomething(uncaught_exception); + // CHECK-FIXES: res = doSomething(uncaught_exception); + + res = MACRO(); + // CHECK-MESSAGES: [[@LINE-1]]:9: warning: 'std::uncaught_exception' is deprecated, use 'std::uncaught_exceptions' instead + // CHECK-FIXES: res = MACRO(); + + res = std::uncaught_exception(); + // CHECK-MESSAGES: [[@LINE-1]]:9: warning: 'std::uncaught_exception' is deprecated, use 'std::uncaught_exceptions' instead + // CHECK-FIXES: res = std::uncaught_exceptions(); + + using std::uncaught_exception; + // CHECK-MESSAGES: [[@LINE-1]]:14: warning: 'std::uncaught_exception' is deprecated, use 'std::uncaught_exceptions' instead + // CHECK-FIXES: using std::uncaught_exceptions; + + res = uncaught_exception(); + // CHECK-MESSAGES: [[@LINE-1]]:9: warning: 'std::uncaught_exception' is deprecated, use 'std::uncaught_exceptions' instead + // CHECK-FIXES: res = uncaught_exceptions(); + + res = doSomething(std::uncaught_exception); + // CHECK-MESSAGES: [[@LINE-1]]:21: warning: 'std::uncaught_exception' is deprecated, use 'std::uncaught_exceptions' instead + // CHECK-FIXES: res = doSomething(std::uncaught_exceptions); + + res = doSomething(uncaught_exception); + // CHECK-MESSAGES: [[@LINE-1]]:21: warning: 'std::uncaught_exception' is deprecated, use 'std::uncaught_exceptions' instead + // CHECK-FIXES: res = doSomething(uncaught_exceptions); + + int (*foo)(); + foo = &uncaught_exception; + // CHECK-MESSAGES: [[@LINE-1]]:10: warning: 'std::uncaught_exception' is deprecated, use 'std::uncaught_exceptions' instead + // CHECK-FIXES: foo = &uncaught_exceptions; + + res = doSomething2(); + // CHECK-MESSAGES: [[@LINE-1]]:22: warning: 'std::uncaught_exception' is deprecated, use 'std::uncaught_exceptions' instead + // CHECK-FIXES: res = doSomething2(); + + return res; +}