2017-02-23 09:23:23 +00:00
|
|
|
#include "query.h"
|
|
|
|
|
2017-03-25 20:32:44 +00:00
|
|
|
#include "indexer.h"
|
2018-01-06 21:46:41 +00:00
|
|
|
#include "serializers/json.h"
|
2017-03-25 20:32:44 +00:00
|
|
|
|
2017-05-15 03:51:53 +00:00
|
|
|
#include <doctest/doctest.h>
|
2017-09-22 01:14:57 +00:00
|
|
|
#include <optional.h>
|
2017-07-30 04:24:02 +00:00
|
|
|
#include <loguru.hpp>
|
2017-03-25 20:32:44 +00:00
|
|
|
|
2017-04-19 00:05:14 +00:00
|
|
|
#include <cassert>
|
2017-02-20 19:08:27 +00:00
|
|
|
#include <cstdint>
|
2017-02-25 23:59:09 +00:00
|
|
|
#include <functional>
|
2017-02-21 09:08:52 +00:00
|
|
|
#include <iostream>
|
2017-09-22 01:14:57 +00:00
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
2017-02-21 09:08:52 +00:00
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
// TODO: Make all copy constructors explicit.
|
2017-02-26 19:45:59 +00:00
|
|
|
|
2017-04-08 08:17:29 +00:00
|
|
|
namespace {
|
2017-04-07 06:57:26 +00:00
|
|
|
|
2018-01-11 08:32:19 +00:00
|
|
|
template <typename T>
|
|
|
|
void VerifyUnique(const std::vector<T>& values0) {
|
2018-01-20 16:57:41 +00:00
|
|
|
// FIXME: Run on a big code-base for a while and verify no assertions are
|
|
|
|
// triggered.
|
2018-01-11 08:32:19 +00:00
|
|
|
#if false
|
|
|
|
auto values = values0;
|
|
|
|
std::sort(values.begin(), values.end());
|
|
|
|
assert(std::unique(values.begin(), values.end()) == values.end());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-12-12 07:56:50 +00:00
|
|
|
optional<QueryType::Def> ToQuery(const IdMap& id_map,
|
|
|
|
const IndexType::Def& type) {
|
2017-05-22 06:09:09 +00:00
|
|
|
if (type.detailed_name.empty())
|
|
|
|
return nullopt;
|
|
|
|
|
2017-12-12 07:56:50 +00:00
|
|
|
QueryType::Def result;
|
2017-04-15 04:58:07 +00:00
|
|
|
result.detailed_name = type.detailed_name;
|
2018-01-31 07:01:45 +00:00
|
|
|
result.short_name_offset = type.short_name_offset;
|
|
|
|
result.short_name_size = type.short_name_size;
|
2018-01-15 23:51:20 +00:00
|
|
|
result.kind = type.kind;
|
2018-02-11 17:12:22 +00:00
|
|
|
if (!type.hover.empty())
|
|
|
|
result.hover = type.hover;
|
|
|
|
if (!type.comments.empty())
|
|
|
|
result.comments = type.comments;
|
2018-02-08 08:06:04 +00:00
|
|
|
result.file = id_map.primary_file;
|
2018-02-11 04:01:10 +00:00
|
|
|
result.spell = id_map.ToQuery(type.spell);
|
|
|
|
result.extent = id_map.ToQuery(type.extent);
|
2018-02-05 18:12:28 +00:00
|
|
|
result.alias_of = id_map.ToQuery(type.alias_of);
|
|
|
|
result.parents = id_map.ToQuery(type.parents);
|
|
|
|
result.types = id_map.ToQuery(type.types);
|
|
|
|
result.funcs = id_map.ToQuery(type.funcs);
|
|
|
|
result.vars = id_map.ToQuery(type.vars);
|
2017-02-26 19:45:59 +00:00
|
|
|
return result;
|
|
|
|
}
|
2017-04-08 08:17:29 +00:00
|
|
|
|
2017-12-12 07:56:50 +00:00
|
|
|
optional<QueryFunc::Def> ToQuery(const IdMap& id_map,
|
|
|
|
const IndexFunc::Def& func) {
|
2017-05-22 06:09:09 +00:00
|
|
|
if (func.detailed_name.empty())
|
|
|
|
return nullopt;
|
|
|
|
|
2017-12-12 07:56:50 +00:00
|
|
|
QueryFunc::Def result;
|
2017-04-15 04:58:07 +00:00
|
|
|
result.detailed_name = func.detailed_name;
|
2018-01-31 06:39:39 +00:00
|
|
|
result.short_name_offset = func.short_name_offset;
|
|
|
|
result.short_name_size = func.short_name_size;
|
2018-01-15 23:51:20 +00:00
|
|
|
result.kind = func.kind;
|
2018-01-26 17:28:29 +00:00
|
|
|
result.storage = func.storage;
|
2018-02-11 17:12:22 +00:00
|
|
|
if (!func.hover.empty())
|
|
|
|
result.hover = func.hover;
|
|
|
|
if (!func.comments.empty())
|
|
|
|
result.comments = func.comments;
|
2018-02-08 08:06:04 +00:00
|
|
|
result.file = id_map.primary_file;
|
2018-02-11 04:30:27 +00:00
|
|
|
result.spell = id_map.ToQuery(func.spell);
|
|
|
|
result.extent = id_map.ToQuery(func.extent);
|
2018-02-05 18:12:28 +00:00
|
|
|
result.declaring_type = id_map.ToQuery(func.declaring_type);
|
|
|
|
result.base = id_map.ToQuery(func.base);
|
|
|
|
result.locals = id_map.ToQuery(func.locals);
|
2017-04-14 02:17:15 +00:00
|
|
|
result.callees = id_map.ToQuery(func.callees);
|
2017-02-26 19:45:59 +00:00
|
|
|
return result;
|
|
|
|
}
|
2017-04-08 08:17:29 +00:00
|
|
|
|
2017-12-12 07:56:50 +00:00
|
|
|
optional<QueryVar::Def> ToQuery(const IdMap& id_map, const IndexVar::Def& var) {
|
2017-05-22 06:09:09 +00:00
|
|
|
if (var.detailed_name.empty())
|
|
|
|
return nullopt;
|
|
|
|
|
2017-12-12 07:56:50 +00:00
|
|
|
QueryVar::Def result;
|
2017-04-15 04:58:07 +00:00
|
|
|
result.detailed_name = var.detailed_name;
|
2018-01-31 04:59:31 +00:00
|
|
|
result.short_name_offset = var.short_name_offset;
|
|
|
|
result.short_name_size = var.short_name_size;
|
2018-02-11 17:12:22 +00:00
|
|
|
if (!var.hover.empty())
|
|
|
|
result.hover = var.hover;
|
|
|
|
if (!var.comments.empty())
|
|
|
|
result.comments = var.comments;
|
2018-02-08 08:06:04 +00:00
|
|
|
result.file = id_map.primary_file;
|
2018-02-11 17:12:22 +00:00
|
|
|
result.spell = id_map.ToQuery(var.spell);
|
|
|
|
result.extent = id_map.ToQuery(var.extent);
|
2018-02-11 18:25:37 +00:00
|
|
|
result.type = id_map.ToQuery(var.type);
|
2018-01-28 04:25:14 +00:00
|
|
|
result.kind = var.kind;
|
|
|
|
result.storage = var.storage;
|
2017-02-26 19:45:59 +00:00
|
|
|
return result;
|
2017-02-26 08:11:47 +00:00
|
|
|
}
|
2017-02-25 23:59:09 +00:00
|
|
|
|
2017-04-16 08:31:28 +00:00
|
|
|
// Adds the mergeable updates in |source| to |dest|. If a mergeable update for
|
|
|
|
// the destination type already exists, it will be combined. This makes merging
|
|
|
|
// updates take longer but reduces import time on the querydb thread.
|
|
|
|
template <typename TId, typename TValue>
|
|
|
|
void AddMergeableRange(
|
2017-09-22 01:14:57 +00:00
|
|
|
std::vector<MergeableUpdate<TId, TValue>>* dest,
|
2018-02-07 05:45:58 +00:00
|
|
|
std::vector<MergeableUpdate<TId, TValue>>&& source) {
|
2017-04-16 08:31:28 +00:00
|
|
|
// TODO: Consider caching the lookup table. It can probably save even more
|
|
|
|
// time at the cost of some additional memory.
|
|
|
|
|
|
|
|
// Build lookup table.
|
2017-04-19 05:45:37 +00:00
|
|
|
spp::sparse_hash_map<TId, size_t> id_to_index;
|
2017-04-16 08:31:28 +00:00
|
|
|
id_to_index.resize(dest->size());
|
|
|
|
for (size_t i = 0; i < dest->size(); ++i)
|
|
|
|
id_to_index[(*dest)[i].id] = i;
|
|
|
|
|
|
|
|
// Add entries. Try to add them to an existing entry.
|
2018-02-07 05:45:58 +00:00
|
|
|
for (auto& entry : source) {
|
2017-04-16 08:31:28 +00:00
|
|
|
auto it = id_to_index.find(entry.id);
|
|
|
|
if (it != id_to_index.end()) {
|
2018-02-07 05:45:58 +00:00
|
|
|
AddRange(&(*dest)[it->second].to_add, std::move(entry.to_add));
|
|
|
|
AddRange(&(*dest)[it->second].to_remove, std::move(entry.to_remove));
|
2017-09-22 01:14:57 +00:00
|
|
|
} else {
|
2018-02-07 05:45:58 +00:00
|
|
|
dest->push_back(std::move(entry));
|
2017-04-16 08:31:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-11 21:42:48 +00:00
|
|
|
// Compares |previous| and |current|, adding all elements that are in |previous|
|
|
|
|
// but not |current| to |removed|, and all elements that are in |current| but
|
|
|
|
// not |previous| to |added|.
|
2017-04-19 06:56:37 +00:00
|
|
|
//
|
|
|
|
// Returns true iff |removed| or |added| are non-empty.
|
2017-09-22 01:14:57 +00:00
|
|
|
template <typename T>
|
2018-02-07 06:46:05 +00:00
|
|
|
bool ComputeDifferenceForUpdate(std::vector<T>&& previous,
|
|
|
|
std::vector<T>&& current,
|
2017-09-22 01:14:57 +00:00
|
|
|
std::vector<T>* removed,
|
|
|
|
std::vector<T>* added) {
|
2017-04-19 06:56:37 +00:00
|
|
|
// We need to sort to use std::set_difference.
|
|
|
|
std::sort(previous.begin(), previous.end());
|
|
|
|
std::sort(current.begin(), current.end());
|
2017-04-07 07:12:53 +00:00
|
|
|
|
2018-01-01 20:36:08 +00:00
|
|
|
auto it0 = previous.begin(), it1 = current.begin();
|
|
|
|
while (it0 != previous.end() && it1 != current.end()) {
|
|
|
|
// Elements in |previous| that are not in |current|.
|
|
|
|
if (*it0 < *it1)
|
2018-02-07 06:46:05 +00:00
|
|
|
removed->push_back(std::move(*it0++));
|
2018-01-01 20:36:08 +00:00
|
|
|
// Elements in |current| that are not in |previous|.
|
|
|
|
else if (*it1 < *it0)
|
2018-02-07 06:46:05 +00:00
|
|
|
added->push_back(std::move(*it1++));
|
2018-01-01 20:36:08 +00:00
|
|
|
else
|
|
|
|
++it0, ++it1;
|
|
|
|
}
|
|
|
|
while (it0 != previous.end())
|
2018-02-07 06:46:05 +00:00
|
|
|
removed->push_back(std::move(*it0++));
|
2018-01-01 20:36:08 +00:00
|
|
|
while (it1 != current.end())
|
2018-02-07 06:46:05 +00:00
|
|
|
added->push_back(std::move(*it1++));
|
2017-04-07 07:12:53 +00:00
|
|
|
|
2017-04-19 06:56:37 +00:00
|
|
|
return !removed->empty() || !added->empty();
|
|
|
|
}
|
2017-04-07 07:12:53 +00:00
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
template <typename T>
|
|
|
|
void CompareGroups(std::vector<T>& previous_data,
|
|
|
|
std::vector<T>& current_data,
|
|
|
|
std::function<void(T*)> on_removed,
|
|
|
|
std::function<void(T*)> on_added,
|
|
|
|
std::function<void(T*, T*)> on_found) {
|
2017-04-19 06:56:37 +00:00
|
|
|
std::sort(previous_data.begin(), previous_data.end());
|
|
|
|
std::sort(current_data.begin(), current_data.end());
|
2017-04-07 07:12:53 +00:00
|
|
|
|
2017-04-19 06:56:37 +00:00
|
|
|
auto prev_it = previous_data.begin();
|
|
|
|
auto curr_it = current_data.begin();
|
|
|
|
while (prev_it != previous_data.end() && curr_it != current_data.end()) {
|
|
|
|
// same id
|
2017-12-12 07:56:50 +00:00
|
|
|
if (prev_it->usr == curr_it->usr) {
|
2017-04-19 06:56:37 +00:00
|
|
|
on_found(&*prev_it, &*curr_it);
|
|
|
|
++prev_it;
|
|
|
|
++curr_it;
|
|
|
|
}
|
2017-04-07 07:12:53 +00:00
|
|
|
|
2017-04-19 06:56:37 +00:00
|
|
|
// prev_id is smaller - prev_it has data curr_it does not have.
|
2017-12-12 07:56:50 +00:00
|
|
|
else if (prev_it->usr < curr_it->usr) {
|
2017-04-19 06:56:37 +00:00
|
|
|
on_removed(&*prev_it);
|
|
|
|
++prev_it;
|
|
|
|
}
|
2017-04-07 07:12:53 +00:00
|
|
|
|
2017-04-19 06:56:37 +00:00
|
|
|
// prev_id is bigger - curr_it has data prev_it does not have.
|
|
|
|
else {
|
|
|
|
on_added(&*curr_it);
|
|
|
|
++curr_it;
|
|
|
|
}
|
|
|
|
}
|
2017-04-07 07:12:53 +00:00
|
|
|
|
2017-04-19 06:56:37 +00:00
|
|
|
// if prev_it still has data, that means it is not in curr_it and was removed.
|
|
|
|
while (prev_it != previous_data.end()) {
|
|
|
|
on_removed(&*prev_it);
|
|
|
|
++prev_it;
|
|
|
|
}
|
2017-04-07 07:12:53 +00:00
|
|
|
|
2017-04-19 06:56:37 +00:00
|
|
|
// if curr_it still has data, that means it is not in prev_it and was added.
|
|
|
|
while (curr_it != current_data.end()) {
|
|
|
|
on_added(&*curr_it);
|
|
|
|
++curr_it;
|
|
|
|
}
|
|
|
|
}
|
2017-04-07 07:12:53 +00:00
|
|
|
|
2018-01-30 05:34:28 +00:00
|
|
|
QueryFile::DefUpdate BuildFileDefUpdate(const IdMap& id_map, const IndexFile& indexed) {
|
2017-04-15 05:41:35 +00:00
|
|
|
QueryFile::Def def;
|
|
|
|
def.path = indexed.path;
|
2017-05-21 03:46:15 +00:00
|
|
|
def.includes = indexed.includes;
|
2017-10-28 22:09:14 +00:00
|
|
|
def.inactive_regions = indexed.skipped_by_preprocessor;
|
2018-01-27 17:29:28 +00:00
|
|
|
def.dependencies = indexed.dependencies;
|
2017-04-07 05:42:57 +00:00
|
|
|
|
2017-11-30 21:56:55 +00:00
|
|
|
// Convert enum to markdown compatible strings
|
2018-02-08 02:29:34 +00:00
|
|
|
def.language = [&indexed]() {
|
2017-12-01 17:50:39 +00:00
|
|
|
switch (indexed.language) {
|
2017-11-30 21:56:55 +00:00
|
|
|
case LanguageId::C:
|
|
|
|
return "c";
|
|
|
|
case LanguageId::Cpp:
|
|
|
|
return "cpp";
|
|
|
|
case LanguageId::ObjC:
|
|
|
|
return "objectivec";
|
|
|
|
default:
|
|
|
|
return "";
|
2017-12-01 17:50:39 +00:00
|
|
|
}
|
|
|
|
}();
|
2017-11-30 21:56:55 +00:00
|
|
|
|
2018-02-11 04:01:10 +00:00
|
|
|
auto add_outline = [&](Range range, Id<void> id, SymbolKind kind,
|
|
|
|
Role role) {
|
|
|
|
def.outline.push_back(SymbolRef(range, id, kind, role));
|
2017-02-27 07:23:43 +00:00
|
|
|
};
|
2018-02-11 04:01:10 +00:00
|
|
|
auto add_all_symbols = [&](Range range, Id<void> id, SymbolKind kind,
|
|
|
|
Role role) {
|
|
|
|
def.all_symbols.push_back(SymbolRef(range, id, kind, role));
|
|
|
|
};
|
|
|
|
auto add_all_symbols_use = [&](Use use, Id<void> id, SymbolKind kind) {
|
|
|
|
def.all_symbols.push_back(
|
|
|
|
SymbolRef(use.range, id, kind, use.role));
|
|
|
|
};
|
|
|
|
auto add_outline_use = [&](Use use, Id<void> id, SymbolKind kind) {
|
|
|
|
def.outline.push_back(SymbolRef(use.range, id, kind, use.role));
|
2017-04-03 02:21:21 +00:00
|
|
|
};
|
2017-02-27 07:23:43 +00:00
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
for (const IndexType& type : indexed.types) {
|
2018-02-11 04:01:10 +00:00
|
|
|
QueryTypeId id = id_map.ToQuery(type.id);
|
|
|
|
if (type.def.spell)
|
|
|
|
add_all_symbols_use(*type.def.spell, id, SymbolKind::Type);
|
|
|
|
if (type.def.extent)
|
|
|
|
add_outline_use(*type.def.extent, id, SymbolKind::Type);
|
|
|
|
for (Use use : type.uses)
|
2018-02-09 17:42:10 +00:00
|
|
|
add_all_symbols(use.range, id, SymbolKind::Type, use.role);
|
2017-02-27 07:23:43 +00:00
|
|
|
}
|
2017-05-12 06:08:15 +00:00
|
|
|
for (const IndexFunc& func : indexed.funcs) {
|
2018-02-11 04:01:10 +00:00
|
|
|
QueryFuncId id = id_map.ToQuery(func.id);
|
2018-02-11 21:56:34 +00:00
|
|
|
if (func.def.spell)
|
2018-02-11 04:30:27 +00:00
|
|
|
add_all_symbols_use(*func.def.spell, id, SymbolKind::Func);
|
2018-02-11 21:56:34 +00:00
|
|
|
if (func.def.extent)
|
2018-02-11 04:30:27 +00:00
|
|
|
add_outline_use(*func.def.extent, id, SymbolKind::Func);
|
2017-05-27 21:09:20 +00:00
|
|
|
for (const IndexFunc::Declaration& decl : func.declarations) {
|
2018-02-09 17:42:10 +00:00
|
|
|
add_all_symbols(decl.spelling, id, SymbolKind::Func,
|
2018-02-11 04:01:10 +00:00
|
|
|
Role::Declaration);
|
|
|
|
add_outline(decl.spelling, id, SymbolKind::Func, Role::Declaration);
|
2017-04-03 02:21:21 +00:00
|
|
|
}
|
2018-02-10 20:53:18 +00:00
|
|
|
for (Use caller : func.uses) {
|
2018-01-03 00:30:03 +00:00
|
|
|
// Make ranges of implicit function calls larger (spanning one more column
|
|
|
|
// to the left/right). This is hacky but useful. e.g.
|
2018-01-11 02:43:01 +00:00
|
|
|
// textDocument/definition on the space/semicolon in `A a;` or `return
|
|
|
|
// 42;` will take you to the constructor.
|
2018-02-09 06:06:09 +00:00
|
|
|
Range range = caller.range;
|
2018-02-11 04:01:10 +00:00
|
|
|
if (caller.role & Role::Implicit) {
|
2018-01-14 19:39:29 +00:00
|
|
|
if (range.start.column > 0)
|
2018-01-03 00:30:03 +00:00
|
|
|
range.start.column--;
|
|
|
|
range.end.column++;
|
|
|
|
}
|
2018-02-09 17:42:10 +00:00
|
|
|
add_all_symbols(range, id, SymbolKind::Func,
|
2018-02-11 04:01:10 +00:00
|
|
|
caller.role | Role::Call);
|
2017-05-23 06:57:46 +00:00
|
|
|
}
|
2017-02-27 07:23:43 +00:00
|
|
|
}
|
2017-05-12 06:08:15 +00:00
|
|
|
for (const IndexVar& var : indexed.vars) {
|
2018-02-11 04:01:10 +00:00
|
|
|
QueryVarId id = id_map.ToQuery(var.id);
|
|
|
|
if (var.def.spell)
|
|
|
|
add_all_symbols_use(*var.def.spell, id, SymbolKind::Var);
|
|
|
|
if (var.def.extent)
|
|
|
|
add_outline_use(*var.def.extent, id, SymbolKind::Var);
|
2018-01-27 05:50:17 +00:00
|
|
|
for (const Range& decl : var.declarations) {
|
2018-02-11 04:01:10 +00:00
|
|
|
add_all_symbols(decl, id_map.ToQuery(var.id), SymbolKind::Var,
|
|
|
|
Role::Definition);
|
|
|
|
add_outline(decl, id_map.ToQuery(var.id), SymbolKind::Var,
|
|
|
|
Role::Declaration);
|
2018-01-27 05:50:17 +00:00
|
|
|
}
|
2018-02-11 04:01:10 +00:00
|
|
|
for (Use use : var.uses)
|
|
|
|
add_all_symbols(use.range, id_map.ToQuery(var.id), SymbolKind::Var,
|
2018-02-09 17:42:10 +00:00
|
|
|
use.role);
|
2017-02-27 07:23:43 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
std::sort(def.outline.begin(), def.outline.end(),
|
|
|
|
[](const SymbolRef& a, const SymbolRef& b) {
|
2018-02-09 17:42:10 +00:00
|
|
|
return a.range.start < b.range.start;
|
2017-09-22 01:14:57 +00:00
|
|
|
});
|
|
|
|
std::sort(def.all_symbols.begin(), def.all_symbols.end(),
|
|
|
|
[](const SymbolRef& a, const SymbolRef& b) {
|
2018-02-09 17:42:10 +00:00
|
|
|
return a.range.start < b.range.start;
|
2017-09-22 01:14:57 +00:00
|
|
|
});
|
2017-04-07 05:42:57 +00:00
|
|
|
|
2018-01-30 05:34:28 +00:00
|
|
|
return QueryFile::DefUpdate(def, indexed.file_contents);
|
2017-02-27 07:23:43 +00:00
|
|
|
}
|
|
|
|
|
2018-02-02 07:10:37 +00:00
|
|
|
Maybe<QueryFileId> GetQueryFileIdFromPath(QueryDatabase* query_db,
|
|
|
|
const std::string& path,
|
|
|
|
bool create_if_missing) {
|
2018-01-30 00:55:51 +00:00
|
|
|
NormalizedPath normalized_path(path);
|
|
|
|
auto it = query_db->usr_to_file.find(normalized_path);
|
2017-05-09 04:20:28 +00:00
|
|
|
if (it != query_db->usr_to_file.end())
|
|
|
|
return QueryFileId(it->second.id);
|
2018-01-20 16:57:41 +00:00
|
|
|
if (!create_if_missing)
|
2018-01-20 18:32:39 +00:00
|
|
|
return {};
|
2017-04-07 08:01:58 +00:00
|
|
|
|
2018-02-03 18:33:22 +00:00
|
|
|
RawId idx = query_db->files.size();
|
2018-01-30 00:55:51 +00:00
|
|
|
query_db->usr_to_file[normalized_path] = QueryFileId(idx);
|
2017-04-15 05:41:35 +00:00
|
|
|
query_db->files.push_back(QueryFile(path));
|
2017-04-08 07:11:57 +00:00
|
|
|
return QueryFileId(idx);
|
2017-04-07 08:01:58 +00:00
|
|
|
}
|
|
|
|
|
2018-02-02 07:10:37 +00:00
|
|
|
Maybe<QueryTypeId> GetQueryTypeIdFromUsr(QueryDatabase* query_db,
|
|
|
|
Usr usr,
|
|
|
|
bool create_if_missing) {
|
2017-05-09 04:20:28 +00:00
|
|
|
auto it = query_db->usr_to_type.find(usr);
|
|
|
|
if (it != query_db->usr_to_type.end())
|
|
|
|
return QueryTypeId(it->second.id);
|
2018-01-20 16:57:41 +00:00
|
|
|
if (!create_if_missing)
|
2018-01-20 18:32:39 +00:00
|
|
|
return {};
|
2017-04-07 08:01:58 +00:00
|
|
|
|
2018-02-03 18:33:22 +00:00
|
|
|
RawId idx = query_db->types.size();
|
2017-05-09 04:20:28 +00:00
|
|
|
query_db->usr_to_type[usr] = QueryTypeId(idx);
|
2017-04-15 05:41:35 +00:00
|
|
|
query_db->types.push_back(QueryType(usr));
|
2017-04-08 07:11:57 +00:00
|
|
|
return QueryTypeId(idx);
|
2017-04-07 08:01:58 +00:00
|
|
|
}
|
|
|
|
|
2018-02-02 07:10:37 +00:00
|
|
|
Maybe<QueryFuncId> GetQueryFuncIdFromUsr(QueryDatabase* query_db,
|
|
|
|
Usr usr,
|
|
|
|
bool create_if_missing) {
|
2017-05-09 04:20:28 +00:00
|
|
|
auto it = query_db->usr_to_func.find(usr);
|
|
|
|
if (it != query_db->usr_to_func.end())
|
|
|
|
return QueryFuncId(it->second.id);
|
2018-01-20 16:57:41 +00:00
|
|
|
if (!create_if_missing)
|
2018-01-20 18:32:39 +00:00
|
|
|
return {};
|
|
|
|
|
2018-02-03 18:33:22 +00:00
|
|
|
RawId idx = query_db->funcs.size();
|
2017-05-09 04:20:28 +00:00
|
|
|
query_db->usr_to_func[usr] = QueryFuncId(idx);
|
2017-04-15 05:41:35 +00:00
|
|
|
query_db->funcs.push_back(QueryFunc(usr));
|
2017-04-08 07:11:57 +00:00
|
|
|
return QueryFuncId(idx);
|
2017-04-07 08:01:58 +00:00
|
|
|
}
|
|
|
|
|
2018-02-02 07:10:37 +00:00
|
|
|
Maybe<QueryVarId> GetQueryVarIdFromUsr(QueryDatabase* query_db,
|
|
|
|
Usr usr,
|
|
|
|
bool create_if_missing) {
|
2017-05-09 04:20:28 +00:00
|
|
|
auto it = query_db->usr_to_var.find(usr);
|
|
|
|
if (it != query_db->usr_to_var.end())
|
|
|
|
return QueryVarId(it->second.id);
|
2018-01-20 16:57:41 +00:00
|
|
|
if (!create_if_missing)
|
2018-01-20 18:32:39 +00:00
|
|
|
return {};
|
2017-04-07 08:01:58 +00:00
|
|
|
|
2018-02-03 18:33:22 +00:00
|
|
|
RawId idx = query_db->vars.size();
|
2017-05-09 04:20:28 +00:00
|
|
|
query_db->usr_to_var[usr] = QueryVarId(idx);
|
2017-04-15 05:41:35 +00:00
|
|
|
query_db->vars.push_back(QueryVar(usr));
|
2017-04-08 07:11:57 +00:00
|
|
|
return QueryVarId(idx);
|
2017-04-07 08:01:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 18:32:39 +00:00
|
|
|
} // namespace
|
|
|
|
|
2018-02-02 07:10:37 +00:00
|
|
|
Maybe<QueryFileId> QueryDatabase::GetQueryFileIdFromPath(
|
2018-01-20 16:57:41 +00:00
|
|
|
const std::string& path) {
|
|
|
|
return ::GetQueryFileIdFromPath(this, path, false);
|
|
|
|
}
|
|
|
|
|
2018-02-02 07:10:37 +00:00
|
|
|
Maybe<QueryTypeId> QueryDatabase::GetQueryTypeIdFromUsr(Usr usr) {
|
2018-01-20 16:57:41 +00:00
|
|
|
return ::GetQueryTypeIdFromUsr(this, usr, false);
|
|
|
|
}
|
|
|
|
|
2018-02-02 07:10:37 +00:00
|
|
|
Maybe<QueryFuncId> QueryDatabase::GetQueryFuncIdFromUsr(Usr usr) {
|
2018-01-20 16:57:41 +00:00
|
|
|
return ::GetQueryFuncIdFromUsr(this, usr, false);
|
|
|
|
}
|
|
|
|
|
2018-02-02 07:10:37 +00:00
|
|
|
Maybe<QueryVarId> QueryDatabase::GetQueryVarIdFromUsr(Usr usr) {
|
2018-01-20 16:57:41 +00:00
|
|
|
return ::GetQueryVarIdFromUsr(this, usr, false);
|
|
|
|
}
|
|
|
|
|
2017-04-15 05:41:35 +00:00
|
|
|
IdMap::IdMap(QueryDatabase* query_db, const IdCache& local_ids)
|
2017-09-22 01:14:57 +00:00
|
|
|
: local_ids(local_ids) {
|
|
|
|
// LOG_S(INFO) << "Creating IdMap for " << local_ids.primary_file;
|
2018-01-20 18:32:39 +00:00
|
|
|
primary_file =
|
2018-02-02 07:10:37 +00:00
|
|
|
*GetQueryFileIdFromPath(query_db, local_ids.primary_file, true);
|
2017-04-08 06:45:28 +00:00
|
|
|
|
2017-04-08 07:11:57 +00:00
|
|
|
cached_type_ids_.resize(local_ids.type_id_to_usr.size());
|
2017-04-07 08:01:58 +00:00
|
|
|
for (const auto& entry : local_ids.type_id_to_usr)
|
2017-09-22 01:14:57 +00:00
|
|
|
cached_type_ids_[entry.first] =
|
2018-02-02 07:10:37 +00:00
|
|
|
*GetQueryTypeIdFromUsr(query_db, entry.second, true);
|
2017-04-07 08:01:58 +00:00
|
|
|
|
2017-04-08 07:11:57 +00:00
|
|
|
cached_func_ids_.resize(local_ids.func_id_to_usr.size());
|
2017-04-07 08:01:58 +00:00
|
|
|
for (const auto& entry : local_ids.func_id_to_usr)
|
2017-09-22 01:14:57 +00:00
|
|
|
cached_func_ids_[entry.first] =
|
2018-02-02 07:10:37 +00:00
|
|
|
*GetQueryFuncIdFromUsr(query_db, entry.second, true);
|
2017-04-07 08:01:58 +00:00
|
|
|
|
2017-04-08 07:11:57 +00:00
|
|
|
cached_var_ids_.resize(local_ids.var_id_to_usr.size());
|
2017-04-07 08:01:58 +00:00
|
|
|
for (const auto& entry : local_ids.var_id_to_usr)
|
2018-01-20 16:57:41 +00:00
|
|
|
cached_var_ids_[entry.first] =
|
2018-02-02 07:10:37 +00:00
|
|
|
*GetQueryVarIdFromUsr(query_db, entry.second, true);
|
2017-04-08 07:11:57 +00:00
|
|
|
}
|
|
|
|
|
2018-02-11 04:01:10 +00:00
|
|
|
Use IdMap::ToQuery(Range range, Role role) const {
|
2018-02-10 20:53:18 +00:00
|
|
|
return Use(range, primary_file, SymbolKind:: File, role);
|
2018-02-08 18:38:27 +00:00
|
|
|
}
|
2017-04-08 07:11:57 +00:00
|
|
|
QueryTypeId IdMap::ToQuery(IndexTypeId id) const {
|
2017-04-15 05:55:01 +00:00
|
|
|
assert(cached_type_ids_.find(id) != cached_type_ids_.end());
|
|
|
|
return QueryTypeId(cached_type_ids_.find(id)->second);
|
2017-04-07 08:01:58 +00:00
|
|
|
}
|
2017-04-08 07:11:57 +00:00
|
|
|
QueryFuncId IdMap::ToQuery(IndexFuncId id) const {
|
2018-02-02 07:10:37 +00:00
|
|
|
if (id == IndexFuncId())
|
|
|
|
return QueryFuncId();
|
2017-04-15 05:55:01 +00:00
|
|
|
assert(cached_func_ids_.find(id) != cached_func_ids_.end());
|
|
|
|
return QueryFuncId(cached_func_ids_.find(id)->second);
|
2017-04-08 07:11:57 +00:00
|
|
|
}
|
|
|
|
QueryVarId IdMap::ToQuery(IndexVarId id) const {
|
2017-04-15 05:55:01 +00:00
|
|
|
assert(cached_var_ids_.find(id) != cached_var_ids_.end());
|
|
|
|
return QueryVarId(cached_var_ids_.find(id)->second);
|
2017-04-08 07:11:57 +00:00
|
|
|
}
|
2018-02-10 20:53:18 +00:00
|
|
|
|
|
|
|
Use IdMap::ToQuery(Reference ref) const {
|
2018-02-09 17:42:10 +00:00
|
|
|
switch (ref.kind) {
|
2018-02-10 20:53:18 +00:00
|
|
|
case SymbolKind::Invalid:
|
|
|
|
break;
|
|
|
|
case SymbolKind::File:
|
|
|
|
ref.id = primary_file;
|
|
|
|
break;
|
|
|
|
case SymbolKind::Func:
|
|
|
|
ref.id = ToQuery(IndexFuncId(ref.id));
|
|
|
|
break;
|
|
|
|
case SymbolKind::Type:
|
|
|
|
ref.id = ToQuery(IndexTypeId(ref.id));
|
|
|
|
break;
|
|
|
|
case SymbolKind::Var:
|
|
|
|
ref.id = ToQuery(IndexVarId(ref.id));
|
|
|
|
break;
|
2018-02-09 07:10:54 +00:00
|
|
|
}
|
2018-02-10 20:53:18 +00:00
|
|
|
return ref;
|
2017-04-08 08:04:38 +00:00
|
|
|
}
|
2018-02-10 20:53:18 +00:00
|
|
|
SymbolRef IdMap::ToQuery(SymbolRef ref) const {
|
|
|
|
ref.Reference::operator=(ToQuery(static_cast<Reference>(ref)));
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
Use IdMap::ToQuery(Use use) const {
|
|
|
|
use.Reference::operator=(ToQuery(static_cast<Reference>(use)));
|
|
|
|
return use;
|
|
|
|
}
|
|
|
|
|
|
|
|
Use IdMap::ToQuery(IndexFunc::Declaration decl) const {
|
2017-05-27 21:09:20 +00:00
|
|
|
// TODO: expose more than just QueryLocation.
|
2018-02-10 20:53:18 +00:00
|
|
|
return Use(decl.spelling, primary_file, SymbolKind::File,
|
2018-02-11 04:01:10 +00:00
|
|
|
Role::Declaration);
|
2018-02-08 03:14:44 +00:00
|
|
|
}
|
2018-02-10 20:53:18 +00:00
|
|
|
std::vector<Use> IdMap::ToQuery(const std::vector<Range>& a) const {
|
|
|
|
std::vector<Use> ret;
|
2018-02-08 03:14:44 +00:00
|
|
|
ret.reserve(a.size());
|
|
|
|
for (auto& x : a)
|
2018-02-11 04:01:10 +00:00
|
|
|
ret.push_back(ToQuery(x, Role::Reference));
|
2018-02-08 03:14:44 +00:00
|
|
|
return ret;
|
2017-05-27 21:09:20 +00:00
|
|
|
}
|
2017-04-07 08:01:58 +00:00
|
|
|
|
2017-04-19 06:56:37 +00:00
|
|
|
// ----------------------
|
|
|
|
// INDEX THREAD FUNCTIONS
|
|
|
|
// ----------------------
|
|
|
|
|
2017-04-07 05:42:57 +00:00
|
|
|
// static
|
2017-09-22 01:14:57 +00:00
|
|
|
IndexUpdate IndexUpdate::CreateDelta(const IdMap* previous_id_map,
|
|
|
|
const IdMap* current_id_map,
|
|
|
|
IndexFile* previous,
|
|
|
|
IndexFile* current) {
|
2017-04-19 06:56:37 +00:00
|
|
|
// This function runs on an indexer thread.
|
|
|
|
|
2017-04-08 06:45:28 +00:00
|
|
|
if (!previous_id_map) {
|
|
|
|
assert(!previous);
|
2018-01-30 05:34:28 +00:00
|
|
|
IndexFile empty(current->path, "<empty>");
|
2017-05-22 06:09:09 +00:00
|
|
|
return IndexUpdate(*current_id_map, *current_id_map, empty, *current);
|
2017-04-08 06:45:28 +00:00
|
|
|
}
|
|
|
|
return IndexUpdate(*previous_id_map, *current_id_map, *previous, *current);
|
2017-04-07 05:42:57 +00:00
|
|
|
}
|
2017-02-27 07:23:43 +00:00
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
IndexUpdate::IndexUpdate(const IdMap& previous_id_map,
|
|
|
|
const IdMap& current_id_map,
|
|
|
|
IndexFile& previous_file,
|
|
|
|
IndexFile& current_file) {
|
|
|
|
// This function runs on an indexer thread.
|
|
|
|
|
|
|
|
// |query_name| is the name of the variable on the query type.
|
|
|
|
// |index_name| is the name of the variable on the index type.
|
|
|
|
// |type| is the type of the variable.
|
2018-02-07 06:46:05 +00:00
|
|
|
#define PROCESS_UPDATE_DIFF(type_id, query_name, index_name, type) \
|
|
|
|
{ \
|
|
|
|
/* Check for changes. */ \
|
|
|
|
std::vector<type> removed, added; \
|
|
|
|
auto query_previous = previous_id_map.ToQuery(previous->index_name); \
|
|
|
|
auto query_current = current_id_map.ToQuery(current->index_name); \
|
|
|
|
bool did_add = ComputeDifferenceForUpdate(std::move(query_previous), \
|
|
|
|
std::move(query_current), \
|
|
|
|
&removed, &added); \
|
|
|
|
if (did_add) { \
|
|
|
|
query_name.push_back(MergeableUpdate<type_id, type>( \
|
|
|
|
current_id_map.ToQuery(current->id), std::move(added), \
|
|
|
|
std::move(removed))); \
|
|
|
|
} \
|
2017-04-08 07:52:57 +00:00
|
|
|
}
|
2017-02-27 07:23:43 +00:00
|
|
|
// File
|
2018-01-30 05:34:28 +00:00
|
|
|
files_def_update.push_back(BuildFileDefUpdate(current_id_map, current_file));
|
2017-02-25 23:59:09 +00:00
|
|
|
|
2017-05-25 00:18:04 +00:00
|
|
|
// **NOTE** We only remove entries if they were defined in the previous index.
|
|
|
|
// For example, if a type is included from another file it will be defined
|
|
|
|
// simply so we can attribute the usage/reference to it. If the reference goes
|
|
|
|
// away we don't want to remove the type/func/var usage.
|
|
|
|
|
2017-02-25 23:59:09 +00:00
|
|
|
// Types
|
2017-09-22 01:14:57 +00:00
|
|
|
CompareGroups<IndexType>(
|
|
|
|
previous_file.types, current_file.types,
|
|
|
|
/*onRemoved:*/
|
|
|
|
[this, &previous_id_map](IndexType* type) {
|
2018-02-11 04:01:10 +00:00
|
|
|
if (type->def.spell)
|
2017-12-12 07:56:50 +00:00
|
|
|
types_removed.push_back(type->usr);
|
2017-09-22 01:14:57 +00:00
|
|
|
else {
|
|
|
|
if (!type->derived.empty())
|
|
|
|
types_derived.push_back(QueryType::DerivedUpdate(
|
|
|
|
previous_id_map.ToQuery(type->id), {},
|
|
|
|
previous_id_map.ToQuery(type->derived)));
|
|
|
|
if (!type->instances.empty())
|
|
|
|
types_instances.push_back(QueryType::InstancesUpdate(
|
|
|
|
previous_id_map.ToQuery(type->id), {},
|
|
|
|
previous_id_map.ToQuery(type->instances)));
|
|
|
|
if (!type->uses.empty())
|
|
|
|
types_uses.push_back(
|
|
|
|
QueryType::UsesUpdate(previous_id_map.ToQuery(type->id), {},
|
|
|
|
previous_id_map.ToQuery(type->uses)));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/*onAdded:*/
|
|
|
|
[this, ¤t_id_map](IndexType* type) {
|
2017-12-12 07:56:50 +00:00
|
|
|
optional<QueryType::Def> def_update =
|
2017-09-22 01:14:57 +00:00
|
|
|
ToQuery(current_id_map, type->def);
|
|
|
|
if (def_update)
|
2017-12-12 07:56:50 +00:00
|
|
|
types_def_update.push_back(
|
2018-02-07 06:46:05 +00:00
|
|
|
QueryType::DefUpdate(type->usr, std::move(*def_update)));
|
2017-09-22 01:14:57 +00:00
|
|
|
if (!type->derived.empty())
|
|
|
|
types_derived.push_back(
|
|
|
|
QueryType::DerivedUpdate(current_id_map.ToQuery(type->id),
|
|
|
|
current_id_map.ToQuery(type->derived)));
|
|
|
|
if (!type->instances.empty())
|
|
|
|
types_instances.push_back(QueryType::InstancesUpdate(
|
|
|
|
current_id_map.ToQuery(type->id),
|
|
|
|
current_id_map.ToQuery(type->instances)));
|
|
|
|
if (!type->uses.empty())
|
|
|
|
types_uses.push_back(
|
|
|
|
QueryType::UsesUpdate(current_id_map.ToQuery(type->id),
|
|
|
|
current_id_map.ToQuery(type->uses)));
|
|
|
|
},
|
|
|
|
/*onFound:*/
|
2017-12-12 07:56:50 +00:00
|
|
|
[this, &previous_id_map, ¤t_id_map](IndexType* previous,
|
|
|
|
IndexType* current) {
|
|
|
|
optional<QueryType::Def> previous_remapped_def =
|
|
|
|
ToQuery(previous_id_map, previous->def);
|
|
|
|
optional<QueryType::Def> current_remapped_def =
|
|
|
|
ToQuery(current_id_map, current->def);
|
2017-09-22 01:14:57 +00:00
|
|
|
if (current_remapped_def &&
|
|
|
|
previous_remapped_def != current_remapped_def &&
|
2017-12-12 07:56:50 +00:00
|
|
|
!current_remapped_def->detailed_name.empty()) {
|
2018-02-07 06:46:05 +00:00
|
|
|
types_def_update.push_back(QueryType::DefUpdate(
|
|
|
|
current->usr, std::move(*current_remapped_def)));
|
2017-12-12 07:56:50 +00:00
|
|
|
}
|
2017-09-22 01:14:57 +00:00
|
|
|
|
|
|
|
PROCESS_UPDATE_DIFF(QueryTypeId, types_derived, derived, QueryTypeId);
|
|
|
|
PROCESS_UPDATE_DIFF(QueryTypeId, types_instances, instances,
|
|
|
|
QueryVarId);
|
2018-02-10 20:53:18 +00:00
|
|
|
PROCESS_UPDATE_DIFF(QueryTypeId, types_uses, uses, Use);
|
2017-09-22 01:14:57 +00:00
|
|
|
});
|
2017-02-25 23:59:09 +00:00
|
|
|
|
|
|
|
// Functions
|
2017-09-22 01:14:57 +00:00
|
|
|
CompareGroups<IndexFunc>(
|
|
|
|
previous_file.funcs, current_file.funcs,
|
|
|
|
/*onRemoved:*/
|
|
|
|
[this, &previous_id_map](IndexFunc* func) {
|
2018-02-11 04:01:10 +00:00
|
|
|
if (func->def.spell) {
|
2017-12-12 07:56:50 +00:00
|
|
|
funcs_removed.push_back(func->usr);
|
2017-09-22 01:14:57 +00:00
|
|
|
} else {
|
|
|
|
if (!func->declarations.empty())
|
|
|
|
funcs_declarations.push_back(QueryFunc::DeclarationsUpdate(
|
|
|
|
previous_id_map.ToQuery(func->id), {},
|
|
|
|
previous_id_map.ToQuery(func->declarations)));
|
|
|
|
if (!func->derived.empty())
|
|
|
|
funcs_derived.push_back(QueryFunc::DerivedUpdate(
|
|
|
|
previous_id_map.ToQuery(func->id), {},
|
|
|
|
previous_id_map.ToQuery(func->derived)));
|
2018-02-10 20:53:18 +00:00
|
|
|
if (!func->uses.empty())
|
|
|
|
funcs_uses.push_back(QueryFunc::UsesUpdate(
|
2017-09-22 01:14:57 +00:00
|
|
|
previous_id_map.ToQuery(func->id), {},
|
2018-02-10 20:53:18 +00:00
|
|
|
previous_id_map.ToQuery(func->uses)));
|
2017-09-22 01:14:57 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
/*onAdded:*/
|
|
|
|
[this, ¤t_id_map](IndexFunc* func) {
|
2017-12-12 07:56:50 +00:00
|
|
|
optional<QueryFunc::Def> def_update =
|
2017-09-22 01:14:57 +00:00
|
|
|
ToQuery(current_id_map, func->def);
|
|
|
|
if (def_update)
|
2017-12-12 07:56:50 +00:00
|
|
|
funcs_def_update.push_back(
|
2018-02-07 06:46:05 +00:00
|
|
|
QueryFunc::DefUpdate(func->usr, std::move(*def_update)));
|
2017-09-22 01:14:57 +00:00
|
|
|
if (!func->declarations.empty())
|
|
|
|
funcs_declarations.push_back(QueryFunc::DeclarationsUpdate(
|
|
|
|
current_id_map.ToQuery(func->id),
|
|
|
|
current_id_map.ToQuery(func->declarations)));
|
|
|
|
if (!func->derived.empty())
|
|
|
|
funcs_derived.push_back(
|
|
|
|
QueryFunc::DerivedUpdate(current_id_map.ToQuery(func->id),
|
|
|
|
current_id_map.ToQuery(func->derived)));
|
2018-02-10 20:53:18 +00:00
|
|
|
if (!func->uses.empty())
|
|
|
|
funcs_uses.push_back(
|
|
|
|
QueryFunc::UsesUpdate(current_id_map.ToQuery(func->id),
|
|
|
|
current_id_map.ToQuery(func->uses)));
|
2017-09-22 01:14:57 +00:00
|
|
|
},
|
|
|
|
/*onFound:*/
|
2017-12-12 07:56:50 +00:00
|
|
|
[this, &previous_id_map, ¤t_id_map](IndexFunc* previous,
|
|
|
|
IndexFunc* current) {
|
|
|
|
optional<QueryFunc::Def> previous_remapped_def =
|
|
|
|
ToQuery(previous_id_map, previous->def);
|
|
|
|
optional<QueryFunc::Def> current_remapped_def =
|
|
|
|
ToQuery(current_id_map, current->def);
|
2017-09-22 01:14:57 +00:00
|
|
|
if (current_remapped_def &&
|
|
|
|
previous_remapped_def != current_remapped_def &&
|
2017-12-12 07:56:50 +00:00
|
|
|
!current_remapped_def->detailed_name.empty()) {
|
2018-02-07 06:46:05 +00:00
|
|
|
funcs_def_update.push_back(QueryFunc::DefUpdate(
|
|
|
|
current->usr, std::move(*current_remapped_def)));
|
2017-12-12 07:56:50 +00:00
|
|
|
}
|
2017-09-22 01:14:57 +00:00
|
|
|
|
|
|
|
PROCESS_UPDATE_DIFF(QueryFuncId, funcs_declarations, declarations,
|
2018-02-10 20:53:18 +00:00
|
|
|
Use);
|
2017-09-22 01:14:57 +00:00
|
|
|
PROCESS_UPDATE_DIFF(QueryFuncId, funcs_derived, derived, QueryFuncId);
|
2018-02-10 20:53:18 +00:00
|
|
|
PROCESS_UPDATE_DIFF(QueryFuncId, funcs_uses, uses, Use);
|
2017-09-22 01:14:57 +00:00
|
|
|
});
|
2017-02-25 23:59:09 +00:00
|
|
|
|
|
|
|
// Variables
|
2017-09-22 01:14:57 +00:00
|
|
|
CompareGroups<IndexVar>(
|
|
|
|
previous_file.vars, current_file.vars,
|
|
|
|
/*onRemoved:*/
|
|
|
|
[this, &previous_id_map](IndexVar* var) {
|
2018-02-11 04:01:10 +00:00
|
|
|
if (var->def.spell) {
|
2017-12-12 07:56:50 +00:00
|
|
|
vars_removed.push_back(var->usr);
|
2017-09-22 01:14:57 +00:00
|
|
|
} else {
|
2018-01-27 05:50:17 +00:00
|
|
|
if (!var->declarations.empty())
|
|
|
|
vars_declarations.push_back(QueryVar::DeclarationsUpdate(
|
|
|
|
previous_id_map.ToQuery(var->id), {},
|
|
|
|
previous_id_map.ToQuery(var->declarations)));
|
2017-09-22 01:14:57 +00:00
|
|
|
if (!var->uses.empty())
|
|
|
|
vars_uses.push_back(
|
|
|
|
QueryVar::UsesUpdate(previous_id_map.ToQuery(var->id), {},
|
|
|
|
previous_id_map.ToQuery(var->uses)));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/*onAdded:*/
|
|
|
|
[this, ¤t_id_map](IndexVar* var) {
|
2017-12-12 07:56:50 +00:00
|
|
|
optional<QueryVar::Def> def_update = ToQuery(current_id_map, var->def);
|
2017-09-22 01:14:57 +00:00
|
|
|
if (def_update)
|
2018-02-07 06:46:05 +00:00
|
|
|
vars_def_update.push_back(
|
|
|
|
QueryVar::DefUpdate(var->usr, std::move(*def_update)));
|
2018-01-27 05:50:17 +00:00
|
|
|
if (!var->declarations.empty())
|
|
|
|
vars_declarations.push_back(QueryVar::DeclarationsUpdate(
|
|
|
|
current_id_map.ToQuery(var->id),
|
|
|
|
current_id_map.ToQuery(var->declarations)));
|
2017-09-22 01:14:57 +00:00
|
|
|
if (!var->uses.empty())
|
|
|
|
vars_uses.push_back(
|
|
|
|
QueryVar::UsesUpdate(current_id_map.ToQuery(var->id),
|
|
|
|
current_id_map.ToQuery(var->uses)));
|
|
|
|
},
|
|
|
|
/*onFound:*/
|
2017-12-12 07:56:50 +00:00
|
|
|
[this, &previous_id_map, ¤t_id_map](IndexVar* previous,
|
|
|
|
IndexVar* current) {
|
|
|
|
optional<QueryVar::Def> previous_remapped_def =
|
|
|
|
ToQuery(previous_id_map, previous->def);
|
|
|
|
optional<QueryVar::Def> current_remapped_def =
|
|
|
|
ToQuery(current_id_map, current->def);
|
2017-09-22 01:14:57 +00:00
|
|
|
if (current_remapped_def &&
|
|
|
|
previous_remapped_def != current_remapped_def &&
|
|
|
|
!current_remapped_def->detailed_name.empty())
|
2018-02-07 06:46:05 +00:00
|
|
|
vars_def_update.push_back(QueryVar::DefUpdate(
|
|
|
|
current->usr, std::move(*current_remapped_def)));
|
2017-09-22 01:14:57 +00:00
|
|
|
|
2018-02-10 20:53:18 +00:00
|
|
|
PROCESS_UPDATE_DIFF(QueryVarId, vars_declarations, declarations, Use);
|
|
|
|
PROCESS_UPDATE_DIFF(QueryVarId, vars_uses, uses, Use);
|
2017-09-22 01:14:57 +00:00
|
|
|
});
|
2017-02-25 23:59:09 +00:00
|
|
|
|
|
|
|
#undef PROCESS_UPDATE_DIFF
|
|
|
|
}
|
2017-02-21 09:08:52 +00:00
|
|
|
|
2017-12-12 05:21:03 +00:00
|
|
|
// This function runs on an indexer thread.
|
2018-02-06 07:22:44 +00:00
|
|
|
void IndexUpdate::Merge(IndexUpdate&& update) {
|
2018-02-07 05:45:58 +00:00
|
|
|
#define INDEX_UPDATE_APPEND(name) AddRange(&name, std::move(update.name));
|
|
|
|
#define INDEX_UPDATE_MERGE(name) AddMergeableRange(&name, std::move(update.name));
|
2017-02-27 07:23:43 +00:00
|
|
|
|
2017-04-16 08:31:28 +00:00
|
|
|
INDEX_UPDATE_APPEND(files_removed);
|
|
|
|
INDEX_UPDATE_APPEND(files_def_update);
|
2017-02-27 07:23:43 +00:00
|
|
|
|
2017-04-16 08:31:28 +00:00
|
|
|
INDEX_UPDATE_APPEND(types_removed);
|
|
|
|
INDEX_UPDATE_APPEND(types_def_update);
|
2017-02-27 07:23:43 +00:00
|
|
|
INDEX_UPDATE_MERGE(types_derived);
|
2017-04-21 07:03:33 +00:00
|
|
|
INDEX_UPDATE_MERGE(types_instances);
|
2017-02-27 07:23:43 +00:00
|
|
|
INDEX_UPDATE_MERGE(types_uses);
|
|
|
|
|
2017-04-16 08:31:28 +00:00
|
|
|
INDEX_UPDATE_APPEND(funcs_removed);
|
|
|
|
INDEX_UPDATE_APPEND(funcs_def_update);
|
2017-02-27 07:23:43 +00:00
|
|
|
INDEX_UPDATE_MERGE(funcs_declarations);
|
|
|
|
INDEX_UPDATE_MERGE(funcs_derived);
|
2018-02-10 20:53:18 +00:00
|
|
|
INDEX_UPDATE_MERGE(funcs_uses);
|
2017-02-27 07:23:43 +00:00
|
|
|
|
2017-04-16 08:31:28 +00:00
|
|
|
INDEX_UPDATE_APPEND(vars_removed);
|
|
|
|
INDEX_UPDATE_APPEND(vars_def_update);
|
2018-01-27 05:50:17 +00:00
|
|
|
INDEX_UPDATE_MERGE(vars_declarations);
|
2017-02-27 07:23:43 +00:00
|
|
|
INDEX_UPDATE_MERGE(vars_uses);
|
2017-02-26 19:45:59 +00:00
|
|
|
|
2017-04-16 08:31:28 +00:00
|
|
|
#undef INDEX_UPDATE_APPEND
|
2017-02-27 07:23:43 +00:00
|
|
|
#undef INDEX_UPDATE_MERGE
|
|
|
|
}
|
2017-02-26 19:45:59 +00:00
|
|
|
|
2017-05-19 07:02:01 +00:00
|
|
|
std::string IndexUpdate::ToString() {
|
|
|
|
rapidjson::StringBuffer output;
|
2018-01-06 21:46:41 +00:00
|
|
|
rapidjson::Writer<rapidjson::StringBuffer> writer;
|
|
|
|
JsonWriter json_writer(&writer);
|
2017-05-19 07:02:01 +00:00
|
|
|
IndexUpdate& update = *this;
|
2018-01-06 21:46:41 +00:00
|
|
|
Reflect(json_writer, update);
|
2017-05-19 07:02:01 +00:00
|
|
|
return output.GetString();
|
|
|
|
}
|
2017-02-26 19:45:59 +00:00
|
|
|
|
2018-01-30 00:55:51 +00:00
|
|
|
NormalizedPath::NormalizedPath(const std::string& path)
|
|
|
|
: path(LowerPathIfCaseInsensitive(path)) {}
|
|
|
|
|
|
|
|
bool NormalizedPath::operator==(const NormalizedPath& rhs) const {
|
|
|
|
return path == rhs.path;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NormalizedPath::operator!=(const NormalizedPath& rhs) const {
|
|
|
|
return path != rhs.path;
|
|
|
|
}
|
|
|
|
|
2017-04-19 06:56:37 +00:00
|
|
|
// ------------------------
|
|
|
|
// QUERYDB THREAD FUNCTIONS
|
|
|
|
// ------------------------
|
2017-02-26 08:11:47 +00:00
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
void QueryDatabase::RemoveUsrs(SymbolKind usr_kind,
|
|
|
|
const std::vector<Usr>& to_remove) {
|
2017-04-19 06:56:37 +00:00
|
|
|
// This function runs on the querydb thread.
|
|
|
|
|
2017-04-22 07:32:29 +00:00
|
|
|
// When we remove an element, we just erase the state from the storage. We do
|
|
|
|
// not update array indices because that would take a huge amount of time for
|
|
|
|
// a very large index.
|
2017-04-19 06:56:37 +00:00
|
|
|
//
|
2017-04-22 07:32:29 +00:00
|
|
|
// There means that there is some memory growth that will never be reclaimed,
|
|
|
|
// but it should be pretty minimal and is solved by simply restarting the
|
|
|
|
// indexer and loading from cache, which is a fast operation.
|
|
|
|
//
|
|
|
|
// TODO: Add "cquery: Reload Index" command which unloads all querydb state
|
|
|
|
// and fully reloads from cache. This will address the memory leak above.
|
|
|
|
|
2017-05-09 04:20:28 +00:00
|
|
|
switch (usr_kind) {
|
|
|
|
case SymbolKind::Type: {
|
2018-02-02 01:24:46 +00:00
|
|
|
for (const Usr& usr : to_remove) {
|
|
|
|
QueryType& type = types[usr_to_type[usr].id];
|
2018-02-02 06:47:22 +00:00
|
|
|
if (type.symbol_idx)
|
|
|
|
symbols[type.symbol_idx->id].kind = SymbolKind::Invalid;
|
2018-02-04 02:34:07 +00:00
|
|
|
//type.def = QueryType::Def();
|
2018-02-02 01:24:46 +00:00
|
|
|
type.def = nullopt;
|
|
|
|
}
|
2017-05-09 04:20:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SymbolKind::Func: {
|
2018-02-02 01:24:46 +00:00
|
|
|
for (const Usr& usr : to_remove) {
|
|
|
|
QueryFunc& func = funcs[usr_to_func[usr].id];
|
2018-02-02 06:47:22 +00:00
|
|
|
if (func.symbol_idx)
|
|
|
|
symbols[func.symbol_idx->id].kind = SymbolKind::Invalid;
|
2018-02-04 02:34:07 +00:00
|
|
|
//func.def = QueryFunc::Def();
|
2018-02-02 01:24:46 +00:00
|
|
|
func.def = nullopt;
|
|
|
|
}
|
2017-05-09 04:20:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SymbolKind::Var: {
|
2018-02-02 01:24:46 +00:00
|
|
|
for (const Usr& usr : to_remove) {
|
|
|
|
QueryVar& var = vars[usr_to_var[usr].id];
|
2018-02-02 06:47:22 +00:00
|
|
|
if (var.symbol_idx)
|
|
|
|
symbols[var.symbol_idx->id].kind = SymbolKind::Invalid;
|
2018-02-04 02:34:07 +00:00
|
|
|
//var.def = QueryVar::Def();
|
2018-02-02 01:24:46 +00:00
|
|
|
var.def = nullopt;
|
|
|
|
}
|
2017-05-09 04:20:28 +00:00
|
|
|
break;
|
2017-04-22 07:32:29 +00:00
|
|
|
}
|
2018-01-26 06:42:58 +00:00
|
|
|
case SymbolKind::File:
|
2017-05-21 23:48:21 +00:00
|
|
|
case SymbolKind::Invalid:
|
|
|
|
break;
|
2017-04-22 07:32:29 +00:00
|
|
|
}
|
2017-04-19 06:56:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void QueryDatabase::ApplyIndexUpdate(IndexUpdate* update) {
|
2017-09-22 01:14:57 +00:00
|
|
|
// This function runs on the querydb thread.
|
|
|
|
|
|
|
|
// Example types:
|
|
|
|
// storage_name => std::vector<optional<QueryType>>
|
|
|
|
// merge_update => QueryType::DerivedUpdate =>
|
|
|
|
// MergeableUpdate<QueryTypeId, QueryTypeId> def => QueryType
|
|
|
|
// def->def_var_name => std::vector<QueryTypeId>
|
2017-10-17 05:44:58 +00:00
|
|
|
#define HANDLE_MERGEABLE(update_var_name, def_var_name, storage_name) \
|
|
|
|
for (auto merge_update : update->update_var_name) { \
|
|
|
|
auto& def = storage_name[merge_update.id.id]; \
|
|
|
|
AddRange(&def.def_var_name, merge_update.to_add); \
|
|
|
|
RemoveRange(&def.def_var_name, merge_update.to_remove); \
|
2018-01-11 08:32:19 +00:00
|
|
|
VerifyUnique(def.def_var_name); \
|
2017-04-19 06:56:37 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 06:42:58 +00:00
|
|
|
for (const std::string& filename : update->files_removed)
|
2018-01-30 00:55:51 +00:00
|
|
|
files[usr_to_file[NormalizedPath(filename)].id].def = nullopt;
|
2017-04-19 06:56:37 +00:00
|
|
|
ImportOrUpdate(update->files_def_update);
|
|
|
|
|
2017-05-09 04:20:28 +00:00
|
|
|
RemoveUsrs(SymbolKind::Type, update->types_removed);
|
2018-02-07 05:26:38 +00:00
|
|
|
ImportOrUpdate(std::move(update->types_def_update));
|
2018-02-05 18:12:28 +00:00
|
|
|
HANDLE_MERGEABLE(types_derived, derived, types);
|
|
|
|
HANDLE_MERGEABLE(types_instances, instances, types);
|
2017-04-19 06:56:37 +00:00
|
|
|
HANDLE_MERGEABLE(types_uses, uses, types);
|
|
|
|
|
2017-05-09 04:20:28 +00:00
|
|
|
RemoveUsrs(SymbolKind::Func, update->funcs_removed);
|
2018-02-07 05:26:38 +00:00
|
|
|
ImportOrUpdate(std::move(update->funcs_def_update));
|
2017-04-19 06:56:37 +00:00
|
|
|
HANDLE_MERGEABLE(funcs_declarations, declarations, funcs);
|
2018-02-05 18:12:28 +00:00
|
|
|
HANDLE_MERGEABLE(funcs_derived, derived, funcs);
|
2018-02-10 20:53:18 +00:00
|
|
|
HANDLE_MERGEABLE(funcs_uses, uses, funcs);
|
2017-04-19 06:56:37 +00:00
|
|
|
|
2017-05-09 04:20:28 +00:00
|
|
|
RemoveUsrs(SymbolKind::Var, update->vars_removed);
|
2018-02-07 05:26:38 +00:00
|
|
|
ImportOrUpdate(std::move(update->vars_def_update));
|
2018-01-27 05:50:17 +00:00
|
|
|
HANDLE_MERGEABLE(vars_declarations, declarations, vars);
|
2017-04-19 06:56:37 +00:00
|
|
|
HANDLE_MERGEABLE(vars_uses, uses, vars);
|
|
|
|
|
|
|
|
#undef HANDLE_MERGEABLE
|
2017-02-27 07:23:43 +00:00
|
|
|
}
|
2017-02-26 19:45:59 +00:00
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
void QueryDatabase::ImportOrUpdate(
|
|
|
|
const std::vector<QueryFile::DefUpdate>& updates) {
|
2017-04-19 06:56:37 +00:00
|
|
|
// This function runs on the querydb thread.
|
|
|
|
|
2017-04-07 05:42:57 +00:00
|
|
|
for (auto& def : updates) {
|
2018-01-30 05:34:28 +00:00
|
|
|
auto it = usr_to_file.find(NormalizedPath(def.value.path));
|
2017-05-09 04:20:28 +00:00
|
|
|
assert(it != usr_to_file.end());
|
2017-04-08 06:45:28 +00:00
|
|
|
|
2017-10-17 05:44:58 +00:00
|
|
|
QueryFile& existing = files[it->second.id];
|
2017-04-22 07:32:29 +00:00
|
|
|
|
2018-01-30 05:34:28 +00:00
|
|
|
existing.def = def.value;
|
2018-02-11 18:25:37 +00:00
|
|
|
UpdateSymbols(&existing.symbol_idx, SymbolKind::File, it->second);
|
2017-02-26 19:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
void QueryDatabase::ImportOrUpdate(
|
2018-02-07 05:26:38 +00:00
|
|
|
std::vector<QueryType::DefUpdate>&& updates) {
|
2017-04-19 06:56:37 +00:00
|
|
|
// This function runs on the querydb thread.
|
|
|
|
|
2017-04-07 05:42:57 +00:00
|
|
|
for (auto& def : updates) {
|
2017-12-12 07:56:50 +00:00
|
|
|
assert(!def.value.detailed_name.empty());
|
2017-04-08 06:45:28 +00:00
|
|
|
|
2017-05-09 04:20:28 +00:00
|
|
|
auto it = usr_to_type.find(def.usr);
|
|
|
|
assert(it != usr_to_type.end());
|
2017-05-21 07:56:21 +00:00
|
|
|
|
2017-05-09 04:20:28 +00:00
|
|
|
assert(it->second.id >= 0 && it->second.id < types.size());
|
2017-10-17 05:44:58 +00:00
|
|
|
QueryType& existing = types[it->second.id];
|
2017-04-19 06:56:37 +00:00
|
|
|
|
|
|
|
// Keep the existing definition if it is higher quality.
|
2018-02-11 04:01:10 +00:00
|
|
|
if (!(existing.def && existing.def->spell &&
|
|
|
|
!def.value.spell)) {
|
2018-02-07 05:26:38 +00:00
|
|
|
existing.def = std::move(def.value);
|
2018-02-11 18:25:37 +00:00
|
|
|
UpdateSymbols(&existing.symbol_idx, SymbolKind::Type, it->second);
|
2018-02-04 02:34:07 +00:00
|
|
|
}
|
2017-02-26 19:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
void QueryDatabase::ImportOrUpdate(
|
2018-02-07 05:26:38 +00:00
|
|
|
std::vector<QueryFunc::DefUpdate>&& updates) {
|
2017-04-19 06:56:37 +00:00
|
|
|
// This function runs on the querydb thread.
|
|
|
|
|
2017-04-07 05:42:57 +00:00
|
|
|
for (auto& def : updates) {
|
2017-12-12 07:56:50 +00:00
|
|
|
assert(!def.value.detailed_name.empty());
|
2017-04-08 06:45:28 +00:00
|
|
|
|
2017-05-09 04:20:28 +00:00
|
|
|
auto it = usr_to_func.find(def.usr);
|
|
|
|
assert(it != usr_to_func.end());
|
2017-05-09 01:21:21 +00:00
|
|
|
|
2017-05-09 04:20:28 +00:00
|
|
|
assert(it->second.id >= 0 && it->second.id < funcs.size());
|
2017-10-17 05:44:58 +00:00
|
|
|
QueryFunc& existing = funcs[it->second.id];
|
2017-04-19 06:56:37 +00:00
|
|
|
|
|
|
|
// Keep the existing definition if it is higher quality.
|
2018-02-11 04:01:10 +00:00
|
|
|
if (!(existing.def && existing.def->spell &&
|
|
|
|
!def.value.spell)) {
|
2018-02-07 05:26:38 +00:00
|
|
|
existing.def = std::move(def.value);
|
2018-02-11 18:25:37 +00:00
|
|
|
UpdateSymbols(&existing.symbol_idx, SymbolKind::Func, it->second);
|
2018-02-04 02:34:07 +00:00
|
|
|
}
|
2017-02-26 19:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-26 08:11:47 +00:00
|
|
|
|
2018-02-07 05:26:38 +00:00
|
|
|
void QueryDatabase::ImportOrUpdate(std::vector<QueryVar::DefUpdate>&& updates) {
|
2017-04-19 06:56:37 +00:00
|
|
|
// This function runs on the querydb thread.
|
|
|
|
|
2017-04-07 05:42:57 +00:00
|
|
|
for (auto& def : updates) {
|
2017-12-12 07:56:50 +00:00
|
|
|
assert(!def.value.detailed_name.empty());
|
2017-04-08 06:45:28 +00:00
|
|
|
|
2017-05-09 04:20:28 +00:00
|
|
|
auto it = usr_to_var.find(def.usr);
|
|
|
|
assert(it != usr_to_var.end());
|
2017-05-09 01:21:21 +00:00
|
|
|
|
2017-05-09 04:20:28 +00:00
|
|
|
assert(it->second.id >= 0 && it->second.id < vars.size());
|
2017-10-17 05:44:58 +00:00
|
|
|
QueryVar& existing = vars[it->second.id];
|
2017-04-19 06:56:37 +00:00
|
|
|
|
|
|
|
// Keep the existing definition if it is higher quality.
|
2018-02-11 04:01:10 +00:00
|
|
|
if (!(existing.def && existing.def->spell &&
|
|
|
|
!def.value.spell)) {
|
2018-02-07 05:26:38 +00:00
|
|
|
existing.def = std::move(def.value);
|
2018-02-04 02:34:07 +00:00
|
|
|
if (!def.value.is_local())
|
2018-02-11 18:25:37 +00:00
|
|
|
UpdateSymbols(&existing.symbol_idx, SymbolKind::Var, it->second);
|
2018-02-04 02:34:07 +00:00
|
|
|
}
|
2017-02-26 08:11:47 +00:00
|
|
|
}
|
2017-02-26 19:45:59 +00:00
|
|
|
}
|
2017-02-26 08:11:47 +00:00
|
|
|
|
2018-02-02 06:47:22 +00:00
|
|
|
void QueryDatabase::UpdateSymbols(Maybe<Id<void>>* symbol_idx,
|
2018-02-02 01:59:01 +00:00
|
|
|
SymbolKind kind,
|
2018-02-11 18:25:37 +00:00
|
|
|
Id<void> idx) {
|
2018-02-11 21:56:34 +00:00
|
|
|
if (!symbol_idx->HasValue()) {
|
2018-02-02 06:47:22 +00:00
|
|
|
*symbol_idx = Id<void>(symbols.size());
|
2018-02-10 00:42:33 +00:00
|
|
|
symbols.push_back(SymbolIdx{idx, kind});
|
2017-02-26 08:11:47 +00:00
|
|
|
}
|
2017-05-09 01:21:21 +00:00
|
|
|
}
|
2017-05-12 06:08:15 +00:00
|
|
|
|
2018-02-03 18:33:22 +00:00
|
|
|
std::string_view QueryDatabase::GetSymbolDetailedName(RawId symbol_idx) const {
|
2018-02-11 18:25:37 +00:00
|
|
|
RawId idx = symbols[symbol_idx].id.id;
|
2018-02-02 01:59:01 +00:00
|
|
|
switch (symbols[symbol_idx].kind) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case SymbolKind::File:
|
|
|
|
if (files[idx].def)
|
|
|
|
return files[idx].def->path;
|
|
|
|
break;
|
|
|
|
case SymbolKind::Func:
|
|
|
|
if (funcs[idx].def)
|
|
|
|
return funcs[idx].def->detailed_name;
|
|
|
|
break;
|
|
|
|
case SymbolKind::Type:
|
|
|
|
if (types[idx].def)
|
|
|
|
return types[idx].def->detailed_name;
|
|
|
|
break;
|
|
|
|
case SymbolKind::Var:
|
|
|
|
if (vars[idx].def)
|
|
|
|
return vars[idx].def->detailed_name;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2018-02-03 18:33:22 +00:00
|
|
|
std::string_view QueryDatabase::GetSymbolShortName(RawId symbol_idx) const {
|
2018-02-11 18:25:37 +00:00
|
|
|
RawId idx = symbols[symbol_idx].id.id;
|
2018-02-02 01:59:01 +00:00
|
|
|
switch (symbols[symbol_idx].kind) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case SymbolKind::File:
|
|
|
|
if (files[idx].def)
|
|
|
|
return files[idx].def->path;
|
|
|
|
break;
|
|
|
|
case SymbolKind::Func:
|
|
|
|
if (funcs[idx].def)
|
|
|
|
return funcs[idx].def->ShortName();
|
|
|
|
break;
|
|
|
|
case SymbolKind::Type:
|
|
|
|
if (types[idx].def)
|
|
|
|
return types[idx].def->ShortName();
|
|
|
|
break;
|
|
|
|
case SymbolKind::Var:
|
|
|
|
if (vars[idx].def)
|
|
|
|
return vars[idx].def->ShortName();
|
|
|
|
break;
|
2018-02-02 01:24:46 +00:00
|
|
|
}
|
2018-02-02 01:59:01 +00:00
|
|
|
return "";
|
2018-02-02 01:24:46 +00:00
|
|
|
}
|
|
|
|
|
2018-02-04 02:34:07 +00:00
|
|
|
|
2017-11-19 18:05:06 +00:00
|
|
|
TEST_SUITE("query") {
|
|
|
|
IndexUpdate GetDelta(IndexFile previous, IndexFile current) {
|
|
|
|
QueryDatabase db;
|
|
|
|
IdMap previous_map(&db, previous.id_cache);
|
|
|
|
IdMap current_map(&db, current.id_cache);
|
|
|
|
return IndexUpdate::CreateDelta(&previous_map, ¤t_map, &previous,
|
|
|
|
¤t);
|
|
|
|
}
|
2017-05-25 00:18:04 +00:00
|
|
|
|
2017-11-19 18:05:06 +00:00
|
|
|
TEST_CASE("remove defs") {
|
2018-01-30 05:34:28 +00:00
|
|
|
IndexFile previous("foo.cc", "<empty>");
|
|
|
|
IndexFile current("foo.cc", "<empty>");
|
2017-05-25 00:18:04 +00:00
|
|
|
|
2018-01-20 16:57:41 +00:00
|
|
|
previous.Resolve(previous.ToTypeId(HashUsr("usr1")))
|
2018-02-11 04:01:10 +00:00
|
|
|
->def.spell = Use(Range(Position(1, 0)), Id<void>(), SymbolKind::File, Role::None);
|
2018-01-20 16:57:41 +00:00
|
|
|
previous.Resolve(previous.ToFuncId(HashUsr("usr2")))
|
2018-02-11 04:30:27 +00:00
|
|
|
->def.spell = Use(Range(Position(2, 0)), Id<void>(), SymbolKind::File, Role::None);
|
2018-01-20 16:57:41 +00:00
|
|
|
previous.Resolve(previous.ToVarId(HashUsr("usr3")))
|
2018-02-11 04:01:10 +00:00
|
|
|
->def.spell = Use(Range(Position(3, 0)), Id<void>(), SymbolKind::File, Role::None);
|
2017-05-25 00:18:04 +00:00
|
|
|
|
2017-11-19 18:05:06 +00:00
|
|
|
IndexUpdate update = GetDelta(previous, current);
|
2017-05-25 00:18:04 +00:00
|
|
|
|
2018-01-14 21:18:12 +00:00
|
|
|
REQUIRE(update.types_removed == std::vector<Usr>{HashUsr("usr1")});
|
|
|
|
REQUIRE(update.funcs_removed == std::vector<Usr>{HashUsr("usr2")});
|
|
|
|
REQUIRE(update.vars_removed == std::vector<Usr>{HashUsr("usr3")});
|
2017-11-19 18:05:06 +00:00
|
|
|
}
|
2017-05-15 03:51:53 +00:00
|
|
|
|
2017-11-19 18:05:06 +00:00
|
|
|
TEST_CASE("do not remove ref-only defs") {
|
2018-01-30 05:34:28 +00:00
|
|
|
IndexFile previous("foo.cc", "<empty>");
|
|
|
|
IndexFile current("foo.cc", "<empty>");
|
2017-05-15 03:51:53 +00:00
|
|
|
|
2018-01-14 21:18:12 +00:00
|
|
|
previous.Resolve(previous.ToTypeId(HashUsr("usr1")))
|
2018-02-08 18:38:27 +00:00
|
|
|
->uses.push_back(Reference{Range(Position(1, 0))});
|
2018-01-14 21:18:12 +00:00
|
|
|
previous.Resolve(previous.ToFuncId(HashUsr("usr2")))
|
2018-02-10 20:53:18 +00:00
|
|
|
->uses.push_back(Use(Range(Position(2, 0)), Id<void>(0),
|
2018-02-11 04:01:10 +00:00
|
|
|
SymbolKind::Func, Role::None));
|
2018-01-14 21:18:12 +00:00
|
|
|
previous.Resolve(previous.ToVarId(HashUsr("usr3")))
|
2018-02-09 05:11:35 +00:00
|
|
|
->uses.push_back(Reference{Range(Position(3, 0))});
|
2017-05-15 03:51:53 +00:00
|
|
|
|
2017-11-19 18:05:06 +00:00
|
|
|
IndexUpdate update = GetDelta(previous, current);
|
2017-05-15 03:51:53 +00:00
|
|
|
|
2017-11-19 18:05:06 +00:00
|
|
|
REQUIRE(update.types_removed == std::vector<Usr>{});
|
|
|
|
REQUIRE(update.funcs_removed == std::vector<Usr>{});
|
|
|
|
REQUIRE(update.vars_removed == std::vector<Usr>{});
|
|
|
|
}
|
2017-05-15 03:51:53 +00:00
|
|
|
|
2017-11-19 18:05:06 +00:00
|
|
|
TEST_CASE("func callers") {
|
2018-01-30 05:34:28 +00:00
|
|
|
IndexFile previous("foo.cc", "<empty>");
|
|
|
|
IndexFile current("foo.cc", "<empty>");
|
2017-05-15 03:51:53 +00:00
|
|
|
|
2018-01-14 21:18:12 +00:00
|
|
|
IndexFunc* pf = previous.Resolve(previous.ToFuncId(HashUsr("usr")));
|
|
|
|
IndexFunc* cf = current.Resolve(current.ToFuncId(HashUsr("usr")));
|
2017-05-15 03:51:53 +00:00
|
|
|
|
2018-02-10 20:53:18 +00:00
|
|
|
pf->uses.push_back(Use(Range(Position(1, 0)), Id<void>(0),
|
2018-02-11 04:01:10 +00:00
|
|
|
SymbolKind::Func, Role::None));
|
2018-02-10 20:53:18 +00:00
|
|
|
cf->uses.push_back(Use(Range(Position(2, 0)), Id<void>(0),
|
2018-02-11 04:01:10 +00:00
|
|
|
SymbolKind::Func, Role::None));
|
2017-05-15 03:51:53 +00:00
|
|
|
|
2017-11-19 18:05:06 +00:00
|
|
|
IndexUpdate update = GetDelta(previous, current);
|
2017-05-21 07:56:21 +00:00
|
|
|
|
2017-11-19 18:05:06 +00:00
|
|
|
REQUIRE(update.funcs_removed == std::vector<Usr>{});
|
2018-02-10 20:53:18 +00:00
|
|
|
REQUIRE(update.funcs_uses.size() == 1);
|
|
|
|
REQUIRE(update.funcs_uses[0].id == QueryFuncId(0));
|
|
|
|
REQUIRE(update.funcs_uses[0].to_remove.size() == 1);
|
|
|
|
REQUIRE(update.funcs_uses[0].to_remove[0].range ==
|
2017-11-19 18:05:06 +00:00
|
|
|
Range(Position(1, 0)));
|
2018-02-10 20:53:18 +00:00
|
|
|
REQUIRE(update.funcs_uses[0].to_add.size() == 1);
|
|
|
|
REQUIRE(update.funcs_uses[0].to_add[0].range ==
|
2017-11-19 22:11:54 +00:00
|
|
|
Range(Position(2, 0)));
|
2017-11-19 18:05:06 +00:00
|
|
|
}
|
2017-05-15 03:51:53 +00:00
|
|
|
|
2017-11-19 18:05:06 +00:00
|
|
|
TEST_CASE("type usages") {
|
2018-01-30 05:34:28 +00:00
|
|
|
IndexFile previous("foo.cc", "<empty>");
|
|
|
|
IndexFile current("foo.cc", "<empty>");
|
2017-05-20 07:33:11 +00:00
|
|
|
|
2018-01-14 21:18:12 +00:00
|
|
|
IndexType* pt = previous.Resolve(previous.ToTypeId(HashUsr("usr")));
|
|
|
|
IndexType* ct = current.Resolve(current.ToTypeId(HashUsr("usr")));
|
2017-05-20 07:33:11 +00:00
|
|
|
|
2018-02-08 18:38:27 +00:00
|
|
|
pt->uses.push_back(Reference{Range(Position(1, 0))});
|
|
|
|
ct->uses.push_back(Reference{Range(Position(2, 0))});
|
2017-05-20 07:33:11 +00:00
|
|
|
|
2017-11-19 18:05:06 +00:00
|
|
|
IndexUpdate update = GetDelta(previous, current);
|
2017-05-21 07:56:21 +00:00
|
|
|
|
2017-11-19 18:05:06 +00:00
|
|
|
REQUIRE(update.types_removed == std::vector<Usr>{});
|
2017-12-12 07:56:50 +00:00
|
|
|
REQUIRE(update.types_def_update.empty());
|
2017-11-19 18:05:06 +00:00
|
|
|
REQUIRE(update.types_uses.size() == 1);
|
|
|
|
REQUIRE(update.types_uses[0].to_remove.size() == 1);
|
|
|
|
REQUIRE(update.types_uses[0].to_remove[0].range == Range(Position(1, 0)));
|
|
|
|
REQUIRE(update.types_uses[0].to_add.size() == 1);
|
|
|
|
REQUIRE(update.types_uses[0].to_add[0].range == Range(Position(2, 0)));
|
|
|
|
}
|
2017-05-20 07:33:11 +00:00
|
|
|
|
2017-11-19 18:05:06 +00:00
|
|
|
TEST_CASE("apply delta") {
|
2018-01-30 05:34:28 +00:00
|
|
|
IndexFile previous("foo.cc", "<empty>");
|
|
|
|
IndexFile current("foo.cc", "<empty>");
|
2017-11-19 18:05:06 +00:00
|
|
|
|
2018-01-14 21:18:12 +00:00
|
|
|
IndexFunc* pf = previous.Resolve(previous.ToFuncId(HashUsr("usr")));
|
|
|
|
IndexFunc* cf = current.Resolve(current.ToFuncId(HashUsr("usr")));
|
2018-02-10 20:53:18 +00:00
|
|
|
pf->uses.push_back(Use(Range(Position(1, 0)), Id<void>(0),
|
2018-02-11 04:01:10 +00:00
|
|
|
SymbolKind::Func, Role::None));
|
2018-02-10 20:53:18 +00:00
|
|
|
pf->uses.push_back(Use(Range(Position(2, 0)), Id<void>(0),
|
2018-02-11 04:01:10 +00:00
|
|
|
SymbolKind::Func, Role::None));
|
2018-02-10 20:53:18 +00:00
|
|
|
cf->uses.push_back(Use(Range(Position(4, 0)), Id<void>(0),
|
2018-02-11 04:01:10 +00:00
|
|
|
SymbolKind::Func, Role::None));
|
2018-02-10 20:53:18 +00:00
|
|
|
cf->uses.push_back(Use(Range(Position(5, 0)), Id<void>(0),
|
2018-02-11 04:01:10 +00:00
|
|
|
SymbolKind::Func, Role::None));
|
2017-11-19 18:05:06 +00:00
|
|
|
|
|
|
|
QueryDatabase db;
|
|
|
|
IdMap previous_map(&db, previous.id_cache);
|
|
|
|
IdMap current_map(&db, current.id_cache);
|
|
|
|
REQUIRE(db.funcs.size() == 1);
|
|
|
|
|
|
|
|
IndexUpdate import_update =
|
|
|
|
IndexUpdate::CreateDelta(nullptr, &previous_map, nullptr, &previous);
|
|
|
|
IndexUpdate delta_update = IndexUpdate::CreateDelta(
|
|
|
|
&previous_map, ¤t_map, &previous, ¤t);
|
|
|
|
|
|
|
|
db.ApplyIndexUpdate(&import_update);
|
2018-02-10 20:53:18 +00:00
|
|
|
REQUIRE(db.funcs[0].uses.size() == 2);
|
|
|
|
REQUIRE(db.funcs[0].uses[0].range == Range(Position(1, 0)));
|
|
|
|
REQUIRE(db.funcs[0].uses[1].range == Range(Position(2, 0)));
|
2017-11-19 18:05:06 +00:00
|
|
|
|
|
|
|
db.ApplyIndexUpdate(&delta_update);
|
2018-02-10 20:53:18 +00:00
|
|
|
REQUIRE(db.funcs[0].uses.size() == 2);
|
|
|
|
REQUIRE(db.funcs[0].uses[0].range == Range(Position(4, 0)));
|
|
|
|
REQUIRE(db.funcs[0].uses[1].range == Range(Position(5, 0)));
|
2017-11-19 18:05:06 +00:00
|
|
|
}
|
2017-05-20 07:33:11 +00:00
|
|
|
}
|