@@ -2079,6 +2079,23 @@ def replace_vars(line: str):
20792079 else :
20802080 return line_res
20812081
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+
20822099 if pp_defs is None :
20832100 pp_defs = {}
20842101 if include_dirs is None :
@@ -2097,11 +2114,13 @@ def replace_vars(line: str):
20972114 # Handle multiline macro continuation
20982115 if def_cont_name is not None :
20992116 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 :
21022123 def_cont_name = None
2103- else :
2104- defs_tmp [def_cont_name ] += line [0 :- 1 ].strip ()
21052124 continue
21062125 # Handle conditional statements
21072126 match = FRegex .PP_REGEX .match (line )
@@ -2110,14 +2129,14 @@ def replace_vars(line: str):
21102129 def_name = None
21112130 if_start = False
21122131 # Opening conditional statements
2113- if match .group (1 ) == "if " :
2132+ if match .group (1 ). lower () == "if " :
21142133 is_path = eval_pp_if (line [match .end (1 ) :], defs_tmp )
21152134 if_start = True
2116- elif match .group (1 ) == "ifdef" :
2135+ elif match .group (1 ). lower () == "ifdef" :
21172136 if_start = True
21182137 def_name = line [match .end (0 ) :].strip ()
21192138 is_path = def_name in defs_tmp
2120- elif match .group (1 ) == "ifndef" :
2139+ elif match .group (1 ). lower () == "ifndef" :
21212140 if_start = True
21222141 def_name = line [match .end (0 ) :].strip ()
21232142 is_path = not (def_name in defs_tmp )
@@ -2135,7 +2154,7 @@ def replace_vars(line: str):
21352154 inc_start = False
21362155 exc_start = False
21372156 exc_continue = False
2138- if match .group (1 ) == "elif" :
2157+ if match .group (1 ). lower () == "elif" :
21392158 if (not pp_stack_group ) or (pp_stack_group [- 1 ][0 ] != len (pp_stack )):
21402159 # First elif statement for this elif group
21412160 if pp_stack [- 1 ][0 ] < 0 :
@@ -2155,7 +2174,7 @@ def replace_vars(line: str):
21552174 inc_start = True
21562175 else :
21572176 exc_start = True
2158- elif match .group (1 ) == "else" :
2177+ elif match .group (1 ). lower () == "else" :
21592178 if pp_stack [- 1 ][0 ] < 0 :
21602179 pp_stack [- 1 ][0 ] = i + 1
21612180 exc_start = True
@@ -2171,7 +2190,7 @@ def replace_vars(line: str):
21712190 pp_skips .append (pp_stack .pop ())
21722191 pp_stack .append ([- 1 , - 1 ])
21732192 inc_start = True
2174- elif match .group (1 ) == "endif" :
2193+ elif match .group (1 ). lower () == "endif" :
21752194 if pp_stack_group and (pp_stack_group [- 1 ][0 ] == len (pp_stack )):
21762195 pp_stack_group .pop ()
21772196 if pp_stack [- 1 ][0 ] < 0 :
@@ -2209,12 +2228,18 @@ def replace_vars(line: str):
22092228 if eq_ind >= 0 :
22102229 # Handle multiline macros
22112230 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 ()
22132232 def_cont_name = def_name
22142233 else :
2215- defs_tmp [ def_name ] = line [match .end (0 ) + eq_ind :].strip ()
2234+ def_value = line [match .end (0 ) + eq_ind :].strip ()
22162235 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
22182243 elif (match .group (1 ) == "undef" ) and (def_name in defs_tmp ):
22192244 defs_tmp .pop (def_name , None )
22202245 log .debug (f"{ line .strip ()} !!! Define statement({ i + 1 } )" )
@@ -2265,8 +2290,15 @@ def replace_vars(line: str):
22652290 continue
22662291 def_regex = def_regexes .get (def_tmp )
22672292 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" )
22692297 def_regexes [def_tmp ] = def_regex
2298+
2299+ if isinstance (def_regex , tuple ):
2300+ def_regex , value = def_regex
2301+
22702302 line_new , nsubs = def_regex .subn (value , line )
22712303 if nsubs > 0 :
22722304 log .debug (
0 commit comments