Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions hpb_generator/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,22 @@ inline std::string ToPreproc(absl::string_view str) {
return absl::AsciiStrToUpper(ToCIdent(str));
}

inline std::string StringifiedBackend(Context& ctx) {
return ctx.options().backend == Backend::UPB ? "upb" : "cpp";
}

inline void EmitFileWarning(const google::protobuf::FileDescriptor* file, Context& ctx) {
ctx.EmitLegacy(
R"cc(
/* This file was generated by hpb_generator (Handle Protobuf) "
from the input
* file:
*
* $0
*
* Do not edit -- your changes will be discarded when the file is
* regenerated. */
)cc",
file->name());
ctx.Emit("\n");
ctx.Emit({{"file_name", file->name()}, {"backend", StringifiedBackend(ctx)}},
R"cc(
/* This file was generated by hpb ($backend$) "
from the input
* file:
*
* $file_name$
*
* Do not edit -- your changes will be discarded when the file is
* regenerated. */
)cc");
}

// TODO: b/346865271 append ::hpb instead of ::protos after namespace swap
Expand Down
46 changes: 39 additions & 7 deletions hpb_generator/protoc-gen-hpb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ namespace protobuf = ::proto2;
using FileDescriptor = ::google::protobuf::FileDescriptor;
using google::protobuf::Edition;

void WriteSource(const protobuf::FileDescriptor* file, Context& ctx);
void WriteHeader(const protobuf::FileDescriptor* file, Context& ctx);
void WriteSource(const protobuf::FileDescriptor* file, Context& ctx);
void WriteMessageImplementations(const protobuf::FileDescriptor* file,
Context& ctx);
void WriteTypedefForwardingHeader(
Expand Down Expand Up @@ -103,10 +103,42 @@ void WriteForwardDecls(const protobuf::FileDescriptor* file, Context& ctx) {
}

void WriteHeader(const protobuf::FileDescriptor* file, Context& ctx) {
if (ctx.options().backend == Backend::CPP) {
abort();
}
EmitFileWarning(file, ctx);

if (ctx.options().backend == Backend::CPP || true) {
const auto msgs = SortedMessages(file);
auto ns = absl::StrCat(absl::StrReplaceAll(file->package(), {{".", "::"}}),
"::hpb");
for (auto message : msgs) {
ctx.Emit({{"class_name", ClassName(message)}, {"namespace", ns}},
R"cc(

namespace $namespace$ {

class $class_name$ {
public:
using CProxy = bool;
using Proxy = bool;
using Access = bool;

$class_name$() = default;

$class_name$(void* msg) : msg_(msg) {}

void* msg() const { return msg_; }

// $class_name$::$class_name$(int x): x_(x) {
// }

// friend class hpb::internal::backend::cpp::Shim;
// friend struct hpb::internal::PrivateAccess;
private:
void* msg_;
};
} // namespace $namespace$
)cc");
}
}
ctx.EmitLegacy(
R"cc(
#ifndef $0_HPB_PROTO_H_
Expand Down Expand Up @@ -171,10 +203,10 @@ void WriteHeader(const protobuf::FileDescriptor* file, Context& ctx) {

// Writes a .hpb.cc source file.
void WriteSource(const protobuf::FileDescriptor* file, Context& ctx) {
if (ctx.options().backend == Backend::CPP) {
abort();
}
EmitFileWarning(file, ctx);
if (ctx.options().backend == Backend::CPP || true) {
return;
}

ctx.EmitLegacy(
R"cc(
Expand Down
Loading