8
8
9
9
10
10
def remove_c_comments_emptylines (text ):
11
- text = re .sub (r' /\*.*?\*/' , '' , text , flags = re .DOTALL ) # Remove multi-line comments
12
- text = re .sub (r' //.*' , '' , text ) # Remove single-line comments
13
- return re .sub (r' \n\s*\n+' , ' \n ' , text ) # Remove empty lines
11
+ text = re .sub (r" /\*.*?\*/" , "" , text , flags = re .DOTALL ) # Remove multi-line comments
12
+ text = re .sub (r" //.*" , "" , text ) # Remove single-line comments
13
+ return re .sub (r" \n\s*\n+" , " \n " , text ) # Remove empty lines
14
14
15
15
16
16
def remove_c_includes (lines ):
17
- return [line for line in lines if not re .match (r' ^\s*#include\s' , line )]
17
+ return [line for line in lines if not re .match (r" ^\s*#include\s" , line )]
18
18
19
19
20
20
def remove_special_defines (lines , defines ):
21
- return [line for line in lines if not any (f' #define { define } ' in line for define in defines )]
21
+ return [line for line in lines if not any (f" #define { define } " in line for define in defines )]
22
22
23
23
24
24
def apply_cffi_defines_syntax (lines ):
25
- return [re .sub (r' #\s*define\s+(\w+).*' , r' #define \1 ...' , line ) for line in lines ]
25
+ return [re .sub (r" #\s*define\s+(\w+).*" , r" #define \1 ..." , line ) for line in lines ]
26
26
27
27
28
28
def remove_c_ifdef (lines ):
@@ -34,15 +34,15 @@ def remove_c_ifdef(lines):
34
34
for line in lines :
35
35
stripped_line = line .rstrip ()
36
36
37
- if re .match (r' ^#\s*(if|el|endif)' , stripped_line ):
38
- stripped_line = stripped_line .replace (' ' , '' )
39
- ifdef_count += stripped_line .count (' #if' ) - stripped_line .count (' #endif' )
37
+ if re .match (r" ^#\s*(if|el|endif)" , stripped_line ):
38
+ stripped_line = stripped_line .replace (" " , "" )
39
+ ifdef_count += stripped_line .count (" #if" ) - stripped_line .count (" #endif" )
40
40
continue
41
41
42
42
if ifdef_count == 0 :
43
43
processed_lines .append (stripped_line )
44
44
elif ifdef_count < 0 and line != lines [- 1 ]:
45
- msg = ' Unbalanced #if/#endif preprocessor directives.'
45
+ msg = " Unbalanced #if/#endif preprocessor directives."
46
46
raise ValueError (msg )
47
47
48
48
return processed_lines
@@ -56,16 +56,16 @@ def concatenate_c_defines(lines):
56
56
for line in lines :
57
57
stripped_line = line .rstrip ()
58
58
59
- if (re .match (r' #\s*define' , stripped_line ) or in_define ) and stripped_line .endswith (' \\ ' ):
59
+ if (re .match (r" #\s*define" , stripped_line ) or in_define ) and stripped_line .endswith (" \\ " ):
60
60
in_define = True
61
61
buffer .append (
62
- re .sub (r' #\s*define' , ' #define' , stripped_line ).rstrip (' \\ ' ).strip ()
62
+ re .sub (r" #\s*define" , " #define" , stripped_line ).rstrip (" \\ " ).strip ()
63
63
) # Normalize #define and remove trailing backslash
64
64
continue # Skip the rest of the loop to avoid resetting the buffer
65
65
66
66
if in_define :
67
67
buffer .append (stripped_line )
68
- processed_lines .append (' ' .join (buffer ))
68
+ processed_lines .append (" " .join (buffer ))
69
69
buffer = [] # Reset the buffer for the next definition
70
70
in_define = False
71
71
continue # Skip the rest of the loop to avoid adding the line again
@@ -85,25 +85,25 @@ def remove_deprecated_functions(lines, deprecation):
85
85
for line in lines :
86
86
stripped_line = line .rstrip ()
87
87
88
- if re .match (r' #\s*define' , stripped_line ) or in_define :
89
- in_define = bool (stripped_line .endswith (' \\ ' ))
88
+ if re .match (r" #\s*define" , stripped_line ) or in_define :
89
+ in_define = bool (stripped_line .endswith (" \\ " ))
90
90
processed_lines .append (stripped_line )
91
91
continue
92
92
93
- if stripped_line .startswith (' struct' ) or re .match (r' typedef\s+struct' , stripped_line ) or in_struct :
93
+ if stripped_line .startswith (" struct" ) or re .match (r" typedef\s+struct" , stripped_line ) or in_struct :
94
94
in_struct = True
95
95
processed_lines .append (stripped_line )
96
- brace_count += stripped_line .count ('{' ) - stripped_line .count ('}' )
96
+ brace_count += stripped_line .count ("{" ) - stripped_line .count ("}" )
97
97
if brace_count == 0 : # End of struct block
98
98
in_struct = False
99
99
continue
100
100
101
101
buffer .append (stripped_line )
102
102
103
103
# Check for the end of a function declaration
104
- if stripped_line .endswith (';' ) and not in_struct :
104
+ if stripped_line .endswith (";" ) and not in_struct :
105
105
# Extend if not DEPRECATED
106
- if not any (d in ' ' .join (buffer ) for d in deprecation ):
106
+ if not any (d in " " .join (buffer ) for d in deprecation ):
107
107
processed_lines .extend (buffer )
108
108
buffer = [] # Reset the buffer for the next definition
109
109
@@ -119,14 +119,14 @@ def remove_function_attributes(lines, attributes):
119
119
for attribute , replacement in attributes .items ():
120
120
# Attributes can be functions with (...), so using regular expression
121
121
# Remove the definition
122
- if re .search (rf' #\s*define\s+{ attribute } (\(.*\))?\b' , stripped_line ):
122
+ if re .search (rf" #\s*define\s+{ attribute } (\(.*\))?\b" , stripped_line ):
123
123
stripped_line = None
124
124
break
125
125
126
- if re .search (rf' \b{ attribute } (\(.*\))?\b' , stripped_line ):
127
- stripped_line = re .sub (rf' \b{ attribute } (\(.*\))?' , f' { replacement } ' , stripped_line )
128
- stripped_line = stripped_line .replace (' ;' , ';' )
129
- stripped_line = stripped_line .replace (' ' , ' ' )
126
+ if re .search (rf" \b{ attribute } (\(.*\))?\b" , stripped_line ):
127
+ stripped_line = re .sub (rf" \b{ attribute } (\(.*\))?" , f" { replacement } " , stripped_line )
128
+ stripped_line = stripped_line .replace (" ;" , ";" )
129
+ stripped_line = stripped_line .replace (" " , " " )
130
130
131
131
if stripped_line :
132
132
processed_lines .append (stripped_line )
@@ -141,7 +141,7 @@ def remove_header_guard(lines, keywords):
141
141
stripped_line = line .rstrip ()
142
142
143
143
for keyword in keywords :
144
- if re .search (rf' #\s*define\s+{ keyword } .*_H\b' , stripped_line ):
144
+ if re .search (rf" #\s*define\s+{ keyword } .*_H\b" , stripped_line ):
145
145
continue
146
146
processed_lines .append (stripped_line )
147
147
@@ -157,12 +157,12 @@ def concatenate_c_struct(lines):
157
157
for line in lines :
158
158
stripped_line = line .strip ()
159
159
160
- if stripped_line .startswith (' struct' ) or re .match (r' typedef\s+struct' , stripped_line ) or in_struct :
160
+ if stripped_line .startswith (" struct" ) or re .match (r" typedef\s+struct" , stripped_line ) or in_struct :
161
161
in_struct = True
162
- brace_count += stripped_line .count ('{' ) - stripped_line .count ('}' )
162
+ brace_count += stripped_line .count ("{" ) - stripped_line .count ("}" )
163
163
buffer .append (stripped_line )
164
164
if brace_count == 0 : # End of struct block
165
- processed_lines .append (' ' .join (buffer ).strip ())
165
+ processed_lines .append (" " .join (buffer ).strip ())
166
166
buffer = [] # Reset the buffer for the next definition
167
167
in_struct = False
168
168
continue # Skip the rest of the loop to avoid adding the line again
@@ -173,72 +173,72 @@ def concatenate_c_struct(lines):
173
173
174
174
175
175
def make_header_cffi_compliant (src_header_dir , src_header , cffi_dir ):
176
- with open (os .path .join (src_header_dir , src_header ), encoding = ' utf-8' ) as f :
176
+ with open (os .path .join (src_header_dir , src_header ), encoding = " utf-8" ) as f :
177
177
text = remove_c_comments_emptylines (f .read ())
178
- lines = text .split (' \n ' )
178
+ lines = text .split (" \n " )
179
179
180
180
lines = remove_c_includes (lines )
181
181
lines = remove_c_ifdef (lines )
182
182
lines = concatenate_c_defines (lines )
183
- lines = remove_deprecated_functions (lines , [' DEPRECATED' ])
184
- lines = remove_header_guard (lines , [' SECP256K1' ])
183
+ lines = remove_deprecated_functions (lines , [" DEPRECATED" ])
184
+ lines = remove_header_guard (lines , [" SECP256K1" ])
185
185
lines = remove_function_attributes (
186
186
lines ,
187
187
{
188
- ' SECP256K1_API' : ' extern' ,
189
- ' SECP256K1_WARN_UNUSED_RESULT' : '' ,
190
- ' SECP256K1_DEPRECATED' : '' ,
191
- ' SECP256K1_ARG_NONNULL' : '' ,
188
+ " SECP256K1_API" : " extern" ,
189
+ " SECP256K1_WARN_UNUSED_RESULT" : "" ,
190
+ " SECP256K1_DEPRECATED" : "" ,
191
+ " SECP256K1_ARG_NONNULL" : "" ,
192
192
},
193
193
)
194
194
lines = remove_special_defines (
195
195
lines ,
196
196
[
197
197
# Deprecated flags
198
- ' SECP256K1_CONTEXT_VERIFY' ,
199
- ' SECP256K1_CONTEXT_SIGN' ,
200
- ' SECP256K1_FLAGS_BIT_CONTEXT_VERIFY' ,
201
- ' SECP256K1_FLAGS_BIT_CONTEXT_SIGN' ,
198
+ " SECP256K1_CONTEXT_VERIFY" ,
199
+ " SECP256K1_CONTEXT_SIGN" ,
200
+ " SECP256K1_FLAGS_BIT_CONTEXT_VERIFY" ,
201
+ " SECP256K1_FLAGS_BIT_CONTEXT_SIGN" ,
202
202
# Testing flags
203
- ' SECP256K1_CONTEXT_DECLASSIFY' ,
204
- ' SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY' ,
203
+ " SECP256K1_CONTEXT_DECLASSIFY" ,
204
+ " SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY" ,
205
205
# Not for direct use - That may not mean to remove them!
206
206
# 'SECP256K1_FLAGS_TYPE_MASK',
207
207
# 'SECP256K1_FLAGS_TYPE_CONTEXT',
208
208
# 'SECP256K1_FLAGS_TYPE_COMPRESSION',
209
209
# 'SECP256K1_FLAGS_BIT_COMPRESSION',
210
210
# Not supported
211
- ' SECP256K1_SCHNORRSIG_EXTRAPARAMS_MAGIC' ,
212
- ' SECP256K1_SCHNORRSIG_EXTRAPARAMS' ,
211
+ " SECP256K1_SCHNORRSIG_EXTRAPARAMS_MAGIC" ,
212
+ " SECP256K1_SCHNORRSIG_EXTRAPARAMS" ,
213
213
],
214
214
)
215
215
lines = apply_cffi_defines_syntax (lines )
216
216
217
- logging .info (' Writting: %s in %s' , src_header , cffi_dir )
217
+ logging .info (" Writting: %s in %s" , src_header , cffi_dir )
218
218
output_filename = os .path .join (cffi_dir , src_header )
219
- with open (output_filename , 'w' , encoding = ' utf-8' ) as f_out :
220
- f_out .write (' \n ' .join (lines ))
219
+ with open (output_filename , "w" , encoding = " utf-8" ) as f_out :
220
+ f_out .write (" \n " .join (lines ))
221
221
222
222
223
- if __name__ == ' __main__' :
224
- parser = argparse .ArgumentParser (description = ' Process a header file.' )
225
- parser .add_argument (' src_header_dir' , type = str , help = ' The path to the header file to be processed.' )
226
- parser .add_argument (' cffi_header' , type = str , help = ' The path where the compliant header will be written.' )
227
- parser .add_argument (' cffi_dir' , type = str , help = ' The path where the compliant header will be written.' , default = '.' )
223
+ if __name__ == " __main__" :
224
+ parser = argparse .ArgumentParser (description = " Process a header file." )
225
+ parser .add_argument (" src_header_dir" , type = str , help = " The path to the header file to be processed." )
226
+ parser .add_argument (" cffi_header" , type = str , help = " The path where the compliant header will be written." )
227
+ parser .add_argument (" cffi_dir" , type = str , help = " The path where the compliant header will be written." , default = "." )
228
228
229
229
args = parser .parse_args ()
230
230
231
231
# Verify args are valid
232
232
if not os .path .isdir (args .src_header_dir ):
233
- logging .error (' Error: Directory: %s not found.' , args .src_header_dir )
233
+ logging .error (" Error: Directory: %s not found." , args .src_header_dir )
234
234
sys .exit (1 )
235
235
236
236
if not os .path .isdir (args .cffi_dir ):
237
- logging .error (' Error: Directory: %s not found.' , args .cffi_dir )
237
+ logging .error (" Error: Directory: %s not found." , args .cffi_dir )
238
238
sys .exit (1 )
239
239
240
240
if not os .path .isfile (os .path .join (args .src_header_dir , args .cffi_header )):
241
- logging .error (' Error: %s not found in %s.' , args .cffi_header , args .src_header_dir )
241
+ logging .error (" Error: %s not found in %s." , args .cffi_header , args .src_header_dir )
242
242
sys .exit (1 )
243
243
244
244
make_header_cffi_compliant (args .src_header_dir , args .cffi_header , args .cffi_dir )
0 commit comments