Add a flag in the C++ generator for stripping non-functional bits.

The resulting code won't have functional reflection, but allows us to compare the functionality between two sets of gencode.

PiperOrigin-RevId: 537170849
pull/12961/head
Mike Kruskal 2 years ago committed by Copybara-Service
parent 6f24fceb36
commit 46ea61ae94
  1. 8
      src/google/protobuf/compiler/cpp/file.cc
  2. 2
      src/google/protobuf/compiler/cpp/generator.cc
  3. 18
      src/google/protobuf/compiler/cpp/helpers.h
  4. 12
      src/google/protobuf/compiler/cpp/message.cc
  5. 1
      src/google/protobuf/compiler/cpp/options.h
  6. 6
      src/google/protobuf/compiler/cpp/parse_function_generator.cc

@ -1003,6 +1003,11 @@ void FileGenerator::GenerateReflectionInitializationCode(io::Printer* p) {
{{"desc_name", desc_name},
{"encoded_file_proto",
[&] {
if (options_.strip_nonfunctional_codegen) {
p->Emit(R"cc("")cc");
return;
}
absl::string_view data = file_data;
if (data.size() <= 65535) {
static constexpr size_t kBytesPerLine = 40;
@ -1084,7 +1089,8 @@ void FileGenerator::GenerateReflectionInitializationCode(io::Printer* p) {
p->Emit(
{
{"eager", eager ? "true" : "false"},
{"file_proto_len", file_data.size()},
{"file_proto_len",
options_.strip_nonfunctional_codegen ? 0 : file_data.size()},
{"proto_name", desc_name},
{"deps_ptr", num_deps == 0
? "nullptr"

@ -198,6 +198,8 @@ bool CppGenerator::Generate(const FileDescriptor* file,
"Unknown value for experimental_tail_call_table_mode: ", value);
return false;
}
} else if (key == "experimental_strip_nonfunctional_codegen") {
file_options.strip_nonfunctional_codegen = true;
} else {
*error = absl::StrCat("Unknown generator option: ", key);
return false;

@ -915,16 +915,19 @@ class PROTOC_EXPORT Formatter {
};
template <typename T>
std::string FieldComment(const T* field) {
std::string FieldComment(const T* field, const Options& options) {
if (options.strip_nonfunctional_codegen) {
return field->name();
}
// Print the field's (or oneof's) proto-syntax definition as a comment.
// We don't want to print group bodies so we cut off after the first
// line.
DebugStringOptions options;
options.elide_group_body = true;
options.elide_oneof_body = true;
DebugStringOptions debug_options;
debug_options.elide_group_body = true;
debug_options.elide_oneof_body = true;
for (absl::string_view chunk :
absl::StrSplit(field->DebugStringWithOptions(options), '\n')) {
absl::StrSplit(field->DebugStringWithOptions(debug_options), '\n')) {
return std::string(chunk);
}
@ -932,8 +935,9 @@ std::string FieldComment(const T* field) {
}
template <class T>
void PrintFieldComment(const Formatter& format, const T* field) {
format("// $1$\n", FieldComment(field));
void PrintFieldComment(const Formatter& format, const T* field,
const Options& options) {
format("// $1$\n", FieldComment(field, options));
}
class PROTOC_EXPORT NamespaceOpener {

@ -699,7 +699,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) {
auto v = p->WithVars(FieldVars(field, options_));
auto t = p->WithVars(MakeTrackerCalls(field, options_));
p->Emit(
{{"field_comment", FieldComment(field)},
{{"field_comment", FieldComment(field, options_)},
Sub("const_impl", "const;").WithSuffix(";"),
Sub("impl", ";").WithSuffix(";"),
{"sizer",
@ -1134,7 +1134,7 @@ void MessageGenerator::GenerateFieldAccessorDefinitions(io::Printer* p) {
p->Emit("// $classname$\n\n");
for (auto field : FieldRange(descriptor_)) {
PrintFieldComment(Formatter{p}, field);
PrintFieldComment(Formatter{p}, field, options_);
auto v = p->WithVars(FieldVars(field, options_));
auto t = p->WithVars(MakeTrackerCalls(field, options_));
@ -3688,7 +3688,7 @@ void MessageGenerator::GenerateSerializeOneField(io::Printer* p,
return;
}
PrintFieldComment(Formatter{p}, field);
PrintFieldComment(Formatter{p}, field, options_);
if (HasHasbit(field)) {
p->Emit(
{
@ -3959,7 +3959,7 @@ void MessageGenerator::GenerateSerializeWithCachedSizesBody(io::Printer* p) {
re.Flush();
if (field->options().weak()) {
largest_weak_field.ReplaceIfLarger(field);
PrintFieldComment(Formatter{p}, field);
PrintFieldComment(Formatter{p}, field, options_);
} else {
e.EmitIfNotNull(largest_weak_field.Release());
e.Emit(field);
@ -4204,7 +4204,7 @@ void MessageGenerator::GenerateByteSize(io::Printer* p) {
const FieldDescriptor* field = chunk[j];
bool have_enclosing_if = false;
PrintFieldComment(format, field);
PrintFieldComment(format, field, options_);
if (field->is_repeated()) {
// No presence check is required.
@ -4246,7 +4246,7 @@ void MessageGenerator::GenerateByteSize(io::Printer* p) {
format("switch ($1$_case()) {\n", oneof->name());
format.Indent();
for (auto field : FieldRange(oneof)) {
PrintFieldComment(format, field);
PrintFieldComment(format, field, options_);
format("case k$1$: {\n", UnderscoresToCamelCase(field->name(), true));
format.Indent();
field_generators_.get(field).GenerateByteSize(p);

@ -88,6 +88,7 @@ struct Options {
bool force_eagerly_verified_lazy = false;
bool force_inline_string = false;
#endif // !PROTOBUF_STABLE_EXPERIMENTS
bool strip_nonfunctional_codegen = false;
};
} // namespace cpp

@ -613,7 +613,7 @@ void ParseFunctionGenerator::GenerateTailCallTable(Formatter& format) {
void ParseFunctionGenerator::GenerateFastFieldEntries(Formatter& format) {
for (const auto& info : tc_table_info_->fast_path_fields) {
if (info.field != nullptr) {
PrintFieldComment(format, info.field);
PrintFieldComment(format, info.field, options_);
}
if (info.func_name.empty()) {
format("{::_pbi::TcParser::MiniParse, {}},\n");
@ -794,7 +794,7 @@ static void FormatFieldKind(Formatter& format,
void ParseFunctionGenerator::GenerateFieldEntries(Formatter& format) {
for (const auto& entry : tc_table_info_->field_entries) {
const FieldDescriptor* field = entry.field;
PrintFieldComment(format, field);
PrintFieldComment(format, field, options_);
format("{");
if (IsWeak(field, options_)) {
// Weak fields are handled by the generated fallback function.
@ -1317,7 +1317,7 @@ void ParseFunctionGenerator::GenerateFieldSwitch(
for (const auto* field : fields) {
bool cold = ShouldSplit(field, options_);
format.Set("field", FieldMemberName(field, cold));
PrintFieldComment(format, field);
PrintFieldComment(format, field, options_);
format("case $1$:\n", field->number());
format.Indent();
uint32_t fallback_tag = 0;

Loading…
Cancel
Save