[1/N] Enable cppcoreguidelines-special-member-functions (#137405)

Fixes #ISSUE_NUMBER

Pull Request resolved: https://github.com/pytorch/pytorch/pull/137405
Approved by: https://github.com/ezyang
This commit is contained in:
cyy
2024-10-23 00:16:53 +00:00
committed by PyTorch MergeBot
parent 7e951c1675
commit 38d3c27849
23 changed files with 89 additions and 10 deletions

View File

@ -112,6 +112,10 @@ class TORCH_API RefcountedMapAllocator : private RefcountedMapAllocatorArgCheck,
size_t size);
static RefcountedMapAllocator* fromDataPtr(const at::DataPtr&);
RefcountedMapAllocator(const RefcountedMapAllocator&) = delete;
RefcountedMapAllocator(RefcountedMapAllocator&&) = delete;
RefcountedMapAllocator& operator=(const RefcountedMapAllocator&) = delete;
RefcountedMapAllocator& operator=(RefcountedMapAllocator&&) = delete;
static at::DataPtr makeDataPtr(
const char* filename,
int flags,

View File

@ -87,8 +87,6 @@ void reportOutOfMemoryToProfiler(
}
}
MemoryReportingInfoBase::MemoryReportingInfoBase() = default;
void MemoryReportingInfoBase::reportOutOfMemory(
int64_t /*alloc_size*/,
size_t /*total_allocated*/,

View File

@ -157,6 +157,7 @@ inline bool operator!=(std::nullptr_t, const DataPtr& dp) noexcept {
// possible, or the raw interface will incorrectly reported as unsupported,
// when it is actually possible.
// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions)
struct C10_API Allocator {
virtual ~Allocator() = default;
@ -223,10 +224,24 @@ struct C10_API Allocator {
// allocation InefficientStdFunctionContext, on top of the dynamic
// allocation which is implied by std::function itself.
struct C10_API InefficientStdFunctionContext {
void* ptr_;
void* ptr_{nullptr};
std::function<void(void*)> deleter_;
InefficientStdFunctionContext(void* ptr, std::function<void(void*)> deleter)
: ptr_(ptr), deleter_(std::move(deleter)) {}
InefficientStdFunctionContext(const InefficientStdFunctionContext&) = delete;
InefficientStdFunctionContext(InefficientStdFunctionContext&& rhs) noexcept
: ptr_(std::exchange(rhs.ptr_, nullptr)),
deleter_(std::move(rhs.deleter_)) {}
InefficientStdFunctionContext& operator=(
const InefficientStdFunctionContext&) = delete;
// NOLINTNEXTLINE(performance-noexcept-move-constructor)
InefficientStdFunctionContext& operator=(
InefficientStdFunctionContext&& rhs) {
this->~InefficientStdFunctionContext();
ptr_ = std::exchange(rhs.ptr_, nullptr);
deleter_ = std::move(rhs.deleter_);
return *this;
}
~InefficientStdFunctionContext() {
if (deleter_) {
deleter_(ptr_);
@ -270,9 +285,6 @@ struct AllocatorRegisterer {
// An interface for reporting thread local memory usage
// per device
struct C10_API MemoryReportingInfoBase : public c10::DebugInfoBase {
MemoryReportingInfoBase();
~MemoryReportingInfoBase() override = default;
/**
* alloc_size corresponds to the size of the ptr.
*
@ -312,6 +324,7 @@ C10_API void reportOutOfMemoryToProfiler(
Device device);
// used to hold traceback information in allocators
// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions)
struct GatheredContext {
virtual ~GatheredContext() = default;
};

View File

@ -75,9 +75,6 @@ ProfiledCPUMemoryReporter& profiledCPUMemoryReporter() {
template <uint32_t PreGuardBytes, uint32_t PostGuardBytes>
class DefaultMobileCPUAllocator final : public at::Allocator {
public:
DefaultMobileCPUAllocator() = default;
~DefaultMobileCPUAllocator() override = default;
static void deleter(void* const pointer) {
if (C10_UNLIKELY(!pointer)) {
return;

View File

@ -34,6 +34,8 @@ class DeviceGuard {
const impl::DeviceGuardImplInterface* impl)
: guard_(device, impl) {}
~DeviceGuard() = default;
/// Copy is disallowed
DeviceGuard(const DeviceGuard&) = delete;
DeviceGuard& operator=(const DeviceGuard&) = delete;
@ -143,6 +145,7 @@ class OptionalDeviceGuard {
const impl::DeviceGuardImplInterface* impl)
: guard_(device, impl) {}
~OptionalDeviceGuard() = default;
/// Copy is disallowed
OptionalDeviceGuard(const OptionalDeviceGuard&) = delete;
OptionalDeviceGuard& operator=(const OptionalDeviceGuard&) = delete;

View File

@ -61,6 +61,7 @@ struct C10_API GeneratorImpl : public c10::intrusive_ptr_target {
GeneratorImpl(const GeneratorImpl& other) = delete;
GeneratorImpl(GeneratorImpl&& other) = delete;
GeneratorImpl& operator=(const GeneratorImpl& other) = delete;
GeneratorImpl& operator=(GeneratorImpl&& other) = delete;
~GeneratorImpl() override = default;
c10::intrusive_ptr<GeneratorImpl> clone() const;

View File

@ -16,6 +16,10 @@ struct C10_API AutoGradMode {
AutoGradMode(bool enabled) : prev_mode(GradMode::is_enabled()) {
GradMode::set_enabled(enabled);
}
AutoGradMode(const AutoGradMode&) = delete;
AutoGradMode(AutoGradMode&&) = delete;
AutoGradMode& operator=(const AutoGradMode&) = delete;
AutoGradMode& operator=(AutoGradMode&&) = delete;
~AutoGradMode() {
GradMode::set_enabled(prev_mode);
}
@ -35,6 +39,10 @@ struct C10_API AutoFwGradMode {
: prev_mode(AutogradState::get_tls_state().get_fw_grad_mode()) {
AutogradState::get_tls_state().set_fw_grad_mode(enabled);
}
AutoFwGradMode(const AutoFwGradMode&) = delete;
AutoFwGradMode(AutoFwGradMode&&) = delete;
AutoFwGradMode& operator=(const AutoFwGradMode&) = delete;
AutoFwGradMode& operator=(AutoFwGradMode&&) = delete;
~AutoFwGradMode() {
AutogradState::get_tls_state().set_fw_grad_mode(prev_mode);
}

View File

@ -73,6 +73,11 @@ struct C10_API InferenceMode {
c10::impl::_force_tls_local_dispatch_key_set(cur_keyset);
}
InferenceMode(const InferenceMode&) = delete;
InferenceMode(InferenceMode&&) = delete;
InferenceMode& operator=(const InferenceMode&) = delete;
InferenceMode& operator=(InferenceMode&&) = delete;
~InferenceMode() {
AutogradState::set_tls_state(prev_mode);
c10::impl::_force_tls_local_dispatch_key_set(prev_keyset);

View File

@ -81,9 +81,11 @@ template <typename T>
struct SafePyObjectT : private SafePyObject {
SafePyObjectT(PyObject* data, c10::impl::PyInterpreter* pyinterpreter)
: SafePyObject(data, pyinterpreter) {}
~SafePyObjectT() = default;
SafePyObjectT(SafePyObjectT&& other) noexcept : SafePyObject(other) {}
SafePyObjectT(SafePyObjectT const&) = delete;
SafePyObjectT& operator=(SafePyObjectT const&) = delete;
SafePyObjectT& operator=(SafePyObjectT&&) = delete;
using SafePyObject::ptr;
using SafePyObject::pyinterpreter;

View File

@ -27,6 +27,7 @@ namespace c10 {
struct StreamGuard {
/// No default constructor, see Note [Omitted default constructor from RAII]
explicit StreamGuard() = delete;
~StreamGuard() = default;
/// Set the current device to the device associated with the passed stream,
/// and set the current stream on that device to the passed stream.
@ -111,6 +112,7 @@ struct OptionalStreamGuard {
// See Note [Move assignment for RAII guards is tricky]
OptionalStreamGuard& operator=(OptionalStreamGuard&& other) = delete;
~OptionalStreamGuard() = default;
/// Resets the currently set stream to the original stream and
/// the currently set device to the original device. Then,
@ -162,6 +164,7 @@ struct MultiStreamGuard {
// See Note [Move assignment for RAII guards is tricky]
MultiStreamGuard& operator=(MultiStreamGuard&& other) = delete;
~MultiStreamGuard() = default;
private:
c10::impl::InlineMultiStreamGuard<impl::VirtualGuardImpl> guard_;

View File

@ -22,7 +22,9 @@ class C10_API SymbolicShapeMeta {
bool strides_valid_ = true; // e.g. for sparse where there are no strides
SymbolicShapeMeta() = default;
~SymbolicShapeMeta() = default;
SymbolicShapeMeta(const SymbolicShapeMeta& other);
SymbolicShapeMeta(SymbolicShapeMeta&& other) = delete;
SymbolicShapeMeta& operator=(const SymbolicShapeMeta& other) = delete;
SymbolicShapeMeta& operator=(SymbolicShapeMeta&& other) = delete;

View File

@ -133,6 +133,7 @@ struct C10_API PlacementDeleteContext {
DataPtr data_ptr_;
PlacementDtor placement_dtor_;
size_t size_;
PlacementDeleteContext(
DataPtr&& data_ptr,
PlacementDtor placement_dtor,
@ -140,6 +141,11 @@ struct C10_API PlacementDeleteContext {
: data_ptr_(std::move(data_ptr)),
placement_dtor_(placement_dtor),
size_(size) {}
PlacementDeleteContext(PlacementDeleteContext&&) noexcept = delete;
PlacementDeleteContext(const PlacementDeleteContext&) = delete;
PlacementDeleteContext& operator=(const PlacementDeleteContext&) = delete;
PlacementDeleteContext& operator=(PlacementDeleteContext&&) = delete;
static DataPtr makeDataPtr(
DataPtr&& data_ptr,
PlacementDtor placement_dtor,
@ -237,6 +243,7 @@ struct C10_API ExtraMeta {
std::optional<std::string> custom_storage_error_msg_ = std::nullopt;
ExtraMeta() = default;
~ExtraMeta() = default;
ExtraMeta(const ExtraMeta& other) {
if (other.symbolic_shape_meta_) {
symbolic_shape_meta_ =

View File

@ -62,7 +62,7 @@ class InlineDeviceGuard {
// DeviceGuard which reads the current device and promises to
// restore to that device on exit. However, most cases where you
// would have written this, you probably meant to actually just
// use OptionalDeviceGuard (since you don't actually need the
// use DeviceGuard (since you don't actually need the
// restore to happen if you don't ever actually set the device).
// We remove the constructor here to encourage you to think about
// what you actually want to happen.
@ -221,6 +221,7 @@ class InlineOptionalDeviceGuard {
explicit InlineOptionalDeviceGuard()
: guard_() // See Note [Explicit initialization of optional fields]
{}
~InlineOptionalDeviceGuard() = default;
/// Set the current device to the passed Device, if it is not nullopt.
explicit InlineOptionalDeviceGuard(std::optional<Device> device_opt)
@ -286,6 +287,7 @@ class InlineOptionalDeviceGuard {
// It's in principle possible to raise an error when this occurs
// by doing some extra thread-local bookkeeping. But why bother?
// Just don't provide the constructor.
InlineOptionalDeviceGuard(const InlineOptionalDeviceGuard<T>& other) = delete;
InlineOptionalDeviceGuard(InlineOptionalDeviceGuard<T>&& other) = delete;
// Note [Move assignment for RAII guards is tricky]
@ -335,6 +337,8 @@ class InlineOptionalDeviceGuard {
//
// We could solve this with an extra thread-local variable. But no one is
// actually using move-assignment. So just get rid of it.
InlineOptionalDeviceGuard& operator=(const InlineOptionalDeviceGuard& other) =
delete;
InlineOptionalDeviceGuard& operator=(InlineOptionalDeviceGuard&& other) =
delete;

View File

@ -135,6 +135,7 @@ class InlineOptionalStreamGuard {
explicit InlineOptionalStreamGuard()
: guard_() // See Note [Explicit initialization of optional fields]
{}
~InlineOptionalStreamGuard() = default;
/// Set the current device to the device associated with the passed stream,
/// and set the current stream on that device to the passed stream,
@ -151,6 +152,9 @@ class InlineOptionalStreamGuard {
explicit InlineOptionalStreamGuard(Args&&... args)
: guard_(std::in_place, std::forward<Args>(args)...) {}
InlineOptionalStreamGuard(const InlineOptionalStreamGuard<T>& other) = delete;
InlineOptionalStreamGuard& operator=(const InlineOptionalStreamGuard& other) =
delete;
// See Note [Move construction for RAII guards is tricky]
InlineOptionalStreamGuard(InlineOptionalStreamGuard<T>&& other) = delete;

View File

@ -132,6 +132,11 @@ struct C10_API ForceDispatchKeyGuard {
updated_set.excluded_ = exclude;
c10::impl::_force_tls_local_dispatch_key_set(updated_set);
}
ForceDispatchKeyGuard(ForceDispatchKeyGuard&&) noexcept = delete;
ForceDispatchKeyGuard(const ForceDispatchKeyGuard&) = delete;
ForceDispatchKeyGuard& operator=(const ForceDispatchKeyGuard&) = delete;
ForceDispatchKeyGuard& operator=(ForceDispatchKeyGuard&&) = delete;
~ForceDispatchKeyGuard() {
c10::impl::_force_tls_local_dispatch_key_set(saved_keyset_);
}

View File

@ -15,6 +15,7 @@ struct C10_API DisablePythonDispatcher {
DisablePythonDispatcher() : old_(PythonDispatcherTLS::get_state()) {
PythonDispatcherTLS::set_state({});
}
~DisablePythonDispatcher() {
PythonDispatcherTLS::set_state(old_);
}

View File

@ -147,6 +147,7 @@ struct CUDAStreamGuard {
/// stream, and set the current CUDA stream on that device to the passed
/// stream. Errors if the Stream is not a CUDA stream.
explicit CUDAStreamGuard(Stream stream) : guard_(stream) {}
~CUDAStreamGuard() = default;
/// Copy is disallowed
CUDAStreamGuard(const CUDAStreamGuard&) = delete;

View File

@ -139,6 +139,7 @@ int Constructable::numMoveAssignmentCalls;
struct NonCopyable {
NonCopyable() = default;
~NonCopyable() = default;
NonCopyable(NonCopyable&&) noexcept = default;
NonCopyable& operator=(NonCopyable&&) noexcept = default;
@ -883,9 +884,12 @@ TEST(SmallVectorCustomTest, NoAssignTest) {
struct MovedFrom {
bool hasValue{true};
MovedFrom() = default;
~MovedFrom() = default;
MovedFrom(const MovedFrom& m) = delete;
MovedFrom(MovedFrom&& m) noexcept : hasValue(m.hasValue) {
m.hasValue = false;
}
MovedFrom& operator=(const MovedFrom& m) = delete;
MovedFrom& operator=(MovedFrom&& m) noexcept {
hasValue = m.hasValue;
m.hasValue = false;
@ -917,6 +921,7 @@ struct EmplaceableArg {
EmplaceableArg(EmplaceableArg& X)
: State(X.State == EAS_Arg ? EAS_LValue : EAS_Failure) {}
~EmplaceableArg() = default;
explicit EmplaceableArg(bool) : State(EAS_Arg) {}
EmplaceableArg& operator=(EmplaceableArg&&) = delete;
@ -932,6 +937,7 @@ struct Emplaceable {
EmplaceableState State;
Emplaceable() : State(ES_Emplaced) {}
~Emplaceable() = default;
template <class A0Ty>
explicit Emplaceable(A0Ty&& A0)

View File

@ -26,6 +26,7 @@ namespace c10 {
* std::char_traits if we wanted to use it with our constexpr basic_string_view.
*/
template <class CharT>
// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions)
class basic_string_view final {
public:
using value_type = CharT;

View File

@ -328,6 +328,7 @@ class C10_API TypeMeta final {
* type, use TypeMeta::Make<T>().
*/
TypeMeta() noexcept;
~TypeMeta() = default;
/**
* Copy constructor.
@ -339,6 +340,7 @@ class C10_API TypeMeta final {
*/
TypeMeta& operator=(const TypeMeta& src) noexcept = default;
TypeMeta& operator=(TypeMeta&& src) noexcept = default;
TypeMeta(TypeMeta&& rhs) noexcept = default;
inline TypeMeta& operator=(ScalarType scalar_type) noexcept {

View File

@ -29,7 +29,9 @@ class TORCH_API SavedVariable {
const std::optional<Variable>& variable,
bool is_output,
bool is_inplace_on_view = false);
SavedVariable(const SavedVariable&) = delete;
SavedVariable(SavedVariable&&) = default;
SavedVariable& operator=(const SavedVariable&) = delete;
SavedVariable& operator=(SavedVariable&&) = default;
~SavedVariable() {
if (fw_grad_) {

View File

@ -11,6 +11,12 @@ struct StashTorchFunctionModeGuard {
~StashTorchFunctionModeGuard() {
at::impl::PythonTorchFunctionTLS::push_onto_stack(cur_mode_);
}
StashTorchFunctionModeGuard(const StashTorchFunctionModeGuard&) = delete;
StashTorchFunctionModeGuard(StashTorchFunctionModeGuard&&) = delete;
StashTorchFunctionModeGuard& operator=(const StashTorchFunctionModeGuard&) =
delete;
StashTorchFunctionModeGuard& operator=(StashTorchFunctionModeGuard&&) =
delete;
const std::shared_ptr<c10::SafePyObject>& get_cur_mode() {
return cur_mode_;

View File

@ -206,6 +206,9 @@ class TORCH_API CppFunction final {
~CppFunction();
CppFunction(const CppFunction&) = delete;
CppFunction& operator=(const CppFunction&) = delete;
CppFunction(CppFunction&&) noexcept = default;
CppFunction& operator=(CppFunction&&) = default;
@ -563,6 +566,7 @@ class TORCH_API Library final {
Library& operator=(const Library&) = delete;
Library(Library&&) = default;
Library& operator=(Library&&) = default;
~Library() = default;
// Some notes about the API design here. We had the following constraints:
//