Skip to content

Commit 9558ceb

Browse files
committed
Merge #152: refactor: Fix compiler and clang-tidy warnings
0b8b7f9 refactor: Fix `-Wsign-compare` compiler warning (Hennadii Stepanov) a02c079 clang-tidy: Suppress `performance-enum-size` check warning (Hennadii Stepanov) 593807a clang-tidy: Fix `readability-container-size-empty` check (Hennadii Stepanov) 68c1c6c clang-tidy: Fix `readability-avoid-return-with-void-value` check (Hennadii Stepanov) 4abaa98 clang-tidy: Fix `readability-avoid-nested-conditional-operator` check (Hennadii Stepanov) 01ef094 clang-tidy: Fix `performance-unnecessary-value-param` check (Hennadii Stepanov) c665a43 clang-tidy: Fix `misc-use-internal-linkage` check (Hennadii Stepanov) 15c77e9 clang-tidy: Fix `misc-include-cleaner` check (Hennadii Stepanov) 848c902 clang-tidy: Fix `misc-const-correctness` check (Hennadii Stepanov) 4a2508c clang-tidy: Fix `modernize-type-traits` check (Hennadii Stepanov) 8170d3d clang-tidy: Suppress `bugprone-empty-catch` check warning (Hennadii Stepanov) c068596 clang-tidy: Fix `bugprone-crtp-constructor-accessibility` check (Hennadii Stepanov) 00036c0 clang-tidy: Disable `performance-avoid-endl` check (Hennadii Stepanov) 359a615 clang-tidy: Disable `misc-use-anonymous-namespace` check (Hennadii Stepanov) Pull request description: Most of these changes are not my own. This is combination of commits from #144 and #151 which fix a variety of compiler warnings. Several of the fixes are just suppressions, so followups can be done to improve code and fix problems more completely. More details can be found in #144. Top commit has no ACKs. Tree-SHA512: 581720d357dce36553f91fb4bf92c4ade2c228ce199b342c28637f2615b22aa6e5908c83a698320438e5d53309581ec64410f469cbb782843970caee3c21d62e
2 parents c6a61d8 + 0b8b7f9 commit 9558ceb

File tree

13 files changed

+132
-87
lines changed

13 files changed

+132
-87
lines changed

.clang-tidy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ misc-*,
1111
-misc-no-recursion,
1212
-misc-unconventional-assign-operator,
1313
-misc-unused-parameters,
14+
-misc-use-anonymous-namespace,
1415
modernize-*,
1516
-modernize-avoid-c-arrays,
1617
-modernize-concat-nested-namespaces,
@@ -19,6 +20,7 @@ modernize-*,
1920
-modernize-use-trailing-return-type,
2021
-modernize-use-using,
2122
performance-*,
23+
-performance-avoid-endl,
2224
-performance-noexcept-move-constructor,
2325
readability-*,
2426
-readability-braces-around-statements,

example/calculator.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
#include <calculator.h>
66
#include <fstream>
77
#include <init.capnp.h>
8-
#include <init.capnp.proxy-types.h>
8+
#include <init.capnp.proxy.h> // NOLINT(misc-include-cleaner)
99
#include <init.h>
1010
#include <iostream>
1111
#include <memory>
1212
#include <mp/proxy-io.h>
1313
#include <printer.h>
1414
#include <stdexcept>
15+
#include <string>
16+
#include <utility>
1517

1618
class CalculatorImpl : public Calculator
1719
{
@@ -30,7 +32,7 @@ class InitImpl : public Init
3032
}
3133
};
3234

33-
void LogPrint(bool raise, const std::string& message)
35+
static void LogPrint(bool raise, const std::string& message)
3436
{
3537
if (raise) throw std::runtime_error(message);
3638
std::ofstream("debug.log", std::ios_base::app) << message << std::endl;
@@ -43,7 +45,7 @@ int main(int argc, char** argv)
4345
return 1;
4446
}
4547
mp::EventLoop loop("mpcalculator", LogPrint);
46-
int fd = std::stoi(argv[1]);
48+
const int fd = std::stoi(argv[1]);
4749
std::unique_ptr<Init> init = std::make_unique<InitImpl>();
4850
mp::ServeStream<InitInterface>(loop, fd, *init);
4951
loop.loop();

example/example.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,24 @@
44

55
#include <filesystem>
66
#include <fstream>
7+
#include <future>
78
#include <init.capnp.h>
8-
#include <init.capnp.proxy-types.h>
9+
#include <init.capnp.proxy.h>
910
#include <iostream>
1011
#include <mp/proxy-io.h>
12+
#include <mp/util.h>
13+
#include <stdexcept>
14+
#include <string>
15+
#include <thread>
16+
#include <tuple>
17+
#include <vector>
1118

1219
namespace fs = std::filesystem;
1320

14-
auto Spawn(mp::EventLoop& loop, const std::string& process_argv0, const std::string& new_exe_name)
21+
static auto Spawn(mp::EventLoop& loop, const std::string& process_argv0, const std::string& new_exe_name)
1522
{
1623
int pid;
17-
int fd = mp::SpawnProcess(pid, [&](int fd) -> std::vector<std::string> {
24+
const int fd = mp::SpawnProcess(pid, [&](int fd) -> std::vector<std::string> {
1825
fs::path path = process_argv0;
1926
path.remove_filename();
2027
path.append(new_exe_name);
@@ -23,7 +30,7 @@ auto Spawn(mp::EventLoop& loop, const std::string& process_argv0, const std::str
2330
return std::make_tuple(mp::ConnectStream<InitInterface>(loop, fd), pid);
2431
}
2532

26-
void LogPrint(bool raise, const std::string& message)
33+
static void LogPrint(bool raise, const std::string& message)
2734
{
2835
if (raise) throw std::runtime_error(message);
2936
std::ofstream("debug.log", std::ios_base::app) << message << std::endl;

example/printer.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
#include <fstream>
66
#include <init.capnp.h>
7-
#include <init.capnp.proxy-types.h>
7+
#include <init.capnp.proxy.h> // NOLINT(misc-include-cleaner)
88
#include <init.h>
99
#include <iostream>
1010
#include <memory>
1111
#include <mp/proxy-io.h>
1212
#include <printer.h>
1313
#include <stdexcept>
14+
#include <string>
1415

1516
class PrinterImpl : public Printer
1617
{
@@ -24,7 +25,7 @@ class InitImpl : public Init
2425
std::unique_ptr<Printer> makePrinter() override { return std::make_unique<PrinterImpl>(); }
2526
};
2627

27-
void LogPrint(bool raise, const std::string& message)
28+
static void LogPrint(bool raise, const std::string& message)
2829
{
2930
if (raise) throw std::runtime_error(message);
3031
std::ofstream("debug.log", std::ios_base::app) << message << std::endl;
@@ -37,7 +38,7 @@ int main(int argc, char** argv)
3738
return 1;
3839
}
3940
mp::EventLoop loop("mpprinter", LogPrint);
40-
int fd = std::stoi(argv[1]);
41+
const int fd = std::stoi(argv[1]);
4142
std::unique_ptr<Init> init = std::make_unique<InitImpl>();
4243
mp::ServeStream<InitInterface>(loop, fd, *init);
4344
loop.loop();

include/mp/proxy-io.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ struct ProxyClient<Thread> : public ProxyClientBase<Thread, ::capnp::Void>
6363
ProxyClient(const ProxyClient&) = delete;
6464
~ProxyClient();
6565

66-
void setCleanup(std::function<void()> fn);
66+
void setCleanup(const std::function<void()>& fn);
6767

6868
//! Cleanup function to run when the connection is closed. If the Connection
6969
//! gets destroyed before this ProxyClient<Thread> object, this cleanup
@@ -152,7 +152,7 @@ class EventLoop
152152
template <typename Callable>
153153
void sync(Callable&& callable)
154154
{
155-
return post(std::ref(callable));
155+
post(std::ref(callable));
156156
}
157157

158158
//! Start asynchronous worker thread if necessary. This is only done if
@@ -247,7 +247,7 @@ struct Waiter
247247
template <typename Fn>
248248
void post(Fn&& fn)
249249
{
250-
std::unique_lock<std::mutex> lock(m_mutex);
250+
const std::unique_lock<std::mutex> lock(m_mutex);
251251
assert(!m_fn);
252252
m_fn = std::move(fn);
253253
m_cv.notify_all();
@@ -269,7 +269,7 @@ struct Waiter
269269
fn();
270270
lock.lock();
271271
}
272-
bool done = pred();
272+
const bool done = pred();
273273
return done;
274274
});
275275
}
@@ -488,7 +488,7 @@ ProxyServerBase<Interface, Impl>::~ProxyServerBase()
488488
CleanupRun(fns);
489489
});
490490
}
491-
assert(m_context.cleanup_fns.size() == 0);
491+
assert(m_context.cleanup_fns.empty());
492492
std::unique_lock<std::mutex> lock(m_context.connection->m_loop.m_mutex);
493493
m_context.connection->m_loop.removeClient(lock);
494494
}
@@ -523,7 +523,7 @@ using ConnThread = ConnThreads::iterator;
523523
// Retrieve ProxyClient<Thread> object associated with this connection from a
524524
// map, or create a new one and insert it into the map. Return map iterator and
525525
// inserted bool.
526-
std::tuple<ConnThread, bool> SetThread(ConnThreads& threads, std::mutex& mutex, Connection* connection, std::function<Thread::Client()> make_thread);
526+
std::tuple<ConnThread, bool> SetThread(ConnThreads& threads, std::mutex& mutex, Connection* connection, const std::function<Thread::Client()>& make_thread);
527527

528528
struct ThreadContext
529529
{

include/mp/proxy-types.h

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ struct StructField
3939

4040
// clang-format off
4141
template<typename A = Accessor> auto get() const -> decltype(A::get(this->m_struct)) { return A::get(this->m_struct); }
42-
template<typename A = Accessor> auto has() const -> typename std::enable_if<A::optional, bool>::type { return A::getHas(m_struct); }
43-
template<typename A = Accessor> auto has() const -> typename std::enable_if<!A::optional && A::boxed, bool>::type { return A::has(m_struct); }
44-
template<typename A = Accessor> auto has() const -> typename std::enable_if<!A::optional && !A::boxed, bool>::type { return true; }
45-
template<typename A = Accessor> auto want() const -> typename std::enable_if<A::requested, bool>::type { return A::getWant(m_struct); }
46-
template<typename A = Accessor> auto want() const -> typename std::enable_if<!A::requested, bool>::type { return true; }
42+
template<typename A = Accessor> auto has() const -> std::enable_if_t<A::optional, bool> { return A::getHas(m_struct); }
43+
template<typename A = Accessor> auto has() const -> std::enable_if_t<!A::optional && A::boxed, bool> { return A::has(m_struct); }
44+
template<typename A = Accessor> auto has() const -> std::enable_if_t<!A::optional && !A::boxed, bool> { return true; }
45+
template<typename A = Accessor> auto want() const -> std::enable_if_t<A::requested, bool> { return A::getWant(m_struct); }
46+
template<typename A = Accessor> auto want() const -> std::enable_if_t<!A::requested, bool> { return true; }
4747
template<typename A = Accessor, typename... Args> decltype(auto) set(Args&&... args) const { return A::set(this->m_struct, std::forward<Args>(args)...); }
4848
template<typename A = Accessor, typename... Args> decltype(auto) init(Args&&... args) const { return A::init(this->m_struct, std::forward<Args>(args)...); }
49-
template<typename A = Accessor> auto setHas() const -> typename std::enable_if<A::optional>::type { return A::setHas(m_struct); }
50-
template<typename A = Accessor> auto setHas() const -> typename std::enable_if<!A::optional>::type { }
51-
template<typename A = Accessor> auto setWant() const -> typename std::enable_if<A::requested>::type { return A::setWant(m_struct); }
52-
template<typename A = Accessor> auto setWant() const -> typename std::enable_if<!A::requested>::type { }
49+
template<typename A = Accessor> auto setHas() const -> std::enable_if_t<A::optional> { return A::setHas(m_struct); }
50+
template<typename A = Accessor> auto setHas() const -> std::enable_if_t<!A::optional> { }
51+
template<typename A = Accessor> auto setWant() const -> std::enable_if_t<A::requested> { return A::setWant(m_struct); }
52+
template<typename A = Accessor> auto setWant() const -> std::enable_if_t<!A::requested> { }
5353
// clang-format on
5454
};
5555

@@ -314,6 +314,9 @@ struct IterateFieldsHelper
314314
{
315315
static_cast<Derived*>(this)->handleField(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2), ParamList());
316316
}
317+
private:
318+
IterateFieldsHelper() = default;
319+
friend Derived;
317320
};
318321

319322
struct IterateFields : IterateFieldsHelper<IterateFields, 0>
@@ -372,14 +375,14 @@ struct ClientParam
372375
// position when unpacking tuple might be slower than pattern matching
373376
// approach in the stack overflow solution
374377
template <size_t I, typename... Args>
375-
auto callBuild(Args&&... args) -> typename std::enable_if<(I < sizeof...(Types))>::type
378+
auto callBuild(Args&&... args) -> std::enable_if_t<(I < sizeof...(Types))>
376379
{
377380
callBuild<I + 1>(std::forward<Args>(args)..., std::get<I>(m_client_param->m_values));
378381
}
379382

380383
template <size_t I, typename Params, typename ParamList, typename... Values>
381384
auto callBuild(ClientInvokeContext& invoke_context, Params& params, ParamList, Values&&... values) ->
382-
typename std::enable_if<(I == sizeof...(Types))>::type
385+
std::enable_if_t<(I == sizeof...(Types))>
383386
{
384387
MaybeBuildField(std::integral_constant<bool, Accessor::in>(), ParamList(), invoke_context,
385388
Make<StructField, Accessor>(params), std::forward<Values>(values)...);
@@ -400,14 +403,14 @@ struct ClientParam
400403
}
401404

402405
template <int I, typename... Args>
403-
auto callRead(Args&&... args) -> typename std::enable_if<(I < sizeof...(Types))>::type
406+
auto callRead(Args&&... args) -> std::enable_if_t<(I < sizeof...(Types))>
404407
{
405408
callRead<I + 1>(std::forward<Args>(args)..., std::get<I>(m_client_param->m_values));
406409
}
407410

408411
template <int I, typename Results, typename... Params, typename... Values>
409412
auto callRead(ClientInvokeContext& invoke_context, Results& results, TypeList<Params...>, Values&&... values)
410-
-> typename std::enable_if<I == sizeof...(Types)>::type
413+
-> std::enable_if_t<I == sizeof...(Types)>
411414
{
412415
MaybeReadField(std::integral_constant<bool, Accessor::out>(), TypeList<Decay<Params>...>(), invoke_context,
413416
Make<StructField, Accessor>(results), ReadDestUpdate(values)...);
@@ -623,15 +626,15 @@ void clientInvoke(ProxyClient& proxy_client, const GetRequest& get_request, Fiel
623626
} catch (...) {
624627
exception = std::current_exception();
625628
}
626-
std::unique_lock<std::mutex> lock(invoke_context.thread_context.waiter->m_mutex);
629+
const std::unique_lock<std::mutex> lock(invoke_context.thread_context.waiter->m_mutex);
627630
done = true;
628631
invoke_context.thread_context.waiter->m_cv.notify_all();
629632
},
630633
[&](const ::kj::Exception& e) {
631634
kj_exception = kj::str("kj::Exception: ", e).cStr();
632635
proxy_client.m_context.connection->m_loop.logPlain()
633636
<< "{" << invoke_context.thread_context.thread_name << "} IPC client exception " << kj_exception;
634-
std::unique_lock<std::mutex> lock(invoke_context.thread_context.waiter->m_mutex);
637+
const std::unique_lock<std::mutex> lock(invoke_context.thread_context.waiter->m_mutex);
635638
done = true;
636639
invoke_context.thread_context.waiter->m_cv.notify_all();
637640
}));
@@ -648,7 +651,7 @@ void clientInvoke(ProxyClient& proxy_client, const GetRequest& get_request, Fiel
648651
//! duplication and branching in generic code that forwards calls to functions.
649652
template <typename Fn, typename Ret>
650653
auto ReplaceVoid(Fn&& fn, Ret&& ret) ->
651-
typename std::enable_if<std::is_same<void, decltype(fn())>::value, decltype(ret())>::type
654+
std::enable_if_t<std::is_same_v<void, decltype(fn())>, decltype(ret())>
652655
{
653656
fn();
654657
return ret();
@@ -657,7 +660,7 @@ auto ReplaceVoid(Fn&& fn, Ret&& ret) ->
657660
//! Overload of above for non-void `fn()` case.
658661
template <typename Fn, typename Ret>
659662
auto ReplaceVoid(Fn&& fn, Ret&& ret) ->
660-
typename std::enable_if<!std::is_same<void, decltype(fn())>::value, decltype(fn())>::type
663+
std::enable_if_t<!std::is_same_v<void, decltype(fn())>, decltype(fn())>
661664
{
662665
return fn();
663666
}

include/mp/proxy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ struct FunctionTraits<_Result (_Class::*const)(_Params...)>
198198
template <size_t N>
199199
using Param = typename std::tuple_element<N, std::tuple<_Params...>>::type;
200200
using Fields =
201-
typename std::conditional<std::is_same<void, Result>::value, Params, TypeList<_Params..., _Result>>::type;
201+
std::conditional_t<std::is_same_v<void, Result>, Params, TypeList<_Params..., _Result>>;
202202
};
203203

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

include/mp/util.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ struct TypeList
4343
//! Example:
4444
//! Make<std::pair>(5, true) // Constructs std::pair<int, bool>(5, true);
4545
template <template <typename...> class Class, typename... Types, typename... Args>
46-
Class<Types..., typename std::remove_reference<Args>::type...> Make(Args&&... args)
46+
Class<Types..., std::remove_reference_t<Args>...> Make(Args&&... args)
4747
{
48-
return Class<Types..., typename std::remove_reference<Args>::type...>{std::forward<Args>(args)...};
48+
return Class<Types..., std::remove_reference_t<Args>...>{std::forward<Args>(args)...};
4949
}
5050

5151
//! Type helper splitting a TypeList into two halves at position index.
@@ -83,7 +83,7 @@ using RemoveCvRef = std::remove_cv_t<std::remove_reference_t<T>>;
8383

8484
//! Type helper abbreviating std::decay.
8585
template <typename T>
86-
using Decay = typename std::decay<T>::type;
86+
using Decay = std::decay_t<T>;
8787

8888
//! SFINAE helper, see using Require below.
8989
template <typename SfinaeExpr, typename Result_>
@@ -142,7 +142,7 @@ struct UnlockGuard
142142
template <typename Lock, typename Callback>
143143
void Unlock(Lock& lock, Callback&& callback)
144144
{
145-
UnlockGuard<Lock> unlock(lock);
145+
const UnlockGuard<Lock> unlock(lock);
146146
callback();
147147
}
148148

@@ -157,7 +157,7 @@ struct DestructorCatcher
157157
{
158158
}
159159
~DestructorCatcher() noexcept try {
160-
} catch (const kj::Exception& e) {
160+
} catch (const kj::Exception& e) { // NOLINT(bugprone-empty-catch)
161161
}
162162
};
163163

@@ -181,7 +181,7 @@ struct AsyncCallable
181181

182182
//! Construct AsyncCallable object.
183183
template <typename Callable>
184-
AsyncCallable<typename std::remove_reference<Callable>::type> MakeAsyncCallable(Callable&& callable)
184+
AsyncCallable<std::remove_reference_t<Callable>> MakeAsyncCallable(Callable&& callable)
185185
{
186186
return std::move(callable);
187187
}

0 commit comments

Comments
 (0)