Skip to content

Commit dae656a

Browse files
committed
Fix prettyasserts.py for pointer asserts
1 parent 469c863 commit dae656a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

scripts/prettyasserts.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ def write_header(f, limit=LIMIT):
8686
f.writeln("}")
8787
f.writeln()
8888
f.writeln("__attribute__((unused))")
89+
f.writeln("static void __pretty_assert_print_ptr(")
90+
f.writeln(" const void *v, size_t size) {")
91+
f.writeln(" (void)size;")
92+
f.writeln(" printf(\"%p\", v);")
93+
f.writeln("}")
94+
f.writeln()
95+
f.writeln("__attribute__((unused))")
8996
f.writeln("static void __pretty_assert_print_mem(")
9097
f.writeln(" const void *v, size_t size) {")
9198
f.writeln(" const uint8_t *v_ = v;")
@@ -183,6 +190,23 @@ def write_header(f, limit=LIMIT):
183190
f.writeln(" _rh, strlen(_rh)); \\")
184191
f.writeln(" } \\")
185192
f.writeln("} while (0)")
193+
for op, cmp in sorted(CMP.items()):
194+
# Only EQ and NE are supported when compared to NULL.
195+
if cmp not in ['eq', 'ne']:
196+
continue
197+
f.writeln("#define __PRETTY_ASSERT_PTR_%s(lh, rh) do { \\"
198+
% cmp.upper())
199+
f.writeln(" const void *_lh = (const void*)(uintptr_t)lh; \\")
200+
f.writeln(" const void *_rh = (const void*)(uintptr_t)rh; \\")
201+
f.writeln(" if (!(_lh %s _rh)) { \\" % op)
202+
f.writeln(" __pretty_assert_fail( \\")
203+
f.writeln(" __FILE__, __LINE__, \\")
204+
f.writeln(" __pretty_assert_print_ptr, \"%s\", \\"
205+
% cmp)
206+
f.writeln(" (const void*){_lh}, 0, \\")
207+
f.writeln(" (const void*){_rh}, 0); \\")
208+
f.writeln(" } \\")
209+
f.writeln("} while (0)")
186210
f.writeln()
187211
f.writeln()
188212

@@ -301,6 +325,8 @@ def p_assert(p):
301325
cmp = p.expect('cmp') ; p.accept('ws')
302326
rh = p_expr(p) ; p.accept('ws')
303327
p.expect(')')
328+
if rh == 'NULL' or lh == 'NULL':
329+
return mkassert('ptr', CMP[cmp], lh, rh)
304330
return mkassert('int', CMP[cmp], lh, rh)
305331
except ParseFailure:
306332
p.pop(state)

0 commit comments

Comments
 (0)