Utilize WrapNamespace in protoc-gen-hpb and remove {WriteStartNamespace, WriteEndNameSpace}

PiperOrigin-RevId: 691566269
pull/19051/head
Hong Shin 5 months ago committed by Copybara-Service
parent eb8f810de9
commit 99e35c0fe5
  1. 27
      hpb_generator/context.h
  2. 24
      hpb_generator/names.cc
  3. 67
      hpb_generator/protoc-gen-hpb.cc

@ -12,6 +12,7 @@
#include <utility> #include <utility>
#include "absl/strings/ascii.h" #include "absl/strings/ascii.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_replace.h" #include "absl/strings/str_replace.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
#include "absl/strings/substitute.h" #include "absl/strings/substitute.h"
@ -102,6 +103,32 @@ inline void EmitFileWarning(const google::protobuf::FileDescriptor* file, Contex
file->name()); file->name());
ctx.Emit("\n"); ctx.Emit("\n");
} }
// TODO: b/346865271 append ::hpb instead of ::protos after namespace swap
inline std::string NamespaceFromPackageName(absl::string_view package_name) {
return absl::StrCat(absl::StrReplaceAll(package_name, {{".", "::"}}),
"::protos");
}
template <typename T>
void WrapNamespace(const google::protobuf::FileDescriptor* file, Context& ctx, T&& body) {
if (file->package().empty()) {
body();
} else {
ctx.Emit(
{
{"body", body},
{"namespace", NamespaceFromPackageName(file->package())},
},
R"cc(
namespace $namespace$ {
$body$
} // namespace $namespace$
)cc");
}
}
} // namespace protobuf } // namespace protobuf
} // namespace google::hpb_generator } // namespace google::hpb_generator

@ -19,12 +19,6 @@ namespace protobuf = ::proto2;
namespace { namespace {
// TODO: b/346865271 append ::hpb instead of ::protos after namespace swap
std::string NamespaceFromPackageName(absl::string_view package_name) {
return absl::StrCat(absl::StrReplaceAll(package_name, {{".", "::"}}),
"::protos");
}
std::string DotsToColons(const absl::string_view name) { std::string DotsToColons(const absl::string_view name) {
return absl::StrReplaceAll(name, {{".", "::"}}); return absl::StrReplaceAll(name, {{".", "::"}});
} }
@ -121,24 +115,6 @@ std::string CppHeaderFilename(const google::protobuf::FileDescriptor* file) {
return compiler::StripProto(file->name()) + ".upb.proto.h"; return compiler::StripProto(file->name()) + ".upb.proto.h";
} }
void WriteStartNamespace(const protobuf::FileDescriptor* file, Context& ctx) {
// Skip namespace generation if package name is not specified.
if (file->package().empty()) {
return;
}
ctx.EmitLegacy("namespace $0 {\n\n",
NamespaceFromPackageName(file->package()));
}
void WriteEndNamespace(const protobuf::FileDescriptor* file, Context& ctx) {
if (file->package().empty()) {
return;
}
ctx.EmitLegacy("} // namespace $0\n\n",
NamespaceFromPackageName(file->package()));
}
std::string CppConstType(const protobuf::FieldDescriptor* field) { std::string CppConstType(const protobuf::FieldDescriptor* field) {
return CppTypeInternal(field, /* is_const= */ true, return CppTypeInternal(field, /* is_const= */ true,
/* is_type_parameter= */ false); /* is_type_parameter= */ false);

@ -163,25 +163,24 @@ void WriteHeader(const protobuf::FileDescriptor* file, Context& ctx,
} }
WriteHeaderMessageForwardDecls(file, ctx, strip_feature_includes); WriteHeaderMessageForwardDecls(file, ctx, strip_feature_includes);
WriteStartNamespace(file, ctx);
std::vector<const protobuf::EnumDescriptor*> this_file_enums = std::vector<const protobuf::EnumDescriptor*> this_file_enums =
SortedEnums(file); SortedEnums(file);
// Write Class and Enums. WrapNamespace(file, ctx, [&]() {
WriteEnumDeclarations(this_file_enums, ctx); // Write Class and Enums.
ctx.Emit("\n"); WriteEnumDeclarations(this_file_enums, ctx);
ctx.Emit("\n");
for (auto message : this_file_messages) {
WriteMessageClassDeclarations(message, this_file_exts, this_file_enums,
ctx);
}
ctx.Emit("\n");
WriteExtensionIdentifiersHeader(this_file_exts, ctx); for (auto message : this_file_messages) {
ctx.Emit("\n"); WriteMessageClassDeclarations(message, this_file_exts, this_file_enums,
ctx);
}
ctx.Emit("\n");
WriteEndNamespace(file, ctx); WriteExtensionIdentifiersHeader(this_file_exts, ctx);
ctx.Emit("\n");
});
ctx.Emit("\n#include \"upb/port/undef.inc\"\n\n"); ctx.Emit("\n#include \"upb/port/undef.inc\"\n\n");
// End of "C" section. // End of "C" section.
@ -213,12 +212,12 @@ void WriteSource(const protobuf::FileDescriptor* file, Context& ctx,
} }
ctx.EmitLegacy("#include \"upb/port/def.inc\"\n"); ctx.EmitLegacy("#include \"upb/port/def.inc\"\n");
WriteStartNamespace(file, ctx); WrapNamespace(file, ctx, [&]() {
WriteMessageImplementations(file, ctx); WriteMessageImplementations(file, ctx);
const std::vector<const protobuf::FieldDescriptor*> this_file_exts = const std::vector<const protobuf::FieldDescriptor*> this_file_exts =
SortedExtensions(file); SortedExtensions(file);
WriteExtensionIdentifiers(this_file_exts, ctx); WriteExtensionIdentifiers(this_file_exts, ctx);
WriteEndNamespace(file, ctx); });
ctx.Emit("#include \"upb/port/undef.inc\"\n\n"); ctx.Emit("#include \"upb/port/undef.inc\"\n\n");
} }
@ -238,23 +237,21 @@ void WriteTypedefForwardingHeader(
const protobuf::FileDescriptor* file, const protobuf::FileDescriptor* file,
const std::vector<const protobuf::Descriptor*>& file_messages, const std::vector<const protobuf::Descriptor*>& file_messages,
Context& ctx) { Context& ctx) {
WriteStartNamespace(file, ctx); WrapNamespace(file, ctx, [&]() {
// Forward-declare types defined in this file.
// Forward-declare types defined in this file. for (auto message : file_messages) {
for (auto message : file_messages) { ctx.Emit({{"class_name", ClassName(message)}},
ctx.EmitLegacy( R"cc(
R"cc( class $class_name$;
class $0; namespace internal {
namespace internal { class $class_name$Access;
class $0Access; class $class_name$Proxy;
class $0Proxy; class $class_name$CProxy;
class $0CProxy; } // namespace internal
} // namespace internal )cc");
)cc", }
ClassName(message)); });
}
ctx.Emit("\n"); ctx.Emit("\n");
WriteEndNamespace(file, ctx);
} }
/// Writes includes for upb C minitables and fwd.h for transitive typedefs. /// Writes includes for upb C minitables and fwd.h for transitive typedefs.

Loading…
Cancel
Save