diff --git a/hpb/backend/BUILD b/hpb/backend/BUILD index fc615dd4dc355..7f06107dd0286 100644 --- a/hpb/backend/BUILD +++ b/hpb/backend/BUILD @@ -5,6 +5,7 @@ # license that can be found in the LICENSE file or at # https://developers.google.com/open-source/licenses/bsd +load("@rules_cc//cc:cc_test.bzl", "cc_test") load("@rules_cc//cc:defs.bzl", "cc_library") package(default_applicable_licenses = ["//:license"]) @@ -26,3 +27,16 @@ cc_library( ], }), ) + +cc_test( + name = "shared_test", + srcs = ["shared_test.cc"], + deps = [ + "//hpb", + "//hpb:arena", + "//hpb:ptr", + "//hpb_generator/tests:test_model_hpb_proto", + "@googletest//:gtest", + "@googletest//:gtest_main", + ], +) diff --git a/hpb/backend/cpp/BUILD b/hpb/backend/cpp/BUILD index 35a94e61166e2..4b0561956e2e7 100644 --- a/hpb/backend/cpp/BUILD +++ b/hpb/backend/cpp/BUILD @@ -5,7 +5,6 @@ # license that can be found in the LICENSE file or at # https://developers.google.com/open-source/licenses/bsd -load("@rules_cc//cc:cc_test.bzl", "cc_test") load("@rules_cc//cc:defs.bzl", "cc_library") package(default_applicable_licenses = ["//:license"]) @@ -43,16 +42,3 @@ cc_library( hdrs = ["repeated_field.h"], visibility = ["//hpb:__subpackages__"], ) - -cc_test( - name = "cpp_test", - srcs = ["cpp_test.cc"], - deps = [ - "//hpb", - "//hpb:arena", - "//hpb:ptr", - "//hpb_generator/tests:test_model_hpb_proto", - "@googletest//:gtest", - "@googletest//:gtest_main", - ], -) diff --git a/hpb/backend/cpp/cpp_test.cc b/hpb/backend/shared_test.cc similarity index 68% rename from hpb/backend/cpp/cpp_test.cc rename to hpb/backend/shared_test.cc index 6500249cd8c85..cc2060b2b9331 100644 --- a/hpb/backend/cpp/cpp_test.cc +++ b/hpb/backend/shared_test.cc @@ -15,11 +15,17 @@ namespace hpb::testing { namespace { using ::hpb_unittest::protos::TestModel; +using ::hpb_unittest::protos::TestModel_Category_IMAGES; -TEST(CppBackend, CanCreateMessage) { +// Tests in this file are run against both backends {upb, cpp} to ensure +// api conformance, compatibility, and correctness. + +TEST(MultiBackend, CanCreateMessage) { hpb::Arena arena; hpb::Ptr test_model_ptr = hpb::CreateMessage(arena); (void)test_model_ptr; } + +TEST(MultiBackend, MessageEnums) { EXPECT_EQ(5, TestModel_Category_IMAGES); } } // namespace } // namespace hpb::testing diff --git a/hpb_generator/generator.cc b/hpb_generator/generator.cc index f534ade7115b2..d93dac10ef8a4 100644 --- a/hpb_generator/generator.cc +++ b/hpb_generator/generator.cc @@ -57,6 +57,50 @@ void WriteForwardDecls(const google::protobuf::FileDescriptor* file, Context& ct void WriteHeader(const google::protobuf::FileDescriptor* file, Context& ctx) { if (ctx.options().backend == Backend::CPP) { EmitFileWarning(file, ctx); + ctx.Emit({{"filename", ToPreproc(file->name())}}, + R"cc( +#ifndef $filename$_HPB_PROTO_H_ +#define $filename$_HPB_PROTO_H_ + )cc"); + + // Import headers for proto public dependencies. + for (int i = 0; i < file->public_dependency_count(); i++) { + if (i == 0) { + ctx.Emit("// Public Imports.\n"); + } + ctx.Emit({{"header", CppHeaderFilename(file->public_dependency(i))}}, + "#include \"$header$\"\n"); + if (i == file->public_dependency_count() - 1) { + ctx.Emit("\n"); + } + } + + ctx.Emit( + "#include \"hpb/internal/os_macros_undef.inc\"\n"); + + const std::vector this_file_messages = + SortedMessages(file); + const std::vector + this_file_exts{}; // TODO: extensions + + if (!this_file_messages.empty()) { + ctx.Emit("\n"); + } + + WriteHeaderMessageForwardDecls(file, ctx); + WriteForwardDecls(file, ctx); + + std::vector this_file_enums = + SortedEnums(file); + + WrapNamespace(file, ctx, [&]() { + // Write Class and Enums. + WriteEnumDeclarations(this_file_enums, ctx); + ctx.Emit("\n"); + // TODO: class decls + // TODO: extension identifiers + }); + const auto msgs = SortedMessages(file); for (auto message : msgs) { ctx.Emit({{"type", QualifiedClassName(message)}, @@ -90,6 +134,11 @@ void WriteHeader(const google::protobuf::FileDescriptor* file, Context& ctx) { } // namespace $namespace$ )cc"); } + ctx.Emit( + "#include " + "\"hpb/internal/os_macros_restore.inc\"\n"); + ctx.Emit({{"filename", ToPreproc(file->name())}}, + "#endif /* $filename$_HPB_PROTO_H_ */\n"); return; } EmitFileWarning(file, ctx); @@ -254,8 +303,10 @@ void WriteTypedefForwardingHeader( void WriteHeaderMessageForwardDecls(const google::protobuf::FileDescriptor* file, Context& ctx) { // Import forward-declaration of types defined in this file. - ctx.Emit({{"upb_filename", UpbCFilename(file)}}, - "#include \"$upb_filename$\"\n"); + if (ctx.options().backend == Backend::UPB) { + ctx.Emit({{"upb_filename", UpbCFilename(file)}}, + "#include \"$upb_filename$\"\n"); + } WriteForwardDecls(file, ctx); // Import forward-declaration of types in dependencies. for (int i = 0; i < file->dependency_count(); ++i) {