From c28426bbb405bac685ec2303b549fedc6a616076 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Fri, 9 Feb 2018 19:07:45 -0800 Subject: [PATCH] Remove QueryLocation and clean up --- src/message_handler.cc | 6 ++-- src/messages/cquery_base.cc | 4 +-- src/messages/cquery_call_tree.cc | 1 - src/messages/cquery_derived.cc | 4 +-- src/messages/cquery_member_hierarchy.cc | 2 +- src/messages/cquery_type_hierarchy_tree.cc | 2 +- src/messages/text_document_code_action.cc | 18 +++++----- src/messages/text_document_code_lens.cc | 38 ++++++++++++---------- src/messages/text_document_definition.cc | 2 +- src/messages/text_document_highlight.cc | 2 +- src/messages/text_document_rename.cc | 2 +- src/query.cc | 15 --------- src/query.h | 37 --------------------- src/query_utils.cc | 26 +++++++-------- 14 files changed, 55 insertions(+), 104 deletions(-) diff --git a/src/message_handler.cc b/src/message_handler.cc index 980a1599..28baf160 100644 --- a/src/message_handler.cc +++ b/src/message_handler.cc @@ -117,7 +117,7 @@ void EmitSemanticHighlighting(QueryDatabase* db, // This switch statement also filters out symbols that are not highlighted. switch (sym.kind) { case SymbolKind::Func: { - QueryFunc& func = sym.Func(db); + QueryFunc& func = db->GetFunc(sym); if (!func.def) continue; // applies to for loop // Don't highlight overloadable operators or implicit lambda -> @@ -148,7 +148,7 @@ void EmitSemanticHighlighting(QueryDatabase* db, break; } case SymbolKind::Var: { - QueryVar& var = sym.Var(db); + QueryVar& var = db->GetVar(sym); if (!var.def) continue; // applies to for loop parent_kind = var.def->parent_kind; @@ -158,7 +158,7 @@ void EmitSemanticHighlighting(QueryDatabase* db, break; } case SymbolKind::Type: { - QueryType& type = sym.Type(db); + QueryType& type = db->GetType(sym); if (!type.def) continue; // applies to for loop kind = type.def->kind; diff --git a/src/messages/cquery_base.cc b/src/messages/cquery_base.cc index ebe89a8f..70861a42 100644 --- a/src/messages/cquery_base.cc +++ b/src/messages/cquery_base.cc @@ -35,13 +35,13 @@ struct CqueryBaseHandler : BaseMessageHandler { }); for (const SymbolRef& sym : syms) { if (sym.kind == SymbolKind::Type) { - QueryType& type = sym.Type(db); + QueryType& type = db->GetType(sym); if (type.def) out.result = GetLsLocations(db, working_files, ToReference(db, type.def->parents)); break; } else if (sym.kind == SymbolKind::Func) { - QueryFunc& func = sym.Func(db); + QueryFunc& func = db->GetFunc(sym); if (func.def) out.result = GetLsLocations(db, working_files, ToReference(db, func.def->base)); diff --git a/src/messages/cquery_call_tree.cc b/src/messages/cquery_call_tree.cc index 13feaa4d..b277dec4 100644 --- a/src/messages/cquery_call_tree.cc +++ b/src/messages/cquery_call_tree.cc @@ -79,7 +79,6 @@ std::vector BuildExpandCallTree( return {}; std::vector result; - std::unordered_set seen_locations; auto handle_caller = [&](QueryFuncRef caller, Out_CqueryCallTree::CallType call_type) { diff --git a/src/messages/cquery_derived.cc b/src/messages/cquery_derived.cc index db09cfbf..aaf1cfec 100644 --- a/src/messages/cquery_derived.cc +++ b/src/messages/cquery_derived.cc @@ -35,12 +35,12 @@ struct CqueryDerivedHandler : BaseMessageHandler { }); for (const SymbolRef& sym : syms) { if (sym.kind == SymbolKind::Type) { - QueryType& type = sym.Type(db); + QueryType& type = db->GetType(sym); out.result = GetLsLocations(db, working_files, ToReference(db, type.derived)); break; } else if (sym.kind == SymbolKind::Func) { - QueryFunc& func = sym.Func(db); + QueryFunc& func = db->GetFunc(sym); out.result = GetLsLocations(db, working_files, ToReference(db, func.derived)); break; diff --git a/src/messages/cquery_member_hierarchy.cc b/src/messages/cquery_member_hierarchy.cc index 62cb21fe..0749b954 100644 --- a/src/messages/cquery_member_hierarchy.cc +++ b/src/messages/cquery_member_hierarchy.cc @@ -99,7 +99,7 @@ struct CqueryMemberHierarchyInitialHandler break; } if (sym.kind == SymbolKind::Var) { - QueryVar& var = sym.Var(db); + QueryVar& var = db->GetVar(sym); if (var.def && var.def->variable_type) out.result = BuildInitial(db, working_files, *var.def->variable_type); break; diff --git a/src/messages/cquery_type_hierarchy_tree.cc b/src/messages/cquery_type_hierarchy_tree.cc index 9e371e07..ac30ff16 100644 --- a/src/messages/cquery_type_hierarchy_tree.cc +++ b/src/messages/cquery_type_hierarchy_tree.cc @@ -168,7 +168,7 @@ struct CqueryTypeHierarchyTreeHandler for (const SymbolRef& sym : FindSymbolsAtLocation(working_file, file, request->params.position)) { if (sym.kind == SymbolKind::Type) { - QueryType& type = sym.Type(db); + QueryType& type = db->GetType(sym); if (type.def) out.result = BuildInheritanceHierarchyForType(db, working_files, type); diff --git a/src/messages/text_document_code_action.cc b/src/messages/text_document_code_action.cc index 589ae3d9..09fc97b2 100644 --- a/src/messages/text_document_code_action.cc +++ b/src/messages/text_document_code_action.cc @@ -69,22 +69,22 @@ optional GetImplementationFile(QueryDatabase* db, for (SymbolRef sym : file->def->outline) { switch (sym.kind) { case SymbolKind::Func: { - QueryFunc& func = sym.Func(db); + QueryFunc& func = db->GetFunc(sym); // Note: we ignore the definition if it is in the same file (ie, // possibly a header). if (func.def && func.def->definition_extent) { - QueryFileId t = GetFileId(db, *func.def->definition_extent); + QueryFileId t = db->GetFileId(*func.def->definition_extent); if (t != file_id) return t; } break; } case SymbolKind::Var: { - QueryVar& var = sym.Var(db); + QueryVar& var = db->GetVar(sym); // Note: we ignore the definition if it is in the same file (ie, // possibly a header). if (var.def && var.def->definition_extent) { - QueryFileId t = GetFileId(db, *var.def->definition_extent); + QueryFileId t = db->GetFileId(*var.def->definition_extent); if (t != file_id) return t; } @@ -151,7 +151,7 @@ optional BuildAutoImplementForFunction(QueryDatabase* db, QueryFunc& func) { assert(func.def); for (const Reference& decl : func.declarations) { - if (GetFileId(db, decl) != decl_file_id) + if (db->GetFileId(decl) != decl_file_id) continue; optional ls_decl = GetLsRange(working_file, decl.range); @@ -200,12 +200,12 @@ optional BuildAutoImplementForFunction(QueryDatabase* db, for (SymbolRef sym : file.def->outline) { switch (sym.kind) { case SymbolKind::Func: { - QueryFunc& sym_func = sym.Func(db); + QueryFunc& sym_func = db->GetFunc(sym); if (!sym_func.def || !sym_func.def->definition_extent) break; for (const Reference& func_decl : sym_func.declarations) { - if (GetFileId(db, func_decl) == decl_file_id) { + if (db->GetFileId(func_decl) == decl_file_id) { int dist = func_decl.range.start.line - decl.range.start.line; if (abs(dist) < abs(best_dist)) { optional def_loc = GetLsLocation( @@ -346,7 +346,7 @@ struct TextDocumentCodeActionHandler for (SymbolRef sym : syms) { switch (sym.kind) { case SymbolKind::Type: { - QueryType& type = sym.Type(db); + QueryType& type = db->GetType(sym); if (!type.def) break; @@ -397,7 +397,7 @@ struct TextDocumentCodeActionHandler } case SymbolKind::Func: { - QueryFunc& func = sym.Func(db); + QueryFunc& func = db->GetFunc(sym); if (!func.def || func.def->definition_extent) break; diff --git a/src/messages/text_document_code_lens.cc b/src/messages/text_document_code_lens.cc index 6a577bb6..8c5fc8f2 100644 --- a/src/messages/text_document_code_lens.cc +++ b/src/messages/text_document_code_lens.cc @@ -76,11 +76,15 @@ struct CommonCodeLensParams { WorkingFile* working_file; }; -// FIXME Reference +SymbolRef OffsetStartColumn(SymbolRef sym, int16_t offset) { + sym.range.start.column += offset; + return sym; +} + void AddCodeLens(const char* singular, const char* plural, CommonCodeLensParams* common, - QueryLocation loc, + SymbolRef loc, const std::vector& uses, bool force_display) { TCodeLens code_lens; @@ -90,7 +94,8 @@ void AddCodeLens(const char* singular, code_lens.range = *range; code_lens.command = lsCommand(); code_lens.command->command = "cquery.showReferences"; - code_lens.command->arguments.uri = GetLsDocumentUri(common->db, loc.FileId()); + code_lens.command->arguments.uri = + GetLsDocumentUri(common->db, common->db->GetFileId(loc)); code_lens.command->arguments.position = code_lens.range.start; // Add unique uses. @@ -146,22 +151,21 @@ struct TextDocumentCodeLensHandler switch (sym.kind) { case SymbolKind::Type: { - QueryType& type = sym.Type(db); + QueryType& type = db->GetType(sym); if (!type.def) continue; if (type.def->kind == ClangSymbolKind::Namespace) continue; - AddCodeLens("ref", "refs", &common, sym.OffsetStartColumn(db, 0), + AddCodeLens("ref", "refs", &common, OffsetStartColumn(sym, 0), type.uses, true /*force_display*/); - AddCodeLens("derived", "derived", &common, - sym.OffsetStartColumn(db, 1), + AddCodeLens("derived", "derived", &common, OffsetStartColumn(sym, 1), ToReference(db, type.derived), false /*force_display*/); - AddCodeLens("var", "vars", &common, sym.OffsetStartColumn(db, 2), + AddCodeLens("var", "vars", &common, OffsetStartColumn(sym, 2), ToReference(db, type.instances), false /*force_display*/); break; } case SymbolKind::Func: { - QueryFunc& func = sym.Func(db); + QueryFunc& func = db->GetFunc(sym); if (!func.def) continue; @@ -187,27 +191,27 @@ struct TextDocumentCodeLensHandler if (base_callers.empty() && derived_callers.empty()) { SymbolRef loc = try_ensure_spelling(sym); AddCodeLens("call", "calls", &common, - loc.OffsetStartColumn(db, offset++), + OffsetStartColumn(loc, offset++), ToReference(db, func.callers), true /*force_display*/); } else { SymbolRef loc = try_ensure_spelling(sym); AddCodeLens("direct call", "direct calls", &common, - loc.OffsetStartColumn(db, offset++), + OffsetStartColumn(loc, offset++), ToReference(db, func.callers), false /*force_display*/); if (!base_callers.empty()) AddCodeLens("base call", "base calls", &common, - loc.OffsetStartColumn(db, offset++), + OffsetStartColumn(loc, offset++), ToReference(db, base_callers), false /*force_display*/); if (!derived_callers.empty()) AddCodeLens("derived call", "derived calls", &common, - loc.OffsetStartColumn(db, offset++), + OffsetStartColumn(loc, offset++), ToReference(db, derived_callers), false /*force_display*/); } AddCodeLens("derived", "derived", &common, - sym.OffsetStartColumn(db, offset++), + OffsetStartColumn(sym, offset++), ToReference(db, func.derived), false /*force_display*/); // "Base" @@ -234,7 +238,7 @@ struct TextDocumentCodeLensHandler } } } else { - AddCodeLens("base", "base", &common, sym.OffsetStartColumn(db, 1), + AddCodeLens("base", "base", &common, OffsetStartColumn(sym, 1), ToReference(db, func.def->base), false /*force_display*/); } @@ -242,7 +246,7 @@ struct TextDocumentCodeLensHandler break; } case SymbolKind::Var: { - QueryVar& var = sym.Var(db); + QueryVar& var = db->GetVar(sym); if (!var.def) continue; @@ -255,7 +259,7 @@ struct TextDocumentCodeLensHandler if (var.def->is_macro()) force_display = false; - AddCodeLens("ref", "refs", &common, sym.OffsetStartColumn(db, 0), + AddCodeLens("ref", "refs", &common, OffsetStartColumn(sym, 0), var.uses, force_display); break; } diff --git a/src/messages/text_document_definition.cc b/src/messages/text_document_definition.cc index aeea04e1..b066592c 100644 --- a/src/messages/text_document_definition.cc +++ b/src/messages/text_document_definition.cc @@ -31,7 +31,7 @@ std::vector GetGotoDefinitionTargets(QueryDatabase* db, case SymbolKind::Var: { std::vector ret = GetDeclarationsOfSymbolForGotoDefinition(db, sym); - QueryVar& var = sym.Var(db); + QueryVar& var = db->GetVar(sym); if (var.def && var.def->variable_type) { std::vector types = GetDeclarationsOfSymbolForGotoDefinition( db, SymbolRef(Range(), Id(var.def->variable_type->id), diff --git a/src/messages/text_document_highlight.cc b/src/messages/text_document_highlight.cc index 878284bd..147dff3b 100644 --- a/src/messages/text_document_highlight.cc +++ b/src/messages/text_document_highlight.cc @@ -41,7 +41,7 @@ struct TextDocumentDocumentHighlightHandler std::vector uses = GetUsesOfSymbol(db, sym, true); out.result.reserve(uses.size()); for (const Reference& use : uses) { - if (GetFileId(db, use) != file_id) + if (db->GetFileId(use) != file_id) continue; optional ls_location = diff --git a/src/messages/text_document_rename.cc b/src/messages/text_document_rename.cc index e008a73a..136c8523 100644 --- a/src/messages/text_document_rename.cc +++ b/src/messages/text_document_rename.cc @@ -16,7 +16,7 @@ lsWorkspaceEdit BuildWorkspaceEdit(QueryDatabase* db, if (!ls_location) continue; - QueryFileId file_id = GetFileId(db, ref); + QueryFileId file_id = db->GetFileId(ref); if (path_to_edit.find(file_id) == path_to_edit.end()) { path_to_edit[file_id] = lsTextDocumentEdit(); diff --git a/src/query.cc b/src/query.cc index 0ff0b8fa..37949939 100644 --- a/src/query.cc +++ b/src/query.cc @@ -393,21 +393,6 @@ Maybe GetQueryVarIdFromUsr(QueryDatabase* query_db, } // namespace -// FIXME Reference Remove -QueryFileId GetFileId(QueryDatabase* db, Reference ref) { - return db->GetFileId(ref); -} - -QueryFunc& SymbolRef::Func(QueryDatabase* db) const { - return db->funcs[Idx()]; -} -QueryType& SymbolRef::Type(QueryDatabase* db) const { - return db->types[Idx()]; -} -QueryVar& SymbolRef::Var(QueryDatabase* db) const { - return db->vars[Idx()]; -} - Maybe QueryDatabase::GetQueryFileIdFromPath( const std::string& path) { return ::GetQueryFileIdFromPath(this, path, false); diff --git a/src/query.h b/src/query.h index 46862f51..f55b7605 100644 --- a/src/query.h +++ b/src/query.h @@ -20,32 +20,6 @@ using QueryVarId = Id; struct IdMap; -struct QueryLocation { - Range range; - QueryFileId path; - SymbolRole role; - - bool HasValue() const { return range.HasValue(); } - QueryFileId FileId() const { return path; } - - operator Reference() const { - return Reference{range, Id(path), SymbolKind::File, role}; - } - - std::tuple ToTuple() const { - return std::make_tuple(range, path, role); - } - bool operator==(const QueryLocation& o) const { - return ToTuple() == o.ToTuple(); - } - bool operator!=(const QueryLocation& o) const { return !(*this == o); } - bool operator<(const QueryLocation& o) const { - return ToTuple() < o.ToTuple(); - } -}; -MAKE_REFLECT_STRUCT(QueryLocation, range, path, role); -MAKE_HASHABLE(QueryLocation, t.range, t.path, t.role); - struct SymbolIdx { RawId idx; SymbolKind kind; @@ -63,8 +37,6 @@ struct SymbolIdx { MAKE_REFLECT_STRUCT(SymbolIdx, kind, idx); MAKE_HASHABLE(SymbolIdx, t.kind, t.idx); -QueryFileId GetFileId(QueryDatabase* db, Reference ref); - struct SymbolRef : Reference { SymbolRef() = default; SymbolRef(Range range, Id id, SymbolKind kind, SymbolRole role) @@ -75,15 +47,6 @@ struct SymbolRef : Reference { RawId Idx() const { return RawId(id); } operator SymbolIdx() const { return SymbolIdx{Idx(), kind, }; } - QueryFunc& Func(QueryDatabase* db) const; - QueryType& Type(QueryDatabase* db) const; - QueryVar& Var(QueryDatabase* db) const; - - QueryLocation OffsetStartColumn(QueryDatabase* db, int16_t offset) const { - QueryLocation ret = {range, GetFileId(db, *this), role}; - ret.range.start.column += offset; - return ret; - } }; struct QueryFuncRef : Reference { diff --git a/src/query_utils.cc b/src/query_utils.cc index c9611c49..ae95627a 100644 --- a/src/query_utils.cc +++ b/src/query_utils.cc @@ -46,19 +46,19 @@ optional GetDefinitionSpellingOfSymbol(QueryDatabase* db, SymbolRef sym) { switch (sym.kind) { case SymbolKind::Type: { - QueryType& type = sym.Type(db); + QueryType& type = db->GetType(sym); if (type.def) return *type.def->definition_spelling; break; } case SymbolKind::Func: { - QueryFunc& func = sym.Func(db); + QueryFunc& func = db->GetFunc(sym); if (func.def) return func.def->definition_spelling; break; } case SymbolKind::Var: { - QueryVar& var = sym.Var(db); + QueryVar& var = db->GetVar(sym); if (var.def) return var.def->definition_spelling; break; @@ -76,19 +76,19 @@ optional GetDefinitionExtentOfSymbol(QueryDatabase* db, SymbolRef sym) { switch (sym.kind) { case SymbolKind::Type: { - QueryType& type = sym.Type(db); + QueryType& type = db->GetType(sym); if (type.def) return type.def->definition_extent; break; } case SymbolKind::Func: { - QueryFunc& func = sym.Func(db); + QueryFunc& func = db->GetFunc(sym); if (func.def) return func.def->definition_extent; break; } case SymbolKind::Var: { - QueryVar& var = sym.Var(db); + QueryVar& var = db->GetVar(sym); if (var.def) return var.def->definition_extent; break; @@ -107,13 +107,13 @@ optional GetDeclarationFileForSymbol(QueryDatabase* db, SymbolRef sym) { switch (sym.kind) { case SymbolKind::Type: { - QueryType& type = sym.Type(db); + QueryType& type = db->GetType(sym); if (type.def && type.def->definition_spelling) return db->GetFileId(*type.def->definition_spelling); break; } case SymbolKind::Func: { - QueryFunc& func = sym.Func(db); + QueryFunc& func = db->GetFunc(sym); if (!func.declarations.empty()) return db->GetFileId(func.declarations[0]); if (func.def && func.def->definition_spelling) @@ -121,7 +121,7 @@ optional GetDeclarationFileForSymbol(QueryDatabase* db, break; } case SymbolKind::Var: { - QueryVar& var = sym.Var(db); + QueryVar& var = db->GetVar(sym); if (var.def && var.def->definition_spelling) return db->GetFileId(*var.def->definition_spelling); break; @@ -366,7 +366,7 @@ optional GetLsLocation(QueryDatabase* db, WorkingFiles* working_files, Reference ref) { std::string path; - QueryFileId file_id = GetFileId(db, ref); + QueryFileId file_id = db->GetFileId(ref); if (!file_id.HasValue()) return nullopt; lsDocumentUri uri = GetLsDocumentUri(db, file_id, &path); @@ -413,7 +413,7 @@ optional GetSymbolInfo(QueryDatabase* db, return info; } case SymbolKind::Type: { - QueryType& type = sym.Type(db); + QueryType& type = db->GetType(sym); if (!type.def) break; @@ -436,7 +436,7 @@ optional GetSymbolInfo(QueryDatabase* db, return info; } case SymbolKind::Func: { - QueryFunc& func = sym.Func(db); + QueryFunc& func = db->GetFunc(sym); if (!func.def) break; @@ -457,7 +457,7 @@ optional GetSymbolInfo(QueryDatabase* db, return info; } case SymbolKind::Var: { - QueryVar& var = sym.Var(db); + QueryVar& var = db->GetVar(sym); if (!var.def) break;