1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
#!/usr/bin/env python3
# libmisc/wrap-cc - Wrapper around GCC to enhance the preprocessor
#
# Copyright (C) 2025 Luke T. Shumaker <lukeshu@lukeshu.com>
# SPDX-License-Identifier: AGPL-3.0-or-later
import os
import re
import subprocess
import sys
import typing
def scan_tuple(
text: str, beg: int, on_part: typing.Callable[[str], None] | None = None
) -> int:
assert text[beg] == "("
pos = beg + 1
arg_start = pos
parens = 1
instring = False
while parens:
c = text[pos]
if instring:
match c:
case "\\":
pos += 1
case '"':
instring = False
else:
match c:
case "(":
parens += 1
case ")":
parens -= 1
if on_part and parens == 0 and text[beg + 1 : pos].strip():
on_part(text[arg_start:pos])
case ",":
if on_part and parens == 1:
on_part(text[arg_start:pos])
arg_start = pos + 1
case '"':
instring = True
pos += 1
assert text[pos - 1] == ")"
return pos - 1
def unquote(cstr: str) -> str:
assert len(cstr) >= 2 and cstr[0] == '"' and cstr[-1] == '"'
cstr = cstr[1:-1]
out = ""
while cstr:
if cstr[0] == "\\":
match cstr[1]:
case "n":
out += "\n"
cstr = cstr[2:]
case "\\":
out += "\\"
cstr = cstr[2:]
case '"':
out += '"'
cstr = cstr[2:]
else:
out += cstr[0]
cstr = cstr[1:]
return out
def preprocess(all_args: list[str]) -> typing.NoReturn:
# argparse #################################################################
_args = all_args
def shift(n: int) -> list[str]:
nonlocal _args
ret = _args[:n]
_args = _args[n:]
return ret
arg0 = shift(1)[0]
common_flags: list[str] = []
output_flags: list[str] = []
positional: list[str] = []
while _args:
if len(_args[0]) > 2 and _args[0][0] == "-" and _args[0][1] in "IDU":
_args = [_args[0][:2], _args[0][2:], *_args[1:]]
match _args[0]:
# Mode
case "-E" | "-quiet":
common_flags += shift(1)
case "-lang-asm":
os.execvp(all_args[0], all_args)
# Search path
case "-I" | "-imultilib" | "-isystem":
common_flags += shift(2)
# Define/Undefine
case "-D" | "-U":
common_flags += shift(2)
# Optimization
case "-O0" | "-O1" | "-O2" | "-O3" | "-Os" | "-Ofast" | "-Og" | "-Oz":
common_flags += shift(1)
case "-g":
common_flags += shift(1)
# Output files
case "-MD" | "-MF" | "-MT" | "-dumpbase" | "-dumpbase-ext":
output_flags += shift(2)
case "-o":
output_flags += shift(2)
# Other
case _:
if _args[0].startswith("-"):
if _args[0].startswith("-std="):
common_flags += shift(1)
elif _args[0].startswith("-m"):
common_flags += shift(1)
elif _args[0].startswith("-f"):
common_flags += shift(1)
elif _args[0].startswith("-W"):
common_flags += shift(1)
else:
raise ValueError(f"unknown flag: {_args!r}")
else:
positional += shift(1)
if len(positional) != 1:
raise ValueError("expected 1 input file")
infile = positional[0]
# enhance ##################################################################
common_flags += ["-D", "__LIBMISC_ENHANCED_CPP__"]
text = subprocess.run(
[arg0, *common_flags, infile],
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=sys.stderr,
check=True,
text=True,
).stdout
macros: dict[str, str] = {}
marker = "__xx__LM_DEFAPPEND__xx__"
pos = 0
while (marker_beg := text.find(marker, pos)) >= 0:
args: list[str] = []
def add_arg(arg: str) -> None:
nonlocal args
args.append(arg)
beg_paren = marker_beg + len(marker)
end_paren = scan_tuple(text, beg_paren, add_arg)
before = text[:marker_beg]
# old = text[marker_beg : end_paren + 1]
after = text[end_paren + 1 :]
assert len(args) == 2
k = unquote(args[0].strip())
v = unquote(args[1].strip())
if k not in macros:
macros[k] = v
else:
macros[k] += " " + v
text = before + after
pos = len(before)
common_flags += ["-D", marker + "=LM_EAT"]
for k, v in macros.items():
common_flags += ["-D", k + "=" + v]
# Run, for-real ############################################################
os.execvp(arg0, [arg0, *common_flags, *output_flags, infile])
def cpp_squash_linemarkers(text: str) -> str:
out = ""
buf_marker = ""
buf_body = ""
for line in text.splitlines(keepends=True):
if line.startswith("# "):
if buf_marker != line or buf_body.strip():
out += buf_marker
if buf_body.strip():
out += buf_body
buf_marker = line
buf_body = ""
else:
buf_body += line
if buf_body.strip():
out += buf_marker
out += buf_body
return out
class Preprocessor:
_cpp: list[str]
_builtin_defines: list[str] | None = None
def __init__(self, cpp: list[str]) -> None:
self._cpp = cpp
self._builtin_defines = None
@property
def builtin_defines(self) -> list[str]:
if self._builtin_defines is None:
self._builtin_defines = subprocess.run(
[*self._cpp, "-quiet", "-undef", "-nostdinc", "-dM"],
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=sys.stderr,
check=True,
text=True,
).stdout.splitlines(keepends=True)
return self._builtin_defines
def process_file(self, infile: str, flags: list[str]) -> str:
# First/main preprocessor pass.
text = subprocess.run(
[*self._cpp, "-dD", *flags, infile],
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=sys.stderr,
check=True,
text=True,
).stdout
# Extra (subclass) processing.
text = self.extra(text)
# Split the combined "-dD" output into "-dM" output and normal
# output.
macro_text = ""
normal_text = ""
for line in text.splitlines(keepends=True):
if line.startswith("#define ") or line.startswith("#undef "):
macro_text += line
normal_text += "\n"
else:
normal_text += line
return normal_text
def process_fragment(self, before: str, text: str) -> str:
before_lines = before.splitlines(keepends=True)
builtin_defines = self.builtin_defines
prefix = "".join(
[
line
for line in before_lines
if line.startswith("#") and line not in builtin_defines
]
)
prefix = cpp_squash_linemarkers(prefix)
text = subprocess.run(
[
*self._cpp,
"-quiet",
"-undef",
"-nostdinc",
"-dD",
],
input=prefix + text,
stdout=subprocess.PIPE,
stderr=sys.stderr,
check=True,
text=True,
).stdout
text = cpp_squash_linemarkers(text)
text = text[text.index(prefix) + len(prefix) :]
if text.startswith("# "):
text = "\n" + text
text = self.extra(text)
return text
def extra(self, text: str) -> str:
return text
################################################################################
class EnhancedPreprocessor(Preprocessor):
def process_file(self, infile: str, flags: list[str]) -> str:
return super().process_file(infile, ["-D__LIBMISC_ENHANCED_CPP__", *flags])
special_macros = [
"LM_EVAL",
"LM_FOREACH_PARAM",
"LM_FOREACH_TUPLE",
]
re_special = re.compile(
r"__xx_(?P<macro>"
+ "|".join([re.escape(m) for m in special_macros])
+ r")_xx__\("
)
def extra(self, text: str) -> str:
pos = 0
while intro := self.re_special.search(text, pos):
nl = text.rfind("\n", 0, intro.start())
if text[nl + 1] == "#":
pos = intro.end()
continue
macro = intro.group("macro")
args: list[str] = []
def add_arg(arg: str) -> None:
args.append(arg)
beg_paren = intro.end() - 1
end_paren = cpp_scan_tuple(text, beg_paren, add_arg)
before = text[: intro.start()]
# old = text[intro.start() : end_paren + 1]
after = text[end_paren + 1 :]
new = self._eval_macro(before, macro, args)
text = before + new + after
pos = len(before) + len(new)
return text
def _eval_macro(self, before: str, macro: str, args: list[str]) -> str:
match macro:
case "LM_EVAL": # LM_EVAL(...)
ret = ",".join(args)
while True:
ret2 = self.process_fragment(before, ret)
if ret2 == ret:
break
ret = ret2
return ret
case "LM_FOREACH_PARAM": # LM_FOREACH_PARAM(func, (fixedparams), params...)
assert len(args) >= 2
func = args[0].strip()
fixedparams = args[1].strip()[1:-1].strip()
if fixedparams:
fixedparams += ", "
ret = ""
for param in args[2:]:
ret += f"{func}({fixedparams}{param})"
return ret
case "LM_FOREACH_TUPLE": # LM_FOREACH_TUPLE(tuples, func, fixedparams...)
tuples_str = args[0].lstrip()
func = args[1].strip()
fixedparams = "".join([a.strip() + ", " for a in args[2:]])
ret = ""
while tuples_str:
end_paren = cpp_scan_tuple(tuples_str, 0)
tup = tuples_str[1:end_paren]
ret += f"{func}({fixedparams}{tup})"
tuples_str = tuples_str[end_paren + 1 :].lstrip()
return ret
case _:
raise ValueError(f"unknown macro: {macro}")
################################################################################
def main(all_args: list[str]) -> typing.NoReturn:
if len(all_args) >= 2 and all_args[0].endswith("cc1") and all_args[1] == "-E":
preprocess(all_args)
else:
os.execvp(all_args[0], all_args)
if __name__ == "__main__":
main(sys.argv[1:])
|