From e7c90b62b55e31377e78e2e83225d30682cbbbb6 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Fri, 9 Feb 2018 22:51:58 -0800 Subject: [PATCH] $cquery/base for virtual void f() = 0 --- src/messages/cquery_base.cc | 6 +-- src/messages/cquery_callers.cc | 4 +- src/messages/cquery_vars.cc | 8 ++-- src/messages/text_document_definition.cc | 2 +- src/messages/text_document_highlight.cc | 4 +- src/messages/text_document_hover.cc | 2 +- src/messages/text_document_rename.cc | 2 +- src/query_utils.cc | 56 +++++++++++++++++------- src/query_utils.h | 21 +++------ 9 files changed, 59 insertions(+), 46 deletions(-) diff --git a/src/messages/cquery_base.cc b/src/messages/cquery_base.cc index 70861a42..7c1fbb3f 100644 --- a/src/messages/cquery_base.cc +++ b/src/messages/cquery_base.cc @@ -33,7 +33,7 @@ struct CqueryBaseHandler : BaseMessageHandler { return (a.role & SymbolRole::Definition) > (b.role & SymbolRole::Definition); }); - for (const SymbolRef& sym : syms) { + for (SymbolRef sym : syms) { if (sym.kind == SymbolKind::Type) { QueryType& type = db->GetType(sym); if (type.def) @@ -43,8 +43,8 @@ struct CqueryBaseHandler : BaseMessageHandler { } else if (sym.kind == SymbolKind::Func) { QueryFunc& func = db->GetFunc(sym); if (func.def) - out.result = - GetLsLocations(db, working_files, ToReference(db, func.def->base)); + out.result = GetLsLocations(db, working_files, + ToReference(db, func.def->base)); break; } } diff --git a/src/messages/cquery_callers.cc b/src/messages/cquery_callers.cc index 006a72b9..58df39ee 100644 --- a/src/messages/cquery_callers.cc +++ b/src/messages/cquery_callers.cc @@ -23,10 +23,10 @@ struct CqueryCallersHandler : BaseMessageHandler { Out_LocationList out; out.id = request->id; - for (const SymbolRef& sym : + for (SymbolRef sym : FindSymbolsAtLocation(working_file, file, request->params.position)) { if (sym.kind == SymbolKind::Func) { - QueryFunc& func = db->funcs[sym.Idx()]; + QueryFunc& func = db->GetFunc(sym); std::vector uses = ToReference(db, func.callers); for (QueryFuncRef func_ref : GetCallersForAllBaseFunctions(db, func)) uses.push_back(func_ref); diff --git a/src/messages/cquery_vars.cc b/src/messages/cquery_vars.cc index 1ab4252f..079310cd 100644 --- a/src/messages/cquery_vars.cc +++ b/src/messages/cquery_vars.cc @@ -23,14 +23,14 @@ struct CqueryVarsHandler : BaseMessageHandler { Out_LocationList out; out.id = request->id; - for (const SymbolRef& ref : + for (SymbolRef sym : FindSymbolsAtLocation(working_file, file, request->params.position)) { - RawId idx = ref.Idx(); - switch (ref.kind) { + RawId idx = sym.Idx(); + switch (sym.kind) { default: break; case SymbolKind::Var: { - QueryVar& var = db->vars[idx]; + QueryVar& var = db->GetVar(sym); if (!var.def || !var.def->variable_type) continue; idx = var.def->variable_type->id; diff --git a/src/messages/text_document_definition.cc b/src/messages/text_document_definition.cc index b066592c..eff44ed0 100644 --- a/src/messages/text_document_definition.cc +++ b/src/messages/text_document_definition.cc @@ -65,7 +65,7 @@ struct TextDocumentDefinitionHandler int target_line = request->params.position.line; int target_column = request->params.position.character; - for (const SymbolRef& sym : + for (SymbolRef sym : FindSymbolsAtLocation(working_file, file, request->params.position)) { // Found symbol. Return definition. diff --git a/src/messages/text_document_highlight.cc b/src/messages/text_document_highlight.cc index 147dff3b..ca2c90f6 100644 --- a/src/messages/text_document_highlight.cc +++ b/src/messages/text_document_highlight.cc @@ -35,12 +35,12 @@ struct TextDocumentDocumentHighlightHandler Out_TextDocumentDocumentHighlight out; out.id = request->id; - for (const SymbolRef& sym : + for (SymbolRef sym : FindSymbolsAtLocation(working_file, file, request->params.position)) { // Found symbol. Return references to highlight. std::vector uses = GetUsesOfSymbol(db, sym, true); out.result.reserve(uses.size()); - for (const Reference& use : uses) { + for (Reference use : uses) { if (db->GetFileId(use) != file_id) continue; diff --git a/src/messages/text_document_hover.cc b/src/messages/text_document_hover.cc index 98a2336d..1875d958 100644 --- a/src/messages/text_document_hover.cc +++ b/src/messages/text_document_hover.cc @@ -87,7 +87,7 @@ struct TextDocumentHoverHandler : BaseMessageHandler { Out_TextDocumentHover out; out.id = request->id; - for (const SymbolRef& sym : + for (SymbolRef sym : FindSymbolsAtLocation(working_file, file, request->params.position)) { // Found symbol. Return hover. optional ls_range = GetLsRange( diff --git a/src/messages/text_document_rename.cc b/src/messages/text_document_rename.cc index 136c8523..ddbeb933 100644 --- a/src/messages/text_document_rename.cc +++ b/src/messages/text_document_rename.cc @@ -95,7 +95,7 @@ struct TextDocumentRenameHandler : BaseMessageHandler { Out_TextDocumentRename out; out.id = request->id; - for (const SymbolRef& sym : + for (SymbolRef sym : FindSymbolsAtLocation(working_file, file, request->params.position)) { // Found symbol. Return references to rename. out.result = BuildWorkspaceEdit(db, working_files, diff --git a/src/query_utils.cc b/src/query_utils.cc index ae95627a..779123b1 100644 --- a/src/query_utils.cc +++ b/src/query_utils.cc @@ -18,14 +18,6 @@ int ComputeRangeSize(const Range& range) { } // namespace -optional GetDefinitionSpellingOfSymbol(QueryDatabase* db, - const QueryTypeId& id) { - QueryType& type = db->types[id.id]; - if (type.def) - return type.def->definition_spelling; - return nullopt; -} - optional GetDefinitionSpellingOfSymbol(QueryDatabase* db, const QueryFuncId& id) { QueryFunc& func = db->funcs[id.id]; @@ -34,14 +26,6 @@ optional GetDefinitionSpellingOfSymbol(QueryDatabase* db, return nullopt; } -optional GetDefinitionSpellingOfSymbol(QueryDatabase* db, - const QueryVarId& id) { - QueryVar& var = db->vars[id.id]; - if (var.def) - return var.def->definition_spelling; - return nullopt; -} - optional GetDefinitionSpellingOfSymbol(QueryDatabase* db, SymbolRef sym) { switch (sym.kind) { @@ -145,6 +129,46 @@ std::vector ToReference(QueryDatabase* db, return ret; } +std::vector ToReference(QueryDatabase* db, + const std::vector& ids) { + std::vector ret; + ret.reserve(ids.size()); + for (auto id : ids) { + QueryFunc& func = db->funcs[id.id]; + if (func.def && func.def->definition_spelling) + ret.push_back(*func.def->definition_spelling); + else if (func.declarations.size()) + ret.push_back(func.declarations[0]); + } + return ret; +} + +std::vector ToReference(QueryDatabase* db, + const std::vector& ids) { + std::vector ret; + ret.reserve(ids.size()); + for (auto id : ids) { + QueryType& type = db->types[id.id]; + if (type.def && type.def->definition_spelling) + ret.push_back(*type.def->definition_spelling); + } + return ret; +} + +std::vector ToReference(QueryDatabase* db, + const std::vector& ids) { + std::vector ret; + ret.reserve(ids.size()); + for (auto id : ids) { + QueryVar& var = db->vars[id.id]; + if (var.def && var.def->definition_spelling) + ret.push_back(*var.def->definition_spelling); + else if (var.declarations.size()) + ret.push_back(var.declarations[0]); + } + return ret; +} + std::vector GetUsesOfSymbol(QueryDatabase* db, SymbolRef sym, bool include_decl) { diff --git a/src/query_utils.h b/src/query_utils.h index b0994fe8..783a1515 100644 --- a/src/query_utils.h +++ b/src/query_utils.h @@ -7,12 +7,8 @@ #include -optional GetDefinitionSpellingOfSymbol(QueryDatabase* db, - const QueryTypeId& id); optional GetDefinitionSpellingOfSymbol(QueryDatabase* db, const QueryFuncId& id); -optional GetDefinitionSpellingOfSymbol(QueryDatabase* db, - const QueryVarId& id); optional GetDefinitionSpellingOfSymbol(QueryDatabase* db, SymbolRef sym); optional GetDefinitionExtentOfSymbol(QueryDatabase* db, @@ -22,19 +18,12 @@ optional GetDeclarationFileForSymbol(QueryDatabase* db, std::vector ToReference(QueryDatabase* db, const std::vector& refs); - -template std::vector ToReference(QueryDatabase* db, - const std::vector>& ids) { - std::vector ret; - ret.reserve(ids.size()); - for (auto id : ids) { - optional loc = GetDefinitionSpellingOfSymbol(db, id); - if (loc) - ret.push_back(*loc); - } - return ret; -} + const std::vector& ids); +std::vector ToReference(QueryDatabase* db, + const std::vector& ids); +std::vector ToReference(QueryDatabase* db, + const std::vector& ids); std::vector GetUsesOfSymbol(QueryDatabase* db, SymbolRef sym,