Skip to content

Commit 2880fef

Browse files
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
1 parent ce7a02c commit 2880fef

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

src/google/protobuf/compiler/cpp/enum_field.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@
3535
#include "google/protobuf/compiler/cpp/enum_field.h"
3636

3737
#include <string>
38+
#include <tuple>
3839

3940
#include "absl/container/flat_hash_map.h"
4041
#include "google/protobuf/compiler/cpp/field.h"
4142
#include "google/protobuf/compiler/cpp/helpers.h"
43+
#include "google/protobuf/descriptor.pb.h"
4244
#include "google/protobuf/wire_format.h"
4345

4446
namespace google {
@@ -83,9 +85,10 @@ void EnumFieldGenerator::GeneratePrivateMembers(io::Printer* printer) const {
8385
void EnumFieldGenerator::GenerateAccessorDeclarations(
8486
io::Printer* printer) const {
8587
Formatter format(printer, variables_);
88+
format("$deprecated_attr$$type$ ${1$$name$$}$() const;\n", descriptor_);
89+
format("$deprecated_attr$void ${1$set_$name$$}$($type$ value);\n",
90+
std::make_tuple(descriptor_, GeneratedCodeInfo::Annotation::SET));
8691
format(
87-
"$deprecated_attr$$type$ ${1$$name$$}$() const;\n"
88-
"$deprecated_attr$void ${1$set_$name$$}$($type$ value);\n"
8992
"private:\n"
9093
"$type$ ${1$_internal_$name$$}$() const;\n"
9194
"void ${1$_internal_set_$name$$}$($type$ value);\n"

src/google/protobuf/compiler/cpp/helpers.h

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <cstdint>
4040
#include <iterator>
4141
#include <string>
42+
#include <tuple>
4243

4344
#include "google/protobuf/compiler/scc.h"
4445
#include "google/protobuf/compiler/code_generator.h"
@@ -862,23 +863,62 @@ class PROTOC_EXPORT Formatter {
862863
return absl::StrCat(x);
863864
}
864865
static std::string ToString(absl::Hex x) { return absl::StrCat(x); }
865-
static std::string ToString(const FieldDescriptor* d) { return Payload(d); }
866-
static std::string ToString(const Descriptor* d) { return Payload(d); }
867-
static std::string ToString(const EnumDescriptor* d) { return Payload(d); }
866+
static std::string ToString(const FieldDescriptor* d) {
867+
return Payload(d, GeneratedCodeInfo::Annotation::NONE);
868+
}
869+
static std::string ToString(const Descriptor* d) {
870+
return Payload(d, GeneratedCodeInfo::Annotation::NONE);
871+
}
872+
static std::string ToString(const EnumDescriptor* d) {
873+
return Payload(d, GeneratedCodeInfo::Annotation::NONE);
874+
}
868875
static std::string ToString(const EnumValueDescriptor* d) {
869-
return Payload(d);
876+
return Payload(d, GeneratedCodeInfo::Annotation::NONE);
877+
}
878+
static std::string ToString(const OneofDescriptor* d) {
879+
return Payload(d, GeneratedCodeInfo::Annotation::NONE);
880+
}
881+
882+
static std::string ToString(
883+
std::tuple<const FieldDescriptor*,
884+
GeneratedCodeInfo::Annotation::Semantic>
885+
p) {
886+
return Payload(std::get<0>(p), std::get<1>(p));
887+
}
888+
static std::string ToString(
889+
std::tuple<const Descriptor*, GeneratedCodeInfo::Annotation::Semantic>
890+
p) {
891+
return Payload(std::get<0>(p), std::get<1>(p));
892+
}
893+
static std::string ToString(
894+
std::tuple<const EnumDescriptor*, GeneratedCodeInfo::Annotation::Semantic>
895+
p) {
896+
return Payload(std::get<0>(p), std::get<1>(p));
897+
}
898+
static std::string ToString(
899+
std::tuple<const EnumValueDescriptor*,
900+
GeneratedCodeInfo::Annotation::Semantic>
901+
p) {
902+
return Payload(std::get<0>(p), std::get<1>(p));
903+
}
904+
static std::string ToString(
905+
std::tuple<const OneofDescriptor*,
906+
GeneratedCodeInfo::Annotation::Semantic>
907+
p) {
908+
return Payload(std::get<0>(p), std::get<1>(p));
870909
}
871-
static std::string ToString(const OneofDescriptor* d) { return Payload(d); }
872910

873911
template <typename Descriptor>
874-
static std::string Payload(const Descriptor* descriptor) {
912+
static std::string Payload(const Descriptor* descriptor,
913+
GeneratedCodeInfo::Annotation::Semantic semantic) {
875914
std::vector<int> path;
876915
descriptor->GetLocationPath(&path);
877916
GeneratedCodeInfo::Annotation annotation;
878917
for (int index : path) {
879918
annotation.add_path(index);
880919
}
881920
annotation.set_source_file(descriptor->file()->name());
921+
annotation.set_semantic(semantic);
882922
return annotation.SerializeAsString();
883923
}
884924
};

src/google/protobuf/compiler/cpp/primitive_field.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@
3535
#include "google/protobuf/compiler/cpp/primitive_field.h"
3636

3737
#include <string>
38+
#include <tuple>
3839

3940
#include "google/protobuf/io/printer.h"
4041
#include "absl/container/flat_hash_map.h"
4142
#include "absl/strings/str_cat.h"
4243
#include "google/protobuf/compiler/cpp/helpers.h"
44+
#include "google/protobuf/descriptor.pb.h"
4345
#include "google/protobuf/wire_format.h"
4446

4547
namespace google {
@@ -143,9 +145,10 @@ void PrimitiveFieldGenerator::GeneratePrivateMembers(
143145
void PrimitiveFieldGenerator::GenerateAccessorDeclarations(
144146
io::Printer* printer) const {
145147
Formatter format(printer, variables_);
148+
format("$deprecated_attr$$type$ ${1$$name$$}$() const;\n", descriptor_);
149+
format("$deprecated_attr$void ${1$set_$name$$}$($type$ value);\n",
150+
std::make_tuple(descriptor_, GeneratedCodeInfo::Annotation::SET));
146151
format(
147-
"$deprecated_attr$$type$ ${1$$name$$}$() const;\n"
148-
"$deprecated_attr$void ${1$set_$name$$}$($type$ value);\n"
149152
"private:\n"
150153
"$type$ ${1$_internal_$name$$}$() const;\n"
151154
"void ${1$_internal_set_$name$$}$($type$ value);\n"

0 commit comments

Comments
 (0)