diff --git a/hpb_generator/BUILD b/hpb_generator/BUILD index e23647031e..643d1ed6c2 100644 --- a/hpb_generator/BUILD +++ b/hpb_generator/BUILD @@ -53,13 +53,13 @@ cc_library( visibility = ["//visibility:private"], deps = [ ":gen_utils", + ":keywords", ":names", ":output", "//:protobuf", "//src/google/protobuf", "//upb_generator:common", "//upb_generator:file_layout", - "//upb_generator:keywords", "//upb_generator/c:names", "//upb_generator/minitable:names", "@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( name = "output", srcs = ["output.cc"], @@ -101,10 +112,10 @@ cc_library( hdrs = ["names.h"], visibility = ["//visibility:private"], deps = [ + ":keywords", ":output", "//src/google/protobuf", "//src/google/protobuf/compiler:code_generator", - "//upb_generator:keywords", "@com_google_absl//absl/strings:string_view", ], ) diff --git a/hpb_generator/gen_accessors.cc b/hpb_generator/gen_accessors.cc index 13fee6b0c1..a66870cd30 100644 --- a/hpb_generator/gen_accessors.cc +++ b/hpb_generator/gen_accessors.cc @@ -15,11 +15,11 @@ #include "absl/strings/string_view.h" #include "google/protobuf/compiler/hpb/gen_repeated_fields.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/output.h" #include "google/protobuf/descriptor.h" #include "upb_generator/c/names.h" -#include "upb_generator/keywords.h" #include "upb_generator/minitable/names.h" 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 diff --git a/upb_generator/keywords.cc b/hpb_generator/keywords.cc similarity index 73% rename from upb_generator/keywords.cc rename to hpb_generator/keywords.cc index beaf7dfc4a..2e5e116f31 100644 --- a/upb_generator/keywords.cc +++ b/hpb_generator/keywords.cc @@ -5,15 +5,18 @@ // license that can be found in the LICENSE file or at // https://developers.google.com/open-source/licenses/bsd -#include "upb_generator/keywords.h" +#include "google/protobuf/compiler/hpb/keywords.h" +#include #include -#include -namespace upb { -namespace generator { +#include "absl/container/flat_hash_set.h" +#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", "alignas", @@ -110,22 +113,22 @@ static const char* const kKeywordList[] = { "requires", }; -static std::unordered_set* MakeKeywordsMap() { - auto* result = new std::unordered_set(); +static absl::flat_hash_set* MakeKeywordsMap() { + auto* result = new absl::flat_hash_set(); for (const auto keyword : kKeywordList) { result->emplace(keyword); } return result; } -static std::unordered_set& kKeywords = *MakeKeywordsMap(); +static absl::flat_hash_set& kKeywords = *MakeKeywordsMap(); -std::string ResolveKeywordConflict(const std::string& name) { +std::string ResolveKeywordConflict(absl::string_view name) { if (kKeywords.count(name) > 0) { - return name + "_"; + return absl::StrCat(name, "_"); } - return name; + return std::string(name); } -} // namespace generator -} // namespace upb +} // namespace protobuf +} // namespace google::hpb_generator diff --git a/upb_generator/keywords.h b/hpb_generator/keywords.h similarity index 50% rename from upb_generator/keywords.h rename to hpb_generator/keywords.h index d4b6ed34bb..70975d1e33 100644 --- a/upb_generator/keywords.h +++ b/hpb_generator/keywords.h @@ -5,18 +5,19 @@ // license that can be found in the LICENSE file or at // https://developers.google.com/open-source/licenses/bsd -#ifndef UPB_PROTOS_GENERATOR_KEYWORDS_H -#define UPB_PROTOS_GENERATOR_KEYWORDS_H +#ifndef PROTOBUF_COMPILER_HBP_GENERATOR_KEYWORDS_H +#define PROTOBUF_COMPILER_HBP_GENERATOR_KEYWORDS_H #include -namespace upb { -namespace generator { +#include "absl/strings/string_view.h" + +namespace google::protobuf::hpb_generator { // 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 upb +} // namespace protobuf +} // namespace google::hpb_generator -#endif // UPB_PROTOS_GENERATOR_KEYWORDS_H +#endif // PROTOBUF_COMPILER_HBP_GENERATOR_KEYWORDS_H diff --git a/hpb_generator/names.cc b/hpb_generator/names.cc index a081c59757..166f4af7c2 100644 --- a/hpb_generator/names.cc +++ b/hpb_generator/names.cc @@ -11,7 +11,7 @@ #include "absl/strings/string_view.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 protobuf = ::proto2; @@ -92,7 +92,7 @@ std::string ClassName(const protobuf::Descriptor* descriptor) { } if (parent) res += ClassName(parent) + "_"; absl::StrAppend(&res, descriptor->name()); - return ::upb::generator::ResolveKeywordConflict(res); + return ResolveKeywordConflict(res); } std::string QualifiedClassName(const protobuf::Descriptor* descriptor) { diff --git a/upb_generator/BUILD b/upb_generator/BUILD index 70160e2aac..80888ab1e6 100644 --- a/upb_generator/BUILD +++ b/upb_generator/BUILD @@ -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( name = "plugin", hdrs = [