diff --git a/src/google/protobuf/compiler/cpp/helpers.cc b/src/google/protobuf/compiler/cpp/helpers.cc index d36967f7dcb6a..e24acde93ff68 100644 --- a/src/google/protobuf/compiler/cpp/helpers.cc +++ b/src/google/protobuf/compiler/cpp/helpers.cc @@ -176,16 +176,17 @@ static const char* const kKeywordList[] = { #endif // !PROTOBUF_FUTURE_BREAKING_CHANGES }; -static absl::flat_hash_set* MakeKeywordsMap() { - auto* result = new absl::flat_hash_set(); - for (const auto keyword : kKeywordList) { - result->emplace(keyword); +const absl::flat_hash_set& Keywords() { + static auto* keywords = []{ + auto keywords = new absl::flat_hash_set(); + + for (const auto keyword : kKeywordList) { + keywords->emplace(keyword); + } } - return result; + return *keywords; } -static absl::flat_hash_set& kKeywords = *MakeKeywordsMap(); - std::string IntTypeName(const Options& options, const std::string& type) { return type + "_t"; } @@ -509,7 +510,7 @@ std::string SuperClassName(const Descriptor* descriptor, } std::string ResolveKeyword(const std::string& name) { - if (kKeywords.count(name) > 0) { + if (Keywords().count(name) > 0) { return name + "_"; } return name; @@ -518,7 +519,7 @@ std::string ResolveKeyword(const std::string& name) { std::string FieldName(const FieldDescriptor* field) { std::string result = field->name(); LowerString(&result); - if (kKeywords.count(result) > 0) { + if (Keywords().count(result) > 0) { result.append("_"); } return result; @@ -552,7 +553,7 @@ std::string QualifiedOneofCaseConstantName(const FieldDescriptor* field) { std::string EnumValueName(const EnumValueDescriptor* enum_value) { std::string result = enum_value->name(); - if (kKeywords.count(result) > 0) { + if (Keywords().count(result) > 0) { result.append("_"); } return result; @@ -863,7 +864,7 @@ std::string SafeFunctionName(const Descriptor* descriptor, // Single underscore will also make it conflicting with the private data // member. We use double underscore to escape function names. function_name.append("__"); - } else if (kKeywords.count(name) > 0) { + } else if (Keywords().count(name) > 0) { // If the field name is a keyword, we append the underscore back to keep it // consistent with other function names. function_name.append("_"); @@ -1116,7 +1117,7 @@ bool IsAnyMessage(const Descriptor* descriptor, const Options& options) { } bool IsWellKnownMessage(const FileDescriptor* file) { - static const absl::flat_hash_set well_known_files{ + static const auto* well_known_files = new absl::flat_hash_set{ "google/protobuf/any.proto", "google/protobuf/api.proto", "google/protobuf/compiler/plugin.proto", @@ -1130,7 +1131,7 @@ bool IsWellKnownMessage(const FileDescriptor* file) { "google/protobuf/type.proto", "google/protobuf/wrappers.proto", }; - return well_known_files.find(file->name()) != well_known_files.end(); + return well_known_files->find(file->name()) != well_known_files->end(); } static void GenerateUtf8CheckCode(const FieldDescriptor* field,