From f7a58bb060cd06d7f0d9d0810e7b151dbc391fb5 Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Fri, 19 Nov 2021 15:34:25 -0500 Subject: [PATCH] Add a envvar to provide a prefix on all proto package prefixed symbols. While at it, fix a header comment. --- .../compiler/objectivec/objectivec_helpers.cc | 18 +++++++++++++++--- .../compiler/objectivec/objectivec_helpers.h | 4 ++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc b/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc index 5f4b7f60fa..74943cb0a8 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc @@ -102,9 +102,14 @@ class PrefixModeStorage { bool is_package_exempted(const std::string& package); + // When using a proto package as the prefix, this should be added as the + // prefix in front of it. + const std::string& forced_package_prefix() const { return forced_prefix_; } + private: bool use_package_name_; std::string exception_path_; + std::string forced_prefix_; std::unordered_set exceptions_; }; @@ -120,6 +125,13 @@ PrefixModeStorage::PrefixModeStorage() { if (exception_path) { exception_path_ = exception_path; } + + // This one is a not expected to be common, so it doesn't get a generation + // option, just the env var. + const char* prefix = getenv("GPB_OBJC_USE_PACKAGE_AS_PREFIX_PREFIX"); + if (prefix) { + forced_prefix_ = prefix; + } } bool PrefixModeStorage::is_package_exempted(const std::string& package) { @@ -510,8 +522,8 @@ std::string FileClassPrefix(const FileDescriptor* file) { return file->options().objc_class_prefix(); } - // If package prefix isn't enabled or no package, done. - if (!g_prefix_mode.use_package_name() || file->package().empty()) { + // If package prefix isn't enabled, done. + if (!g_prefix_mode.use_package_name()) { return ""; } @@ -538,7 +550,7 @@ std::string FileClassPrefix(const FileDescriptor* file) { if (!result.empty()) { result.append("_"); } - return result; + return g_prefix_mode.forced_package_prefix() + result; } std::string FilePath(const FileDescriptor* file) { diff --git a/src/google/protobuf/compiler/objectivec/objectivec_helpers.h b/src/google/protobuf/compiler/objectivec/objectivec_helpers.h index 13a1052406..f4b71ced8e 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_helpers.h +++ b/src/google/protobuf/compiler/objectivec/objectivec_helpers.h @@ -54,8 +54,8 @@ namespace objectivec { bool PROTOC_EXPORT UseProtoPackageAsDefaultPrefix(); void PROTOC_EXPORT SetUseProtoPackageAsDefaultPrefix(bool on_or_off); // Get/Set the path to a file to load as exceptions when -// `UseProtoPackageAsDefaultPrefixUseProtoPackageAsDefaultPrefix()` is `true`. -// And empty string means there should be no exceptions loaded. +// `UseProtoPackageAsDefaultPrefix()` is `true`. An empty string means there +// should be no exceptions. std::string PROTOC_EXPORT GetProtoPackagePrefixExceptionList(); void PROTOC_EXPORT SetProtoPackagePrefixExceptionList( const std::string& file_path);