Skip to content

Commit 27c7e8e

Browse files
committed
Merge bitcoin-core#172: refactor: fix warnings from clang-tidy-20 and bitcoin-tidy
57a65b8 clang-tidy: Suppress bitcoin-nontrivial-threadlocal error (Ryan Ofsky) 3a96cdc clang-tidy: Fix bugprone-move-forwarding-reference error (Ryan Ofsky) c1e8c1a clang-tidy: Fix bugprone-move-forwarding-reference errors (Ryan Ofsky) Pull request description: Warnings are described in commit messages and were reported by Sjors in bitcoin/bitcoin#31802 (comment) ACKs for top commit: Sjors: ACK 57a65b8 TheCharlatan: ACK 57a65b8 Tree-SHA512: 3018126aa174f3c1d11abe5a5601c5c3fdee49b4a6ca4c19abd766eea88ba765d5a0f942dd28db5db9332256fb3162c9638615004e1348d0d2687d9b54dab2cc
2 parents 2fe87d0 + 57a65b8 commit 27c7e8e

File tree

6 files changed

+42
-14
lines changed

6 files changed

+42
-14
lines changed

include/mp/proxy-io.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ struct Waiter
249249
{
250250
const std::unique_lock<std::mutex> lock(m_mutex);
251251
assert(!m_fn);
252-
m_fn = std::move(fn);
252+
m_fn = std::forward<Fn>(fn);
253253
m_cv.notify_all();
254254
}
255255

@@ -333,7 +333,7 @@ class Connection
333333
// to the EventLoop TaskSet to avoid "Promise callback destroyed itself"
334334
// error in cases where f deletes this Connection object.
335335
m_on_disconnect.add(m_network.onDisconnect().then(
336-
[f = std::move(f), this]() mutable { m_loop.m_task_set->add(kj::evalLater(kj::mv(f))); }));
336+
[f = std::forward<F>(f), this]() mutable { m_loop.m_task_set->add(kj::evalLater(kj::mv(f))); }));
337337
}
338338

339339
EventLoop& m_loop;
@@ -634,7 +634,10 @@ void ListenConnections(EventLoop& loop, int fd, InitImpl& init)
634634
});
635635
}
636636

637-
extern thread_local ThreadContext g_thread_context;
637+
extern thread_local ThreadContext g_thread_context; // NOLINT(bitcoin-nontrivial-threadlocal)
638+
// Silence nonstandard bitcoin tidy error "Variable with non-trivial destructor
639+
// cannot be thread_local" which should not be a problem on modern platforms, and
640+
// could lead to a small memory leak at worst on older ones.
638641

639642
} // namespace mp
640643

include/mp/proxy-types.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ struct ClientException
385385
template <typename Accessor, typename... Types>
386386
struct ClientParam
387387
{
388-
ClientParam(Types&&... values) : m_values(values...) {}
388+
ClientParam(Types&&... values) : m_values{std::forward<Types>(values)...} {}
389389

390390
struct BuildParams : IterateFieldsHelper<BuildParams, sizeof...(Types)>
391391
{
@@ -399,7 +399,14 @@ struct ClientParam
399399
ParamList(), Priority<1>(), std::forward<Values>(values)..., Make<StructField, Accessor>(params));
400400
};
401401

402-
std::apply(fun, m_client_param->m_values);
402+
// Note: The m_values tuple just consists of lvalue and rvalue
403+
// references, so calling std::move doesn't change the tuple, it
404+
// just causes std::apply to call the std::get overload that returns
405+
// && instead of &, so rvalue references are preserved and not
406+
// turned into lvalue references. This allows the BuildField call to
407+
// move from the argument if it is an rvalue reference or was passed
408+
// by value.
409+
std::apply(fun, std::move(m_client_param->m_values));
403410
}
404411

405412
BuildParams(ClientParam* client_param) : m_client_param(client_param) {}
@@ -577,7 +584,7 @@ void serverDestroy(Server& server)
577584
//!
578585
//! ProxyClient<ClassName>::M0::Result ProxyClient<ClassName>::methodName(M0::Param<0> arg0, M0::Param<1> arg1) {
579586
//! typename M0::Result result;
580-
//! clientInvoke(*this, &InterfaceName::Client::methodNameRequest, MakeClientParam<...>(arg0), MakeClientParam<...>(arg1), MakeClientParam<...>(result));
587+
//! clientInvoke(*this, &InterfaceName::Client::methodNameRequest, MakeClientParam<...>(M0::Fwd<0>(arg0)), MakeClientParam<...>(M0::Fwd<1>(arg1)), MakeClientParam<...>(result));
581588
//! return result;
582589
//! }
583590
//!

include/mp/proxy.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ struct ProxyServerCustom : public ProxyServerBase<Interface, Impl>
181181
//!
182182
//! Params - TypeList of C++ ClassName::methodName parameter types
183183
//! Result - Return type of ClassName::method
184-
//! Param<N> - helper to access individual parameters by index number.
184+
//! Param<N> - helper to access individual parameter types by index number.
185+
//! Fwd<N> - helper to forward arguments by index number.
185186
//! Fields - helper alias that appends Result type to the Params typelist if
186187
//! it not void.
187188
template <class Fn>
@@ -199,6 +200,16 @@ struct FunctionTraits<_Result (_Class::*const)(_Params...)>
199200
using Param = typename std::tuple_element<N, std::tuple<_Params...>>::type;
200201
using Fields =
201202
std::conditional_t<std::is_same_v<void, Result>, Params, TypeList<_Params..., _Result>>;
203+
204+
//! Enable perfect forwarding for clientInvoke calls. If parameter is a
205+
//! value type or rvalue reference type, pass it as an rvalue-reference to
206+
//! MakeClientParam and BuildField calls so it can be moved from, and if it
207+
//! is an lvalue reference, pass it an lvalue reference so it won't be moved
208+
//! from. This method does the same thing as std::forward except it takes a
209+
//! parameter number instead of a type as a template argument, so generated
210+
//! code calling this can be less repetitive and verbose.
211+
template <size_t N>
212+
static decltype(auto) Fwd(Param<N>& arg) { return static_cast<Param<N>&&>(arg); }
202213
};
203214

204215
//! Traits class for a proxy method, providing the same

include/mp/type-interface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void CustomBuildField(TypeList<std::shared_ptr<Impl>>,
4444
{
4545
if (value) {
4646
using Interface = typename decltype(output.get())::Calls;
47-
output.set(CustomMakeProxyServer<Interface, Impl>(invoke_context, std::move(value)));
47+
output.set(CustomMakeProxyServer<Interface, Impl>(invoke_context, std::forward<Value>(value)));
4848
}
4949
}
5050

include/mp/util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ struct AsyncCallable
183183
template <typename Callable>
184184
AsyncCallable<std::remove_reference_t<Callable>> MakeAsyncCallable(Callable&& callable)
185185
{
186-
return std::move(callable);
186+
return std::forward<Callable>(callable);
187187
}
188188

189189
//! Format current thread name as "{exe_name}-{$pid}/{thread_name}-{$tid}".

src/mp/gen.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -512,10 +512,20 @@ static void Generate(kj::StringPtr src_prefix,
512512

513513
add_accessor(field_name);
514514

515+
std::ostringstream fwd_args;
515516
for (int i = 0; i < field.args; ++i) {
516517
if (argc > 0) client_args << ",";
518+
519+
// Add to client method parameter list.
517520
client_args << "M" << method_ordinal << "::Param<" << argc << "> " << field_name;
518521
if (field.args > 1) client_args << i;
522+
523+
// Add to MakeClientParam argument list using Fwd helper for perfect forwarding.
524+
if (i > 0) fwd_args << ", ";
525+
fwd_args << "M" << method_ordinal << "::Fwd<" << argc << ">(" << field_name;
526+
if (field.args > 1) fwd_args << i;
527+
fwd_args << ")";
528+
519529
++argc;
520530
}
521531
client_invoke << ", ";
@@ -529,13 +539,10 @@ static void Generate(kj::StringPtr src_prefix,
529539
client_invoke << "Accessor<" << base_name << "_fields::" << Cap(field_name) << ", "
530540
<< field_flags.str() << ">>(";
531541

532-
if (field.retval || field.args == 1) {
542+
if (field.retval) {
533543
client_invoke << field_name;
534544
} else {
535-
for (int i = 0; i < field.args; ++i) {
536-
if (i > 0) client_invoke << ", ";
537-
client_invoke << field_name << i;
538-
}
545+
client_invoke << fwd_args.str();
539546
}
540547
client_invoke << ")";
541548

0 commit comments

Comments
 (0)