diff --git a/.clang-tidy b/.clang-tidy index 3ab59c7..2d29f12 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -11,6 +11,7 @@ misc-*, -misc-no-recursion, -misc-unconventional-assign-operator, -misc-unused-parameters, +-misc-use-anonymous-namespace, modernize-*, -modernize-avoid-c-arrays, -modernize-concat-nested-namespaces, @@ -19,6 +20,7 @@ modernize-*, -modernize-use-trailing-return-type, -modernize-use-using, performance-*, +-performance-avoid-endl, -performance-noexcept-move-constructor, readability-*, -readability-braces-around-statements, diff --git a/CMakeLists.txt b/CMakeLists.txt index a01d08c..61e1be6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,6 +48,7 @@ configure_file(include/mp/config.h.in "${CMAKE_CURRENT_BINARY_DIR}/include/mp/co # Generated C++ Capn'Proto schema files capnp_generate_cpp(MP_PROXY_SRCS MP_PROXY_HDRS include/mp/proxy.capnp) +set_source_files_properties(${MP_PROXY_SRCS} PROPERTIES SKIP_LINTING ON) # util library add_library(mputil OBJECT src/mp/util.cpp) diff --git a/cmake/TargetCapnpSources.cmake b/cmake/TargetCapnpSources.cmake index cdc86c6..f614bb1 100644 --- a/cmake/TargetCapnpSources.cmake +++ b/cmake/TargetCapnpSources.cmake @@ -66,18 +66,20 @@ function(target_capnp_sources target include_prefix) set(generated_headers "") foreach(capnp_file IN LISTS TCS_UNPARSED_ARGUMENTS) - add_custom_command( - OUTPUT ${capnp_file}.c++ ${capnp_file}.h ${capnp_file}.proxy-client.c++ ${capnp_file}.proxy-types.h ${capnp_file}.proxy-server.c++ ${capnp_file}.proxy-types.c++ ${capnp_file}.proxy.h - COMMAND Libmultiprocess::mpgen ${CMAKE_CURRENT_SOURCE_DIR} ${include_prefix} ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_file} ${TCS_IMPORT_PATHS} ${MP_INCLUDE_DIR} - DEPENDS ${capnp_file} - VERBATIM - ) - target_sources(${target} PRIVATE + set(generated_sources ${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.c++ ${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.proxy-client.c++ ${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.proxy-server.c++ ${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.proxy-types.c++ ) + add_custom_command( + OUTPUT ${generated_sources} ${capnp_file}.h ${capnp_file}.proxy-types.h ${capnp_file}.proxy.h + COMMAND Libmultiprocess::mpgen ${CMAKE_CURRENT_SOURCE_DIR} ${include_prefix} ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_file} ${TCS_IMPORT_PATHS} ${MP_INCLUDE_DIR} + DEPENDS ${capnp_file} + VERBATIM + ) + target_sources(${target} PRIVATE ${generated_sources}) + set_source_files_properties(${generated_sources} PROPERTIES SKIP_LINTING ON) list(APPEND generated_headers ${capnp_file}.h) endforeach() diff --git a/example/calculator.cpp b/example/calculator.cpp index 65f6476..4290d68 100644 --- a/example/calculator.cpp +++ b/example/calculator.cpp @@ -5,13 +5,15 @@ #include #include #include -#include +#include // NOLINT(misc-include-cleaner) #include #include #include #include #include #include +#include +#include class CalculatorImpl : public Calculator { @@ -30,7 +32,7 @@ class InitImpl : public Init } }; -void LogPrint(bool raise, const std::string& message) +static void LogPrint(bool raise, const std::string& message) { if (raise) throw std::runtime_error(message); std::ofstream("debug.log", std::ios_base::app) << message << std::endl; @@ -43,7 +45,7 @@ int main(int argc, char** argv) return 1; } mp::EventLoop loop("mpcalculator", LogPrint); - int fd = std::stoi(argv[1]); + const int fd = std::stoi(argv[1]); std::unique_ptr init = std::make_unique(); mp::ServeStream(loop, fd, *init); loop.loop(); diff --git a/example/example.cpp b/example/example.cpp index 036dcb5..a4f84c5 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -4,17 +4,24 @@ #include #include +#include #include -#include +#include #include #include +#include +#include +#include +#include +#include +#include namespace fs = std::filesystem; -auto Spawn(mp::EventLoop& loop, const std::string& process_argv0, const std::string& new_exe_name) +static auto Spawn(mp::EventLoop& loop, const std::string& process_argv0, const std::string& new_exe_name) { int pid; - int fd = mp::SpawnProcess(pid, [&](int fd) -> std::vector { + const int fd = mp::SpawnProcess(pid, [&](int fd) -> std::vector { fs::path path = process_argv0; path.remove_filename(); path.append(new_exe_name); @@ -23,7 +30,7 @@ auto Spawn(mp::EventLoop& loop, const std::string& process_argv0, const std::str return std::make_tuple(mp::ConnectStream(loop, fd), pid); } -void LogPrint(bool raise, const std::string& message) +static void LogPrint(bool raise, const std::string& message) { if (raise) throw std::runtime_error(message); std::ofstream("debug.log", std::ios_base::app) << message << std::endl; diff --git a/example/printer.cpp b/example/printer.cpp index 0a51cdd..ccaed68 100644 --- a/example/printer.cpp +++ b/example/printer.cpp @@ -4,13 +4,14 @@ #include #include -#include +#include // NOLINT(misc-include-cleaner) #include #include #include #include #include #include +#include class PrinterImpl : public Printer { @@ -24,7 +25,7 @@ class InitImpl : public Init std::unique_ptr makePrinter() override { return std::make_unique(); } }; -void LogPrint(bool raise, const std::string& message) +static void LogPrint(bool raise, const std::string& message) { if (raise) throw std::runtime_error(message); std::ofstream("debug.log", std::ios_base::app) << message << std::endl; @@ -37,7 +38,7 @@ int main(int argc, char** argv) return 1; } mp::EventLoop loop("mpprinter", LogPrint); - int fd = std::stoi(argv[1]); + const int fd = std::stoi(argv[1]); std::unique_ptr init = std::make_unique(); mp::ServeStream(loop, fd, *init); loop.loop(); diff --git a/include/mp/proxy-io.h b/include/mp/proxy-io.h index cc459d3..4430a42 100644 --- a/include/mp/proxy-io.h +++ b/include/mp/proxy-io.h @@ -63,7 +63,7 @@ struct ProxyClient : public ProxyClientBase ProxyClient(const ProxyClient&) = delete; ~ProxyClient(); - void setCleanup(std::function fn); + void setCleanup(const std::function& fn); //! Cleanup function to run when the connection is closed. If the Connection //! gets destroyed before this ProxyClient object, this cleanup @@ -247,7 +247,7 @@ struct Waiter template void post(Fn&& fn) { - std::unique_lock lock(m_mutex); + std::unique_lock lock(m_mutex); // NOLINT(misc-const-correctness) assert(!m_fn); m_fn = std::move(fn); m_cv.notify_all(); @@ -269,7 +269,7 @@ struct Waiter fn(); lock.lock(); } - bool done = pred(); + const bool done = pred(); return done; }); } @@ -488,7 +488,7 @@ ProxyServerBase::~ProxyServerBase() CleanupRun(fns); }); } - assert(m_context.cleanup_fns.size() == 0); + assert(m_context.cleanup_fns.empty()); std::unique_lock lock(m_context.connection->m_loop.m_mutex); m_context.connection->m_loop.removeClient(lock); } @@ -523,7 +523,7 @@ using ConnThread = ConnThreads::iterator; // Retrieve ProxyClient object associated with this connection from a // map, or create a new one and insert it into the map. Return map iterator and // inserted bool. -std::tuple SetThread(ConnThreads& threads, std::mutex& mutex, Connection* connection, std::function make_thread); +std::tuple SetThread(ConnThreads& threads, std::mutex& mutex, Connection* connection, const std::function& make_thread); struct ThreadContext { diff --git a/include/mp/proxy-types.h b/include/mp/proxy-types.h index 6c1b1aa..e6465b7 100644 --- a/include/mp/proxy-types.h +++ b/include/mp/proxy-types.h @@ -39,17 +39,17 @@ struct StructField // clang-format off template auto get() const -> decltype(A::get(this->m_struct)) { return A::get(this->m_struct); } - template auto has() const -> typename std::enable_if::type { return A::getHas(m_struct); } - template auto has() const -> typename std::enable_if::type { return A::has(m_struct); } - template auto has() const -> typename std::enable_if::type { return true; } - template auto want() const -> typename std::enable_if::type { return A::getWant(m_struct); } - template auto want() const -> typename std::enable_if::type { return true; } + template auto has() const -> std::enable_if_t { return A::getHas(m_struct); } + template auto has() const -> std::enable_if_t { return A::has(m_struct); } + template auto has() const -> std::enable_if_t { return true; } + template auto want() const -> std::enable_if_t { return A::getWant(m_struct); } + template auto want() const -> std::enable_if_t { return true; } template decltype(auto) set(Args&&... args) const { return A::set(this->m_struct, std::forward(args)...); } template decltype(auto) init(Args&&... args) const { return A::init(this->m_struct, std::forward(args)...); } - template auto setHas() const -> typename std::enable_if::type { return A::setHas(m_struct); } - template auto setHas() const -> typename std::enable_if::type { } - template auto setWant() const -> typename std::enable_if::type { return A::setWant(m_struct); } - template auto setWant() const -> typename std::enable_if::type { } + template auto setHas() const -> std::enable_if_t { return A::setHas(m_struct); } + template auto setHas() const -> std::enable_if_t { } + template auto setWant() const -> std::enable_if_t { return A::setWant(m_struct); } + template auto setWant() const -> std::enable_if_t { } // clang-format on }; @@ -314,6 +314,9 @@ struct IterateFieldsHelper { static_cast(this)->handleField(std::forward(arg1), std::forward(arg2), ParamList()); } +private: + IterateFieldsHelper() = default; + friend Derived; }; struct IterateFields : IterateFieldsHelper @@ -372,14 +375,14 @@ struct ClientParam // position when unpacking tuple might be slower than pattern matching // approach in the stack overflow solution template - auto callBuild(Args&&... args) -> typename std::enable_if<(I < sizeof...(Types))>::type + auto callBuild(Args&&... args) -> std::enable_if_t<(I < sizeof...(Types))> { callBuild(std::forward(args)..., std::get(m_client_param->m_values)); } template auto callBuild(ClientInvokeContext& invoke_context, Params& params, ParamList, Values&&... values) -> - typename std::enable_if<(I == sizeof...(Types))>::type + std::enable_if_t<(I == sizeof...(Types))> { MaybeBuildField(std::integral_constant(), ParamList(), invoke_context, Make(params), std::forward(values)...); @@ -400,14 +403,14 @@ struct ClientParam } template - auto callRead(Args&&... args) -> typename std::enable_if<(I < sizeof...(Types))>::type + auto callRead(Args&&... args) -> std::enable_if_t<(I < sizeof...(Types))> { callRead(std::forward(args)..., std::get(m_client_param->m_values)); } template auto callRead(ClientInvokeContext& invoke_context, Results& results, TypeList, Values&&... values) - -> typename std::enable_if::type + -> std::enable_if_t { MaybeReadField(std::integral_constant(), TypeList...>(), invoke_context, Make(results), ReadDestUpdate(values)...); @@ -623,7 +626,7 @@ void clientInvoke(ProxyClient& proxy_client, const GetRequest& get_request, Fiel } catch (...) { exception = std::current_exception(); } - std::unique_lock lock(invoke_context.thread_context.waiter->m_mutex); + std::unique_lock lock(invoke_context.thread_context.waiter->m_mutex); // NOLINT(misc-const-correctness) done = true; invoke_context.thread_context.waiter->m_cv.notify_all(); }, @@ -631,7 +634,7 @@ void clientInvoke(ProxyClient& proxy_client, const GetRequest& get_request, Fiel kj_exception = kj::str("kj::Exception: ", e).cStr(); proxy_client.m_context.connection->m_loop.logPlain() << "{" << invoke_context.thread_context.thread_name << "} IPC client exception " << kj_exception; - std::unique_lock lock(invoke_context.thread_context.waiter->m_mutex); + std::unique_lock lock(invoke_context.thread_context.waiter->m_mutex); // NOLINT(misc-const-correctness) done = true; invoke_context.thread_context.waiter->m_cv.notify_all(); })); @@ -648,7 +651,7 @@ void clientInvoke(ProxyClient& proxy_client, const GetRequest& get_request, Fiel //! duplication and branching in generic code that forwards calls to functions. template auto ReplaceVoid(Fn&& fn, Ret&& ret) -> - typename std::enable_if::value, decltype(ret())>::type + std::enable_if_t, decltype(ret())> { fn(); return ret(); @@ -657,7 +660,7 @@ auto ReplaceVoid(Fn&& fn, Ret&& ret) -> //! Overload of above for non-void `fn()` case. template auto ReplaceVoid(Fn&& fn, Ret&& ret) -> - typename std::enable_if::value, decltype(fn())>::type + std::enable_if_t, decltype(fn())> { return fn(); } diff --git a/include/mp/proxy.h b/include/mp/proxy.h index 92c0fa5..76be099 100644 --- a/include/mp/proxy.h +++ b/include/mp/proxy.h @@ -198,7 +198,7 @@ struct FunctionTraits<_Result (_Class::*const)(_Params...)> template using Param = typename std::tuple_element>::type; using Fields = - typename std::conditional::value, Params, TypeList<_Params..., _Result>>::type; + std::conditional_t, Params, TypeList<_Params..., _Result>>; }; //! Traits class for a proxy method, providing the same diff --git a/include/mp/util.h b/include/mp/util.h index 032fc0f..0569c44 100644 --- a/include/mp/util.h +++ b/include/mp/util.h @@ -43,9 +43,9 @@ struct TypeList //! Example: //! Make(5, true) // Constructs std::pair(5, true); template