protoc-c/c_message.cc: Force int size on oneof enums

Fixes #220. Patch by Dave Benson from the mailing list thread.
This commit is contained in:
Ilya Lipnitskiy 2016-06-16 23:59:23 -07:00
parent 651ec8f512
commit bb6553c397

View File

@ -148,6 +148,7 @@ GenerateStructDefinition(io::Printer* printer) {
} }
// Generate the case enums for unions // Generate the case enums for unions
vars["opt_comma"] = ",";
for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
const OneofDescriptor *oneof = descriptor_->oneof_decl(i); const OneofDescriptor *oneof = descriptor_->oneof_decl(i);
vars["oneofname"] = FullNameToUpper(oneof->name()); vars["oneofname"] = FullNameToUpper(oneof->name());
@ -160,8 +161,13 @@ GenerateStructDefinition(io::Printer* printer) {
const FieldDescriptor *field = oneof->field(j); const FieldDescriptor *field = oneof->field(j);
vars["fieldname"] = FullNameToUpper(field->name()); vars["fieldname"] = FullNameToUpper(field->name());
vars["fieldnum"] = SimpleItoa(field->number()); vars["fieldnum"] = SimpleItoa(field->number());
printer->Print(vars, "$ucclassname$__$oneofname$_$fieldname$ = $fieldnum$,\n"); bool isLast = j == oneof->field_count() - 1;
if (isLast) {
vars["opt_comma"] = "";
}
printer->Print(vars, "$ucclassname$__$oneofname$_$fieldname$ = $fieldnum$$opt_comma$\n");
} }
printer->Print(vars, " PROTOBUF_C__FORCE_ENUM_TO_BE_INT_SIZE($ucclassname$)\n");
printer->Outdent(); printer->Outdent();
printer->Print(vars, "} $foneofname$Case;\n\n"); printer->Print(vars, "} $foneofname$Case;\n\n");
} }