Skip to content

Commit

Permalink
feat(proto_indexer): link proto field types in MarkedSource (#5857)
Browse files Browse the repository at this point in the history
  • Loading branch information
schroederc committed Sep 20, 2023
1 parent 4402c00 commit 1c0347d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 35 deletions.
1 change: 0 additions & 1 deletion kythe/cxx/indexer/proto/BUILD
Expand Up @@ -62,7 +62,6 @@ cc_library(
"//kythe/proto:xref_cc_proto",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:node_hash_set",
"@com_google_absl//absl/functional:bind_front",
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/status:statusor",
Expand Down
9 changes: 2 additions & 7 deletions kythe/cxx/indexer/proto/file_descriptor_walker.cc
Expand Up @@ -18,7 +18,6 @@

#include <optional>

#include "absl/functional/bind_front.h"
#include "absl/log/check.h"
#include "absl/log/log.h"
#include "absl/status/statusor.h"
Expand Down Expand Up @@ -439,12 +438,8 @@ void FileDescriptorWalker::VisitField(const std::string* parent_name,
v_name, location);
}

AttachMarkedSource(
v_name,
GenerateMarkedSourceForDescriptor(
field, absl::bind_front(&ProtoGraphBuilder::VNameForDescriptor<
google::protobuf::FieldDescriptor>,
builder_)));
AttachMarkedSource(v_name,
GenerateMarkedSourceForDescriptor(field, builder_));

// Check for [deprecated=true] annotations and emit deprecation tags.
if (field->options().deprecated()) {
Expand Down
36 changes: 23 additions & 13 deletions kythe/cxx/indexer/proto/marked_source.cc
Expand Up @@ -16,13 +16,13 @@

#include "kythe/cxx/indexer/proto/marked_source.h"

#include <functional>
#include <optional>

#include "absl/strings/str_split.h"
#include "google/protobuf/descriptor.h"
#include "kythe/cxx/common/indexing/KytheOutputStream.h"
#include "kythe/cxx/common/kythe_uri.h"
#include "kythe/cxx/indexer/proto/proto_graph_builder.h"
#include "kythe/proto/common.pb.h"

namespace kythe {
Expand Down Expand Up @@ -98,7 +98,8 @@ std::optional<MarkedSource> GenerateMarkedSourceForDescriptor(
}

std::optional<MarkedSource> GenerateMarkedSourceForType(
const google::protobuf::FieldDescriptor* descriptor) {
const google::protobuf::FieldDescriptor* descriptor,
ProtoGraphBuilder* builder) {
MarkedSource type;
type.set_kind(MarkedSource::TYPE);
switch (descriptor->type()) {
Expand All @@ -108,25 +109,26 @@ std::optional<MarkedSource> GenerateMarkedSourceForType(
type.set_post_child_text(", ");
type.set_post_text(">");

auto key =
GenerateMarkedSourceForType(descriptor->message_type()->map_key());
auto key = GenerateMarkedSourceForType(
descriptor->message_type()->map_key(), builder);
auto val = GenerateMarkedSourceForType(
descriptor->message_type()->map_value());
descriptor->message_type()->map_value(), builder);
if (!key || !val) {
return std::nullopt;
}

*type.add_child() = *key;
*type.add_child() = *val;
} else if (!GenerateMarkedSourceForDottedName(
descriptor->message_type()->full_name(),
type.add_child())) {
descriptor->message_type()->full_name(), type.add_child(),
builder->VNameForDescriptor(descriptor->message_type()))) {
return std::nullopt;
}
break;
case google::protobuf::FieldDescriptor::TYPE_ENUM:
if (!GenerateMarkedSourceForDottedName(
descriptor->enum_type()->full_name(), type.add_child())) {
descriptor->enum_type()->full_name(), type.add_child(),
builder->VNameForDescriptor(descriptor->enum_type()))) {
return std::nullopt;
}
break;
Expand All @@ -139,8 +141,7 @@ std::optional<MarkedSource> GenerateMarkedSourceForType(

std::optional<MarkedSource> GenerateMarkedSourceForDescriptor(
const google::protobuf::FieldDescriptor* descriptor,
const std::function<proto::VName(const google::protobuf::FieldDescriptor*)>&
vname_for_desc) {
ProtoGraphBuilder* builder) {
std::string full_name;
if (const google::protobuf::OneofDescriptor* oneof =
descriptor->real_containing_oneof()) {
Expand All @@ -167,11 +168,11 @@ std::optional<MarkedSource> GenerateMarkedSourceForDescriptor(
}
}
if (const std::optional<MarkedSource> t =
GenerateMarkedSourceForType(descriptor)) {
GenerateMarkedSourceForType(descriptor, builder)) {
*ms.add_child() = *t;
}
if (GenerateMarkedSourceForDottedName(full_name, ms.add_child(),
vname_for_desc(descriptor))) {
if (GenerateMarkedSourceForDottedName(
full_name, ms.add_child(), builder->VNameForDescriptor(descriptor))) {
return ms;
}
return std::nullopt;
Expand All @@ -187,4 +188,13 @@ std::optional<MarkedSource> GenerateMarkedSourceForDescriptor(
return GenerateMarkedSourceForDescriptor("rpc", descriptor);
}

std::optional<MarkedSource> GenerateMarkedSourceForDescriptor(
const google::protobuf::OneofDescriptor* descriptor) {
MarkedSource ms;
if (GenerateMarkedSourceForDottedName(descriptor->full_name(), &ms)) {
return ms;
}
return std::nullopt;
}

} // namespace kythe
19 changes: 5 additions & 14 deletions kythe/cxx/indexer/proto/marked_source.h
Expand Up @@ -21,6 +21,7 @@

#include "google/protobuf/descriptor.h"
#include "kythe/cxx/common/indexing/KytheOutputStream.h"
#include "kythe/cxx/indexer/proto/proto_graph_builder.h"

namespace kythe {

Expand All @@ -33,18 +34,6 @@ bool GenerateMarkedSourceForDottedName(
absl::string_view name, MarkedSource* root,
std::optional<proto::VName> vname = std::nullopt);

// Given a proto descriptor, generates an appropriate code fact. Returns
// `None` if a code fact couldn't be generated.
template <typename T>
std::optional<MarkedSource> GenerateMarkedSourceForDescriptor(
const T* descriptor) {
MarkedSource ms;
if (GenerateMarkedSourceForDottedName(descriptor->full_name(), &ms)) {
return ms;
}
return std::nullopt;
}

std::optional<MarkedSource> GenerateMarkedSourceForDescriptor(
const google::protobuf::Descriptor* descriptor);

Expand All @@ -56,15 +45,17 @@ std::optional<MarkedSource> GenerateMarkedSourceForDescriptor(

std::optional<MarkedSource> GenerateMarkedSourceForDescriptor(
const google::protobuf::FieldDescriptor* descriptor,
const std::function<proto::VName(const google::protobuf::FieldDescriptor*)>&
vname_for_desc);
ProtoGraphBuilder* builder);

std::optional<MarkedSource> GenerateMarkedSourceForDescriptor(
const google::protobuf::ServiceDescriptor* descriptor);

std::optional<MarkedSource> GenerateMarkedSourceForDescriptor(
const google::protobuf::MethodDescriptor* descriptor);

std::optional<MarkedSource> GenerateMarkedSourceForDescriptor(
const google::protobuf::OneofDescriptor* descriptor);

} // namespace kythe

#endif // KYTHE_CXX_INDEXER_PROTO_MARKED_SOURCE_H_
6 changes: 6 additions & 0 deletions kythe/cxx/indexer/proto/testdata/basic/message-fields.proto
Expand Up @@ -19,6 +19,12 @@ message MessageFields {
//- FNRoot child.0 FNLabel
//- FNLabel.kind "MODIFIER"
//- FNLabel.pre_text "optional"
//- FNRoot child.1 FNType
//- FNType.kind "TYPE"
//- FNType child.0 FNTypeName
//- FNTypeName child.1 FNTypeIdent
//- FNTypeIdent.pre_text "Type1"
//- FNTypeIdent link Type1Node
//- FNRoot child.2 FNName
//- FNName child.0 FNQualName
//- FNName child.1 FNIdent
Expand Down

0 comments on commit 1c0347d

Please sign in to comment.