2017-02-23 09:23:23 +00:00
|
|
|
#include "query.h"
|
|
|
|
|
2017-03-25 20:32:44 +00:00
|
|
|
#include "indexer.h"
|
2018-02-12 01:15:56 +00:00
|
|
|
#include "serializer.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-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>
|
2018-05-11 21:23:53 +00:00
|
|
|
#include <optional>
|
2017-09-22 01:14:57 +00:00
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
2017-02-21 09:08:52 +00:00
|
|
|
|
2018-05-30 06:56:14 +00:00
|
|
|
// Used by |REMOVE_ADD| so only |range| is needed.
|
2018-05-05 03:40:52 +00:00
|
|
|
MAKE_HASHABLE(Use, t.range, t.file_id);
|
2018-04-08 06:32:35 +00:00
|
|
|
|
2017-04-08 08:17:29 +00:00
|
|
|
namespace {
|
2017-04-07 06:57:26 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
void AssignFileId(int file_id, SymbolRef& ref) {
|
|
|
|
if (ref.kind == SymbolKind::File)
|
|
|
|
ref.usr = file_id;
|
2018-04-08 06:32:35 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
void AssignFileId(int file_id, Use& use) {
|
|
|
|
if (use.kind == SymbolKind::File)
|
|
|
|
use.usr = file_id;
|
|
|
|
use.file_id = file_id;
|
2018-04-08 06:32:35 +00:00
|
|
|
}
|
|
|
|
|
2018-02-24 02:24:54 +00:00
|
|
|
template <typename T>
|
2018-04-30 04:49:03 +00:00
|
|
|
void AssignFileId(int file_id, T&) {}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void AssignFileId(int file_id, Maybe<T>& x) {
|
|
|
|
if (x)
|
|
|
|
AssignFileId(file_id, *x);
|
2018-02-24 02:24:54 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
template <typename T>
|
|
|
|
void AssignFileId(int file_id, std::vector<T>& xs) {
|
|
|
|
for (T& x : xs)
|
|
|
|
AssignFileId(file_id, x);
|
2017-02-26 19:45:59 +00:00
|
|
|
}
|
2017-04-08 08:17:29 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
void AddRange(int file_id, std::vector<Use>& into, const std::vector<Use>& from) {
|
|
|
|
into.reserve(into.size() + from.size());
|
|
|
|
for (Use use : from) {
|
|
|
|
use.file_id = file_id;
|
|
|
|
into.push_back(use);
|
|
|
|
}
|
2017-02-26 19:45:59 +00:00
|
|
|
}
|
2017-04-08 08:17:29 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
void AddRange(int _, std::vector<Usr>& into, const std::vector<Usr>& from) {
|
|
|
|
into.insert(into.end(), from.begin(), from.end());
|
2017-02-26 08:11:47 +00:00
|
|
|
}
|
2017-02-25 23:59:09 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
template <typename T>
|
2018-05-05 03:40:52 +00:00
|
|
|
void RemoveRange(std::vector<T>& from, const std::vector<T>& to_remove) {
|
2018-04-30 04:49:03 +00:00
|
|
|
if (to_remove.size()) {
|
|
|
|
std::unordered_set<T> to_remove_set(to_remove.begin(), to_remove.end());
|
2018-05-05 03:40:52 +00:00
|
|
|
from.erase(
|
|
|
|
std::remove_if(from.begin(), from.end(),
|
2018-04-30 04:49:03 +00:00
|
|
|
[&](const T& t) { return to_remove_set.count(t) > 0; }),
|
2018-05-05 03:40:52 +00:00
|
|
|
from.end());
|
2017-04-16 08:31:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
QueryFile::DefUpdate BuildFileDefUpdate(const IndexFile& indexed) {
|
2017-04-15 05:41:35 +00:00
|
|
|
QueryFile::Def def;
|
2018-05-05 03:40:52 +00:00
|
|
|
def.path = std::move(indexed.path);
|
|
|
|
def.args = std::move(indexed.args);
|
|
|
|
def.includes = std::move(indexed.includes);
|
|
|
|
def.inactive_regions = std::move(indexed.skipped_by_preprocessor);
|
2018-05-05 22:29:17 +00:00
|
|
|
def.dependencies.reserve(indexed.dependencies.size());
|
|
|
|
for (auto& dep : indexed.dependencies)
|
2018-05-15 05:13:18 +00:00
|
|
|
def.dependencies.push_back(dep.first());
|
2018-05-05 03:40:52 +00:00
|
|
|
def.language = indexed.language;
|
2017-11-30 21:56:55 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
auto add_all_symbols = [&](Use use, Usr usr, SymbolKind kind) {
|
|
|
|
def.all_symbols.push_back(SymbolRef(use.range, usr, kind, use.role));
|
2018-02-11 04:01:10 +00:00
|
|
|
};
|
2018-04-30 04:49:03 +00:00
|
|
|
auto add_outline = [&](Use use, Usr usr, SymbolKind kind) {
|
|
|
|
def.outline.push_back(SymbolRef(use.range, usr, kind, use.role));
|
2017-04-03 02:21:21 +00:00
|
|
|
};
|
2017-02-27 07:23:43 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
for (auto& it : indexed.usr2type) {
|
|
|
|
const IndexType& type = it.second;
|
2018-02-11 04:01:10 +00:00
|
|
|
if (type.def.spell)
|
2018-04-30 04:49:03 +00:00
|
|
|
add_all_symbols(*type.def.spell, type.usr, SymbolKind::Type);
|
2018-02-11 04:01:10 +00:00
|
|
|
if (type.def.extent)
|
2018-04-30 04:49:03 +00:00
|
|
|
add_outline(*type.def.extent, type.usr, SymbolKind::Type);
|
2018-02-20 21:56:56 +00:00
|
|
|
for (Use decl : type.declarations) {
|
2018-04-30 04:49:03 +00:00
|
|
|
add_all_symbols(decl, type.usr, SymbolKind::Type);
|
2018-02-25 01:24:20 +00:00
|
|
|
// Constructor positions have references to the class,
|
|
|
|
// which we do not want to show in textDocument/documentSymbol
|
|
|
|
if (!(decl.role & Role::Reference))
|
2018-04-30 04:49:03 +00:00
|
|
|
add_outline(decl, type.usr, SymbolKind::Type);
|
2018-02-20 21:56:56 +00:00
|
|
|
}
|
2018-02-11 04:01:10 +00:00
|
|
|
for (Use use : type.uses)
|
2018-04-30 04:49:03 +00:00
|
|
|
add_all_symbols(use, type.usr, SymbolKind::Type);
|
2017-02-27 07:23:43 +00:00
|
|
|
}
|
2018-04-30 04:49:03 +00:00
|
|
|
for (auto& it: indexed.usr2func) {
|
|
|
|
const IndexFunc& func = it.second;
|
2018-02-11 21:56:34 +00:00
|
|
|
if (func.def.spell)
|
2018-04-30 04:49:03 +00:00
|
|
|
add_all_symbols(*func.def.spell, func.usr, SymbolKind::Func);
|
2018-02-11 21:56:34 +00:00
|
|
|
if (func.def.extent)
|
2018-04-30 04:49:03 +00:00
|
|
|
add_outline(*func.def.extent, func.usr, SymbolKind::Func);
|
|
|
|
for (Use use : func.declarations) {
|
|
|
|
add_all_symbols(use, func.usr, SymbolKind::Func);
|
|
|
|
add_outline(use, func.usr, SymbolKind::Func);
|
2017-04-03 02:21:21 +00:00
|
|
|
}
|
2018-02-12 04:22:47 +00:00
|
|
|
for (Use use : 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-12 04:22:47 +00:00
|
|
|
if (use.role & Role::Implicit) {
|
|
|
|
if (use.range.start.column > 0)
|
|
|
|
use.range.start.column--;
|
|
|
|
use.range.end.column++;
|
2018-01-03 00:30:03 +00:00
|
|
|
}
|
2018-04-30 04:49:03 +00:00
|
|
|
add_all_symbols(use, func.usr, SymbolKind::Func);
|
2017-05-23 06:57:46 +00:00
|
|
|
}
|
2017-02-27 07:23:43 +00:00
|
|
|
}
|
2018-04-30 04:49:03 +00:00
|
|
|
for (auto& it : indexed.usr2var) {
|
|
|
|
const IndexVar& var = it.second;
|
2018-02-22 07:23:39 +00:00
|
|
|
if (var.def.spell)
|
2018-04-30 04:49:03 +00:00
|
|
|
add_all_symbols(*var.def.spell, var.usr, SymbolKind::Var);
|
2018-02-22 07:23:39 +00:00
|
|
|
if (var.def.extent)
|
2018-04-30 04:49:03 +00:00
|
|
|
add_outline(*var.def.extent, var.usr, SymbolKind::Var);
|
2018-02-12 04:22:47 +00:00
|
|
|
for (Use decl : var.declarations) {
|
2018-04-30 04:49:03 +00:00
|
|
|
add_all_symbols(decl, var.usr, SymbolKind::Var);
|
|
|
|
add_outline(decl, var.usr, SymbolKind::Var);
|
2018-01-27 05:50:17 +00:00
|
|
|
}
|
2018-02-11 04:01:10 +00:00
|
|
|
for (Use use : var.uses)
|
2018-04-30 04:49:03 +00:00
|
|
|
add_all_symbols(use, var.usr, SymbolKind::Var);
|
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-18 08:02:19 +00:00
|
|
|
// Returns true if an element with the same file is found.
|
|
|
|
template <typename Q>
|
2018-05-13 20:30:24 +00:00
|
|
|
bool TryReplaceDef(llvm::SmallVectorImpl<Q>& def_list, Q&& def) {
|
2018-02-18 08:02:19 +00:00
|
|
|
for (auto& def1 : def_list)
|
2018-05-05 03:40:52 +00:00
|
|
|
if (def1.spell->file_id == def.spell->file_id) {
|
|
|
|
def1 = std::move(def);
|
2018-02-18 08:02:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-20 18:32:39 +00:00
|
|
|
} // namespace
|
|
|
|
|
2017-04-19 06:56:37 +00:00
|
|
|
// ----------------------
|
|
|
|
// INDEX THREAD FUNCTIONS
|
|
|
|
// ----------------------
|
|
|
|
|
2017-04-07 05:42:57 +00:00
|
|
|
// static
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexUpdate IndexUpdate::CreateDelta(IndexFile* previous,
|
2017-09-22 01:14:57 +00:00
|
|
|
IndexFile* current) {
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexUpdate r;
|
|
|
|
static IndexFile empty(current->path, "<empty>");
|
|
|
|
if (!previous)
|
|
|
|
previous = ∅
|
2017-09-22 01:14:57 +00:00
|
|
|
|
2018-05-05 03:40:52 +00:00
|
|
|
r.files_def_update = BuildFileDefUpdate(std::move(*current));
|
2017-02-25 23:59:09 +00:00
|
|
|
|
2018-05-30 06:56:14 +00:00
|
|
|
r.funcs_hint = current->usr2func.size() - previous->usr2func.size();
|
2018-05-04 04:20:10 +00:00
|
|
|
for (auto& it : previous->usr2func) {
|
|
|
|
auto& func = it.second;
|
|
|
|
if (func.def.spell)
|
|
|
|
r.funcs_removed.push_back(func.usr);
|
2018-05-05 03:40:52 +00:00
|
|
|
r.funcs_declarations[func.usr].first = std::move(func.declarations);
|
|
|
|
r.funcs_uses[func.usr].first = std::move(func.uses);
|
|
|
|
r.funcs_derived[func.usr].first = std::move(func.derived);
|
2018-05-04 04:20:10 +00:00
|
|
|
}
|
|
|
|
for (auto& it : current->usr2func) {
|
|
|
|
auto& func = it.second;
|
2018-05-05 03:40:52 +00:00
|
|
|
if (func.def.spell && func.def.detailed_name.size())
|
2018-05-04 04:20:10 +00:00
|
|
|
r.funcs_def_update.emplace_back(it.first, func.def);
|
2018-05-05 03:40:52 +00:00
|
|
|
r.funcs_declarations[func.usr].second = std::move(func.declarations);
|
|
|
|
r.funcs_uses[func.usr].second = std::move(func.uses);
|
|
|
|
r.funcs_derived[func.usr].second = std::move(func.derived);
|
2018-05-04 04:20:10 +00:00
|
|
|
}
|
2017-02-21 09:08:52 +00:00
|
|
|
|
2018-05-30 06:56:14 +00:00
|
|
|
r.types_hint = current->usr2type.size() - previous->usr2type.size();
|
2018-05-04 04:20:10 +00:00
|
|
|
for (auto& it : previous->usr2type) {
|
|
|
|
auto& type = it.second;
|
|
|
|
if (type.def.spell)
|
|
|
|
r.types_removed.push_back(type.usr);
|
2018-05-05 03:40:52 +00:00
|
|
|
r.types_declarations[type.usr].first = std::move(type.declarations);
|
|
|
|
r.types_uses[type.usr].first = std::move(type.uses);
|
|
|
|
r.types_derived[type.usr].first = std::move(type.derived);
|
|
|
|
r.types_instances[type.usr].first = std::move(type.instances);
|
2018-05-04 04:20:10 +00:00
|
|
|
};
|
|
|
|
for (auto& it : current->usr2type) {
|
|
|
|
auto& type = it.second;
|
2018-05-05 03:40:52 +00:00
|
|
|
if (type.def.spell && type.def.detailed_name.size())
|
2018-05-04 04:20:10 +00:00
|
|
|
r.types_def_update.emplace_back(it.first, type.def);
|
2018-05-05 03:40:52 +00:00
|
|
|
r.types_declarations[type.usr].second = std::move(type.declarations);
|
|
|
|
r.types_uses[type.usr].second = std::move(type.uses);
|
|
|
|
r.types_derived[type.usr].second = std::move(type.derived);
|
|
|
|
r.types_instances[type.usr].second = std::move(type.instances);
|
2018-05-04 04:20:10 +00:00
|
|
|
};
|
2017-02-26 19:45:59 +00:00
|
|
|
|
2018-05-30 06:56:14 +00:00
|
|
|
r.vars_hint = current->usr2var.size() - previous->usr2var.size();
|
2018-05-04 04:20:10 +00:00
|
|
|
for (auto& it : previous->usr2var) {
|
|
|
|
auto& var = it.second;
|
|
|
|
if (var.def.spell)
|
|
|
|
r.vars_removed.push_back(var.usr);
|
2018-05-05 03:40:52 +00:00
|
|
|
r.vars_declarations[var.usr].first = std::move(var.declarations);
|
|
|
|
r.vars_uses[var.usr].first = std::move(var.uses);
|
2018-05-04 04:20:10 +00:00
|
|
|
}
|
|
|
|
for (auto& it : current->usr2var) {
|
|
|
|
auto& var = it.second;
|
2018-05-05 03:40:52 +00:00
|
|
|
if (var.def.spell && var.def.detailed_name.size())
|
2018-05-04 04:20:10 +00:00
|
|
|
r.vars_def_update.emplace_back(it.first, var.def);
|
2018-05-05 03:40:52 +00:00
|
|
|
r.vars_declarations[var.usr].second = std::move(var.declarations);
|
|
|
|
r.vars_uses[var.usr].second = std::move(var.uses);
|
2018-05-04 04:20:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
2017-05-19 07:02:01 +00:00
|
|
|
}
|
2017-02-26 19:45:59 +00:00
|
|
|
|
2018-05-30 06:56:14 +00:00
|
|
|
void DB::RemoveUsrs(SymbolKind kind,
|
|
|
|
int file_id,
|
|
|
|
const std::vector<Usr>& to_remove) {
|
2018-04-30 04:49:03 +00:00
|
|
|
switch (kind) {
|
2017-05-09 04:20:28 +00:00
|
|
|
case SymbolKind::Func: {
|
2018-05-30 06:56:14 +00:00
|
|
|
for (Usr usr : to_remove) {
|
|
|
|
// FIXME
|
|
|
|
if (!HasFunc(usr)) continue;
|
2018-04-30 04:49:03 +00:00
|
|
|
QueryFunc& func = Func(usr);
|
2018-05-13 20:30:24 +00:00
|
|
|
auto it = llvm::find_if(func.def, [=](const QueryFunc::Def& def) {
|
2018-05-05 03:40:52 +00:00
|
|
|
return def.spell->file_id == file_id;
|
2018-02-18 08:02:19 +00:00
|
|
|
});
|
2018-05-13 20:30:24 +00:00
|
|
|
if (it != func.def.end())
|
|
|
|
func.def.erase(it);
|
2018-02-02 01:24:46 +00:00
|
|
|
}
|
2017-05-09 04:20:28 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-05-30 06:56:14 +00:00
|
|
|
case SymbolKind::Type: {
|
|
|
|
for (Usr usr : to_remove) {
|
|
|
|
// FIXME
|
|
|
|
if (!HasType(usr)) continue;
|
|
|
|
QueryType& type = Type(usr);
|
|
|
|
auto it = llvm::find_if(type.def, [=](const QueryType::Def& def) {
|
|
|
|
return def.spell->file_id == file_id;
|
|
|
|
});
|
|
|
|
if (it != type.def.end())
|
|
|
|
type.def.erase(it);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-05-09 04:20:28 +00:00
|
|
|
case SymbolKind::Var: {
|
2018-05-30 06:56:14 +00:00
|
|
|
for (Usr usr : to_remove) {
|
|
|
|
// FIXME
|
|
|
|
if (!HasVar(usr)) continue;
|
2018-04-30 04:49:03 +00:00
|
|
|
QueryVar& var = Var(usr);
|
2018-05-13 20:30:24 +00:00
|
|
|
auto it = llvm::find_if(var.def, [=](const QueryVar::Def& def) {
|
2018-05-05 03:40:52 +00:00
|
|
|
return def.spell->file_id == file_id;
|
2018-02-18 08:02:19 +00:00
|
|
|
});
|
2018-05-13 20:30:24 +00:00
|
|
|
if (it != var.def.end())
|
|
|
|
var.def.erase(it);
|
2018-02-02 01:24:46 +00:00
|
|
|
}
|
2017-05-09 04:20:28 +00:00
|
|
|
break;
|
2017-04-22 07:32:29 +00:00
|
|
|
}
|
2018-02-18 08:02:19 +00:00
|
|
|
default:
|
2017-05-21 23:48:21 +00:00
|
|
|
break;
|
2017-04-22 07:32:29 +00:00
|
|
|
}
|
2017-04-19 06:56:37 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 06:56:14 +00:00
|
|
|
void DB::ApplyIndexUpdate(IndexUpdate* u) {
|
|
|
|
#define REMOVE_ADD(C, F) \
|
|
|
|
for (auto& it : u->C##s_##F) { \
|
|
|
|
auto R = C##_usr.try_emplace({it.first}, C##_usr.size()); \
|
|
|
|
if (R.second) \
|
|
|
|
C##s.emplace_back().usr = it.first; \
|
|
|
|
auto& entity = C##s[R.first->second]; \
|
|
|
|
AssignFileId(u->file_id, it.second.first); \
|
|
|
|
RemoveRange(entity.F, it.second.first); \
|
|
|
|
AssignFileId(u->file_id, it.second.second); \
|
|
|
|
AddRange(u->file_id, entity.F, it.second.second); \
|
2017-04-19 06:56:37 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
if (u->files_removed)
|
|
|
|
files[name2file_id[LowerPathIfInsensitive(*u->files_removed)]].def =
|
|
|
|
std::nullopt;
|
|
|
|
u->file_id = u->files_def_update ? Update(std::move(*u->files_def_update)) : -1;
|
|
|
|
|
2018-05-30 06:56:14 +00:00
|
|
|
const double grow = 1.3;
|
|
|
|
size_t t;
|
|
|
|
|
|
|
|
if ((t = funcs.size() + u->funcs_hint) > funcs.capacity()) {
|
|
|
|
t = size_t(t * grow);
|
|
|
|
funcs.reserve(t);
|
|
|
|
func_usr.reserve(t);
|
|
|
|
}
|
2018-04-30 04:49:03 +00:00
|
|
|
RemoveUsrs(SymbolKind::Func, u->file_id, u->funcs_removed);
|
|
|
|
Update(u->file_id, std::move(u->funcs_def_update));
|
2018-05-30 06:56:14 +00:00
|
|
|
REMOVE_ADD(func, declarations);
|
|
|
|
REMOVE_ADD(func, derived);
|
|
|
|
REMOVE_ADD(func, uses);
|
|
|
|
|
|
|
|
if ((t = types.size() + u->types_hint) > types.capacity()) {
|
|
|
|
t = size_t(t * grow);
|
|
|
|
types.reserve(t);
|
|
|
|
type_usr.reserve(t);
|
|
|
|
}
|
|
|
|
RemoveUsrs(SymbolKind::Type, u->file_id, u->types_removed);
|
2018-04-30 04:49:03 +00:00
|
|
|
Update(u->file_id, std::move(u->types_def_update));
|
2018-05-30 06:56:14 +00:00
|
|
|
REMOVE_ADD(type, declarations);
|
|
|
|
REMOVE_ADD(type, derived);
|
|
|
|
REMOVE_ADD(type, instances);
|
|
|
|
REMOVE_ADD(type, uses);
|
|
|
|
|
|
|
|
if ((t = vars.size() + u->vars_hint) > vars.capacity()) {
|
|
|
|
t = size_t(t * grow);
|
|
|
|
vars.reserve(t);
|
|
|
|
var_usr.reserve(t);
|
|
|
|
}
|
2018-04-30 04:49:03 +00:00
|
|
|
RemoveUsrs(SymbolKind::Var, u->file_id, u->vars_removed);
|
|
|
|
Update(u->file_id, std::move(u->vars_def_update));
|
2018-05-30 06:56:14 +00:00
|
|
|
REMOVE_ADD(var, declarations);
|
|
|
|
REMOVE_ADD(var, uses);
|
2017-04-19 06:56:37 +00:00
|
|
|
|
2018-05-30 06:56:14 +00:00
|
|
|
#undef REMOVE_ADD
|
2017-02-27 07:23:43 +00:00
|
|
|
}
|
2017-02-26 19:45:59 +00:00
|
|
|
|
2018-05-30 06:56:14 +00:00
|
|
|
int DB::Update(QueryFile::DefUpdate&& u) {
|
2018-04-30 04:49:03 +00:00
|
|
|
int id = files.size();
|
|
|
|
auto it = name2file_id.try_emplace(LowerPathIfInsensitive(u.value.path), id);
|
|
|
|
if (it.second)
|
|
|
|
files.emplace_back().id = id;
|
|
|
|
QueryFile& existing = files[it.first->second];
|
|
|
|
existing.def = u.value;
|
|
|
|
return existing.id;
|
|
|
|
}
|
|
|
|
|
2018-05-30 06:56:14 +00:00
|
|
|
void DB::Update(int file_id, std::vector<std::pair<Usr, QueryFunc::Def>>&& us) {
|
2018-05-04 04:20:10 +00:00
|
|
|
for (auto& u : us) {
|
|
|
|
auto& def = u.second;
|
2018-04-30 04:49:03 +00:00
|
|
|
assert(!def.detailed_name.empty());
|
|
|
|
AssignFileId(file_id, def.spell);
|
|
|
|
AssignFileId(file_id, def.extent);
|
|
|
|
AssignFileId(file_id, def.callees);
|
2018-05-30 06:56:14 +00:00
|
|
|
auto R = func_usr.try_emplace({u.first}, func_usr.size());
|
|
|
|
if (R.second)
|
|
|
|
funcs.emplace_back();
|
|
|
|
QueryFunc& existing = funcs[R.first->second];
|
2018-05-04 04:20:10 +00:00
|
|
|
existing.usr = u.first;
|
2018-05-30 06:56:14 +00:00
|
|
|
if (!TryReplaceDef(existing.def, std::move(def)))
|
2018-05-13 20:30:24 +00:00
|
|
|
existing.def.push_back(std::move(def));
|
2017-02-26 19:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 06:56:14 +00:00
|
|
|
void DB::Update(int file_id, std::vector<std::pair<Usr, QueryType::Def>>&& us) {
|
2018-05-04 04:20:10 +00:00
|
|
|
for (auto& u : us) {
|
|
|
|
auto& def = u.second;
|
2018-04-30 04:49:03 +00:00
|
|
|
assert(!def.detailed_name.empty());
|
|
|
|
AssignFileId(file_id, def.spell);
|
|
|
|
AssignFileId(file_id, def.extent);
|
2018-05-30 06:56:14 +00:00
|
|
|
auto R = type_usr.try_emplace({u.first}, type_usr.size());
|
|
|
|
if (R.second)
|
|
|
|
types.emplace_back();
|
|
|
|
QueryType& existing = types[R.first->second];
|
2018-05-04 04:20:10 +00:00
|
|
|
existing.usr = u.first;
|
2018-05-30 06:56:14 +00:00
|
|
|
if (!TryReplaceDef(existing.def, std::move(def)))
|
2018-05-13 20:30:24 +00:00
|
|
|
existing.def.push_back(std::move(def));
|
2018-05-30 06:56:14 +00:00
|
|
|
|
2017-02-26 19:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-26 08:11:47 +00:00
|
|
|
|
2018-05-30 06:56:14 +00:00
|
|
|
void DB::Update(int file_id, std::vector<std::pair<Usr, QueryVar::Def>>&& us) {
|
2018-05-04 04:20:10 +00:00
|
|
|
for (auto& u : us) {
|
|
|
|
auto& def = u.second;
|
2018-04-30 04:49:03 +00:00
|
|
|
assert(!def.detailed_name.empty());
|
|
|
|
AssignFileId(file_id, def.spell);
|
|
|
|
AssignFileId(file_id, def.extent);
|
2018-05-30 06:56:14 +00:00
|
|
|
auto R = var_usr.try_emplace({u.first}, var_usr.size());
|
|
|
|
if (R.second)
|
|
|
|
vars.emplace_back();
|
|
|
|
QueryVar& existing = vars[R.first->second];
|
2018-05-04 04:20:10 +00:00
|
|
|
existing.usr = u.first;
|
2018-05-30 06:56:14 +00:00
|
|
|
if (!TryReplaceDef(existing.def, std::move(def)))
|
2018-05-13 20:30:24 +00:00
|
|
|
existing.def.push_back(std::move(def));
|
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-05-30 06:56:14 +00:00
|
|
|
std::string_view DB::GetSymbolName(SymbolIdx sym, bool qualified) {
|
|
|
|
Usr usr = sym.usr;
|
|
|
|
switch (sym.kind) {
|
2018-02-02 01:59:01 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case SymbolKind::File:
|
2018-04-30 04:49:03 +00:00
|
|
|
if (files[usr].def)
|
|
|
|
return files[usr].def->path;
|
2018-02-02 01:59:01 +00:00
|
|
|
break;
|
|
|
|
case SymbolKind::Func:
|
2018-04-30 04:49:03 +00:00
|
|
|
if (const auto* def = Func(usr).AnyDef())
|
2018-04-06 00:00:07 +00:00
|
|
|
return def->Name(qualified);
|
2018-02-02 01:59:01 +00:00
|
|
|
break;
|
|
|
|
case SymbolKind::Type:
|
2018-04-30 04:49:03 +00:00
|
|
|
if (const auto* def = Type(usr).AnyDef())
|
2018-04-06 00:00:07 +00:00
|
|
|
return def->Name(qualified);
|
2018-02-02 01:59:01 +00:00
|
|
|
break;
|
|
|
|
case SymbolKind::Var:
|
2018-04-30 04:49:03 +00:00
|
|
|
if (const auto* def = Var(usr).AnyDef())
|
2018-04-06 00:00:07 +00:00
|
|
|
return def->Name(qualified);
|
2018-02-02 01:59:01 +00:00
|
|
|
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
|
|
|
}
|