Skip to content

Commit

Permalink
Begin emitting semantic metadata for some C++ proto features.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Nov 10, 2022
1 parent ce7a02c commit 2880fef
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/google/protobuf/compiler/cpp/enum_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
Expand Down
52 changes: 46 additions & 6 deletions src/google/protobuf/compiler/cpp/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -862,23 +863,62 @@ 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;
for (int index : path) {
annotation.add_path(index);
}
annotation.set_source_file(descriptor->file()->name());
annotation.set_semantic(semantic);
return annotation.SerializeAsString();
}
};
Expand Down
7 changes: 5 additions & 2 deletions src/google/protobuf/compiler/cpp/primitive_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 2880fef

Please sign in to comment.