Breaking change: Remove C++ legacy syntax descriptor APIs

PiperOrigin-RevId: 589879506
pull/15020/head
Mike Kruskal 1 year ago committed by Copybara-Service
parent 1dd6a7d06e
commit cf2d6965dc
  1. 1
      python/build_targets.bzl
  2. 16
      src/google/protobuf/BUILD.bazel
  3. 25
      src/google/protobuf/descriptor.cc
  4. 50
      src/google/protobuf/descriptor.h
  5. 97
      src/google/protobuf/descriptor_legacy.h

@ -121,7 +121,6 @@ def build_targets(name):
deps = [
":proto_api",
"//:protobuf",
"//src/google/protobuf:descriptor_legacy",
] + select({
"//conditions:default": [],
":use_fast_cpp_protos": ["//external:python_headers"],

@ -580,20 +580,6 @@ cc_library(
strip_include_prefix = "/src",
)
cc_library(
name = "descriptor_legacy",
hdrs = ["descriptor_legacy.h"],
copts = COPTS,
linkopts = LINK_OPTS,
strip_include_prefix = "/src",
visibility = ["//:__subpackages__"],
deps = [
":port_def",
":protobuf_nowkt",
"@com_google_absl//absl/strings",
],
)
cc_library(
name = "descriptor_visitor",
hdrs = ["descriptor_visitor.h"],
@ -1043,7 +1029,6 @@ cc_test(
}),
deps = [
":cc_test_protos",
":descriptor_legacy",
":protobuf",
":test_textproto",
"//src/google/protobuf/compiler:importer",
@ -1339,7 +1324,6 @@ cc_test(
}),
deps = [
":cc_test_protos",
":descriptor_legacy",
":protobuf",
":test_util",
"//src/google/protobuf/stubs",

@ -810,21 +810,6 @@ const char* const FieldDescriptor::kLabelToName[MAX_LABEL + 1] = {
"repeated", // LABEL_REPEATED
};
const char* FileDescriptor::SyntaxName(FileDescriptor::Syntax syntax) {
switch (syntax) {
case SYNTAX_PROTO2:
return "proto2";
case SYNTAX_PROTO3:
return "proto3";
case SYNTAX_EDITIONS:
return "editions";
case SYNTAX_UNKNOWN:
return "unknown";
}
ABSL_LOG(FATAL) << "can't reach here.";
return nullptr;
}
static const char* const kNonLinkedWeakMessageReplacementName = "google.protobuf.Empty";
#if !defined(_MSC_VER) || (_MSC_VER >= 1900 && _MSC_VER < 1912)
@ -3802,6 +3787,11 @@ bool FieldDescriptor::legacy_enum_field_treated_as_closed() const {
enum_type()->is_closed());
}
bool FieldDescriptor::has_optional_keyword() const {
return proto3_optional_ || (file()->edition() == Edition::EDITION_PROTO2 &&
is_optional() && !containing_oneof());
}
// Location methods ===============================================
bool FileDescriptor::GetSourceLocation(const std::vector<int>& path,
@ -5070,7 +5060,6 @@ FileDescriptor* DescriptorPool::NewPlaceholderFileWithMutexHeld(
placeholder->tables_ = &FileDescriptorTables::GetEmptyInstance();
placeholder->source_code_info_ = &SourceCodeInfo::default_instance();
placeholder->is_placeholder_ = true;
placeholder->syntax_ = FileDescriptor::SYNTAX_UNKNOWN;
placeholder->finished_building_ = true;
// All other fields are zero or nullptr.
@ -5682,16 +5671,12 @@ FileDescriptor* DescriptorBuilder::BuildFileImpl(
// TODO: Report error when the syntax is empty after all the protos
// have added the syntax statement.
if (proto.syntax().empty() || proto.syntax() == "proto2") {
file_->syntax_ = FileDescriptor::SYNTAX_PROTO2;
file_->edition_ = Edition::EDITION_PROTO2;
} else if (proto.syntax() == "proto3") {
file_->syntax_ = FileDescriptor::SYNTAX_PROTO3;
file_->edition_ = Edition::EDITION_PROTO3;
} else if (proto.syntax() == "editions") {
file_->syntax_ = FileDescriptor::SYNTAX_EDITIONS;
file_->edition_ = proto.edition();
} else {
file_->syntax_ = FileDescriptor::SYNTAX_UNKNOWN;
file_->edition_ = Edition::EDITION_UNKNOWN;
AddError(proto.name(), proto, DescriptorPool::ErrorCollector::OTHER, [&] {
return absl::StrCat("Unrecognized syntax: ", proto.syntax());

@ -1856,35 +1856,9 @@ class PROTOBUF_EXPORT FileDescriptor : private internal::SymbolBase {
// descriptor.proto, and any available extensions of that message.
const FileOptions& options() const;
private:
// With the upcoming release of editions, syntax should not be used for
// business logic. Instead, the various feature helpers defined in this file
// should be used to query more targeted behaviors. For example:
// has_presence, is_closed, requires_utf8_validation.
enum Syntax
#ifndef SWIG
: int
#endif // !SWIG
{
SYNTAX_UNKNOWN = 0,
SYNTAX_PROTO2 = 2,
SYNTAX_PROTO3 = 3,
SYNTAX_EDITIONS = 99,
};
PROTOBUF_IGNORE_DEPRECATION_START
Syntax syntax() const;
PROTOBUF_IGNORE_DEPRECATION_STOP
// Define a visibility-restricted wrapper for internal use until the migration
// is complete.
friend class FileDescriptorLegacy;
PROTOBUF_IGNORE_DEPRECATION_START
static const char* SyntaxName(Syntax syntax);
PROTOBUF_IGNORE_DEPRECATION_STOP
public:
// Returns EDITION_UNKNOWN if syntax() is not SYNTAX_EDITIONS.
// Returns edition of this file. For legacy proto2/proto3 files, special
// EDITION_PROTO2 and EDITION_PROTO3 values are used.
Edition edition() const;
// Find a top-level message type by name (not full_name). Returns nullptr if
@ -1920,7 +1894,7 @@ class PROTOBUF_EXPORT FileDescriptor : private internal::SymbolBase {
// Fill the json_name field of FieldDescriptorProto for all fields. Can only
// be called after CopyTo().
void CopyJsonNameTo(FileDescriptorProto* proto) const;
// Fills in the file-level settings of this file (e.g. syntax, package,
// Fills in the file-level settings of this file (e.g. edition, package,
// file options) to `proto`.
void CopyHeadingTo(FileDescriptorProto* proto) const;
@ -1962,8 +1936,6 @@ class PROTOBUF_EXPORT FileDescriptor : private internal::SymbolBase {
// that type accessor functions that can possibly build a dependent file
// aren't called during the process of building the file.
bool finished_building_;
// Actually a `Syntax` but stored as uint8_t to save space.
uint8_t syntax_;
// This one is here to fill the padding.
int extension_count_;
@ -2725,19 +2697,9 @@ inline bool FieldDescriptor::is_map() const {
return type() == TYPE_MESSAGE && is_map_message_type();
}
inline bool FieldDescriptor::has_optional_keyword() const {
PROTOBUF_IGNORE_DEPRECATION_START
return proto3_optional_ ||
(file()->syntax() == FileDescriptor::SYNTAX_PROTO2 && is_optional() &&
!containing_oneof());
PROTOBUF_IGNORE_DEPRECATION_STOP
}
inline const OneofDescriptor* FieldDescriptor::real_containing_oneof() const {
PROTOBUF_IGNORE_DEPRECATION_START
auto* oneof = containing_oneof();
return oneof && !oneof->is_synthetic() ? oneof : nullptr;
PROTOBUF_IGNORE_DEPRECATION_STOP
}
// To save space, index() is computed by looking at the descriptor's position
@ -2844,12 +2806,6 @@ inline const FileDescriptor* FileDescriptor::weak_dependency(int index) const {
return dependency(weak_dependencies_[index]);
}
PROTOBUF_IGNORE_DEPRECATION_START
inline FileDescriptor::Syntax FileDescriptor::syntax() const {
return static_cast<Syntax>(syntax_);
}
PROTOBUF_IGNORE_DEPRECATION_STOP
namespace internal {
// FieldRange(desc) provides an iterable range for the fields of a

@ -1,97 +0,0 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
// Author: kenton@google.com (Kenton Varda)
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
//
// This file contains classes which describe a type of protocol message.
// You can use a message's descriptor to learn at runtime what fields
// it contains and what the types of those fields are. The Message
// interface also allows you to dynamically access and modify individual
// fields by passing the FieldDescriptor of the field you are interested
// in.
//
// Most users will not care about descriptors, because they will write
// code specific to certain protocol types and will simply use the classes
// generated by the protocol compiler directly. Advanced users who want
// to operate on arbitrary types (not known at compile time) may want to
// read descriptors in order to learn about the contents of a message.
// A very small number of users will want to construct their own
// Descriptors, either because they are implementing Message manually or
// because they are writing something like the protocol compiler.
//
// For an example of how you might use descriptors, see the code example
// at the top of message.h.
#ifndef GOOGLE_PROTOBUF_DESCRIPTOR_LEGACY_H__
#define GOOGLE_PROTOBUF_DESCRIPTOR_LEGACY_H__
#include "absl/strings/string_view.h"
#include "google/protobuf/descriptor.h"
// Must be included last.
#include "google/protobuf/port_def.inc"
// This file is meant to be a temporary housing for legacy descriptor APIs we
// want to deprecate and remove. This will help prevent backslide by allowing
// us to control visibility.
namespace google {
namespace protobuf {
PROTOBUF_IGNORE_DEPRECATION_START
// Wraps FileDescriptor.
class FileDescriptorLegacy {
public:
explicit FileDescriptorLegacy(const FileDescriptor* desc) : desc_(desc) {}
// Any dependencies on file-level syntax keyword should be replaced by
// feature-level switches to support go/protobuf-editions.
enum Syntax {
SYNTAX_UNKNOWN = FileDescriptor::SYNTAX_UNKNOWN,
SYNTAX_PROTO2 = FileDescriptor::SYNTAX_PROTO2,
SYNTAX_PROTO3 = FileDescriptor::SYNTAX_PROTO3,
SYNTAX_EDITIONS = FileDescriptor::SYNTAX_EDITIONS,
};
Syntax syntax() const { return static_cast<Syntax>(desc_->syntax()); }
static absl::string_view SyntaxName(Syntax syntax) {
return FileDescriptor::SyntaxName(
static_cast<FileDescriptor::Syntax>(syntax));
}
private:
const FileDescriptor* desc_;
};
class FieldDescriptorLegacy {
public:
explicit FieldDescriptorLegacy(const FieldDescriptor* desc) : desc_(desc) {}
bool has_optional_keyword() const { return desc_->has_optional_keyword(); }
private:
const FieldDescriptor* desc_;
};
class OneofDescriptorLegacy {
public:
explicit OneofDescriptorLegacy(const OneofDescriptor* desc) : desc_(desc) {}
bool is_synthetic() const { return desc_->is_synthetic(); }
private:
const OneofDescriptor* desc_;
};
PROTOBUF_IGNORE_DEPRECATION_STOP
} // namespace protobuf
} // namespace google
#include "google/protobuf/port_undef.inc"
#endif // GOOGLE_PROTOBUF_DESCRIPTOR_LEGACY_H__
Loading…
Cancel
Save