Skip to content

Commit

Permalink
cmake: link against zlib; use StringMap
Browse files Browse the repository at this point in the history
  • Loading branch information
MaskRay committed May 16, 2018
1 parent 19d0aad commit ba45e7c
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 82 deletions.
3 changes: 2 additions & 1 deletion cmake/FindClang.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
11 changes: 5 additions & 6 deletions src/import_pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> lock(vfs->mutex);
vfs->state[dep.first].stage = 0;
vfs->state[dep.first().str()].stage = 0;
}
} else
reparse = 2;
Expand All @@ -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);
Expand Down Expand Up @@ -189,7 +189,7 @@ bool Indexer_Parse(DiagnosticsEngine* diag_engine,
if (entry.id >= 0) {
std::lock_guard<std::mutex> 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.
Expand Down Expand Up @@ -388,7 +388,6 @@ void MainLoop(MultiQueueWaiter* querydb_waiter,
}

// Run query db main loop.
SetThreadName("querydb");
auto* queue = QueueManager::instance();
while (true) {
std::vector<std::unique_ptr<InMessage>> messages =
Expand Down
4 changes: 3 additions & 1 deletion src/indexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "symbol.h"
#include "utils.h"

#include <llvm/ADT/StringMap.h>

#include <stdint.h>
#include <algorithm>
#include <optional>
Expand Down Expand Up @@ -297,7 +299,7 @@ struct IndexFile {
std::vector<Range> skipped_by_preprocessor;

std::vector<IndexInclude> includes;
std::unordered_map<std::string, int64_t> dependencies;
llvm::StringMap<int64_t> dependencies;
std::unordered_map<Usr, IndexFunc> usr2func;
std::unordered_map<Usr, IndexType> usr2type;
std::unordered_map<Usr, IndexVar> usr2var;
Expand Down
17 changes: 8 additions & 9 deletions src/message_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/messages/text_document_definition.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/method.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
14 changes: 6 additions & 8 deletions src/project.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <llvm/ADT/ArrayRef.h>
#include <llvm/Option/ArgList.h>
#include <llvm/Option/OptTable.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/LineIterator.h>
using namespace clang;
using namespace llvm;
using namespace llvm::opt;
Expand All @@ -29,7 +31,6 @@ using namespace llvm::opt;
#include <unistd.h>
#endif

#include <fstream>
#include <limits>
#include <unordered_set>
#include <vector>
Expand Down Expand Up @@ -145,14 +146,11 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry(

std::vector<std::string> ReadCompilerArgumentsFromFile(
const std::string& path) {
auto MBOrErr = MemoryBuffer::getFile(path);
if (!MBOrErr) return {};
std::vector<std::string> 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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/query.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 4 additions & 5 deletions src/query.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "indexer.h"
#include "serializer.h"

#include <llvm/ADT/DenseMap.h>
#include <llvm/ADT/SmallVector.h>

struct QueryFile;
Expand Down Expand Up @@ -136,10 +135,10 @@ struct QueryDatabase {
std::vector<SymbolIdx> symbols;

std::vector<QueryFile> files;
std::unordered_map<std::string, int> name2file_id;
llvm::DenseMap<Usr, QueryFunc> usr2func;
llvm::DenseMap<Usr, QueryType> usr2type;
llvm::DenseMap<Usr, QueryVar> usr2var;
llvm::StringMap<int> name2file_id;
std::unordered_map<Usr, QueryFunc> usr2func;
std::unordered_map<Usr, QueryType> usr2type;
std::unordered_map<Usr, QueryVar> usr2var;

// Marks the given Usrs as invalid.
void RemoveUsrs(SymbolKind usr_kind, const std::vector<Usr>& to_remove);
Expand Down
4 changes: 2 additions & 2 deletions src/query_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Maybe<Use> GetDefinitionExtent(QueryDatabase* db, SymbolIdx sym);
// Get defining declaration (if exists) or an arbitrary declaration (otherwise)
// for each id.
template <typename Q>
std::vector<Use> GetDeclarations(llvm::DenseMap<Usr, Q>& usr2entity,
std::vector<Use> GetDeclarations(std::unordered_map<Usr, Q>& usr2entity,
const std::vector<Usr>& usrs) {
std::vector<Use> ret;
ret.reserve(usrs.size());
Expand Down Expand Up @@ -135,7 +135,7 @@ void EachOccurrenceWithParent(QueryDatabase* db,
}

template <typename Q, typename Fn>
void EachDefinedEntity(llvm::DenseMap<Usr, Q>& collection,
void EachDefinedEntity(std::unordered_map<Usr, Q>& collection,
const std::vector<Usr>& usrs,
Fn&& fn) {
for (Usr usr : usrs) {
Expand Down
66 changes: 45 additions & 21 deletions src/serializer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <stdexcept>

using namespace llvm;

bool gTestOutputMode = false;

//// Elementary types
Expand Down Expand Up @@ -149,6 +151,49 @@ void Reflect(Writer& visitor, JsonNull& value) {
visitor.Null();
}

// std::unordered_map
template <typename V>
void Reflect(Reader& visitor, std::unordered_map<Usr, V>& map) {
visitor.IterArray([&](Reader& entry) {
V val;
Reflect(entry, val);
auto usr = val.usr;
map[usr] = std::move(val);
});
}
template <typename V>
void Reflect(Writer& visitor, std::unordered_map<Usr, V>& map) {
std::vector<std::pair<uint64_t, V>> 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<int64_t>& 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<int64_t>& 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();
Expand Down Expand Up @@ -313,27 +358,6 @@ void Reflect(Writer& visitor, SerializeFormat& value) {
}
}

void Reflect(Reader& visitor, std::unordered_map<std::string, int64_t>& 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<std::string, int64_t>& 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: {
Expand Down
26 changes: 0 additions & 26 deletions src/serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <string>
#include <string_view>
#include <type_traits>
#include <unordered_map>
#include <vector>

enum class SerializeFormat { Binary, Json };
Expand Down Expand Up @@ -282,31 +281,6 @@ void Reflect(Writer& visitor, std::vector<T>& values) {
visitor.EndArray();
}

// std::unordered_map
template <typename V>
void Reflect(Reader& visitor, std::unordered_map<uint64_t, V>& map) {
visitor.IterArray([&](Reader& entry) {
V val;
Reflect(entry, val);
auto usr = val.usr;
map[usr] = std::move(val);
});
}
template <typename V>
void Reflect(Writer& visitor, std::unordered_map<uint64_t, V>& map) {
std::vector<std::pair<uint64_t, V>> 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<std::string, int64_t>& map);
void Reflect(Writer& visitor, std::unordered_map<std::string, int64_t>& map);

// ReflectMember

template <typename T>
Expand Down

0 comments on commit ba45e7c

Please sign in to comment.