From ba45e7ca6375f8dbcbedca5e939dc6e1bc101cda Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Mon, 14 May 2018 22:13:18 -0700 Subject: [PATCH] cmake: link against zlib; use StringMap --- cmake/FindClang.cmake | 3 +- src/import_pipeline.cc | 11 ++-- src/indexer.h | 4 +- src/message_handler.cc | 17 +++--- src/messages/text_document_definition.cc | 2 +- src/method.cc | 2 +- src/project.cc | 14 +++-- src/query.cc | 2 +- src/query.h | 9 ++-- src/query_utils.h | 4 +- src/serializer.cc | 66 ++++++++++++++++-------- src/serializer.h | 26 ---------- 12 files changed, 78 insertions(+), 82 deletions(-) diff --git a/cmake/FindClang.cmake b/cmake/FindClang.cmake index cc808ccc..ad085d48 100644 --- a/cmake/FindClang.cmake +++ b/cmake/FindClang.cmake @@ -118,5 +118,6 @@ if(Clang_FOUND AND NOT TARGET Clang::Clang) INTERFACE_INCLUDE_DIRECTORIES "${Clang_INCLUDE_DIR};${Clang_BUILD_INCLUDE_DIR};${LLVM_INCLUDE_DIR};${LLVM_BUILD_INCLUDE_DIR}") find_package(Curses REQUIRED) - set_property(TARGET Clang::Clang PROPERTY IMPORTED_LINK_INTERFACE_LIBRARIES "${_Clang_LIBRARIES};${CURSES_LIBRARIES}") + find_package(ZLIB REQUIRED) + set_property(TARGET Clang::Clang PROPERTY IMPORTED_LINK_INTERFACE_LIBRARIES "${_Clang_LIBRARIES};${CURSES_LIBRARIES};${ZLIB_LIBRARIES}") endif() diff --git a/src/import_pipeline.cc b/src/import_pipeline.cc index 625ba878..5ed8ebd8 100644 --- a/src/import_pipeline.cc +++ b/src/import_pipeline.cc @@ -115,11 +115,11 @@ bool Indexer_Parse(DiagnosticsEngine* diag_engine, path_to_index, entry.args, std::nullopt)) reparse = 2; for (const auto& dep : prev->dependencies) - if (auto write_time1 = LastWriteTime(dep.first)) { + if (auto write_time1 = LastWriteTime(dep.first().str())) { if (dep.second < *write_time1) { reparse = 2; std::lock_guard lock(vfs->mutex); - vfs->state[dep.first].stage = 0; + vfs->state[dep.first().str()].stage = 0; } } else reparse = 2; @@ -134,8 +134,8 @@ bool Indexer_Parse(DiagnosticsEngine* diag_engine, request.is_interactive); } for (const auto& dep : dependencies) - if (vfs->Mark(dep.first, 0, 2)) { - prev = cache.RawCacheLoad(dep.first); + if (vfs->Mark(dep.first().str(), 0, 2)) { + prev = cache.RawCacheLoad(dep.first().str()); IndexUpdate update = IndexUpdate::CreateDelta(nullptr, prev.get()); queue->on_indexed.PushBack(Index_OnIndexed(std::move(update), perf), request.is_interactive); @@ -189,7 +189,7 @@ bool Indexer_Parse(DiagnosticsEngine* diag_engine, if (entry.id >= 0) { std::lock_guard lock(project->mutex_); for (auto& dep : curr->dependencies) - project->absolute_path_to_entry_index_[dep.first] = entry.id; + project->absolute_path_to_entry_index_[dep.first()] = entry.id; } // Build delta update. @@ -388,7 +388,6 @@ void MainLoop(MultiQueueWaiter* querydb_waiter, } // Run query db main loop. - SetThreadName("querydb"); auto* queue = QueueManager::instance(); while (true) { std::vector> messages = diff --git a/src/indexer.h b/src/indexer.h index 12bed98c..55dc00e8 100644 --- a/src/indexer.h +++ b/src/indexer.h @@ -13,6 +13,8 @@ #include "symbol.h" #include "utils.h" +#include + #include #include #include @@ -297,7 +299,7 @@ struct IndexFile { std::vector skipped_by_preprocessor; std::vector includes; - std::unordered_map dependencies; + llvm::StringMap dependencies; std::unordered_map usr2func; std::unordered_map usr2type; std::unordered_map usr2var; diff --git a/src/message_handler.cc b/src/message_handler.cc index a16f59bb..cf17c013 100644 --- a/src/message_handler.cc +++ b/src/message_handler.cc @@ -245,15 +245,14 @@ void EmitSemanticHighlighting(QueryDatabase* db, detailed_name.substr(0, detailed_name.find('<')); int16_t start_line = sym.range.start.line; int16_t start_col = sym.range.start.column; - if (start_line >= 0 && start_line < working_file->index_lines.size()) { - std::string_view line = working_file->index_lines[start_line]; - sym.range.end.line = start_line; - if (start_col + concise_name.size() <= line.size() && - line.compare(start_col, concise_name.size(), concise_name) == 0) - sym.range.end.column = start_col + concise_name.size(); - else - continue; // applies to for loop - } + if (start_line < 0 || start_line >= working_file->index_lines.size()) + continue; + std::string_view line = working_file->index_lines[start_line]; + sym.range.end.line = start_line; + if (!(start_col + concise_name.size() <= line.size() && + line.compare(start_col, concise_name.size(), concise_name) == 0)) + continue; + sym.range.end.column = start_col + concise_name.size(); break; } case SymbolKind::Type: diff --git a/src/messages/text_document_definition.cc b/src/messages/text_document_definition.cc index 103436d1..1d7e545a 100644 --- a/src/messages/text_document_definition.cc +++ b/src/messages/text_document_definition.cc @@ -155,7 +155,7 @@ struct Handler_TextDocumentDefinition auto pos = name.rfind(query); if (pos != std::string::npos) { std::get<0>(score) = int(name.size() - query.size()); - std::get<1>(score) = -pos; + std::get<1>(score) = -int(pos); } if (score < best_score) { best_score = score; diff --git a/src/method.cc b/src/method.cc index f0ba5ce1..38da766a 100644 --- a/src/method.cc +++ b/src/method.cc @@ -12,7 +12,7 @@ MethodType kMethodType_CclsPublishSemanticHighlighting = void Reflect(Reader& visitor, lsRequestId& value) { if (visitor.IsInt64()) { value.type = lsRequestId::kInt; - value.value = visitor.GetInt64(); + value.value = int(visitor.GetInt64()); } else if (visitor.IsInt()) { value.type = lsRequestId::kInt; value.value = visitor.GetInt(); diff --git a/src/project.cc b/src/project.cc index 7f8dfbff..063c9e5a 100644 --- a/src/project.cc +++ b/src/project.cc @@ -16,6 +16,8 @@ #include #include #include +#include +#include using namespace clang; using namespace llvm; using namespace llvm::opt; @@ -29,7 +31,6 @@ using namespace llvm::opt; #include #endif -#include #include #include #include @@ -145,14 +146,11 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry( std::vector ReadCompilerArgumentsFromFile( const std::string& path) { + auto MBOrErr = MemoryBuffer::getFile(path); + if (!MBOrErr) return {}; std::vector args; - std::ifstream fin(path); - for (std::string line; std::getline(fin, line);) { - TrimInPlace(line); - if (line.empty() || StartsWith(line, "#")) - continue; - args.push_back(line); - } + for (line_iterator I(*MBOrErr.get(), true, '#'), E; I != E; ++I) + args.push_back(*I); return args; } diff --git a/src/query.cc b/src/query.cc index 5ed2d77e..59edfe03 100644 --- a/src/query.cc +++ b/src/query.cc @@ -77,7 +77,7 @@ QueryFile::DefUpdate BuildFileDefUpdate(const IndexFile& indexed) { def.inactive_regions = std::move(indexed.skipped_by_preprocessor); def.dependencies.reserve(indexed.dependencies.size()); for (auto& dep : indexed.dependencies) - def.dependencies.push_back(dep.first); + def.dependencies.push_back(dep.first()); def.language = indexed.language; auto add_all_symbols = [&](Use use, Usr usr, SymbolKind kind) { diff --git a/src/query.h b/src/query.h index f85d986b..0186e070 100644 --- a/src/query.h +++ b/src/query.h @@ -3,7 +3,6 @@ #include "indexer.h" #include "serializer.h" -#include #include struct QueryFile; @@ -136,10 +135,10 @@ struct QueryDatabase { std::vector symbols; std::vector files; - std::unordered_map name2file_id; - llvm::DenseMap usr2func; - llvm::DenseMap usr2type; - llvm::DenseMap usr2var; + llvm::StringMap name2file_id; + std::unordered_map usr2func; + std::unordered_map usr2type; + std::unordered_map usr2var; // Marks the given Usrs as invalid. void RemoveUsrs(SymbolKind usr_kind, const std::vector& to_remove); diff --git a/src/query_utils.h b/src/query_utils.h index 3b57ae9d..e76d3212 100644 --- a/src/query_utils.h +++ b/src/query_utils.h @@ -11,7 +11,7 @@ Maybe GetDefinitionExtent(QueryDatabase* db, SymbolIdx sym); // Get defining declaration (if exists) or an arbitrary declaration (otherwise) // for each id. template -std::vector GetDeclarations(llvm::DenseMap& usr2entity, +std::vector GetDeclarations(std::unordered_map& usr2entity, const std::vector& usrs) { std::vector ret; ret.reserve(usrs.size()); @@ -135,7 +135,7 @@ void EachOccurrenceWithParent(QueryDatabase* db, } template -void EachDefinedEntity(llvm::DenseMap& collection, +void EachDefinedEntity(std::unordered_map& collection, const std::vector& usrs, Fn&& fn) { for (Usr usr : usrs) { diff --git a/src/serializer.cc b/src/serializer.cc index b98abbde..734c147a 100644 --- a/src/serializer.cc +++ b/src/serializer.cc @@ -10,6 +10,8 @@ #include +using namespace llvm; + bool gTestOutputMode = false; //// Elementary types @@ -149,6 +151,49 @@ void Reflect(Writer& visitor, JsonNull& value) { visitor.Null(); } +// std::unordered_map +template +void Reflect(Reader& visitor, std::unordered_map& map) { + visitor.IterArray([&](Reader& entry) { + V val; + Reflect(entry, val); + auto usr = val.usr; + map[usr] = std::move(val); + }); +} +template +void Reflect(Writer& visitor, std::unordered_map& map) { + std::vector> xs(map.begin(), map.end()); + std::sort(xs.begin(), xs.end(), + [](const auto& a, const auto& b) { return a.first < b.first; }); + visitor.StartArray(xs.size()); + for (auto& it : xs) + Reflect(visitor, it.second); + visitor.EndArray(); +} + +// Used by IndexFile::dependencies. Timestamps are emitted for Binary. +void Reflect(Reader& visitor, StringMap& map) { + visitor.IterArray([&](Reader& entry) { + std::string name; + Reflect(entry, name); + if (visitor.Format() == SerializeFormat::Binary) + Reflect(entry, map[name]); + else + map[name] = 0; + }); +} +void Reflect(Writer& visitor, StringMap& map) { + visitor.StartArray(map.size()); + for (auto& it : map) { + std::string key = it.first(); + Reflect(visitor, key); + if (visitor.Format() == SerializeFormat::Binary) + Reflect(visitor, it.second); + } + visitor.EndArray(); +} + // TODO: Move this to indexer.cc void Reflect(Reader& visitor, IndexInclude& value) { REFLECT_MEMBER_START(); @@ -313,27 +358,6 @@ void Reflect(Writer& visitor, SerializeFormat& value) { } } -void Reflect(Reader& visitor, std::unordered_map& map) { - visitor.IterArray([&](Reader& entry) { - std::string name; - Reflect(entry, name); - if (visitor.Format() == SerializeFormat::Binary) - Reflect(entry, map[name]); - else - map[name] = 0; - }); -} -void Reflect(Writer& visitor, std::unordered_map& map) { - visitor.StartArray(map.size()); - for (auto& it : map) { - std::string key = it.first; - Reflect(visitor, key); - if (visitor.Format() == SerializeFormat::Binary) - Reflect(visitor, it.second); - } - visitor.EndArray(); -} - std::string Serialize(SerializeFormat format, IndexFile& file) { switch (format) { case SerializeFormat::Binary: { diff --git a/src/serializer.h b/src/serializer.h index 3e1bfe87..cc296c79 100644 --- a/src/serializer.h +++ b/src/serializer.h @@ -13,7 +13,6 @@ #include #include #include -#include #include enum class SerializeFormat { Binary, Json }; @@ -282,31 +281,6 @@ void Reflect(Writer& visitor, std::vector& values) { visitor.EndArray(); } -// std::unordered_map -template -void Reflect(Reader& visitor, std::unordered_map& map) { - visitor.IterArray([&](Reader& entry) { - V val; - Reflect(entry, val); - auto usr = val.usr; - map[usr] = std::move(val); - }); -} -template -void Reflect(Writer& visitor, std::unordered_map& map) { - std::vector> xs(map.begin(), map.end()); - std::sort(xs.begin(), xs.end(), - [](const auto& a, const auto& b) { return a.first < b.first; }); - visitor.StartArray(xs.size()); - for (auto& it : xs) - Reflect(visitor, it.second); - visitor.EndArray(); -} - -// Used by IndexFile::dependencies. Timestamps are emitted for Binary. -void Reflect(Reader& visitor, std::unordered_map& map); -void Reflect(Writer& visitor, std::unordered_map& map); - // ReflectMember template