0002-Reimplement-FieldSyntax-to-maximize-compatibility-ac.patch 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. From ee3d9e5423c93ee6b828fdda8e7fef13a77634eb Mon Sep 17 00:00:00 2001
  2. From: Robert Edmonds <edmonds@users.noreply.github.com>
  3. Date: Wed, 20 Mar 2024 22:25:54 -0400
  4. Subject: [PATCH] Reimplement FieldSyntax() to maximize compatibility across
  5. protobuf versions
  6. Recent versions of Google protobuf have broken the interfaces for
  7. determining the syntax version of a .proto file. The current protobuf-c
  8. 1.5.0 release does not compile with Google protobuf 26.0 due to the most
  9. recentage breakage. There is a possible workaround involving the Google
  10. protobuf `FileDescriptorLegacy` class, which is documented as:
  11. // TODO Remove this deprecated API entirely.
  12. So we probably shouldn't rely on it.
  13. Instead, this commit obtains the `FileDescriptorProto` corresponding
  14. to the passed in `FieldDescriptor` and interrogates the `syntax` field
  15. directly. This is a single implementation with no version-specific
  16. workarounds. Hopefully this won't break in the next Google protobuf
  17. release.
  18. I tested the `FieldSyntax()` implementation in this commit across a
  19. number of different Google protobuf releases and found that it worked
  20. (`make && make check`) on all of them:
  21. - Google protobuf 3.6.1.3 (Ubuntu 20.04)
  22. - Google protobuf 3.12.4 (Ubuntu 22.04)
  23. - Google protobuf 3.21.12 (Debian 12 + Debian unstable)
  24. - Google protobuf 3.25.2 (Debian experimental)
  25. - Google protobuf 26.1-dev
  26. Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
  27. Upstream: https://github.com/protobuf-c/protobuf-c/pull/711
  28. ---
  29. protoc-c/c_helpers.h | 24 ++++++++++++++----------
  30. 1 file changed, 14 insertions(+), 10 deletions(-)
  31. diff --git a/protoc-c/c_helpers.h b/protoc-c/c_helpers.h
  32. index 062d330..be28b60 100644
  33. --- a/protoc-c/c_helpers.h
  34. +++ b/protoc-c/c_helpers.h
  35. @@ -70,10 +70,6 @@
  36. #include <protobuf-c/protobuf-c.pb.h>
  37. #include <google/protobuf/io/printer.h>
  38. -#if GOOGLE_PROTOBUF_VERSION >= 4023000
  39. -# include <google/protobuf/descriptor_legacy.h>
  40. -#endif
  41. -
  42. namespace google {
  43. namespace protobuf {
  44. namespace compiler {
  45. @@ -173,13 +169,21 @@ struct NameIndex
  46. int compare_name_indices_by_name(const void*, const void*);
  47. // Return the syntax version of the file containing the field.
  48. -// This wrapper is needed to be able to compile against protobuf2.
  49. inline int FieldSyntax(const FieldDescriptor* field) {
  50. -#if GOOGLE_PROTOBUF_VERSION >= 4023000
  51. - return FileDescriptorLegacy(field->file()).syntax() == FileDescriptorLegacy::SYNTAX_PROTO3 ? 3 : 2;
  52. -#else
  53. - return field->file()->syntax() == FileDescriptor::SYNTAX_PROTO3 ? 3 : 2;
  54. -#endif
  55. + auto proto = FileDescriptorProto();
  56. + field->file()->CopyTo(&proto);
  57. +
  58. + if (proto.has_syntax()) {
  59. + auto syntax = proto.syntax();
  60. + assert(syntax == "proto2" || syntax == "proto3");
  61. + if (syntax == "proto2") {
  62. + return 2;
  63. + } else if (syntax == "proto3") {
  64. + return 3;
  65. + }
  66. + }
  67. +
  68. + return 2;
  69. }
  70. // Work around changes in protobuf >= 22.x without breaking compilation against
  71. --
  72. 2.34.1