Use GetQuery{Type,Func,Var}IdFromUsr for querying QueryDatabase::usr_to_{type,func_var} (#315)

* Use GetQuery{Type,Func,Var}IdFromUsr for querying usr_to_{type,func_var}

Instead of directly touching usr_to_{type,func_var}, use dedicated
methods to touch them for more abstraction around QueryDatabase.

* Use `Usr` as parameter for GetQueryVarIdFromUsr()

To maintain code consistency with other similar helpers.
This commit is contained in:
khng300 2018-01-21 00:57:41 +08:00 committed by Fangrui Song
parent b8a3e089ce
commit 427f7b79e3
3 changed files with 63 additions and 22 deletions

View File

@ -188,9 +188,10 @@ struct CqueryCallTreeExpandHandler
out.id = request->id; out.id = request->id;
// FIXME // FIXME
auto func_id = db->usr_to_func.find(std::stoull(request->params.usr)); optional<QueryFuncId> func_id =
if (func_id != db->usr_to_func.end()) db->GetQueryFuncIdFromUsr(std::stoull(request->params.usr));
out.result = BuildExpandCallTree(db, working_files, func_id->second); if (func_id)
out.result = BuildExpandCallTree(db, working_files, func_id.value());
QueueManager::WriteStdout(IpcId::CqueryCallTreeExpand, out); QueueManager::WriteStdout(IpcId::CqueryCallTreeExpand, out);
} }

View File

@ -21,7 +21,8 @@ namespace {
template <typename T> template <typename T>
void VerifyUnique(const std::vector<T>& values0) { void VerifyUnique(const std::vector<T>& values0) {
// FIXME: Run on a big code-base for a while and verify no assertions are triggered. // FIXME: Run on a big code-base for a while and verify no assertions are
// triggered.
#if false #if false
auto values = values0; auto values = values0;
std::sort(values.begin(), values.end()); std::sort(values.begin(), values.end());
@ -280,23 +281,29 @@ QueryFile::Def BuildFileDef(const IdMap& id_map, const IndexFile& indexed) {
} // namespace } // namespace
QueryFileId GetQueryFileIdFromPath(QueryDatabase* query_db, optional<QueryFileId> GetQueryFileIdFromPath(QueryDatabase* query_db,
const std::string& path) { const std::string& path,
bool create_if_missing) {
auto it = query_db->usr_to_file.find(LowerPathIfCaseInsensitive(path)); auto it = query_db->usr_to_file.find(LowerPathIfCaseInsensitive(path));
if (it != query_db->usr_to_file.end()) if (it != query_db->usr_to_file.end())
return QueryFileId(it->second.id); return QueryFileId(it->second.id);
if (!create_if_missing)
return {};
size_t idx = query_db->files.size(); size_t idx = query_db->files.size();
query_db->usr_to_file[LowerPathIfCaseInsensitive(path)] = query_db->usr_to_file[LowerPathIfCaseInsensitive(path)] = QueryFileId(idx);
QueryFileId(idx);
query_db->files.push_back(QueryFile(path)); query_db->files.push_back(QueryFile(path));
return QueryFileId(idx); return QueryFileId(idx);
} }
QueryTypeId GetQueryTypeIdFromUsr(QueryDatabase* query_db, Usr usr) { optional<QueryTypeId> GetQueryTypeIdFromUsr(QueryDatabase* query_db,
Usr usr,
bool create_if_missing) {
auto it = query_db->usr_to_type.find(usr); auto it = query_db->usr_to_type.find(usr);
if (it != query_db->usr_to_type.end()) if (it != query_db->usr_to_type.end())
return QueryTypeId(it->second.id); return QueryTypeId(it->second.id);
if (!create_if_missing)
return {};
size_t idx = query_db->types.size(); size_t idx = query_db->types.size();
query_db->usr_to_type[usr] = QueryTypeId(idx); query_db->usr_to_type[usr] = QueryTypeId(idx);
@ -304,10 +311,14 @@ QueryTypeId GetQueryTypeIdFromUsr(QueryDatabase* query_db, Usr usr) {
return QueryTypeId(idx); return QueryTypeId(idx);
} }
QueryFuncId GetQueryFuncIdFromUsr(QueryDatabase* query_db, Usr usr) { optional<QueryFuncId> GetQueryFuncIdFromUsr(QueryDatabase* query_db,
Usr usr,
bool create_if_missing) {
auto it = query_db->usr_to_func.find(usr); auto it = query_db->usr_to_func.find(usr);
if (it != query_db->usr_to_func.end()) if (it != query_db->usr_to_func.end())
return QueryFuncId(it->second.id); return QueryFuncId(it->second.id);
if (!create_if_missing)
return {};
size_t idx = query_db->funcs.size(); size_t idx = query_db->funcs.size();
query_db->usr_to_func[usr] = QueryFuncId(idx); query_db->usr_to_func[usr] = QueryFuncId(idx);
@ -315,10 +326,14 @@ QueryFuncId GetQueryFuncIdFromUsr(QueryDatabase* query_db, Usr usr) {
return QueryFuncId(idx); return QueryFuncId(idx);
} }
QueryVarId GetQueryVarIdFromUsr(QueryDatabase* query_db, const Usr& usr) { optional<QueryVarId> GetQueryVarIdFromUsr(QueryDatabase* query_db,
Usr usr,
bool create_if_missing) {
auto it = query_db->usr_to_var.find(usr); auto it = query_db->usr_to_var.find(usr);
if (it != query_db->usr_to_var.end()) if (it != query_db->usr_to_var.end())
return QueryVarId(it->second.id); return QueryVarId(it->second.id);
if (!create_if_missing)
return {};
size_t idx = query_db->vars.size(); size_t idx = query_db->vars.size();
query_db->usr_to_var[usr] = QueryVarId(idx); query_db->usr_to_var[usr] = QueryVarId(idx);
@ -326,24 +341,43 @@ QueryVarId GetQueryVarIdFromUsr(QueryDatabase* query_db, const Usr& usr) {
return QueryVarId(idx); return QueryVarId(idx);
} }
optional<QueryFileId> QueryDatabase::GetQueryFileIdFromPath(
const std::string& path) {
return ::GetQueryFileIdFromPath(this, path, false);
}
optional<QueryTypeId> QueryDatabase::GetQueryTypeIdFromUsr(Usr usr) {
return ::GetQueryTypeIdFromUsr(this, usr, false);
}
optional<QueryFuncId> QueryDatabase::GetQueryFuncIdFromUsr(Usr usr) {
return ::GetQueryFuncIdFromUsr(this, usr, false);
}
optional<QueryVarId> QueryDatabase::GetQueryVarIdFromUsr(Usr usr) {
return ::GetQueryVarIdFromUsr(this, usr, false);
}
IdMap::IdMap(QueryDatabase* query_db, const IdCache& local_ids) IdMap::IdMap(QueryDatabase* query_db, const IdCache& local_ids)
: local_ids(local_ids) { : local_ids(local_ids) {
// LOG_S(INFO) << "Creating IdMap for " << local_ids.primary_file; // LOG_S(INFO) << "Creating IdMap for " << local_ids.primary_file;
primary_file = GetQueryFileIdFromPath(query_db, local_ids.primary_file); primary_file =
GetQueryFileIdFromPath(query_db, local_ids.primary_file, true).value();
cached_type_ids_.resize(local_ids.type_id_to_usr.size()); cached_type_ids_.resize(local_ids.type_id_to_usr.size());
for (const auto& entry : local_ids.type_id_to_usr) for (const auto& entry : local_ids.type_id_to_usr)
cached_type_ids_[entry.first] = cached_type_ids_[entry.first] =
GetQueryTypeIdFromUsr(query_db, entry.second); GetQueryTypeIdFromUsr(query_db, entry.second, true).value();
cached_func_ids_.resize(local_ids.func_id_to_usr.size()); cached_func_ids_.resize(local_ids.func_id_to_usr.size());
for (const auto& entry : local_ids.func_id_to_usr) for (const auto& entry : local_ids.func_id_to_usr)
cached_func_ids_[entry.first] = cached_func_ids_[entry.first] =
GetQueryFuncIdFromUsr(query_db, entry.second); GetQueryFuncIdFromUsr(query_db, entry.second, true).value();
cached_var_ids_.resize(local_ids.var_id_to_usr.size()); cached_var_ids_.resize(local_ids.var_id_to_usr.size());
for (const auto& entry : local_ids.var_id_to_usr) for (const auto& entry : local_ids.var_id_to_usr)
cached_var_ids_[entry.first] = GetQueryVarIdFromUsr(query_db, entry.second); cached_var_ids_[entry.first] =
GetQueryVarIdFromUsr(query_db, entry.second, true).value();
} }
QueryLocation IdMap::ToQuery(Range range) const { QueryLocation IdMap::ToQuery(Range range) const {
@ -717,7 +751,7 @@ void QueryDatabase::RemoveUsrs(SymbolKind usr_kind,
switch (usr_kind) { switch (usr_kind) {
case SymbolKind::File: { case SymbolKind::File: {
// FIXME // FIXME
//for (const Usr& usr : to_remove) // for (const Usr& usr : to_remove)
// files[usr_to_file[usr].id].def = nullopt; // files[usr_to_file[usr].id].def = nullopt;
break; break;
} }
@ -900,12 +934,12 @@ TEST_SUITE("query") {
IndexFile previous("foo.cc", nullopt); IndexFile previous("foo.cc", nullopt);
IndexFile current("foo.cc", nullopt); IndexFile current("foo.cc", nullopt);
previous.Resolve(previous.ToTypeId(HashUsr("usr1")))->def.definition_spelling = previous.Resolve(previous.ToTypeId(HashUsr("usr1")))
Range(Position(1, 0)); ->def.definition_spelling = Range(Position(1, 0));
previous.Resolve(previous.ToFuncId(HashUsr("usr2")))->def.definition_spelling = previous.Resolve(previous.ToFuncId(HashUsr("usr2")))
Range(Position(2, 0)); ->def.definition_spelling = Range(Position(2, 0));
previous.Resolve(previous.ToVarId(HashUsr("usr3")))->def.definition_spelling = previous.Resolve(previous.ToVarId(HashUsr("usr3")))
Range(Position(3, 0)); ->def.definition_spelling = Range(Position(3, 0));
IndexUpdate update = GetDelta(previous, current); IndexUpdate update = GetDelta(previous, current);

View File

@ -366,6 +366,12 @@ struct QueryDatabase {
size_t symbol_index, size_t symbol_index,
const std::string& short_name, const std::string& short_name,
const std::string& detailed_name); const std::string& detailed_name);
// Query the indexing structure to look up symbol id for given Usr.
optional<QueryFileId> GetQueryFileIdFromPath(const std::string& path);
optional<QueryTypeId> GetQueryTypeIdFromUsr(Usr usr);
optional<QueryFuncId> GetQueryFuncIdFromUsr(Usr usr);
optional<QueryVarId> GetQueryVarIdFromUsr(Usr usr);
}; };
struct IdMap { struct IdMap {