Skip to content

Commit 778e2b5

Browse files
authored
Merge branch 'main' into attributes
2 parents fe5f361 + 2d48489 commit 778e2b5

File tree

149 files changed

+1866
-502
lines changed

Some content is hidden

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

149 files changed

+1866
-502
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,10 @@ void UseAfterMoveFinder::getReinits(
315315
"::std::unordered_map", "::std::unordered_multiset",
316316
"::std::unordered_multimap"))))));
317317

318-
auto StandardSmartPointerTypeMatcher = hasType(hasUnqualifiedDesugaredType(
319-
recordType(hasDeclaration(cxxRecordDecl(hasAnyName(
320-
"::std::unique_ptr", "::std::shared_ptr", "::std::weak_ptr"))))));
318+
auto StandardResettableOwnerTypeMatcher = hasType(
319+
hasUnqualifiedDesugaredType(recordType(hasDeclaration(cxxRecordDecl(
320+
hasAnyName("::std::unique_ptr", "::std::shared_ptr",
321+
"::std::weak_ptr", "::std::optional", "::std::any"))))));
321322

322323
// Matches different types of reinitialization.
323324
auto ReinitMatcher =
@@ -340,7 +341,7 @@ void UseAfterMoveFinder::getReinits(
340341
callee(cxxMethodDecl(hasAnyName("clear", "assign")))),
341342
// reset() on standard smart pointers.
342343
cxxMemberCallExpr(
343-
on(expr(DeclRefMatcher, StandardSmartPointerTypeMatcher)),
344+
on(expr(DeclRefMatcher, StandardResettableOwnerTypeMatcher)),
344345
callee(cxxMethodDecl(hasName("reset")))),
345346
// Methods that have the [[clang::reinitializes]] attribute.
346347
cxxMemberCallExpr(

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ Changes in existing checks
194194
<clang-tidy/checks/bugprone/unsafe-functions>` check to allow specifying
195195
additional functions to match.
196196

197+
- Improved :doc:`bugprone-use-after-move
198+
<clang-tidy/checks/bugprone/use-after-move>` to avoid triggering on
199+
``reset()`` calls on moved-from ``std::optional`` and ``std::any`` objects,
200+
similarly to smart pointers.
201+
197202
- Improved :doc:`cert-flp30-c <clang-tidy/checks/cert/flp30-c>` check to
198203
fix false positive that floating point variable is only used in increment
199204
expression.

clang-tools-extra/docs/clang-tidy/checks/bugprone/use-after-move.rst

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,13 @@ Any occurrence of the moved variable that is not a reinitialization (see below)
196196
is considered to be a use.
197197

198198
An exception to this are objects of type ``std::unique_ptr``,
199-
``std::shared_ptr`` and ``std::weak_ptr``, which have defined move behavior
200-
(objects of these classes are guaranteed to be empty after they have been moved
201-
from). Therefore, an object of these classes will only be considered to be used
202-
if it is dereferenced, i.e. if ``operator*``, ``operator->`` or ``operator[]``
203-
(in the case of ``std::unique_ptr<T []>``) is called on it.
199+
``std::shared_ptr``, ``std::weak_ptr``, ``std::optional``, and ``std::any``.
200+
An exception to this are objects of type ``std::unique_ptr``,
201+
``std::shared_ptr``, ``std::weak_ptr``, ``std::optional``, and ``std::any``, which
202+
can be reinitialized via ``reset``. For smart pointers specifically, the
203+
moved-from objects have a well-defined state of being ``nullptr``s, and only
204+
``operator*``, ``operator->`` and ``operator[]`` are considered bad accesses as
205+
they would be dereferencing a ``nullptr``.
204206

205207
If multiple uses occur after a move, only the first of these is flagged.
206208

@@ -222,7 +224,8 @@ The check considers a variable to be reinitialized in the following cases:
222224
``unordered_multimap``.
223225

224226
- ``reset()`` is called on the variable and the variable is of type
225-
``std::unique_ptr``, ``std::shared_ptr`` or ``std::weak_ptr``.
227+
``std::unique_ptr``, ``std::shared_ptr``, ``std::weak_ptr``,
228+
``std::optional``, or ``std::any``.
226229

227230
- A member function marked with the ``[[clang::reinitializes]]`` attribute is
228231
called on the variable.

clang-tools-extra/test/clang-tidy/checkers/bugprone/use-after-move.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ struct weak_ptr {
3333
bool expired() const;
3434
};
3535

36+
template <typename T>
37+
struct optional {
38+
optional();
39+
T& operator*();
40+
const T& operator*() const;
41+
void reset();
42+
};
43+
44+
struct any {
45+
any();
46+
void reset();
47+
};
48+
3649
template <typename T1, typename T2>
3750
struct pair {};
3851

@@ -257,6 +270,14 @@ void standardSmartPtr() {
257270
// CHECK-NOTES: [[@LINE-1]]:5: warning: 'ptr' used after it was moved
258271
// CHECK-NOTES: [[@LINE-3]]:5: note: move occurred here
259272
}
273+
{
274+
std::optional<A> opt;
275+
std::move(opt);
276+
A val = *opt;
277+
(void)val;
278+
// CHECK-NOTES: [[@LINE-2]]:14: warning: 'opt' used after it was moved
279+
// CHECK-NOTES: [[@LINE-4]]:5: note: move occurred here
280+
}
260281
{
261282
// std::weak_ptr<> cannot be dereferenced directly, so we only check that
262283
// member functions may be called on it after a move.
@@ -994,10 +1015,10 @@ void standardContainerAssignIsReinit() {
9941015
}
9951016
}
9961017

997-
// Resetting the standard smart pointer types using reset() is treated as a
1018+
// Resetting the standard smart owning types using reset() is treated as a
9981019
// re-initialization. (We don't test std::weak_ptr<> because it can't be
9991020
// dereferenced directly.)
1000-
void standardSmartPointerResetIsReinit() {
1021+
void resetIsReinit() {
10011022
{
10021023
std::unique_ptr<A> ptr;
10031024
std::move(ptr);
@@ -1010,6 +1031,20 @@ void standardSmartPointerResetIsReinit() {
10101031
ptr.reset(new A);
10111032
*ptr;
10121033
}
1034+
{
1035+
std::optional<A> opt;
1036+
std::move(opt);
1037+
opt.reset();
1038+
std::optional<A> opt2 = opt;
1039+
(void)opt2;
1040+
}
1041+
{
1042+
std::any a;
1043+
std::move(a);
1044+
a.reset();
1045+
std::any a2 = a;
1046+
(void)a2;
1047+
}
10131048
}
10141049

10151050
void reinitAnnotation() {

clang/docs/ReleaseNotes.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,17 @@ Target Specific Changes
710710
AMDGPU Support
711711
^^^^^^^^^^^^^^
712712

713+
- Added headers ``gpuintrin.h`` and ``amdgpuintrin.h`` that contains common
714+
definitions for GPU builtin functions. This header can be included for OpenMP,
715+
CUDA, HIP, OpenCL, and C/C++.
716+
717+
NVPTX Support
718+
^^^^^^^^^^^^^^
719+
720+
- Added headers ``gpuintrin.h`` and ``nvptxintrin.h`` that contains common
721+
definitions for GPU builtin functions. This header can be included for OpenMP,
722+
CUDA, HIP, OpenCL, and C/C++.
723+
713724
X86 Support
714725
^^^^^^^^^^^
715726

clang/include/clang/AST/Type.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include "clang/Basic/PointerAuthOptions.h"
3232
#include "clang/Basic/SourceLocation.h"
3333
#include "clang/Basic/Specifiers.h"
34-
#include "clang/Basic/TargetInfo.h"
3534
#include "clang/Basic/Visibility.h"
3635
#include "llvm/ADT/APInt.h"
3736
#include "llvm/ADT/APSInt.h"

clang/lib/Driver/Compilation.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@
1010
#include "clang/Basic/LLVM.h"
1111
#include "clang/Driver/Action.h"
1212
#include "clang/Driver/Driver.h"
13-
#include "clang/Driver/DriverDiagnostic.h"
1413
#include "clang/Driver/Job.h"
1514
#include "clang/Driver/Options.h"
1615
#include "clang/Driver/ToolChain.h"
1716
#include "clang/Driver/Util.h"
18-
#include "llvm/ADT/STLExtras.h"
19-
#include "llvm/ADT/SmallVector.h"
2017
#include "llvm/Option/ArgList.h"
2118
#include "llvm/Option/OptSpecifier.h"
2219
#include "llvm/Option/Option.h"

clang/lib/Driver/Distro.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include "clang/Driver/Distro.h"
1010
#include "clang/Basic/LLVM.h"
11-
#include "llvm/ADT/SmallVector.h"
1211
#include "llvm/ADT/StringRef.h"
1312
#include "llvm/ADT/StringSwitch.h"
1413
#include "llvm/Support/ErrorOr.h"

clang/lib/Driver/Driver.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
#include "clang/Config/config.h"
5757
#include "clang/Driver/Action.h"
5858
#include "clang/Driver/Compilation.h"
59-
#include "clang/Driver/DriverDiagnostic.h"
6059
#include "clang/Driver/InputInfo.h"
6160
#include "clang/Driver/Job.h"
6261
#include "clang/Driver/Options.h"

clang/lib/Driver/DriverOptions.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "clang/Driver/Options.h"
10-
#include "llvm/ADT/STLExtras.h"
1110
#include "llvm/Option/OptTable.h"
12-
#include "llvm/Option/Option.h"
1311
#include <cassert>
1412

1513
using namespace clang::driver;

0 commit comments

Comments
 (0)