From 988194a591f0b82b1ae52590e4daa5f0519ef8c7 Mon Sep 17 00:00:00 2001 From: Ilukatsk <128325768+Ilukatsk@users.noreply.github.com> Date: Thu, 29 Feb 2024 13:16:22 -0800 Subject: [PATCH] Fixed the use of c++ keywords within namespace names (#15954) Closes #15954 COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/15954 from Ilukatsk:main acf3fa436cf153c054a349394215ccf893cc215a PiperOrigin-RevId: 611573919 --- src/google/protobuf/compiler/cpp/helpers.cc | 28 +++++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/google/protobuf/compiler/cpp/helpers.cc b/src/google/protobuf/compiler/cpp/helpers.cc index ec96b94962..c69ce3031a 100644 --- a/src/google/protobuf/compiler/cpp/helpers.cc +++ b/src/google/protobuf/compiler/cpp/helpers.cc @@ -28,7 +28,9 @@ #include "absl/strings/ascii.h" #include "absl/strings/escaping.h" #include "absl/strings/str_cat.h" +#include "absl/strings/str_join.h" #include "absl/strings/str_replace.h" +#include "absl/strings/str_split.h" #include "absl/strings/string_view.h" #include "absl/strings/substitute.h" #include "absl/synchronization/mutex.h" @@ -55,10 +57,6 @@ namespace { constexpr absl::string_view kAnyMessageName = "Any"; constexpr absl::string_view kAnyProtoFile = "google/protobuf/any.proto"; -std::string DotsToColons(absl::string_view name) { - return absl::StrReplaceAll(name, {{".", "::"}}); -} - static const char* const kKeywordList[] = { // clang-format off "NULL", @@ -427,6 +425,21 @@ std::string QualifiedExtensionName(const FieldDescriptor* d) { return QualifiedExtensionName(d, Options()); } +std::string ResolveKeyword(absl::string_view name) { + if (Keywords().count(name) > 0) { + return absl::StrCat(name, "_"); + } + return std::string(name); +} + +std::string DotsToColons(absl::string_view name) { + std::vector scope = absl::StrSplit(name, ".", absl::SkipEmpty()); + for (auto& word : scope) { + word = ResolveKeyword(word); + } + return absl::StrJoin(scope, "::"); +} + std::string Namespace(absl::string_view package) { if (package.empty()) return ""; return absl::StrCat("::", DotsToColons(package)); @@ -504,13 +517,6 @@ std::string SuperClassName(const Descriptor* descriptor, "::internal::", simple_base); } -std::string ResolveKeyword(absl::string_view name) { - if (Keywords().count(name) > 0) { - return absl::StrCat(name, "_"); - } - return std::string(name); -} - std::string FieldName(const FieldDescriptor* field) { std::string result = field->name(); absl::AsciiStrToLower(&result);