Skip to content

Commit 082cf00

Browse files
honglookercopybara-github
authored andcommitted
hpb_generator: abrogate output in favour of io::Printer
We unify hpb's impl to utilize Protobuf's canonical printer, as opposed to a bespoke solution. - augment hpb::Context with EmitLegacy, which keeps the $* syntax while we convert. This allows for a far simpler transitional pathway, and we can incrementally port to .Emit() as we see fit. Of course, all new calls should use .Emit(). - use the Context to wrap io::Printer and any hpb-specific info (e.g. what backend we're using) PiperOrigin-RevId: 686198921
1 parent 5aa7abc commit 082cf00

16 files changed

+397
-559
lines changed

hpb_generator/context.h

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,19 @@
99
#define GOOGLE_PROTOBUF_COMPILER_HPB_CONTEXT_H__
1010

1111
#include <memory>
12+
#include <string>
1213
#include <utility>
1314

15+
#include "absl/strings/ascii.h"
16+
#include "absl/strings/str_replace.h"
1417
#include "absl/strings/string_view.h"
18+
#include "absl/strings/substitute.h"
1519
#include "absl/types/source_location.h"
1620
#include "absl/types/span.h"
21+
#include "google/protobuf/descriptor.h"
1722
#include "google/protobuf/io/printer.h"
1823
#include "google/protobuf/io/zero_copy_stream.h"
24+
#include "upb_generator/c/names.h"
1925
namespace google::protobuf::hpb_generator {
2026

2127
enum class Backend { UPB, CPP };
@@ -38,11 +44,8 @@ struct Options {
3844
*/
3945
class Context final {
4046
public:
41-
Context(std::unique_ptr<io::ZeroCopyOutputStream> stream,
42-
const Options& options)
43-
: stream_(std::move(stream)),
44-
printer_(stream_.get()),
45-
options_(options) {}
47+
Context(io::ZeroCopyOutputStream* stream, const Options& options)
48+
: stream_(stream), printer_(stream_), options_(options) {}
4649

4750
void Emit(absl::Span<const io::Printer::Sub> vars, absl::string_view format,
4851
absl::SourceLocation loc = absl::SourceLocation::current()) {
@@ -54,19 +57,56 @@ class Context final {
5457
printer_.Emit(format, loc);
5558
}
5659

60+
// TODO: b/373438292 - Remove EmitLegacy in favor of Emit.
61+
// This is an interim solution while we migrate from Output to io::Printer
62+
template <class... Arg>
63+
void EmitLegacy(absl::string_view format, const Arg&... arg) {
64+
auto res = absl::Substitute(format, arg...);
65+
printer_.Emit(res, absl::SourceLocation::current());
66+
}
67+
5768
const Options& options() { return options_; }
69+
io::Printer& printer() { return printer_; }
5870

5971
Context(const Context&) = delete;
6072
Context& operator=(const Context&) = delete;
6173
Context(Context&&) = delete;
6274
Context& operator=(Context&&) = delete;
6375

6476
private:
65-
std::unique_ptr<io::ZeroCopyOutputStream> stream_;
77+
io::ZeroCopyOutputStream* stream_;
6678
io::Printer printer_;
6779
const Options& options_;
6880
};
6981

82+
// TODO: b/373438292 - re-house these 4 legacy funcs post io::Printer move
83+
inline std::string MessageName(const google::protobuf::Descriptor* descriptor) {
84+
return upb::generator::CApiMessageType(descriptor->full_name());
85+
}
86+
87+
inline std::string ToCIdent(absl::string_view str) {
88+
return absl::StrReplaceAll(str, {{".", "_"}, {"/", "_"}, {"-", "_"}});
89+
}
90+
91+
inline std::string ToPreproc(absl::string_view str) {
92+
return absl::AsciiStrToUpper(ToCIdent(str));
93+
}
94+
95+
inline void EmitFileWarning(const google::protobuf::FileDescriptor* file, Context& ctx) {
96+
ctx.EmitLegacy(
97+
R"cc(
98+
/* This file was generated by hpb_generator (Handle Protobuf) "
99+
from the input
100+
* file:
101+
*
102+
* $0
103+
*
104+
* Do not edit -- your changes will be discarded when the file is
105+
* regenerated. */
106+
)cc",
107+
file->name());
108+
ctx.Emit("\n");
109+
}
70110
} // namespace protobuf
71111
} // namespace google::hpb_generator
72112

0 commit comments

Comments
 (0)