Are there some better ways to address warnings when compiling protocol buffer generated source file?

后端 未结 3 994
长发绾君心
长发绾君心 2021-01-11 09:52

For a simple proto file:

message Person {
  required int32 id = 1;
  required string name = 2;
  optional string email = 3;
}

It\'s been compiled by pr

3条回答
  •  广开言路
    2021-01-11 10:18

    You can hack the source of the protoc compiler to have it inject the pragmas into the generated files automatically.

    In src/google/protobuf/compiler/cpp/cpp_file.cc in GenerateHeader(io::Printer* printer) around line 94, change the first printer->Print call to:

      // Generate top of header.
      printer->Print(
        "// Generated by the protocol buffer compiler.  DO NOT EDIT!\n"
        "// source: $filename$\n"
        "\n"
        "#ifndef PROTOBUF_$filename_identifier$__INCLUDED\n"
        "#define PROTOBUF_$filename_identifier$__INCLUDED\n"
        "\n"
        "#ifdef _MSC_VER\n"
        "#  pragma warning(push)\n"
        "#  pragma warning(disable: 4127 4244 4267)\n"
        "#endif\n"
        "\n"
        "#include \n"
        "\n",
        "filename", file_->name(),
        "filename_identifier", filename_identifier);
    

    Then at the end of the same function at around line 294, change the last printer->Print call to:

      printer->Print(
        "#ifdef _MSC_VER\n"
        "#  pragma warning(pop)\n"
        "#endif\n"
        "\n"
        "#endif  // PROTOBUF_$filename_identifier$__INCLUDED\n",
        "filename_identifier", filename_identifier);
    

    Now you just need to compile the protoc target and run the new protoc.exe to have the pragmas in the generated headers.

提交回复
热议问题