From 273af8306fa0b89ec28eca20b8003a550c5c7d26 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sat, 20 Jan 2018 10:32:39 -0800 Subject: [PATCH] Move GetQueryFileIdFromPath to unnamed namespace, make them inline and optional --- .travis.yml | 2 +- src/messages/cquery_call_tree.cc | 9 +++--- src/query.cc | 54 +++++++++++++++++--------------- src/query.h | 10 +++--- 4 files changed, 38 insertions(+), 37 deletions(-) diff --git a/.travis.yml b/.travis.yml index f1499dfb..e49e73df 100644 --- a/.travis.yml +++ b/.travis.yml @@ -138,4 +138,4 @@ notifications: channels: - "ircs://chat.freenode.net:6697/#cquery" template: - - "[ %{repository_slug}#%{commit}] %{result} on %{branch} by %{author} (%{build_url})" + - "[ %{repository_slug}#%{commit}] %{result} on %{branch} by %{author} (%{build_url} )" diff --git a/src/messages/cquery_call_tree.cc b/src/messages/cquery_call_tree.cc index a768b47e..8f0bdb9a 100644 --- a/src/messages/cquery_call_tree.cc +++ b/src/messages/cquery_call_tree.cc @@ -187,10 +187,11 @@ struct CqueryCallTreeExpandHandler Out_CqueryCallTree out; out.id = request->id; - // FIXME Change VSCode plugin to use number representation of USR hash - QueryFuncId func_id = db->GetQueryFuncIdFromUsr(std::stoull(request->params.usr)); - if (func_id.id != QueryFuncId::INVALID_ID) - out.result = BuildExpandCallTree(db, working_files, func_id); + // FIXME + optional func_id = + db->GetQueryFuncIdFromUsr(std::stoull(request->params.usr)); + if (func_id) + out.result = BuildExpandCallTree(db, working_files, func_id.value()); QueueManager::WriteStdout(IpcId::CqueryCallTreeExpand, out); } diff --git a/src/query.cc b/src/query.cc index f917644e..2f300510 100644 --- a/src/query.cc +++ b/src/query.cc @@ -279,16 +279,14 @@ QueryFile::Def BuildFileDef(const IdMap& id_map, const IndexFile& indexed) { return def; } -} // namespace - -QueryFileId GetQueryFileIdFromPath(QueryDatabase* query_db, - const std::string& path, - bool create_if_missing) { +inline optional GetQueryFileIdFromPath(QueryDatabase* query_db, + const std::string& path, + bool create_if_missing) { auto it = query_db->usr_to_file.find(LowerPathIfCaseInsensitive(path)); if (it != query_db->usr_to_file.end()) return QueryFileId(it->second.id); if (!create_if_missing) - return QueryFileId(QueryFileId::INVALID_ID); + return {}; size_t idx = query_db->files.size(); query_db->usr_to_file[LowerPathIfCaseInsensitive(path)] = QueryFileId(idx); @@ -296,14 +294,14 @@ QueryFileId GetQueryFileIdFromPath(QueryDatabase* query_db, return QueryFileId(idx); } -QueryTypeId GetQueryTypeIdFromUsr(QueryDatabase* query_db, - Usr usr, - bool create_if_missing) { +inline optional GetQueryTypeIdFromUsr(QueryDatabase* query_db, + Usr usr, + bool create_if_missing) { auto it = query_db->usr_to_type.find(usr); if (it != query_db->usr_to_type.end()) return QueryTypeId(it->second.id); if (!create_if_missing) - return QueryTypeId(QueryTypeId::INVALID_ID); + return {}; size_t idx = query_db->types.size(); query_db->usr_to_type[usr] = QueryTypeId(idx); @@ -311,28 +309,29 @@ QueryTypeId GetQueryTypeIdFromUsr(QueryDatabase* query_db, return QueryTypeId(idx); } -QueryFuncId GetQueryFuncIdFromUsr(QueryDatabase* query_db, - Usr usr, - bool create_if_missing) { +inline optional GetQueryFuncIdFromUsr(QueryDatabase* query_db, + Usr usr, + bool create_if_missing) { auto it = query_db->usr_to_func.find(usr); if (it != query_db->usr_to_func.end()) return QueryFuncId(it->second.id); if (!create_if_missing) - return QueryFuncId(QueryFuncId::INVALID_ID); + return {}; + size_t idx = query_db->funcs.size(); query_db->usr_to_func[usr] = QueryFuncId(idx); query_db->funcs.push_back(QueryFunc(usr)); return QueryFuncId(idx); } -QueryVarId GetQueryVarIdFromUsr(QueryDatabase* query_db, - Usr usr, - bool create_if_missing) { +inline optional GetQueryVarIdFromUsr(QueryDatabase* query_db, + Usr usr, + bool create_if_missing) { auto it = query_db->usr_to_var.find(usr); if (it != query_db->usr_to_var.end()) return QueryVarId(it->second.id); if (!create_if_missing) - return QueryVarId(QueryVarId::INVALID_ID); + return {}; size_t idx = query_db->vars.size(); query_db->usr_to_var[usr] = QueryVarId(idx); @@ -340,42 +339,45 @@ QueryVarId GetQueryVarIdFromUsr(QueryDatabase* query_db, return QueryVarId(idx); } -QueryFileId QueryDatabase::GetQueryFileIdFromPath( +} // namespace + +optional QueryDatabase::GetQueryFileIdFromPath( const std::string& path) { return ::GetQueryFileIdFromPath(this, path, false); } -QueryTypeId QueryDatabase::GetQueryTypeIdFromUsr(Usr usr) { +optional QueryDatabase::GetQueryTypeIdFromUsr(Usr usr) { return ::GetQueryTypeIdFromUsr(this, usr, false); } -QueryFuncId QueryDatabase::GetQueryFuncIdFromUsr(Usr usr) { +optional QueryDatabase::GetQueryFuncIdFromUsr(Usr usr) { return ::GetQueryFuncIdFromUsr(this, usr, false); } -QueryVarId QueryDatabase::GetQueryVarIdFromUsr(Usr usr) { +optional QueryDatabase::GetQueryVarIdFromUsr(Usr usr) { return ::GetQueryVarIdFromUsr(this, usr, false); } IdMap::IdMap(QueryDatabase* query_db, const IdCache& local_ids) : local_ids(local_ids) { // LOG_S(INFO) << "Creating IdMap for " << local_ids.primary_file; - primary_file = GetQueryFileIdFromPath(query_db, local_ids.primary_file, true); + primary_file = + GetQueryFileIdFromPath(query_db, local_ids.primary_file, true).value(); cached_type_ids_.resize(local_ids.type_id_to_usr.size()); for (const auto& entry : local_ids.type_id_to_usr) cached_type_ids_[entry.first] = - GetQueryTypeIdFromUsr(query_db, entry.second, true); + GetQueryTypeIdFromUsr(query_db, entry.second, true).value(); cached_func_ids_.resize(local_ids.func_id_to_usr.size()); for (const auto& entry : local_ids.func_id_to_usr) cached_func_ids_[entry.first] = - GetQueryFuncIdFromUsr(query_db, entry.second, true); + GetQueryFuncIdFromUsr(query_db, entry.second, true).value(); cached_var_ids_.resize(local_ids.var_id_to_usr.size()); for (const auto& entry : local_ids.var_id_to_usr) cached_var_ids_[entry.first] = - GetQueryVarIdFromUsr(query_db, entry.second, true); + GetQueryVarIdFromUsr(query_db, entry.second, true).value(); } QueryLocation IdMap::ToQuery(Range range) const { diff --git a/src/query.h b/src/query.h index f5a775d4..91f21b88 100644 --- a/src/query.h +++ b/src/query.h @@ -368,12 +368,10 @@ struct QueryDatabase { const std::string& detailed_name); // Query the indexing structure to look up symbol id for given Usr. - // In case the given Usr does not exist in the indexing structure, - // |INVALID_ID| will be returned - QueryFileId GetQueryFileIdFromPath(const std::string& path); - QueryTypeId GetQueryTypeIdFromUsr(Usr usr); - QueryFuncId GetQueryFuncIdFromUsr(Usr usr); - QueryVarId GetQueryVarIdFromUsr(Usr usr); + optional GetQueryFileIdFromPath(const std::string& path); + optional GetQueryTypeIdFromUsr(Usr usr); + optional GetQueryFuncIdFromUsr(Usr usr); + optional GetQueryVarIdFromUsr(Usr usr); }; struct IdMap {