From 2880fef06cb7443ba24dc1264ba9f02115407f2c Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Thu, 10 Nov 2022 13:51:58 -0800 Subject: [PATCH] Begin emitting semantic metadata for some C++ proto features. This is a longstanding feature request from users. By marking generated code with semantics, we will be able to filter read calls from write calls. PiperOrigin-RevId: 487630592 --- .../protobuf/compiler/cpp/enum_field.cc | 7 ++- src/google/protobuf/compiler/cpp/helpers.h | 52 ++++++++++++++++--- .../protobuf/compiler/cpp/primitive_field.cc | 7 ++- 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/src/google/protobuf/compiler/cpp/enum_field.cc b/src/google/protobuf/compiler/cpp/enum_field.cc index d5181cd9a3..cab0372a4b 100644 --- a/src/google/protobuf/compiler/cpp/enum_field.cc +++ b/src/google/protobuf/compiler/cpp/enum_field.cc @@ -35,10 +35,12 @@ #include "google/protobuf/compiler/cpp/enum_field.h" #include +#include #include "absl/container/flat_hash_map.h" #include "google/protobuf/compiler/cpp/field.h" #include "google/protobuf/compiler/cpp/helpers.h" +#include "google/protobuf/descriptor.pb.h" #include "google/protobuf/wire_format.h" namespace google { @@ -83,9 +85,10 @@ void EnumFieldGenerator::GeneratePrivateMembers(io::Printer* printer) const { void EnumFieldGenerator::GenerateAccessorDeclarations( io::Printer* printer) const { Formatter format(printer, variables_); + format("$deprecated_attr$$type$ ${1$$name$$}$() const;\n", descriptor_); + format("$deprecated_attr$void ${1$set_$name$$}$($type$ value);\n", + std::make_tuple(descriptor_, GeneratedCodeInfo::Annotation::SET)); format( - "$deprecated_attr$$type$ ${1$$name$$}$() const;\n" - "$deprecated_attr$void ${1$set_$name$$}$($type$ value);\n" "private:\n" "$type$ ${1$_internal_$name$$}$() const;\n" "void ${1$_internal_set_$name$$}$($type$ value);\n" diff --git a/src/google/protobuf/compiler/cpp/helpers.h b/src/google/protobuf/compiler/cpp/helpers.h index 1afe4b1728..42c546459c 100644 --- a/src/google/protobuf/compiler/cpp/helpers.h +++ b/src/google/protobuf/compiler/cpp/helpers.h @@ -39,6 +39,7 @@ #include #include #include +#include #include "google/protobuf/compiler/scc.h" #include "google/protobuf/compiler/code_generator.h" @@ -862,16 +863,54 @@ class PROTOC_EXPORT Formatter { return absl::StrCat(x); } static std::string ToString(absl::Hex x) { return absl::StrCat(x); } - static std::string ToString(const FieldDescriptor* d) { return Payload(d); } - static std::string ToString(const Descriptor* d) { return Payload(d); } - static std::string ToString(const EnumDescriptor* d) { return Payload(d); } + static std::string ToString(const FieldDescriptor* d) { + return Payload(d, GeneratedCodeInfo::Annotation::NONE); + } + static std::string ToString(const Descriptor* d) { + return Payload(d, GeneratedCodeInfo::Annotation::NONE); + } + static std::string ToString(const EnumDescriptor* d) { + return Payload(d, GeneratedCodeInfo::Annotation::NONE); + } static std::string ToString(const EnumValueDescriptor* d) { - return Payload(d); + return Payload(d, GeneratedCodeInfo::Annotation::NONE); + } + static std::string ToString(const OneofDescriptor* d) { + return Payload(d, GeneratedCodeInfo::Annotation::NONE); + } + + static std::string ToString( + std::tuple + p) { + return Payload(std::get<0>(p), std::get<1>(p)); + } + static std::string ToString( + std::tuple + p) { + return Payload(std::get<0>(p), std::get<1>(p)); + } + static std::string ToString( + std::tuple + p) { + return Payload(std::get<0>(p), std::get<1>(p)); + } + static std::string ToString( + std::tuple + p) { + return Payload(std::get<0>(p), std::get<1>(p)); + } + static std::string ToString( + std::tuple + p) { + return Payload(std::get<0>(p), std::get<1>(p)); } - static std::string ToString(const OneofDescriptor* d) { return Payload(d); } template - static std::string Payload(const Descriptor* descriptor) { + static std::string Payload(const Descriptor* descriptor, + GeneratedCodeInfo::Annotation::Semantic semantic) { std::vector path; descriptor->GetLocationPath(&path); GeneratedCodeInfo::Annotation annotation; @@ -879,6 +918,7 @@ class PROTOC_EXPORT Formatter { annotation.add_path(index); } annotation.set_source_file(descriptor->file()->name()); + annotation.set_semantic(semantic); return annotation.SerializeAsString(); } }; diff --git a/src/google/protobuf/compiler/cpp/primitive_field.cc b/src/google/protobuf/compiler/cpp/primitive_field.cc index 52ce221314..216c51daf2 100644 --- a/src/google/protobuf/compiler/cpp/primitive_field.cc +++ b/src/google/protobuf/compiler/cpp/primitive_field.cc @@ -35,11 +35,13 @@ #include "google/protobuf/compiler/cpp/primitive_field.h" #include +#include #include "google/protobuf/io/printer.h" #include "absl/container/flat_hash_map.h" #include "absl/strings/str_cat.h" #include "google/protobuf/compiler/cpp/helpers.h" +#include "google/protobuf/descriptor.pb.h" #include "google/protobuf/wire_format.h" namespace google { @@ -143,9 +145,10 @@ void PrimitiveFieldGenerator::GeneratePrivateMembers( void PrimitiveFieldGenerator::GenerateAccessorDeclarations( io::Printer* printer) const { Formatter format(printer, variables_); + format("$deprecated_attr$$type$ ${1$$name$$}$() const;\n", descriptor_); + format("$deprecated_attr$void ${1$set_$name$$}$($type$ value);\n", + std::make_tuple(descriptor_, GeneratedCodeInfo::Annotation::SET)); format( - "$deprecated_attr$$type$ ${1$$name$$}$() const;\n" - "$deprecated_attr$void ${1$set_$name$$}$($type$ value);\n" "private:\n" "$type$ ${1$_internal_$name$$}$() const;\n" "void ${1$_internal_set_$name$$}$($type$ value);\n"