diff --git a/src/cache_manager.cc b/src/cache_manager.cc index 11efee32..5799d016 100644 --- a/src/cache_manager.cc +++ b/src/cache_manager.cc @@ -43,7 +43,7 @@ struct RealCacheManager : ICacheManager { return ReadContent(GetCachePath(path)); } - std::unique_ptr RawCacheLoad(const std::string& path) { + std::unique_ptr RawCacheLoad(const std::string& path) override { if (!config_->enableCacheRead) return nullptr; @@ -102,7 +102,7 @@ struct FakeCacheManager : ICacheManager { return nullopt; } - std::unique_ptr RawCacheLoad(const std::string& path) { + std::unique_ptr RawCacheLoad(const std::string& path) override { for (const FakeCacheEntry& entry : entries_) { if (entry.path == path) { return Deserialize(SerializeFormat::Json, path, entry.json, nullopt); diff --git a/src/indexer.h b/src/indexer.h index 5e4eaaab..3856b555 100644 --- a/src/indexer.h +++ b/src/indexer.h @@ -64,8 +64,7 @@ bool operator!=(const Id& a, const Id& b) { template void Reflect(Reader& visitor, Id& id) { - if (visitor.IsUint64()) - id.id = visitor.GetUint64(); + id.id = visitor.GetUint64(); } template void Reflect(Writer& visitor, Id& value) { @@ -239,7 +238,7 @@ template void Reflect(TVisitor& visitor, TypeDefDefinitionData& value) { - REFLECT_MEMBER_START(); + REFLECT_MEMBER_START(12); REFLECT_MEMBER(short_name); REFLECT_MEMBER(detailed_name); REFLECT_MEMBER(kind); @@ -338,7 +337,7 @@ template & value) { - REFLECT_MEMBER_START(); + REFLECT_MEMBER_START(12); REFLECT_MEMBER(short_name); REFLECT_MEMBER(detailed_name); REFLECT_MEMBER(kind); @@ -452,7 +451,7 @@ template void Reflect(TVisitor& visitor, VarDefDefinitionData& value) { - REFLECT_MEMBER_START(); + REFLECT_MEMBER_START(9); REFLECT_MEMBER(short_name); REFLECT_MEMBER(detailed_name); REFLECT_MEMBER(kind); diff --git a/src/language_server_api.cc b/src/language_server_api.cc index bd487ac3..31940b7e 100644 --- a/src/language_server_api.cc +++ b/src/language_server_api.cc @@ -194,7 +194,7 @@ void lsResponseError::Write(Writer& visitor) { auto& value = *this; int code2 = static_cast(this->code); - visitor.StartObject(); + visitor.StartObject(2 + !!data); REFLECT_MEMBER2("code", code2); REFLECT_MEMBER(message); if (data) { diff --git a/src/language_server_api.h b/src/language_server_api.h index f3d0094a..142bc52f 100644 --- a/src/language_server_api.h +++ b/src/language_server_api.h @@ -226,7 +226,7 @@ struct lsCommand { }; template void Reflect(TVisitor& visitor, lsCommand& value) { - REFLECT_MEMBER_START(); + REFLECT_MEMBER_START(3); REFLECT_MEMBER(title); REFLECT_MEMBER(command); REFLECT_MEMBER(arguments); @@ -245,7 +245,7 @@ struct lsCodeLens { }; template void Reflect(TVisitor& visitor, lsCodeLens& value) { - REFLECT_MEMBER_START(); + REFLECT_MEMBER_START(3); REFLECT_MEMBER(range); REFLECT_MEMBER(command); REFLECT_MEMBER(data); @@ -979,7 +979,7 @@ struct Out_TextDocumentPublishDiagnostics template void Reflect(TVisitor& visitor, Out_TextDocumentPublishDiagnostics& value) { std::string method = "textDocument/publishDiagnostics"; - REFLECT_MEMBER_START(); + REFLECT_MEMBER_START(3); REFLECT_MEMBER(jsonrpc); REFLECT_MEMBER2("method", method); REFLECT_MEMBER(params); @@ -1044,7 +1044,7 @@ struct Out_ShowLogMessage : public lsOutMessage { }; template void Reflect(TVisitor& visitor, Out_ShowLogMessage& value) { - REFLECT_MEMBER_START(); + REFLECT_MEMBER_START(3); REFLECT_MEMBER(jsonrpc); std::string method = value.method(); REFLECT_MEMBER2("method", method); diff --git a/src/messages/text_document_code_lens.cc b/src/messages/text_document_code_lens.cc index f137b40f..5a67c330 100644 --- a/src/messages/text_document_code_lens.cc +++ b/src/messages/text_document_code_lens.cc @@ -18,7 +18,7 @@ struct lsCodeLensCommandArguments { std::vector locations; }; void Reflect(Writer& visitor, lsCodeLensCommandArguments& value) { - visitor.StartArray(); + visitor.StartArray(3); Reflect(visitor, value.uri); Reflect(visitor, value.position); Reflect(visitor, value.locations); diff --git a/src/messages/text_document_hover.cc b/src/messages/text_document_hover.cc index 61e6dc7e..36ccc3af 100644 --- a/src/messages/text_document_hover.cc +++ b/src/messages/text_document_hover.cc @@ -58,7 +58,7 @@ struct Out_TextDocumentHover : public lsOutMessage { }; MAKE_REFLECT_STRUCT(Out_TextDocumentHover::Result, contents, range); void Reflect(Writer& visitor, Out_TextDocumentHover& value) { - REFLECT_MEMBER_START(); + REFLECT_MEMBER_START(3); REFLECT_MEMBER(jsonrpc); REFLECT_MEMBER(id); if (value.result) diff --git a/src/query.h b/src/query.h index 046c5ac4..6091f990 100644 --- a/src/query.h +++ b/src/query.h @@ -161,7 +161,7 @@ struct MergeableUpdate { }; template void Reflect(TVisitor& visitor, MergeableUpdate& value) { - REFLECT_MEMBER_START(); + REFLECT_MEMBER_START(3); REFLECT_MEMBER(id); REFLECT_MEMBER(to_add); REFLECT_MEMBER(to_remove); @@ -177,7 +177,7 @@ struct WithUsr { }; template void Reflect(TVisitor& visitor, WithUsr& value) { - REFLECT_MEMBER_START(); + REFLECT_MEMBER_START(2); REFLECT_MEMBER(usr); REFLECT_MEMBER(value); REFLECT_MEMBER_END(); diff --git a/src/serializer.cc b/src/serializer.cc index 3b5c824b..d00cd7a9 100644 --- a/src/serializer.cc +++ b/src/serializer.cc @@ -2,7 +2,7 @@ // TODO Move Json* to serializers/json.cc #include "serializers/json.h" -//#include "serializers/msgpack.h" +#include "serializers/msgpack.h" #include "indexer.h" @@ -31,8 +31,7 @@ void Reflect(Writer& visitor, int16_t& value) { // int32_t void Reflect(Reader& visitor, int32_t& value) { - if (visitor.IsInt()) - value = visitor.GetInt(); + value = visitor.GetInt(); } void Reflect(Writer& visitor, int32_t& value) { visitor.Int(value); @@ -40,8 +39,7 @@ void Reflect(Writer& visitor, int32_t& value) { // int64_t void Reflect(Reader& visitor, int64_t& value) { - if (visitor.IsInt64()) - value = visitor.GetInt64(); + value = visitor.GetInt64(); } void Reflect(Writer& visitor, int64_t& value) { visitor.Int64(value); @@ -49,8 +47,7 @@ void Reflect(Writer& visitor, int64_t& value) { // uint64_t void Reflect(Reader& visitor, uint64_t& value) { - if (visitor.IsUint64()) - value = visitor.GetUint64(); + value = visitor.GetUint64(); } void Reflect(Writer& visitor, uint64_t& value) { visitor.Uint64(value); @@ -58,8 +55,7 @@ void Reflect(Writer& visitor, uint64_t& value) { // bool void Reflect(Reader& visitor, bool& value) { - if (visitor.IsBool()) - value = visitor.GetBool(); + value = visitor.GetBool(); } void Reflect(Writer& visitor, bool& value) { visitor.Bool(value); @@ -82,13 +78,13 @@ void ReflectMember(Writer& visitor, const char* name, std::string& value) { // TODO: Move this to indexer.cc void Reflect(Reader& visitor, IndexInclude& value) { - REFLECT_MEMBER_START(); + REFLECT_MEMBER_START(2); REFLECT_MEMBER(line); REFLECT_MEMBER(resolved_path); REFLECT_MEMBER_END(); } void Reflect(Writer& visitor, IndexInclude& value) { - REFLECT_MEMBER_START(); + REFLECT_MEMBER_START(2); REFLECT_MEMBER(line); if (gTestOutputMode) { std::string basename = GetBaseName(value.resolved_path); @@ -103,7 +99,7 @@ void Reflect(Writer& visitor, IndexInclude& value) { template void Reflect(TVisitor& visitor, IndexType& value) { - REFLECT_MEMBER_START(); + REFLECT_MEMBER_START(16); REFLECT_MEMBER2("id", value.id); REFLECT_MEMBER2("usr", value.usr); REFLECT_MEMBER2("short_name", value.def.short_name); @@ -125,7 +121,7 @@ void Reflect(TVisitor& visitor, IndexType& value) { template void Reflect(TVisitor& visitor, IndexFunc& value) { - REFLECT_MEMBER_START(); + REFLECT_MEMBER_START(16); REFLECT_MEMBER2("id", value.id); REFLECT_MEMBER2("is_operator", value.def.is_operator); REFLECT_MEMBER2("usr", value.usr); @@ -147,7 +143,7 @@ void Reflect(TVisitor& visitor, IndexFunc& value) { template void Reflect(TVisitor& visitor, IndexVar& value) { - REFLECT_MEMBER_START(); + REFLECT_MEMBER_START(13); REFLECT_MEMBER2("id", value.id); REFLECT_MEMBER2("usr", value.usr); REFLECT_MEMBER2("short_name", value.def.short_name); @@ -165,7 +161,7 @@ void Reflect(TVisitor& visitor, IndexVar& value) { } // IndexFile -bool ReflectMemberStart(Writer& visitor, IndexFile& value) { +bool ReflectMemberStart(Writer& visitor, IndexFile& value, size_t n) { auto it = value.id_cache.usr_to_type_id.find(""); if (it != value.id_cache.usr_to_type_id.end()) { value.Resolve(it->second)->def.short_name = ""; @@ -173,12 +169,12 @@ bool ReflectMemberStart(Writer& visitor, IndexFile& value) { } value.version = IndexFile::kCurrentVersion; - DefaultReflectMemberStart(visitor); + DefaultReflectMemberStart(visitor, n); return true; } template void Reflect(TVisitor& visitor, IndexFile& value) { - REFLECT_MEMBER_START(); + REFLECT_MEMBER_START(5 + (gTestOutputMode ? 0 : 6)); if (!gTestOutputMode) { REFLECT_MEMBER(version); REFLECT_MEMBER(last_modification_time); diff --git a/src/serializer.h b/src/serializer.h index b79055b5..a5945109 100644 --- a/src/serializer.h +++ b/src/serializer.h @@ -13,13 +13,14 @@ enum class SerializeFormat { Json, MessagePack }; class Reader { public: virtual ~Reader() {} + virtual SerializeFormat Format() const = 0; - virtual bool IsBool() = 0; + //virtual bool IsBool() = 0; virtual bool IsNull() = 0; virtual bool IsArray() = 0; virtual bool IsInt() = 0; - virtual bool IsInt64() = 0; - virtual bool IsUint64() = 0; + //virtual bool IsInt64() = 0; + //virtual bool IsUint64() = 0; virtual bool IsString() = 0; virtual bool GetBool() = 0; @@ -38,6 +39,7 @@ class Reader { class Writer { public: virtual ~Writer() {} + virtual SerializeFormat Format() const = 0; virtual void Null() = 0; virtual void Bool(bool x) = 0; @@ -46,17 +48,17 @@ class Writer { virtual void Uint64(uint64_t x) = 0; virtual void String(const char* x) = 0; virtual void String(const char* x, size_t len) = 0; - virtual void StartArray() = 0; + virtual void StartArray(size_t) = 0; virtual void EndArray() = 0; - virtual void StartObject() = 0; + virtual void StartObject(size_t) = 0; virtual void EndObject() = 0; virtual void Key(const char* name) = 0; }; struct IndexFile; -#define REFLECT_MEMBER_START() \ - if (!ReflectMemberStart(visitor, value)) \ +#define REFLECT_MEMBER_START(n) \ + if (!ReflectMemberStart(visitor, value, n)) \ return #define REFLECT_MEMBER_START1(value) \ if (!ReflectMemberStart(visitor, value)) \ @@ -74,19 +76,25 @@ struct IndexFile; value = static_cast(value0); \ } +// clang-format off +// Config has many fields, we need to support at least its number of fields. +#define NUM_VA_ARGS_IMPL(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,N,...) N +#define NUM_VA_ARGS(...) NUM_VA_ARGS_IMPL(__VA_ARGS__,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1) +// clang-format on + #define _MAPPABLE_REFLECT_MEMBER(name) REFLECT_MEMBER(name); #define MAKE_REFLECT_EMPTY_STRUCT(type, ...) \ template \ void Reflect(TVisitor& visitor, type& value) { \ - REFLECT_MEMBER_START(); \ + REFLECT_MEMBER_START(0); \ REFLECT_MEMBER_END(); \ } #define MAKE_REFLECT_STRUCT(type, ...) \ template \ void Reflect(TVisitor& visitor, type& value) { \ - REFLECT_MEMBER_START(); \ + REFLECT_MEMBER_START(NUM_VA_ARGS(__VA_ARGS__)); \ MACRO_MAP(_MAPPABLE_REFLECT_MEMBER, __VA_ARGS__) \ REFLECT_MEMBER_END(); \ } @@ -97,7 +105,7 @@ struct IndexFile; // This currently only supports writers. #define MAKE_REFLECT_STRUCT_WRITER_AS_ARRAY(type, ...) \ inline void Reflect(Writer& visitor, type& value) { \ - visitor.StartArray(); \ + visitor.StartArray(NUM_VA_ARGS(__VA_ARGS__)); \ MACRO_MAP(_MAPPABLE_REFLECT_ARRAY, __VA_ARGS__) \ visitor.EndArray(); \ } @@ -180,7 +188,7 @@ void Reflect(Reader& visitor, std::vector& values) { } template void Reflect(Writer& visitor, std::vector& values) { - visitor.StartArray(); + visitor.StartArray(values.size()); for (auto& value : values) Reflect(visitor, value); visitor.EndArray(); @@ -188,12 +196,12 @@ void Reflect(Writer& visitor, std::vector& values) { // Writer: -inline void DefaultReflectMemberStart(Writer& visitor) { - visitor.StartObject(); +inline void DefaultReflectMemberStart(Writer& visitor, size_t n) { + visitor.StartObject(n); } template -bool ReflectMemberStart(Writer& visitor, T& value) { - visitor.StartObject(); +bool ReflectMemberStart(Writer& visitor, T& value, size_t n) { + visitor.StartObject(n); return true; } template @@ -208,7 +216,7 @@ void ReflectMember(Writer& visitor, const char* name, T& value) { template void ReflectMember(Writer& visitor, const char* name, std::vector& values) { visitor.Key(name); - visitor.StartArray(); + visitor.StartArray(values.size()); for (auto& value : values) Reflect(visitor, value); visitor.EndArray(); @@ -225,9 +233,9 @@ void ReflectMember(Writer& visitor, const char* name, std::string& value); // Reader: -inline void DefaultReflectMemberStart(Reader& visitor) {} +inline void DefaultReflectMemberStart(Reader& visitor, size_t n) {} template -bool ReflectMemberStart(Reader& visitor, T& value) { +bool ReflectMemberStart(Reader& visitor, T& value, size_t n) { return true; } template diff --git a/src/serializers/json.h b/src/serializers/json.h index ed345234..ef12b502 100644 --- a/src/serializers/json.h +++ b/src/serializers/json.h @@ -10,13 +10,14 @@ class JsonReader : public Reader { public: JsonReader(rapidjson::GenericValue>* m) : m_(m) {} + SerializeFormat Format() const override { return SerializeFormat::Json; } - bool IsBool() override { return m_->IsBool(); } + //bool IsBool() override { return m_->IsBool(); } bool IsNull() override { return m_->IsNull(); } bool IsArray() override { return m_->IsArray(); } bool IsInt() override { return m_->IsInt(); } - bool IsInt64() override { return m_->IsInt64(); } - bool IsUint64() override { return m_->IsUint64(); } + //bool IsInt64() override { return m_->IsInt64(); } + //bool IsUint64() override { return m_->IsUint64(); } bool IsString() override { return m_->IsString(); } bool GetBool() override { return m_->GetBool(); } @@ -52,6 +53,7 @@ class JsonWriter : public Writer { public: JsonWriter(rapidjson::Writer* m) : m_(m) {} + SerializeFormat Format() const override { return SerializeFormat::Json; } void Null() override { m_->Null(); } void Bool(bool x) override { m_->Bool(x); } @@ -60,9 +62,9 @@ class JsonWriter : public Writer { void Uint64(uint64_t x) override { m_->Uint64(x); } void String(const char* x) override { m_->String(x); } void String(const char* x, size_t len) override { m_->String(x, len); } - void StartArray() override { m_->StartArray(); } + void StartArray(size_t) override { m_->StartArray(); } void EndArray() override { m_->EndArray(); } - void StartObject() override { m_->StartObject(); } + void StartObject(size_t) override { m_->StartObject(); } void EndObject() override { m_->EndObject(); } void Key(const char* name) override { m_->Key(name); } };