7 #define N_STRUCTS 180 /* 1280 */
11 const char *simple_types
[] = { "bool", "char", "short", "int", "float",
12 "double", "long double", "wchar_t", "void *",
16 void gl(const char *c
) {
20 void g(const char *c
) {
29 char base_present
[N_STRUCTS
][N_STRUCTS
];
31 // The return type for each function when doing covariant testcase generation.
32 short ret_types
[N_STRUCTS
][N_FUNCS
*FUNCSPACING
];
34 bool is_ambiguous(int s
, int base
) {
35 for (int i
= 0; i
< N_STRUCTS
; ++i
) {
36 if ((base_present
[base
][i
] & base_present
[s
][i
]) == 1)
42 void add_bases(int s
, int base
) {
43 for (int i
= 0; i
< N_STRUCTS
; ++i
)
44 base_present
[s
][i
] |= base_present
[base
][i
];
47 for (int i
= 0; i
< N_FUNCS
*FUNCSPACING
; ++i
) {
48 if (!ret_types
[base
][i
])
50 if (!ret_types
[s
][i
]) {
51 ret_types
[s
][i
] = ret_types
[base
][i
];
54 if (base_present
[ret_types
[base
][i
]][ret_types
[s
][i
]])
55 // If the return type of the function from this base dominates
56 ret_types
[s
][i
] = ret_types
[base
][i
];
57 if (base_present
[ret_types
[s
][i
]][ret_types
[base
][i
]])
58 // If a previous base dominates
60 // If neither dominates, we'll use this class.
65 // This contains the class that has the final override for
66 // each class, for each function.
67 short final_override
[N_STRUCTS
][N_FUNCS
*FUNCSPACING
];
70 bool polymorphic
= false;
72 static int bases
[N_BASES
];
73 int i_bases
= random() % (N_BASES
*2);
74 if (i_bases
>= N_BASES
)
75 // PARAM: 1/2 of all clases should have no bases
78 bool first_base
= true;
80 // PARAM: 3/4 of all should be class, the rest are structs
81 if (random() % 4 == 0)
91 int base
= random() % (s
-1) + 1;
92 if (!base_present
[s
][base
]) {
93 if (is_ambiguous(s
, base
))
101 if (random()%8 == 0) {
102 // PARAM: 1/8th the bases are virtual
104 // We have a vtable and rtti, but technically we're not polymorphic
105 // polymorphic = true;
108 // PARAM: 1/4 are public, 1/8 are privare, 1/8 are protected, the reset, default
109 int base_protection
= 0;
111 base_protection
= random()%8;
112 switch (base_protection
) {
122 g("private "); break;
124 g("protected "); break;
128 bases
[n_bases
] = base
;
129 base_present
[s
][base
] = base_type
;
138 int n_fields
= N_FIELDS
== 0 ? 0 : random() % (N_FIELDS
*4);
139 // PARAM: 3/4 of all structs should have no members
140 if (n_fields
>= N_FIELDS
)
142 for (int i
= 0; i
< n_fields
; ++i
) {
143 int t
= random() % (sizeof(simple_types
) / sizeof(simple_types
[0]));
144 g(" "); g(simple_types
[t
]); g(" field"); g(i
); gl(";");
147 /* Virtual functions */
148 static int funcs
[N_FUNCS
*FUNCSPACING
];
149 // PARAM: 1/2 of all structs should have no virtual functions
150 int n_funcs
= random() % (N_FUNCS
*2);
151 if (n_funcs
> N_FUNCS
)
154 for (int i
= 0; i
< n_funcs
; ++i
) {
155 int fn
= old_func
+ random() % FUNCSPACING
+ 1;
159 ret_type
= random() % s
+ 1;
160 if (!base_present
[s
][ret_type
]
161 || !base_present
[ret_type
][ret_types
[s
][fn
]])
162 if (ret_types
[s
][fn
]) {
163 printf(" // Found one for s%d for s%d* fun%d.\n", s
,
164 ret_types
[s
][fn
], fn
);
165 ret_type
= ret_types
[s
][fn
];
169 printf(" // Wow found one for s%d for fun%d.\n", s
, fn
);
170 ret_types
[s
][fn
] = ret_type
;
173 g(" virtual s"); g(ret_type
); g("* fun");
175 g(" virtual void fun");
176 g(fn
); g("(char *t) { mix(\"vfn this offset\", (char *)this - t); mix(\"vfn uuid\", "); g(++uuid
);
178 gl("); return 0; }");
181 final_override
[s
][fn
] = s
;
185 // Add required overriders for correctness
186 for (int i
= 0; i
< n_bases
; ++i
) {
189 for (int fn
= 0; fn
< N_FUNCS
*FUNCSPACING
; ++fn
) {
190 // For each possible function
191 int new_base
= final_override
[base
][fn
];
193 // If the base didn't have a final overrider, skip
196 int prev_base
= final_override
[s
][fn
];
198 // Skip functions defined in this class
201 // If we don't want to change the info, skip
202 if (prev_base
== new_base
)
205 if (prev_base
== 0) {
206 // record the final override
207 final_override
[s
][fn
] = new_base
;
211 if (base_present
[prev_base
][new_base
]) {
212 // The previous base dominates the new base, no update necessary
213 printf(" // No override for fun%d in s%d as s%d dominates s%d.\n",
214 fn
, s
, prev_base
, new_base
);
218 if (base_present
[new_base
][prev_base
]) {
219 // The new base dominates the old base, no override necessary
220 printf(" // No override for fun%d in s%d as s%d dominates s%d.\n",
221 fn
, s
, new_base
, prev_base
);
222 // record the final override
223 final_override
[s
][fn
] = new_base
;
227 printf(" // Found we needed override for fun%d in s%d.\n", fn
, s
);
229 // record the final override
230 funcs
[n_funcs
++] = fn
;
231 if (n_funcs
== (N_FUNCS
*FUNCSPACING
-1))
235 if (!ret_types
[s
][fn
]) {
236 ret_types
[s
][fn
] = ret_type
= s
;
238 ret_type
= ret_types
[s
][fn
];
240 printf(" // Calculated return type in s%d as s%d* fun%d.\n",
245 g(" virtual s"); g(ret_type
); g("* fun");
247 g(" virtual void fun");
248 g(fn
); g("(char *t) { mix(\"vfn this offset\", (char *)this - t); mix(\"vfn uuid\", "); g(++uuid
);
250 gl("); return 0; }");
253 final_override
[s
][fn
] = s
;
258 gl(" void calc(char *t) {");
260 // mix in the type number
261 g(" mix(\"type num\", "); g(s
); gl(");");
263 g(" mix(\"type size\", sizeof (s"); g(s
); gl("));");
264 // mix in the this offset
265 gl(" mix(\"subobject offset\", (char *)this - t);");
269 // mix in offset to the complete object under construction
270 gl(" mix(\"real top v current top\", t - (char *)dynamic_cast<void*>(this));");
273 /* check base layout and overrides */
274 for (int i
= 0; i
< n_bases
; ++i
) {
275 g(" calc_s"); g(bases
[i
]); gl("(t);");
279 /* check dynamic_cast to each direct base */
280 for (int i
= 0; i
< n_bases
; ++i
) {
281 g(" if ((char *)dynamic_cast<s"); g(bases
[i
]); gl("*>(this))");
282 g(" mix(\"base dyn cast\", t - (char *)dynamic_cast<s"); g(bases
[i
]); gl("*>(this));");
283 g(" else mix(\"no dyncast\", "); g(++uuid
); gl(");");
287 /* check field layout */
288 for (int i
= 0; i
< n_fields
; ++i
) {
289 g(" mix(\"field offset\", (char *)&field"); g(i
); gl(" - (char *)this);");
292 g(" mix(\"no fields\", "); g(++uuid
); gl(");");
295 /* check functions */
296 for (int i
= 0; i
< n_funcs
; ++i
) {
297 g(" fun"); g(funcs
[i
]); gl("(t);");
300 g(" mix(\"no funcs\", "); g(++uuid
); gl(");");
306 g(" s"); g(s
); g("() ");
308 for (int i
= 0; i
< n_bases
; ++i
) {
314 g("s"); g(bases
[i
]); g("((char *)this)");
316 gl(" { calc((char *)this); }");
317 g(" ~s"); g(s
); gl("() { calc((char *)this); }");
319 // ctor with this to the complete object
320 g(" s"); g(s
); gl("(char *t) { calc(t); }");
321 g(" void calc_s"); g(s
); gl("(char *t) { calc(t); }");
322 g("} a"); g(s
); gl(";");
325 main(int argc
, char **argv
) {
329 seed
= atol(argv
[1]);
331 initstate(seed
, state
, sizeof(state
));
332 gl("extern \"C\" int printf(const char *...);");
334 gl("long long sum;");
335 gl("void mix(const char *desc, long long i) {");
336 // If this ever becomes too slow, we can remove this after we improve the
338 gl(" printf(\"%s: %lld\\n\", desc, i);");
339 gl(" sum += ((sum ^ i) << 3) + (sum<<1) - i;");
342 // PARAM: Randomly size testcases or large testcases?
343 int n_structs
= /* random() % */ N_STRUCTS
;
344 for (int i
= 1; i
< n_structs
; ++i
)
347 gl(" printf(\"%llx\\n\", sum);");