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
pull/10951/head
Protobuf Team Bot 2 years ago committed by Copybara-Service
parent ce7a02c94c
commit 2880fef06c
  1. 7
      src/google/protobuf/compiler/cpp/enum_field.cc
  2. 52
      src/google/protobuf/compiler/cpp/helpers.h
  3. 7
      src/google/protobuf/compiler/cpp/primitive_field.cc

@ -35,10 +35,12 @@
#include "google/protobuf/compiler/cpp/enum_field.h"
#include <string>
#include <tuple>
#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"

@ -39,6 +39,7 @@
#include <cstdint>
#include <iterator>
#include <string>
#include <tuple>
#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<const FieldDescriptor*,
GeneratedCodeInfo::Annotation::Semantic>
p) {
return Payload(std::get<0>(p), std::get<1>(p));
}
static std::string ToString(
std::tuple<const Descriptor*, GeneratedCodeInfo::Annotation::Semantic>
p) {
return Payload(std::get<0>(p), std::get<1>(p));
}
static std::string ToString(
std::tuple<const EnumDescriptor*, GeneratedCodeInfo::Annotation::Semantic>
p) {
return Payload(std::get<0>(p), std::get<1>(p));
}
static std::string ToString(
std::tuple<const EnumValueDescriptor*,
GeneratedCodeInfo::Annotation::Semantic>
p) {
return Payload(std::get<0>(p), std::get<1>(p));
}
static std::string ToString(
std::tuple<const OneofDescriptor*,
GeneratedCodeInfo::Annotation::Semantic>
p) {
return Payload(std::get<0>(p), std::get<1>(p));
}
static std::string ToString(const OneofDescriptor* d) { return Payload(d); }
template <typename Descriptor>
static std::string Payload(const Descriptor* descriptor) {
static std::string Payload(const Descriptor* descriptor,
GeneratedCodeInfo::Annotation::Semantic semantic) {
std::vector<int> 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();
}
};

@ -35,11 +35,13 @@
#include "google/protobuf/compiler/cpp/primitive_field.h"
#include <string>
#include <tuple>
#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"

Loading…
Cancel
Save