cpp.py
987 Bytes
import subprocess
def _lnomap (line) :
if line.startswith("#") :
try :
return int(line.split(None, 2)[1])
except :
pass
def cpp (path, args) :
lmap = {}
code = []
args = ["cpp", "-D__CCT__", "-U__GNUC__"] + args + [path]
pos = 1
out = subprocess.check_output(args)
for line in (l.rstrip() for l in out.decode().splitlines()) :
num = _lnomap(line)
if num is not None :
pos = num
else :
code.append(line)
lmap[len(code)] = pos
pos += 1
return "\n".join(code), lmap
def test (path) :
old = [l.rstrip() for l in open(path)]
new, lno = cpp(path, "x86", [])
new = [l.rstrip() for l in new.splitlines()]
for i, l in enumerate(new) :
s = old[lno.get(i+1, i+1) - 1]
if s != l :
print("@%s -> %s" % (i+1, lno.get(i+1, i+1) - 1))
print("<", s)
print(">", l)
return old, new, lno