explode_asserts.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. #!/usr/bin/env python3
  2. import re
  3. import sys
  4. PATTERN = ['LFS_ASSERT', 'assert']
  5. PREFIX = 'LFS'
  6. MAXWIDTH = 16
  7. ASSERT = "__{PREFIX}_ASSERT_{TYPE}_{COMP}"
  8. FAIL = """
  9. __attribute__((unused))
  10. static void __{prefix}_assert_fail_{type}(
  11. const char *file, int line, const char *comp,
  12. {ctype} lh, size_t lsize,
  13. {ctype} rh, size_t rsize) {{
  14. printf("%s:%d:assert: assert failed with ", file, line);
  15. __{prefix}_assert_print_{type}(lh, lsize);
  16. printf(", expected %s ", comp);
  17. __{prefix}_assert_print_{type}(rh, rsize);
  18. printf("\\n");
  19. fflush(NULL);
  20. raise(SIGABRT);
  21. }}
  22. """
  23. COMP = {
  24. '==': 'eq',
  25. '!=': 'ne',
  26. '<=': 'le',
  27. '>=': 'ge',
  28. '<': 'lt',
  29. '>': 'gt',
  30. }
  31. TYPE = {
  32. 'int': {
  33. 'ctype': 'intmax_t',
  34. 'fail': FAIL,
  35. 'print': """
  36. __attribute__((unused))
  37. static void __{prefix}_assert_print_{type}({ctype} v, size_t size) {{
  38. (void)size;
  39. printf("%"PRIiMAX, v);
  40. }}
  41. """,
  42. 'assert': """
  43. #define __{PREFIX}_ASSERT_{TYPE}_{COMP}(file, line, lh, rh)
  44. do {{
  45. __typeof__(lh) _lh = lh;
  46. __typeof__(lh) _rh = (__typeof__(lh))rh;
  47. if (!(_lh {op} _rh)) {{
  48. __{prefix}_assert_fail_{type}(file, line, "{comp}",
  49. (intmax_t)_lh, 0, (intmax_t)_rh, 0);
  50. }}
  51. }} while (0)
  52. """
  53. },
  54. 'bool': {
  55. 'ctype': 'bool',
  56. 'fail': FAIL,
  57. 'print': """
  58. __attribute__((unused))
  59. static void __{prefix}_assert_print_{type}({ctype} v, size_t size) {{
  60. (void)size;
  61. printf("%s", v ? "true" : "false");
  62. }}
  63. """,
  64. 'assert': """
  65. #define __{PREFIX}_ASSERT_{TYPE}_{COMP}(file, line, lh, rh)
  66. do {{
  67. bool _lh = !!(lh);
  68. bool _rh = !!(rh);
  69. if (!(_lh {op} _rh)) {{
  70. __{prefix}_assert_fail_{type}(file, line, "{comp}",
  71. _lh, 0, _rh, 0);
  72. }}
  73. }} while (0)
  74. """
  75. },
  76. 'mem': {
  77. 'ctype': 'const void *',
  78. 'fail': FAIL,
  79. 'print': """
  80. __attribute__((unused))
  81. static void __{prefix}_assert_print_{type}({ctype} v, size_t size) {{
  82. const uint8_t *s = v;
  83. printf("\\\"");
  84. for (size_t i = 0; i < size && i < {maxwidth}; i++) {{
  85. if (s[i] >= ' ' && s[i] <= '~') {{
  86. printf("%c", s[i]);
  87. }} else {{
  88. printf("\\\\x%02x", s[i]);
  89. }}
  90. }}
  91. if (size > {maxwidth}) {{
  92. printf("...");
  93. }}
  94. printf("\\\"");
  95. }}
  96. """,
  97. 'assert': """
  98. #define __{PREFIX}_ASSERT_{TYPE}_{COMP}(file, line, lh, rh, size)
  99. do {{
  100. const void *_lh = lh;
  101. const void *_rh = rh;
  102. if (!(memcmp(_lh, _rh, size) {op} 0)) {{
  103. __{prefix}_assert_fail_{type}(file, line, "{comp}",
  104. _lh, size, _rh, size);
  105. }}
  106. }} while (0)
  107. """
  108. },
  109. 'str': {
  110. 'ctype': 'const char *',
  111. 'fail': FAIL,
  112. 'print': """
  113. __attribute__((unused))
  114. static void __{prefix}_assert_print_{type}({ctype} v, size_t size) {{
  115. __{prefix}_assert_print_mem(v, size);
  116. }}
  117. """,
  118. 'assert': """
  119. #define __{PREFIX}_ASSERT_{TYPE}_{COMP}(file, line, lh, rh)
  120. do {{
  121. const char *_lh = lh;
  122. const char *_rh = rh;
  123. if (!(strcmp(_lh, _rh) {op} 0)) {{
  124. __{prefix}_assert_fail_{type}(file, line, "{comp}",
  125. _lh, strlen(_lh), _rh, strlen(_rh));
  126. }}
  127. }} while (0)
  128. """
  129. }
  130. }
  131. def mkdecls(outf, maxwidth=16):
  132. outf.write("#include <stdio.h>\n")
  133. outf.write("#include <stdbool.h>\n")
  134. outf.write("#include <stdint.h>\n")
  135. outf.write("#include <inttypes.h>\n")
  136. outf.write("#include <signal.h>\n")
  137. for type, desc in sorted(TYPE.items()):
  138. format = {
  139. 'type': type.lower(), 'TYPE': type.upper(),
  140. 'ctype': desc['ctype'],
  141. 'prefix': PREFIX.lower(), 'PREFIX': PREFIX.upper(),
  142. 'maxwidth': maxwidth,
  143. }
  144. outf.write(re.sub('\s+', ' ',
  145. desc['print'].strip().format(**format))+'\n')
  146. outf.write(re.sub('\s+', ' ',
  147. desc['fail'].strip().format(**format))+'\n')
  148. for op, comp in sorted(COMP.items()):
  149. format.update({
  150. 'comp': comp.lower(), 'COMP': comp.upper(),
  151. 'op': op,
  152. })
  153. outf.write(re.sub('\s+', ' ',
  154. desc['assert'].strip().format(**format))+'\n')
  155. def mkassert(type, comp, lh, rh, size=None):
  156. format = {
  157. 'type': type.lower(), 'TYPE': type.upper(),
  158. 'comp': comp.lower(), 'COMP': comp.upper(),
  159. 'prefix': PREFIX.lower(), 'PREFIX': PREFIX.upper(),
  160. 'lh': lh.strip(' '),
  161. 'rh': rh.strip(' '),
  162. 'size': size,
  163. }
  164. if size:
  165. return ((ASSERT + '(__FILE__, __LINE__, {lh}, {rh}, {size})')
  166. .format(**format))
  167. else:
  168. return ((ASSERT + '(__FILE__, __LINE__, {lh}, {rh})')
  169. .format(**format))
  170. # simple recursive descent parser
  171. LEX = {
  172. 'ws': [r'(?:\s|\n|#.*?\n|//.*?\n|/\*.*?\*/)+'],
  173. 'assert': PATTERN,
  174. 'string': [r'"(?:\\.|[^"])*"', r"'(?:\\.|[^'])\'"],
  175. 'arrow': ['=>'],
  176. 'paren': ['\(', '\)'],
  177. 'op': ['strcmp', 'memcmp', '->'],
  178. 'comp': ['==', '!=', '<=', '>=', '<', '>'],
  179. 'logic': ['\&\&', '\|\|'],
  180. 'sep': [':', ';', '\{', '\}', ','],
  181. }
  182. class ParseFailure(Exception):
  183. def __init__(self, expected, found):
  184. self.expected = expected
  185. self.found = found
  186. def __str__(self):
  187. return "expected %r, found %s..." % (
  188. self.expected, repr(self.found)[:70])
  189. class Parse:
  190. def __init__(self, inf, lexemes):
  191. p = '|'.join('(?P<%s>%s)' % (n, '|'.join(l))
  192. for n, l in lexemes.items())
  193. p = re.compile(p, re.DOTALL)
  194. data = inf.read()
  195. tokens = []
  196. while True:
  197. m = p.search(data)
  198. if m:
  199. if m.start() > 0:
  200. tokens.append((None, data[:m.start()]))
  201. tokens.append((m.lastgroup, m.group()))
  202. data = data[m.end():]
  203. else:
  204. tokens.append((None, data))
  205. break
  206. self.tokens = tokens
  207. self.off = 0
  208. def lookahead(self, *pattern):
  209. if self.off < len(self.tokens):
  210. token = self.tokens[self.off]
  211. if token[0] in pattern or token[1] in pattern:
  212. self.m = token[1]
  213. return self.m
  214. self.m = None
  215. return self.m
  216. def accept(self, *patterns):
  217. m = self.lookahead(*patterns)
  218. if m is not None:
  219. self.off += 1
  220. return m
  221. def expect(self, *patterns):
  222. m = self.accept(*patterns)
  223. if not m:
  224. raise ParseFailure(patterns, self.tokens[self.off:])
  225. return m
  226. def push(self):
  227. return self.off
  228. def pop(self, state):
  229. self.off = state
  230. def passert(p):
  231. def pastr(p):
  232. p.expect('assert') ; p.accept('ws') ; p.expect('(') ; p.accept('ws')
  233. p.expect('strcmp') ; p.accept('ws') ; p.expect('(') ; p.accept('ws')
  234. lh = pexpr(p) ; p.accept('ws')
  235. p.expect(',') ; p.accept('ws')
  236. rh = pexpr(p) ; p.accept('ws')
  237. p.expect(')') ; p.accept('ws')
  238. comp = p.expect('comp') ; p.accept('ws')
  239. p.expect('0') ; p.accept('ws')
  240. p.expect(')')
  241. return mkassert('str', COMP[comp], lh, rh)
  242. def pamem(p):
  243. p.expect('assert') ; p.accept('ws') ; p.expect('(') ; p.accept('ws')
  244. p.expect('memcmp') ; p.accept('ws') ; p.expect('(') ; p.accept('ws')
  245. lh = pexpr(p) ; p.accept('ws')
  246. p.expect(',') ; p.accept('ws')
  247. rh = pexpr(p) ; p.accept('ws')
  248. p.expect(',') ; p.accept('ws')
  249. size = pexpr(p) ; p.accept('ws')
  250. p.expect(')') ; p.accept('ws')
  251. comp = p.expect('comp') ; p.accept('ws')
  252. p.expect('0') ; p.accept('ws')
  253. p.expect(')')
  254. return mkassert('mem', COMP[comp], lh, rh, size)
  255. def paint(p):
  256. p.expect('assert') ; p.accept('ws') ; p.expect('(') ; p.accept('ws')
  257. lh = pexpr(p) ; p.accept('ws')
  258. comp = p.expect('comp') ; p.accept('ws')
  259. rh = pexpr(p) ; p.accept('ws')
  260. p.expect(')')
  261. return mkassert('int', COMP[comp], lh, rh)
  262. def pabool(p):
  263. p.expect('assert') ; p.accept('ws') ; p.expect('(') ; p.accept('ws')
  264. lh = pexprs(p) ; p.accept('ws')
  265. p.expect(')')
  266. return mkassert('bool', 'eq', lh, 'true')
  267. def pa(p):
  268. return p.expect('assert')
  269. state = p.push()
  270. lastf = None
  271. for pa in [pastr, pamem, paint, pabool, pa]:
  272. try:
  273. return pa(p)
  274. except ParseFailure as f:
  275. p.pop(state)
  276. lastf = f
  277. else:
  278. raise lastf
  279. def pexpr(p):
  280. res = []
  281. while True:
  282. if p.accept('('):
  283. res.append(p.m)
  284. while True:
  285. res.append(pexprs(p))
  286. if p.accept('sep'):
  287. res.append(p.m)
  288. else:
  289. break
  290. res.append(p.expect(')'))
  291. elif p.lookahead('assert'):
  292. res.append(passert(p))
  293. elif p.accept('assert', 'ws', 'string', 'op', None):
  294. res.append(p.m)
  295. else:
  296. return ''.join(res)
  297. def pexprs(p):
  298. res = []
  299. while True:
  300. res.append(pexpr(p))
  301. if p.accept('comp', 'logic', ','):
  302. res.append(p.m)
  303. else:
  304. return ''.join(res)
  305. def pstmt(p):
  306. ws = p.accept('ws') or ''
  307. lh = pexprs(p)
  308. if p.accept('=>'):
  309. rh = pexprs(p)
  310. return ws + mkassert('int', 'eq', lh, rh)
  311. else:
  312. return ws + lh
  313. def main(args):
  314. inf = open(args.input, 'r') if args.input else sys.stdin
  315. outf = open(args.output, 'w') if args.output else sys.stdout
  316. lexemes = LEX.copy()
  317. if args.pattern:
  318. lexemes['assert'] = args.pattern
  319. p = Parse(inf, lexemes)
  320. # write extra verbose asserts
  321. mkdecls(outf, maxwidth=args.maxwidth)
  322. if args.input:
  323. outf.write("#line %d \"%s\"\n" % (1, args.input))
  324. # parse and write out stmt at a time
  325. try:
  326. while True:
  327. outf.write(pstmt(p))
  328. if p.accept('sep'):
  329. outf.write(p.m)
  330. else:
  331. break
  332. except ParseFailure as f:
  333. pass
  334. for i in range(p.off, len(p.tokens)):
  335. outf.write(p.tokens[i][1])
  336. if __name__ == "__main__":
  337. import argparse
  338. parser = argparse.ArgumentParser(
  339. description="Cpp step that increases assert verbosity")
  340. parser.add_argument('input', nargs='?',
  341. help="Input C file after cpp.")
  342. parser.add_argument('-o', '--output', required=True,
  343. help="Output C file.")
  344. parser.add_argument('-p', '--pattern', action='append',
  345. help="Patterns to search for starting an assert statement.")
  346. parser.add_argument('--maxwidth', default=MAXWIDTH, type=int,
  347. help="Maximum number of characters to display for strcmp and memcmp.")
  348. main(parser.parse_args())