From 5935db10fd0abebfd26816615c5c9a2a50f5bf5e Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Thu, 10 Oct 2019 19:24:44 -0700 Subject: [PATCH] Fix some clang-tidy warnings Incorporated some fixes by Daniel Chabrowski (#467) --- src/indexer.cc | 2 +- src/lsp.cc | 4 ++-- src/message_handler.cc | 6 ++---- src/message_handler.hh | 5 +++-- src/messages/initialize.cc | 2 +- src/messages/textDocument_formatting.cc | 4 ++-- src/messages/workspace.cc | 2 +- src/pipeline.cc | 22 ++++++++++++---------- src/pipeline.hh | 7 ++++--- src/project.cc | 4 ++-- src/project.hh | 2 +- src/sema_manager.cc | 8 +++++--- src/serializer.cc | 7 ++++--- src/serializer.hh | 7 ++++--- src/working_files.cc | 2 +- 15 files changed, 45 insertions(+), 39 deletions(-) diff --git a/src/indexer.cc b/src/indexer.cc index 37467c7f..9c17a7dd 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -663,7 +663,7 @@ public: rd->isInvalidDecl() || !validateRecord(rd)) offset = -1; for (FieldDecl *fd : rd->fields()) { - int offset1 = offset < 0 ? -1 : offset + ctx->getFieldOffset(fd); + int offset1 = offset < 0 ? -1 : int(offset + ctx->getFieldOffset(fd)); if (fd->getIdentifier()) type.def.vars.emplace_back(getUsr(fd), offset1); else if (const auto *rt1 = fd->getType()->getAs()) { diff --git a/src/lsp.cc b/src/lsp.cc index 6ef0d9b8..662f5026 100644 --- a/src/lsp.cc +++ b/src/lsp.cc @@ -33,7 +33,7 @@ void reflect(JsonWriter &visitor, RequestId &value) { visitor.null_(); break; case RequestId::kInt: - visitor.int_(atoll(value.value.c_str())); + visitor.int64(atoll(value.value.c_str())); break; case RequestId::kString: visitor.string(value.value.c_str(), value.value.size()); @@ -51,7 +51,7 @@ void DocumentUri::setPath(const std::string &path) { // file:///c%3A/Users/jacob/Desktop/superindex/indexer/full_tests raw_uri = path; - size_t index = raw_uri.find(":"); + size_t index = raw_uri.find(':'); if (index == 1) { // widows drive letters must always be 1 char raw_uri.replace(raw_uri.begin() + index, raw_uri.begin() + index + 1, "%3A"); diff --git a/src/message_handler.cc b/src/message_handler.cc index 5c4cdfca..6e9266e6 100644 --- a/src/message_handler.cc +++ b/src/message_handler.cc @@ -109,10 +109,8 @@ void ReplyOnce::replyLocationLink(std::vector &result) { if (g_config->client.linkSupport) { (*this)(result); } else { - std::vector result1; - for (auto &loc : result) - result1.emplace_back(std::move(loc)); - (*this)(result1); + (*this)(std::vector(std::make_move_iterator(result.begin()), + std::make_move_iterator(result.end()))); } } diff --git a/src/message_handler.hh b/src/message_handler.hh index 7d70e627..02084b60 100644 --- a/src/message_handler.hh +++ b/src/message_handler.hh @@ -20,8 +20,9 @@ struct WorkingFile; struct WorkingFiles; namespace pipeline { -void reply(RequestId id, const std::function &fn); -void replyError(RequestId id, const std::function &fn); +void reply(const RequestId &id, const std::function &fn); +void replyError(const RequestId &id, + const std::function &fn); } // namespace pipeline struct CodeActionParam { diff --git a/src/messages/initialize.cc b/src/messages/initialize.cc index ca046bc5..5861d28c 100644 --- a/src/messages/initialize.cc +++ b/src/messages/initialize.cc @@ -370,7 +370,7 @@ void do_initialize(MessageHandler *m, InitializeParam ¶m, // may take a long time. Indexer threads will emit status/progress // reports. if (g_config->index.threads == 0) - g_config->index.threads = std::thread::hardware_concurrency(); + g_config->index.threads = (int)std::thread::hardware_concurrency(); LOG_S(INFO) << "start " << g_config->index.threads << " indexers"; for (int i = 0; i < g_config->index.threads; i++) diff --git a/src/messages/textDocument_formatting.cc b/src/messages/textDocument_formatting.cc index aa071356..261d0a9e 100644 --- a/src/messages/textDocument_formatting.cc +++ b/src/messages/textDocument_formatting.cc @@ -32,8 +32,8 @@ std::vector replacementsToEdits(std::string_view code, const tooling::Replacements &repls) { std::vector ret; int i = 0, line = 0, col = 0; - auto move = [&](int p) { - for (; i < p; i++) + auto move = [&](unsigned p) { + for (; i < (int)p; i++) if (code[i] == '\n') line++, col = 0; else { diff --git a/src/messages/workspace.cc b/src/messages/workspace.cc index a1fd5a51..c604ab62 100644 --- a/src/messages/workspace.cc +++ b/src/messages/workspace.cc @@ -89,7 +89,7 @@ void MessageHandler::workspace_didChangeWorkspaceFolders( if (folder == real) real.clear(); LOG_S(INFO) << "add workspace folder " << wf.name << ": " - << (real.empty() ? folder : folder + " -> " + real); + << (real.empty() ? folder : (folder + " -> ").append(real)); workspaceFolders.emplace_back(); auto it = workspaceFolders.end() - 1; for (; it != workspaceFolders.begin() && folder < it[-1].first; --it) diff --git a/src/pipeline.cc b/src/pipeline.cc index 4773c69b..9ebccb5d 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -605,13 +605,13 @@ void mainLoop() { SemaManager manager( &project, &wfiles, - [&](std::string path, std::vector diagnostics) { + [](const std::string &path, std::vector diagnostics) { PublishDiagnosticParam params; params.uri = DocumentUri::fromPath(path); - params.diagnostics = diagnostics; + params.diagnostics = std::move(diagnostics); notify("textDocument/publishDiagnostics", params); }, - [](RequestId id) { + [](const RequestId &id) { if (id.valid()) { ResponseError err; err.code = ErrorCode::InternalError; @@ -706,8 +706,9 @@ void standalone(const std::string &root) { WorkingFiles wfiles; VFS vfs; SemaManager manager( - nullptr, nullptr, [&](std::string, std::vector) {}, - [](RequestId id) {}); + nullptr, nullptr, + [](const std::string &, const std::vector &) {}, + [](const RequestId &id) {}); IncludeComplete complete(&project); MessageHandler handler; @@ -745,7 +746,7 @@ void standalone(const std::string &root) { void index(const std::string &path, const std::vector &args, IndexMode mode, bool must_exist, RequestId id) { pending_index_requests++; - index_request->pushBack({path, args, mode, must_exist, id}, + index_request->pushBack({path, args, mode, must_exist, std::move(id)}, mode != IndexMode::Background); } @@ -789,7 +790,7 @@ void notifyOrRequest(const char *method, bool request, for_stdout->pushBack(output.GetString()); } -static void reply(RequestId id, const char *key, +static void reply(const RequestId &id, const char *key, const std::function &fn) { rapidjson::StringBuffer output; rapidjson::Writer w(output); @@ -802,7 +803,7 @@ static void reply(RequestId id, const char *key, w.Null(); break; case RequestId::kInt: - w.Int(atoll(id.value.c_str())); + w.Int64(atoll(id.value.c_str())); break; case RequestId::kString: w.String(id.value.c_str(), id.value.size()); @@ -817,11 +818,12 @@ static void reply(RequestId id, const char *key, for_stdout->pushBack(output.GetString()); } -void reply(RequestId id, const std::function &fn) { +void reply(const RequestId &id, const std::function &fn) { reply(id, "result", fn); } -void replyError(RequestId id, const std::function &fn) { +void replyError(const RequestId &id, + const std::function &fn) { reply(id, "error", fn); } } // namespace pipeline diff --git a/src/pipeline.hh b/src/pipeline.hh index 0b50c282..1c223307 100644 --- a/src/pipeline.hh +++ b/src/pipeline.hh @@ -68,10 +68,11 @@ template void request(const char *method, T &result) { notifyOrRequest(method, true, [&](JsonWriter &w) { reflect(w, result); }); } -void reply(RequestId id, const std::function &fn); +void reply(const RequestId &id, const std::function &fn); -void replyError(RequestId id, const std::function &fn); -template void replyError(RequestId id, T &result) { +void replyError(const RequestId &id, + const std::function &fn); +template void replyError(const RequestId &id, T &result) { replyError(id, [&](JsonWriter &w) { reflect(w, result); }); } } // namespace pipeline diff --git a/src/project.cc b/src/project.cc index c358b28c..e22e523c 100644 --- a/src/project.cc +++ b/src/project.cc @@ -237,7 +237,7 @@ bool appendToCDB(const std::vector &args) { return args.size() && StringRef("%compile_commands.json") == args[0]; } -std::vector getFallback(const std::string path) { +std::vector getFallback(const std::string &path) { std::vector argv{"clang"}; if (sys::path::extension(path) == ".h") argv.push_back("-xobjective-c++-header"); @@ -595,7 +595,7 @@ Project::Entry Project::findEntry(const std::string &path, bool can_redirect, return ret; } -void Project::index(WorkingFiles *wfiles, RequestId id) { +void Project::index(WorkingFiles *wfiles, const RequestId &id) { auto &gi = g_config->index; GroupMatch match(gi.whitelist, gi.blacklist), match_i(gi.initialWhitelist, gi.initialBlacklist); diff --git a/src/project.hh b/src/project.hh index abcfe5d4..f43419f1 100644 --- a/src/project.hh +++ b/src/project.hh @@ -64,7 +64,7 @@ struct Project { void setArgsForFile(const std::vector &args, const std::string &path); - void index(WorkingFiles *wfiles, RequestId id); + void index(WorkingFiles *wfiles, const RequestId &id); void indexRelated(const std::string &path); }; } // namespace ccls diff --git a/src/sema_manager.cc b/src/sema_manager.cc index d1af645b..6a228f39 100644 --- a/src/sema_manager.cc +++ b/src/sema_manager.cc @@ -604,7 +604,7 @@ void *diagnosticMain(void *manager_) { ret.severity = 1; break; } - ret.code = d.category; + ret.code = (int)d.category; return ret; }; @@ -673,8 +673,10 @@ std::shared_ptr Session::getPreamble() { SemaManager::SemaManager(Project *project, WorkingFiles *wfiles, OnDiagnostic on_diagnostic, OnDropped on_dropped) - : project_(project), wfiles(wfiles), on_diagnostic_(on_diagnostic), - on_dropped_(on_dropped), pch(std::make_shared()) { + : project_(project), wfiles(wfiles), + on_diagnostic_(std::move(on_diagnostic)), + on_dropped_(std::move(on_dropped)), + pch(std::make_shared()) { spawnThread(ccls::preambleMain, this); spawnThread(ccls::completionMain, this); spawnThread(ccls::diagnosticMain, this); diff --git a/src/serializer.cc b/src/serializer.cc index 99cea067..b1b93faa 100644 --- a/src/serializer.cc +++ b/src/serializer.cc @@ -13,6 +13,7 @@ #include #include +#include #include #include @@ -24,7 +25,7 @@ bool gTestOutputMode = false; namespace ccls { -void JsonReader::iterArray(std::function fn) { +void JsonReader::iterArray(llvm::function_ref fn) { if (!m->IsArray()) throw std::invalid_argument("array"); // Use "0" to indicate any element for now. @@ -37,7 +38,7 @@ void JsonReader::iterArray(std::function fn) { } path_.pop_back(); } -void JsonReader::member(const char *name, std::function fn) { +void JsonReader::member(const char *name, llvm::function_ref fn) { path_.push_back(name); auto it = m->FindMember(name); if (it != m->MemberEnd()) { @@ -70,7 +71,7 @@ void JsonWriter::startObject() { m->StartObject(); } void JsonWriter::endObject() { m->EndObject(); } void JsonWriter::key(const char *name) { m->Key(name); } void JsonWriter::null_() { m->Null(); } -void JsonWriter::int_(int v) { m->Int(v); } +void JsonWriter::int64(int64_t v) { m->Int64(v); } void JsonWriter::string(const char *s) { m->String(s); } void JsonWriter::string(const char *s, size_t len) { m->String(s, len); } diff --git a/src/serializer.hh b/src/serializer.hh index aa481f19..2177e1fd 100644 --- a/src/serializer.hh +++ b/src/serializer.hh @@ -22,6 +22,7 @@ namespace llvm { class CachedHashStringRef; class StringRef; +template class function_ref; } // namespace llvm namespace ccls { @@ -36,8 +37,8 @@ struct JsonReader { JsonReader(rapidjson::Value *m) : m(m) {} void startObject() {} void endObject() {} - void iterArray(std::function fn); - void member(const char *name, std::function fn); + void iterArray(llvm::function_ref fn); + void member(const char *name, llvm::function_ref fn); bool isNull(); std::string getString(); std::string getPath() const; @@ -57,7 +58,7 @@ struct JsonWriter { void endObject(); void key(const char *name); void null_(); - void int_(int v); + void int64(int64_t v); void string(const char *s); void string(const char *s, size_t len); }; diff --git a/src/working_files.cc b/src/working_files.cc index 8e7ccb7d..09813ab1 100644 --- a/src/working_files.cc +++ b/src/working_files.cc @@ -131,7 +131,7 @@ int alignColumn(const std::string &a, int column, std::string b, bool is_end) { if (column < head) return column; if ((int)a.size() - tail < column) - return column + b.size() - a.size(); + return column + (int)b.size() - (int)a.size(); if (std::max(a.size(), b.size()) - head - tail >= kMaxColumnAlignSize) return std::min(column, (int)b.size());