Move IndexVar::def::declarations to IndexVar::declarations

This commit is contained in:
Fangrui Song 2018-01-26 21:50:17 -08:00
parent e892c23955
commit 0bbabbcbd2
6 changed files with 28 additions and 13 deletions

View File

@ -1431,7 +1431,7 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
var->def.definition_spelling = decl_spell; var->def.definition_spelling = decl_spell;
var->def.definition_extent = decl_cursor.get_extent(); var->def.definition_extent = decl_cursor.get_extent();
} else { } else {
var->def.declarations.push_back(decl_spell); var->declarations.push_back(decl_spell);
} }
AddDeclInitializerUsages(db, decl_cursor); AddDeclInitializerUsages(db, decl_cursor);

View File

@ -373,7 +373,6 @@ struct VarDefDefinitionData {
ClangStorageClass storage = ClangStorageClass::SC_Invalid; ClangStorageClass storage = ClangStorageClass::SC_Invalid;
optional<std::string> hover; optional<std::string> hover;
optional<std::string> comments; optional<std::string> comments;
std::vector<Range> declarations;
// TODO: definitions should be a list of ranges, since there can be more // TODO: definitions should be a list of ranges, since there can be more
// than one - when?? // than one - when??
optional<Range> definition_spelling; optional<Range> definition_spelling;
@ -397,7 +396,6 @@ struct VarDefDefinitionData {
const VarDefDefinitionData<TypeId, FuncId, VarId, Range>& other) const { const VarDefDefinitionData<TypeId, FuncId, VarId, Range>& other) const {
return short_name == other.short_name && return short_name == other.short_name &&
detailed_name == other.detailed_name && hover == other.hover && detailed_name == other.detailed_name && hover == other.hover &&
declarations.size() == other.declarations.size() &&
definition_spelling == other.definition_spelling && definition_spelling == other.definition_spelling &&
definition_extent == other.definition_extent && definition_extent == other.definition_extent &&
variable_type == other.variable_type && variable_type == other.variable_type &&
@ -439,6 +437,7 @@ struct IndexVar {
Def def; Def def;
std::vector<Range> declarations;
// Usages. // Usages.
std::vector<Range> uses; std::vector<Range> uses;

View File

@ -83,7 +83,6 @@ optional<QueryVar::Def> ToQuery(const IdMap& id_map, const IndexVar::Def& var) {
result.storage = var.storage; result.storage = var.storage;
result.hover = var.hover; result.hover = var.hover;
result.comments = var.comments; result.comments = var.comments;
result.declarations = id_map.ToQuery(var.declarations);
result.definition_spelling = id_map.ToQuery(var.definition_spelling); result.definition_spelling = id_map.ToQuery(var.definition_spelling);
result.definition_extent = id_map.ToQuery(var.definition_extent); result.definition_extent = id_map.ToQuery(var.definition_extent);
result.variable_type = id_map.ToQuery(var.variable_type); result.variable_type = id_map.ToQuery(var.variable_type);
@ -264,6 +263,10 @@ QueryFile::Def BuildFileDef(const IdMap& id_map, const IndexFile& indexed) {
var.def.definition_spelling.value()); var.def.definition_spelling.value());
if (var.def.definition_extent.has_value()) if (var.def.definition_extent.has_value())
add_outline(id_map.ToSymbol(var.id), var.def.definition_extent.value()); add_outline(id_map.ToSymbol(var.id), var.def.definition_extent.value());
for (const Range& decl : var.declarations) {
add_all_symbols(id_map.ToSymbol(var.id), decl);
add_outline(id_map.ToSymbol(var.id), decl);
}
for (const Range& use : var.uses) for (const Range& use : var.uses)
add_all_symbols(id_map.ToSymbol(var.id), use); add_all_symbols(id_map.ToSymbol(var.id), use);
} }
@ -658,6 +661,10 @@ IndexUpdate::IndexUpdate(const IdMap& previous_id_map,
if (var->def.definition_spelling) { if (var->def.definition_spelling) {
vars_removed.push_back(var->usr); vars_removed.push_back(var->usr);
} else { } else {
if (!var->declarations.empty())
vars_declarations.push_back(QueryVar::DeclarationsUpdate(
previous_id_map.ToQuery(var->id), {},
previous_id_map.ToQuery(var->declarations)));
if (!var->uses.empty()) if (!var->uses.empty())
vars_uses.push_back( vars_uses.push_back(
QueryVar::UsesUpdate(previous_id_map.ToQuery(var->id), {}, QueryVar::UsesUpdate(previous_id_map.ToQuery(var->id), {},
@ -669,6 +676,10 @@ IndexUpdate::IndexUpdate(const IdMap& previous_id_map,
optional<QueryVar::Def> def_update = ToQuery(current_id_map, var->def); optional<QueryVar::Def> def_update = ToQuery(current_id_map, var->def);
if (def_update) if (def_update)
vars_def_update.push_back(QueryVar::DefUpdate(var->usr, *def_update)); vars_def_update.push_back(QueryVar::DefUpdate(var->usr, *def_update));
if (!var->declarations.empty())
vars_declarations.push_back(QueryVar::DeclarationsUpdate(
current_id_map.ToQuery(var->id),
current_id_map.ToQuery(var->declarations)));
if (!var->uses.empty()) if (!var->uses.empty())
vars_uses.push_back( vars_uses.push_back(
QueryVar::UsesUpdate(current_id_map.ToQuery(var->id), QueryVar::UsesUpdate(current_id_map.ToQuery(var->id),
@ -687,6 +698,8 @@ IndexUpdate::IndexUpdate(const IdMap& previous_id_map,
vars_def_update.push_back( vars_def_update.push_back(
QueryVar::DefUpdate(current->usr, *current_remapped_def)); QueryVar::DefUpdate(current->usr, *current_remapped_def));
PROCESS_UPDATE_DIFF(QueryVarId, vars_declarations, declarations,
QueryLocation);
PROCESS_UPDATE_DIFF(QueryVarId, vars_uses, uses, QueryLocation); PROCESS_UPDATE_DIFF(QueryVarId, vars_uses, uses, QueryLocation);
}); });
@ -715,6 +728,7 @@ void IndexUpdate::Merge(const IndexUpdate& update) {
INDEX_UPDATE_APPEND(vars_removed); INDEX_UPDATE_APPEND(vars_removed);
INDEX_UPDATE_APPEND(vars_def_update); INDEX_UPDATE_APPEND(vars_def_update);
INDEX_UPDATE_MERGE(vars_declarations);
INDEX_UPDATE_MERGE(vars_uses); INDEX_UPDATE_MERGE(vars_uses);
#undef INDEX_UPDATE_APPEND #undef INDEX_UPDATE_APPEND
@ -805,6 +819,7 @@ void QueryDatabase::ApplyIndexUpdate(IndexUpdate* update) {
RemoveUsrs(SymbolKind::Var, update->vars_removed); RemoveUsrs(SymbolKind::Var, update->vars_removed);
ImportOrUpdate(update->vars_def_update); ImportOrUpdate(update->vars_def_update);
HANDLE_MERGEABLE(vars_declarations, declarations, vars);
HANDLE_MERGEABLE(vars_uses, uses, vars); HANDLE_MERGEABLE(vars_uses, uses, vars);
#undef HANDLE_MERGEABLE #undef HANDLE_MERGEABLE

View File

@ -256,10 +256,12 @@ struct QueryVar {
using Def = using Def =
VarDefDefinitionData<QueryTypeId, QueryFuncId, QueryVarId, QueryLocation>; VarDefDefinitionData<QueryTypeId, QueryFuncId, QueryVarId, QueryLocation>;
using DefUpdate = WithUsr<Def>; using DefUpdate = WithUsr<Def>;
using DeclarationsUpdate = MergeableUpdate<QueryVarId, QueryLocation>;
using UsesUpdate = MergeableUpdate<QueryVarId, QueryLocation>; using UsesUpdate = MergeableUpdate<QueryVarId, QueryLocation>;
Usr usr; Usr usr;
optional<Def> def; optional<Def> def;
std::vector<QueryLocation> declarations;
std::vector<QueryLocation> uses; std::vector<QueryLocation> uses;
size_t detailed_name_idx = (size_t)-1; size_t detailed_name_idx = (size_t)-1;
@ -302,6 +304,7 @@ struct IndexUpdate {
// Variable updates. // Variable updates.
std::vector<Usr> vars_removed; std::vector<Usr> vars_removed;
std::vector<QueryVar::DefUpdate> vars_def_update; std::vector<QueryVar::DefUpdate> vars_def_update;
std::vector<QueryVar::DeclarationsUpdate> vars_declarations;
std::vector<QueryVar::UsesUpdate> vars_uses; std::vector<QueryVar::UsesUpdate> vars_uses;
private: private:
@ -328,6 +331,7 @@ MAKE_REFLECT_STRUCT(IndexUpdate,
funcs_callers, funcs_callers,
vars_removed, vars_removed,
vars_def_update, vars_def_update,
vars_declarations,
vars_uses); vars_uses);
// The query database is heavily optimized for fast queries. It is stored // The query database is heavily optimized for fast queries. It is stored

View File

@ -203,11 +203,11 @@ std::vector<QueryLocation> GetUsesOfSymbol(QueryDatabase* db,
case SymbolKind::Var: { case SymbolKind::Var: {
QueryVar& var = db->vars[symbol.idx]; QueryVar& var = db->vars[symbol.idx];
std::vector<QueryLocation> ret = var.uses; std::vector<QueryLocation> ret = var.uses;
if (include_decl && var.def) { if (include_decl) {
if (var.def->definition_spelling) if (var.def && var.def->definition_spelling)
ret.push_back(*var.def->definition_spelling); ret.push_back(*var.def->definition_spelling);
ret.insert(ret.end(), var.def->declarations.begin(), ret.insert(ret.end(), var.declarations.begin(),
var.def->declarations.end()); var.declarations.end());
} }
return ret; return ret;
} }
@ -242,10 +242,7 @@ std::vector<QueryLocation> GetDeclarationsOfSymbolForGotoDefinition(
return func.declarations; return func.declarations;
} }
case SymbolKind::Var: { case SymbolKind::Var: {
QueryVar& var = db->vars[symbol.idx]; return db->vars[symbol.idx].declarations;
if (var.def)
return var.def->declarations;
break;
} }
default: default:
break; break;

View File

@ -189,7 +189,7 @@ void Reflect(TVisitor& visitor, IndexVar& value) {
REFLECT_MEMBER2("storage", value.def.storage); REFLECT_MEMBER2("storage", value.def.storage);
REFLECT_MEMBER2("hover", value.def.hover); REFLECT_MEMBER2("hover", value.def.hover);
REFLECT_MEMBER2("comments", value.def.comments); REFLECT_MEMBER2("comments", value.def.comments);
REFLECT_MEMBER2("declarations", value.def.declarations); REFLECT_MEMBER2("declarations", value.declarations);
REFLECT_MEMBER2("definition_spelling", value.def.definition_spelling); REFLECT_MEMBER2("definition_spelling", value.def.definition_spelling);
REFLECT_MEMBER2("definition_extent", value.def.definition_extent); REFLECT_MEMBER2("definition_extent", value.def.definition_extent);
REFLECT_MEMBER2("variable_type", value.def.variable_type); REFLECT_MEMBER2("variable_type", value.def.variable_type);