Fix some clang-tidy warnings

Incorporated some fixes by Daniel Chabrowski (#467)
This commit is contained in:
Fangrui Song 2019-10-10 19:24:44 -07:00
parent d61bf578f3
commit 6a488bfbf4
18 changed files with 54 additions and 51 deletions

View File

@ -36,8 +36,8 @@ std::string pathFromFileEntry(const FileEntry &file) {
static Pos decomposed2LineAndCol(const SourceManager &sm, static Pos decomposed2LineAndCol(const SourceManager &sm,
std::pair<FileID, unsigned> i) { std::pair<FileID, unsigned> i) {
int l = sm.getLineNumber(i.first, i.second) - 1, int l = (int)sm.getLineNumber(i.first, i.second) - 1,
c = sm.getColumnNumber(i.first, i.second) - 1; c = (int)sm.getColumnNumber(i.first, i.second) - 1;
bool invalid = false; bool invalid = false;
StringRef buf = sm.getBufferData(i.first, &invalid); StringRef buf = sm.getBufferData(i.first, &invalid);
if (!invalid) { if (!invalid) {
@ -114,7 +114,6 @@ const char *clangBuiltinTypeName(int kind) {
case BuiltinType::Bool: case BuiltinType::Bool:
return "bool"; return "bool";
case BuiltinType::Char_S: case BuiltinType::Char_S:
return "char";
case BuiltinType::Char_U: case BuiltinType::Char_U:
return "char"; return "char";
case BuiltinType::SChar: case BuiltinType::SChar:

View File

@ -101,7 +101,7 @@ FuzzyMatcher::FuzzyMatcher(std::string_view pattern, int sensitivity) {
for (size_t i = 0; i < pattern.size(); i++) for (size_t i = 0; i < pattern.size(); i++)
if (pattern[i] != ' ') { if (pattern[i] != ' ') {
pat += pattern[i]; pat += pattern[i];
low_pat[n] = ::tolower(pattern[i]); low_pat[n] = (char)::tolower(pattern[i]);
pat_role[n] = pat_role[i]; pat_role[n] = pat_role[i];
n++; n++;
} }
@ -115,7 +115,7 @@ int FuzzyMatcher::match(std::string_view text, bool strict) {
return kMinScore + 1; return kMinScore + 1;
this->text = text; this->text = text;
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
low_text[i] = ::tolower(text[i]); low_text[i] = (char)::tolower(text[i]);
calculateRoles(text, text_role, &text_set); calculateRoles(text, text_role, &text_set);
if (strict && n && !!pat_role[0] != !!text_role[0]) if (strict && n && !!pat_role[0] != !!text_role[0])
return kMinScore; return kMinScore;

View File

@ -461,9 +461,7 @@ public:
} }
while (ret.size() && isspace(ret.back())) while (ret.size() && isspace(ret.back()))
ret.pop_back(); ret.pop_back();
if (StringRef(ret).endswith("*/")) if (StringRef(ret).endswith("*/") || StringRef(ret).endswith("\n/"))
ret.resize(ret.size() - 2);
else if (StringRef(ret).endswith("\n/"))
ret.resize(ret.size() - 2); ret.resize(ret.size() - 2);
while (ret.size() && isspace(ret.back())) while (ret.size() && isspace(ret.back()))
ret.pop_back(); ret.pop_back();
@ -676,7 +674,7 @@ public:
rd->isInvalidDecl() || !validateRecord(rd)) rd->isInvalidDecl() || !validateRecord(rd))
offset = -1; offset = -1;
for (FieldDecl *fd : rd->fields()) { 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()) if (fd->getIdentifier())
type.def.vars.emplace_back(getUsr(fd), offset1); type.def.vars.emplace_back(getUsr(fd), offset1);
else if (const auto *rt1 = fd->getType()->getAs<RecordType>()) { else if (const auto *rt1 = fd->getType()->getAs<RecordType>()) {

View File

@ -45,7 +45,7 @@ void reflect(JsonWriter &visitor, RequestId &value) {
visitor.null_(); visitor.null_();
break; break;
case RequestId::kInt: case RequestId::kInt:
visitor.int_(atoll(value.value.c_str())); visitor.int64(atoll(value.value.c_str()));
break; break;
case RequestId::kString: case RequestId::kString:
visitor.string(value.value.c_str(), value.value.size()); visitor.string(value.value.c_str(), value.value.size());
@ -63,7 +63,7 @@ void DocumentUri::setPath(const std::string &path) {
// file:///c%3A/Users/jacob/Desktop/superindex/indexer/full_tests // file:///c%3A/Users/jacob/Desktop/superindex/indexer/full_tests
raw_uri = path; 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 if (index == 1) { // widows drive letters must always be 1 char
raw_uri.replace(raw_uri.begin() + index, raw_uri.begin() + index + 1, raw_uri.replace(raw_uri.begin() + index, raw_uri.begin() + index + 1,
"%3A"); "%3A");

View File

@ -121,10 +121,8 @@ void ReplyOnce::replyLocationLink(std::vector<LocationLink> &result) {
if (g_config->client.linkSupport) { if (g_config->client.linkSupport) {
(*this)(result); (*this)(result);
} else { } else {
std::vector<Location> result1; (*this)(std::vector<Location>(std::make_move_iterator(result.begin()),
for (auto &loc : result) std::make_move_iterator(result.end())));
result1.emplace_back(std::move(loc));
(*this)(result1);
} }
} }

View File

@ -32,8 +32,9 @@ struct WorkingFile;
struct WorkingFiles; struct WorkingFiles;
namespace pipeline { namespace pipeline {
void reply(RequestId id, const std::function<void(JsonWriter &)> &fn); void reply(const RequestId &id, const std::function<void(JsonWriter &)> &fn);
void replyError(RequestId id, const std::function<void(JsonWriter &)> &fn); void replyError(const RequestId &id,
const std::function<void(JsonWriter &)> &fn);
} // namespace pipeline } // namespace pipeline
struct CodeActionParam { struct CodeActionParam {

View File

@ -382,7 +382,7 @@ void do_initialize(MessageHandler *m, InitializeParam &param,
// may take a long time. Indexer threads will emit status/progress // may take a long time. Indexer threads will emit status/progress
// reports. // reports.
if (g_config->index.threads == 0) 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"; LOG_S(INFO) << "start " << g_config->index.threads << " indexers";
for (int i = 0; i < g_config->index.threads; i++) for (int i = 0; i < g_config->index.threads; i++)

View File

@ -44,8 +44,8 @@ std::vector<TextEdit> replacementsToEdits(std::string_view code,
const tooling::Replacements &repls) { const tooling::Replacements &repls) {
std::vector<TextEdit> ret; std::vector<TextEdit> ret;
int i = 0, line = 0, col = 0; int i = 0, line = 0, col = 0;
auto move = [&](int p) { auto move = [&](unsigned p) {
for (; i < p; i++) for (; i < (int)p; i++)
if (code[i] == '\n') if (code[i] == '\n')
line++, col = 0; line++, col = 0;
else { else {

View File

@ -101,7 +101,7 @@ void MessageHandler::workspace_didChangeWorkspaceFolders(
if (folder == real) if (folder == real)
real.clear(); real.clear();
LOG_S(INFO) << "add workspace folder " << wf.name << ": " LOG_S(INFO) << "add workspace folder " << wf.name << ": "
<< (real.empty() ? folder : folder + " -> " + real); << (real.empty() ? folder : (folder + " -> ").append(real));
workspaceFolders.emplace_back(); workspaceFolders.emplace_back();
auto it = workspaceFolders.end() - 1; auto it = workspaceFolders.end() - 1;
for (; it != workspaceFolders.begin() && folder < it[-1].first; --it) for (; it != workspaceFolders.begin() && folder < it[-1].first; --it)

View File

@ -616,13 +616,13 @@ void mainLoop() {
SemaManager manager( SemaManager manager(
&project, &wfiles, &project, &wfiles,
[&](std::string path, std::vector<Diagnostic> diagnostics) { [](const std::string &path, std::vector<Diagnostic> diagnostics) {
PublishDiagnosticParam params; PublishDiagnosticParam params;
params.uri = DocumentUri::fromPath(path); params.uri = DocumentUri::fromPath(path);
params.diagnostics = diagnostics; params.diagnostics = std::move(diagnostics);
notify("textDocument/publishDiagnostics", params); notify("textDocument/publishDiagnostics", params);
}, },
[](RequestId id) { [](const RequestId &id) {
if (id.valid()) { if (id.valid()) {
ResponseError err; ResponseError err;
err.code = ErrorCode::InternalError; err.code = ErrorCode::InternalError;
@ -717,8 +717,9 @@ void standalone(const std::string &root) {
WorkingFiles wfiles; WorkingFiles wfiles;
VFS vfs; VFS vfs;
SemaManager manager( SemaManager manager(
nullptr, nullptr, [&](std::string, std::vector<Diagnostic>) {}, nullptr, nullptr,
[](RequestId id) {}); [](const std::string &, const std::vector<Diagnostic> &) {},
[](const RequestId &id) {});
IncludeComplete complete(&project); IncludeComplete complete(&project);
MessageHandler handler; MessageHandler handler;
@ -756,7 +757,7 @@ void standalone(const std::string &root) {
void index(const std::string &path, const std::vector<const char *> &args, void index(const std::string &path, const std::vector<const char *> &args,
IndexMode mode, bool must_exist, RequestId id) { IndexMode mode, bool must_exist, RequestId id) {
pending_index_requests++; 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); mode != IndexMode::Background);
} }
@ -800,7 +801,7 @@ void notifyOrRequest(const char *method, bool request,
for_stdout->pushBack(output.GetString()); 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<void(JsonWriter &)> &fn) { const std::function<void(JsonWriter &)> &fn) {
rapidjson::StringBuffer output; rapidjson::StringBuffer output;
rapidjson::Writer<rapidjson::StringBuffer> w(output); rapidjson::Writer<rapidjson::StringBuffer> w(output);
@ -813,7 +814,7 @@ static void reply(RequestId id, const char *key,
w.Null(); w.Null();
break; break;
case RequestId::kInt: case RequestId::kInt:
w.Int(atoll(id.value.c_str())); w.Int64(atoll(id.value.c_str()));
break; break;
case RequestId::kString: case RequestId::kString:
w.String(id.value.c_str(), id.value.size()); w.String(id.value.c_str(), id.value.size());
@ -828,11 +829,12 @@ static void reply(RequestId id, const char *key,
for_stdout->pushBack(output.GetString()); for_stdout->pushBack(output.GetString());
} }
void reply(RequestId id, const std::function<void(JsonWriter &)> &fn) { void reply(const RequestId &id, const std::function<void(JsonWriter &)> &fn) {
reply(id, "result", fn); reply(id, "result", fn);
} }
void replyError(RequestId id, const std::function<void(JsonWriter &)> &fn) { void replyError(const RequestId &id,
const std::function<void(JsonWriter &)> &fn) {
reply(id, "error", fn); reply(id, "error", fn);
} }
} // namespace pipeline } // namespace pipeline

View File

@ -65,10 +65,11 @@ template <typename T> void request(const char *method, T &result) {
notifyOrRequest(method, true, [&](JsonWriter &w) { reflect(w, result); }); notifyOrRequest(method, true, [&](JsonWriter &w) { reflect(w, result); });
} }
void reply(RequestId id, const std::function<void(JsonWriter &)> &fn); void reply(const RequestId &id, const std::function<void(JsonWriter &)> &fn);
void replyError(RequestId id, const std::function<void(JsonWriter &)> &fn); void replyError(const RequestId &id,
template <typename T> void replyError(RequestId id, T &result) { const std::function<void(JsonWriter &)> &fn);
template <typename T> void replyError(const RequestId &id, T &result) {
replyError(id, [&](JsonWriter &w) { reflect(w, result); }); replyError(id, [&](JsonWriter &w) { reflect(w, result); });
} }
} // namespace pipeline } // namespace pipeline

View File

@ -249,7 +249,7 @@ bool appendToCDB(const std::vector<const char *> &args) {
return args.size() && StringRef("%compile_commands.json") == args[0]; return args.size() && StringRef("%compile_commands.json") == args[0];
} }
std::vector<const char *> getFallback(const std::string path) { std::vector<const char *> getFallback(const std::string &path) {
std::vector<const char *> argv{"clang"}; std::vector<const char *> argv{"clang"};
if (sys::path::extension(path) == ".h") if (sys::path::extension(path) == ".h")
argv.push_back("-xobjective-c++-header"); argv.push_back("-xobjective-c++-header");
@ -607,7 +607,7 @@ Project::Entry Project::findEntry(const std::string &path, bool can_redirect,
return ret; return ret;
} }
void Project::index(WorkingFiles *wfiles, RequestId id) { void Project::index(WorkingFiles *wfiles, const RequestId &id) {
auto &gi = g_config->index; auto &gi = g_config->index;
GroupMatch match(gi.whitelist, gi.blacklist), GroupMatch match(gi.whitelist, gi.blacklist),
match_i(gi.initialWhitelist, gi.initialBlacklist); match_i(gi.initialWhitelist, gi.initialBlacklist);

View File

@ -76,7 +76,7 @@ struct Project {
void setArgsForFile(const std::vector<const char *> &args, void setArgsForFile(const std::vector<const char *> &args,
const std::string &path); const std::string &path);
void index(WorkingFiles *wfiles, RequestId id); void index(WorkingFiles *wfiles, const RequestId &id);
void indexRelated(const std::string &path); void indexRelated(const std::string &path);
}; };
} // namespace ccls } // namespace ccls

View File

@ -135,7 +135,7 @@ IndexUpdate IndexUpdate::createDelta(IndexFile *previous, IndexFile *current) {
previous = &empty; previous = &empty;
r.lid2path = std::move(current->lid2path); r.lid2path = std::move(current->lid2path);
r.funcs_hint = current->usr2func.size() - previous->usr2func.size(); r.funcs_hint = int(current->usr2func.size() - previous->usr2func.size());
for (auto &it : previous->usr2func) { for (auto &it : previous->usr2func) {
auto &func = it.second; auto &func = it.second;
if (func.def.detailed_name[0]) if (func.def.detailed_name[0])
@ -153,7 +153,7 @@ IndexUpdate IndexUpdate::createDelta(IndexFile *previous, IndexFile *current) {
r.funcs_derived[func.usr].second = std::move(func.derived); r.funcs_derived[func.usr].second = std::move(func.derived);
} }
r.types_hint = current->usr2type.size() - previous->usr2type.size(); r.types_hint = int(current->usr2type.size() - previous->usr2type.size());
for (auto &it : previous->usr2type) { for (auto &it : previous->usr2type) {
auto &type = it.second; auto &type = it.second;
if (type.def.detailed_name[0]) if (type.def.detailed_name[0])
@ -173,7 +173,7 @@ IndexUpdate IndexUpdate::createDelta(IndexFile *previous, IndexFile *current) {
r.types_instances[type.usr].second = std::move(type.instances); r.types_instances[type.usr].second = std::move(type.instances);
}; };
r.vars_hint = current->usr2var.size() - previous->usr2var.size(); r.vars_hint = int(current->usr2var.size() - previous->usr2var.size());
for (auto &it : previous->usr2var) { for (auto &it : previous->usr2var) {
auto &var = it.second; auto &var = it.second;
if (var.def.detailed_name[0]) if (var.def.detailed_name[0])

View File

@ -231,7 +231,7 @@ class StoreDiags : public DiagnosticConsumer {
} }
public: public:
StoreDiags(std::string path) : path(path) {} StoreDiags(std::string path) : path(std::move(path)) {}
std::vector<Diag> take() { return std::move(output); } std::vector<Diag> take() { return std::move(output); }
bool isConcerned(const SourceManager &sm, SourceLocation l) { bool isConcerned(const SourceManager &sm, SourceLocation l) {
FileID fid = sm.getFileID(l); FileID fid = sm.getFileID(l);
@ -615,7 +615,7 @@ void *diagnosticMain(void *manager_) {
ret.severity = 1; ret.severity = 1;
break; break;
} }
ret.code = d.category; ret.code = (int)d.category;
return ret; return ret;
}; };
@ -683,8 +683,10 @@ std::shared_ptr<PreambleData> Session::getPreamble() {
SemaManager::SemaManager(Project *project, WorkingFiles *wfiles, SemaManager::SemaManager(Project *project, WorkingFiles *wfiles,
OnDiagnostic on_diagnostic, OnDropped on_dropped) OnDiagnostic on_diagnostic, OnDropped on_dropped)
: project_(project), wfiles(wfiles), on_diagnostic_(on_diagnostic), : project_(project), wfiles(wfiles),
on_dropped_(on_dropped), pch(std::make_shared<PCHContainerOperations>()) { on_diagnostic_(std::move(on_diagnostic)),
on_dropped_(std::move(on_dropped)),
pch(std::make_shared<PCHContainerOperations>()) {
spawnThread(ccls::preambleMain, this); spawnThread(ccls::preambleMain, this);
spawnThread(ccls::completionMain, this); spawnThread(ccls::completionMain, this);
spawnThread(ccls::diagnosticMain, this); spawnThread(ccls::diagnosticMain, this);

View File

@ -25,6 +25,7 @@ limitations under the License.
#include <llvm/ADT/CachedHashString.h> #include <llvm/ADT/CachedHashString.h>
#include <llvm/ADT/DenseSet.h> #include <llvm/ADT/DenseSet.h>
#include <llvm/ADT/STLExtras.h>
#include <mutex> #include <mutex>
#include <stdexcept> #include <stdexcept>
@ -35,7 +36,7 @@ bool gTestOutputMode = false;
namespace ccls { namespace ccls {
void JsonReader::iterArray(std::function<void()> fn) { void JsonReader::iterArray(llvm::function_ref<void()> fn) {
if (!m->IsArray()) if (!m->IsArray())
throw std::invalid_argument("array"); throw std::invalid_argument("array");
// Use "0" to indicate any element for now. // Use "0" to indicate any element for now.
@ -48,7 +49,7 @@ void JsonReader::iterArray(std::function<void()> fn) {
} }
path_.pop_back(); path_.pop_back();
} }
void JsonReader::member(const char *name, std::function<void()> fn) { void JsonReader::member(const char *name, llvm::function_ref<void()> fn) {
path_.push_back(name); path_.push_back(name);
auto it = m->FindMember(name); auto it = m->FindMember(name);
if (it != m->MemberEnd()) { if (it != m->MemberEnd()) {
@ -81,7 +82,7 @@ void JsonWriter::startObject() { m->StartObject(); }
void JsonWriter::endObject() { m->EndObject(); } void JsonWriter::endObject() { m->EndObject(); }
void JsonWriter::key(const char *name) { m->Key(name); } void JsonWriter::key(const char *name) { m->Key(name); }
void JsonWriter::null_() { m->Null(); } 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) { m->String(s); }
void JsonWriter::string(const char *s, size_t len) { m->String(s, len); } void JsonWriter::string(const char *s, size_t len) { m->String(s, len); }

View File

@ -34,6 +34,7 @@ limitations under the License.
namespace llvm { namespace llvm {
class CachedHashStringRef; class CachedHashStringRef;
class StringRef; class StringRef;
template <typename Fn> class function_ref;
} // namespace llvm } // namespace llvm
namespace ccls { namespace ccls {
@ -48,8 +49,8 @@ struct JsonReader {
JsonReader(rapidjson::Value *m) : m(m) {} JsonReader(rapidjson::Value *m) : m(m) {}
void startObject() {} void startObject() {}
void endObject() {} void endObject() {}
void iterArray(std::function<void()> fn); void iterArray(llvm::function_ref<void()> fn);
void member(const char *name, std::function<void()> fn); void member(const char *name, llvm::function_ref<void()> fn);
bool isNull(); bool isNull();
std::string getString(); std::string getString();
std::string getPath() const; std::string getPath() const;
@ -69,7 +70,7 @@ struct JsonWriter {
void endObject(); void endObject();
void key(const char *name); void key(const char *name);
void null_(); void null_();
void int_(int v); void int64(int64_t v);
void string(const char *s); void string(const char *s);
void string(const char *s, size_t len); void string(const char *s, size_t len);
}; };

View File

@ -143,7 +143,7 @@ int alignColumn(const std::string &a, int column, std::string b, bool is_end) {
if (column < head) if (column < head)
return column; return column;
if ((int)a.size() - tail < 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) if (std::max(a.size(), b.size()) - head - tail >= kMaxColumnAlignSize)
return std::min(column, (int)b.size()); return std::min(column, (int)b.size());