1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
|
From b28683f8027bf1e886b748b5603eb16d203b5a92 Mon Sep 17 00:00:00 2001
From: Robert Edmonds <edmonds@users.noreply.github.com>
Date: Sat, 8 Feb 2025 18:18:33 -0500
Subject: [PATCH 01/11] protoc-gen-c/c_helpers.h: Move compat defines into new
header file compat.h
Upstream-Status: Backport [https://gitlab.archlinux.org/archlinux/packaging/packages/protobuf-c/-/blob/main/protobuf-30.patch]
Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com>
---
protoc-gen-c/c_field.cc | 1 +
protoc-gen-c/c_helpers.cc | 1 +
protoc-gen-c/c_helpers.h | 10 -------
protoc-gen-c/c_message.cc | 1 +
protoc-gen-c/c_primitive_field.cc | 1 +
protoc-gen-c/compat.h | 46 +++++++++++++++++++++++++++++++
protoc-gen-c/main.cc | 1 +
7 files changed, 51 insertions(+), 10 deletions(-)
create mode 100644 protoc-gen-c/compat.h
diff --git a/protoc-gen-c/c_field.cc b/protoc-gen-c/c_field.cc
index 5e79967b..d6d8597e 100644
--- a/protoc-gen-c/c_field.cc
+++ b/protoc-gen-c/c_field.cc
@@ -74,6 +74,7 @@
#include "c_message_field.h"
#include "c_primitive_field.h"
#include "c_string_field.h"
+#include "compat.h"
namespace protobuf_c {
diff --git a/protoc-gen-c/c_helpers.cc b/protoc-gen-c/c_helpers.cc
index 5edcf904..c38843f8 100644
--- a/protoc-gen-c/c_helpers.cc
+++ b/protoc-gen-c/c_helpers.cc
@@ -73,6 +73,7 @@
#include <google/protobuf/stubs/common.h>
#include "c_helpers.h"
+#include "compat.h"
namespace protobuf_c {
diff --git a/protoc-gen-c/c_helpers.h b/protoc-gen-c/c_helpers.h
index 943676e9..e69504bb 100644
--- a/protoc-gen-c/c_helpers.h
+++ b/protoc-gen-c/c_helpers.h
@@ -186,16 +186,6 @@ inline int FieldSyntax(const google::protobuf::FieldDescriptor* field) {
return 2;
}
-// Work around changes in protobuf >= 22.x without breaking compilation against
-// older protobuf versions.
-#if GOOGLE_PROTOBUF_VERSION >= 4022000
-# define GOOGLE_ARRAYSIZE ABSL_ARRAYSIZE
-# define GOOGLE_CHECK_EQ ABSL_CHECK_EQ
-# define GOOGLE_CHECK_EQ ABSL_CHECK_EQ
-# define GOOGLE_DCHECK_GE ABSL_DCHECK_GE
-# define GOOGLE_LOG ABSL_LOG
-#endif
-
} // namespace protobuf_c
#endif // PROTOBUF_C_PROTOC_GEN_C_C_HELPERS_H__
diff --git a/protoc-gen-c/c_message.cc b/protoc-gen-c/c_message.cc
index d4a9a01e..46413873 100644
--- a/protoc-gen-c/c_message.cc
+++ b/protoc-gen-c/c_message.cc
@@ -78,6 +78,7 @@
#include "c_extension.h"
#include "c_helpers.h"
#include "c_message.h"
+#include "compat.h"
namespace protobuf_c {
diff --git a/protoc-gen-c/c_primitive_field.cc b/protoc-gen-c/c_primitive_field.cc
index 253b00bd..588f60e6 100644
--- a/protoc-gen-c/c_primitive_field.cc
+++ b/protoc-gen-c/c_primitive_field.cc
@@ -67,6 +67,7 @@
#include "c_helpers.h"
#include "c_primitive_field.h"
+#include "compat.h"
namespace protobuf_c {
diff --git a/protoc-gen-c/compat.h b/protoc-gen-c/compat.h
new file mode 100644
index 00000000..2ee78281
--- /dev/null
+++ b/protoc-gen-c/compat.h
@@ -0,0 +1,46 @@
+// Copyright (c) 2008-2025, Dave Benson and the protobuf-c authors.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+//
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#ifndef PROTOBUF_C_PROTOC_GEN_C_COMPAT_H__
+#define PROTOBUF_C_PROTOC_GEN_C_COMPAT_H__
+
+#if GOOGLE_PROTOBUF_VERSION >= 4022000
+# define GOOGLE_ARRAYSIZE ABSL_ARRAYSIZE
+# define GOOGLE_CHECK_EQ ABSL_CHECK_EQ
+# define GOOGLE_DCHECK_GE ABSL_DCHECK_GE
+# define GOOGLE_LOG ABSL_LOG
+#endif
+
+namespace protobuf_c {
+
+namespace compat {
+
+} // namespace compat
+
+} // namespace protobuf_c
+
+#endif // PROTOBUF_C_PROTOC_GEN_C_COMPAT_H__
diff --git a/protoc-gen-c/main.cc b/protoc-gen-c/main.cc
index 0656c113..5ab929d3 100644
--- a/protoc-gen-c/main.cc
+++ b/protoc-gen-c/main.cc
@@ -32,6 +32,7 @@
#include "c_generator.h"
#include "c_helpers.h"
+#include "compat.h"
int main(int argc, char* argv[]) {
protobuf_c::CGenerator c_generator;
From 1678f1fba6f2d3e5c1db2817495ddcd72bd4e87b Mon Sep 17 00:00:00 2001
From: Robert Edmonds <edmonds@users.noreply.github.com>
Date: Sat, 8 Feb 2025 20:09:03 -0500
Subject: [PATCH 02/11] protoc-gen-c/compat.h: Add `compat::StringView` type
protobuf >= 30.x replaces `const std::string&` in various APIs with
its own string view type that may actually be a `absl::string_view`.
Introduce our own `compat::StringView` type that we can use instead
of `const std::string&` in order to support compiling across multiple
protobuf versions.
---
protoc-gen-c/compat.h | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/protoc-gen-c/compat.h b/protoc-gen-c/compat.h
index 2ee78281..fe8041b5 100644
--- a/protoc-gen-c/compat.h
+++ b/protoc-gen-c/compat.h
@@ -28,6 +28,8 @@
#ifndef PROTOBUF_C_PROTOC_GEN_C_COMPAT_H__
#define PROTOBUF_C_PROTOC_GEN_C_COMPAT_H__
+#include <string>
+
#if GOOGLE_PROTOBUF_VERSION >= 4022000
# define GOOGLE_ARRAYSIZE ABSL_ARRAYSIZE
# define GOOGLE_CHECK_EQ ABSL_CHECK_EQ
@@ -39,6 +41,12 @@ namespace protobuf_c {
namespace compat {
+#if GOOGLE_PROTOBUF_VERSION >= 6030000
+typedef google::protobuf::internal::DescriptorStringView StringView;
+#else
+typedef const std::string& StringView;
+#endif
+
} // namespace compat
} // namespace protobuf_c
From db5252c131c82fb48ee599179b6989a577b7fbc8 Mon Sep 17 00:00:00 2001
From: Robert Edmonds <edmonds@users.noreply.github.com>
Date: Sat, 8 Feb 2025 20:13:44 -0500
Subject: [PATCH 03/11] Remove some unused functions
---
protoc-gen-c/c_helpers.cc | 64 ---------------------------------------
protoc-gen-c/c_helpers.h | 3 --
2 files changed, 67 deletions(-)
diff --git a/protoc-gen-c/c_helpers.cc b/protoc-gen-c/c_helpers.cc
index c38843f8..bbb4a615 100644
--- a/protoc-gen-c/c_helpers.cc
+++ b/protoc-gen-c/c_helpers.cc
@@ -90,14 +90,6 @@ namespace protobuf_c {
#pragma warning(disable:4996)
#endif
-std::string DotsToUnderscores(const std::string& name) {
- return StringReplace(name, ".", "_", true);
-}
-
-std::string DotsToColons(const std::string& name) {
- return StringReplace(name, ".", "::", true);
-}
-
std::string SimpleFtoa(float f) {
char buf[100];
snprintf(buf,sizeof(buf),"%.*g", FLT_DIG, f);
@@ -333,11 +325,6 @@ std::string FilenameIdentifier(const std::string& filename) {
return result;
}
-// Return the name of the BuildDescriptors() function for a given file.
-std::string GlobalBuildDescriptorsName(const std::string& filename) {
- return "proto_BuildDescriptors_" + FilenameIdentifier(filename);
-}
-
std::string GetLabelName(google::protobuf::FieldDescriptor::Label label) {
switch (label) {
case google::protobuf::FieldDescriptor::LABEL_OPTIONAL: return "optional";
@@ -392,57 +379,6 @@ WriteIntRanges(google::protobuf::io::Printer* printer, int n_values, const int *
}
}
-
-
-// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx
-// XXXXXXXXX this stuff is copied from strutils.cc !!!! XXXXXXXXXXXXXXXXXXXXXXXXXXXXx
-// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx
-// ----------------------------------------------------------------------
-// StringReplace()
-// Replace the "old" pattern with the "new" pattern in a string,
-// and append the result to "res". If replace_all is false,
-// it only replaces the first instance of "old."
-// ----------------------------------------------------------------------
-
-void StringReplace(const std::string& s, const std::string& oldsub,
- const std::string& newsub, bool replace_all,
- std::string* res) {
- if (oldsub.empty()) {
- res->append(s); // if empty, append the given string.
- return;
- }
-
- std::string::size_type start_pos = 0;
- std::string::size_type pos;
- do {
- pos = s.find(oldsub, start_pos);
- if (pos == std::string::npos) {
- break;
- }
- res->append(s, start_pos, pos - start_pos);
- res->append(newsub);
- start_pos = pos + oldsub.size(); // start searching again after the "old"
- } while (replace_all);
- res->append(s, start_pos, s.length() - start_pos);
-}
-
-
-// ----------------------------------------------------------------------
-// StringReplace()
-// Give me a string and two patterns "old" and "new", and I replace
-// the first instance of "old" in the string with "new", if it
-// exists. If "global" is true; call this repeatedly until it
-// fails. RETURN a new string, regardless of whether the replacement
-// happened or not.
-// ----------------------------------------------------------------------
-
-std::string StringReplace(const std::string& s, const std::string& oldsub,
- const std::string& newsub, bool replace_all) {
- std::string ret;
- StringReplace(s, oldsub, newsub, replace_all, &ret);
- return ret;
-}
-
// ----------------------------------------------------------------------
// SplitStringUsing()
// Split a string using a character delimiter. Append the components
diff --git a/protoc-gen-c/c_helpers.h b/protoc-gen-c/c_helpers.h
index e69504bb..377d4272 100644
--- a/protoc-gen-c/c_helpers.h
+++ b/protoc-gen-c/c_helpers.h
@@ -150,9 +150,6 @@ const char* DeclaredTypeMethodName(google::protobuf::FieldDescriptor::Type type)
// Convert a file name into a valid identifier.
std::string FilenameIdentifier(const std::string& filename);
-// Return the name of the BuildDescriptors() function for a given file.
-std::string GlobalBuildDescriptorsName(const std::string& filename);
-
// return 'required', 'optional', or 'repeated'
std::string GetLabelName(google::protobuf::FieldDescriptor::Label label);
From bc2cb66d908f016dd3f7082c8a6ad7c58bc03412 Mon Sep 17 00:00:00 2001
From: Robert Edmonds <edmonds@users.noreply.github.com>
Date: Sat, 8 Feb 2025 20:18:05 -0500
Subject: [PATCH 04/11] Use `compat::StringView` type across various function
signatures
---
protoc-gen-c/c_helpers.cc | 44 +++++++++++++++++++++------------------
protoc-gen-c/c_helpers.h | 36 ++++++++++++++++----------------
2 files changed, 42 insertions(+), 38 deletions(-)
diff --git a/protoc-gen-c/c_helpers.cc b/protoc-gen-c/c_helpers.cc
index bbb4a615..c759c8c2 100644
--- a/protoc-gen-c/c_helpers.cc
+++ b/protoc-gen-c/c_helpers.cc
@@ -96,6 +96,7 @@ std::string SimpleFtoa(float f) {
buf[sizeof(buf)-1] = 0; /* should NOT be necessary */
return buf;
}
+
std::string SimpleDtoa(double d) {
char buf[100];
snprintf(buf,sizeof(buf),"%.*g", DBL_DIG, d);
@@ -103,7 +104,7 @@ std::string SimpleDtoa(double d) {
return buf;
}
-std::string CamelToUpper(const std::string &name) {
+std::string CamelToUpper(compat::StringView name) {
bool was_upper = true; // suppress initial _
std::string rv = "";
int len = name.length();
@@ -120,7 +121,8 @@ std::string CamelToUpper(const std::string &name) {
}
return rv;
}
-std::string CamelToLower(const std::string &name) {
+
+std::string CamelToLower(compat::StringView name) {
bool was_upper = true; // suppress initial _
std::string rv = "";
int len = name.length();
@@ -138,8 +140,7 @@ std::string CamelToLower(const std::string &name) {
return rv;
}
-
-std::string ToUpper(const std::string &name) {
+std::string ToUpper(compat::StringView name) {
std::string rv = "";
int len = name.length();
for (int i = 0; i < len; i++) {
@@ -147,7 +148,8 @@ std::string ToUpper(const std::string &name) {
}
return rv;
}
-std::string ToLower(const std::string &name) {
+
+std::string ToLower(compat::StringView name) {
std::string rv = "";
int len = name.length();
for (int i = 0; i < len; i++) {
@@ -155,7 +157,8 @@ std::string ToLower(const std::string &name) {
}
return rv;
}
-std::string ToCamel(const std::string &name) {
+
+std::string ToCamel(compat::StringView name) {
std::string rv = "";
int len = name.length();
bool next_is_upper = true;
@@ -172,7 +175,7 @@ std::string ToCamel(const std::string &name) {
return rv;
}
-std::string OverrideFullName(const std::string &full_name, const google::protobuf::FileDescriptor* file) {
+std::string OverrideFullName(compat::StringView full_name, const google::protobuf::FileDescriptor* file) {
const ProtobufCFileOptions opt = file->options().GetExtension(pb_c_file);
if (!opt.has_c_package())
return full_name;
@@ -184,7 +187,7 @@ std::string OverrideFullName(const std::string &full_name, const google::protobu
return new_name + full_name.substr(file->package().length());
}
-std::string FullNameToLower(const std::string &full_name, const google::protobuf::FileDescriptor* file) {
+std::string FullNameToLower(compat::StringView full_name, const google::protobuf::FileDescriptor* file) {
std::vector<std::string> pieces;
SplitStringUsing(OverrideFullName(full_name, file), ".", &pieces);
std::string rv = "";
@@ -195,7 +198,8 @@ std::string FullNameToLower(const std::string &full_name, const google::protobuf
}
return rv;
}
-std::string FullNameToUpper(const std::string &full_name, const google::protobuf::FileDescriptor* file) {
+
+std::string FullNameToUpper(compat::StringView full_name, const google::protobuf::FileDescriptor* file) {
std::vector<std::string> pieces;
SplitStringUsing(OverrideFullName(full_name, file), ".", &pieces);
std::string rv = "";
@@ -206,7 +210,8 @@ std::string FullNameToUpper(const std::string &full_name, const google::protobuf
}
return rv;
}
-std::string FullNameToC(const std::string &full_name, const google::protobuf::FileDescriptor* file) {
+
+std::string FullNameToC(compat::StringView full_name, const google::protobuf::FileDescriptor* file) {
std::vector<std::string> pieces;
SplitStringUsing(OverrideFullName(full_name, file), ".", &pieces);
std::string rv = "";
@@ -248,7 +253,7 @@ void PrintComment(google::protobuf::io::Printer* printer, std::string comment)
}
}
-std::string ConvertToSpaces(const std::string &input) {
+std::string ConvertToSpaces(compat::StringView input) {
return std::string(input.size(), ' ');
}
@@ -259,8 +264,7 @@ int compare_name_indices_by_name(const void *a, const void *b)
return strcmp (ni_a->name, ni_b->name);
}
-
-std::string CEscape(const std::string& src);
+std::string CEscape(compat::StringView src);
const char* const kKeywordList[] = {
"and", "and_eq", "asm", "auto", "bitand", "bitor", "bool", "break", "case",
@@ -300,7 +304,7 @@ std::string FieldDeprecated(const google::protobuf::FieldDescriptor* field) {
return "";
}
-std::string StripProto(const std::string& filename) {
+std::string StripProto(compat::StringView filename) {
if (HasSuffixString(filename, ".protodevel")) {
return StripSuffixString(filename, ".protodevel");
} else {
@@ -309,7 +313,7 @@ std::string StripProto(const std::string& filename) {
}
// Convert a file name into a valid identifier.
-std::string FilenameIdentifier(const std::string& filename) {
+std::string FilenameIdentifier(compat::StringView filename) {
std::string result;
for (unsigned i = 0; i < filename.size(); i++) {
if (isalnum(filename[i])) {
@@ -335,7 +339,7 @@ std::string GetLabelName(google::protobuf::FieldDescriptor::Label label) {
}
unsigned
-WriteIntRanges(google::protobuf::io::Printer* printer, int n_values, const int *values, const std::string &name)
+WriteIntRanges(google::protobuf::io::Printer* printer, int n_values, const int *values, compat::StringView name)
{
std::map<std::string, std::string> vars;
vars["name"] = name;
@@ -389,7 +393,7 @@ WriteIntRanges(google::protobuf::io::Printer* printer, int n_values, const int *
// ----------------------------------------------------------------------
template <typename ITR>
static inline
-void SplitStringToIteratorUsing(const std::string& full,
+void SplitStringToIteratorUsing(compat::StringView full,
const char* delim,
ITR& result) {
// Optimize the common case where delim is a single character.
@@ -422,7 +426,7 @@ void SplitStringToIteratorUsing(const std::string& full,
}
}
-void SplitStringUsing(const std::string& full,
+void SplitStringUsing(compat::StringView full,
const char* delim,
std::vector<std::string>* result) {
std::back_insert_iterator< std::vector<std::string> > it(*result);
@@ -435,7 +439,6 @@ char* FastHexToBuffer(int i, char* buffer)
return buffer;
}
-
static int CEscapeInternal(const char* src, int src_len, char* dest,
int dest_len, bool use_hex) {
const char* src_end = src + src_len;
@@ -478,7 +481,8 @@ static int CEscapeInternal(const char* src, int src_len, char* dest,
dest[used] = '\0'; // doesn't count towards return value though
return used;
}
-std::string CEscape(const std::string& src) {
+
+std::string CEscape(compat::StringView src) {
const int dest_length = src.size() * 4 + 1; // Maximum possible expansion
std::unique_ptr<char[]> dest(new char[dest_length]);
const int len = CEscapeInternal(src.data(), src.size(),
diff --git a/protoc-gen-c/c_helpers.h b/protoc-gen-c/c_helpers.h
index 377d4272..ccd39ca2 100644
--- a/protoc-gen-c/c_helpers.h
+++ b/protoc-gen-c/c_helpers.h
@@ -73,6 +73,8 @@
#include <protobuf-c/protobuf-c.pb.h>
+#include "compat.h"
+
namespace protobuf_c {
// --- Borrowed from stubs. ---
@@ -84,11 +86,10 @@ template <typename T> std::string SimpleItoa(T n) {
std::string SimpleFtoa(float f);
std::string SimpleDtoa(double f);
-void SplitStringUsing(const std::string &str, const char *delim, std::vector<std::string> *out);
-std::string CEscape(const std::string& src);
-std::string StringReplace(const std::string& s, const std::string& oldsub, const std::string& newsub, bool replace_all);
-inline bool HasSuffixString(const std::string& str, const std::string& suffix) { return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0; }
-inline std::string StripSuffixString(const std::string& str, const std::string& suffix) { if (HasSuffixString(str, suffix)) { return str.substr(0, str.size() - suffix.size()); } else { return str; } }
+void SplitStringUsing(compat::StringView str, const char *delim, std::vector<std::string> *out);
+std::string CEscape(compat::StringView src);
+inline bool HasSuffixString(compat::StringView str, compat::StringView suffix) { return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0; }
+inline std::string StripSuffixString(compat::StringView str, compat::StringView suffix) { if (HasSuffixString(str, suffix)) { return str.substr(0, str.size() - suffix.size()); } else { return str; } }
char* FastHexToBuffer(int i, char* buffer);
@@ -110,31 +111,31 @@ inline const google::protobuf::Descriptor* FieldScope(const google::protobuf::Fi
// convert a CamelCase class name into an all uppercase affair
// with underscores separating words, e.g. MyClass becomes MY_CLASS.
-std::string CamelToUpper(const std::string &class_name);
-std::string CamelToLower(const std::string &class_name);
+std::string CamelToUpper(compat::StringView class_name);
+std::string CamelToLower(compat::StringView class_name);
// lowercased, underscored name to camel case
-std::string ToCamel(const std::string &name);
+std::string ToCamel(compat::StringView name);
// lowercase the string
-std::string ToLower(const std::string &class_name);
-std::string ToUpper(const std::string &class_name);
+std::string ToLower(compat::StringView class_name);
+std::string ToUpper(compat::StringView class_name);
// full_name() to lowercase with underscores
-std::string FullNameToLower(const std::string &full_name, const google::protobuf::FileDescriptor *file);
-std::string FullNameToUpper(const std::string &full_name, const google::protobuf::FileDescriptor *file);
+std::string FullNameToLower(compat::StringView full_name, const google::protobuf::FileDescriptor *file);
+std::string FullNameToUpper(compat::StringView full_name, const google::protobuf::FileDescriptor *file);
// full_name() to c-typename (with underscores for packages, otherwise camel case)
-std::string FullNameToC(const std::string &class_name, const google::protobuf::FileDescriptor *file);
+std::string FullNameToC(compat::StringView class_name, const google::protobuf::FileDescriptor *file);
// Splits, indents, formats, and prints comment lines
void PrintComment(google::protobuf::io::Printer* printer, std::string comment);
// make a string of spaces as long as input
-std::string ConvertToSpaces(const std::string &input);
+std::string ConvertToSpaces(compat::StringView input);
// Strips ".proto" or ".protodevel" from the end of a filename.
-std::string StripProto(const std::string& filename);
+std::string StripProto(compat::StringView filename);
// Get the C++ type name for a primitive type (e.g. "double", "::google::protobuf::int32", etc.).
// Note: non-built-in type names will be qualified, meaning they will start
@@ -148,15 +149,14 @@ const char* PrimitiveTypeName(google::protobuf::FieldDescriptor::CppType type);
const char* DeclaredTypeMethodName(google::protobuf::FieldDescriptor::Type type);
// Convert a file name into a valid identifier.
-std::string FilenameIdentifier(const std::string& filename);
+std::string FilenameIdentifier(compat::StringView filename);
// return 'required', 'optional', or 'repeated'
std::string GetLabelName(google::protobuf::FieldDescriptor::Label label);
-
// write IntRanges entries for a bunch of sorted values.
// returns the number of ranges there are to bsearch.
-unsigned WriteIntRanges(google::protobuf::io::Printer* printer, int n_values, const int *values, const std::string &name);
+unsigned WriteIntRanges(google::protobuf::io::Printer* printer, int n_values, const int *values, compat::StringView name);
struct NameIndex
{
From 75f1c32cc429233a3726358c999009f9ea373b45 Mon Sep 17 00:00:00 2001
From: Robert Edmonds <edmonds@users.noreply.github.com>
Date: Sat, 8 Feb 2025 20:25:43 -0500
Subject: [PATCH 05/11] Convert string views to owned strings where necessary
---
protoc-gen-c/c_enum.cc | 2 +-
protoc-gen-c/c_enum_field.cc | 2 +-
protoc-gen-c/c_helpers.cc | 8 ++++----
protoc-gen-c/c_helpers.h | 3 +--
4 files changed, 7 insertions(+), 8 deletions(-)
diff --git a/protoc-gen-c/c_enum.cc b/protoc-gen-c/c_enum.cc
index 9212ab82..311e4c86 100644
--- a/protoc-gen-c/c_enum.cc
+++ b/protoc-gen-c/c_enum.cc
@@ -152,7 +152,7 @@ void EnumGenerator::GenerateValueInitializer(google::protobuf::io::Printer *prin
descriptor_->file()->options().optimize_for() ==
google::protobuf::FileOptions_OptimizeMode_CODE_SIZE;
vars["enum_value_name"] = vd->name();
- vars["c_enum_value_name"] = FullNameToUpper(descriptor_->full_name(), descriptor_->file()) + "__" + vd->name();
+ vars["c_enum_value_name"] = FullNameToUpper(descriptor_->full_name(), descriptor_->file()) + "__" + std::string(vd->name());
vars["value"] = SimpleItoa(vd->number());
if (optimize_code_size)
printer->Print(vars, " { NULL, NULL, $value$ }, /* CODE_SIZE */\n");
diff --git a/protoc-gen-c/c_enum_field.cc b/protoc-gen-c/c_enum_field.cc
index 0926ae59..c3111f50 100644
--- a/protoc-gen-c/c_enum_field.cc
+++ b/protoc-gen-c/c_enum_field.cc
@@ -78,7 +78,7 @@ void SetEnumVariables(const google::protobuf::FieldDescriptor* descriptor,
(*variables)["type"] = FullNameToC(descriptor->enum_type()->full_name(), descriptor->enum_type()->file());
const google::protobuf::EnumValueDescriptor* default_value = descriptor->default_value_enum();
(*variables)["default"] = FullNameToUpper(default_value->type()->full_name(), default_value->type()->file())
- + "__" + default_value->name();
+ + "__" + std::string(default_value->name());
(*variables)["deprecated"] = FieldDeprecated(descriptor);
}
diff --git a/protoc-gen-c/c_helpers.cc b/protoc-gen-c/c_helpers.cc
index c759c8c2..1aecef93 100644
--- a/protoc-gen-c/c_helpers.cc
+++ b/protoc-gen-c/c_helpers.cc
@@ -178,13 +178,13 @@ std::string ToCamel(compat::StringView name) {
std::string OverrideFullName(compat::StringView full_name, const google::protobuf::FileDescriptor* file) {
const ProtobufCFileOptions opt = file->options().GetExtension(pb_c_file);
if (!opt.has_c_package())
- return full_name;
+ return std::string(full_name);
std::string new_name = opt.c_package();
if (file->package().empty())
new_name += ".";
- return new_name + full_name.substr(file->package().length());
+ return new_name + std::string(full_name.substr(file->package().length()));
}
std::string FullNameToLower(compat::StringView full_name, const google::protobuf::FileDescriptor* file) {
@@ -418,10 +418,10 @@ void SplitStringToIteratorUsing(compat::StringView full,
while (begin_index != std::string::npos) {
end_index = full.find_first_of(delim, begin_index);
if (end_index == std::string::npos) {
- *result++ = full.substr(begin_index);
+ *result++ = std::string(full.substr(begin_index));
return;
}
- *result++ = full.substr(begin_index, (end_index - begin_index));
+ *result++ = std::string(full.substr(begin_index, (end_index - begin_index)));
begin_index = full.find_first_not_of(delim, end_index);
}
}
diff --git a/protoc-gen-c/c_helpers.h b/protoc-gen-c/c_helpers.h
index ccd39ca2..985e4db6 100644
--- a/protoc-gen-c/c_helpers.h
+++ b/protoc-gen-c/c_helpers.h
@@ -89,10 +89,9 @@ std::string SimpleDtoa(double f);
void SplitStringUsing(compat::StringView str, const char *delim, std::vector<std::string> *out);
std::string CEscape(compat::StringView src);
inline bool HasSuffixString(compat::StringView str, compat::StringView suffix) { return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0; }
-inline std::string StripSuffixString(compat::StringView str, compat::StringView suffix) { if (HasSuffixString(str, suffix)) { return str.substr(0, str.size() - suffix.size()); } else { return str; } }
+inline std::string StripSuffixString(compat::StringView str, compat::StringView suffix) { if (HasSuffixString(str, suffix)) { return std::string(str.substr(0, str.size() - suffix.size())); } else { return std::string(str); } }
char* FastHexToBuffer(int i, char* buffer);
-
// Get the (unqualified) name that should be used for this field in C code.
// The name is coerced to lower-case to emulate proto1 behavior. People
// should be using lowercase-with-underscores style for proto field names
From 0edca93db369fb84f01cc0d4e3ee4cd6c2ad7f4f Mon Sep 17 00:00:00 2001
From: Robert Edmonds <edmonds@users.noreply.github.com>
Date: Sat, 8 Feb 2025 20:39:11 -0500
Subject: [PATCH 06/11] Convert various uses of `const char *` to
`compat::StringView`
Also replace some uses of arrays manually allocated with new/delete with
uses of `std::vector`.
---
protoc-gen-c/c_enum.cc | 35 ++++++++++++++++++-----------------
protoc-gen-c/c_helpers.cc | 2 +-
protoc-gen-c/c_helpers.h | 2 +-
protoc-gen-c/c_message.cc | 28 ++++++++++++----------------
protoc-gen-c/c_service.cc | 19 +++++++++----------
5 files changed, 41 insertions(+), 45 deletions(-)
diff --git a/protoc-gen-c/c_enum.cc b/protoc-gen-c/c_enum.cc
index 311e4c86..c7839edd 100644
--- a/protoc-gen-c/c_enum.cc
+++ b/protoc-gen-c/c_enum.cc
@@ -142,7 +142,7 @@ struct ValueIndex
int value;
unsigned index;
unsigned final_index; /* index in uniqified array of values */
- const char *name;
+ compat::StringView name;
};
void EnumGenerator::GenerateValueInitializer(google::protobuf::io::Printer *printer, int index)
{
@@ -176,7 +176,7 @@ static int compare_value_indices_by_name(const void *a, const void *b)
{
const ValueIndex *vi_a = (const ValueIndex *) a;
const ValueIndex *vi_b = (const ValueIndex *) b;
- return strcmp (vi_a->name, vi_b->name);
+ return vi_a->name.compare(vi_b->name);
}
void EnumGenerator::GenerateEnumDescriptor(google::protobuf::io::Printer* printer) {
@@ -194,18 +194,20 @@ void EnumGenerator::GenerateEnumDescriptor(google::protobuf::io::Printer* printe
// Sort by name and value, dropping duplicate values if they appear later.
// TODO: use a c++ paradigm for this!
- NameIndex *name_index = new NameIndex[descriptor_->value_count()];
- ValueIndex *value_index = new ValueIndex[descriptor_->value_count()];
- for (int j = 0; j < descriptor_->value_count(); j++) {
+ std::vector<ValueIndex> value_index;
+ for (unsigned j = 0; j < descriptor_->value_count(); j++) {
const google::protobuf::EnumValueDescriptor *vd = descriptor_->value(j);
- name_index[j].index = j;
- name_index[j].name = vd->name().c_str();
- value_index[j].index = j;
- value_index[j].value = vd->number();
- value_index[j].name = vd->name().c_str();
+ value_index.push_back({
+ .value = vd->number(),
+ .index = j,
+ .final_index = 0,
+ .name = vd->name(),
+ });
}
- qsort(value_index, descriptor_->value_count(),
- sizeof(ValueIndex), compare_value_indices_by_value_then_index);
+ qsort(&value_index[0],
+ value_index.size(),
+ sizeof(ValueIndex),
+ compare_value_indices_by_value_then_index);
// only record unique values
int n_unique_values;
@@ -275,8 +277,10 @@ void EnumGenerator::GenerateEnumDescriptor(google::protobuf::io::Printer* printe
vars["n_ranges"] = SimpleItoa(n_ranges);
if (!optimize_code_size) {
- qsort(value_index, descriptor_->value_count(),
- sizeof(ValueIndex), compare_value_indices_by_name);
+ qsort(&value_index[0],
+ value_index.size(),
+ sizeof(ValueIndex),
+ compare_value_indices_by_name);
printer->Print(vars,
"static const ProtobufCEnumValueIndex $lcclassname$__enum_values_by_name[$value_count$] =\n"
"{\n");
@@ -319,9 +323,6 @@ void EnumGenerator::GenerateEnumDescriptor(google::protobuf::io::Printer* printe
" NULL,NULL,NULL,NULL /* reserved[1234] */\n"
"};\n");
}
-
- delete[] value_index;
- delete[] name_index;
}
} // namespace protobuf_c
diff --git a/protoc-gen-c/c_helpers.cc b/protoc-gen-c/c_helpers.cc
index 1aecef93..dec9ce28 100644
--- a/protoc-gen-c/c_helpers.cc
+++ b/protoc-gen-c/c_helpers.cc
@@ -261,7 +261,7 @@ int compare_name_indices_by_name(const void *a, const void *b)
{
const NameIndex *ni_a = (const NameIndex *) a;
const NameIndex *ni_b = (const NameIndex *) b;
- return strcmp (ni_a->name, ni_b->name);
+ return ni_a->name.compare(ni_b->name);
}
std::string CEscape(compat::StringView src);
diff --git a/protoc-gen-c/c_helpers.h b/protoc-gen-c/c_helpers.h
index 985e4db6..69369997 100644
--- a/protoc-gen-c/c_helpers.h
+++ b/protoc-gen-c/c_helpers.h
@@ -160,7 +160,7 @@ unsigned WriteIntRanges(google::protobuf::io::Printer* printer, int n_values, co
struct NameIndex
{
unsigned index;
- const char *name;
+ compat::StringView name;
};
int compare_name_indices_by_name(const void*, const void*);
diff --git a/protoc-gen-c/c_message.cc b/protoc-gen-c/c_message.cc
index 46413873..7252923c 100644
--- a/protoc-gen-c/c_message.cc
+++ b/protoc-gen-c/c_message.cc
@@ -567,27 +567,26 @@ GenerateMessageDescriptor(google::protobuf::io::Printer* printer, bool gen_init)
"static const ProtobufCFieldDescriptor $lcclassname$__field_descriptors[$n_fields$] =\n"
"{\n");
printer->Indent();
- const google::protobuf::FieldDescriptor **sorted_fields = new const google::protobuf::FieldDescriptor *[descriptor_->field_count()];
+
+ std::vector<const google::protobuf::FieldDescriptor*> sorted_fields;
for (int i = 0; i < descriptor_->field_count(); i++) {
- sorted_fields[i] = descriptor_->field(i);
+ sorted_fields.push_back(descriptor_->field(i));
}
- qsort (sorted_fields, descriptor_->field_count(),
+ qsort(&sorted_fields[0], sorted_fields.size(),
sizeof(const google::protobuf::FieldDescriptor*),
compare_pfields_by_number);
- for (int i = 0; i < descriptor_->field_count(); i++) {
- const google::protobuf::FieldDescriptor* field = sorted_fields[i];
+ for (auto field : sorted_fields) {
field_generators_.get(field).GenerateDescriptorInitializer(printer);
}
printer->Outdent();
printer->Print(vars, "};\n");
if (!optimize_code_size) {
- NameIndex *field_indices = new NameIndex [descriptor_->field_count()];
- for (int i = 0; i < descriptor_->field_count(); i++) {
- field_indices[i].name = sorted_fields[i]->name().c_str();
- field_indices[i].index = i;
+ std::vector<NameIndex> field_indices;
+ for (unsigned i = 0; i < descriptor_->field_count(); i++) {
+ field_indices.push_back({ .index = i, .name = sorted_fields[i]->name() });
}
- qsort (field_indices, descriptor_->field_count(), sizeof (NameIndex),
+ qsort(&field_indices[0], field_indices.size(), sizeof(NameIndex),
compare_name_indices_by_name);
printer->Print(vars, "static const unsigned $lcclassname$__field_indices_by_name[] = {\n");
for (int i = 0; i < descriptor_->field_count(); i++) {
@@ -596,19 +595,16 @@ GenerateMessageDescriptor(google::protobuf::io::Printer* printer, bool gen_init)
printer->Print(vars, " $index$, /* field[$index$] = $name$ */\n");
}
printer->Print("};\n");
- delete[] field_indices;
}
// create range initializers
- int *values = new int[descriptor_->field_count()];
+ std::vector<int> values;
for (int i = 0; i < descriptor_->field_count(); i++) {
- values[i] = sorted_fields[i]->number();
+ values.push_back(sorted_fields[i]->number());
}
int n_ranges = WriteIntRanges(printer,
- descriptor_->field_count(), values,
+ descriptor_->field_count(), &values[0],
vars["lcclassname"] + "__number_ranges");
- delete [] values;
- delete [] sorted_fields;
vars["n_ranges"] = SimpleItoa(n_ranges);
} else {
diff --git a/protoc-gen-c/c_service.cc b/protoc-gen-c/c_service.cc
index ee4d4a95..2c3ddcf3 100644
--- a/protoc-gen-c/c_service.cc
+++ b/protoc-gen-c/c_service.cc
@@ -184,19 +184,19 @@ void ServiceGenerator::GenerateInit(google::protobuf::io::Printer* printer)
"}\n");
}
-struct MethodIndexAndName { unsigned i; const char *name; };
+struct MethodIndexAndName { unsigned i; compat::StringView name; };
static int
compare_method_index_and_name_by_name (const void *a, const void *b)
{
const MethodIndexAndName *ma = (const MethodIndexAndName *) a;
const MethodIndexAndName *mb = (const MethodIndexAndName *) b;
- return strcmp (ma->name, mb->name);
+ return ma->name.compare(mb->name);
}
void ServiceGenerator::GenerateServiceDescriptor(google::protobuf::io::Printer* printer)
{
int n_methods = descriptor_->method_count();
- MethodIndexAndName *mi_array = new MethodIndexAndName[n_methods];
+ std::vector<MethodIndexAndName> mi_array;
bool optimize_code_size = descriptor_->file()->options().has_optimize_for() &&
descriptor_->file()->options().optimize_for() ==
@@ -205,7 +205,7 @@ void ServiceGenerator::GenerateServiceDescriptor(google::protobuf::io::Printer*
vars_["n_methods"] = SimpleItoa(n_methods);
printer->Print(vars_, "static const ProtobufCMethodDescriptor $lcfullname$__method_descriptors[$n_methods$] =\n"
"{\n");
- for (int i = 0; i < n_methods; i++) {
+ for (unsigned i = 0; i < n_methods; i++) {
const google::protobuf::MethodDescriptor* method = descriptor_->method(i);
vars_["method"] = method->name();
vars_["input_descriptor"] = "&" + FullNameToLower(method->input_type()->full_name(), method->input_type()->file()) + "__descriptor";
@@ -217,14 +217,15 @@ void ServiceGenerator::GenerateServiceDescriptor(google::protobuf::io::Printer*
printer->Print(vars_,
" { \"$method$\", $input_descriptor$, $output_descriptor$ },\n");
}
- mi_array[i].i = i;
- mi_array[i].name = method->name().c_str();
+ mi_array.push_back({i, method->name()});
}
printer->Print(vars_, "};\n");
if (!optimize_code_size) {
- qsort ((void*)mi_array, n_methods, sizeof (MethodIndexAndName),
- compare_method_index_and_name_by_name);
+ qsort(&mi_array[0],
+ mi_array.size(),
+ sizeof(MethodIndexAndName),
+ compare_method_index_and_name_by_name);
printer->Print(vars_, "const unsigned $lcfullname$__method_indices_by_name[] = {\n");
for (int i = 0; i < n_methods; i++) {
vars_["i"] = SimpleItoa(mi_array[i].i);
@@ -258,8 +259,6 @@ void ServiceGenerator::GenerateServiceDescriptor(google::protobuf::io::Printer*
" $lcfullname$__method_indices_by_name\n"
"};\n");
}
-
- delete[] mi_array;
}
void ServiceGenerator::GenerateCallersImplementations(google::protobuf::io::Printer* printer)
From ebeddac1a746393a16d9ba4cf80e3d12c3ab7d7f Mon Sep 17 00:00:00 2001
From: Robert Edmonds <edmonds@users.noreply.github.com>
Date: Sat, 8 Feb 2025 20:56:48 -0500
Subject: [PATCH 07/11] Fix indentation of
MessageGenerator::GenerateMessageDescriptor()
---
protoc-gen-c/c_message.cc | 321 +++++++++++++++++++-------------------
1 file changed, 159 insertions(+), 162 deletions(-)
diff --git a/protoc-gen-c/c_message.cc b/protoc-gen-c/c_message.cc
index 7252923c..2a3b2a2f 100644
--- a/protoc-gen-c/c_message.cc
+++ b/protoc-gen-c/c_message.cc
@@ -461,199 +461,196 @@ GenerateHelperFunctionDefinitions(google::protobuf::io::Printer* printer,
void MessageGenerator::
GenerateMessageDescriptor(google::protobuf::io::Printer* printer, bool gen_init) {
- std::map<std::string, std::string> vars;
- vars["fullname"] = descriptor_->full_name();
- vars["classname"] = FullNameToC(descriptor_->full_name(), descriptor_->file());
- vars["lcclassname"] = FullNameToLower(descriptor_->full_name(), descriptor_->file());
- vars["shortname"] = ToCamel(descriptor_->name());
- vars["n_fields"] = SimpleItoa(descriptor_->field_count());
- vars["packagename"] = descriptor_->file()->package();
-
- bool optimize_code_size = descriptor_->file()->options().has_optimize_for() &&
- descriptor_->file()->options().optimize_for() ==
- google::protobuf::FileOptions_OptimizeMode_CODE_SIZE;
-
- const ProtobufCMessageOptions opt =
- descriptor_->options().GetExtension(pb_c_msg);
- // Override parent settings, if needed
- if (opt.has_gen_init_helpers())
- gen_init = opt.gen_init_helpers();
-
- for (int i = 0; i < descriptor_->nested_type_count(); i++) {
- nested_generators_[i]->GenerateMessageDescriptor(printer, gen_init);
- }
+ std::map<std::string, std::string> vars;
+ vars["fullname"] = descriptor_->full_name();
+ vars["classname"] = FullNameToC(descriptor_->full_name(), descriptor_->file());
+ vars["lcclassname"] = FullNameToLower(descriptor_->full_name(), descriptor_->file());
+ vars["shortname"] = ToCamel(descriptor_->name());
+ vars["n_fields"] = SimpleItoa(descriptor_->field_count());
+ vars["packagename"] = descriptor_->file()->package();
- for (int i = 0; i < descriptor_->enum_type_count(); i++) {
- enum_generators_[i]->GenerateEnumDescriptor(printer);
- }
+ bool optimize_code_size = descriptor_->file()->options().has_optimize_for() &&
+ descriptor_->file()->options().optimize_for() ==
+ google::protobuf::FileOptions_OptimizeMode_CODE_SIZE;
- for (int i = 0; i < descriptor_->field_count(); i++) {
- const google::protobuf::FieldDescriptor* fd = descriptor_->field(i);
- if (fd->has_default_value()) {
- field_generators_.get(fd).GenerateDefaultValueImplementations(printer);
- }
- }
+ const ProtobufCMessageOptions opt = descriptor_->options().GetExtension(pb_c_msg);
+ // Override parent settings, if needed
+ if (opt.has_gen_init_helpers()) {
+ gen_init = opt.gen_init_helpers();
+ }
- for (int i = 0; i < descriptor_->field_count(); i++) {
- const google::protobuf::FieldDescriptor* fd = descriptor_->field(i);
- const ProtobufCFieldOptions opt = fd->options().GetExtension(pb_c_field);
- if (fd->has_default_value()) {
-
- bool already_defined = false;
- vars["name"] = fd->name();
- vars["lcname"] = CamelToLower(fd->name());
- vars["maybe_static"] = "static ";
- vars["field_dv_ctype_suffix"] = "";
- vars["default_value"] = field_generators_.get(fd).GetDefaultValue();
- switch (fd->cpp_type()) {
- case google::protobuf::FieldDescriptor::CPPTYPE_INT32:
- vars["field_dv_ctype"] = "int32_t";
- break;
- case google::protobuf::FieldDescriptor::CPPTYPE_INT64:
- vars["field_dv_ctype"] = "int64_t";
- break;
- case google::protobuf::FieldDescriptor::CPPTYPE_UINT32:
- vars["field_dv_ctype"] = "uint32_t";
- break;
- case google::protobuf::FieldDescriptor::CPPTYPE_UINT64:
- vars["field_dv_ctype"] = "uint64_t";
- break;
- case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT:
- vars["field_dv_ctype"] = "float";
- break;
- case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
- vars["field_dv_ctype"] = "double";
- break;
- case google::protobuf::FieldDescriptor::CPPTYPE_BOOL:
- vars["field_dv_ctype"] = "protobuf_c_boolean";
- break;
-
- case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE:
- // NOTE: not supported by protobuf
- vars["maybe_static"] = "";
- vars["field_dv_ctype"] = "{ ... }";
- GOOGLE_LOG(FATAL) << "Messages can't have default values!";
- break;
- case google::protobuf::FieldDescriptor::CPPTYPE_STRING:
- if (fd->type() == google::protobuf::FieldDescriptor::TYPE_BYTES || opt.string_as_bytes())
- {
- vars["field_dv_ctype"] = "ProtobufCBinaryData";
- }
- else /* STRING type */
- {
- already_defined = true;
- vars["maybe_static"] = "";
- vars["field_dv_ctype"] = "char";
- vars["field_dv_ctype_suffix"] = "[]";
- }
- break;
- case google::protobuf::FieldDescriptor::CPPTYPE_ENUM:
- {
- const google::protobuf::EnumValueDescriptor* vd = fd->default_value_enum();
- vars["field_dv_ctype"] = FullNameToC(vd->type()->full_name(), vd->type()->file());
- break;
- }
- default:
- GOOGLE_LOG(FATAL) << "Unknown CPPTYPE";
- break;
- }
- if (!already_defined)
- printer->Print(vars, "$maybe_static$const $field_dv_ctype$ $lcclassname$__$lcname$__default_value$field_dv_ctype_suffix$ = $default_value$;\n");
- }
- }
+ for (int i = 0; i < descriptor_->nested_type_count(); i++) {
+ nested_generators_[i]->GenerateMessageDescriptor(printer, gen_init);
+ }
- if ( descriptor_->field_count() ) {
- printer->Print(vars,
- "static const ProtobufCFieldDescriptor $lcclassname$__field_descriptors[$n_fields$] =\n"
- "{\n");
- printer->Indent();
+ for (int i = 0; i < descriptor_->enum_type_count(); i++) {
+ enum_generators_[i]->GenerateEnumDescriptor(printer);
+ }
- std::vector<const google::protobuf::FieldDescriptor*> sorted_fields;
for (int i = 0; i < descriptor_->field_count(); i++) {
- sorted_fields.push_back(descriptor_->field(i));
- }
- qsort(&sorted_fields[0], sorted_fields.size(),
- sizeof(const google::protobuf::FieldDescriptor*),
- compare_pfields_by_number);
- for (auto field : sorted_fields) {
- field_generators_.get(field).GenerateDescriptorInitializer(printer);
+ const google::protobuf::FieldDescriptor* fd = descriptor_->field(i);
+ if (fd->has_default_value()) {
+ field_generators_.get(fd).GenerateDefaultValueImplementations(printer);
+ }
}
- printer->Outdent();
- printer->Print(vars, "};\n");
- if (!optimize_code_size) {
- std::vector<NameIndex> field_indices;
- for (unsigned i = 0; i < descriptor_->field_count(); i++) {
- field_indices.push_back({ .index = i, .name = sorted_fields[i]->name() });
+ for (int i = 0; i < descriptor_->field_count(); i++) {
+ const google::protobuf::FieldDescriptor* fd = descriptor_->field(i);
+ const ProtobufCFieldOptions opt = fd->options().GetExtension(pb_c_field);
+ if (fd->has_default_value()) {
+ bool already_defined = false;
+ vars["name"] = fd->name();
+ vars["lcname"] = CamelToLower(fd->name());
+ vars["maybe_static"] = "static ";
+ vars["field_dv_ctype_suffix"] = "";
+ vars["default_value"] = field_generators_.get(fd).GetDefaultValue();
+ switch (fd->cpp_type()) {
+ case google::protobuf::FieldDescriptor::CPPTYPE_INT32:
+ vars["field_dv_ctype"] = "int32_t";
+ break;
+ case google::protobuf::FieldDescriptor::CPPTYPE_INT64:
+ vars["field_dv_ctype"] = "int64_t";
+ break;
+ case google::protobuf::FieldDescriptor::CPPTYPE_UINT32:
+ vars["field_dv_ctype"] = "uint32_t";
+ break;
+ case google::protobuf::FieldDescriptor::CPPTYPE_UINT64:
+ vars["field_dv_ctype"] = "uint64_t";
+ break;
+ case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT:
+ vars["field_dv_ctype"] = "float";
+ break;
+ case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
+ vars["field_dv_ctype"] = "double";
+ break;
+ case google::protobuf::FieldDescriptor::CPPTYPE_BOOL:
+ vars["field_dv_ctype"] = "protobuf_c_boolean";
+ break;
+ case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE:
+ // NOTE: not supported by protobuf
+ vars["maybe_static"] = "";
+ vars["field_dv_ctype"] = "{ ... }";
+ GOOGLE_LOG(FATAL) << "Messages can't have default values!";
+ break;
+ case google::protobuf::FieldDescriptor::CPPTYPE_STRING:
+ if (fd->type() == google::protobuf::FieldDescriptor::TYPE_BYTES || opt.string_as_bytes()) {
+ vars["field_dv_ctype"] = "ProtobufCBinaryData";
+ } else {
+ /* STRING type */
+ already_defined = true;
+ vars["maybe_static"] = "";
+ vars["field_dv_ctype"] = "char";
+ vars["field_dv_ctype_suffix"] = "[]";
+ }
+ break;
+ case google::protobuf::FieldDescriptor::CPPTYPE_ENUM: {
+ const google::protobuf::EnumValueDescriptor* vd = fd->default_value_enum();
+ vars["field_dv_ctype"] = FullNameToC(vd->type()->full_name(), vd->type()->file());
+ break;
+ }
+ default:
+ GOOGLE_LOG(FATAL) << "Unknown CPPTYPE";
+ break;
+ }
+ if (!already_defined) {
+ printer->Print(vars, "$maybe_static$const $field_dv_ctype$ $lcclassname$__$lcname$__default_value$field_dv_ctype_suffix$ = $default_value$;\n");
+ }
}
- qsort(&field_indices[0], field_indices.size(), sizeof(NameIndex),
- compare_name_indices_by_name);
- printer->Print(vars, "static const unsigned $lcclassname$__field_indices_by_name[] = {\n");
+ }
+
+ if (descriptor_->field_count()) {
+ printer->Print(vars,
+ "static const ProtobufCFieldDescriptor $lcclassname$__field_descriptors[$n_fields$] =\n"
+ "{\n");
+ printer->Indent();
+
+ std::vector<const google::protobuf::FieldDescriptor*> sorted_fields;
for (int i = 0; i < descriptor_->field_count(); i++) {
- vars["index"] = SimpleItoa(field_indices[i].index);
- vars["name"] = field_indices[i].name;
- printer->Print(vars, " $index$, /* field[$index$] = $name$ */\n");
+ sorted_fields.push_back(descriptor_->field(i));
}
- printer->Print("};\n");
- }
+ qsort(&sorted_fields[0],
+ sorted_fields.size(),
+ sizeof(const google::protobuf::FieldDescriptor*),
+ compare_pfields_by_number);
+ for (auto field : sorted_fields) {
+ field_generators_.get(field).GenerateDescriptorInitializer(printer);
+ }
+ printer->Outdent();
+ printer->Print(vars, "};\n");
- // create range initializers
- std::vector<int> values;
- for (int i = 0; i < descriptor_->field_count(); i++) {
- values.push_back(sorted_fields[i]->number());
- }
- int n_ranges = WriteIntRanges(printer,
- descriptor_->field_count(), &values[0],
- vars["lcclassname"] + "__number_ranges");
+ if (!optimize_code_size) {
+ std::vector<NameIndex> field_indices;
+ for (unsigned i = 0; i < descriptor_->field_count(); i++) {
+ field_indices.push_back({ .index = i, .name = sorted_fields[i]->name() });
+ }
+ qsort(&field_indices[0],
+ field_indices.size(),
+ sizeof(NameIndex),
+ compare_name_indices_by_name);
+ printer->Print(vars, "static const unsigned $lcclassname$__field_indices_by_name[] = {\n");
+ for (int i = 0; i < descriptor_->field_count(); i++) {
+ vars["index"] = SimpleItoa(field_indices[i].index);
+ vars["name"] = field_indices[i].name;
+ printer->Print(vars, " $index$, /* field[$index$] = $name$ */\n");
+ }
+ printer->Print("};\n");
+ }
- vars["n_ranges"] = SimpleItoa(n_ranges);
- } else {
- /* MS compiler can't handle arrays with zero size and empty
- * initialization list. Furthermore it is an extension of GCC only but
- * not a standard. */
- vars["n_ranges"] = "0";
- printer->Print(vars,
- "#define $lcclassname$__field_descriptors NULL\n"
- "#define $lcclassname$__field_indices_by_name NULL\n"
- "#define $lcclassname$__number_ranges NULL\n");
+ // create range initializers
+ std::vector<int> values;
+ for (int i = 0; i < descriptor_->field_count(); i++) {
+ values.push_back(sorted_fields[i]->number());
}
+ int n_ranges = WriteIntRanges(printer,
+ descriptor_->field_count(),
+ &values[0],
+ vars["lcclassname"] + "__number_ranges");
+
+ vars["n_ranges"] = SimpleItoa(n_ranges);
+ } else {
+ /* MS compiler can't handle arrays with zero size and empty
+ * initialization list. Furthermore it is an extension of GCC only but
+ * not a standard. */
+ vars["n_ranges"] = "0";
+ printer->Print(vars,
+ "#define $lcclassname$__field_descriptors NULL\n"
+ "#define $lcclassname$__field_indices_by_name NULL\n"
+ "#define $lcclassname$__number_ranges NULL\n");
+ }
printer->Print(vars,
- "const ProtobufCMessageDescriptor $lcclassname$__descriptor =\n"
- "{\n"
- " PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC,\n");
+ "const ProtobufCMessageDescriptor $lcclassname$__descriptor =\n"
+ "{\n"
+ " PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC,\n");
if (optimize_code_size) {
printer->Print(" NULL,NULL,NULL,NULL, /* CODE_SIZE */\n");
} else {
printer->Print(vars,
- " \"$fullname$\",\n"
- " \"$shortname$\",\n"
- " \"$classname$\",\n"
- " \"$packagename$\",\n");
+ " \"$fullname$\",\n"
+ " \"$shortname$\",\n"
+ " \"$classname$\",\n"
+ " \"$packagename$\",\n");
}
printer->Print(vars,
- " sizeof($classname$),\n"
- " $n_fields$,\n"
- " $lcclassname$__field_descriptors,\n");
+ " sizeof($classname$),\n"
+ " $n_fields$,\n"
+ " $lcclassname$__field_descriptors,\n");
if (optimize_code_size) {
printer->Print(" NULL, /* CODE_SIZE */\n");
} else {
- printer->Print(vars,
- " $lcclassname$__field_indices_by_name,\n");
+ printer->Print(vars, " $lcclassname$__field_indices_by_name,\n");
}
printer->Print(vars,
- " $n_ranges$,"
- " $lcclassname$__number_ranges,\n");
+ " $n_ranges$,"
+ " $lcclassname$__number_ranges,\n");
if (gen_init) {
- printer->Print(vars,
- " (ProtobufCMessageInit) $lcclassname$__init,\n");
+ printer->Print(vars, " (ProtobufCMessageInit) $lcclassname$__init,\n");
} else {
- printer->Print(vars,
- " NULL, /* gen_init_helpers = false */\n");
+ printer->Print(vars, " NULL, /* gen_init_helpers = false */\n");
}
printer->Print(vars,
- " NULL,NULL,NULL /* reserved[123] */\n"
- "};\n");
+ " NULL,NULL,NULL /* reserved[123] */\n"
+ "};\n");
}
int MessageGenerator::GetOneofUnionOrder(const google::protobuf::FieldDescriptor* fd)
From c59b146aee2d97091ca2adeecd3f2741cb7f0082 Mon Sep 17 00:00:00 2001
From: Robert Edmonds <edmonds@users.noreply.github.com>
Date: Sat, 8 Feb 2025 21:10:37 -0500
Subject: [PATCH 08/11] compat: Use absl::string_view instead of
google::protobuf::internal::DescriptorStringView
Even though google::protobuf::internal::DescriptorStringView is exposed
in public protobuf headers, it's probably not a good idea to rely on an
"internal" typedef.
According to https://protobuf.dev/news/2024-10-02/#descriptor-apis:
v30 will update return types in descriptor (such as full_name) to be
absl::string_view.
So `absl::string_view` is probably the right type to use here.
---
protoc-gen-c/compat.h | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/protoc-gen-c/compat.h b/protoc-gen-c/compat.h
index fe8041b5..a70cef34 100644
--- a/protoc-gen-c/compat.h
+++ b/protoc-gen-c/compat.h
@@ -37,12 +37,16 @@
# define GOOGLE_LOG ABSL_LOG
#endif
+#if GOOGLE_PROTOBUF_VERSION >= 6030000
+# include <absl/strings/string_view.h>
+#endif
+
namespace protobuf_c {
namespace compat {
#if GOOGLE_PROTOBUF_VERSION >= 6030000
-typedef google::protobuf::internal::DescriptorStringView StringView;
+typedef absl::string_view StringView;
#else
typedef const std::string& StringView;
#endif
From 9c56038fd9d3cc2552c297457d7a66efe5cbd2c7 Mon Sep 17 00:00:00 2001
From: Robert Edmonds <edmonds@users.noreply.github.com>
Date: Sat, 8 Feb 2025 21:37:30 -0500
Subject: [PATCH 09/11] Makefile.am: Add compat.h to
protoc_gen_c_protoc_gen_c_SOURCES
---
Makefile.am | 1 +
1 file changed, 1 insertion(+)
diff --git a/Makefile.am b/Makefile.am
index 77aa9d99..26d19f16 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -102,6 +102,7 @@ protoc_gen_c_protoc_gen_c_SOURCES = \
protoc-gen-c/c_service.h \
protoc-gen-c/c_string_field.cc \
protoc-gen-c/c_string_field.h \
+ protoc-gen-c/compat.h \
protobuf-c/protobuf-c.pb.cc \
protobuf-c/protobuf-c.pb.h \
protoc-gen-c/main.cc
From 4ebd5cd8238d1f2ac6291b8c8925f34e16ce2123 Mon Sep 17 00:00:00 2001
From: Robert Edmonds <edmonds@users.noreply.github.com>
Date: Sat, 8 Feb 2025 21:38:07 -0500
Subject: [PATCH 10/11] compat: Conditionalize the include of <string>
It is only needed on older protobuf versions where absl::string_view is
not being used.
---
protoc-gen-c/compat.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/protoc-gen-c/compat.h b/protoc-gen-c/compat.h
index a70cef34..f6ace7cb 100644
--- a/protoc-gen-c/compat.h
+++ b/protoc-gen-c/compat.h
@@ -28,8 +28,6 @@
#ifndef PROTOBUF_C_PROTOC_GEN_C_COMPAT_H__
#define PROTOBUF_C_PROTOC_GEN_C_COMPAT_H__
-#include <string>
-
#if GOOGLE_PROTOBUF_VERSION >= 4022000
# define GOOGLE_ARRAYSIZE ABSL_ARRAYSIZE
# define GOOGLE_CHECK_EQ ABSL_CHECK_EQ
@@ -39,6 +37,8 @@
#if GOOGLE_PROTOBUF_VERSION >= 6030000
# include <absl/strings/string_view.h>
+#else
+# include <string>
#endif
namespace protobuf_c {
From 9a6b35e1e6956fb5cb044910448049b7a5339244 Mon Sep 17 00:00:00 2001
From: Robert Edmonds <edmonds@users.noreply.github.com>
Date: Sat, 8 Feb 2025 21:44:42 -0500
Subject: [PATCH 11/11] Cater to Microsoft Visual C++
Apparently MSVC doesn't support designated initializers for some reason.
---
protoc-gen-c/c_enum.cc | 9 ++-------
protoc-gen-c/c_message.cc | 2 +-
2 files changed, 3 insertions(+), 8 deletions(-)
diff --git a/protoc-gen-c/c_enum.cc b/protoc-gen-c/c_enum.cc
index c7839edd..1940ba9d 100644
--- a/protoc-gen-c/c_enum.cc
+++ b/protoc-gen-c/c_enum.cc
@@ -195,14 +195,9 @@ void EnumGenerator::GenerateEnumDescriptor(google::protobuf::io::Printer* printe
// Sort by name and value, dropping duplicate values if they appear later.
// TODO: use a c++ paradigm for this!
std::vector<ValueIndex> value_index;
- for (unsigned j = 0; j < descriptor_->value_count(); j++) {
+ for (int j = 0; j < descriptor_->value_count(); j++) {
const google::protobuf::EnumValueDescriptor *vd = descriptor_->value(j);
- value_index.push_back({
- .value = vd->number(),
- .index = j,
- .final_index = 0,
- .name = vd->name(),
- });
+ value_index.push_back({ vd->number(), (unsigned)j, 0, vd->name() });
}
qsort(&value_index[0],
value_index.size(),
diff --git a/protoc-gen-c/c_message.cc b/protoc-gen-c/c_message.cc
index 2a3b2a2f..94889179 100644
--- a/protoc-gen-c/c_message.cc
+++ b/protoc-gen-c/c_message.cc
@@ -581,7 +581,7 @@ GenerateMessageDescriptor(google::protobuf::io::Printer* printer, bool gen_init)
if (!optimize_code_size) {
std::vector<NameIndex> field_indices;
for (unsigned i = 0; i < descriptor_->field_count(); i++) {
- field_indices.push_back({ .index = i, .name = sorted_fields[i]->name() });
+ field_indices.push_back({ i, sorted_fields[i]->name() });
}
qsort(&field_indices[0],
field_indices.size(),
|