Skip to content

Commit

Permalink
Add a flag in the C++ generator for stripping non-functional bits.
Browse files Browse the repository at this point in the history
The resulting code won't have functional reflection, but allows us to compare the functionality between two sets of gencode.

PiperOrigin-RevId: 537170849
  • Loading branch information
mkruskal-google authored and copybara-github committed Jun 2, 2023
1 parent 6f24fce commit 46ea61a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
8 changes: 7 additions & 1 deletion src/google/protobuf/compiler/cpp/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions src/google/protobuf/compiler/cpp/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 11 additions & 7 deletions src/google/protobuf/compiler/cpp/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -915,25 +915,29 @@ 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);
}

return "<unknown>";
}

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 {
Expand Down
12 changes: 6 additions & 6 deletions src/google/protobuf/compiler/cpp/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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_));
Expand Down Expand Up @@ -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(
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/google/protobuf/compiler/cpp/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/google/protobuf/compiler/cpp/parse_function_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 46ea61a

Please sign in to comment.