-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathifdef-rewriter.py
172 lines (159 loc) · 7.3 KB
/
ifdef-rewriter.py
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
#!/usr/bin/env python
# -*- Mode: Python; indent-tabs-mode: nil -*-
# vi: set ts=4 sw=4 expandtab:
#
# The MIT License
#
# Copyright (c) 2011 Alexander Macdonald
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import os
import shutil
import tempfile
import re
import sys
import optparse
from ifdef.parser import *
def serializeifdefs(xs, dst, opts):
# we only tag the else/endif with the conditional if it
# is further than 'threshold' lines away from the
# if/ifdef/ifndef/elif that declared it.
threshold = 4
for x in xs:
if isinstance(x, Ifdef):
rewrite = True
d = x.children[0].cond.expr
if len(opts.enabled) > 0 or len(opts.disabled) > 0:
found = False
if d is None:
continue
for n in opts.enabled + opts.disabled:
if d.find(n) >= 0:
found = True
break
if found:
newcond = expr.parse(d)[0]
for n in opts.enabled:
newcond = substitutevalue(newcond, n, DefinedValue(1))
for n in opts.disabled:
newcond = substitutevalue(newcond, n, None)
newcond = evalexpr(newcond)
if len(x.children) == 1:
if newcond is False or newcond is None:
# simple removal
continue
elif newcond is True:
# preserve contents of if, removing ifdefs
serializeifdefs(x.children[0].children, dst, opts)
continue
else:
# re-write expr!
x.children[0].cond.expr = newcond
if len(x.children) == 2 and x.children[1].cond == None:
if newcond is False or newcond is None:
# remove if, preserve else
serializeifdefs(x.children[1].children, dst, opts)
continue
elif newcond is True:
# preserve contents of if, remove ifdefs and else block
serializeifdefs(x.children[0].children, dst, opts)
continue
else:
# re-write expr!
x.children[0].cond.expr = newcond
else:
rewrite = False
if rewrite:
prevbranch = None
for b in x.children:
d = b.cond
if d is None:
# This is an "else" branch
d = parseline(b.startline)
if opts.updatecomments:
d.comment = ""
if b.startpos - prevbranch.startpos > threshold:
e = printifdefexpr(prevbranch.cond).strip()
if prevbranch.cond.token in ["ifndef", "ifdef"]:
e = "%s %s" % (prevbranch.cond.token, e)
d.comment = "// " + e
dst.write(printifdef(d) + "\n")
serializeifdefs(b.children, dst, opts)
prevbranch = b
if opts.updatecomments:
d = parseline(x.children[-1].endline)
d.comment = ""
if x.children[-1].endpos - x.children[-1].startpos > threshold:
c = x.children[-2].cond if x.children[-1].cond is None else x.children[-1].cond
e = printifdefexpr(c).strip()
if c.token in ["ifndef", "ifdef"]:
e = "%s %s" % (c.token, e)
d.comment = "// " + e
dst.write(printifdef(d) + "\n")
else:
dst.write(x.children[-1].endline)
else:
for b in x.children:
dst.write(b.startline)
serializeifdefs(b.children, dst, opts)
dst.write(x.children[-1].endline)
elif isinstance(x, Branch):
serializeifdefs(x.children, dst, opts)
else:
dst.write(x)
def tidyifdefs(file, opts):
print "Tidying ifdefs in %s" % file
dst = tempfile.NamedTemporaryFile()
try:
root = parsefile(file)
serializeifdefs(root.children, dst, opts)
# close the output file, then replace the
# source with the cleaned file
dst.flush()
shutil.copyfile(dst.name, file)
except KeyboardInterrupt:
exit(0)
except:
print " -- Failed to parse %s: %s" % (file, sys.exc_info()[0])
print sys.exc_info()
dst.close()
# ------------------------------------------------------------------------------
# Main Entrypoint
# ------------------------------------------------------------------------------
if __name__ == "__main__":
optParser = optparse.OptionParser(usage='usage: %prog [ files ]\n\nIf no files are given then a recursive search for files ending\nwith c/cpp/mm/h is performed in the current directory.')
optParser.set_defaults()
optParser.add_option( '-e', '--always-enabled', dest="enabled", default = [], action="append")
optParser.add_option( '-d', '--always-disabled', dest="disabled", default = [], action="append")
optParser.add_option( '-i', '--ignore-file', dest="ignored", default = [], action="append")
optParser.add_option( '-u', '--dont-update-comments', dest="updatecomments", default = True, action="store_false")
(opts, args) = optParser.parse_args()
if len(args) == 0:
for (path, dirs, files) in os.walk("."):
for file in files:
if file in opts.ignored:
continue
if any(file.endswith(x) for x in [".c", ".cpp", ".h", ".mm"]):
fullpath = "%s/%s" % (path, file)
tidyifdefs(fullpath, opts)
else:
for fullpath in args:
if file in opts.ignored:
continue
tidyifdefs(fullpath, opts)