From cf8d1596e1dbac83c36aa328cebacfb51675aac3 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Sat, 18 Oct 2025 11:07:51 +0300 Subject: [PATCH 1/4] [clang-tidy] Rename 'cert-err60-cpp' to 'bugprone-exception-type-not-nothrow-copy-constructible' --- .../bugprone/BugproneTidyModule.cpp | 3 ++ .../clang-tidy/bugprone/CMakeLists.txt | 1 + ...nTypeNotNothrowCopyConstructibleCheck.cpp} | 12 ++++--- ...ionTypeNotNothrowCopyConstructibleCheck.h} | 18 ++++++----- .../clang-tidy/cert/CERTTidyModule.cpp | 6 ++-- .../clang-tidy/cert/CMakeLists.txt | 1 - clang-tools-extra/docs/ReleaseNotes.rst | 5 +++ ...on-type-not-nothrow-copy-constructible.rst | 31 +++++++++++++++++++ .../docs/clang-tidy/checks/cert/err60-cpp.rst | 9 ++++-- .../docs/clang-tidy/checks/list.rst | 3 +- ...n-type-not-nothrow-copy-constructible.cpp} | 4 +-- 11 files changed, 71 insertions(+), 22 deletions(-) rename clang-tools-extra/clang-tidy/{cert/ThrownExceptionTypeCheck.cpp => bugprone/ExceptionTypeNotNothrowCopyConstructibleCheck.cpp} (74%) rename clang-tools-extra/clang-tidy/{cert/ThrownExceptionTypeCheck.h => bugprone/ExceptionTypeNotNothrowCopyConstructibleCheck.h} (51%) create mode 100644 clang-tools-extra/docs/clang-tidy/checks/bugprone/exception-type-not-nothrow-copy-constructible.rst rename clang-tools-extra/test/clang-tidy/checkers/{cert/throw-exception-type.cpp => bugprone/exception-type-not-nothrow-copy-constructible.cpp} (92%) diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp index e6115f67656bc..eed2a10450e6a 100644 --- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp @@ -29,6 +29,7 @@ #include "EasilySwappableParametersCheck.h" #include "EmptyCatchCheck.h" #include "ExceptionEscapeCheck.h" +#include "ExceptionTypeNotNothrowCopyConstructibleCheck.h" #include "FoldInitTypeCheck.h" #include "ForwardDeclarationNamespaceCheck.h" #include "ForwardingReferenceOverloadCheck.h" @@ -148,6 +149,8 @@ class BugproneModule : public ClangTidyModule { CheckFactories.registerCheck("bugprone-empty-catch"); CheckFactories.registerCheck( "bugprone-exception-escape"); + CheckFactories.registerCheck( + "bugprone-exception-type-not-nothrow-copy-constructible"); CheckFactories.registerCheck("bugprone-fold-init-type"); CheckFactories.registerCheck( "bugprone-forward-declaration-namespace"); diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt index c8943e5b22ef8..5199552923e20 100644 --- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt @@ -25,6 +25,7 @@ add_clang_library(clangTidyBugproneModule STATIC EasilySwappableParametersCheck.cpp EmptyCatchCheck.cpp ExceptionEscapeCheck.cpp + ExceptionTypeNotNothrowCopyConstructibleCheck.cpp FoldInitTypeCheck.cpp ForwardDeclarationNamespaceCheck.cpp ForwardingReferenceOverloadCheck.cpp diff --git a/clang-tools-extra/clang-tidy/cert/ThrownExceptionTypeCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ExceptionTypeNotNothrowCopyConstructibleCheck.cpp similarity index 74% rename from clang-tools-extra/clang-tidy/cert/ThrownExceptionTypeCheck.cpp rename to clang-tools-extra/clang-tidy/bugprone/ExceptionTypeNotNothrowCopyConstructibleCheck.cpp index 2225a90aeece1..6d0dae21b8c67 100644 --- a/clang-tools-extra/clang-tidy/cert/ThrownExceptionTypeCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/ExceptionTypeNotNothrowCopyConstructibleCheck.cpp @@ -6,15 +6,16 @@ // //===----------------------------------------------------------------------===// -#include "ThrownExceptionTypeCheck.h" +#include "ExceptionTypeNotNothrowCopyConstructibleCheck.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" using namespace clang::ast_matchers; -namespace clang::tidy::cert { +namespace clang::tidy::bugprone { -void ThrownExceptionTypeCheck::registerMatchers(MatchFinder *Finder) { +void ExceptionTypeNotNothrowCopyConstructibleCheck::registerMatchers( + MatchFinder *Finder) { Finder->addMatcher( traverse( TK_AsIs, @@ -25,10 +26,11 @@ void ThrownExceptionTypeCheck::registerMatchers(MatchFinder *Finder) { this); } -void ThrownExceptionTypeCheck::check(const MatchFinder::MatchResult &Result) { +void ExceptionTypeNotNothrowCopyConstructibleCheck::check( + const MatchFinder::MatchResult &Result) { const auto *E = Result.Nodes.getNodeAs("expr"); diag(E->getExprLoc(), "thrown exception type is not nothrow copy constructible"); } -} // namespace clang::tidy::cert +} // namespace clang::tidy::bugprone diff --git a/clang-tools-extra/clang-tidy/cert/ThrownExceptionTypeCheck.h b/clang-tools-extra/clang-tidy/bugprone/ExceptionTypeNotNothrowCopyConstructibleCheck.h similarity index 51% rename from clang-tools-extra/clang-tidy/cert/ThrownExceptionTypeCheck.h rename to clang-tools-extra/clang-tidy/bugprone/ExceptionTypeNotNothrowCopyConstructibleCheck.h index 41a5145209686..f7b8764acd0a6 100644 --- a/clang-tools-extra/clang-tidy/cert/ThrownExceptionTypeCheck.h +++ b/clang-tools-extra/clang-tidy/bugprone/ExceptionTypeNotNothrowCopyConstructibleCheck.h @@ -1,4 +1,5 @@ //===----------------------------------------------------------------------===// +//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,20 +7,21 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_THROWNEXCEPTIONTYPECHECK_H -#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_THROWNEXCEPTIONTYPECHECK_H +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_EXCEPTIONTYPENOTNOTHROWCOPYCONSTRUCTIBLECHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_EXCEPTIONTYPENOTNOTHROWCOPYCONSTRUCTIBLECHECK_H #include "../ClangTidyCheck.h" -namespace clang::tidy::cert { +namespace clang::tidy::bugprone { /// Checks whether a thrown object is nothrow copy constructible. /// /// For the user-facing documentation see: -/// https://clang.llvm.org/extra/clang-tidy/checks/cert/err60-cpp.html -class ThrownExceptionTypeCheck : public ClangTidyCheck { +/// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/exception-type-not-nothrow-copy-constructible.html +class ExceptionTypeNotNothrowCopyConstructibleCheck : public ClangTidyCheck { public: - ThrownExceptionTypeCheck(StringRef Name, ClangTidyContext *Context) + ExceptionTypeNotNothrowCopyConstructibleCheck(StringRef Name, + ClangTidyContext *Context) : ClangTidyCheck(Name, Context) {} bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { return LangOpts.CPlusPlus; @@ -28,6 +30,6 @@ class ThrownExceptionTypeCheck : public ClangTidyCheck { void check(const ast_matchers::MatchFinder::MatchResult &Result) override; }; -} // namespace clang::tidy::cert +} // namespace clang::tidy::bugprone -#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_THROWNEXCEPTIONTYPECHECK_H +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_EXCEPTIONTYPENOTNOTHROWCOPYCONSTRUCTIBLECHECK_H diff --git a/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp b/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp index c1ca2cec7a1eb..4080977a5c3e1 100644 --- a/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp @@ -11,6 +11,7 @@ #include "../ClangTidyModuleRegistry.h" #include "../bugprone/BadSignalToKillThreadCheck.h" #include "../bugprone/CommandProcessorCheck.h" +#include "../bugprone/ExceptionTypeNotNothrowCopyConstructibleCheck.h" #include "../bugprone/PointerArithmeticOnPolymorphicObjectCheck.h" #include "../bugprone/ReservedIdentifierCheck.h" #include "../bugprone/SignalHandlerCheck.h" @@ -41,7 +42,6 @@ #include "MutatingCopyCheck.h" #include "NonTrivialTypesLibcMemoryCallsCheck.h" #include "ProperlySeededRandomGeneratorCheck.h" -#include "ThrownExceptionTypeCheck.h" namespace { @@ -261,7 +261,9 @@ class CERTModule : public ClangTidyModule { "cert-err52-cpp"); CheckFactories.registerCheck( "cert-err58-cpp"); - CheckFactories.registerCheck("cert-err60-cpp"); + CheckFactories + .registerCheck( + "cert-err60-cpp"); CheckFactories.registerCheck( "cert-err61-cpp"); // MEM diff --git a/clang-tools-extra/clang-tidy/cert/CMakeLists.txt b/clang-tools-extra/clang-tidy/cert/CMakeLists.txt index 453d1d30921e9..539124903655b 100644 --- a/clang-tools-extra/clang-tidy/cert/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/cert/CMakeLists.txt @@ -12,7 +12,6 @@ add_clang_library(clangTidyCERTModule STATIC MutatingCopyCheck.cpp NonTrivialTypesLibcMemoryCallsCheck.cpp ProperlySeededRandomGeneratorCheck.cpp - ThrownExceptionTypeCheck.cpp LINK_LIBS clangTidy diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 33cc401bcb78f..4496ae9f727e4 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -249,6 +249,11 @@ New check aliases ` keeping initial check as an alias to the new one. +- Renamed :doc:`cert-err60-cpp ` to + :doc:`bugprone-exception-type-not-nothrow-copy-constructible + ` + keeping initial check as an alias to the new one. + Changes in existing checks ^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/exception-type-not-nothrow-copy-constructible.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/exception-type-not-nothrow-copy-constructible.rst new file mode 100644 index 0000000000000..9875a48a4f633 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/exception-type-not-nothrow-copy-constructible.rst @@ -0,0 +1,31 @@ +.. title:: clang-tidy - bugprone-exception-type-not-nothrow-copy-constructible + +bugprone-exception-type-not-nothrow-copy-constructible +======================================================= + +Checks whether a thrown object is nothrow copy constructible. + +Exception objects are required to be copy constructible in C++. However, +exceptions should generally be nothrow copy constructible to avoid potential +issues when unwinding the stack. If an exception is thrown during stack +unwinding (such as from a copy constructor of an exception object), the +program will terminate via ``std::terminate``. + +.. code-block:: c++ + + class SomeException { + public: + SomeException() = default; + SomeException(const SomeException&) { /* may throw */ } + }; + + void f() { + throw SomeException(); // warning: thrown exception type is not nothrow copy constructible + } + +References +---------- + +This check corresponds to the CERT C++ Coding Standard rule +`ERR60-CPP. Exception objects must be nothrow copy constructible +`_. \ No newline at end of file diff --git a/clang-tools-extra/docs/clang-tidy/checks/cert/err60-cpp.rst b/clang-tools-extra/docs/clang-tidy/checks/cert/err60-cpp.rst index 9fcb840fc06f8..faa0c0aa0b373 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/cert/err60-cpp.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/cert/err60-cpp.rst @@ -1,11 +1,14 @@ .. title:: clang-tidy - cert-err60-cpp +.. meta:: + :http-equiv=refresh: 5;URL=../bugprone/exception-type-not-nothrow-copy-constructible.html cert-err60-cpp ============== -This check flags all throw expressions where the exception object is not nothrow -copy constructible. +The `cert-err60-cpp`` check is an alias, please see +`bugprone-exception-type-not-nothrow-copy-constructible <../bugprone/exception-type-not-nothrow-copy-constructible.html>`_ +for more information. This check corresponds to the CERT C++ Coding Standard rule `ERR60-CPP. Exception objects must be nothrow copy constructible -`_. +`_. diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index a324d18704c01..bfb21057aa02b 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -97,6 +97,7 @@ Clang-Tidy Checks :doc:`bugprone-easily-swappable-parameters `, :doc:`bugprone-empty-catch `, :doc:`bugprone-exception-escape `, + :doc:`bugprone-exception-type-not-nothrow-copy-constructible `, :doc:`bugprone-fold-init-type `, :doc:`bugprone-forward-declaration-namespace `, :doc:`bugprone-forwarding-reference-overload `, @@ -175,7 +176,6 @@ Clang-Tidy Checks :doc:`bugprone-virtual-near-miss `, "Yes" :doc:`cert-dcl58-cpp `, :doc:`cert-err33-c `, - :doc:`cert-err60-cpp `, :doc:`cert-flp30-c `, :doc:`cert-mem57-cpp `, :doc:`cert-msc50-cpp `, @@ -446,6 +446,7 @@ Check aliases :doc:`cert-err34-c `, :doc:`bugprone-unchecked-string-to-number-conversion `, :doc:`cert-err52-cpp `, :doc:`modernize-avoid-setjmp-longjmp `, :doc:`cert-err58-cpp `, :doc:`bugprone-throwing-static-initialization `, + :doc:`cert-err60-cpp `, :doc:`bugprone-exception-type-not-nothrow-copy-constructible `, :doc:`cert-err61-cpp `, :doc:`misc-throw-by-value-catch-by-reference `, :doc:`cert-exp42-c `, :doc:`bugprone-suspicious-memory-comparison `, :doc:`cert-fio38-c `, :doc:`misc-non-copyable-objects `, diff --git a/clang-tools-extra/test/clang-tidy/checkers/cert/throw-exception-type.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-type-not-nothrow-copy-constructible.cpp similarity index 92% rename from clang-tools-extra/test/clang-tidy/checkers/cert/throw-exception-type.cpp rename to clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-type-not-nothrow-copy-constructible.cpp index 34ca83795c397..ddcc6b1b1848f 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/cert/throw-exception-type.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-type-not-nothrow-copy-constructible.cpp @@ -1,4 +1,4 @@ -// RUN: %check_clang_tidy -std=c++11,c++14 %s cert-err60-cpp %t -- -- -fcxx-exceptions +// RUN: %check_clang_tidy -std=c++11,c++14 %s bugprone-exception-type-not-nothrow-copy-constructible %t -- -- -fcxx-exceptions // FIXME: Split off parts of this test that rely on dynamic exception // specifications, and run this test in all language modes. // FIXME: Fix the checker to work in C++17 or later mode. @@ -92,7 +92,7 @@ void f() { throw U(); // ok throw V(); // ok throw W(); // match, noexcept(false) - // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: thrown exception type is not nothrow copy constructible [cert-err60-cpp] + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: thrown exception type is not nothrow copy constructible [bugprone-exception-type-not-nothrow-copy-constructible] throw X(); // match, no noexcept clause, nontrivial // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: thrown exception type is not nothrow copy constructible throw Y(); // ok From c42cc1895c4a2a01d6026809f8ebfb597a5b548f Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Sat, 18 Oct 2025 11:09:13 +0300 Subject: [PATCH 2/4] fix doc --- clang-tools-extra/docs/clang-tidy/checks/cert/err60-cpp.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang-tools-extra/docs/clang-tidy/checks/cert/err60-cpp.rst b/clang-tools-extra/docs/clang-tidy/checks/cert/err60-cpp.rst index faa0c0aa0b373..bbdb2975835a8 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/cert/err60-cpp.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/cert/err60-cpp.rst @@ -5,7 +5,7 @@ cert-err60-cpp ============== -The `cert-err60-cpp`` check is an alias, please see +The `cert-err60-cpp` check is an alias, please see `bugprone-exception-type-not-nothrow-copy-constructible <../bugprone/exception-type-not-nothrow-copy-constructible.html>`_ for more information. From dac68454c9e786c6639654547c376b57f2c7b137 Mon Sep 17 00:00:00 2001 From: Baranov Victor Date: Sun, 19 Oct 2025 00:10:18 +0300 Subject: [PATCH 3/4] Apply suggestions from code review Co-authored-by: Victor Chernyakin --- .../bugprone/ExceptionTypeNotNothrowCopyConstructibleCheck.h | 1 - .../bugprone/exception-type-not-nothrow-copy-constructible.rst | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/ExceptionTypeNotNothrowCopyConstructibleCheck.h b/clang-tools-extra/clang-tidy/bugprone/ExceptionTypeNotNothrowCopyConstructibleCheck.h index f7b8764acd0a6..5af59eb580d73 100644 --- a/clang-tools-extra/clang-tidy/bugprone/ExceptionTypeNotNothrowCopyConstructibleCheck.h +++ b/clang-tools-extra/clang-tidy/bugprone/ExceptionTypeNotNothrowCopyConstructibleCheck.h @@ -1,5 +1,4 @@ //===----------------------------------------------------------------------===// -//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/exception-type-not-nothrow-copy-constructible.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/exception-type-not-nothrow-copy-constructible.rst index 9875a48a4f633..a0adb9a2e0b15 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/exception-type-not-nothrow-copy-constructible.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/exception-type-not-nothrow-copy-constructible.rst @@ -1,7 +1,7 @@ .. title:: clang-tidy - bugprone-exception-type-not-nothrow-copy-constructible bugprone-exception-type-not-nothrow-copy-constructible -======================================================= +====================================================== Checks whether a thrown object is nothrow copy constructible. From f6aadcd166be03a37b31a7d0f45599bf62305d15 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Sun, 2 Nov 2025 18:59:10 +0300 Subject: [PATCH 4/4] Rename to bugprone-exception-copy-constructor-throws --- .../bugprone/BugproneTidyModule.cpp | 6 ++-- .../clang-tidy/bugprone/CMakeLists.txt | 2 +- ...> ExceptionCopyConstructorThrowsCheck.cpp} | 6 ++-- ... => ExceptionCopyConstructorThrowsCheck.h} | 13 ++++---- .../clang-tidy/cert/CERTTidyModule.cpp | 7 ++--- clang-tools-extra/docs/ReleaseNotes.rst | 4 +-- .../exception-copy-constructor-throws.rst | 31 +++++++++++++++++++ ...on-type-not-nothrow-copy-constructible.rst | 31 ------------------- .../docs/clang-tidy/checks/cert/err60-cpp.rst | 4 +-- .../docs/clang-tidy/checks/list.rst | 4 +-- ... => exception-copy-constructor-throws.cpp} | 4 +-- 11 files changed, 55 insertions(+), 57 deletions(-) rename clang-tools-extra/clang-tidy/bugprone/{ExceptionTypeNotNothrowCopyConstructibleCheck.cpp => ExceptionCopyConstructorThrowsCheck.cpp} (85%) rename clang-tools-extra/clang-tidy/bugprone/{ExceptionTypeNotNothrowCopyConstructibleCheck.h => ExceptionCopyConstructorThrowsCheck.h} (63%) create mode 100644 clang-tools-extra/docs/clang-tidy/checks/bugprone/exception-copy-constructor-throws.rst delete mode 100644 clang-tools-extra/docs/clang-tidy/checks/bugprone/exception-type-not-nothrow-copy-constructible.rst rename clang-tools-extra/test/clang-tidy/checkers/bugprone/{exception-type-not-nothrow-copy-constructible.cpp => exception-copy-constructor-throws.cpp} (95%) diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp index eed2a10450e6a..bd1a43ee226f7 100644 --- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp @@ -28,8 +28,8 @@ #include "DynamicStaticInitializersCheck.h" #include "EasilySwappableParametersCheck.h" #include "EmptyCatchCheck.h" +#include "ExceptionCopyConstructorThrowsCheck.h" #include "ExceptionEscapeCheck.h" -#include "ExceptionTypeNotNothrowCopyConstructibleCheck.h" #include "FoldInitTypeCheck.h" #include "ForwardDeclarationNamespaceCheck.h" #include "ForwardingReferenceOverloadCheck.h" @@ -147,10 +147,10 @@ class BugproneModule : public ClangTidyModule { CheckFactories.registerCheck( "bugprone-easily-swappable-parameters"); CheckFactories.registerCheck("bugprone-empty-catch"); + CheckFactories.registerCheck( + "bugprone-exception-copy-constructor-throws"); CheckFactories.registerCheck( "bugprone-exception-escape"); - CheckFactories.registerCheck( - "bugprone-exception-type-not-nothrow-copy-constructible"); CheckFactories.registerCheck("bugprone-fold-init-type"); CheckFactories.registerCheck( "bugprone-forward-declaration-namespace"); diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt index 5199552923e20..fa47c8ad0f49e 100644 --- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt @@ -24,8 +24,8 @@ add_clang_library(clangTidyBugproneModule STATIC DynamicStaticInitializersCheck.cpp EasilySwappableParametersCheck.cpp EmptyCatchCheck.cpp + ExceptionCopyConstructorThrowsCheck.cpp ExceptionEscapeCheck.cpp - ExceptionTypeNotNothrowCopyConstructibleCheck.cpp FoldInitTypeCheck.cpp ForwardDeclarationNamespaceCheck.cpp ForwardingReferenceOverloadCheck.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/ExceptionTypeNotNothrowCopyConstructibleCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ExceptionCopyConstructorThrowsCheck.cpp similarity index 85% rename from clang-tools-extra/clang-tidy/bugprone/ExceptionTypeNotNothrowCopyConstructibleCheck.cpp rename to clang-tools-extra/clang-tidy/bugprone/ExceptionCopyConstructorThrowsCheck.cpp index 6d0dae21b8c67..73658459b8e26 100644 --- a/clang-tools-extra/clang-tidy/bugprone/ExceptionTypeNotNothrowCopyConstructibleCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/ExceptionCopyConstructorThrowsCheck.cpp @@ -6,7 +6,7 @@ // //===----------------------------------------------------------------------===// -#include "ExceptionTypeNotNothrowCopyConstructibleCheck.h" +#include "ExceptionCopyConstructorThrowsCheck.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" @@ -14,7 +14,7 @@ using namespace clang::ast_matchers; namespace clang::tidy::bugprone { -void ExceptionTypeNotNothrowCopyConstructibleCheck::registerMatchers( +void ExceptionCopyConstructorThrowsCheck::registerMatchers( MatchFinder *Finder) { Finder->addMatcher( traverse( @@ -26,7 +26,7 @@ void ExceptionTypeNotNothrowCopyConstructibleCheck::registerMatchers( this); } -void ExceptionTypeNotNothrowCopyConstructibleCheck::check( +void ExceptionCopyConstructorThrowsCheck::check( const MatchFinder::MatchResult &Result) { const auto *E = Result.Nodes.getNodeAs("expr"); diag(E->getExprLoc(), diff --git a/clang-tools-extra/clang-tidy/bugprone/ExceptionTypeNotNothrowCopyConstructibleCheck.h b/clang-tools-extra/clang-tidy/bugprone/ExceptionCopyConstructorThrowsCheck.h similarity index 63% rename from clang-tools-extra/clang-tidy/bugprone/ExceptionTypeNotNothrowCopyConstructibleCheck.h rename to clang-tools-extra/clang-tidy/bugprone/ExceptionCopyConstructorThrowsCheck.h index 5af59eb580d73..f1d7cca0e5bad 100644 --- a/clang-tools-extra/clang-tidy/bugprone/ExceptionTypeNotNothrowCopyConstructibleCheck.h +++ b/clang-tools-extra/clang-tidy/bugprone/ExceptionCopyConstructorThrowsCheck.h @@ -6,8 +6,8 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_EXCEPTIONTYPENOTNOTHROWCOPYCONSTRUCTIBLECHECK_H -#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_EXCEPTIONTYPENOTNOTHROWCOPYCONSTRUCTIBLECHECK_H +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_EXCEPTIONCOPYCONSTRUCTORTHROWSCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_EXCEPTIONCOPYCONSTRUCTORTHROWSCHECK_H #include "../ClangTidyCheck.h" @@ -16,11 +16,10 @@ namespace clang::tidy::bugprone { /// Checks whether a thrown object is nothrow copy constructible. /// /// For the user-facing documentation see: -/// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/exception-type-not-nothrow-copy-constructible.html -class ExceptionTypeNotNothrowCopyConstructibleCheck : public ClangTidyCheck { +/// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/exception-copy-constructor-throws.html +class ExceptionCopyConstructorThrowsCheck : public ClangTidyCheck { public: - ExceptionTypeNotNothrowCopyConstructibleCheck(StringRef Name, - ClangTidyContext *Context) + ExceptionCopyConstructorThrowsCheck(StringRef Name, ClangTidyContext *Context) : ClangTidyCheck(Name, Context) {} bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { return LangOpts.CPlusPlus; @@ -31,4 +30,4 @@ class ExceptionTypeNotNothrowCopyConstructibleCheck : public ClangTidyCheck { } // namespace clang::tidy::bugprone -#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_EXCEPTIONTYPENOTNOTHROWCOPYCONSTRUCTIBLECHECK_H +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_EXCEPTIONCOPYCONSTRUCTORTHROWSCHECK_H diff --git a/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp b/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp index 4080977a5c3e1..f081e33515208 100644 --- a/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp @@ -11,7 +11,7 @@ #include "../ClangTidyModuleRegistry.h" #include "../bugprone/BadSignalToKillThreadCheck.h" #include "../bugprone/CommandProcessorCheck.h" -#include "../bugprone/ExceptionTypeNotNothrowCopyConstructibleCheck.h" +#include "../bugprone/ExceptionCopyConstructorThrowsCheck.h" #include "../bugprone/PointerArithmeticOnPolymorphicObjectCheck.h" #include "../bugprone/ReservedIdentifierCheck.h" #include "../bugprone/SignalHandlerCheck.h" @@ -261,9 +261,8 @@ class CERTModule : public ClangTidyModule { "cert-err52-cpp"); CheckFactories.registerCheck( "cert-err58-cpp"); - CheckFactories - .registerCheck( - "cert-err60-cpp"); + CheckFactories.registerCheck( + "cert-err60-cpp"); CheckFactories.registerCheck( "cert-err61-cpp"); // MEM diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 4496ae9f727e4..044bee785f85b 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -250,8 +250,8 @@ New check aliases keeping initial check as an alias to the new one. - Renamed :doc:`cert-err60-cpp ` to - :doc:`bugprone-exception-type-not-nothrow-copy-constructible - ` + :doc:`bugprone-exception-copy-constructor-throws + ` keeping initial check as an alias to the new one. Changes in existing checks diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/exception-copy-constructor-throws.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/exception-copy-constructor-throws.rst new file mode 100644 index 0000000000000..8c3becf80a541 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/exception-copy-constructor-throws.rst @@ -0,0 +1,31 @@ +.. title:: clang-tidy - bugprone-exception-copy-constructor-throws + +bugprone-exception-copy-constructor-throws +========================================== + +Checks whether a thrown object's copy constructor can throw. + +Exception objects are required to be copy constructible in C++. However, an +exception's copy constructor should not throw to avoid potential issues when +unwinding the stack. If an exception is thrown during stack unwinding (such +as from a copy constructor of an exception object), the program will +terminate via ``std::terminate``. + +.. code-block:: c++ + + class SomeException { + public: + SomeException() = default; + SomeException(const SomeException&) { /* may throw */ } + }; + + void f() { + throw SomeException(); // warning: thrown exception type's copy constructor can throw + } + +References +---------- + +This check corresponds to the CERT C++ Coding Standard rule +`ERR60-CPP. Exception objects must be nothrow copy constructible +`_. \ No newline at end of file diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/exception-type-not-nothrow-copy-constructible.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/exception-type-not-nothrow-copy-constructible.rst deleted file mode 100644 index a0adb9a2e0b15..0000000000000 --- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/exception-type-not-nothrow-copy-constructible.rst +++ /dev/null @@ -1,31 +0,0 @@ -.. title:: clang-tidy - bugprone-exception-type-not-nothrow-copy-constructible - -bugprone-exception-type-not-nothrow-copy-constructible -====================================================== - -Checks whether a thrown object is nothrow copy constructible. - -Exception objects are required to be copy constructible in C++. However, -exceptions should generally be nothrow copy constructible to avoid potential -issues when unwinding the stack. If an exception is thrown during stack -unwinding (such as from a copy constructor of an exception object), the -program will terminate via ``std::terminate``. - -.. code-block:: c++ - - class SomeException { - public: - SomeException() = default; - SomeException(const SomeException&) { /* may throw */ } - }; - - void f() { - throw SomeException(); // warning: thrown exception type is not nothrow copy constructible - } - -References ----------- - -This check corresponds to the CERT C++ Coding Standard rule -`ERR60-CPP. Exception objects must be nothrow copy constructible -`_. \ No newline at end of file diff --git a/clang-tools-extra/docs/clang-tidy/checks/cert/err60-cpp.rst b/clang-tools-extra/docs/clang-tidy/checks/cert/err60-cpp.rst index bbdb2975835a8..8d6dd1bf4b9b7 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/cert/err60-cpp.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/cert/err60-cpp.rst @@ -1,12 +1,12 @@ .. title:: clang-tidy - cert-err60-cpp .. meta:: - :http-equiv=refresh: 5;URL=../bugprone/exception-type-not-nothrow-copy-constructible.html + :http-equiv=refresh: 5;URL=../bugprone/exception-copy-constructor-throws.html cert-err60-cpp ============== The `cert-err60-cpp` check is an alias, please see -`bugprone-exception-type-not-nothrow-copy-constructible <../bugprone/exception-type-not-nothrow-copy-constructible.html>`_ +`bugprone-exception-copy-constructor-throws <../bugprone/exception-copy-constructor-throws.html>`_ for more information. This check corresponds to the CERT C++ Coding Standard rule diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index bfb21057aa02b..fdc7c7171d036 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -96,8 +96,8 @@ Clang-Tidy Checks :doc:`bugprone-dynamic-static-initializers `, :doc:`bugprone-easily-swappable-parameters `, :doc:`bugprone-empty-catch `, + :doc:`bugprone-exception-copy-constructor-throws `, :doc:`bugprone-exception-escape `, - :doc:`bugprone-exception-type-not-nothrow-copy-constructible `, :doc:`bugprone-fold-init-type `, :doc:`bugprone-forward-declaration-namespace `, :doc:`bugprone-forwarding-reference-overload `, @@ -446,7 +446,7 @@ Check aliases :doc:`cert-err34-c `, :doc:`bugprone-unchecked-string-to-number-conversion `, :doc:`cert-err52-cpp `, :doc:`modernize-avoid-setjmp-longjmp `, :doc:`cert-err58-cpp `, :doc:`bugprone-throwing-static-initialization `, - :doc:`cert-err60-cpp `, :doc:`bugprone-exception-type-not-nothrow-copy-constructible `, + :doc:`cert-err60-cpp `, :doc:`bugprone-exception-copy-constructor-throws `, :doc:`cert-err61-cpp `, :doc:`misc-throw-by-value-catch-by-reference `, :doc:`cert-exp42-c `, :doc:`bugprone-suspicious-memory-comparison `, :doc:`cert-fio38-c `, :doc:`misc-non-copyable-objects `, diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-type-not-nothrow-copy-constructible.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-copy-constructor-throws.cpp similarity index 95% rename from clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-type-not-nothrow-copy-constructible.cpp rename to clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-copy-constructor-throws.cpp index ddcc6b1b1848f..7e2d586175c1b 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-type-not-nothrow-copy-constructible.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-copy-constructor-throws.cpp @@ -1,4 +1,4 @@ -// RUN: %check_clang_tidy -std=c++11,c++14 %s bugprone-exception-type-not-nothrow-copy-constructible %t -- -- -fcxx-exceptions +// RUN: %check_clang_tidy -std=c++11,c++14 %s bugprone-exception-copy-constructor-throws %t -- -- -fcxx-exceptions // FIXME: Split off parts of this test that rely on dynamic exception // specifications, and run this test in all language modes. // FIXME: Fix the checker to work in C++17 or later mode. @@ -92,7 +92,7 @@ void f() { throw U(); // ok throw V(); // ok throw W(); // match, noexcept(false) - // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: thrown exception type is not nothrow copy constructible [bugprone-exception-type-not-nothrow-copy-constructible] + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: thrown exception type is not nothrow copy constructible [bugprone-exception-copy-constructor-throws] throw X(); // match, no noexcept clause, nontrivial // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: thrown exception type is not nothrow copy constructible throw Y(); // ok