Protocol Buffers - Google's data interchange format (grpc依赖) https://developers.google.com/protocol-buffers/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.5 KiB

Created proper `names.h` headers for all upb generators. The goal of the `names.h` convention is to have a single canonical place where a code generator can define the set of symbols it exports to other code generators, and a canonical place where the name mangling logic is implemented. Each upb code generator now has its own `names.h` file defining the symbols that it owns & exports: * `third_party/upb/upb_generator/c/names.h` (for `foo.upb.h` files) * `third_party/upb/upb_generator/minitable/names.h` (for `foo.upb_minitable.h` files) * `third_party/upb/upb_generator/reflection/names.h` (for `foo.upbdefs.h` files) This is a significant improvement over the previous situation where the name mangling functions were co-mingled in `common.h`/`mangle.h`, or sprinkled throughout the generators, with no clear structure for which code generator owns which symbols. With this structure in place, the visibility lists for the various `names.h` files provide a clear dependency graph for how different generators depend on each other. In general, we want to keep dependencies on the "C" code generator to a minimum, since it is the largest and most complicated of upb's generated APIs, and is also the most prone to symbol name clashes. Note that upb's `names.h` headers are somewhat unusual, in that we do not want them to depend on C++'s reflection or upb's reflection. Most `names.h` headers in protobuf would use types like `proto2::Descriptor`, but we don't want upb to depend on C++ reflection, especially during its bootstrapping process. We also don't want to force users to build upb defs just to use these name mangling functions. So we use only plain string types like `absl::string_view` and `std::string`. PiperOrigin-RevId: 672397247
3 months ago
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include "upb_generator/minitable/names.h"
#include <string>
#include "absl/strings/str_replace.h"
#include "absl/strings/string_view.h"
#include "upb_generator/minitable/names_internal.h"
namespace upb {
namespace generator {
namespace {
std::string ToCIdent(absl::string_view str) {
return absl::StrReplaceAll(str, {{".", "_"}, {"/", "_"}, {"-", "_"}});
}
std::string MangleName(absl::string_view name) {
return absl::StrReplaceAll(name, {{"_", "_0"}, {".", "__"}});
}
} // namespace
std::string MiniTableHeaderFilename(absl::string_view proto_filename) {
return MiniTableHeaderFilename(proto_filename, false);
}
std::string MiniTableMessageVarName(absl::string_view full_name) {
return MangleName(full_name) + "_msg_init";
}
std::string MiniTableMessagePtrVarName(absl::string_view full_name) {
return MiniTableMessageVarName(full_name) + "_ptr";
}
std::string MiniTableEnumVarName(absl::string_view full_name) {
return MangleName(full_name) + "_enum_init";
}
std::string MiniTableExtensionVarName(absl::string_view full_name) {
return ToCIdent(full_name) + "_ext";
}
std::string MiniTableFileVarName(absl::string_view proto_filename) {
return ToCIdent(proto_filename) + "_upb_file_layout";
}
} // namespace generator
} // namespace upb