kbobyrev created this revision.
kbobyrev added a reviewer: sammccall.
Herald added subscribers: cfe-commits, usaxena95, arphaman.
Herald added a project: clang.
kbobyrev requested review of this revision.
Herald added subscribers: MaskRay, ilya-biryukov.

This will allow us detect missing values.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D89882

Files:
  clang-tools-extra/clangd/index/remote/Index.proto
  clang-tools-extra/clangd/index/remote/marshalling/Marshalling.cpp
  clang-tools-extra/clangd/unittests/remote/MarshallingTests.cpp

Index: clang-tools-extra/clangd/unittests/remote/MarshallingTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/remote/MarshallingTests.cpp
+++ clang-tools-extra/clangd/unittests/remote/MarshallingTests.cpp
@@ -377,7 +377,8 @@
   EXPECT_EQ(static_cast<unsigned>(Serialized.subjects_size()),
             Request.Subjects.size());
   EXPECT_EQ(Serialized.limit(), Request.Limit);
-  EXPECT_EQ(static_cast<RelationKind>(Serialized.predicate()),
+  // Serialized enums are offset by one.
+  EXPECT_EQ(static_cast<RelationKind>(Serialized.predicate() - 1),
             Request.Predicate);
   auto Deserialized = ProtobufMarshaller.fromProtobuf(&Serialized);
   ASSERT_TRUE(bool(Deserialized));
Index: clang-tools-extra/clangd/index/remote/marshalling/Marshalling.cpp
===================================================================
--- clang-tools-extra/clangd/index/remote/marshalling/Marshalling.cpp
+++ clang-tools-extra/clangd/index/remote/marshalling/Marshalling.cpp
@@ -112,7 +112,7 @@
   if (!IDs)
     return IDs.takeError();
   Req.IDs = std::move(*IDs);
-  Req.Filter = static_cast<RefKind>(Message->filter());
+  Req.Filter = static_cast<RefKind>(Message->filter() - 1);
   if (Message->limit())
     Req.Limit = Message->limit();
   return Req;
@@ -125,7 +125,7 @@
   if (!IDs)
     return IDs.takeError();
   Req.Subjects = std::move(*IDs);
-  Req.Predicate = static_cast<RelationKind>(Message->predicate());
+  Req.Predicate = static_cast<RelationKind>(Message->predicate() - 1);
   if (Message->limit())
     Req.Limit = Message->limit();
   return Req;
@@ -152,7 +152,7 @@
     return Declaration.takeError();
   Result.CanonicalDeclaration = *Declaration;
   Result.References = Message.references();
-  Result.Origin = static_cast<clangd::SymbolOrigin>(Message.origin());
+  Result.Origin = static_cast<clangd::SymbolOrigin>(Message.origin() - 1);
   Result.Signature = Message.signature();
   Result.TemplateSpecializationArgs = Message.template_specialization_args();
   Result.CompletionSnippetSuffix = Message.completion_snippet_suffix();
@@ -165,7 +165,7 @@
       return SerializedHeader.takeError();
     Result.IncludeHeaders.push_back(*SerializedHeader);
   }
-  Result.Flags = static_cast<clangd::Symbol::SymbolFlag>(Message.flags());
+  Result.Flags = static_cast<clangd::Symbol::SymbolFlag>(Message.flags() - 1);
   return Result;
 }
 
@@ -177,7 +177,7 @@
   if (!Location)
     return Location.takeError();
   Result.Location = *Location;
-  Result.Kind = static_cast<clangd::RefKind>(Message.kind());
+  Result.Kind = static_cast<clangd::RefKind>(Message.kind() - 1);
   return Result;
 }
 
@@ -226,7 +226,7 @@
   RefsRequest RPCRequest;
   for (const auto &ID : From.IDs)
     RPCRequest.add_ids(ID.str());
-  RPCRequest.set_filter(static_cast<uint32_t>(From.Filter));
+  RPCRequest.set_filter(static_cast<uint32_t>(From.Filter) + 1);
   if (From.Limit)
     RPCRequest.set_limit(*From.Limit);
   return RPCRequest;
@@ -236,7 +236,7 @@
   RelationsRequest RPCRequest;
   for (const auto &ID : From.Subjects)
     RPCRequest.add_subjects(ID.str());
-  RPCRequest.set_predicate(static_cast<uint32_t>(From.Predicate));
+  RPCRequest.set_predicate(static_cast<uint32_t>(From.Predicate) + 1);
   if (From.Limit)
     RPCRequest.set_limit(*From.Limit);
   return RPCRequest;
@@ -259,7 +259,7 @@
     return Declaration.takeError();
   *Result.mutable_canonical_declaration() = *Declaration;
   Result.set_references(From.References);
-  Result.set_origin(static_cast<uint32_t>(From.Origin));
+  Result.set_origin(static_cast<uint32_t>(From.Origin) + 1);
   Result.set_signature(From.Signature.str());
   Result.set_template_specialization_args(
       From.TemplateSpecializationArgs.str());
@@ -274,13 +274,13 @@
     auto *NextHeader = Result.add_headers();
     *NextHeader = *Serialized;
   }
-  Result.set_flags(static_cast<uint32_t>(From.Flags));
+  Result.set_flags(static_cast<uint32_t>(From.Flags) + 1);
   return Result;
 }
 
 llvm::Expected<Ref> Marshaller::toProtobuf(const clangd::Ref &From) {
   Ref Result;
-  Result.set_kind(static_cast<uint32_t>(From.Kind));
+  Result.set_kind(static_cast<uint32_t>(From.Kind) + 1);
   auto Location = toProtobuf(From.Location);
   if (!Location)
     return Location.takeError();
@@ -352,20 +352,24 @@
 
 clang::index::SymbolInfo Marshaller::fromProtobuf(const SymbolInfo &Message) {
   clang::index::SymbolInfo Result;
-  Result.Kind = static_cast<clang::index::SymbolKind>(Message.kind());
-  Result.SubKind = static_cast<clang::index::SymbolSubKind>(Message.subkind());
-  Result.Lang = static_cast<clang::index::SymbolLanguage>(Message.language());
+  // Enum values are offset by one to detect missing values.
+  Result.Kind = static_cast<clang::index::SymbolKind>(Message.kind() - 1);
+  Result.SubKind =
+      static_cast<clang::index::SymbolSubKind>(Message.subkind() - 1);
+  Result.Lang =
+      static_cast<clang::index::SymbolLanguage>(Message.language() - 1);
   Result.Properties =
-      static_cast<clang::index::SymbolPropertySet>(Message.properties());
+      static_cast<clang::index::SymbolPropertySet>(Message.properties() - 1);
   return Result;
 }
 
 SymbolInfo Marshaller::toProtobuf(const clang::index::SymbolInfo &Info) {
   SymbolInfo Result;
-  Result.set_kind(static_cast<uint32_t>(Info.Kind));
-  Result.set_subkind(static_cast<uint32_t>(Info.SubKind));
-  Result.set_language(static_cast<uint32_t>(Info.Lang));
-  Result.set_properties(static_cast<uint32_t>(Info.Properties));
+  // Enum values are offset by one to detect missing values.
+  Result.set_kind(static_cast<uint32_t>(Info.Kind) + 1);
+  Result.set_subkind(static_cast<uint32_t>(Info.SubKind) + 1);
+  Result.set_language(static_cast<uint32_t>(Info.Lang) + 1);
+  Result.set_properties(static_cast<uint32_t>(Info.Properties) + 1);
   return Result;
 }
 
Index: clang-tools-extra/clangd/index/remote/Index.proto
===================================================================
--- clang-tools-extra/clangd/index/remote/Index.proto
+++ clang-tools-extra/clangd/index/remote/Index.proto
@@ -12,6 +12,7 @@
 
 // Semantics of SymbolIndex match clangd::SymbolIndex with all required
 // structures corresponding to their clangd::* counterparts.
+// NOTE: Enum values are offset by one to detect missing values.
 service SymbolIndex {
   rpc Lookup(LookupRequest) returns (stream LookupReply) {}
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to