From cf35baa1ad70f0dca734f93bcc2b54d8d059bcdd Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 27 Mar 2019 15:32:05 -0700 Subject: Moved macros from upb.h to port_def.inc to avoid leaking them to users. (#160) * Use port_def.inc to prevent macros from leaking to users. * Added helpful comments to port_def.inc/port_undef.inc. --- upbc/generator.cc | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'upbc') diff --git a/upbc/generator.cc b/upbc/generator.cc index 3b581f4..2760069 100644 --- a/upbc/generator.cc +++ b/upbc/generator.cc @@ -352,12 +352,12 @@ void GenerateMessageInHeader(const protobuf::Descriptor* message, Output& output const protobuf::OneofDescriptor* oneof = message->oneof_decl(i); std::string fullname = ToCIdent(oneof->full_name()); output("typedef enum {\n"); - for (int i = 0; i < oneof->field_count(); i++) { - const protobuf::FieldDescriptor* field = oneof->field(i); + for (int j = 0; j < oneof->field_count(); j++) { + const protobuf::FieldDescriptor* field = oneof->field(j); output(" $0_$1 = $2,\n", fullname, field->name(), field->number()); } output( - " $0_NOT_SET = 0,\n" + " $0_NOT_SET = 0\n" "} $0_oneofcases;\n", fullname); output( @@ -689,8 +689,20 @@ void GenerateMessageDefAccessor(const protobuf::Descriptor* d, Output& output) { void WriteDefHeader(const protobuf::FileDescriptor* file, Output& output) { EmitFileWarning(file, output); + output( + "#ifndef $0_UPBDEFS_H_\n" + "#define $0_UPBDEFS_H_\n\n" + "#include \"upb/def.h\"\n" + "#include \"upb/port_def.inc\"\n" + "#ifdef __cplusplus\n" + "extern \"C\" {\n" + "#endif\n\n", + ToPreproc(file->name())); + output("#include \"upb/def.h\"\n"); output("\n"); + output("#include \"upb/port_def.inc\"\n"); + output("\n"); output("extern upb_def_init $0;\n", DefInitSymbol(file)); output("\n"); @@ -698,6 +710,16 @@ void WriteDefHeader(const protobuf::FileDescriptor* file, Output& output) { for (int i = 0; i < file->message_type_count(); i++) { GenerateMessageDefAccessor(file->message_type(i), output); } + + output( + "#ifdef __cplusplus\n" + "} /* extern \"C\" */\n" + "#endif\n" + "\n" + "#include \"upb/port_undef.inc\"\n" + "\n" + "#endif /* $0_UPBDEFS_H_ */\n", + ToPreproc(file->name())); } // Escape C++ trigraphs by escaping question marks to \? -- cgit v1.2.3