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 acf3fa436c
PiperOrigin-RevId: 611573919
pull/15992/head
Ilukatsk 9 months ago committed by Copybara-Service
parent 421040b32b
commit 988194a591
  1. 28
      src/google/protobuf/compiler/cpp/helpers.cc

@ -28,7 +28,9 @@
#include "absl/strings/ascii.h" #include "absl/strings/ascii.h"
#include "absl/strings/escaping.h" #include "absl/strings/escaping.h"
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
#include "absl/strings/str_join.h"
#include "absl/strings/str_replace.h" #include "absl/strings/str_replace.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
#include "absl/strings/substitute.h" #include "absl/strings/substitute.h"
#include "absl/synchronization/mutex.h" #include "absl/synchronization/mutex.h"
@ -55,10 +57,6 @@ namespace {
constexpr absl::string_view kAnyMessageName = "Any"; constexpr absl::string_view kAnyMessageName = "Any";
constexpr absl::string_view kAnyProtoFile = "google/protobuf/any.proto"; 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[] = { static const char* const kKeywordList[] = {
// clang-format off // clang-format off
"NULL", "NULL",
@ -427,6 +425,21 @@ std::string QualifiedExtensionName(const FieldDescriptor* d) {
return QualifiedExtensionName(d, Options()); 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<std::string> scope = absl::StrSplit(name, ".", absl::SkipEmpty());
for (auto& word : scope) {
word = ResolveKeyword(word);
}
return absl::StrJoin(scope, "::");
}
std::string Namespace(absl::string_view package) { std::string Namespace(absl::string_view package) {
if (package.empty()) return ""; if (package.empty()) return "";
return absl::StrCat("::", DotsToColons(package)); return absl::StrCat("::", DotsToColons(package));
@ -504,13 +517,6 @@ std::string SuperClassName(const Descriptor* descriptor,
"::internal::", simple_base); "::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 FieldName(const FieldDescriptor* field) {
std::string result = field->name(); std::string result = field->name();
absl::AsciiStrToLower(&result); absl::AsciiStrToLower(&result);

Loading…
Cancel
Save