Move keywords.h/keywords.cc to hpb generator.

These functions are only used by the hpb generator, so would make most sense to put there.

PiperOrigin-RevId: 672757016
pull/18178/head
Joshua Haberman 7 months ago committed by Copybara-Service
parent 9c835afdce
commit 65b7a2c65e
  1. 15
      hpb_generator/BUILD
  2. 4
      hpb_generator/gen_accessors.cc
  3. 29
      hpb_generator/keywords.cc
  4. 17
      hpb_generator/keywords.h
  5. 4
      hpb_generator/names.cc
  6. 12
      upb_generator/BUILD

@ -53,13 +53,13 @@ cc_library(
visibility = ["//visibility:private"], visibility = ["//visibility:private"],
deps = [ deps = [
":gen_utils", ":gen_utils",
":keywords",
":names", ":names",
":output", ":output",
"//:protobuf", "//:protobuf",
"//src/google/protobuf", "//src/google/protobuf",
"//upb_generator:common", "//upb_generator:common",
"//upb_generator:file_layout", "//upb_generator:file_layout",
"//upb_generator:keywords",
"//upb_generator/c:names", "//upb_generator/c:names",
"//upb_generator/minitable:names", "//upb_generator/minitable:names",
"@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/container:flat_hash_set",
@ -67,6 +67,17 @@ cc_library(
], ],
) )
cc_library(
name = "keywords",
srcs = ["keywords.cc"],
hdrs = ["keywords.h"],
visibility = ["//visibility:private"],
deps = [
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/strings",
],
)
cc_library( cc_library(
name = "output", name = "output",
srcs = ["output.cc"], srcs = ["output.cc"],
@ -101,10 +112,10 @@ cc_library(
hdrs = ["names.h"], hdrs = ["names.h"],
visibility = ["//visibility:private"], visibility = ["//visibility:private"],
deps = [ deps = [
":keywords",
":output", ":output",
"//src/google/protobuf", "//src/google/protobuf",
"//src/google/protobuf/compiler:code_generator", "//src/google/protobuf/compiler:code_generator",
"//upb_generator:keywords",
"@com_google_absl//absl/strings:string_view", "@com_google_absl//absl/strings:string_view",
], ],
) )

@ -15,11 +15,11 @@
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
#include "google/protobuf/compiler/hpb/gen_repeated_fields.h" #include "google/protobuf/compiler/hpb/gen_repeated_fields.h"
#include "google/protobuf/compiler/hpb/gen_utils.h" #include "google/protobuf/compiler/hpb/gen_utils.h"
#include "google/protobuf/compiler/hpb/keywords.h"
#include "google/protobuf/compiler/hpb/names.h" #include "google/protobuf/compiler/hpb/names.h"
#include "google/protobuf/compiler/hpb/output.h" #include "google/protobuf/compiler/hpb/output.h"
#include "google/protobuf/descriptor.h" #include "google/protobuf/descriptor.h"
#include "upb_generator/c/names.h" #include "upb_generator/c/names.h"
#include "upb_generator/keywords.h"
#include "upb_generator/minitable/names.h" #include "upb_generator/minitable/names.h"
namespace google::protobuf::hpb_generator { namespace google::protobuf::hpb_generator {
@ -567,7 +567,7 @@ std::string ResolveFieldName(const protobuf::FieldDescriptor* field,
} }
} }
} }
return upb::generator::ResolveKeywordConflict(std::string(field_name)); return ResolveKeywordConflict(field_name);
} }
} // namespace protobuf } // namespace protobuf

@ -5,15 +5,18 @@
// license that can be found in the LICENSE file or at // license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd // https://developers.google.com/open-source/licenses/bsd
#include "upb_generator/keywords.h" #include "google/protobuf/compiler/hpb/keywords.h"
#include <new>
#include <string> #include <string>
#include <unordered_set>
namespace upb { #include "absl/container/flat_hash_set.h"
namespace generator { #include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
static const char* const kKeywordList[] = { namespace google::protobuf::hpb_generator {
static const absl::string_view kKeywordList[] = {
// //
"NULL", "NULL",
"alignas", "alignas",
@ -110,22 +113,22 @@ static const char* const kKeywordList[] = {
"requires", "requires",
}; };
static std::unordered_set<std::string>* MakeKeywordsMap() { static absl::flat_hash_set<std::string>* MakeKeywordsMap() {
auto* result = new std::unordered_set<std::string>(); auto* result = new absl::flat_hash_set<std::string>();
for (const auto keyword : kKeywordList) { for (const auto keyword : kKeywordList) {
result->emplace(keyword); result->emplace(keyword);
} }
return result; return result;
} }
static std::unordered_set<std::string>& kKeywords = *MakeKeywordsMap(); static absl::flat_hash_set<std::string>& kKeywords = *MakeKeywordsMap();
std::string ResolveKeywordConflict(const std::string& name) { std::string ResolveKeywordConflict(absl::string_view name) {
if (kKeywords.count(name) > 0) { if (kKeywords.count(name) > 0) {
return name + "_"; return absl::StrCat(name, "_");
} }
return name; return std::string(name);
} }
} // namespace generator } // namespace protobuf
} // namespace upb } // namespace google::hpb_generator

@ -5,18 +5,19 @@
// license that can be found in the LICENSE file or at // license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd // https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_PROTOS_GENERATOR_KEYWORDS_H #ifndef PROTOBUF_COMPILER_HBP_GENERATOR_KEYWORDS_H
#define UPB_PROTOS_GENERATOR_KEYWORDS_H #define PROTOBUF_COMPILER_HBP_GENERATOR_KEYWORDS_H
#include <string> #include <string>
namespace upb { #include "absl/strings/string_view.h"
namespace generator {
namespace google::protobuf::hpb_generator {
// Resolves proto field name conflict with C++ reserved keywords. // Resolves proto field name conflict with C++ reserved keywords.
std::string ResolveKeywordConflict(const std::string& name); std::string ResolveKeywordConflict(absl::string_view name);
} // namespace generator } // namespace protobuf
} // namespace upb } // namespace google::hpb_generator
#endif // UPB_PROTOS_GENERATOR_KEYWORDS_H #endif // PROTOBUF_COMPILER_HBP_GENERATOR_KEYWORDS_H

@ -11,7 +11,7 @@
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
#include "google/protobuf/compiler/code_generator.h" #include "google/protobuf/compiler/code_generator.h"
#include "upb_generator/keywords.h" #include "google/protobuf/compiler/hpb/keywords.h"
namespace google::protobuf::hpb_generator { namespace google::protobuf::hpb_generator {
namespace protobuf = ::proto2; namespace protobuf = ::proto2;
@ -92,7 +92,7 @@ std::string ClassName(const protobuf::Descriptor* descriptor) {
} }
if (parent) res += ClassName(parent) + "_"; if (parent) res += ClassName(parent) + "_";
absl::StrAppend(&res, descriptor->name()); absl::StrAppend(&res, descriptor->name());
return ::upb::generator::ResolveKeywordConflict(res); return ResolveKeywordConflict(res);
} }
std::string QualifiedClassName(const protobuf::Descriptor* descriptor) { std::string QualifiedClassName(const protobuf::Descriptor* descriptor) {

@ -84,18 +84,6 @@ bootstrap_cc_library(
], ],
) )
cc_library(
name = "keywords",
srcs = [
"keywords.cc",
],
hdrs = [
"keywords.h",
],
copts = UPB_DEFAULT_CPPOPTS,
visibility = ["//src/google/protobuf/compiler/hpb:__pkg__"],
)
bootstrap_cc_library( bootstrap_cc_library(
name = "plugin", name = "plugin",
hdrs = [ hdrs = [

Loading…
Cancel
Save