Skip to content

Commit 7e5befc

Browse files
wilhuffpaulb777
authored andcommitted
Isolate Firestore nanopb messages in C++ namespaces (#2046)
* Add C++ namespaces and use C++ linkage for nanopb-generated sources. * Regenerate nanopb sources as C++ * Add generated C++ nanopb sources to the podspec
1 parent 3e2d705 commit 7e5befc

37 files changed

+266
-106
lines changed

FirebaseFirestore.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Google Cloud Firestore is a NoSQL document database built for automatic scaling,
2525
s.source_files = [
2626
'Firestore/Source/**/*',
2727
'Firestore/Port/**/*',
28-
'Firestore/Protos/nanopb/**/*.[hc]',
28+
'Firestore/Protos/nanopb/**/*.{h,cc}',
2929
'Firestore/Protos/objc/**/*.[hm]',
3030
'Firestore/core/include/**/*.{h,cc,mm}',
3131
'Firestore/core/src/**/*.{h,cc,mm}',

Firestore/Protos/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ endforeach()
6767
foreach(root ${PROTO_FILE_ROOTS} ${WELL_KNOWN_PROTO_FILE_ROOTS})
6868
list(
6969
APPEND NANOPB_GENERATED_SOURCES
70-
${OUTPUT_DIR}/nanopb/${root}.nanopb.c
70+
${OUTPUT_DIR}/nanopb/${root}.nanopb.cc
7171
${OUTPUT_DIR}/nanopb/${root}.nanopb.h
7272
)
7373
endforeach()

Firestore/Protos/build_protos.py

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,14 @@ def run(self):
121121

122122
self.__run_generator(nanopb_out)
123123

124-
sources = collect_files(nanopb_out, '.nanopb.h', '.nanopb.c')
125-
post_process_files(sources, add_copyright, nanopb_rename_delete)
124+
sources = collect_files(nanopb_out, '.nanopb.h', '.nanopb.cc')
125+
post_process_files(
126+
sources,
127+
add_copyright,
128+
nanopb_add_namespaces,
129+
nanopb_remove_extern_c,
130+
nanopb_rename_delete
131+
)
126132

127133
def __run_generator(self, out_dir):
128134
"""Invokes protoc using the nanopb plugin."""
@@ -133,6 +139,7 @@ def __run_generator(self, out_dir):
133139

134140
nanopb_flags = ' '.join([
135141
'--extension=.nanopb',
142+
'--source-extension=.cc',
136143
'--no-timestamp',
137144
])
138145
cmd.append('--nanopb_out=%s:%s' % (nanopb_flags, out_dir))
@@ -286,6 +293,57 @@ def add_copyright(lines):
286293
return result
287294

288295

296+
def nanopb_add_namespaces(lines):
297+
"""Adds C++ namespaces to the lines.
298+
299+
Args:
300+
lines: The lines to fix.
301+
302+
Returns:
303+
The lines, fixed.
304+
"""
305+
result = []
306+
for line in lines:
307+
if '@@protoc_insertion_point(includes)' in line:
308+
result.append('namespace firebase {\n')
309+
result.append('namespace firestore {\n')
310+
result.append('\n')
311+
312+
if '@@protoc_insertion_point(eof)' in line:
313+
result.append('} // namespace firestore\n')
314+
result.append('} // namespace firebase\n')
315+
result.append('\n')
316+
317+
result.append(line)
318+
319+
return result
320+
321+
322+
def nanopb_remove_extern_c(lines):
323+
"""Removes extern "C" directives from nanopb code.
324+
325+
Args:
326+
lines: A nanobp-generated source file, split into lines.
327+
Returns:
328+
A list of strings, similar to the input but modified to remove extern "C".
329+
"""
330+
result = []
331+
state = 'initial'
332+
for line in lines:
333+
if state == 'initial':
334+
if '#ifdef __cplusplus' in line:
335+
state = 'in-ifdef'
336+
continue
337+
338+
result.append(line)
339+
340+
elif state == 'in-ifdef':
341+
if '#endif' in line:
342+
state = 'initial'
343+
344+
return result
345+
346+
289347
def nanopb_rename_delete(lines):
290348
"""Renames a delete symbol to delete_.
291349

Firestore/Protos/nanopb/firestore/local/maybe_document.nanopb.c renamed to Firestore/Protos/nanopb/firestore/local/maybe_document.nanopb.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
#include "maybe_document.nanopb.h"
2121

22+
namespace firebase {
23+
namespace firestore {
24+
2225
/* @@protoc_insertion_point(includes) */
2326
#if PB_PROTO_HEADER_VERSION != 30
2427
#error Regenerate this file with the current version of nanopb generator.
@@ -71,4 +74,7 @@ PB_STATIC_ASSERT((pb_membersize(firestore_client_NoDocument, read_time) < 256 &&
7174
#endif
7275

7376

77+
} // namespace firestore
78+
} // namespace firebase
79+
7480
/* @@protoc_insertion_point(eof) */

Firestore/Protos/nanopb/firestore/local/maybe_document.nanopb.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525

2626
#include "google/protobuf/timestamp.nanopb.h"
2727

28+
namespace firebase {
29+
namespace firestore {
30+
2831
/* @@protoc_insertion_point(includes) */
2932
#if PB_PROTO_HEADER_VERSION != 30
3033
#error Regenerate this file with the current version of nanopb generator.
3134
#endif
3235

33-
#ifdef __cplusplus
34-
extern "C" {
35-
#endif
3636

3737
/* Struct definitions */
3838
typedef struct _firestore_client_NoDocument {
@@ -96,9 +96,9 @@ extern const pb_field_t firestore_client_MaybeDocument_fields[5];
9696

9797
#endif
9898

99-
#ifdef __cplusplus
100-
} /* extern "C" */
101-
#endif
99+
} // namespace firestore
100+
} // namespace firebase
101+
102102
/* @@protoc_insertion_point(eof) */
103103

104104
#endif

Firestore/Protos/nanopb/firestore/local/mutation.nanopb.c renamed to Firestore/Protos/nanopb/firestore/local/mutation.nanopb.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
#include "mutation.nanopb.h"
2121

22+
namespace firebase {
23+
namespace firestore {
24+
2225
/* @@protoc_insertion_point(includes) */
2326
#if PB_PROTO_HEADER_VERSION != 30
2427
#error Regenerate this file with the current version of nanopb generator.
@@ -64,4 +67,7 @@ PB_STATIC_ASSERT((pb_membersize(firestore_client_WriteBatch, local_write_time) <
6467
#endif
6568

6669

70+
} // namespace firestore
71+
} // namespace firebase
72+
6773
/* @@protoc_insertion_point(eof) */

Firestore/Protos/nanopb/firestore/local/mutation.nanopb.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525

2626
#include "google/protobuf/timestamp.nanopb.h"
2727

28+
namespace firebase {
29+
namespace firestore {
30+
2831
/* @@protoc_insertion_point(includes) */
2932
#if PB_PROTO_HEADER_VERSION != 30
3033
#error Regenerate this file with the current version of nanopb generator.
3134
#endif
3235

33-
#ifdef __cplusplus
34-
extern "C" {
35-
#endif
3636

3737
/* Struct definitions */
3838
typedef struct _firestore_client_MutationQueue {
@@ -80,9 +80,9 @@ extern const pb_field_t firestore_client_WriteBatch_fields[4];
8080

8181
#endif
8282

83-
#ifdef __cplusplus
84-
} /* extern "C" */
85-
#endif
83+
} // namespace firestore
84+
} // namespace firebase
85+
8686
/* @@protoc_insertion_point(eof) */
8787

8888
#endif

Firestore/Protos/nanopb/firestore/local/target.nanopb.c renamed to Firestore/Protos/nanopb/firestore/local/target.nanopb.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
#include "target.nanopb.h"
2121

22+
namespace firebase {
23+
namespace firestore {
24+
2225
/* @@protoc_insertion_point(includes) */
2326
#if PB_PROTO_HEADER_VERSION != 30
2427
#error Regenerate this file with the current version of nanopb generator.
@@ -69,4 +72,7 @@ PB_STATIC_ASSERT((pb_membersize(firestore_client_Target, query) < 256 && pb_memb
6972
#endif
7073

7174

75+
} // namespace firestore
76+
} // namespace firebase
77+
7278
/* @@protoc_insertion_point(eof) */

Firestore/Protos/nanopb/firestore/local/target.nanopb.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525

2626
#include "google/protobuf/timestamp.nanopb.h"
2727

28+
namespace firebase {
29+
namespace firestore {
30+
2831
/* @@protoc_insertion_point(includes) */
2932
#if PB_PROTO_HEADER_VERSION != 30
3033
#error Regenerate this file with the current version of nanopb generator.
3134
#endif
3235

33-
#ifdef __cplusplus
34-
extern "C" {
35-
#endif
3636

3737
/* Struct definitions */
3838
typedef struct _firestore_client_Target {
@@ -92,9 +92,9 @@ extern const pb_field_t firestore_client_TargetGlobal_fields[5];
9292

9393
#endif
9494

95-
#ifdef __cplusplus
96-
} /* extern "C" */
97-
#endif
95+
} // namespace firestore
96+
} // namespace firebase
97+
9898
/* @@protoc_insertion_point(eof) */
9999

100100
#endif

Firestore/Protos/nanopb/google/api/annotations.nanopb.c renamed to Firestore/Protos/nanopb/google/api/annotations.nanopb.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
#include "annotations.nanopb.h"
2121

22+
namespace firebase {
23+
namespace firestore {
24+
2225
/* @@protoc_insertion_point(includes) */
2326
#if PB_PROTO_HEADER_VERSION != 30
2427
#error Regenerate this file with the current version of nanopb generator.
@@ -34,4 +37,7 @@
3437
#endif
3538

3639

40+
} // namespace firestore
41+
} // namespace firebase
42+
3743
/* @@protoc_insertion_point(eof) */

Firestore/Protos/nanopb/google/api/annotations.nanopb.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,22 @@
2323

2424
#include "google/api/http.nanopb.h"
2525

26+
namespace firebase {
27+
namespace firestore {
28+
2629
/* @@protoc_insertion_point(includes) */
2730
#if PB_PROTO_HEADER_VERSION != 30
2831
#error Regenerate this file with the current version of nanopb generator.
2932
#endif
3033

31-
#ifdef __cplusplus
32-
extern "C" {
33-
#endif
3434

3535
/* Extensions */
3636
/* Extension field google_api_http was skipped because only "optional"
3737
type of extension fields is currently supported. */
3838

39-
#ifdef __cplusplus
40-
} /* extern "C" */
41-
#endif
39+
} // namespace firestore
40+
} // namespace firebase
41+
4242
/* @@protoc_insertion_point(eof) */
4343

4444
#endif

Firestore/Protos/nanopb/google/api/http.nanopb.c renamed to Firestore/Protos/nanopb/google/api/http.nanopb.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
#include "http.nanopb.h"
2121

22+
namespace firebase {
23+
namespace firestore {
24+
2225
/* @@protoc_insertion_point(includes) */
2326
#if PB_PROTO_HEADER_VERSION != 30
2427
#error Regenerate this file with the current version of nanopb generator.
@@ -76,4 +79,7 @@ PB_STATIC_ASSERT((pb_membersize(google_api_HttpRule, custom) < 256), YOU_MUST_DE
7679
#endif
7780

7881

82+
} // namespace firestore
83+
} // namespace firebase
84+
7985
/* @@protoc_insertion_point(eof) */

Firestore/Protos/nanopb/google/api/http.nanopb.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
#define PB_GOOGLE_API_HTTP_NANOPB_H_INCLUDED
2222
#include <pb.h>
2323

24+
namespace firebase {
25+
namespace firestore {
26+
2427
/* @@protoc_insertion_point(includes) */
2528
#if PB_PROTO_HEADER_VERSION != 30
2629
#error Regenerate this file with the current version of nanopb generator.
2730
#endif
2831

29-
#ifdef __cplusplus
30-
extern "C" {
31-
#endif
3232

3333
/* Struct definitions */
3434
typedef struct _google_api_CustomHttpPattern {
@@ -104,9 +104,9 @@ extern const pb_field_t google_api_CustomHttpPattern_fields[3];
104104

105105
#endif
106106

107-
#ifdef __cplusplus
108-
} /* extern "C" */
109-
#endif
107+
} // namespace firestore
108+
} // namespace firebase
109+
110110
/* @@protoc_insertion_point(eof) */
111111

112112
#endif

Firestore/Protos/nanopb/google/firestore/v1beta1/common.nanopb.c renamed to Firestore/Protos/nanopb/google/firestore/v1beta1/common.nanopb.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
#include "common.nanopb.h"
2121

22+
namespace firebase {
23+
namespace firestore {
24+
2225
/* @@protoc_insertion_point(includes) */
2326
#if PB_PROTO_HEADER_VERSION != 30
2427
#error Regenerate this file with the current version of nanopb generator.
@@ -78,4 +81,7 @@ PB_STATIC_ASSERT((pb_membersize(google_firestore_v1beta1_Precondition, update_ti
7881
#endif
7982

8083

84+
} // namespace firestore
85+
} // namespace firebase
86+
8187
/* @@protoc_insertion_point(eof) */

Firestore/Protos/nanopb/google/firestore/v1beta1/common.nanopb.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525

2626
#include "google/protobuf/timestamp.nanopb.h"
2727

28+
namespace firebase {
29+
namespace firestore {
30+
2831
/* @@protoc_insertion_point(includes) */
2932
#if PB_PROTO_HEADER_VERSION != 30
3033
#error Regenerate this file with the current version of nanopb generator.
3134
#endif
3235

33-
#ifdef __cplusplus
34-
extern "C" {
35-
#endif
3636

3737
/* Struct definitions */
3838
typedef struct _google_firestore_v1beta1_DocumentMask {
@@ -117,9 +117,9 @@ extern const pb_field_t google_firestore_v1beta1_TransactionOptions_ReadOnly_fie
117117

118118
#endif
119119

120-
#ifdef __cplusplus
121-
} /* extern "C" */
122-
#endif
120+
} // namespace firestore
121+
} // namespace firebase
122+
123123
/* @@protoc_insertion_point(eof) */
124124

125125
#endif

0 commit comments

Comments
 (0)