/
generate3.py
162 lines (147 loc) · 4.79 KB
/
generate3.py
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
#!/usr/bin/env python3
# Copyright (C) 2016 Jussi Pakkanen, Juhani Simola.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of version 3, or (at your option) any later version,
# of the GNU General Public License as published
# by the Free Software Foundation.
#
# 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/>.
# Like generate2.py but preserves the error message object.
import random, os, sys, shutil
class GenerateCode:
def __init__(self):
self.cppdir = 'cpp'
self.num_files = 1000
self.cpp_header_templ = 'int func%d();\n'
self.cpp_templ = '''#include "funcs.h"
int func%d() {
DummyObject dobj;
int a = func%d();
int b = func%d();
int c = func%d();
return a + b + c;
}
'''
self.cpp_dummy = '''class DummyObject {
public:
DummyObject();
~DummyObject();
private:
int i;
};
'''
self.cpp_main = '''#include "funcs.h"
DummyObject::DummyObject() { }
DummyObject::~DummyObject() {}
int main(int argc, char **argv) {
return func0();
}
'''
self.cdir = 'plainc'
self.c_header_templ = 'char* func%d(int *val);\n'
self.c_templ = '''#include "funcs.h"
char* func%d(int *val) {
int a, b, c;
struct Dummy *d = dummy_new();
char* err = func%d(&a);
if (!err)
err = func%d(&b);
if (!err)
err = func%d(&c);
if (!err)
*val = a + b + c;
dummy_delete(d);
return err;
}
'''
self.c_dummy = '''
struct Dummy {
int i;
};
struct Dummy* dummy_new();
void dummy_delete(struct Dummy *d);
'''
self.c_main = '''#include "funcs.h"
#include<stdlib.h>
struct Dummy* dummy_new() {
return malloc(sizeof(struct Dummy));
}
void dummy_delete(struct Dummy *d) {
free(d);
}
int main(int argc, char **argv) {
int val;
char *error = func0(&val);
return val;
}
'''
def deltrees(self):
if os.path.exists(self.cppdir):
shutil.rmtree(self.cppdir)
os.mkdir(self.cppdir)
if os.path.exists(self.cdir):
shutil.rmtree(self.cdir)
os.mkdir(self.cdir)
def run(self):
self.deltrees()
cpp_headers = [self.cpp_dummy]
c_headers = [self.c_dummy]
cpp_headername = os.path.join(self.cppdir, 'funcs.h')
c_headername = os.path.join(self.cdir, 'funcs.h')
meson_cpp = open(os.path.join(self.cppdir, 'meson.build'), 'w')
meson_c = open(os.path.join(self.cdir, 'meson.build'), 'w')
meson_cpp.write('''project('cpp size test', 'cpp', default_options : ['cpp_std=c++11'])
srcs = [
''')
meson_c.write('''project('c size test', 'c', default_options : ['cpp_std=gnu11'])
srcs = [
''')
for i in range(self.num_files):
cpp_ofname = os.path.join(self.cppdir, 'src%d.cpp' % i)
c_ofname = os.path.join(self.cdir, 'src%d.c' % i)
cpp_headers.append(self.cpp_header_templ % i)
c_headers.append(self.c_header_templ % i)
meson_cpp.write(" 'src%d.cpp',\n" % i)
meson_c.write(" 'src%d.c',\n" % i)
if i == self.num_files-1:
open(cpp_ofname, 'w').write('int func%d() { return 1; }\n' % (self.num_files-1))
open(c_ofname, 'w').write('#include <stddef.h>\nchar* func%d(int* val) { *val = 1; return NULL; }\n' % (self.num_files-1))
continue
off1 = random.randint(1, 9)
off2 = random.randint(1, 9)
off3 = random.randint(1, 9)
f1 = min(i + off1, 999)
f2 = min(i + off2, 999)
f3 = min(i + off3, 999)
cpp_contents = self.cpp_templ % (i, f1, f2, f3)
c_contents = self.c_templ % (i, f1, f2, f3)
open(cpp_ofname, 'w').write(cpp_contents)
open(c_ofname, 'w').write(c_contents)
with open(cpp_headername, 'w') as ofile:
ofile.write('#pragma once\n')
for h in cpp_headers:
ofile.write(h)
with open(c_headername, 'w') as ofile:
ofile.write('#pragma once\n')
for h in c_headers:
ofile.write(h)
meson_cpp.write(" 'main.cpp',\n")
meson_c.write(" 'main.c',\n")
open(os.path.join(self.cppdir, 'main.cpp'), 'w').write(self.cpp_main)
open(os.path.join(self.cdir, 'main.c'), 'w').write(self.c_main)
meson_cpp.write(''']
executable('cppbin', srcs)
''')
meson_c.write(''']
executable('cbin', srcs)
''')
if __name__ == '__main__':
g = GenerateCode()
g.run()