Skip to content

Commit

Permalink
Fixed the use of c++ keywords within namespace names (#15954)
Browse files Browse the repository at this point in the history
Closes #15954

COPYBARA_INTEGRATE_REVIEW=#15954 from Ilukatsk:main acf3fa4
PiperOrigin-RevId: 611573919
  • Loading branch information
Ilukatsk authored and copybara-github committed Feb 29, 2024
1 parent 421040b commit 988194a
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/google/protobuf/compiler/cpp/helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand Down Expand Up @@ -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<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) {
if (package.empty()) return "";
return absl::StrCat("::", DotsToColons(package));
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 988194a

Please sign in to comment.