@@ -2079,6 +2079,23 @@ def replace_vars(line: str):
2079
2079
else :
2080
2080
return line_res
2081
2081
2082
+ def expand_func_macro (def_name : str , def_value : tuple [str , str ]):
2083
+ def_args , sub = def_value
2084
+ def_args = def_args .split ("," )
2085
+ regex = re .compile (rf"\b{ def_name } \s*\({ ',' .join (['(.*)' ]* len (def_args ))} \)" )
2086
+
2087
+ for i , arg in enumerate (def_args , start = 1 ):
2088
+ sub = re .sub (rf"\b({ arg .strip ()} )\b" , rf"\\ { i } " , sub )
2089
+
2090
+ return regex , sub
2091
+
2092
+ def append_multiline_macro (def_value : str | tuple , line : str ):
2093
+ if isinstance (def_value , tuple ):
2094
+ def_args , def_value = def_value
2095
+ def_value += line
2096
+ return (def_args , def_value )
2097
+ return def_value + line
2098
+
2082
2099
if pp_defs is None :
2083
2100
pp_defs = {}
2084
2101
if include_dirs is None :
@@ -2097,11 +2114,13 @@ def replace_vars(line: str):
2097
2114
# Handle multiline macro continuation
2098
2115
if def_cont_name is not None :
2099
2116
output_file .append ("" )
2100
- if line .rstrip ()[- 1 ] != "\\ " :
2101
- defs_tmp [def_cont_name ] += line .strip ()
2117
+ is_multiline = line .strip ()[- 1 ] != "\\ "
2118
+ line_to_append = line .strip () if is_multiline else line [0 :- 1 ].strip ()
2119
+ defs_tmp [def_cont_name ] = append_multiline_macro (
2120
+ defs_tmp [def_cont_name ], line_to_append
2121
+ )
2122
+ if is_multiline :
2102
2123
def_cont_name = None
2103
- else :
2104
- defs_tmp [def_cont_name ] += line [0 :- 1 ].strip ()
2105
2124
continue
2106
2125
# Handle conditional statements
2107
2126
match = FRegex .PP_REGEX .match (line )
@@ -2110,14 +2129,14 @@ def replace_vars(line: str):
2110
2129
def_name = None
2111
2130
if_start = False
2112
2131
# Opening conditional statements
2113
- if match .group (1 ) == "if " :
2132
+ if match .group (1 ). lower () == "if " :
2114
2133
is_path = eval_pp_if (line [match .end (1 ) :], defs_tmp )
2115
2134
if_start = True
2116
- elif match .group (1 ) == "ifdef" :
2135
+ elif match .group (1 ). lower () == "ifdef" :
2117
2136
if_start = True
2118
2137
def_name = line [match .end (0 ) :].strip ()
2119
2138
is_path = def_name in defs_tmp
2120
- elif match .group (1 ) == "ifndef" :
2139
+ elif match .group (1 ). lower () == "ifndef" :
2121
2140
if_start = True
2122
2141
def_name = line [match .end (0 ) :].strip ()
2123
2142
is_path = not (def_name in defs_tmp )
@@ -2135,7 +2154,7 @@ def replace_vars(line: str):
2135
2154
inc_start = False
2136
2155
exc_start = False
2137
2156
exc_continue = False
2138
- if match .group (1 ) == "elif" :
2157
+ if match .group (1 ). lower () == "elif" :
2139
2158
if (not pp_stack_group ) or (pp_stack_group [- 1 ][0 ] != len (pp_stack )):
2140
2159
# First elif statement for this elif group
2141
2160
if pp_stack [- 1 ][0 ] < 0 :
@@ -2155,7 +2174,7 @@ def replace_vars(line: str):
2155
2174
inc_start = True
2156
2175
else :
2157
2176
exc_start = True
2158
- elif match .group (1 ) == "else" :
2177
+ elif match .group (1 ). lower () == "else" :
2159
2178
if pp_stack [- 1 ][0 ] < 0 :
2160
2179
pp_stack [- 1 ][0 ] = i + 1
2161
2180
exc_start = True
@@ -2171,7 +2190,7 @@ def replace_vars(line: str):
2171
2190
pp_skips .append (pp_stack .pop ())
2172
2191
pp_stack .append ([- 1 , - 1 ])
2173
2192
inc_start = True
2174
- elif match .group (1 ) == "endif" :
2193
+ elif match .group (1 ). lower () == "endif" :
2175
2194
if pp_stack_group and (pp_stack_group [- 1 ][0 ] == len (pp_stack )):
2176
2195
pp_stack_group .pop ()
2177
2196
if pp_stack [- 1 ][0 ] < 0 :
@@ -2209,12 +2228,18 @@ def replace_vars(line: str):
2209
2228
if eq_ind >= 0 :
2210
2229
# Handle multiline macros
2211
2230
if line .rstrip ()[- 1 ] == "\\ " :
2212
- defs_tmp [ def_name ] = line [match .end (0 ) + eq_ind : - 1 ].strip ()
2231
+ def_value = line [match .end (0 ) + eq_ind : - 1 ].strip ()
2213
2232
def_cont_name = def_name
2214
2233
else :
2215
- defs_tmp [ def_name ] = line [match .end (0 ) + eq_ind :].strip ()
2234
+ def_value = line [match .end (0 ) + eq_ind :].strip ()
2216
2235
else :
2217
- defs_tmp [def_name ] = "True"
2236
+ def_value = "True"
2237
+
2238
+ # are there arguments to parse?
2239
+ if match .group (3 ):
2240
+ def_value = (match .group (4 ), def_value )
2241
+
2242
+ defs_tmp [def_name ] = def_value
2218
2243
elif (match .group (1 ) == "undef" ) and (def_name in defs_tmp ):
2219
2244
defs_tmp .pop (def_name , None )
2220
2245
log .debug (f"{ line .strip ()} !!! Define statement({ i + 1 } )" )
@@ -2265,8 +2290,15 @@ def replace_vars(line: str):
2265
2290
continue
2266
2291
def_regex = def_regexes .get (def_tmp )
2267
2292
if def_regex is None :
2268
- def_regex = re .compile (rf"\b{ def_tmp } \b" )
2293
+ if isinstance (value , tuple ):
2294
+ def_regex = expand_func_macro (def_tmp , value )
2295
+ else :
2296
+ def_regex = re .compile (rf"\b{ def_tmp } \b" )
2269
2297
def_regexes [def_tmp ] = def_regex
2298
+
2299
+ if isinstance (def_regex , tuple ):
2300
+ def_regex , value = def_regex
2301
+
2270
2302
line_new , nsubs = def_regex .subn (value , line )
2271
2303
if nsubs > 0 :
2272
2304
log .debug (
0 commit comments