Skip to content

Commit 78572c9

Browse files
henrymkwem-eight
authored andcommitted
fix redefinition issues with process_file, simplify logic
1 parent 336d717 commit 78572c9

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

tools/decompctx.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@
2121
include_dirs: List[str] = [] # Set with -I flag
2222

2323
include_pattern = re.compile(r'^#\s*include\s*[<"](.+?)[>"]')
24-
guard_pattern = re.compile(r"^#\s*ifndef\s+(.*)$")
25-
once_pattern = re.compile(r"^#\s*pragma\s+once$")
2624

2725
defines = set()
2826
deps = []
27+
failed_includes = False
2928

3029

3130
def import_h_file(in_file: str, r_path: str) -> str:
@@ -38,6 +37,8 @@ def import_h_file(in_file: str, r_path: str) -> str:
3837
return import_c_file(inc_path)
3938
else:
4039
print("Failed to locate", in_file)
40+
global failed_includes
41+
failed_includes = True
4142
return ""
4243

4344

@@ -56,21 +57,13 @@ def import_c_file(in_file: str) -> str:
5657

5758

5859
def process_file(in_file: str, lines: List[str]) -> str:
60+
if in_file in defines:
61+
return ""
62+
defines.add(in_file)
63+
print(f"Processing {in_file}")
64+
5965
out_text = ""
60-
for idx, line in enumerate(lines):
61-
if idx == 0:
62-
guard_match = guard_pattern.match(line.strip())
63-
if guard_match:
64-
if guard_match[1] in defines:
65-
break
66-
defines.add(guard_match[1])
67-
else:
68-
once_match = once_pattern.match(line.strip())
69-
if once_match:
70-
if in_file in defines:
71-
break
72-
defines.add(in_file)
73-
print("Processing file", in_file)
66+
for idx, line in enumerate(lines):
7467
include_match = include_pattern.match(line.strip())
7568
if include_match and not include_match[1].endswith(".s"):
7669
out_text += f'/* "{in_file}" line {idx} "{include_match[1]}" */\n'
@@ -119,9 +112,15 @@ def main():
119112
include_dirs = args.include
120113
output = import_c_file(args.c_file)
121114

115+
if failed_includes:
116+
print(f"Unable to generate {args.output}. Please verify includes are correct.")
117+
exit(1)
118+
122119
with open(os.path.join(root_dir, args.output), "w", encoding="utf-8") as f:
123120
f.write(output)
124121

122+
print(f"Successfully created {args.output}")
123+
125124
if args.depfile:
126125
with open(os.path.join(root_dir, args.depfile), "w", encoding="utf-8") as f:
127126
f.write(sanitize_path(args.output) + ":")

0 commit comments

Comments
 (0)