Skip to content

Commit 356c2c2

Browse files
authored
Fix llvm#75686: add iter_swap and iter_move to the matched name (llvm#76117)
Added support for iter_swap, iter_move in bugprone-exception-escape and performance-noexcept-swap checks. Fixes llvm#75686
1 parent d3ac676 commit 356c2c2

File tree

7 files changed

+40
-12
lines changed

7 files changed

+40
-12
lines changed

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ AST_MATCHER(FunctionDecl, isExplicitThrow) {
2828
Node.getExceptionSpecSourceRange().isValid();
2929
}
3030

31+
AST_MATCHER(FunctionDecl, hasAtLeastOneParameter) {
32+
return Node.getNumParams() > 0;
33+
}
34+
3135
} // namespace
3236

3337
ExceptionEscapeCheck::ExceptionEscapeCheck(StringRef Name,
@@ -58,14 +62,16 @@ void ExceptionEscapeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
5862

5963
void ExceptionEscapeCheck::registerMatchers(MatchFinder *Finder) {
6064
Finder->addMatcher(
61-
functionDecl(isDefinition(),
62-
anyOf(isNoThrow(),
63-
allOf(anyOf(cxxDestructorDecl(),
64-
cxxConstructorDecl(isMoveConstructor()),
65-
cxxMethodDecl(isMoveAssignmentOperator()),
66-
isMain(), hasName("swap")),
67-
unless(isExplicitThrow())),
68-
isEnabled(FunctionsThatShouldNotThrow)))
65+
functionDecl(
66+
isDefinition(),
67+
anyOf(isNoThrow(),
68+
allOf(anyOf(cxxDestructorDecl(),
69+
cxxConstructorDecl(isMoveConstructor()),
70+
cxxMethodDecl(isMoveAssignmentOperator()), isMain(),
71+
allOf(hasAnyName("swap", "iter_swap", "iter_move"),
72+
hasAtLeastOneParameter())),
73+
unless(isExplicitThrow())),
74+
isEnabled(FunctionsThatShouldNotThrow)))
6975
.bind("thrower"),
7076
this);
7177
}

clang-tools-extra/clang-tidy/performance/NoexceptSwapCheck.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ void NoexceptSwapCheck::registerMatchers(MatchFinder *Finder) {
3939
.bind("type"))))),
4040
hasParameter(1, hasType(qualType(hasCanonicalType(
4141
qualType(equalsBoundNode("type")))))));
42-
Finder->addMatcher(functionDecl(unless(isDeleted()), hasName("swap"),
42+
Finder->addMatcher(functionDecl(unless(isDeleted()),
43+
hasAnyName("swap", "iter_swap"),
4344
anyOf(MethodMatcher, FunctionMatcher))
4445
.bind(BindFuncDeclName),
4546
this);

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ Changes in existing checks
260260
casting during type conversions at variable initialization, now with improved
261261
compatibility for C++17 and later versions.
262262

263+
- Improved :doc:`bugprone-exception-escape
264+
<clang-tidy/checks/bugprone/exception-escape>` check by extending the default
265+
check function names to include ``iter_swap`` and ``iter_move``.
266+
263267
- Improved :doc:`bugprone-implicit-widening-of-multiplication-result
264268
<clang-tidy/checks/bugprone/implicit-widening-of-multiplication-result>` check
265269
to correctly emit fixes.
@@ -445,7 +449,8 @@ Changes in existing checks
445449
- Improved :doc:`performance-noexcept-swap
446450
<clang-tidy/checks/performance/noexcept-swap>` check to enforce a stricter
447451
match with the swap function signature and better handling of condition
448-
noexcept expressions, eliminating false-positives.
452+
noexcept expressions, eliminating false-positives. ``iter_swap`` function name
453+
is checked by default.
449454

450455
- Improved :doc:`readability-braces-around-statements
451456
<clang-tidy/checks/readability/braces-around-statements>` check to

clang-tools-extra/docs/clang-tidy/checks/bugprone/exception-escape.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ should not. The functions which should not throw exceptions are the following:
1111
* Move assignment operators
1212
* The ``main()`` functions
1313
* ``swap()`` functions
14+
* ``iter_swap()`` functions
15+
* ``iter_move()`` functions
1416
* Functions marked with ``throw()`` or ``noexcept``
1517
* Other functions given as option
1618

clang-tools-extra/docs/clang-tidy/checks/performance/noexcept-swap.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
performance-noexcept-swap
44
=========================
55

6-
The check flags user-defined swap functions not marked with ``noexcept`` or
6+
The check flags user-defined swap and iter_swap functions not marked with ``noexcept`` or
77
marked with ``noexcept(expr)`` where ``expr`` evaluates to ``false``
88
(but is not a ``false`` literal itself).
99

10-
When a swap function is marked as ``noexcept``, it assures the compiler that
10+
When a swap or iter_swap function is marked as ``noexcept``, it assures the compiler that
1111
no exceptions will be thrown during the swapping of two objects, which allows
1212
the compiler to perform certain optimizations such as omitting exception
1313
handling code.

clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,16 @@ void swap(int&, int&) {
586586
throw 1;
587587
}
588588

589+
void iter_swap(int&, int&) {
590+
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: an exception may be thrown in function 'iter_swap' which should not throw exceptions
591+
throw 1;
592+
}
593+
594+
void iter_move(int&) {
595+
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: an exception may be thrown in function 'iter_move' which should not throw exceptions
596+
throw 1;
597+
}
598+
589599
namespace std {
590600
class bad_alloc {};
591601
}

clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-swap.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ void swap(A &, A &);
3232
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: swap functions should be marked noexcept [performance-noexcept-swap]
3333
// CHECK-FIXES: void swap(A &, A &) noexcept ;
3434

35+
void iter_swap(A &, A &);
36+
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: swap functions should be marked noexcept [performance-noexcept-swap]
37+
// CHECK-FIXES: void iter_swap(A &, A &) noexcept ;
38+
3539
struct B {
3640
static constexpr bool kFalse = false;
3741
void swap(B &) noexcept(kFalse);

0 commit comments

Comments
 (0)