Move MessageInit function to cc_library

This allows rust generator to use this function directly in order to generate symbol names for Message MiniTables.

PiperOrigin-RevId: 584054361
pull/14803/head
Kevin King 2023-11-20 09:51:51 -08:00 committed by Copybara-Service
parent c33967fcbc
commit 9b8b2760f8
4 changed files with 53 additions and 9 deletions

View File

@ -102,6 +102,7 @@ bootstrap_cc_library(
copts = UPB_DEFAULT_CPPOPTS,
visibility = ["//protos_generator:__pkg__"],
deps = [
":mangle",
"//upb:mini_table",
"//upb:mini_table_internal",
"//upb:port",
@ -190,6 +191,20 @@ bootstrap_cc_library(
],
)
cc_library(
name = "mangle",
srcs = ["mangle.cc"],
hdrs = ["mangle.h"],
copts = UPB_DEFAULT_CPPOPTS,
visibility = [
"//rust:__subpackages__",
"//upb:friends",
],
deps = [
"@com_google_absl//absl/strings",
],
)
cc_binary(
name = "libupb_generator.so",
srcs = ["upb_generator_so.c"],

View File

@ -21,15 +21,11 @@
#include "upb/mini_table/field.h"
#include "upb/mini_table/internal/field.h"
#include "upb/reflection/def.hpp"
#include "upb_generator/mangle.h"
// Must be last
#include "upb/port/def.inc"
// Generate a mangled C name for a proto object.
static std::string MangleName(absl::string_view name) {
return absl::StrReplaceAll(name, {{"_", "_0"}, {".", "__"}});
}
namespace upb {
namespace generator {
@ -60,10 +56,6 @@ void EmitFileWarning(absl::string_view name, Output& output) {
name);
}
std::string MessageInit(absl::string_view full_name) {
return MangleName(full_name) + "_msg_init";
}
std::string MessageInitName(upb::MessageDefPtr descriptor) {
return MessageInit(descriptor.full_name());
}

21
upb_generator/mangle.cc Normal file
View File

@ -0,0 +1,21 @@
#include "upb_generator/mangle.h"
#include <string>
#include "absl/strings/str_replace.h"
#include "absl/strings/string_view.h"
// Generate a mangled C name for a proto object.
static std::string MangleName(absl::string_view name) {
return absl::StrReplaceAll(name, {{"_", "_0"}, {".", "__"}});
}
namespace upb {
namespace generator {
std::string MessageInit(absl::string_view full_name) {
return MangleName(full_name) + "_msg_init";
}
} // namespace generator
} // namespace upb

16
upb_generator/mangle.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef THIRD_PARTY_UPB_UPB_GENERATOR_MANGLE_H_
#define THIRD_PARTY_UPB_UPB_GENERATOR_MANGLE_H_
#include <string>
#include "absl/strings/string_view.h"
namespace upb {
namespace generator {
std::string MessageInit(absl::string_view full_name);
} // namespace generator
} // namespace upb
#endif // THIRD_PARTY_UPB_UPB_GENERATOR_MANGLE_H_