Skip to content

Commit

Permalink
Utilize WrapNamespace in protoc-gen-hpb and remove {WriteStartNamespa…
Browse files Browse the repository at this point in the history
…ce, WriteEndNameSpace}

PiperOrigin-RevId: 691566269
  • Loading branch information
honglooker authored and copybara-github committed Oct 30, 2024
1 parent eb8f810 commit 99e35c0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 59 deletions.
27 changes: 27 additions & 0 deletions hpb_generator/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <utility>

#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"
Expand Down Expand Up @@ -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 <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 google::hpb_generator

Expand Down
24 changes: 0 additions & 24 deletions hpb_generator/names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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, {{".", "::"}});
}
Expand Down Expand Up @@ -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);
Expand Down
67 changes: 32 additions & 35 deletions hpb_generator/protoc-gen-hpb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,25 +163,24 @@ void WriteHeader(const protobuf::FileDescriptor* file, Context& ctx,
}

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

std::vector<const protobuf::EnumDescriptor*> 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.
Expand Down Expand Up @@ -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<const protobuf::FieldDescriptor*> this_file_exts =
SortedExtensions(file);
WriteExtensionIdentifiers(this_file_exts, ctx);
WriteEndNamespace(file, ctx);
WrapNamespace(file, ctx, [&]() {
WriteMessageImplementations(file, ctx);
const std::vector<const protobuf::FieldDescriptor*> this_file_exts =
SortedExtensions(file);
WriteExtensionIdentifiers(this_file_exts, ctx);
});

ctx.Emit("#include \"upb/port/undef.inc\"\n\n");
}
Expand All @@ -238,23 +237,21 @@ void WriteTypedefForwardingHeader(
const protobuf::FileDescriptor* file,
const std::vector<const protobuf::Descriptor*>& 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.
Expand Down

0 comments on commit 99e35c0

Please sign in to comment.