From fffb3d3fcc2be94b262a8497e16396c0437997a5 Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Sat, 8 Apr 2017 01:04:38 -0700 Subject: [PATCH] Migrate QueryableFuncDef to indices --- src/command_line.cc | 45 +++++++++++++++++++++++++++++++++++++-------- src/indexer.cc | 4 ++-- src/indexer.h | 10 +++++----- src/query.cc | 45 ++++++++++++++++++++++++++------------------- src/query.h | 28 +++++++++++++++++++++++----- 5 files changed, 93 insertions(+), 39 deletions(-) diff --git a/src/command_line.cc b/src/command_line.cc index 199a70f1..0767ddf8 100644 --- a/src/command_line.cc +++ b/src/command_line.cc @@ -478,6 +478,22 @@ void AddCodeLens( AddCodeLens(db, result, loc, uses0, exclude_loc, only_interesting, singular, plural); } +void AddCodeLens( + QueryableDatabase* db, + std::vector* result, + QueryableLocation loc, + const std::vector& uses, + bool exclude_loc, + bool only_interesting, + const char* singular, + const char* plural) { + std::vector uses0; + uses0.reserve(uses.size()); + for (const QueryFuncRef& use : uses) + uses0.push_back(use.loc); + AddCodeLens(db, result, loc, uses0, exclude_loc, only_interesting, singular, plural); +} + void AddCodeLens( QueryableDatabase* db, std::vector* result, @@ -516,6 +532,25 @@ void AddCodeLens( AddCodeLens(db, result, loc, uses0, exclude_loc, only_interesting, singular, plural); } +void AddCodeLens( + QueryableDatabase* db, + std::vector* result, + QueryableLocation loc, + const std::vector& usrs, + bool exclude_loc, + bool only_interesting, + const char* singular, + const char* plural) { + std::vector uses0; + uses0.reserve(usrs.size()); + for (const QueryFuncId& usr : usrs) { + optional loc = GetDefinitionSpellingOfSymbol(db, usr); + if (loc) + uses0.push_back(loc.value()); + } + AddCodeLens(db, result, loc, uses0, exclude_loc, only_interesting, singular, plural); +} + void AddCodeLens( QueryableDatabase* db, std::vector* result, @@ -705,10 +740,7 @@ void QueryDbMainLoop( info.name = def.def.qualified_name; if (def.def.declaring_type.has_value()) { info.kind = lsSymbolKind::Method; - Usr declaring = def.def.declaring_type.value(); - info.containerName = - db->types[db->usr_to_symbol[declaring].idx] - .def.qualified_name; + info.containerName = db->types[def.def.declaring_type->id].def.qualified_name; } else { info.kind = lsSymbolKind::Function; @@ -852,10 +884,7 @@ void QueryDbMainLoop( info.name = def.def.qualified_name; if (def.def.declaring_type.has_value()) { info.kind = lsSymbolKind::Method; - Usr declaring = def.def.declaring_type.value(); - info.containerName = - db->types[db->usr_to_symbol[declaring].idx] - .def.qualified_name; + info.containerName = db->types[def.def.declaring_type->id].def.qualified_name; } else { info.kind = lsSymbolKind::Function; diff --git a/src/indexer.cc b/src/indexer.cc index 7d74b660..ac866201 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -1140,8 +1140,8 @@ void indexEntityReference(CXClientData client_data, IndexedFuncDef* caller_def = db->Resolve(caller_id); IndexedFuncDef* called_def = db->Resolve(called_id); - caller_def->def.callees.push_back(FuncRef(called_id, loc_spelling.value())); - called_def->callers.push_back(FuncRef(caller_id, loc_spelling.value())); + caller_def->def.callees.push_back(IndexFuncRef(called_id, loc_spelling.value())); + called_def->callers.push_back(IndexFuncRef(caller_id, loc_spelling.value())); AddUsage(called_def->uses, loc_spelling.value()); } else { IndexedFuncDef* called_def = db->Resolve(called_id); diff --git a/src/indexer.h b/src/indexer.h index 50777a5a..e1555509 100644 --- a/src/indexer.h +++ b/src/indexer.h @@ -83,9 +83,9 @@ bool operator!=(const Ref& a, const Ref& b) { return !(a == b); } -using TypeRef = Ref; -using FuncRef = Ref; -using VarRef = Ref; +using IndexTypeRef = Ref; +using IndexFuncRef = Ref; +using IndexVarRef = Ref; // TODO: skip as much forward-processing as possible when |is_system_def| is // set to false. @@ -272,7 +272,7 @@ void Reflect( } struct IndexedFuncDef { - using Def = FuncDefDefinitionData; + using Def = FuncDefDefinitionData; Def def; IndexFuncId id; @@ -289,7 +289,7 @@ struct IndexedFuncDef { // or in class initializer list (redirect to class ctor?) // - Right now those usages will not get listed here (but they should be // inside of all_uses). - std::vector callers; + std::vector callers; // All usages. For interesting usages, see callees. std::vector uses; diff --git a/src/query.cc b/src/query.cc index 7e87b340..1917dced 100644 --- a/src/query.cc +++ b/src/query.cc @@ -34,7 +34,7 @@ QueryableLocation MapIdToUsr(const IdMap& id_map, const Range& range) { return QueryableLocation(id_map.primary_file, range); } // TODO: remove -UsrRef MapIdToUsr(const IdMap& id_map, const FuncRef& id) { +UsrRef MapIdToUsr(const IdMap& id_map, const IndexFuncRef& id) { assert(id_map.local_ids.func_id_to_usr.find(id.id) != id_map.local_ids.func_id_to_usr.end()); return UsrRef( id_map.local_ids.func_id_to_usr.find(id.id)->second /*usr*/, @@ -65,8 +65,8 @@ std::vector MapIdToUsr(const IdMap& id_map, const std::vector& return Transform(id_map, ids); } // TODO: remove -std::vector MapIdToUsr(const IdMap& id_map, const std::vector& ids) { - return Transform(id_map, ids); +std::vector MapIdToUsr(const IdMap& id_map, const std::vector& ids) { + return Transform(id_map, ids); } std::vector MapIdToUsr(const IdMap& id_map, const std::vector& ids) { return Transform(id_map, ids); @@ -91,16 +91,12 @@ QueryableFuncDef::DefUpdate MapIdToUsr(const IdMap& id_map, const IndexedFuncDef QueryableFuncDef::DefUpdate result(def.usr); result.short_name = def.short_name; result.qualified_name = def.qualified_name; - if (def.definition_spelling) - result.definition_spelling = MapIdToUsr(id_map, def.definition_spelling.value()); - if (def.definition_extent) - result.definition_extent = MapIdToUsr(id_map, def.definition_extent.value()); - if (def.declaring_type) - result.declaring_type = MapIdToUsr(id_map, def.declaring_type.value()); - if (def.base) - result.base = MapIdToUsr(id_map, def.base.value()); - result.locals = MapIdToUsr(id_map, def.locals); - result.callees = MapIdToUsr(id_map, def.callees); + result.definition_spelling = id_map.ToQuery(def.definition_spelling); + result.definition_extent = id_map.ToQuery(def.definition_extent); + result.declaring_type = id_map.ToQuery(def.declaring_type); + result.base = id_map.ToQuery(def.base); + result.locals = id_map.ToQuery(def.locals); + result.callees = id_map.ToQuery(def.callees); return result; } QueryableVarDef::DefUpdate MapIdToUsr(const IdMap& id_map, const IndexedVarDef::Def& def) { @@ -195,8 +191,8 @@ QueryableTypeDef::QueryableTypeDef(const IdMap& id_map, const IndexedTypeDef& in QueryableFuncDef::QueryableFuncDef(const IdMap& id_map, const IndexedFuncDef& indexed) : def(MapIdToUsr(id_map, indexed.def)) { declarations = MapIdToUsr(id_map, indexed.declarations); - derived = MapIdToUsr(id_map, indexed.derived); - callers = MapIdToUsr(id_map, indexed.callers); + derived = id_map.ToQuery(indexed.derived); + callers = id_map.ToQuery(indexed.callers); uses = MapIdToUsr(id_map, indexed.uses); } @@ -438,6 +434,9 @@ QueryVarId IdMap::ToQuery(IndexVarId id) const { assert(cached_var_ids_.find(id.id) != cached_var_ids_.end()); return QueryVarId(cached_var_ids_.find(id.id)->second); } +QueryFuncRef IdMap::ToQuery(IndexFuncRef ref) const { + return QueryFuncRef(ToQuery(ref.id), ToQuery(ref.loc)); +} optional IdMap::ToQuery(optional range) const { if (!range) @@ -459,6 +458,11 @@ optional IdMap::ToQuery(optional id) const { return nullopt; return ToQuery(id.value()); } +optional IdMap::ToQuery(optional ref) const { + if (!ref) + return nullopt; + return ToQuery(ref.value()); +} template std::vector ToQueryTransform(const IdMap& id_map, const std::vector& input) { @@ -480,6 +484,9 @@ std::vector IdMap::ToQuery(std::vector ids) const { std::vector IdMap::ToQuery(std::vector ids) const { return ToQueryTransform(*this, ids); } +std::vector IdMap::ToQuery(std::vector refs) const { + return ToQueryTransform(*this, refs); +} SymbolIdx IdMap::ToSymbol(IndexTypeId id) const { return SymbolIdx(SymbolKind::Type, ToQuery(id).id); @@ -596,10 +603,10 @@ IndexUpdate::IndexUpdate(const IdMap& previous_id_map, const IdMap& current_id_m if (previous_remapped_def != current_remapped_def) funcs_def_update.push_back(current_remapped_def); - PROCESS_UPDATE_DIFF(funcs_declarations, declarations, QueryableLocation); - PROCESS_UPDATE_DIFF(funcs_derived, derived, Usr); - PROCESS_UPDATE_DIFF(funcs_callers, callers, UsrRef); - PROCESS_UPDATE_DIFF(funcs_uses, uses, QueryableLocation); + PROCESS_UPDATE_DIFF2(funcs_declarations, declarations, QueryableLocation); + PROCESS_UPDATE_DIFF2(funcs_derived, derived, QueryFuncId); + PROCESS_UPDATE_DIFF2(funcs_callers, callers, QueryFuncRef); + PROCESS_UPDATE_DIFF2(funcs_uses, uses, QueryableLocation); }); // Variables diff --git a/src/query.h b/src/query.h index ca954b8d..86b82f96 100644 --- a/src/query.h +++ b/src/query.h @@ -88,6 +88,21 @@ struct SymbolRef { } }; +struct QueryFuncRef { + QueryFuncId id; + QueryableLocation loc; + + QueryFuncRef(QueryFuncId id, QueryableLocation loc) : id(id), loc(loc) {} + + bool operator==(const QueryFuncRef& that) const { + return id == that.id && loc == that.loc; + } + bool operator!=(const QueryFuncRef& that) const { return !(*this == that); } + bool operator<(const QueryFuncRef& that) const { + return id < that.id && loc.range.start < that.loc.range.start; + } +}; + struct UsrRef { Usr usr; QueryableLocation loc; @@ -175,16 +190,16 @@ struct QueryableTypeDef { }; struct QueryableFuncDef { - using DefUpdate = FuncDefDefinitionData; + using DefUpdate = FuncDefDefinitionData; using DeclarationsUpdate = MergeableUpdate; - using DerivedUpdate = MergeableUpdate; - using CallersUpdate = MergeableUpdate; + using DerivedUpdate = MergeableUpdate; + using CallersUpdate = MergeableUpdate; using UsesUpdate = MergeableUpdate; DefUpdate def; std::vector declarations; - std::vector derived; - std::vector callers; + std::vector derived; + std::vector callers; std::vector uses; size_t qualified_name_idx = -1; @@ -299,14 +314,17 @@ struct IdMap { QueryTypeId ToQuery(IndexTypeId id) const; QueryFuncId ToQuery(IndexFuncId id) const; QueryVarId ToQuery(IndexVarId id) const; + QueryFuncRef ToQuery(IndexFuncRef ref) const; optional ToQuery(optional range) const; optional ToQuery(optional id) const; optional ToQuery(optional id) const; optional ToQuery(optional id) const; + optional ToQuery(optional ref) const; std::vector ToQuery(std::vector ranges) const; std::vector ToQuery(std::vector ids) const; std::vector ToQuery(std::vector ids) const; std::vector ToQuery(std::vector ids) const; + std::vector ToQuery(std::vector refs) const; SymbolIdx ToSymbol(IndexTypeId id) const; SymbolIdx ToSymbol(IndexFuncId id) const;