@@ -202,19 +202,6 @@ def read_file(filename):
202
202
return assign_ids (unique_messages , unique_modules , stable_data )
203
203
204
204
205
- def make_cpp_scoped_enum_decl (e : str , ut : str ) -> str :
206
- parts = e .split ("::" )
207
- enum = parts [- 1 ]
208
- if "(anonymous namespace)" in parts or "{anonymous}" in parts :
209
- raise Exception (
210
- f"Scoped enum { e } is inside an anonymous namespace and cannot be forward declared."
211
- )
212
- if len (parts ) > 1 :
213
- ns = "::" .join (parts [:- 1 ])
214
- return f"namespace { ns } {{ enum struct { enum } : { ut } ; }}"
215
- return f"enum struct { enum } : { ut } ;"
216
-
217
-
218
205
def make_cpp_catalog_defn (m : Message ) -> str :
219
206
return f"""/*
220
207
"{ m .text } "
@@ -234,16 +221,11 @@ def make_cpp_module_defn(m: Module) -> str:
234
221
}}"""
235
222
236
223
237
- def write_cpp (messages , modules , scoped_enums , extra_headers : list [str ], filename : str ):
224
+ def write_cpp (messages , modules , extra_headers : list [str ], filename : str ):
238
225
with open (filename , "w" ) as f :
239
226
f .write ("\n " .join (f'#include "{ h } "' for h in extra_headers ))
240
227
f .write ("\n #include <log/catalog/arguments.hpp>\n " )
241
228
f .write ("\n #include <log/catalog/catalog.hpp>\n \n " )
242
- scoped_enum_decls = [
243
- make_cpp_scoped_enum_decl (e , ut ) for e , ut in scoped_enums .items ()
244
- ]
245
- f .write ("\n " .join (scoped_enum_decls ))
246
- f .write ("\n \n " )
247
229
cpp_catalog_defns = [make_cpp_catalog_defn (m ) for m in messages ]
248
230
f .write ("\n " .join (cpp_catalog_defns ))
249
231
f .write ("\n \n " )
@@ -269,15 +251,18 @@ def fq_name(node):
269
251
index = Index .create ()
270
252
translation_unit = index .parse (filename )
271
253
272
- enums : dict [ str , dict ] = {}
254
+ enums = {}
273
255
for node in translation_unit .cursor .walk_preorder ():
274
256
if node .kind == CursorKind .ENUM_DECL :
275
- new_decl = {
276
- e .spelling : e .enum_value
277
- for e in node .walk_preorder ()
278
- if e .kind == CursorKind .ENUM_CONSTANT_DECL
279
- }
280
- enums [fq_name (node )] = enums .get (fq_name (node ), {}) | new_decl
257
+ enums .update (
258
+ {
259
+ fq_name (node ): {
260
+ e .spelling : e .enum_value
261
+ for e in node .walk_preorder ()
262
+ if e .kind == CursorKind .ENUM_CONSTANT_DECL
263
+ }
264
+ }
265
+ )
281
266
return enums
282
267
283
268
@@ -289,11 +274,11 @@ def write_json(
289
274
)
290
275
for m in stable_data .get ("messages" ):
291
276
j = m .to_json ()
292
- if j not in d ["messages" ]:
277
+ if not j in d ["messages" ]:
293
278
d ["messages" ].append (j )
294
279
for m in stable_data .get ("modules" ):
295
280
j = m .to_json ()
296
- if j not in d ["modules" ]:
281
+ if not j in d ["modules" ]:
297
282
d ["modules" ].append (j )
298
283
299
284
str_catalog = dict (** d , enums = dict ())
@@ -362,13 +347,9 @@ def serialize_modules(client_node: et.Element, modules: list[Module]):
362
347
363
348
364
349
def arg_type_encoding (arg ):
365
- string_re = re .compile (r"encode_(32|u32|64|u64|enum )<(.*)>" )
350
+ string_re = re .compile (r"encode_(32|u32|64|u64)<(.*)>" )
366
351
m = string_re .match (arg )
367
- if "enum" in m .group (1 ):
368
- args_re = re .compile (r"(.*), (.*)" )
369
- args_m = args_re .match (m .group (2 ))
370
- return (f"encode_{ m .group (1 )} " , args_m .group (1 ), args_m .group (2 ))
371
- return (f"encode_{ m .group (1 )} " , m .group (2 ), m .group (2 ))
352
+ return (f"encode_{ m .group (1 )} " , m .group (2 ))
372
353
373
354
374
355
def arg_printf_spec (arg : str ):
@@ -379,20 +360,9 @@ def arg_printf_spec(arg: str):
379
360
"encode_u64" : "%llu" ,
380
361
"float" : "%f" ,
381
362
"double" : "%f" ,
382
- "int" : "%d" ,
383
- "unsigned int" : "%u" ,
384
- "short" : "%d" ,
385
- "unsigned short" : "%u" ,
386
- "signed char" : "%d" ,
387
- "unsigned char" : "%u" ,
388
- "char" : "%c" ,
389
- "long" : "%ld" ,
390
- "unsigned long" : "%lu" ,
391
- "long long" : "%lld" ,
392
- "unsigned long long" : "%llu" ,
393
363
}
394
- enc , _ , ut = arg_type_encoding (arg )
395
- return printf_dict .get (ut , printf_dict .get (enc , "%d" ))
364
+ enc , t = arg_type_encoding (arg )
365
+ return printf_dict .get (t , printf_dict .get (enc , "%d" ))
396
366
397
367
398
368
def serialize_messages (
@@ -569,15 +539,8 @@ def main():
569
539
except Exception as e :
570
540
raise Exception (f"{ str (e )} from file { args .input } " )
571
541
572
- scoped_enums = {}
573
542
if args .cpp_output is not None :
574
- for m in messages :
575
- for i , arg_type in enumerate (m .args ):
576
- enc , enum , ut = arg_type_encoding (arg_type )
577
- if "enum" in enc :
578
- scoped_enums .update ({enum : ut })
579
-
580
- write_cpp (messages , modules , scoped_enums , args .cpp_headers , args .cpp_output )
543
+ write_cpp (messages , modules , args .cpp_headers , args .cpp_output )
581
544
582
545
stable_output = dict (messages = [], modules = [])
583
546
if not args .forget_old_ids :
@@ -600,13 +563,14 @@ def main():
600
563
}
601
564
for m in messages :
602
565
for i , arg_type in enumerate (m .args ):
603
- _ , enum , _ = arg_type_encoding (arg_type )
566
+ _ , enum = arg_type_encoding (arg_type )
604
567
if enum in enums :
605
568
m .enum_lookup = (i + 1 , enums [enum ][0 ])
606
569
except Exception as e :
607
570
print (
608
571
f"Couldn't extract enum info ({ str (e )} ), enum lookup will not be available"
609
572
)
573
+
610
574
else :
611
575
print ("XML output without C++ output: enum lookup will not be available" )
612
576
0 commit comments