Skip to content

Commit 848c902

Browse files
hebastoryanofsky
andcommitted
clang-tidy: Fix misc-const-correctness check
See: https://clang.llvm.org/extra/clang-tidy/checks/misc/const-correctness.html Co-authored-by: Ryan Ofsky <[email protected]>
1 parent 4a2508c commit 848c902

File tree

9 files changed

+36
-36
lines changed

9 files changed

+36
-36
lines changed

example/calculator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ int main(int argc, char** argv)
4343
return 1;
4444
}
4545
mp::EventLoop loop("mpcalculator", LogPrint);
46-
int fd = std::stoi(argv[1]);
46+
const int fd = std::stoi(argv[1]);
4747
std::unique_ptr<Init> init = std::make_unique<InitImpl>();
4848
mp::ServeStream<InitInterface>(loop, fd, *init);
4949
loop.loop();

example/example.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace fs = std::filesystem;
1414
auto Spawn(mp::EventLoop& loop, const std::string& process_argv0, const std::string& new_exe_name)
1515
{
1616
int pid;
17-
int fd = mp::SpawnProcess(pid, [&](int fd) -> std::vector<std::string> {
17+
const int fd = mp::SpawnProcess(pid, [&](int fd) -> std::vector<std::string> {
1818
fs::path path = process_argv0;
1919
path.remove_filename();
2020
path.append(new_exe_name);

example/printer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ int main(int argc, char** argv)
3737
return 1;
3838
}
3939
mp::EventLoop loop("mpprinter", LogPrint);
40-
int fd = std::stoi(argv[1]);
40+
const int fd = std::stoi(argv[1]);
4141
std::unique_ptr<Init> init = std::make_unique<InitImpl>();
4242
mp::ServeStream<InitInterface>(loop, fd, *init);
4343
loop.loop();

include/mp/proxy-io.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

include/mp/proxy-types.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,15 +626,15 @@ void clientInvoke(ProxyClient& proxy_client, const GetRequest& get_request, Fiel
626626
} catch (...) {
627627
exception = std::current_exception();
628628
}
629-
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);
630630
done = true;
631631
invoke_context.thread_context.waiter->m_cv.notify_all();
632632
},
633633
[&](const ::kj::Exception& e) {
634634
kj_exception = kj::str("kj::Exception: ", e).cStr();
635635
proxy_client.m_context.connection->m_loop.logPlain()
636636
<< "{" << invoke_context.thread_context.thread_name << "} IPC client exception " << kj_exception;
637-
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);
638638
done = true;
639639
invoke_context.thread_context.waiter->m_cv.notify_all();
640640
}));

include/mp/util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

src/mp/gen.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ void Generate(kj::StringPtr src_prefix,
151151
}
152152

153153
std::string include_base = include_path;
154-
std::string::size_type p = include_base.rfind('.');
154+
const std::string::size_type p = include_base.rfind('.');
155155
if (p != std::string::npos) include_base.erase(p);
156156

157157
std::vector<std::string> args;
@@ -165,19 +165,19 @@ void Generate(kj::StringPtr src_prefix,
165165
}
166166
args.emplace_back("--output=" capnp_PREFIX "/bin/capnpc-c++");
167167
args.emplace_back(src_file);
168-
int pid = fork();
168+
const int pid = fork();
169169
if (pid == -1) {
170170
throw std::system_error(errno, std::system_category(), "fork");
171171
}
172172
if (!pid) {
173173
mp::ExecProcess(args);
174174
}
175-
int status = mp::WaitProcess(pid);
175+
const int status = mp::WaitProcess(pid);
176176
if (status) {
177177
throw std::runtime_error("Invoking " capnp_PREFIX "/bin/capnp failed");
178178
}
179179

180-
capnp::SchemaParser parser;
180+
const capnp::SchemaParser parser;
181181
auto directory_pointers = kj::heapArray<const kj::ReadableDirectory*>(import_dirs.size());
182182
for (size_t i = 0; i < import_dirs.size(); ++i) {
183183
directory_pointers[i] = import_dirs[i].get();
@@ -244,7 +244,7 @@ void Generate(kj::StringPtr src_prefix,
244244
GetAnnotationText(file_schema.getProto(), NAMESPACE_ANNOTATION_ID, &message_namespace);
245245

246246
std::string base_name = include_base;
247-
size_t output_slash = base_name.rfind('/');
247+
const size_t output_slash = base_name.rfind('/');
248248
if (output_slash != std::string::npos) {
249249
base_name.erase(0, output_slash + 1);
250250
}
@@ -260,7 +260,7 @@ void Generate(kj::StringPtr src_prefix,
260260

261261
auto add_accessor = [&](kj::StringPtr name) {
262262
if (!accessors_done.insert(name).second) return;
263-
std::string cap = Cap(name);
263+
const std::string cap = Cap(name);
264264
accessors << "struct " << cap << "\n";
265265
accessors << "{\n";
266266
accessors << " template<typename S> static auto get(S&& s) -> decltype(s.get" << cap << "()) { return s.get" << cap << "(); }\n";
@@ -368,19 +368,19 @@ void Generate(kj::StringPtr src_prefix,
368368
server << " using ProxyServerCustom::ProxyServerCustom;\n";
369369
server << " ~ProxyServer();\n";
370370

371-
std::ostringstream client_construct;
372-
std::ostringstream client_destroy;
371+
const std::ostringstream client_construct;
372+
const std::ostringstream client_destroy;
373373

374374
int method_ordinal = 0;
375375
ForEachMethod(interface, [&] (const capnp::InterfaceSchema& method_interface, const capnp::InterfaceSchema::Method& method) {
376-
kj::StringPtr method_name = method.getProto().getName();
376+
const kj::StringPtr method_name = method.getProto().getName();
377377
kj::StringPtr proxied_method_name = method_name;
378378
GetAnnotationText(method.getProto(), NAME_ANNOTATION_ID, &proxied_method_name);
379379

380380
const std::string method_prefix = Format() << message_namespace << "::" << method_interface.getShortDisplayName()
381381
<< "::" << Cap(method_name);
382-
bool is_construct = method_name == "construct";
383-
bool is_destroy = method_name == "destroy";
382+
const bool is_construct = method_name == "construct";
383+
const bool is_destroy = method_name == "destroy";
384384

385385
struct Field
386386
{
@@ -530,9 +530,9 @@ void Generate(kj::StringPtr src_prefix,
530530
server_invoke_end << ")";
531531
}
532532

533-
std::string static_str{is_construct || is_destroy ? "static " : ""};
534-
std::string super_str{is_construct || is_destroy ? "Super& super" : ""};
535-
std::string self_str{is_construct || is_destroy ? "super" : "*this"};
533+
const std::string static_str{is_construct || is_destroy ? "static " : ""};
534+
const std::string super_str{is_construct || is_destroy ? "Super& super" : ""};
535+
const std::string self_str{is_construct || is_destroy ? "super" : "*this"};
536536

537537
client << " using M" << method_ordinal << " = ProxyClientMethodTraits<" << method_prefix
538538
<< "Params>;\n";

src/mp/proxy.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Connection::~Connection()
102102
m_sync_cleanup_fns.pop_front();
103103
}
104104
while (!m_async_cleanup_fns.empty()) {
105-
std::unique_lock<std::mutex> lock(m_loop.m_mutex);
105+
const std::unique_lock<std::mutex> lock(m_loop.m_mutex);
106106
m_loop.m_async_fns.emplace_back(std::move(m_async_cleanup_fns.front()));
107107
m_async_cleanup_fns.pop_front();
108108
}
@@ -113,7 +113,7 @@ Connection::~Connection()
113113

114114
CleanupIt Connection::addSyncCleanup(std::function<void()> fn)
115115
{
116-
std::unique_lock<std::mutex> lock(m_loop.m_mutex);
116+
const std::unique_lock<std::mutex> lock(m_loop.m_mutex);
117117
// Add cleanup callbacks to the front of list, so sync cleanup functions run
118118
// in LIFO order. This is a good approach because sync cleanup functions are
119119
// added as client objects are created, and it is natural to clean up
@@ -127,13 +127,13 @@ CleanupIt Connection::addSyncCleanup(std::function<void()> fn)
127127

128128
void Connection::removeSyncCleanup(CleanupIt it)
129129
{
130-
std::unique_lock<std::mutex> lock(m_loop.m_mutex);
130+
const std::unique_lock<std::mutex> lock(m_loop.m_mutex);
131131
m_sync_cleanup_fns.erase(it);
132132
}
133133

134134
void Connection::addAsyncCleanup(std::function<void()> fn)
135135
{
136-
std::unique_lock<std::mutex> lock(m_loop.m_mutex);
136+
const std::unique_lock<std::mutex> lock(m_loop.m_mutex);
137137
// Add async cleanup callbacks to the back of the list. Unlike the sync
138138
// cleanup list, this list order is more significant because it determines
139139
// the order server objects are destroyed when there is a sudden disconnect,
@@ -169,7 +169,7 @@ EventLoop::EventLoop(const char* exe_name, LogFn log_fn, void* context)
169169
EventLoop::~EventLoop()
170170
{
171171
if (m_async_thread.joinable()) m_async_thread.join();
172-
std::lock_guard<std::mutex> lock(m_mutex);
172+
const std::lock_guard<std::mutex> lock(m_mutex);
173173
KJ_ASSERT(m_post_fn == nullptr);
174174
KJ_ASSERT(m_async_fns.empty());
175175
KJ_ASSERT(m_wait_fd == -1);
@@ -192,7 +192,7 @@ void EventLoop::loop()
192192
int post_fd{m_post_fd};
193193
char buffer = 0;
194194
for (;;) {
195-
size_t read_bytes = wait_stream->read(&buffer, 0, 1).wait(m_io_context.waitScope);
195+
const size_t read_bytes = wait_stream->read(&buffer, 0, 1).wait(m_io_context.waitScope);
196196
if (read_bytes != 1) throw std::logic_error("EventLoop wait_stream closed unexpectedly");
197197
std::unique_lock<std::mutex> lock(m_mutex);
198198
if (m_post_fn) {
@@ -212,7 +212,7 @@ void EventLoop::loop()
212212
log() << "EventLoop::loop bye.";
213213
wait_stream = nullptr;
214214
KJ_SYSCALL(::close(post_fd));
215-
std::unique_lock<std::mutex> lock(m_mutex);
215+
const std::unique_lock<std::mutex> lock(m_mutex);
216216
m_wait_fd = -1;
217217
m_post_fd = -1;
218218
}
@@ -258,7 +258,7 @@ void EventLoop::startAsyncThread(std::unique_lock<std::mutex>& lock)
258258
while (true) {
259259
if (!m_async_fns.empty()) {
260260
addClient(lock);
261-
std::function<void()> fn = std::move(m_async_fns.front());
261+
const std::function<void()> fn = std::move(m_async_fns.front());
262262
m_async_fns.pop_front();
263263
Unlock(lock, fn);
264264
removeClient(lock);
@@ -282,7 +282,7 @@ bool EventLoop::done(std::unique_lock<std::mutex>& lock)
282282

283283
std::tuple<ConnThread, bool> SetThread(ConnThreads& threads, std::mutex& mutex, Connection* connection, std::function<Thread::Client()> make_thread)
284284
{
285-
std::unique_lock<std::mutex> lock(mutex);
285+
const std::unique_lock<std::mutex> lock(mutex);
286286
auto thread = threads.find(connection);
287287
if (thread != threads.end()) return {thread, false};
288288
thread = threads.emplace(
@@ -299,7 +299,7 @@ std::tuple<ConnThread, bool> SetThread(ConnThreads& threads, std::mutex& mutex,
299299
// try unregister this callback after connection is destroyed.
300300
thread->second.m_cleanup_it.reset();
301301
// Remove connection pointer about to be destroyed from the map
302-
std::unique_lock<std::mutex> lock(mutex);
302+
const std::unique_lock<std::mutex> lock(mutex);
303303
threads.erase(thread);
304304
});
305305
return {thread, true};
@@ -339,7 +339,7 @@ ProxyServer<Thread>::~ProxyServer()
339339
assert(m_thread_context.waiter.get());
340340
std::unique_ptr<Waiter> waiter;
341341
{
342-
std::unique_lock<std::mutex> lock(m_thread_context.waiter->m_mutex);
342+
const std::unique_lock<std::mutex> lock(m_thread_context.waiter->m_mutex);
343343
//! Reset thread context waiter pointer, as shutdown signal for done
344344
//! lambda passed as waiter->wait() argument in makeThread code below.
345345
waiter = std::move(m_thread_context.waiter);
@@ -367,7 +367,7 @@ ProxyServer<ThreadMap>::ProxyServer(Connection& connection) : m_connection(conne
367367

368368
kj::Promise<void> ProxyServer<ThreadMap>::makeThread(MakeThreadContext context)
369369
{
370-
std::string from = context.getParams().getName();
370+
const std::string from = context.getParams().getName();
371371
std::promise<ThreadContext*> thread_context;
372372
std::thread thread([&thread_context, from, this]() {
373373
g_thread_context.thread_name = ThreadName(m_connection.m_loop.m_exe_name) + " (from " + from + ")";

src/mp/util.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ std::string LogEscape(const kj::StringTree& string)
8080
std::string result;
8181
string.visit([&](const kj::ArrayPtr<const char>& piece) {
8282
if (result.size() > MAX_SIZE) return;
83-
for (char c : piece) {
83+
for (const char c : piece) {
8484
if (c == '\\') {
8585
result.append("\\\\");
8686
} else if (c < 0x20 || c > 0x7e) {
@@ -116,7 +116,7 @@ int SpawnProcess(int& pid, FdToArgsFn&& fd_to_args)
116116
}
117117
if (!pid) {
118118
// Child process must close all potentially open descriptors, except socket 0.
119-
int maxFd = MaxFd();
119+
const int maxFd = MaxFd();
120120
for (int fd = 3; fd < maxFd; ++fd) {
121121
if (fd != fds[0]) {
122122
close(fd);

0 commit comments

Comments
 (0)