|
2 | 2 | # -*- coding: utf-8 -*-
|
3 | 3 |
|
4 | 4 | import sys
|
5 |
| -from pathlib import Path |
6 | 5 |
|
7 | 6 | if len(sys.argv) < 2:
|
8 | 7 | print("Invalid usage of header_guards.py, it should be called with a path to one or multiple files.")
|
|
13 | 12 |
|
14 | 13 | for file in sys.argv[1:]:
|
15 | 14 | header_start = -1
|
16 |
| - HEADER_CHECK_OFFSET = -1 |
| 15 | + header_end = -1 |
17 | 16 |
|
18 | 17 | with open(file.strip(), "rt", encoding="utf-8", newline="\n") as f:
|
19 | 18 | lines = f.readlines()
|
|
28 | 27 | if sline.startswith("/**********"): # Godot header starts this way.
|
29 | 28 | header_start = idx
|
30 | 29 | else:
|
31 |
| - HEADER_CHECK_OFFSET = 0 # There is no Godot header. |
| 30 | + header_end = 0 # There is no Godot header. |
32 | 31 | break
|
33 | 32 | else:
|
34 |
| - if not sline.startswith("*") and not sline.startswith("/*"): # Not in the Godot header anymore. |
35 |
| - HEADER_CHECK_OFFSET = idx + 1 # The include should be two lines below the Godot header. |
| 33 | + if not sline.startswith(("*", "/*")): # Not in the Godot header anymore. |
| 34 | + header_end = idx + 1 # The guard should be two lines below the Godot header. |
36 | 35 | break
|
37 | 36 |
|
38 |
| - if HEADER_CHECK_OFFSET < 0: |
| 37 | + if (HEADER_CHECK_OFFSET := header_end) < 0 or HEADER_CHECK_OFFSET >= len(lines): |
39 | 38 | invalid.append(file)
|
40 | 39 | continue
|
41 | 40 |
|
| 41 | + if lines[HEADER_CHECK_OFFSET].startswith("#pragma once"): |
| 42 | + continue |
| 43 | + |
| 44 | + # Might be using legacy header guards. |
42 | 45 | HEADER_BEGIN_OFFSET = HEADER_CHECK_OFFSET + 1
|
43 | 46 | HEADER_END_OFFSET = len(lines) - 1
|
44 | 47 |
|
45 | 48 | if HEADER_BEGIN_OFFSET >= HEADER_END_OFFSET:
|
46 | 49 | invalid.append(file)
|
47 | 50 | continue
|
48 | 51 |
|
49 |
| - split = file.split("/") # Already in posix-format. |
50 |
| - |
51 |
| - prefix = "" |
52 |
| - if split[0] == "modules" and split[-1] == "register_types.h": |
53 |
| - prefix = f"{split[1]}_" # Name of module. |
54 |
| - elif split[0] == "platform" and (file.endswith("api/api.h") or "/export/" in file): |
55 |
| - prefix = f"{split[1]}_" # Name of platform. |
56 |
| - elif file.startswith("modules/mono/utils") and "mono" not in split[-1]: |
57 |
| - prefix = "MONO_" |
58 |
| - elif file == "servers/rendering/storage/utilities.h": |
59 |
| - prefix = "RENDERER_" |
60 |
| - |
61 |
| - suffix = "" |
62 |
| - if "dummy" in file and "dummy" not in split[-1]: |
63 |
| - suffix = "_DUMMY" |
64 |
| - elif "gles3" in file and "gles3" not in split[-1]: |
65 |
| - suffix = "_GLES3" |
66 |
| - elif "renderer_rd" in file and "rd" not in split[-1]: |
67 |
| - suffix = "_RD" |
68 |
| - elif split[-1] == "ustring.h": |
69 |
| - suffix = "_GODOT" |
70 |
| - |
71 |
| - name = (f"{prefix}{Path(file).stem}{suffix}{Path(file).suffix}".upper() |
72 |
| - .replace(".", "_").replace("-", "_").replace(" ", "_")) # fmt: skip |
73 |
| - |
74 |
| - HEADER_CHECK = f"#ifndef {name}\n" |
75 |
| - HEADER_BEGIN = f"#define {name}\n" |
76 |
| - HEADER_END = f"#endif // {name}\n" |
77 |
| - |
78 |
| - if ( |
79 |
| - lines[HEADER_CHECK_OFFSET] == HEADER_CHECK |
80 |
| - and lines[HEADER_BEGIN_OFFSET] == HEADER_BEGIN |
81 |
| - and lines[HEADER_END_OFFSET] == HEADER_END |
82 |
| - ): |
83 |
| - continue |
84 |
| - |
85 |
| - # Guards might exist but with the wrong names. |
86 | 52 | if (
|
87 | 53 | lines[HEADER_CHECK_OFFSET].startswith("#ifndef")
|
88 | 54 | and lines[HEADER_BEGIN_OFFSET].startswith("#define")
|
89 | 55 | and lines[HEADER_END_OFFSET].startswith("#endif")
|
90 | 56 | ):
|
91 |
| - lines[HEADER_CHECK_OFFSET] = HEADER_CHECK |
92 |
| - lines[HEADER_BEGIN_OFFSET] = HEADER_BEGIN |
93 |
| - lines[HEADER_END_OFFSET] = HEADER_END |
| 57 | + lines[HEADER_CHECK_OFFSET] = "#pragma once" |
| 58 | + lines[HEADER_BEGIN_OFFSET] = "\n" |
| 59 | + lines.pop() |
94 | 60 | with open(file, "wt", encoding="utf-8", newline="\n") as f:
|
95 | 61 | f.writelines(lines)
|
96 | 62 | changed.append(file)
|
97 | 63 | continue
|
98 | 64 |
|
99 |
| - header_check = -1 |
100 |
| - header_begin = -1 |
101 |
| - header_end = -1 |
102 |
| - pragma_once = -1 |
103 |
| - objc = False |
104 |
| - |
105 |
| - for idx, line in enumerate(lines): |
106 |
| - if line.startswith("// #import"): # Some dummy obj-c files only have commented out import lines. |
107 |
| - objc = True |
108 |
| - break |
109 |
| - if not line.startswith("#"): |
110 |
| - continue |
111 |
| - elif line.startswith("#ifndef") and header_check == -1: |
112 |
| - header_check = idx |
113 |
| - elif line.startswith("#define") and header_begin == -1: |
114 |
| - header_begin = idx |
115 |
| - elif line.startswith("#endif") and header_end == -1: |
116 |
| - header_end = idx |
117 |
| - elif line.startswith("#pragma once"): |
118 |
| - pragma_once = idx |
| 65 | + # Verify `#pragma once` doesn't exist at invalid location. |
| 66 | + misplaced = False |
| 67 | + for line in lines: |
| 68 | + if line.startswith("#pragma once"): |
| 69 | + misplaced = True |
119 | 70 | break
|
120 |
| - elif line.startswith("#import"): |
121 |
| - objc = True |
122 |
| - break |
123 |
| - |
124 |
| - if objc: |
125 |
| - continue |
126 |
| - |
127 |
| - if pragma_once != -1: |
128 |
| - lines.pop(pragma_once) |
129 |
| - lines.insert(HEADER_CHECK_OFFSET, HEADER_CHECK) |
130 |
| - lines.insert(HEADER_BEGIN_OFFSET, HEADER_BEGIN) |
131 |
| - lines.append("\n") |
132 |
| - lines.append(HEADER_END) |
133 |
| - with open(file, "wt", encoding="utf-8", newline="\n") as f: |
134 |
| - f.writelines(lines) |
135 |
| - changed.append(file) |
136 |
| - continue |
137 | 71 |
|
138 |
| - if header_check == -1 and header_begin == -1 and header_end == -1: |
139 |
| - # Guards simply didn't exist |
140 |
| - lines.insert(HEADER_CHECK_OFFSET, HEADER_CHECK) |
141 |
| - lines.insert(HEADER_BEGIN_OFFSET, HEADER_BEGIN) |
142 |
| - lines.append("\n") |
143 |
| - lines.append(HEADER_END) |
144 |
| - with open(file, "wt", encoding="utf-8", newline="\n") as f: |
145 |
| - f.writelines(lines) |
146 |
| - changed.append(file) |
| 72 | + if misplaced: |
| 73 | + invalid.append(file) |
147 | 74 | continue
|
148 | 75 |
|
149 |
| - if header_check != -1 and header_begin != -1 and header_end != -1: |
150 |
| - # All prepends "found", see if we can salvage this. |
151 |
| - if header_check == header_begin - 1 and header_begin < header_end: |
152 |
| - lines.pop(header_check) |
153 |
| - lines.pop(header_begin - 1) |
154 |
| - lines.pop(header_end - 2) |
155 |
| - if lines[header_end - 3] == "\n": |
156 |
| - lines.pop(header_end - 3) |
157 |
| - lines.insert(HEADER_CHECK_OFFSET, HEADER_CHECK) |
158 |
| - lines.insert(HEADER_BEGIN_OFFSET, HEADER_BEGIN) |
159 |
| - lines.append("\n") |
160 |
| - lines.append(HEADER_END) |
161 |
| - with open(file, "wt", encoding="utf-8", newline="\n") as f: |
162 |
| - f.writelines(lines) |
163 |
| - changed.append(file) |
164 |
| - continue |
165 |
| - |
166 |
| - invalid.append(file) |
| 76 | + # Assume that we're simply missing a guard entirely. |
| 77 | + lines.insert(HEADER_CHECK_OFFSET, "#pragma once\n\n") |
| 78 | + with open(file, "wt", encoding="utf-8", newline="\n") as f: |
| 79 | + f.writelines(lines) |
| 80 | + changed.append(file) |
167 | 81 |
|
168 | 82 | if changed:
|
169 | 83 | for file in changed:
|
|
0 commit comments