gen-usecounters.py (2138B)
1 #!/usr/bin/env python 2 3 # This Source Code Form is subject to the terms of the Mozilla Public 4 # License, v. 2.0. If a copy of the MPL was not distributed with this 5 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 7 import os 8 import sys 9 10 sys.path.append(os.path.dirname(__file__)) 11 12 import usecounters 13 14 AUTOGENERATED_WARNING_COMMENT = ( 15 "/* THIS FILE IS AUTOGENERATED BY gen-usecounters.py - DO NOT EDIT */" 16 ) 17 18 19 def generate_list(f, counters): 20 def print_optional_macro_declare(name): 21 print( 22 """ 23 #ifndef %(name)s 24 #define %(name)s(interface_, name_) // nothing 25 #define DEFINED_%(name)s 26 #endif 27 """ 28 % {"name": name}, 29 file=f, 30 ) 31 32 def print_optional_macro_undeclare(name): 33 print( 34 """ 35 #ifdef DEFINED_%(name)s 36 #undef DEFINED_%(name)s 37 #undef %(name)s 38 #endif 39 """ 40 % {"name": name}, 41 file=f, 42 ) 43 44 print(AUTOGENERATED_WARNING_COMMENT, file=f) 45 46 print_optional_macro_declare("USE_COUNTER_DOM_METHOD") 47 print_optional_macro_declare("USE_COUNTER_DOM_ATTRIBUTE") 48 print_optional_macro_declare("USE_COUNTER_CUSTOM") 49 50 for counter in counters: 51 if counter["type"] == "method": 52 print( 53 "USE_COUNTER_DOM_METHOD(%s, %s)" 54 % (counter["interface_name"], counter["method_name"]), 55 file=f, 56 ) 57 elif counter["type"] == "attribute": 58 print( 59 "USE_COUNTER_DOM_ATTRIBUTE(%s, %s)" 60 % (counter["interface_name"], counter["attribute_name"]), 61 file=f, 62 ) 63 elif counter["type"] == "custom": 64 desc = counter["desc"].replace("\\", r"\\").replace('"', r"\"") 65 print('USE_COUNTER_CUSTOM(%s, "%s")' % (counter["name"], desc), file=f) 66 67 print_optional_macro_undeclare("USE_COUNTER_DOM_METHOD") 68 print_optional_macro_undeclare("USE_COUNTER_DOM_ATTRIBUTE") 69 print_optional_macro_undeclare("USE_COUNTER_CUSTOM") 70 71 72 def use_counter_list(output_header, conf_filename): 73 counters = usecounters.read_conf(conf_filename) 74 generate_list(output_header, counters)