diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 00000000..3ab59c7b --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,39 @@ +Checks: ' +-*, +bugprone-*, +-bugprone-easily-swappable-parameters, +-bugprone-exception-escape, +-bugprone-move-forwarding-reference, +-bugprone-narrowing-conversions, +-bugprone-reserved-identifier, +misc-*, +-misc-non-private-member-variables-in-classes, +-misc-no-recursion, +-misc-unconventional-assign-operator, +-misc-unused-parameters, +modernize-*, +-modernize-avoid-c-arrays, +-modernize-concat-nested-namespaces, +-modernize-deprecated-headers, +-modernize-use-nodiscard, +-modernize-use-trailing-return-type, +-modernize-use-using, +performance-*, +-performance-noexcept-move-constructor, +readability-*, +-readability-braces-around-statements, +-readability-convert-member-functions-to-static, +-readability-else-after-return, +-readability-function-cognitive-complexity, +-readability-identifier-length, +-readability-implicit-bool-conversion, +-readability-inconsistent-declaration-parameter-name, +-readability-magic-numbers, +-readability-named-parameter, +-readability-uppercase-literal-suffix, +-readability-use-anyofallof, +' +CheckOptions: + - key: modernize-use-override.IgnoreDestructors + value: true +HeaderFilterRegex: 'example/calculator.h|example/init.h|example/printer.h|include/mp/proxy-io.h|include/mp/proxy-types.h|include/mp/proxy.h|include/mp/util.h|test/mp/test/foo-types.h|test/mp/test/foo.h' diff --git a/CMakeLists.txt b/CMakeLists.txt index 717e38a5..04f68c8b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,15 @@ project("Libmultiprocess" CXX) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED YES) +option(Libmultiprocess_ENABLE_CLANG_TIDY "Run clang-tidy with the compiler." OFF) +if(Libmultiprocess_ENABLE_CLANG_TIDY) + find_program(CLANG_TIDY_EXECUTABLE NAMES clang-tidy) + if(NOT CLANG_TIDY_EXECUTABLE) + message(FATAL_ERROR "Libmultiprocess_ENABLE_CLANG_TIDY is ON but clang-tidy is not found.") + endif() + set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_EXECUTABLE}") +endif() + include(CMakePushCheckState) include(CheckCXXSourceCompiles) include(GNUInstallDirs) diff --git a/include/mp/proxy-io.h b/include/mp/proxy-io.h index 07e83a0f..1f3b4ccb 100644 --- a/include/mp/proxy-io.h +++ b/include/mp/proxy-io.h @@ -159,8 +159,8 @@ class EventLoop logger << "{" << LongThreadName(m_exe_name) << "} "; return logger; } - Logger logPlain() { return Logger(false, m_log_fn); } - Logger raise() { return Logger(true, m_log_fn); } + Logger logPlain() { return {false, m_log_fn}; } + Logger raise() { return {true, m_log_fn}; } //! Process name included in thread names so combined debug output from //! multiple processes is easier to understand. @@ -276,7 +276,7 @@ class Connection } Connection(EventLoop& loop, kj::Own&& stream_, - std::function<::capnp::Capability::Client(Connection&)> make_client) + const std::function<::capnp::Capability::Client(Connection&)>& make_client) : m_loop(loop), m_stream(kj::mv(stream_)), m_network(*m_stream, ::capnp::rpc::twoparty::Side::SERVER, ::capnp::ReaderOptions()), m_rpc_system(::capnp::makeRpcServer(m_network, make_client(*this))) diff --git a/include/mp/proxy-types.h b/include/mp/proxy-types.h index 902bd240..9f4b0345 100644 --- a/include/mp/proxy-types.h +++ b/include/mp/proxy-types.h @@ -461,7 +461,7 @@ decltype(auto) CustomReadField(TypeList, InvokeContext& invoke_context, Input&& input, ReadDest&& read_dest, - typename std::enable_if::value>::type* enable = 0) + typename std::enable_if::value>::type* enable = nullptr) { auto value = input.get(); if (value < std::numeric_limits::min() || value > std::numeric_limits::max()) { diff --git a/include/mp/util.h b/include/mp/util.h index ccbea6c9..9ccd3fac 100644 --- a/include/mp/util.h +++ b/include/mp/util.h @@ -337,7 +337,7 @@ struct AsyncCallable } AsyncCallable(const AsyncCallable&) = default; AsyncCallable(AsyncCallable&&) = default; - ~AsyncCallable() noexcept {} + ~AsyncCallable() noexcept = default; ResultOf operator()() const { return (m_callable->value)(); } mutable std::shared_ptr> m_callable; }; diff --git a/src/mp/gen.cpp b/src/mp/gen.cpp index 7c13ae00..a027ab33 100644 --- a/src/mp/gen.cpp +++ b/src/mp/gen.cpp @@ -92,7 +92,7 @@ struct Format m_os << value; return *this; } - operator std::string() { return m_os.str(); } + operator std::string() const { return m_os.str(); } std::ostringstream m_os; }; @@ -149,7 +149,7 @@ void Generate(kj::StringPtr src_prefix, } std::string include_base = include_path; - std::string::size_type p = include_base.rfind("."); + std::string::size_type p = include_base.rfind('.'); if (p != std::string::npos) include_base.erase(p); std::vector args; @@ -238,7 +238,7 @@ void Generate(kj::StringPtr src_prefix, GetAnnotationText(file_schema.getProto(), NAMESPACE_ANNOTATION_ID, &message_namespace); std::string base_name = include_base; - size_t output_slash = base_name.rfind("/"); + size_t output_slash = base_name.rfind('/'); if (output_slash != std::string::npos) { base_name.erase(0, output_slash + 1); } @@ -616,7 +616,7 @@ int main(int argc, char** argv) auto cwd = fs->getCurrentPath(); #endif for (size_t i = 4; i < argc; ++i) { - import_paths.push_back(argv[i]); + import_paths.emplace_back(argv[i]); } for (const char* path : {CMAKE_INSTALL_PREFIX "/include", capnp_PREFIX "/include"}) { #ifdef HAVE_KJ_FILESYSTEM diff --git a/src/mp/proxy.cpp b/src/mp/proxy.cpp index e1ae9b84..adf10e18 100644 --- a/src/mp/proxy.cpp +++ b/src/mp/proxy.cpp @@ -209,7 +209,7 @@ void EventLoop::removeClient(std::unique_lock& lock) m_cv.notify_all(); Unlock(lock, [&] { char buffer = 0; - KJ_SYSCALL(write(m_post_fd, &buffer, 1)); + KJ_SYSCALL(write(m_post_fd, &buffer, 1)); // NOLINT(bugprone-suspicious-semicolon) }); } } diff --git a/src/mp/util.cpp b/src/mp/util.cpp index 72043a03..408bbf8a 100644 --- a/src/mp/util.cpp +++ b/src/mp/util.cpp @@ -128,6 +128,7 @@ int SpawnProcess(int& pid, FdToArgsFn&& fd_to_args) void ExecProcess(const std::vector& args) { std::vector argv; + argv.reserve(args.size()); for (const auto& arg : args) { argv.push_back(const_cast(arg.c_str())); } diff --git a/test/mp/test/foo.h b/test/mp/test/foo.h index 769e9eee..b9f881d1 100644 --- a/test/mp/test/foo.h +++ b/test/mp/test/foo.h @@ -48,7 +48,7 @@ class FooImplementation void initThreadMap() {} int callback(FooCallback& callback, int arg) { return callback.call(arg); } int callbackUnique(std::unique_ptr callback, int arg) { return callback->call(arg); } - int callbackShared(std::shared_ptr callback, int arg) { return callback->call(arg); } + int callbackShared(std::shared_ptr callback, int arg) { return callback->call(arg); } // NOLINT(performance-unnecessary-value-param) void saveCallback(std::shared_ptr callback) { m_callback = std::move(callback); } int callbackSaved(int arg) { return m_callback->call(arg); } int callbackExtended(ExtendedCallback& callback, int arg) { return callback.callExtended(arg); }