summaryrefslogtreecommitdiff
path: root/tools/debug-debug-scripts
blob: 79520e9a5eb937bd6e4c4d220daafbc16ee46ddc (plain) (blame)
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
#!/usr/bin/env python

from __future__ import print_function

import itertools
import os
import subprocess
import sys
import tempfile

error = False

def eprint(s):
    print('Error:', s, file=sys.stderr)

def get_classes_from_file(a):
    global error
    d = {}
    execfile(a, d)
    for (k, v) in sorted(d.items()):
        try:
            name = v.name
        except AttributeError:
            if hasattr(v, 'enabled') and hasattr(v, 'tests'):
                name = 'None::' + k
            else:
                continue
        else:
            if name.split('::')[-1] != k:
                eprint('Mismatch: class %s is for %s' % (k, name))
                error = True
        if not v.enabled:
            eprint('Disabled: %s' % name)
            continue

        try:
            tests = v.tests
        except AttributeError:
            eprint('Unimplemented tests for %s' % name)
            error = True
            continue
        extra = getattr(v, 'test_extra', '').rstrip(' ')
        yield (k, tests, extra)

def c_quote(s):
    s = s.replace('\\', '\\\\')
    s = s.replace('"', '\\"')
    return '"' + s + '"'

def gen_test(name, expr, expected):
    print('static')
    print('void %s()' % name)
    print('{')
    print('    auto value = %s;' % expr)
    print('    const char *expected = %s;' % c_quote(expected))
    print('    do_breakpoint(value, expected);')
    print('}')

def main(args):
    print('// Generated by', __file__)
    print('#include <cstdio>')
    print()
    print('template<class T>')
    print('__attribute__((noinline))')
    print('void do_breakpoint(const T& value, const char *expected)')
    print('{')
    print('    (void)value;')
    print('    (void)expected;')
    print('    if (!expected) printf("printer test: %p = %s\\n", &value, expected);')
    print('}')

    names = []
    for a in args:
        basename, ext = os.path.splitext(a)
        assert ext == '.py'
        print()
        print('// Tests from', a)
        header = basename + '.hpp'
        print('#include "%s"' % header)

        for (k, tests, extra) in get_classes_from_file(a):
            print()
            print('// Tests for', k)
            print(extra)
            for (i, (expr, expected)) in enumerate(tests):
                name = 'testset_%s_subtest_%d' % (k, i)
                gen_test(name, expr, expected)
                names.append(name)
    print('int main()')
    print('{')
    for n in names:
        print('    %s();' % n)
    print('}')
    if error and not os.getenv('TMWA_FORCE_GENERATE'):
        sys.exit(1)


if __name__ == '__main__':
    main(sys.argv[1:])