From 2f17a21b2bad078d780ea70d211f04dc73128dd4 Mon Sep 17 00:00:00 2001 From: cyy Date: Wed, 10 Jan 2024 23:58:04 +0000 Subject: [PATCH] [Reland] [13/N] Enable clang-tidy on headers of torch/csrc (#117088) Reland of #116560 and fixes the issued reported by #116695 Pull Request resolved: https://github.com/pytorch/pytorch/pull/117088 Approved by: https://github.com/albanD --- .lintrunner.toml | 2 ++ torch/csrc/CudaIPCTypes.h | 9 ++++----- torch/csrc/Exceptions.h | 9 +++------ torch/csrc/autograd/engine.cpp | 4 ++-- torch/csrc/autograd/variable.cpp | 3 --- torch/csrc/autograd/variable.h | 1 - torch/csrc/copy_utils.h | 1 + torch/csrc/distributed/rpc/request_callback_impl.cpp | 1 - torch/csrc/distributed/rpc/script_remote_call.cpp | 1 - torch/csrc/jit/resource_guard.h | 4 ++-- torch/csrc/serialization.h | 2 ++ torch/csrc/utils.h | 8 ++------ 12 files changed, 18 insertions(+), 27 deletions(-) diff --git a/.lintrunner.toml b/.lintrunner.toml index c1f4cb6b5b9..4a581567e79 100644 --- a/.lintrunner.toml +++ b/.lintrunner.toml @@ -246,6 +246,8 @@ include_patterns = [ 'c10/**/*.cpp', 'c10/core/**/*.h', 'c10/util/**/*.h', + # Enable coverage of headers in torch/csrc and excluding sub-directories for now. + 'torch/csrc/*.h', 'torch/csrc/**/*.cpp', ] exclude_patterns = [ diff --git a/torch/csrc/CudaIPCTypes.h b/torch/csrc/CudaIPCTypes.h index 58327bae896..f4eb3afee1e 100644 --- a/torch/csrc/CudaIPCTypes.h +++ b/torch/csrc/CudaIPCTypes.h @@ -79,9 +79,8 @@ struct CudaIPCRefCountersFile final { std::string handle, uint64_t size, at::DataPtr data_ptr) - : next_offset_(0), - size_(size), - used_slots_(0), + : size_(size), + handle_(std::move(handle)), refcounted_shared_mem_(std::move(data_ptr)) {} @@ -119,9 +118,9 @@ struct CudaIPCRefCountersFile final { } private: - uint64_t next_offset_; + uint64_t next_offset_{0}; uint64_t size_; - uint64_t used_slots_; + uint64_t used_slots_{0}; std::string handle_; at::DataPtr refcounted_shared_mem_; }; diff --git a/torch/csrc/Exceptions.h b/torch/csrc/Exceptions.h index ccce3d255d3..95a8db4d4d6 100644 --- a/torch/csrc/Exceptions.h +++ b/torch/csrc/Exceptions.h @@ -2,8 +2,6 @@ #include #include -#include -#include #include #include @@ -145,7 +143,7 @@ extern PyObject *THPException_FatalError, *THPException_LinAlgError, // Throwing this exception means that the python error flags have been already // set and control should be immediately returned to the interpreter. struct python_error : public std::exception { - python_error() {} + python_error() = default; python_error(const python_error& other) : type(other.type), @@ -373,15 +371,14 @@ using Arg = typename invoke_traits::template arg::type; template auto wrap_pybind_function_impl_( + // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward) Func&& f, std::index_sequence, bool release_gil) { - using result_type = typename invoke_traits::result_type; namespace py = pybind11; // f=f is needed to handle function references on older compilers - return [f = std::forward(f), - release_gil](Arg... args) -> result_type { + return [f = std::forward(f), release_gil](Arg... args) { HANDLE_TH_ERRORS if (release_gil) { py::gil_scoped_release no_gil; diff --git a/torch/csrc/autograd/engine.cpp b/torch/csrc/autograd/engine.cpp index aaf3709e638..ffda2a3dbd4 100644 --- a/torch/csrc/autograd/engine.cpp +++ b/torch/csrc/autograd/engine.cpp @@ -80,7 +80,7 @@ std::atomic the_compiled_autograd = nullptr; reinterpret_cast(1) std::atomic num_threads_in_backwards; struct CompiledAutogradThreadingDebugCheck { - CompiledAutogradThreadingDebugCheck() : incremented(true) { + CompiledAutogradThreadingDebugCheck() { num_threads_in_backwards++; } ~CompiledAutogradThreadingDebugCheck() { @@ -93,7 +93,7 @@ struct CompiledAutogradThreadingDebugCheck { } private: - bool incremented; + bool incremented{true}; }; } // namespace diff --git a/torch/csrc/autograd/variable.cpp b/torch/csrc/autograd/variable.cpp index 86a37a79b4f..81c0f192899 100644 --- a/torch/csrc/autograd/variable.cpp +++ b/torch/csrc/autograd/variable.cpp @@ -15,13 +15,10 @@ #include #include -#include -#include #include #include #include #include -#include #include #include diff --git a/torch/csrc/autograd/variable.h b/torch/csrc/autograd/variable.h index 4b42b1dc809..2ff5b149aeb 100644 --- a/torch/csrc/autograd/variable.h +++ b/torch/csrc/autograd/variable.h @@ -16,7 +16,6 @@ #include #include #include -#include #include #include #include diff --git a/torch/csrc/copy_utils.h b/torch/csrc/copy_utils.h index 308706dec82..4d7b689016b 100644 --- a/torch/csrc/copy_utils.h +++ b/torch/csrc/copy_utils.h @@ -2,6 +2,7 @@ #include #include +#include #include #include diff --git a/torch/csrc/distributed/rpc/request_callback_impl.cpp b/torch/csrc/distributed/rpc/request_callback_impl.cpp index 80c9944e2bb..5ceb0d23f9b 100644 --- a/torch/csrc/distributed/rpc/request_callback_impl.cpp +++ b/torch/csrc/distributed/rpc/request_callback_impl.cpp @@ -1,6 +1,5 @@ #include -#include #include #include #include diff --git a/torch/csrc/distributed/rpc/script_remote_call.cpp b/torch/csrc/distributed/rpc/script_remote_call.cpp index 68da06d342e..c46234379ba 100644 --- a/torch/csrc/distributed/rpc/script_remote_call.cpp +++ b/torch/csrc/distributed/rpc/script_remote_call.cpp @@ -1,7 +1,6 @@ #include #include -#include #include namespace torch { diff --git a/torch/csrc/jit/resource_guard.h b/torch/csrc/jit/resource_guard.h index 48c689b6cbf..6c2a2fa64b4 100644 --- a/torch/csrc/jit/resource_guard.h +++ b/torch/csrc/jit/resource_guard.h @@ -6,11 +6,11 @@ namespace jit { class ResourceGuard { std::function _destructor; - bool _released; + bool _released{false}; public: ResourceGuard(std::function destructor) - : _destructor(std::move(destructor)), _released(false) {} + : _destructor(std::move(destructor)) {} // NOLINTNEXTLINE(bugprone-exception-escape) ~ResourceGuard() { diff --git a/torch/csrc/serialization.h b/torch/csrc/serialization.h index e2587babf05..3e10784c245 100644 --- a/torch/csrc/serialization.h +++ b/torch/csrc/serialization.h @@ -1,6 +1,8 @@ #ifndef THP_SERIALIZATION_INC #define THP_SERIALIZATION_INC +#include +#include template void doRead(io fildes, void* buf, size_t nbytes); diff --git a/torch/csrc/utils.h b/torch/csrc/utils.h index e9275fbd333..5a610c28d2b 100644 --- a/torch/csrc/utils.h +++ b/torch/csrc/utils.h @@ -184,18 +184,14 @@ template struct mod_traits {}; template -struct mod_traits< - _real, - typename std::enable_if::value>::type> { +struct mod_traits<_real, std::enable_if_t>> { static _real mod(_real a, _real b) { return fmod(a, b); } }; template -struct mod_traits< - _real, - typename std::enable_if::value>::type> { +struct mod_traits<_real, std::enable_if_t>> { static _real mod(_real a, _real b) { return a % b; }