Skip to content

Commit

Permalink
hbp: remove genn'd forwarding header
Browse files Browse the repository at this point in the history
Instead of solely emitting a forwarding header that just houses incomplete types, we opt to forward declare as-needed inside our genn'ed hpb header.

PiperOrigin-RevId: 726050002
  • Loading branch information
honglooker authored and copybara-github committed Feb 12, 2025
1 parent 03b2502 commit 354a5a9
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 40 deletions.
5 changes: 0 additions & 5 deletions hpb/bazel/hpb_proto_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ def _compile_upb_cc_protos(ctx, proto_info, proto_sources):
extension = ".upb.proto.h",
proto_info = proto_info,
)
hdrs += proto_common.declare_generated_files(
ctx.actions,
extension = ".upb.fwd.h",
proto_info = proto_info,
)

proto_common.compile(
actions = ctx.actions,
Expand Down
4 changes: 0 additions & 4 deletions hpb_generator/names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,6 @@ std::string CppSourceFilename(const google::protobuf::FileDescriptor* file) {
return compiler::StripProto(file->name()) + ".upb.proto.cc";
}

std::string ForwardingHeaderFilename(const google::protobuf::FileDescriptor* file) {
return compiler::StripProto(file->name()) + ".upb.fwd.h";
}

std::string UpbCFilename(const google::protobuf::FileDescriptor* file) {
return compiler::StripProto(file->name()) + ".upb.h";
}
Expand Down
1 change: 0 additions & 1 deletion hpb_generator/names.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ std::string QualifiedClassName(const protobuf::Descriptor* descriptor);
std::string QualifiedInternalClassName(const protobuf::Descriptor* descriptor);

std::string CppSourceFilename(const google::protobuf::FileDescriptor* file);
std::string ForwardingHeaderFilename(const google::protobuf::FileDescriptor* file);
std::string UpbCFilename(const google::protobuf::FileDescriptor* file);
std::string CppHeaderFilename(const google::protobuf::FileDescriptor* file);

Expand Down
39 changes: 9 additions & 30 deletions hpb_generator/protoc-gen-hpb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,6 @@ bool Generator::Generate(const protobuf::FileDescriptor* file,
}
}

// Write model.upb.fwd.h
std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> output_stream(
context->Open(ForwardingHeaderFilename(file)));
Context fwd_ctx =
Context(file, output_stream.get(), Options{.backend = Backend::UPB});
WriteForwardingHeader(file, fwd_ctx);

// Write model.upb.proto.h
std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> header_output_stream(
context->Open(CppHeaderFilename(file)));
Expand All @@ -101,29 +94,15 @@ bool Generator::Generate(const protobuf::FileDescriptor* file,
return true;
}

// The forwarding header defines Access/Proxy/CProxy for message classes
// used to include when referencing dependencies to prevent transitive
// dependency headers from being included.
void WriteForwardingHeader(const protobuf::FileDescriptor* file, Context& ctx) {
EmitFileWarning(file, ctx);
ctx.EmitLegacy(
R"cc(
#ifndef $0_UPB_FWD_H_
#define $0_UPB_FWD_H_
)cc",
ToPreproc(file->name()));
ctx.Emit("\n");
void WriteForwardDecls(const protobuf::FileDescriptor* file, Context& ctx) {
for (int i = 0; i < file->public_dependency_count(); ++i) {
ctx.EmitLegacy("#include \"$0\"\n",
ForwardingHeaderFilename(file->public_dependency(i)));
}
if (file->public_dependency_count() > 0) {
ctx.Emit("\n");
const auto target_file_messages =
SortedMessages(file->public_dependency(i));
WriteTypedefForwardingHeader(file->public_dependency(i),
target_file_messages, ctx);
}
const std::vector<const protobuf::Descriptor*> this_file_messages =
SortedMessages(file);
const auto this_file_messages = SortedMessages(file);
WriteTypedefForwardingHeader(file, this_file_messages, ctx);
ctx.EmitLegacy("#endif /* $0_UPB_FWD_H_ */\n", ToPreproc(file->name()));
}

void WriteHeader(const protobuf::FileDescriptor* file, Context& ctx,
Expand Down Expand Up @@ -165,6 +144,7 @@ void WriteHeader(const protobuf::FileDescriptor* file, Context& ctx,
}

WriteHeaderMessageForwardDecls(file, ctx, strip_feature_includes);
WriteForwardDecls(file, ctx);

std::vector<const protobuf::EnumDescriptor*> this_file_enums =
SortedEnums(file);
Expand Down Expand Up @@ -261,16 +241,15 @@ void WriteHeaderMessageForwardDecls(const protobuf::FileDescriptor* file,
Context& ctx, bool strip_feature_includes) {
// Import forward-declaration of types defined in this file.
ctx.EmitLegacy("#include \"$0\"\n", UpbCFilename(file));
ctx.EmitLegacy("#include \"$0\"\n", ForwardingHeaderFilename(file));
WriteForwardDecls(file, ctx);
// Import forward-declaration of types in dependencies.
for (int i = 0; i < file->dependency_count(); ++i) {
if (strip_feature_includes &&
compiler::IsKnownFeatureProto(file->dependency(i)->name())) {
// Strip feature imports for editions codegen tests.
continue;
}
ctx.EmitLegacy("#include \"$0\"\n",
ForwardingHeaderFilename(file->dependency(i)));
WriteForwardDecls(file->dependency(i), ctx);
}
ctx.Emit("\n");
}
Expand Down

0 comments on commit 354a5a9

Please sign in to comment.