Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -148,6 +149,8 @@ class BugproneModule : public ClangTidyModule {
CheckFactories.registerCheck<EmptyCatchCheck>("bugprone-empty-catch");
CheckFactories.registerCheck<ExceptionEscapeCheck>(
"bugprone-exception-escape");
CheckFactories.registerCheck<ExceptionTypeNotNothrowCopyConstructibleCheck>(
"bugprone-exception-type-not-nothrow-copy-constructible");
CheckFactories.registerCheck<FoldInitTypeCheck>("bugprone-fold-init-type");
CheckFactories.registerCheck<ForwardDeclarationNamespaceCheck>(
"bugprone-forward-declaration-namespace");
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ add_clang_library(clangTidyBugproneModule STATIC
EasilySwappableParametersCheck.cpp
EmptyCatchCheck.cpp
ExceptionEscapeCheck.cpp
ExceptionTypeNotNothrowCopyConstructibleCheck.cpp
FoldInitTypeCheck.cpp
ForwardDeclarationNamespaceCheck.cpp
ForwardingReferenceOverloadCheck.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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>("expr");
diag(E->getExprLoc(),
"thrown exception type is not nothrow copy constructible");
}

} // namespace clang::tidy::cert
} // namespace clang::tidy::bugprone
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,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;
Expand All @@ -28,6 +29,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
6 changes: 4 additions & 2 deletions clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -41,7 +42,6 @@
#include "MutatingCopyCheck.h"
#include "NonTrivialTypesLibcMemoryCallsCheck.h"
#include "ProperlySeededRandomGeneratorCheck.h"
#include "ThrownExceptionTypeCheck.h"

namespace {

Expand Down Expand Up @@ -261,7 +261,9 @@ class CERTModule : public ClangTidyModule {
"cert-err52-cpp");
CheckFactories.registerCheck<bugprone::ThrowingStaticInitializationCheck>(
"cert-err58-cpp");
CheckFactories.registerCheck<ThrownExceptionTypeCheck>("cert-err60-cpp");
CheckFactories
.registerCheck<bugprone::ExceptionTypeNotNothrowCopyConstructibleCheck>(
"cert-err60-cpp");
CheckFactories.registerCheck<misc::ThrowByValueCatchByReferenceCheck>(
"cert-err61-cpp");
// MEM
Expand Down
1 change: 0 additions & 1 deletion clang-tools-extra/clang-tidy/cert/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ add_clang_library(clangTidyCERTModule STATIC
MutatingCopyCheck.cpp
NonTrivialTypesLibcMemoryCallsCheck.cpp
ProperlySeededRandomGeneratorCheck.cpp
ThrownExceptionTypeCheck.cpp

LINK_LIBS
clangTidy
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ New check aliases
<clang-tidy/checks/bugprone/throwing-static-initialization>`
keeping initial check as an alias to the new one.

- Renamed :doc:`cert-err60-cpp <clang-tidy/checks/cert/err60-cpp>` to
:doc:`bugprone-exception-type-not-nothrow-copy-constructible
<clang-tidy/checks/bugprone/exception-type-not-nothrow-copy-constructible>`
keeping initial check as an alias to the new one.

Changes in existing checks
^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
Original file line number Diff line number Diff line change
@@ -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
<https://wiki.sei.cmu.edu/confluence/display/cplusplus/ERR60-CPP.+Exception+objects+must+be+nothrow+copy+constructible>`_.
9 changes: 6 additions & 3 deletions clang-tools-extra/docs/clang-tidy/checks/cert/err60-cpp.rst
Original file line number Diff line number Diff line change
@@ -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
<https://www.securecoding.cert.org/confluence/display/cplusplus/ERR60-CPP.+Exception+objects+must+be+nothrow+copy+constructible>`_.
<https://wiki.sei.cmu.edu/confluence/display/cplusplus/ERR60-CPP.+Exception+objects+must+be+nothrow+copy+constructible>`_.
3 changes: 2 additions & 1 deletion clang-tools-extra/docs/clang-tidy/checks/list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Clang-Tidy Checks
:doc:`bugprone-easily-swappable-parameters <bugprone/easily-swappable-parameters>`,
:doc:`bugprone-empty-catch <bugprone/empty-catch>`,
:doc:`bugprone-exception-escape <bugprone/exception-escape>`,
:doc:`bugprone-exception-type-not-nothrow-copy-constructible <bugprone/exception-type-not-nothrow-copy-constructible>`,
:doc:`bugprone-fold-init-type <bugprone/fold-init-type>`,
:doc:`bugprone-forward-declaration-namespace <bugprone/forward-declaration-namespace>`,
:doc:`bugprone-forwarding-reference-overload <bugprone/forwarding-reference-overload>`,
Expand Down Expand Up @@ -175,7 +176,6 @@ Clang-Tidy Checks
:doc:`bugprone-virtual-near-miss <bugprone/virtual-near-miss>`, "Yes"
:doc:`cert-dcl58-cpp <cert/dcl58-cpp>`,
:doc:`cert-err33-c <cert/err33-c>`,
:doc:`cert-err60-cpp <cert/err60-cpp>`,
:doc:`cert-flp30-c <cert/flp30-c>`,
:doc:`cert-mem57-cpp <cert/mem57-cpp>`,
:doc:`cert-msc50-cpp <cert/msc50-cpp>`,
Expand Down Expand Up @@ -446,6 +446,7 @@ Check aliases
:doc:`cert-err34-c <cert/err34-c>`, :doc:`bugprone-unchecked-string-to-number-conversion <bugprone/unchecked-string-to-number-conversion>`,
:doc:`cert-err52-cpp <cert/err52-cpp>`, :doc:`modernize-avoid-setjmp-longjmp <modernize/avoid-setjmp-longjmp>`,
:doc:`cert-err58-cpp <cert/err58-cpp>`, :doc:`bugprone-throwing-static-initialization <bugprone/throwing-static-initialization>`,
:doc:`cert-err60-cpp <cert/err60-cpp>`, :doc:`bugprone-exception-type-not-nothrow-copy-constructible <bugprone/exception-type-not-nothrow-copy-constructible>`,
:doc:`cert-err61-cpp <cert/err61-cpp>`, :doc:`misc-throw-by-value-catch-by-reference <misc/throw-by-value-catch-by-reference>`,
:doc:`cert-exp42-c <cert/exp42-c>`, :doc:`bugprone-suspicious-memory-comparison <bugprone/suspicious-memory-comparison>`,
:doc:`cert-fio38-c <cert/fio38-c>`, :doc:`misc-non-copyable-objects <misc/non-copyable-objects>`,
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down