diff --git a/command_line.cc b/command_line.cc index 1f4bc18b..5d967a63 100644 --- a/command_line.cc +++ b/command_line.cc @@ -71,7 +71,7 @@ bool HasOption(const std::unordered_map& options, cons return options.find(option) != options.end(); } -int main2(int argc, char** argv) { +int main(int argc, char** argv) { std::unordered_map options = ParseOptions(argc, argv); if (argc == 1 || options.find("--help") != options.end()) { @@ -158,11 +158,13 @@ int main2(int argc, char** argv) { std::vector entries = LoadCompilationEntriesFromDirectory(options["--project"]); - std::vector dbs; for (const CompilationEntry& entry : entries) { std::cout << "Parsing " << entry.filename << std::endl; - //IndexedFile db = Parse(2, entry.filename, entry.args); - //dbs.emplace_back(db); + QueryableDatabase db; + IndexedFile file = Parse(entry.filename, entry.args); + + IndexUpdate update(file); + db.ApplyIndexUpdate(&update); //std::cout << db.ToString() << std::endl << std::endl; } diff --git a/indexer.cpp b/indexer.cpp index 35e96902..7ab433da 100644 --- a/indexer.cpp +++ b/indexer.cpp @@ -2,51 +2,51 @@ #include "serializer.h" -IndexedFile::IndexedFile(const std::string& path, IdCache* id_cache) - : path(path), id_cache(id_cache) { +IndexedFile::IndexedFile(const std::string& path) + : path(path) { // TODO: Reconsider if we should still be reusing the same id_cache. // Preallocate any existing resolved ids. - for (const auto& entry : id_cache->usr_to_type_id) + for (const auto& entry : id_cache.usr_to_type_id) types.push_back(IndexedTypeDef(entry.second, entry.first)); - for (const auto& entry : id_cache->usr_to_func_id) + for (const auto& entry : id_cache.usr_to_func_id) funcs.push_back(IndexedFuncDef(entry.second, entry.first)); - for (const auto& entry : id_cache->usr_to_var_id) + for (const auto& entry : id_cache.usr_to_var_id) vars.push_back(IndexedVarDef(entry.second, entry.first)); } // TODO: Optimize for const char*? TypeId IndexedFile::ToTypeId(const std::string& usr) { - auto it = id_cache->usr_to_type_id.find(usr); - if (it != id_cache->usr_to_type_id.end()) + auto it = id_cache.usr_to_type_id.find(usr); + if (it != id_cache.usr_to_type_id.end()) return it->second; TypeId id(types.size()); types.push_back(IndexedTypeDef(id, usr)); - id_cache->usr_to_type_id[usr] = id; - id_cache->type_id_to_usr[id] = usr; + id_cache.usr_to_type_id[usr] = id; + id_cache.type_id_to_usr[id] = usr; return id; } FuncId IndexedFile::ToFuncId(const std::string& usr) { - auto it = id_cache->usr_to_func_id.find(usr); - if (it != id_cache->usr_to_func_id.end()) + auto it = id_cache.usr_to_func_id.find(usr); + if (it != id_cache.usr_to_func_id.end()) return it->second; FuncId id(funcs.size()); funcs.push_back(IndexedFuncDef(id, usr)); - id_cache->usr_to_func_id[usr] = id; - id_cache->func_id_to_usr[id] = usr; + id_cache.usr_to_func_id[usr] = id; + id_cache.func_id_to_usr[id] = usr; return id; } VarId IndexedFile::ToVarId(const std::string& usr) { - auto it = id_cache->usr_to_var_id.find(usr); - if (it != id_cache->usr_to_var_id.end()) + auto it = id_cache.usr_to_var_id.find(usr); + if (it != id_cache.usr_to_var_id.end()) return it->second; VarId id(vars.size()); vars.push_back(IndexedVarDef(id, usr)); - id_cache->usr_to_var_id[usr] = id; - id_cache->var_id_to_usr[id] = usr; + id_cache.usr_to_var_id[usr] = id; + id_cache.var_id_to_usr[id] = usr; return id; } @@ -364,7 +364,7 @@ void VisitDeclForTypeUsageVisitorHandler(clang::Cursor cursor, VisitDeclForTypeU if (param->is_interesting) { IndexedTypeDef* ref_type_def = db->Resolve(ref_type_id); - Location loc = db->id_cache->Resolve(cursor, true /*interesting*/); + Location loc = db->id_cache.Resolve(cursor, true /*interesting*/); ref_type_def->AddUsage(loc); } } @@ -487,7 +487,7 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { var_def->def.qualified_name = ns->QualifiedName(decl->semanticContainer, var_def->def.short_name); //} - Location decl_loc = db->id_cache->Resolve(decl->loc, false /*interesting*/); + Location decl_loc = db->id_cache.Resolve(decl->loc, false /*interesting*/); if (decl->isDefinition) var_def->def.definition = decl_loc; else @@ -533,7 +533,7 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { func_def->def.qualified_name = ns->QualifiedName(decl->semanticContainer, func_def->def.short_name); //} - Location decl_loc = db->id_cache->Resolve(decl->loc, false /*interesting*/); + Location decl_loc = db->id_cache.Resolve(decl->loc, false /*interesting*/); if (decl->isDefinition) func_def->def.definition = decl_loc; else @@ -650,7 +650,7 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { type_def->def.short_name = decl->entityInfo->name; type_def->def.qualified_name = ns->QualifiedName(decl->semanticContainer, type_def->def.short_name); - Location decl_loc = db->id_cache->Resolve(decl->loc, true /*interesting*/); + Location decl_loc = db->id_cache.Resolve(decl->loc, true /*interesting*/); type_def->def.definition = decl_loc.WithInteresting(false); type_def->AddUsage(decl_loc); break; @@ -684,7 +684,7 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { // } assert(decl->isDefinition); - Location decl_loc = db->id_cache->Resolve(decl->loc, true /*interesting*/); + Location decl_loc = db->id_cache.Resolve(decl->loc, true /*interesting*/); type_def->def.definition = decl_loc.WithInteresting(false); type_def->AddUsage(decl_loc); @@ -713,7 +713,7 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { } default: - std::cout << "!! Unhandled indexDeclaration: " << clang::Cursor(decl->cursor).ToString() << " at " << db->id_cache->Resolve(decl->loc, false /*interesting*/).ToString() << std::endl; + std::cout << "!! Unhandled indexDeclaration: " << clang::Cursor(decl->cursor).ToString() << " at " << db->id_cache.Resolve(decl->loc, false /*interesting*/).ToString() << std::endl; std::cout << " entityInfo->kind = " << decl->entityInfo->kind << std::endl; std::cout << " entityInfo->USR = " << decl->entityInfo->USR << std::endl; if (decl->declAsContainer) @@ -749,7 +749,7 @@ void indexEntityReference(CXClientData client_data, const CXIdxEntityRefInfo* re { VarId var_id = db->ToVarId(ref->referencedEntity->cursor); IndexedVarDef* var_def = db->Resolve(var_id); - var_def->uses.push_back(db->id_cache->Resolve(ref->loc, false /*interesting*/)); + var_def->uses.push_back(db->id_cache.Resolve(ref->loc, false /*interesting*/)); break; } @@ -771,7 +771,7 @@ void indexEntityReference(CXClientData client_data, const CXIdxEntityRefInfo* re // Don't report duplicate usages. // TODO: search full history? - Location loc = db->id_cache->Resolve(ref->loc, false /*interesting*/); + Location loc = db->id_cache.Resolve(ref->loc, false /*interesting*/); if (param->last_func_usage_location == loc) break; param->last_func_usage_location = loc; @@ -799,8 +799,8 @@ void indexEntityReference(CXClientData client_data, const CXIdxEntityRefInfo* re if (ref->referencedEntity->kind == CXIdxEntity_CXXConstructor || ref->referencedEntity->kind == CXIdxEntity_CXXDestructor) { - Location parent_loc = db->id_cache->Resolve(ref->parentEntity->cursor, true /*interesting*/); - Location our_loc = db->id_cache->Resolve(ref->loc, true /*is_interesting*/); + Location parent_loc = db->id_cache.Resolve(ref->parentEntity->cursor, true /*interesting*/); + Location our_loc = db->id_cache.Resolve(ref->loc, true /*is_interesting*/); if (!parent_loc.IsEqualTo(our_loc)) { IndexedFuncDef* called_def = db->Resolve(called_id); assert(called_def->def.declaring_type.has_value()); @@ -825,7 +825,7 @@ void indexEntityReference(CXClientData client_data, const CXIdxEntityRefInfo* re // defs here so we will output usages/etc. if (referenced_def->is_bad_def) { bool is_system_def = clang_Location_isInSystemHeader(clang_getCursorLocation(ref->referencedEntity->cursor)); - Location loc = db->id_cache->Resolve(ref->referencedEntity->cursor, false /*interesting*/); + Location loc = db->id_cache.Resolve(ref->referencedEntity->cursor, false /*interesting*/); if (!is_system_def && loc.raw_file_id != -1) referenced_def->is_bad_def = false; } @@ -844,16 +844,16 @@ void indexEntityReference(CXClientData client_data, const CXIdxEntityRefInfo* re // Foo f; // } // - referenced_def->AddUsage(db->id_cache->Resolve(ref->loc, false /*interesting*/)); + referenced_def->AddUsage(db->id_cache.Resolve(ref->loc, false /*interesting*/)); break; } default: - std::cout << "!! Unhandled indexEntityReference: " << cursor.ToString() << " at " << db->id_cache->Resolve(ref->loc, false /*interesting*/).ToString() << std::endl; + std::cout << "!! Unhandled indexEntityReference: " << cursor.ToString() << " at " << db->id_cache.Resolve(ref->loc, false /*interesting*/).ToString() << std::endl; std::cout << " ref->referencedEntity->kind = " << ref->referencedEntity->kind << std::endl; if (ref->parentEntity) std::cout << " ref->parentEntity->kind = " << ref->parentEntity->kind << std::endl; - std::cout << " ref->loc = " << db->id_cache->Resolve(ref->loc, false /*interesting*/).ToString() << std::endl; + std::cout << " ref->loc = " << db->id_cache.Resolve(ref->loc, false /*interesting*/).ToString() << std::endl; std::cout << " ref->kind = " << ref->kind << std::endl; if (ref->parentEntity) std::cout << " parentEntity = " << clang::Cursor(ref->parentEntity->cursor).ToString() << std::endl; @@ -866,7 +866,7 @@ void indexEntityReference(CXClientData client_data, const CXIdxEntityRefInfo* re } -IndexedFile Parse(IdCache* id_cache, std::string filename, std::vector args, bool dump_ast) { +IndexedFile Parse(std::string filename, std::vector args, bool dump_ast) { clang::Index index(0 /*excludeDeclarationsFromPCH*/, 0 /*displayDiagnostics*/); clang::TranslationUnit tu(index, filename, args); @@ -889,7 +889,7 @@ IndexedFile Parse(IdCache* id_cache, std::string filename, std::vector funcs; std::vector vars; - IndexedFile(const std::string& path, IdCache* id_cache); + IndexedFile(const std::string& path); TypeId ToTypeId(const std::string& usr); FuncId ToFuncId(const std::string& usr); @@ -498,4 +498,4 @@ struct IndexedFile { -IndexedFile Parse(IdCache* id_cache, std::string filename, std::vector args, bool dump_ast = false); \ No newline at end of file +IndexedFile Parse(std::string filename, std::vector args, bool dump_ast = false); \ No newline at end of file diff --git a/query.cc b/query.cc index 306e56ce..5736d659 100644 --- a/query.cc +++ b/query.cc @@ -37,42 +37,42 @@ std::vector Transform(const std::vector& input, std::function result.push_back(op(in)); return result; } -Usr MapIdToUsr(IdCache& id_cache, const TypeId& id) { - return id_cache.type_id_to_usr[id]; +Usr MapIdToUsr(const IdCache& id_cache, const TypeId& id) { + return id_cache.type_id_to_usr.find(id)->second; } -Usr MapIdToUsr(IdCache& id_cache, const FuncId& id) { - return id_cache.func_id_to_usr[id]; +Usr MapIdToUsr(const IdCache& id_cache, const FuncId& id) { + return id_cache.func_id_to_usr.find(id)->second; } -Usr MapIdToUsr(IdCache& id_cache, const VarId& id) { - return id_cache.var_id_to_usr[id]; +Usr MapIdToUsr(const IdCache& id_cache, const VarId& id) { + return id_cache.var_id_to_usr.find(id)->second; } -QueryableLocation MapIdToUsr(IdCache& id_cache, const Location& id) { - return QueryableLocation(id_cache.file_id_to_file_path[id.file_id()], id.line, id.column, id.interesting); +QueryableLocation MapIdToUsr(const IdCache& id_cache, const Location& id) { + return QueryableLocation(id_cache.file_id_to_file_path.find(id.file_id())->second, id.line, id.column, id.interesting); } -std::vector MapIdToUsr(IdCache& id_cache, const std::vector& ids) { - return Transform(ids, [&](TypeId id) { return id_cache.type_id_to_usr[id]; }); +std::vector MapIdToUsr(const IdCache& id_cache, const std::vector& ids) { + return Transform(ids, [&](TypeId id) { return id_cache.type_id_to_usr.find(id)->second; }); } -std::vector MapIdToUsr(IdCache& id_cache, const std::vector& ids) { - return Transform(ids, [&](FuncId id) { return id_cache.func_id_to_usr[id]; }); +std::vector MapIdToUsr(const IdCache& id_cache, const std::vector& ids) { + return Transform(ids, [&](FuncId id) { return id_cache.func_id_to_usr.find(id)->second; }); } -std::vector MapIdToUsr(IdCache& id_cache, const std::vector& ids) { - return Transform(ids, [&](VarId id) { return id_cache.var_id_to_usr[id]; }); +std::vector MapIdToUsr(const IdCache& id_cache, const std::vector& ids) { + return Transform(ids, [&](VarId id) { return id_cache.var_id_to_usr.find(id)->second; }); } -std::vector MapIdToUsr(IdCache& id_cache, const std::vector& ids) { +std::vector MapIdToUsr(const IdCache& id_cache, const std::vector& ids) { return Transform(ids, [&](FuncRef ref) { UsrRef result; result.loc = MapIdToUsr(id_cache, ref.loc); - result.usr = id_cache.func_id_to_usr[ref.id]; + result.usr = id_cache.func_id_to_usr.find(ref.id)->second; return result; }); } -std::vector MapIdToUsr(IdCache& id_cache, const std::vector& ids) { +std::vector MapIdToUsr(const IdCache& id_cache, const std::vector& ids) { return Transform(ids, [&](Location id) { - return QueryableLocation(id_cache.file_id_to_file_path[id.file_id()], id.line, id.column, id.interesting); + return QueryableLocation(id_cache.file_id_to_file_path.find(id.file_id())->second, id.line, id.column, id.interesting); }); } -QueryableTypeDef::DefUpdate MapIdToUsr(IdCache& id_cache, const TypeDefDefinitionData<>& def) { +QueryableTypeDef::DefUpdate MapIdToUsr(const IdCache& id_cache, const TypeDefDefinitionData<>& def) { QueryableTypeDef::DefUpdate result(def.usr); if (result.definition) result.definition = MapIdToUsr(id_cache, def.definition.value()); @@ -84,7 +84,7 @@ QueryableTypeDef::DefUpdate MapIdToUsr(IdCache& id_cache, const TypeDefDefinitio result.vars = MapIdToUsr(id_cache, def.vars); return result; } -QueryableFuncDef::DefUpdate MapIdToUsr(IdCache& id_cache, const FuncDefDefinitionData<>& def) { +QueryableFuncDef::DefUpdate MapIdToUsr(const IdCache& id_cache, const FuncDefDefinitionData<>& def) { QueryableFuncDef::DefUpdate result(def.usr); if (result.definition) result.definition = MapIdToUsr(id_cache, def.definition.value()); @@ -96,7 +96,7 @@ QueryableFuncDef::DefUpdate MapIdToUsr(IdCache& id_cache, const FuncDefDefinitio result.callees = MapIdToUsr(id_cache, def.callees); return result; } -QueryableVarDef::DefUpdate MapIdToUsr(IdCache& id_cache, const VarDefDefinitionData<>& def) { +QueryableVarDef::DefUpdate MapIdToUsr(const IdCache& id_cache, const VarDefDefinitionData<>& def) { QueryableVarDef::DefUpdate result(def.usr); if (result.declaration) result.declaration = MapIdToUsr(id_cache, def.declaration.value()); @@ -113,7 +113,7 @@ QueryableFile::QueryableFile(const IndexedFile& indexed) : file_id(indexed.path) { auto add_outline = [this, &indexed](Usr usr, Location location) { - outline.push_back(UsrRef(usr, MapIdToUsr(*indexed.id_cache, location))); + outline.push_back(UsrRef(usr, MapIdToUsr(indexed.id_cache, location))); }; for (const IndexedTypeDef& def : indexed.types) { @@ -336,11 +336,11 @@ void CompareGroups( IndexUpdate::IndexUpdate(IndexedFile& file) { files_added.push_back(QueryableFile(file)); for (const IndexedTypeDef& def : file.types) - types_added.push_back(QueryableTypeDef(*file.id_cache, def)); + types_added.push_back(QueryableTypeDef(file.id_cache, def)); for (const IndexedFuncDef& def : file.funcs) - funcs_added.push_back(QueryableFuncDef(*file.id_cache, def)); + funcs_added.push_back(QueryableFuncDef(file.id_cache, def)); for (const IndexedVarDef& def : file.vars) - vars_added.push_back(QueryableVarDef(*file.id_cache, def)); + vars_added.push_back(QueryableVarDef(file.id_cache, def)); } IndexUpdate::IndexUpdate(IndexedFile& previous_file, IndexedFile& current_file) { @@ -353,8 +353,8 @@ IndexUpdate::IndexUpdate(IndexedFile& previous_file, IndexedFile& current_file) /* Check for changes. */ \ std::vector removed, added; \ bool did_add = ComputeDifferenceForUpdate( \ - MapIdToUsr(*previous_file.id_cache, JOIN(previous_def->, index_name)), \ - MapIdToUsr(*current_file.id_cache, JOIN(current_def->, index_name)), \ + MapIdToUsr(previous_file.id_cache, JOIN(previous_def->, index_name)), \ + MapIdToUsr(current_file.id_cache, JOIN(current_def->, index_name)), \ &removed, &added); \ if (did_add) {\ std::cout << "Adding mergeable update on " << current_def->def.short_name << " (" << current_def->def.usr << ") for field " << #index_name << std::endl; \ @@ -387,11 +387,11 @@ IndexUpdate::IndexUpdate(IndexedFile& previous_file, IndexedFile& current_file) types_removed.push_back(def->def.usr); }, /*onAdded:*/[this, ¤t_file](IndexedTypeDef* def) { - types_added.push_back(QueryableTypeDef(*current_file.id_cache, *def)); + types_added.push_back(QueryableTypeDef(current_file.id_cache, *def)); }, /*onFound:*/[this, &previous_file, ¤t_file](IndexedTypeDef* previous_def, IndexedTypeDef* current_def) { - QueryableTypeDef::DefUpdate previous_remapped_def = MapIdToUsr(*previous_file.id_cache, previous_def->def); - QueryableTypeDef::DefUpdate current_remapped_def = MapIdToUsr(*current_file.id_cache, current_def->def); + QueryableTypeDef::DefUpdate previous_remapped_def = MapIdToUsr(previous_file.id_cache, previous_def->def); + QueryableTypeDef::DefUpdate current_remapped_def = MapIdToUsr(current_file.id_cache, current_def->def); if (previous_remapped_def != current_remapped_def) types_def_changed.push_back(current_remapped_def); @@ -405,11 +405,11 @@ IndexUpdate::IndexUpdate(IndexedFile& previous_file, IndexedFile& current_file) funcs_removed.push_back(def->def.usr); }, /*onAdded:*/[this, ¤t_file](IndexedFuncDef* def) { - funcs_added.push_back(QueryableFuncDef(*current_file.id_cache, *def)); + funcs_added.push_back(QueryableFuncDef(current_file.id_cache, *def)); }, /*onFound:*/[this, &previous_file, ¤t_file](IndexedFuncDef* previous_def, IndexedFuncDef* current_def) { - QueryableFuncDef::DefUpdate previous_remapped_def = MapIdToUsr(*previous_file.id_cache, previous_def->def); - QueryableFuncDef::DefUpdate current_remapped_def = MapIdToUsr(*current_file.id_cache, current_def->def); + QueryableFuncDef::DefUpdate previous_remapped_def = MapIdToUsr(previous_file.id_cache, previous_def->def); + QueryableFuncDef::DefUpdate current_remapped_def = MapIdToUsr(current_file.id_cache, current_def->def); if (previous_remapped_def != current_remapped_def) funcs_def_changed.push_back(current_remapped_def); @@ -425,11 +425,11 @@ IndexUpdate::IndexUpdate(IndexedFile& previous_file, IndexedFile& current_file) vars_removed.push_back(def->def.usr); }, /*onAdded:*/[this, ¤t_file](IndexedVarDef* def) { - vars_added.push_back(QueryableVarDef(*current_file.id_cache, *def)); + vars_added.push_back(QueryableVarDef(current_file.id_cache, *def)); }, /*onFound:*/[this, &previous_file, ¤t_file](IndexedVarDef* previous_def, IndexedVarDef* current_def) { - QueryableVarDef::DefUpdate previous_remapped_def = MapIdToUsr(*previous_file.id_cache, previous_def->def); - QueryableVarDef::DefUpdate current_remapped_def = MapIdToUsr(*current_file.id_cache, current_def->def); + QueryableVarDef::DefUpdate previous_remapped_def = MapIdToUsr(previous_file.id_cache, previous_def->def); + QueryableVarDef::DefUpdate current_remapped_def = MapIdToUsr(current_file.id_cache, current_def->def); if (previous_remapped_def != current_remapped_def) vars_def_changed.push_back(current_remapped_def); @@ -619,13 +619,11 @@ void QueryableDatabase::ApplyIndexUpdate(IndexUpdate* update) { int main233(int argc, char** argv) { - IdCache id_cache; - - IndexedFile indexed_file_a = Parse(&id_cache, "full_tests/index_delta/a_v0.cc", {}); + IndexedFile indexed_file_a = Parse("full_tests/index_delta/a_v0.cc", {}); std::cout << indexed_file_a.ToString() << std::endl; std::cout << std::endl; - IndexedFile indexed_file_b = Parse(&id_cache, "full_tests/index_delta/a_v1.cc", {}); + IndexedFile indexed_file_b = Parse("full_tests/index_delta/a_v1.cc", {}); std::cout << indexed_file_b.ToString() << std::endl; // TODO: We don't need to do ID remapping when computting a diff. Well, we need to do it for the IndexUpdate. IndexUpdate import(indexed_file_a); diff --git a/serializer.cc b/serializer.cc index 6aa9b628..a06e832f 100644 --- a/serializer.cc +++ b/serializer.cc @@ -87,8 +87,8 @@ void Serialize(Writer& writer, const char* key, uint64_t value) { } void Serialize(Writer& writer, IndexedFile* file) { - auto it = file->id_cache->usr_to_type_id.find(""); - if (it != file->id_cache->usr_to_type_id.end()) { + auto it = file->id_cache.usr_to_type_id.find(""); + if (it != file->id_cache.usr_to_type_id.end()) { file->Resolve(it->second)->def.short_name = ""; assert(file->Resolve(it->second)->uses.size() == 0); } diff --git a/test.cc b/test.cc index 42f96ecd..f111827e 100644 --- a/test.cc +++ b/test.cc @@ -90,7 +90,7 @@ void WriteToFile(const std::string& filename, const std::string& content) { file << content; } -int main(int argc, char** argv) { +int main2222222(int argc, char** argv) { // TODO: Assert that we need to be on clang >= 3.9.1 /* @@ -127,8 +127,7 @@ int main(int argc, char** argv) { // Run test. std::cout << "[START] " << path << std::endl; - IdCache id_cache; - IndexedFile db = Parse(&id_cache, path, {}, true /*dump_ast*/); + IndexedFile db = Parse(path, {}, true /*dump_ast*/); std::string actual_output = db.ToString(); //WriteToFile("output.json", actual_output);