use DFS to recursively search and expand includes in a single pass
This commit is contained in:
@@ -61,6 +61,36 @@ def find_include(file_name, inc_dirs):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def process_helper(in_fname, out_f, inc_dirs, replaced_includes):
|
||||||
|
"""
|
||||||
|
Helper function to DFS through include files and replace includes.
|
||||||
|
"""
|
||||||
|
print(f"DFS state: {replaced_includes}")
|
||||||
|
include_regex = re.compile(r"^ *`include +\"(.*)\"")
|
||||||
|
# slurp the input file.
|
||||||
|
# this avoids having a bunch of fds open during recursion
|
||||||
|
with open(in_fname, "r", encoding="utf-8") as in_file:
|
||||||
|
lines = in_file.readlines()
|
||||||
|
|
||||||
|
for num, line in enumerate(lines, 1):
|
||||||
|
match = re.match(include_regex, line)
|
||||||
|
if not match or match.group(1) == "uvm_macros.svh":
|
||||||
|
# copy the line as is
|
||||||
|
out_f.write(line)
|
||||||
|
continue
|
||||||
|
if match.group(1) in replaced_includes:
|
||||||
|
print_info("Skipping duplicate include")
|
||||||
|
continue
|
||||||
|
|
||||||
|
print_info(
|
||||||
|
f"Replacing includes for {match.group(1)}" f" at line {num}"
|
||||||
|
)
|
||||||
|
# search for include and replace
|
||||||
|
inc_file_name = find_include(match.group(1), inc_dirs)
|
||||||
|
replaced_includes.add(match.group(1))
|
||||||
|
process_helper(inc_file_name, out_f, inc_dirs, replaced_includes)
|
||||||
|
|
||||||
|
|
||||||
def process(in_fname, out_fname, inc_dirs=None):
|
def process(in_fname, out_fname, inc_dirs=None):
|
||||||
"""
|
"""
|
||||||
Replace include directives in a file with the full include file.
|
Replace include directives in a file with the full include file.
|
||||||
@@ -71,28 +101,8 @@ def process(in_fname, out_fname, inc_dirs=None):
|
|||||||
inc_dirs (list): list of directories to search for includes
|
inc_dirs (list): list of directories to search for includes
|
||||||
"""
|
"""
|
||||||
replaced_includes = set()
|
replaced_includes = set()
|
||||||
with open(in_fname, "r", encoding="utf-8") as in_file:
|
with open(out_fname, "w", encoding="utf-8") as out_file:
|
||||||
with open(out_fname, "w", encoding="utf-8") as out_file:
|
process_helper(in_fname, out_file, inc_dirs, replaced_includes)
|
||||||
# for each include found, search through all dirs
|
|
||||||
# and replace if found, error if not
|
|
||||||
for num, line in enumerate(in_file, 1):
|
|
||||||
match = re.match(r"^ *`include +\"(.*)\"", line)
|
|
||||||
if not match or match.group(1) == "uvm_macros.svh":
|
|
||||||
# copy the line as is
|
|
||||||
out_file.write(line)
|
|
||||||
continue
|
|
||||||
if match.group(1) in replaced_includes:
|
|
||||||
print_info("Skipping duplicate include")
|
|
||||||
continue
|
|
||||||
|
|
||||||
print_info(
|
|
||||||
f"Replacing includes for {match.group(1)}" f" at line {num}"
|
|
||||||
)
|
|
||||||
# search for include and replace
|
|
||||||
inc_file_name = find_include(match.group(1), inc_dirs)
|
|
||||||
with open(inc_file_name, "r", encoding="utf-8") as inc_file:
|
|
||||||
out_file.writelines(inc_file)
|
|
||||||
replaced_includes.add(match.group(1))
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|||||||
Reference in New Issue
Block a user