Skip to content

Commit

Permalink
feat(proto): add type edges for service RPCs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay Sachs committed Apr 20, 2024
1 parent 1a5bd22 commit 9cd1af5
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
7 changes: 5 additions & 2 deletions kythe/cxx/indexer/proto/file_descriptor_walker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,7 @@ void FileDescriptorWalker::VisitRpcServices(const std::string* ns_name,
builder_->AddMethodToService(v_name, method, method_location);
}

VName input_sig;
{
// Add rpc method's input argument
ScopedLookup input_num(&lookup_path,
Expand All @@ -933,10 +934,11 @@ void FileDescriptorWalker::VisitRpcServices(const std::string* ns_name,
// Only decorate the type name, not the full <package>.<type> span.
TruncateLocationToTypeName(input_location, *input);

VName input_sig = builder_->VNameForDescriptor(input);
input_sig = builder_->VNameForDescriptor(input);
builder_->AddArgumentToMethod(method, input_sig, input_location);
}

VName output_sig;
{
// Add rpc method's output argument
ScopedLookup output_num(&lookup_path,
Expand All @@ -947,9 +949,10 @@ void FileDescriptorWalker::VisitRpcServices(const std::string* ns_name,
// Only decorate the type name, not the full <package>.<type> span.
TruncateLocationToTypeName(output_location, *output);

VName output_sig = builder_->VNameForDescriptor(output);
output_sig = builder_->VNameForDescriptor(output);
builder_->AddArgumentToMethod(method, output_sig, output_location);
}
builder_->AddMethodType(method, input_sig, output_sig);
}
}
}
Expand Down
38 changes: 38 additions & 0 deletions kythe/cxx/indexer/proto/proto_graph_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ std::string StringifyKind(NodeKindID kind) {
std::string StringifyKind(EdgeKindID kind) {
return std::string(spelling_of(kind));
}

const VName& BuiltinRpcType() {
static VName* builtinRpcTypeNode = []() {
VName* builtinRpcTypeNode = new VName();
builtinRpcTypeNode->set_language(kLanguageName);
builtinRpcTypeNode->set_signature("rpc#builtin");
return builtinRpcTypeNode;
}();
return *builtinRpcTypeNode;
}
} // anonymous namespace

void ProtoGraphBuilder::SetText(const VName& node_name,
Expand Down Expand Up @@ -105,6 +115,15 @@ void ProtoGraphBuilder::AddEdge(const VName& start, const VName& end,
recorder_->AddEdge(VNameRef(start), start_to_end_kind, VNameRef(end));
}

void ProtoGraphBuilder::AddEdge(const VName& start, const VName& end,
EdgeKindID start_to_end_kind, int ordinal) {
VLOG(1) << "Writing edge: " << StringifyNode(start) << " >-->--["
<< StringifyKind(start_to_end_kind) << "]-->--> "
<< StringifyNode(end);
recorder_->AddEdge(VNameRef(start), start_to_end_kind, VNameRef(end),
ordinal);
}

VName ProtoGraphBuilder::CreateAndAddAnchorNode(const Location& location) {
VName anchor = location.file;
anchor.set_language(kLanguageName);
Expand Down Expand Up @@ -204,6 +223,25 @@ void ProtoGraphBuilder::AddMethodToService(const VName& service,
AddEdge(method, service, EdgeKindID::kChildOf);
}

void ProtoGraphBuilder::AddMethodType(const VName& method, const VName& input,
const VName& output) {
if (!builtin_rpc_type_emitted) {
AddNode(BuiltinRpcType(), NodeKindID::kTBuiltin);
builtin_rpc_type_emitted = true;
}

VName method_type = method;
method_type.set_signature(absl::StrCat("type::", method.signature(),
"::", output.signature(),
"::", input.signature()));
AddNode(method_type, NodeKindID::kTApp);
AddEdge(method_type, BuiltinRpcType(), EdgeKindID::kParam, 0);
AddEdge(method_type, output, EdgeKindID::kParam, 1);
AddEdge(method_type, input, EdgeKindID::kParam, 2);

AddEdge(method, method_type, EdgeKindID::kHasType);
}

void ProtoGraphBuilder::AddEnumType(const VName* parent, const VName& enum_type,
const Location& location) {
VName anchor = CreateAndAddAnchorNode(location);
Expand Down
12 changes: 12 additions & 0 deletions kythe/cxx/indexer/proto/proto_graph_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ class ProtoGraphBuilder {
void AddEdge(const proto::VName& start, const proto::VName& end,
EdgeKindID start_to_end_kind);

// Records an edge of the given kind between the named nodes in the graph.
void AddEdge(const proto::VName& start, const proto::VName& end,
EdgeKindID start_to_end_kind, int ordinal);

// Creates and add to the graph a proto language-specific declaration node.
proto::VName CreateAndAddAnchorNode(const Location& location);

Expand Down Expand Up @@ -142,6 +146,10 @@ class ProtoGraphBuilder {
AddReference(type, location);
}

// Adds typed edges for the given RPC method.
void AddMethodType(const proto::VName& method, const proto::VName& input,
const proto::VName& output);

// Adds an anchor for location and a Ref edge to referent
void AddReference(const proto::VName& referent, const Location& location);

Expand Down Expand Up @@ -178,6 +186,10 @@ class ProtoGraphBuilder {

// The text of the current file being analyzed.
std::string current_file_contents_;

// Whether the builtin rpc type node has been emitted.
bool builtin_rpc_type_emitted = false;

};

} // namespace kythe
Expand Down
7 changes: 7 additions & 0 deletions kythe/cxx/indexer/proto/testdata/basic/services.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
syntax = "proto2";

package proto_kythe_test;

option java_package = "io.kythe";

//- @TestRequest defines/binding RequestNode
Expand Down Expand Up @@ -46,6 +47,12 @@ service TestService {
//- MNContext child.1 MNContext1
//- MNContext0.pre_text "proto_kythe_test"
//- MNContext1.pre_text "TestService"
//- MethodNode typed MethodType
//- MethodType.node/kind tapp
//- MethodType param.0 RpcTypeBuiltin
//- MethodType param.1 ReplyNode
//- MethodType param.2 RequestNode
//- RpcTypeBuiltin.node/kind tbuiltin
rpc TestMethod(TestRequest) returns (TestReply);

//- @TestRequest ref RequestNode
Expand Down

0 comments on commit 9cd1af5

Please sign in to comment.