Repository: kythe/kythe
Pull request #5855 (2023-09-18 18:19:42)

fix(proto_indexer): handle proto3 presence in signatures


Patch:
diff --git a/kythe/cxx/indexer/proto/marked_source.cc b/kythe/cxx/indexer/proto/marked_source.cc
index a5f5d7230..06c23cdd5 100644
--- a/kythe/cxx/indexer/proto/marked_source.cc
+++ b/kythe/cxx/indexer/proto/marked_source.cc
@@ -143,27 +143,24 @@ std::optional<MarkedSource> GenerateMarkedSourceForDescriptor(
         vname_for_desc) {
   std::string full_name;
   if (const google::protobuf::OneofDescriptor* oneof =
-          descriptor->containing_oneof()) {
+          descriptor->real_containing_oneof()) {
     full_name = oneof->full_name() + "." + descriptor->name();
   } else {
     full_name = descriptor->full_name();
   }
   MarkedSource ms;
   ms.set_post_child_text(" ");
-  if (!descriptor->containing_oneof() && !descriptor->is_map() &&
-      descriptor->label()) {
+  if (!descriptor->real_containing_oneof() && !descriptor->is_map() &&
+      (descriptor->has_optional_keyword() || descriptor->is_required() ||
+       descriptor->is_repeated())) {
     auto* mod = ms.add_child();
     mod->set_kind(MarkedSource::MODIFIER);
-    switch (descriptor->label()) {
-      case google::protobuf::FieldDescriptor::Label::LABEL_OPTIONAL:
-        mod->set_pre_text("optional");
-        break;
-      case google::protobuf::FieldDescriptor::Label::LABEL_REQUIRED:
-        mod->set_pre_text("required");
-        break;
-      case google::protobuf::FieldDescriptor::Label::LABEL_REPEATED:
-        mod->set_pre_text("repeated");
-        break;
+    if (descriptor->has_optional_keyword()) {
+      mod->set_pre_text("optional");
+    } else if (descriptor->is_required()) {
+      mod->set_pre_text("required");
+    } else if (descriptor->is_repeated()) {
+      mod->set_pre_text("repeated");
     }
   }
   if (const std::optional<MarkedSource> t =


Test patch:
diff --git a/kythe/cxx/indexer/proto/testdata/BUILD b/kythe/cxx/indexer/proto/testdata/BUILD
index 26c3ff859..07fc703de 100644
--- a/kythe/cxx/indexer/proto/testdata/BUILD
+++ b/kythe/cxx/indexer/proto/testdata/BUILD
@@ -13,6 +13,13 @@ proto_verifier_test(
     tags = ["basic"],
 )
 
+proto_verifier_test(
+    name = "signatures3",
+    srcs = [":signatures3_proto"],
+    resolve_code_facts = True,
+    tags = ["basic"],
+)
+
 proto_verifier_test(
     name = "file_node_existence",
     srcs = [":basic_file_node_proto"],
@@ -182,6 +189,11 @@ proto_library(
     srcs = ["basic/signatures.proto"],
 )
 
+proto_library(
+    name = "signatures3_proto",
+    srcs = ["basic/signatures3.proto"],
+)
+
 proto_library(
     name = "basic_file_node_proto",
     srcs = ["basic/file_node.proto"],
diff --git a/kythe/cxx/indexer/proto/testdata/basic/signatures3.proto b/kythe/cxx/indexer/proto/testdata/basic/signatures3.proto
new file mode 100644
index 000000000..7386c22f5
--- /dev/null
+++ b/kythe/cxx/indexer/proto/testdata/basic/signatures3.proto
@@ -0,0 +1,17 @@
+// Test proto3 for signatures.
+
+syntax = "proto3";
+
+package proto_kythe_test;
+
+message Message3 {
+  //- @O defines/binding O
+  //- O.code/rendered/qualified_name "proto_kythe_test.Message3.O"
+  //- O.code/rendered/signature "optional string O"
+  optional string O = 1;
+
+  //- @F defines/binding F
+  //- F.code/rendered/qualified_name "proto_kythe_test.Message3.F"
+  //- F.code/rendered/signature "int32 F"
+  int32 F = 2;
+}