From 236e7c13931e0770e96be50a58b0d6f58c02aa6c Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Mon, 9 Apr 2018 00:52:04 -0700 Subject: [PATCH] Simplify indexer and query --- src/clang_indexer.cc | 29 ++------------------ src/config.h | 9 +------ src/iindexer.cc | 9 +------ src/indexer.h | 5 +--- src/messages/ccls_file_info.cc | 10 +++++++ src/query.cc | 45 ++++++++++++++++++++++++++++++- src/query.h | 48 ---------------------------------- src/test.cc | 3 +-- 8 files changed, 60 insertions(+), 98 deletions(-) diff --git a/src/clang_indexer.cc b/src/clang_indexer.cc index 7440acb9..ee96345c 100644 --- a/src/clang_indexer.cc +++ b/src/clang_indexer.cc @@ -717,7 +717,7 @@ const int IndexFile::kMajorVersion = 15; const int IndexFile::kMinorVersion = 0; IndexFile::IndexFile(const std::string& path, const std::string& contents) - : id_cache(path), path(path), file_contents(contents) {} + : id_cache{path}, path(path), file_contents(contents) {} IndexTypeId IndexFile::ToTypeId(Usr usr) { auto it = id_cache.usr_to_type_id.find(usr); @@ -843,9 +843,6 @@ void AddUseSpell(IndexFile* db, std::vector& uses, ClangCursor cursor) { AddUse(db, uses, cursor.get_spell(), cursor.get_lexical_parent().cx_cursor); } -IdCache::IdCache(const std::string& primary_file) - : primary_file(primary_file) {} - void OnIndexDiagnostic(CXClientData client_data, CXDiagnosticSet diagnostics, void* reserved) { @@ -903,24 +900,6 @@ CXIdxClientFile OnIndexIncludedFile(CXClientData client_data, return nullptr; } -ClangCursor::VisitResult DumpVisitor(ClangCursor cursor, - ClangCursor parent, - int* level) { - fprintf(stderr, "%*s%s %s\n", *level * 2, "", - ToString(cursor.get_kind()).c_str(), cursor.get_spell_name().c_str()); - - *level += 1; - cursor.VisitChildren(&DumpVisitor, level); - *level -= 1; - - return ClangCursor::VisitResult::Continue; -} - -void Dump(ClangCursor cursor) { - int level = 0; - cursor.VisitChildren(&DumpVisitor, &level); -} - struct FindChildOfKindParam { CXCursorKind target_kind; std::optional result; @@ -2191,8 +2170,7 @@ std::vector> Parse( const std::vector& args, const std::vector& file_contents, PerformanceImportFile* perf, - ClangIndex* index, - bool dump_ast) { + ClangIndex* index) { if (!g_config->index.enabled) return {}; @@ -2218,9 +2196,6 @@ std::vector> Parse( perf->index_parse = timer.ElapsedMicrosecondsAndReset(); - if (dump_ast) - Dump(clang_getTranslationUnitCursor(tu->cx_tu)); - return ParseWithTu(file_consumer_shared, perf, tu.get(), index, file, args, unsaved_files); } diff --git a/src/config.h b/src/config.h index 196e564f..a0b7c55e 100644 --- a/src/config.h +++ b/src/config.h @@ -223,11 +223,6 @@ struct Config { // Maximum number of definition/reference/... results. int maxNum = 2000; } xref; - - //// For debugging - - // Dump AST after parsing if some pattern matches the source filename. - std::vector dumpAST; }; MAKE_REFLECT_STRUCT(Config::ClientCapability, snippetSupport); MAKE_REFLECT_STRUCT(Config::CodeLens, localVariables); @@ -277,8 +272,6 @@ MAKE_REFLECT_STRUCT(Config, highlight, index, workspaceSymbol, - xref, - - dumpAST); + xref); extern std::unique_ptr g_config; diff --git a/src/iindexer.cc b/src/iindexer.cc index 9b28942d..7578fa22 100644 --- a/src/iindexer.cc +++ b/src/iindexer.cc @@ -12,14 +12,7 @@ struct ClangIndexer : IIndexer { const std::vector& args, const std::vector& file_contents, PerformanceImportFile* perf) override { - bool dump_ast = false; - for (const std::string& pattern : g_config->dumpAST) - if (file.find(pattern) != std::string::npos) { - dump_ast = true; - break; - } - return Parse(file_consumer_shared, file, args, file_contents, perf, - &index, dump_ast); + return Parse(file_consumer_shared, file, args, file_contents, perf, &index); } // Note: constructing this acquires a global lock diff --git a/src/indexer.h b/src/indexer.h index 9d6a828d..6af15b82 100644 --- a/src/indexer.h +++ b/src/indexer.h @@ -402,8 +402,6 @@ struct IdCache { std::unordered_map type_id_to_usr; std::unordered_map func_id_to_usr; std::unordered_map var_id_to_usr; - - IdCache(const std::string& primary_file); }; struct IndexInclude { @@ -484,8 +482,7 @@ std::vector> Parse( const std::vector& args, const std::vector& file_contents, PerformanceImportFile* perf, - ClangIndex* index, - bool dump_ast = false); + ClangIndex* index); std::vector> ParseWithTu( FileConsumerSharedState* file_consumer_shared, PerformanceImportFile* perf, diff --git a/src/messages/ccls_file_info.cc b/src/messages/ccls_file_info.cc index 94475b69..4a8acf59 100644 --- a/src/messages/ccls_file_info.cc +++ b/src/messages/ccls_file_info.cc @@ -2,6 +2,16 @@ #include "query_utils.h" #include "queue_manager.h" +MAKE_REFLECT_STRUCT(QueryFile::Def, + file, + path, + args, + language, + outline, + all_symbols, + inactive_regions, + dependencies); + namespace { MethodType kMethodType = "$ccls/fileInfo"; diff --git a/src/query.cc b/src/query.cc index 8d28f2a0..e6bd14e9 100644 --- a/src/query.cc +++ b/src/query.cc @@ -22,6 +22,49 @@ MAKE_HASHABLE(Range, t.start, t.end); MAKE_HASHABLE(Use, t.range); +template +void Reflect(TVisitor& visitor, MergeableUpdate& value) { + REFLECT_MEMBER_START(); + REFLECT_MEMBER(id); + REFLECT_MEMBER(to_add); + REFLECT_MEMBER(to_remove); + REFLECT_MEMBER_END(); +} + +template +void Reflect(TVisitor& visitor, WithUsr& value) { + REFLECT_MEMBER_START(); + REFLECT_MEMBER(usr); + REFLECT_MEMBER(value); + REFLECT_MEMBER_END(); +} + +template +void Reflect(TVisitor& visitor, WithFileContent& value) { + REFLECT_MEMBER_START(); + REFLECT_MEMBER(value); + REFLECT_MEMBER(file_content); + REFLECT_MEMBER_END(); +} + +// NOTICE: We're not reflecting on files_removed or files_def_update, it is too +// much output when logging +MAKE_REFLECT_STRUCT(IndexUpdate, + types_removed, + types_def_update, + types_derived, + types_instances, + types_uses, + funcs_removed, + funcs_def_update, + funcs_declarations, + funcs_derived, + funcs_uses, + vars_removed, + vars_def_update, + vars_declarations, + vars_uses); + namespace { template @@ -414,7 +457,7 @@ Maybe QueryDatabase::GetQueryVarIdFromUsr(Usr usr) { } IdMap::IdMap(QueryDatabase* query_db, const IdCache& local_ids) - : local_ids(local_ids) { + : local_ids{local_ids} { // LOG_S(INFO) << "Creating IdMap for " << local_ids.primary_file; primary_file = *GetQueryFileIdFromPath(query_db, local_ids.primary_file, true); diff --git a/src/query.h b/src/query.h index 7a15c239..e5cdd719 100644 --- a/src/query.h +++ b/src/query.h @@ -46,14 +46,6 @@ struct MergeableUpdate { std::vector&& to_remove) : id(id), to_add(std::move(to_add)), to_remove(std::move(to_remove)) {} }; -template -void Reflect(TVisitor& visitor, MergeableUpdate& value) { - REFLECT_MEMBER_START(); - REFLECT_MEMBER(id); - REFLECT_MEMBER(to_add); - REFLECT_MEMBER(to_remove); - REFLECT_MEMBER_END(); -} template struct WithUsr { @@ -63,13 +55,6 @@ struct WithUsr { WithUsr(Usr usr, const T& value) : usr(usr), value(value) {} WithUsr(Usr usr, T&& value) : usr(usr), value(std::move(value)) {} }; -template -void Reflect(TVisitor& visitor, WithUsr& value) { - REFLECT_MEMBER_START(); - REFLECT_MEMBER(usr); - REFLECT_MEMBER(value); - REFLECT_MEMBER_END(); -} template struct WithFileContent { @@ -79,13 +64,6 @@ struct WithFileContent { WithFileContent(const T& value, const std::string& file_content) : value(value), file_content(file_content) {} }; -template -void Reflect(TVisitor& visitor, WithFileContent& value) { - REFLECT_MEMBER_START(); - REFLECT_MEMBER(value); - REFLECT_MEMBER(file_content); - REFLECT_MEMBER_END(); -} struct QueryFamily { using FileId = Id; @@ -124,15 +102,6 @@ struct QueryFile { def->path = path; } }; -MAKE_REFLECT_STRUCT(QueryFile::Def, - file, - path, - args, - language, - outline, - all_symbols, - inactive_regions, - dependencies); template struct QueryEntity { @@ -239,23 +208,6 @@ struct IndexUpdate { IndexFile& previous, IndexFile& current); }; -// NOTICE: We're not reflecting on files_removed or files_def_update, it is too -// much output when logging -MAKE_REFLECT_STRUCT(IndexUpdate, - types_removed, - types_def_update, - types_derived, - types_instances, - types_uses, - funcs_removed, - funcs_def_update, - funcs_declarations, - funcs_derived, - funcs_uses, - vars_removed, - vars_def_update, - vars_declarations, - vars_uses); // The query database is heavily optimized for fast queries. It is stored // in-memory. diff --git a/src/test.cc b/src/test.cc index 7155b0bd..2aea6646 100644 --- a/src/test.cc +++ b/src/test.cc @@ -294,8 +294,7 @@ bool RunIndexTests(const std::string& filter_path, bool enable_update) { g_config = std::make_unique(); FileConsumerSharedState file_consumer_shared; PerformanceImportFile perf; - auto dbs = Parse(&file_consumer_shared, path, flags, {}, &perf, &index, - false /*dump_ast*/); + auto dbs = Parse(&file_consumer_shared, path, flags, {}, &perf, &index); for (const auto& entry : all_expected_output) { const std::string& expected_path = entry.first;