diff --git a/hpb_generator/context.h b/hpb_generator/context.h index 58aba5e011..d7a7ae9bb0 100644 --- a/hpb_generator/context.h +++ b/hpb_generator/context.h @@ -12,6 +12,7 @@ #include #include "absl/strings/ascii.h" +#include "absl/strings/str_cat.h" #include "absl/strings/str_replace.h" #include "absl/strings/string_view.h" #include "absl/strings/substitute.h" @@ -102,6 +103,32 @@ inline void EmitFileWarning(const google::protobuf::FileDescriptor* file, Contex file->name()); 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 +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 google::hpb_generator diff --git a/hpb_generator/names.cc b/hpb_generator/names.cc index 8492051ac0..617062ace0 100644 --- a/hpb_generator/names.cc +++ b/hpb_generator/names.cc @@ -19,12 +19,6 @@ namespace protobuf = ::proto2; 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) { return absl::StrReplaceAll(name, {{".", "::"}}); } @@ -121,24 +115,6 @@ std::string CppHeaderFilename(const google::protobuf::FileDescriptor* file) { 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) { return CppTypeInternal(field, /* is_const= */ true, /* is_type_parameter= */ false); diff --git a/hpb_generator/protoc-gen-hpb.cc b/hpb_generator/protoc-gen-hpb.cc index 0b94b833a5..30d6b4e31e 100644 --- a/hpb_generator/protoc-gen-hpb.cc +++ b/hpb_generator/protoc-gen-hpb.cc @@ -163,25 +163,24 @@ void WriteHeader(const protobuf::FileDescriptor* file, Context& ctx, } WriteHeaderMessageForwardDecls(file, ctx, strip_feature_includes); - WriteStartNamespace(file, ctx); std::vector this_file_enums = SortedEnums(file); - // Write Class and Enums. - 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"); + WrapNamespace(file, ctx, [&]() { + // Write Class and Enums. + WriteEnumDeclarations(this_file_enums, ctx); + ctx.Emit("\n"); - WriteExtensionIdentifiersHeader(this_file_exts, ctx); - ctx.Emit("\n"); + for (auto message : this_file_messages) { + 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"); // End of "C" section. @@ -213,12 +212,12 @@ void WriteSource(const protobuf::FileDescriptor* file, Context& ctx, } ctx.EmitLegacy("#include \"upb/port/def.inc\"\n"); - WriteStartNamespace(file, ctx); - WriteMessageImplementations(file, ctx); - const std::vector this_file_exts = - SortedExtensions(file); - WriteExtensionIdentifiers(this_file_exts, ctx); - WriteEndNamespace(file, ctx); + WrapNamespace(file, ctx, [&]() { + WriteMessageImplementations(file, ctx); + const std::vector this_file_exts = + SortedExtensions(file); + WriteExtensionIdentifiers(this_file_exts, ctx); + }); ctx.Emit("#include \"upb/port/undef.inc\"\n\n"); } @@ -238,23 +237,21 @@ void WriteTypedefForwardingHeader( const protobuf::FileDescriptor* file, const std::vector& file_messages, Context& ctx) { - WriteStartNamespace(file, ctx); - - // Forward-declare types defined in this file. - for (auto message : file_messages) { - ctx.EmitLegacy( - R"cc( - class $0; - namespace internal { - class $0Access; - class $0Proxy; - class $0CProxy; - } // namespace internal - )cc", - ClassName(message)); - } + WrapNamespace(file, ctx, [&]() { + // Forward-declare types defined in this file. + for (auto message : file_messages) { + ctx.Emit({{"class_name", ClassName(message)}}, + R"cc( + class $class_name$; + namespace internal { + class $class_name$Access; + class $class_name$Proxy; + class $class_name$CProxy; + } // namespace internal + )cc"); + } + }); ctx.Emit("\n"); - WriteEndNamespace(file, ctx); } /// Writes includes for upb C minitables and fwd.h for transitive typedefs.