| 1354 | }; |
| 1355 | |
| 1356 | bool McpackToProtobuf::Generate(const google::protobuf::FileDescriptor* file, |
| 1357 | const std::string& /*parameter*/, |
| 1358 | google::protobuf::compiler::GeneratorContext* ctx, |
| 1359 | std::string* error) const { |
| 1360 | if (!file->options().GetExtension(idl_support)) { |
| 1361 | // skip the file. |
| 1362 | return true; |
| 1363 | } |
| 1364 | |
| 1365 | std::string cpp_name = butil::EnsureString(file->name()); |
| 1366 | const size_t pos = cpp_name.find_last_of('.'); |
| 1367 | if (pos == std::string::npos) { |
| 1368 | ::butil::string_printf(error, "Bad filename=%s", cpp_name.c_str()); |
| 1369 | return false; |
| 1370 | } |
| 1371 | cpp_name.resize(pos); |
| 1372 | cpp_name.append(".pb.cc"); |
| 1373 | |
| 1374 | google::protobuf::io::Printer inc_printer( |
| 1375 | ctx->OpenForInsert(cpp_name, "includes"), '$'); |
| 1376 | inc_printer.Print("#include <butil/logging.h>\n" |
| 1377 | "#include <mcpack2pb/mcpack2pb.h>\n" |
| 1378 | "#include <gflags/gflags.h>\n"); |
| 1379 | |
| 1380 | google::protobuf::io::Printer gdecl_printer( |
| 1381 | ctx->OpenForInsert(cpp_name, "includes"), '$'); |
| 1382 | google::protobuf::io::Printer gimpl_printer( |
| 1383 | ctx->OpenForInsert(cpp_name, "global_scope"), '$'); |
| 1384 | |
| 1385 | gdecl_printer.Print( |
| 1386 | "\n// ==== declarations generated by brpc/mcpack2pb/protoc-gen-mcpack ====\n" |
| 1387 | "DECLARE_bool(mcpack2pb_absent_field_is_error);\n"); |
| 1388 | |
| 1389 | std::set<std::string> ref_msgs; |
| 1390 | std::set<std::string> ref_maps; |
| 1391 | for (int i = 0; i < file->message_type_count(); ++i) { |
| 1392 | const google::protobuf::Descriptor* d = file->message_type(i); |
| 1393 | if (!generate_parsing(d, ref_msgs, ref_maps, gimpl_printer)) { |
| 1394 | ::butil::string_printf( |
| 1395 | error, "Fail to generate parsing code for %s", |
| 1396 | butil::EnsureString(d->full_name()).c_str()); |
| 1397 | return false; |
| 1398 | } |
| 1399 | if (!generate_serializing(d, ref_msgs, ref_maps, gimpl_printer)) { |
| 1400 | ::butil::string_printf( |
| 1401 | error, "Fail to generate serializing code for %s", |
| 1402 | butil::EnsureString(d->full_name()).c_str()); |
| 1403 | return false; |
| 1404 | } |
| 1405 | std::string var_name = mcpack2pb::to_var_name(butil::EnsureString(d->full_name())); |
| 1406 | gdecl_printer.Print( |
| 1407 | "::mcpack2pb::FieldMap* g_$vmsg$_fields = NULL;\n" |
| 1408 | , "vmsg", var_name); |
| 1409 | } |
| 1410 | if (!generate_declarations(ref_msgs, ref_maps, gdecl_printer)) { |
| 1411 | ::butil::string_printf( |
| 1412 | error, "Fail to generate declarations for %s", |
| 1413 | cpp_name.c_str()); |
nothing calls this directly
no test coverage detected