-
Notifications
You must be signed in to change notification settings - Fork 969
Expand file tree
/
Copy pathexplode_asserts.py
More file actions
executable file
·211 lines (186 loc) · 6.18 KB
/
explode_asserts.py
File metadata and controls
executable file
·211 lines (186 loc) · 6.18 KB
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
#!/usr/bin/env python3
import parsy as p
import re
import io
import sys
ASSERT_PATTERN = p.string('LFS_ASSERT') | p.string('assert')
ASSERT_CHARS = 'La'
ASSERT_TARGET = '__LFS_ASSERT_{TYPE}_{COMP}'
ASSERT_TESTS = {
'int': """
__typeof__({lh}) _lh = {lh};
__typeof__({lh}) _rh = (__typeof__({lh})){rh};
if (!(_lh {op} _rh)) {{
printf("%s:%d:assert: "
"assert failed with %"PRIiMAX", expected {comp} %"PRIiMAX"\\n",
{file}, {line}, (intmax_t)_lh, (intmax_t)_rh);
exit(-2);
}}
""",
'str': """
const char *_lh = {lh};
const char *_rh = {rh};
if (!(strcmp(_lh, _rh) {op} 0)) {{
printf("%s:%d:assert: "
"assert failed with \\\"%s\\\", expected {comp} \\\"%s\\\"\\n",
{file}, {line}, _lh, _rh);
exit(-2);
}}
""",
'bool': """
bool _lh = !!({lh});
bool _rh = !!({rh});
if (!(_lh {op} _rh)) {{
printf("%s:%d:assert: "
"assert failed with %s, expected {comp} %s\\n",
{file}, {line}, _lh ? "true" : "false", _rh ? "true" : "false");
exit(-2);
}}
""",
}
def mkassert(lh, rh='true', type='bool', comp='eq'):
return ((ASSERT_TARGET + "({lh}, {rh}, __FILE__, __LINE__, __func__)")
.format(
type=type, TYPE=type.upper(),
comp=comp, COMP=comp.upper(),
lh=lh.strip(' '),
rh=rh.strip(' ')))
def mkdecl(type, comp, op):
return ((
"#define "+ASSERT_TARGET+"(lh, rh, file, line, func)"
" do {{"+re.sub('\s+', ' ', ASSERT_TESTS[type])+"}} while (0)\n")
.format(
type=type, TYPE=type.upper(),
comp=comp, COMP=comp.upper(),
lh='lh', rh='rh', op=op,
file='file', line='line', func='func'))
# add custom until combinator
def until(self, end):
return end.should_fail('should fail').then(self).many()
p.Parser.until = until
pcomp = (
p.string('==').tag('eq') |
p.string('!=').tag('ne') |
p.string('<=').tag('le') |
p.string('>=').tag('ge') |
p.string('<').tag('lt') |
p.string('>').tag('gt'));
plogic = p.string('&&') | p.string('||')
@p.generate
def pstrassert():
yield ASSERT_PATTERN + p.regex('\s*') + p.string('(') + p.regex('\s*')
yield p.string('strcmp') + p.regex('\s*') + p.string('(') + p.regex('\s*')
lh = yield pexpr.until(p.string(',') | p.string(')') | plogic)
yield p.string(',') + p.regex('\s*')
rh = yield pexpr.until(p.string(')') | plogic)
yield p.string(')') + p.regex('\s*')
op = yield pcomp
yield p.regex('\s*') + p.string('0') + p.regex('\s*') + p.string(')')
return mkassert(''.join(lh), ''.join(rh), 'str', op[0])
@p.generate
def pintassert():
yield ASSERT_PATTERN + p.regex('\s*') + p.string('(') + p.regex('\s*')
lh = yield pexpr.until(pcomp | p.string(')') | plogic)
op = yield pcomp
rh = yield pexpr.until(p.string(')') | plogic)
yield p.string(')')
return mkassert(''.join(lh), ''.join(rh), 'int', op[0])
@p.generate
def pboolassert():
yield ASSERT_PATTERN + p.regex('\s*') + p.string('(') + p.regex('\s*')
expr = yield pexpr.until(p.string(')'))
yield p.string(')')
return mkassert(''.join(expr), 'true', 'bool', 'eq')
passert = p.peek(ASSERT_PATTERN) >> (pstrassert | pintassert | pboolassert)
@p.generate
def pcomment1():
yield p.string('//')
s = yield p.regex('[^\\n]*')
yield p.string('\n')
return '//' + s + '\n'
@p.generate
def pcomment2():
yield p.string('/*')
s = yield p.regex('((?!\*/).)*')
yield p.string('*/')
return '/*' + ''.join(s) + '*/'
@p.generate
def pcomment3():
yield p.string('#')
s = yield p.regex('[^\\n]*')
yield p.string('\n')
return '#' + s + '\n'
pws = p.regex('\s+') | pcomment1 | pcomment2 | pcomment3
@p.generate
def pstring():
q = yield p.regex('["\']')
s = yield (p.string('\\%s' % q) | p.regex('[^%s]' % q)).many()
yield p.string(q)
return q + ''.join(s) + q
@p.generate
def pnested():
l = yield p.string('(')
n = yield pexpr.until(p.string(')'))
r = yield p.string(')')
return l + ''.join(n) + r
pexpr = (
# shortcut for a bit better performance
p.regex('[^%s/#\'"();{}=><,&|-]+' % ASSERT_CHARS) |
pws |
passert |
pstring |
pnested |
p.string('->') |
p.regex('.', re.DOTALL))
@p.generate
def pstmt():
ws = yield pws.many()
lh = yield pexpr.until(p.string('=>') | p.regex('[;{}]'))
op = yield p.string('=>').optional()
if op == '=>':
rh = yield pstmt
return ''.join(ws) + mkassert(''.join(lh), rh, 'int', 'eq')
else:
return ''.join(ws) + ''.join(lh)
@p.generate
def pstmts():
a = yield pstmt
b = yield (p.regex('[;{}]') + pstmt).many()
return [a] + b
def main(args):
inf = open(args.input, 'r') if args.input else sys.stdin
outf = open(args.output, 'w') if args.output else sys.stdout
# parse C code
input = inf.read()
stmts = pstmts.parse(input)
# write extra verbose asserts
outf.write("#include <stdbool.h>\n")
outf.write("#include <stdint.h>\n")
outf.write("#include <inttypes.h>\n")
outf.write(mkdecl('int', 'eq', '=='))
outf.write(mkdecl('int', 'ne', '!='))
outf.write(mkdecl('int', 'lt', '<'))
outf.write(mkdecl('int', 'gt', '>'))
outf.write(mkdecl('int', 'le', '<='))
outf.write(mkdecl('int', 'ge', '>='))
outf.write(mkdecl('str', 'eq', '=='))
outf.write(mkdecl('str', 'ne', '!='))
outf.write(mkdecl('str', 'lt', '<'))
outf.write(mkdecl('str', 'gt', '>'))
outf.write(mkdecl('str', 'le', '<='))
outf.write(mkdecl('str', 'ge', '>='))
outf.write(mkdecl('bool', 'eq', '=='))
if args.input:
outf.write("#line %d \"%s\"\n" % (1, args.input))
# write parsed statements
for stmt in stmts:
outf.write(stmt)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(
description="Cpp step that increases assert verbosity")
parser.add_argument('input', nargs='?',
help="Input C file after cpp.")
parser.add_argument('-o', '--output',
help="Output C file.")
main(parser.parse_args())