summaryrefslogtreecommitdiff
path: root/tools/debug-debug-scripts
blob: 7cb91d95072b59a21d4c37479387eccd611b67e0 (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
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
#!/usr/bin/env python
# encoding: utf-8
from __future__ import print_function

copyright = '''
//    Copyright © 2014 Ben Longbons <b.r.longbons@gmail.com>
//
//    This file is part of The Mana World (Athena server)
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

import glob
import itertools
import os
import subprocess
import sys
import tempfile

import protocol

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('\n', '\\n')
    s = s.replace('"', '\\"')
    return '"' + s + '"'

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

def main(args):
    outdir = args[0]
    args = args[1:]

    for g in glob.glob(os.path.join(outdir, '*.[ch]pp')):
        os.rename(g, g + '.old')

    for a in args:
        names = []
        basename, ext = os.path.splitext(a)
        assert ext == '.py'
        newbase = basename.split('src/')[1].replace('/', '-')
        out = os.path.join(outdir, newbase + '.cpp')
        with protocol.OpenWrite(out) as w:
            print('// %s.cpp - generated by' % newbase, __file__, file=w)
            print(copyright, file=w)
            print('#include <cstdio>', file=w)
            print('// just mention "fwd.hpp" and "../poison.hpp" to make formatter happy', file=w)
            print('namespace tmwa', file=w)
            print('{', file=w)
            print('    void do_nothing_asan_is_funny_global_constructor();', file=w)
            print('    void do_nothing_asan_is_funny_global_constructor() {}', file=w)
            print('} // namespace tmwa', file=w)
            print(file=w)
            print('template<class T>', file=w)
            print('__attribute__((noinline))', file=w)
            print('void do_breakpoint(const T& value, const char *expected)', file=w)
            print('{', file=w)
            print('    (void)value;', file=w)
            print('    (void)expected;', file=w)
            print('    if (!expected) printf("printer test: %p = %s\\n", &value, expected);', file=w)
            print('}', file=w)
            print(file=w)
            print('// Tests from', a, file=w)
            header = basename + '.hpp'
            print('#include "%s"' % header, file=w)

            for (k, tests, extra) in get_classes_from_file(a):
                print(file=w)
                print('// Tests for', k, file=w)
                print(extra, file=w)
                for (i, (expr, expected)) in enumerate(tests):
                    name = 'testset_%s_subtest_%d' % (k, i)
                    gen_test(name, expr, expected, w)
                    names.append(name)
            print('int main()', file=w)
            print('{', file=w)
            for n in names:
                print('    %s();' % n, file=w)
            print('}', file=w)

    for g in glob.glob(os.path.join(outdir, '*.old')):
        print('Obsolete: %s' % g)
        os.remove(g)

    if error and not os.getenv('TMWA_FORCE_GENERATE'):
        sys.exit(1)


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