From 4e153784f6777360b2dcc81c4ad9ca8c2a198eef Mon Sep 17 00:00:00 2001 From: topisani Date: Thu, 30 Nov 2017 22:56:55 +0100 Subject: [PATCH] Index language as enum instead of string --- src/indexer.cc | 32 +++++++++++++++++--------------- src/indexer.h | 16 +++++++++++++--- src/query.cc | 15 ++++++++++++++- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/src/indexer.cc b/src/indexer.cc index 7e793ea1..5be4391e 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -996,21 +996,23 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { return; - // update the file language - // TODO: only do this when |is_first_ownership| in ConsumeFile is true - switch (clang_getCursorLanguage(decl->cursor)) { - case CXLanguage_C: - db->language = "c"; - break; - case CXLanguage_CPlusPlus: - db->language = "cpp"; - break; - case CXLanguage_ObjC: - db->language = "objectivec"; - break; - case CXLanguage_Invalid: - db->language = "invalid"; - break; + // The language of this declaration + LanguageId declLang = [decl] () { + switch (clang_getCursorLanguage(decl->cursor)) { + case CXLanguage_C: + return LanguageId::C; + case CXLanguage_CPlusPlus: + return LanguageId::Cpp; + case CXLanguage_ObjC: + return LanguageId::ObjC; + default: + return LanguageId::Unknown; + }; + } (); + + // Only update the file language if the new language is "greater" than the old + if (declLang > db->language) { + db->language = declLang; } NamespaceHelper* ns = ¶m->ns; diff --git a/src/indexer.h b/src/indexer.h index c28d09fb..1f960ec2 100644 --- a/src/indexer.h +++ b/src/indexer.h @@ -464,6 +464,18 @@ struct IndexInclude { std::string resolved_path; }; +// Used to identify the language at a file level. The ordering is important, as +// a file previously identified as `C`, will be changed to `Cpp` if it +// encounters a c++ declaration. +enum class LanguageId { + Unknown = 0, + C = 1, + Cpp = 2, + ObjC = 3 +}; + +MAKE_REFLECT_TYPE_PROXY(LanguageId, std::underlying_type::type); + struct IndexFile { IdCache id_cache; @@ -473,9 +485,7 @@ struct IndexFile { std::string path; std::vector args; int64_t last_modification_time = 0; - // markdown compatible language identifier. - // "cpp", "c", "objectivec", or invalid" - std::string language; + LanguageId language = LanguageId::Unknown; // The path to the translation unit cc file which caused the creation of this // IndexFile. When parsing a translation unit we generate many IndexFile diff --git a/src/query.cc b/src/query.cc index 76ac9155..b43eee68 100644 --- a/src/query.cc +++ b/src/query.cc @@ -171,10 +171,23 @@ void CompareGroups(std::vector& previous_data, QueryFile::Def BuildFileDef(const IdMap& id_map, const IndexFile& indexed) { QueryFile::Def def; def.path = indexed.path; - def.language = indexed.language; def.includes = indexed.includes; def.inactive_regions = indexed.skipped_by_preprocessor; + // Convert enum to markdown compatible strings + def.language = [indexed] () { + switch (indexed.language) { + case LanguageId::C: + return "c"; + case LanguageId::Cpp: + return "cpp"; + case LanguageId::ObjC: + return "objectivec"; + default: + return ""; + } + } (); + auto add_outline = [&def, &id_map](SymbolIdx idx, Range range) { def.outline.push_back(SymbolRef(idx, id_map.ToQuery(range))); };