Skip to content

Commit fb71c30

Browse files
authored
merge main into amd-staging (#547)
2 parents 73f26e2 + 3170777 commit fb71c30

File tree

146 files changed

+2754
-750
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

146 files changed

+2754
-750
lines changed

clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "DynamicStaticInitializersCheck.h"
3131
#include "EasilySwappableParametersCheck.h"
3232
#include "EmptyCatchCheck.h"
33+
#include "ExceptionCopyConstructorThrowsCheck.h"
3334
#include "ExceptionEscapeCheck.h"
3435
#include "FloatLoopCounterCheck.h"
3536
#include "FoldInitTypeCheck.h"
@@ -155,6 +156,8 @@ class BugproneModule : public ClangTidyModule {
155156
CheckFactories.registerCheck<EasilySwappableParametersCheck>(
156157
"bugprone-easily-swappable-parameters");
157158
CheckFactories.registerCheck<EmptyCatchCheck>("bugprone-empty-catch");
159+
CheckFactories.registerCheck<ExceptionCopyConstructorThrowsCheck>(
160+
"bugprone-exception-copy-constructor-throws");
158161
CheckFactories.registerCheck<ExceptionEscapeCheck>(
159162
"bugprone-exception-escape");
160163
CheckFactories.registerCheck<FloatLoopCounterCheck>(

clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ add_clang_library(clangTidyBugproneModule STATIC
2626
DynamicStaticInitializersCheck.cpp
2727
EasilySwappableParametersCheck.cpp
2828
EmptyCatchCheck.cpp
29+
ExceptionCopyConstructorThrowsCheck.cpp
2930
ExceptionEscapeCheck.cpp
3031
FloatLoopCounterCheck.cpp
3132
FoldInitTypeCheck.cpp

clang-tools-extra/clang-tidy/cert/ThrownExceptionTypeCheck.cpp renamed to clang-tools-extra/clang-tidy/bugprone/ExceptionCopyConstructorThrowsCheck.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "ThrownExceptionTypeCheck.h"
9+
#include "ExceptionCopyConstructorThrowsCheck.h"
1010
#include "clang/AST/ASTContext.h"
1111
#include "clang/ASTMatchers/ASTMatchFinder.h"
1212

1313
using namespace clang::ast_matchers;
1414

15-
namespace clang::tidy::cert {
15+
namespace clang::tidy::bugprone {
1616

17-
void ThrownExceptionTypeCheck::registerMatchers(MatchFinder *Finder) {
17+
void ExceptionCopyConstructorThrowsCheck::registerMatchers(
18+
MatchFinder *Finder) {
1819
Finder->addMatcher(
1920
traverse(
2021
TK_AsIs,
@@ -25,10 +26,11 @@ void ThrownExceptionTypeCheck::registerMatchers(MatchFinder *Finder) {
2526
this);
2627
}
2728

28-
void ThrownExceptionTypeCheck::check(const MatchFinder::MatchResult &Result) {
29+
void ExceptionCopyConstructorThrowsCheck::check(
30+
const MatchFinder::MatchResult &Result) {
2931
const auto *E = Result.Nodes.getNodeAs<Expr>("expr");
3032
diag(E->getExprLoc(),
3133
"thrown exception type is not nothrow copy constructible");
3234
}
3335

34-
} // namespace clang::tidy::cert
36+
} // namespace clang::tidy::bugprone

clang-tools-extra/clang-tidy/cert/ThrownExceptionTypeCheck.h renamed to clang-tools-extra/clang-tidy/bugprone/ExceptionCopyConstructorThrowsCheck.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_THROWNEXCEPTIONTYPECHECK_H
10-
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_THROWNEXCEPTIONTYPECHECK_H
9+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_EXCEPTIONCOPYCONSTRUCTORTHROWSCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_EXCEPTIONCOPYCONSTRUCTORTHROWSCHECK_H
1111

1212
#include "../ClangTidyCheck.h"
1313

14-
namespace clang::tidy::cert {
14+
namespace clang::tidy::bugprone {
1515

1616
/// Checks whether a thrown object is nothrow copy constructible.
1717
///
1818
/// For the user-facing documentation see:
19-
/// https://clang.llvm.org/extra/clang-tidy/checks/cert/err60-cpp.html
20-
class ThrownExceptionTypeCheck : public ClangTidyCheck {
19+
/// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/exception-copy-constructor-throws.html
20+
class ExceptionCopyConstructorThrowsCheck : public ClangTidyCheck {
2121
public:
22-
ThrownExceptionTypeCheck(StringRef Name, ClangTidyContext *Context)
22+
ExceptionCopyConstructorThrowsCheck(StringRef Name, ClangTidyContext *Context)
2323
: ClangTidyCheck(Name, Context) {}
2424
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
2525
return LangOpts.CPlusPlus;
@@ -28,6 +28,6 @@ class ThrownExceptionTypeCheck : public ClangTidyCheck {
2828
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
2929
};
3030

31-
} // namespace clang::tidy::cert
31+
} // namespace clang::tidy::bugprone
3232

33-
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_THROWNEXCEPTIONTYPECHECK_H
33+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_EXCEPTIONCOPYCONSTRUCTORTHROWSCHECK_H

clang-tools-extra/clang-tidy/bugprone/ExceptionEscapeCheck.cpp

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,22 @@ ExceptionEscapeCheck::ExceptionEscapeCheck(StringRef Name,
3636
ClangTidyContext *Context)
3737
: ClangTidyCheck(Name, Context), RawFunctionsThatShouldNotThrow(Options.get(
3838
"FunctionsThatShouldNotThrow", "")),
39-
RawIgnoredExceptions(Options.get("IgnoredExceptions", "")) {
39+
RawIgnoredExceptions(Options.get("IgnoredExceptions", "")),
40+
RawCheckedSwapFunctions(
41+
Options.get("CheckedSwapFunctions", "swap,iter_swap,iter_move")),
42+
CheckDestructors(Options.get("CheckDestructors", true)),
43+
CheckMoveMemberFunctions(Options.get("CheckMoveMemberFunctions", true)),
44+
CheckMain(Options.get("CheckMain", true)),
45+
CheckNothrowFunctions(Options.get("CheckNothrowFunctions", true)) {
4046
llvm::SmallVector<StringRef, 8> FunctionsThatShouldNotThrowVec,
41-
IgnoredExceptionsVec;
47+
IgnoredExceptionsVec, CheckedSwapFunctionsVec;
4248
RawFunctionsThatShouldNotThrow.split(FunctionsThatShouldNotThrowVec, ",", -1,
4349
false);
4450
FunctionsThatShouldNotThrow.insert_range(FunctionsThatShouldNotThrowVec);
4551

52+
RawCheckedSwapFunctions.split(CheckedSwapFunctionsVec, ",", -1, false);
53+
CheckedSwapFunctions.insert_range(CheckedSwapFunctionsVec);
54+
4655
llvm::StringSet<> IgnoredExceptions;
4756
RawIgnoredExceptions.split(IgnoredExceptionsVec, ",", -1, false);
4857
IgnoredExceptions.insert_range(IgnoredExceptionsVec);
@@ -54,20 +63,33 @@ void ExceptionEscapeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
5463
Options.store(Opts, "FunctionsThatShouldNotThrow",
5564
RawFunctionsThatShouldNotThrow);
5665
Options.store(Opts, "IgnoredExceptions", RawIgnoredExceptions);
66+
Options.store(Opts, "CheckedSwapFunctions", RawCheckedSwapFunctions);
67+
Options.store(Opts, "CheckDestructors", CheckDestructors);
68+
Options.store(Opts, "CheckMoveMemberFunctions", CheckMoveMemberFunctions);
69+
Options.store(Opts, "CheckMain", CheckMain);
70+
Options.store(Opts, "CheckNothrowFunctions", CheckNothrowFunctions);
5771
}
5872

5973
void ExceptionEscapeCheck::registerMatchers(MatchFinder *Finder) {
74+
auto MatchIf = [](bool Enabled, const auto &Matcher) {
75+
ast_matchers::internal::Matcher<FunctionDecl> Nothing = unless(anything());
76+
return Enabled ? Matcher : Nothing;
77+
};
6078
Finder->addMatcher(
6179
functionDecl(
6280
isDefinition(),
63-
anyOf(isNoThrow(),
64-
allOf(anyOf(cxxDestructorDecl(),
65-
cxxConstructorDecl(isMoveConstructor()),
66-
cxxMethodDecl(isMoveAssignmentOperator()), isMain(),
67-
allOf(hasAnyName("swap", "iter_swap", "iter_move"),
68-
hasAtLeastOneParameter())),
69-
unless(isExplicitThrow())),
70-
isEnabled(FunctionsThatShouldNotThrow)))
81+
anyOf(
82+
MatchIf(CheckNothrowFunctions, isNoThrow()),
83+
allOf(anyOf(MatchIf(CheckDestructors, cxxDestructorDecl()),
84+
MatchIf(
85+
CheckMoveMemberFunctions,
86+
anyOf(cxxConstructorDecl(isMoveConstructor()),
87+
cxxMethodDecl(isMoveAssignmentOperator()))),
88+
MatchIf(CheckMain, isMain()),
89+
allOf(isEnabled(CheckedSwapFunctions),
90+
hasAtLeastOneParameter())),
91+
unless(isExplicitThrow())),
92+
isEnabled(FunctionsThatShouldNotThrow)))
7193
.bind("thrower"),
7294
this);
7395
}

clang-tools-extra/clang-tidy/bugprone/ExceptionEscapeCheck.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,15 @@ class ExceptionEscapeCheck : public ClangTidyCheck {
3535
private:
3636
StringRef RawFunctionsThatShouldNotThrow;
3737
StringRef RawIgnoredExceptions;
38+
StringRef RawCheckedSwapFunctions;
39+
40+
const bool CheckDestructors;
41+
const bool CheckMoveMemberFunctions;
42+
const bool CheckMain;
43+
const bool CheckNothrowFunctions;
3844

3945
llvm::StringSet<> FunctionsThatShouldNotThrow;
46+
llvm::StringSet<> CheckedSwapFunctions;
4047
utils::ExceptionAnalyzer Tracer;
4148
};
4249

clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "../bugprone/CommandProcessorCheck.h"
1414
#include "../bugprone/CopyConstructorMutatesArgumentCheck.h"
1515
#include "../bugprone/DefaultOperatorNewOnOveralignedTypeCheck.h"
16+
#include "../bugprone/ExceptionCopyConstructorThrowsCheck.h"
1617
#include "../bugprone/FloatLoopCounterCheck.h"
1718
#include "../bugprone/PointerArithmeticOnPolymorphicObjectCheck.h"
1819
#include "../bugprone/RawMemoryCallOnNonTrivialTypeCheck.h"
@@ -41,7 +42,6 @@
4142
#include "../readability/UppercaseLiteralSuffixCheck.h"
4243
#include "LimitedRandomnessCheck.h"
4344
#include "ProperlySeededRandomGeneratorCheck.h"
44-
#include "ThrownExceptionTypeCheck.h"
4545

4646
namespace {
4747

@@ -262,7 +262,8 @@ class CERTModule : public ClangTidyModule {
262262
"cert-err52-cpp");
263263
CheckFactories.registerCheck<bugprone::ThrowingStaticInitializationCheck>(
264264
"cert-err58-cpp");
265-
CheckFactories.registerCheck<ThrownExceptionTypeCheck>("cert-err60-cpp");
265+
CheckFactories.registerCheck<bugprone::ExceptionCopyConstructorThrowsCheck>(
266+
"cert-err60-cpp");
266267
CheckFactories.registerCheck<misc::ThrowByValueCatchByReferenceCheck>(
267268
"cert-err61-cpp");
268269
// MEM

clang-tools-extra/clang-tidy/cert/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ add_clang_library(clangTidyCERTModule STATIC
77
CERTTidyModule.cpp
88
LimitedRandomnessCheck.cpp
99
ProperlySeededRandomGeneratorCheck.cpp
10-
ThrownExceptionTypeCheck.cpp
1110

1211
LINK_LIBS
1312
clangTidy

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ New check aliases
269269
<clang-tidy/checks/bugprone/throwing-static-initialization>`
270270
keeping initial check as an alias to the new one.
271271

272+
- Renamed :doc:`cert-err60-cpp <clang-tidy/checks/cert/err60-cpp>` to
273+
:doc:`bugprone-exception-copy-constructor-throws
274+
<clang-tidy/checks/bugprone/exception-copy-constructor-throws>`
275+
272276
- Renamed :doc:`cert-flp30-c <clang-tidy/checks/cert/flp30-c>` to
273277
:doc:`bugprone-float-loop-counter
274278
<clang-tidy/checks/bugprone/float-loop-counter>`
@@ -302,7 +306,9 @@ Changes in existing checks
302306
exceptions from captures are now diagnosed, exceptions in the bodies of
303307
lambdas that aren't actually invoked are not. Additionally, fixed an issue
304308
where the check wouldn't diagnose throws in arguments to functions or
305-
constructors.
309+
constructors. Added fine-grained configuration via options
310+
`CheckDestructors`, `CheckMoveMemberFunctions`, `CheckMain`,
311+
`CheckedSwapFunctions`, and `CheckNothrowFunctions`.
306312

307313
- Improved :doc:`bugprone-infinite-loop
308314
<clang-tidy/checks/bugprone/infinite-loop>` check by adding detection for
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
.. title:: clang-tidy - bugprone-exception-copy-constructor-throws
2+
3+
bugprone-exception-copy-constructor-throws
4+
==========================================
5+
6+
Checks whether a thrown object's copy constructor can throw.
7+
8+
Exception objects are required to be copy constructible in C++. However, an
9+
exception's copy constructor should not throw to avoid potential issues when
10+
unwinding the stack. If an exception is thrown during stack unwinding (such
11+
as from a copy constructor of an exception object), the program will
12+
terminate via ``std::terminate``.
13+
14+
.. code-block:: c++
15+
16+
class SomeException {
17+
public:
18+
SomeException() = default;
19+
SomeException(const SomeException&) { /* may throw */ }
20+
};
21+
22+
void f() {
23+
throw SomeException(); // warning: thrown exception type's copy constructor can throw
24+
}
25+
26+
References
27+
----------
28+
29+
This check corresponds to the CERT C++ Coding Standard rule
30+
`ERR60-CPP. Exception objects must be nothrow copy constructible
31+
<https://wiki.sei.cmu.edu/confluence/display/cplusplus/ERR60-CPP.+Exception+objects+must+be+nothrow+copy+constructible>`_.

0 commit comments

Comments
 (0)