Added support for excluding optional fields.
[wine/testsucceed.git] / tools / winapi / winapi_test
blobc2c7f66caff71eba4791db4d5b5aef350ee6c713
1 #!/usr/bin/perl -w
3 # Copyright 2002 Patrik Stridvall
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 use strict;
22 BEGIN {
23 $0 =~ m%^(.*?/?tools)/winapi/winapi_test$%;
24 require "$1/winapi/setup.pm";
27 use config qw(
28 &file_type &files_skip &files_filter
29 $current_dir $wine_dir $winapi_dir $winapi_check_dir
31 use output qw($output);
32 use winapi_test_options qw($options);
34 if($options->progress) {
35 $output->enable_progress;
36 } else {
37 $output->disable_progress;
40 use c_parser;
41 use tests qw($tests);
42 use type;
43 use util qw(replace_file);
45 my @tests = ();
46 if ($options->pack) {
47 push @tests, "pack";
50 my @files = ();
52 my %files;
54 my %test_dirs;
55 foreach my $test (@tests) {
56 my @test_dirs = $tests->get_test_dirs($test);
57 foreach my $test_dir (@test_dirs) {
58 my @headers = $tests->get_section($test_dir, $test, "header");
59 foreach my $header (@headers) {
60 $files{"include/$header"} = 1;
65 foreach my $test (@tests) {
66 my @test_dirs = $tests->get_test_dirs($test);
67 foreach my $test_dir (@test_dirs) {
68 my @headers = $tests->get_section($test_dir, $test, "header");
69 foreach my $header (@headers) {
70 if($files{"include/$header"}) {
71 push @files, "include/$header";
72 $files{"include/$header"} = 0;
79 my %file2types;
81 my $progress_output;
82 my $progress_current = 0;
83 my $progress_max = scalar(@files);
85 ########################################################################
86 # find_type
88 my %type_name2type;
90 my %defines = (
91 "ANYSIZE_ARRAY" => 1,
92 "CCHDEVICENAME" => 32,
93 "ELF_VENDOR_SIZE" => 4,
94 "EXCEPTION_MAXIMUM_PARAMETERS" => 15,
95 "HW_PROFILE_GUIDLEN" => 39,
96 "IMAGE_NUMBEROF_DIRECTORY_ENTRIES" => 16,
97 "IMAGE_SIZEOF_SHORT_NAME" => 8,
98 "LF_FACESIZE" => 32,
99 "LF_FULLFACESIZE" => 64,
100 "MAXIMUM_SUPPORTED_EXTENSION" => 512,
101 "MAX_PATH" => 260,
102 "MAX_PROFILE_LEN" => 80,
103 "OFS_MAXPATHNAME" => 128,
104 "SIZE_OF_80387_REGISTERS" => 80,
105 "TOKEN_SOURCE_LENGTH" => 8,
108 my %align_kludge_reported = ("FILETIME" => 1, "LARGE_INTEGER" => 1);
110 sub find_align {
111 my $type_name = shift;
113 local $_ = $type_name;
115 # Remove "count" and "bits"
116 s/^(.*?)\s*(?:\[\s*(.*?)\s*\]|:(\d+))?$/$1/;
118 my $align;
119 if (0) {
120 # Nothing
121 } elsif (/\*+$/) {
122 $align = 4;
123 } elsif(/^(?:(?:signed\s+|unsigned\s+)?char)$/) {
124 $align = 1;
125 } elsif (/^(?:(?:signed\s+|unsigned\s+)?short)$/) {
126 $align = 2;
127 } elsif (/^(?:wchar_t)$/) {
128 $align = 2;
129 } elsif (/^(?:(?:signed\s+|unsigned\s+)?(?:__int32|int|long(?:\s+int)?)|unsigned|signed)$/) {
130 $align = 4;
131 } elsif (/^(?:float)$/) {
132 $align = 4;
133 } elsif (/^(?:signed\s+|unsigned\s+)?__int64$/) {
134 $align = 4;
135 } elsif (/^(?:double)$/) {
136 $align = 4;
137 } elsif (/^(?:long\s+double)$/) {
138 $align = 4;
139 } elsif (/^H(?:DC|BITMAP|BRUSH|ICON|INSTANCE|MENU|METAFILE|WND)$/) {
140 $align = 4;
141 } elsif (/^LP(?:CSTR|CWSTR|DWORD|STR|VOID|THREAD_START_ROUTINE|WSTR)$/) {
142 $align = 4;
143 } elsif (/^(?:(?:MSGBOX)CALLBACK[AW]?|(?:FAR|WND)PROC[AW]?)$/) {
144 $align = 4;
145 } elsif (/^(?:FILETIME|LARGE_INTEGER|LONGLONG)$/) {
146 $align = 4;
147 } else {
148 $align = undef;
151 my $align2;
152 if (defined(my $type = $type_name2type{$_})) {
153 $align2 = $type->align;
156 if (!defined($align)) {
157 $align = $align2;
158 } elsif (defined($align2) && !$align_kludge_reported{$_}) {
159 $align_kludge_reported{$_} = 1;
160 $output->write("$type_name: type needn't be kludged\n");
163 if (!defined($align)) {
164 # $output->write("$type_name: can't find type\n");
167 return $align;
170 my %size_kludge_reported = ("FILETIME" => 1, "LARGE_INTEGER" => 1);
171 my %size_parse_reported;
173 sub find_size {
174 my $type_name = shift;
176 local $_ = $type_name;
178 my $count;
179 my $bits;
180 if (s/^(.*?)\s*(?:\[\s*(.*?)\s*\]|:(\d+))?$/$1/) {
181 $count = $2;
182 $bits = $3;
185 my $size;
186 if (0) {
187 # Nothing
188 } elsif (/\*+$/) {
189 $size = 4;
190 } elsif(/^(?:(?:signed\s+|unsigned\s+)?char)$/) {
191 $size = 1;
192 } elsif (/^(?:(?:signed\s+|unsigned\s+)?short)$/) {
193 $size = 2;
194 } elsif (/^(?:wchar_t)$/) {
195 $size = 2;
196 } elsif (/^(?:(?:signed\s+|unsigned\s+)?(?:__int32|int|long(?:\s+int)?)|unsigned|signed)$/) {
197 $size = 4;
198 } elsif (/^(?:float)$/) {
199 $size = 4;
200 } elsif (/^(?:signed\s+|unsigned\s+)?__int64$/) {
201 $size = 8;
202 } elsif (/^(?:double)$/) {
203 $size = 8;
204 } elsif (/^(?:long\s+double)$/) {
205 $size = 10; # ???
206 } elsif (/^H(?:DC|BITMAP|BRUSH|ICON|INSTANCE|MENU|METAFILE|WND)$/) {
207 $size = 4;
208 } elsif (/^LP(?:CSTR|CWSTR|DWORD|STR|VOID|THREAD_START_ROUTINE|WSTR)$/) {
209 $size = 4;
210 } elsif (/^(?:(?:MSGBOX)CALLBACK[AW]?|(?:FAR|WND)PROC[AW]?)$/) {
211 $size = 4;
212 } elsif (/^(?:FILETIME|LARGE_INTEGER|LONGLONG)$/) {
213 $size = 8;
214 } elsif (/^(?:struct|union)$/) {
215 if (!$size_parse_reported{$_}) {
216 $output->write("$type_name: can't parse type\n");
217 $size_parse_reported{$_} = 1;
219 $size = undef;
220 } else {
221 $size = undef;
224 my $size2;
225 if (defined(my $type = $type_name2type{$_})) {
226 $size2 = $type->size;
229 if (!defined($size)) {
230 $size = $size2;
231 } elsif (defined($size2) && !$size_kludge_reported{$_}) {
232 $size_kludge_reported{$_} = 1;
233 $output->write("$type_name: type needn't be kludged\n");
236 if (!defined($size)) {
237 # $output->write("$type_name: can't find type\n");
238 } elsif (defined($count)) {
239 if ($count =~ /^\d+$/) {
240 $size *= int($count);
241 } elsif (defined($count = $defines{$count})) {
242 $size *= int($count);
243 } else {
244 $output->write("$type_name: can't parse type\n");
245 $size = undef;
247 } elsif (defined($bits)) {
248 $size = -$bits;
251 return $size;
254 foreach my $file (@files) {
255 $progress_current++;
258 open(IN, "< $wine_dir/$file");
259 local $/ = undef;
260 $_ = <IN>;
261 close(IN);
264 my $max_line = 0;
266 local $_ = $_;
267 while(s/^.*?\n//) { $max_line++; }
268 if($_) { $max_line++; }
271 my $parser = new c_parser($file);
273 my $line;
274 my $type;
275 my @packs = (4);
277 my $update_output = sub {
278 my $progress = "";
279 my $prefix = "";
281 $progress .= "$file (file $progress_current of $progress_max)";
282 $prefix .= "$file: ";
284 if(defined($line)) {
285 $progress .= ": line $line of $max_line";
288 $output->progress($progress);
289 $output->prefix($prefix);
292 &$update_output();
294 my $found_line = sub {
295 $line = shift;
297 &$update_output;
299 $parser->set_found_line_callback($found_line);
301 my $found_preprocessor = sub {
302 my $begin_line = shift;
303 my $begin_column = shift;
304 my $preprocessor = shift;
306 local $_ = $preprocessor;
307 if (/^\#\s*include\s+\"pshpack(\d+)\.h\"$/) {
308 push @packs, $1;
309 } elsif(/^\#\s*include\s+\"poppack\.h\"$/) {
310 pop @packs;
313 return 1;
315 $parser->set_found_preprocessor_callback($found_preprocessor);
317 my $found_type = sub {
318 $type = shift;
320 &$update_output();
322 my $name = $type->name;
323 $file2types{$file}{$name} = $type;
325 $type->set_find_align_callback(\&find_align);
326 $type->set_find_size_callback(\&find_size);
328 my $pack = $packs[$#packs];
329 if (!defined($type->pack)) {
330 $type->pack($pack);
332 my $size = $type->size();
333 if (defined($size)) {
334 my $max_field_base_size = 0;
336 foreach my $field ($type->fields()) {
337 my $field_type_name = $field->type_name;
338 my $field_name = $field->name;
339 my $field_size = $field->size;
340 my $field_base_size = $field->base_size;
341 my $field_offset = $field->offset;
342 my $field_align = $field->align;
344 # $output->write("$name: $field_type_name: $field_name: $field_offset: $field_size($field_base_size): $field_align\n");
346 # $output->write("$name: $size\n");
348 $type_name2type{$name} = $type;
349 } else {
350 # $output->write("$name: can't find size\n");
353 return 1;
355 $parser->set_found_type_callback($found_type);
358 my $line = 1;
359 my $column = 0;
360 if(!$parser->parse_c_file(\$_, \$line, \$column)) {
361 $output->write("can't parse file\n");
365 $output->prefix("");
368 ########################################################################
369 # output_header
371 sub output_header {
372 local *OUT = shift;
374 my $test_dir = shift;
375 my @tests = @{(shift)};
377 print OUT "/* File generated automatically from tools/winapi/test.dat; do not edit! */\n";
378 print OUT "/* This file can be copied, modified and distributed without restriction. */\n";
379 print OUT "\n";
381 print OUT "/*\n";
382 foreach my $test (@tests) {
383 my @description = $tests->get_section($test_dir, $test, "description");
384 foreach my $description (@description) {
385 print OUT " * $description\n";
388 print OUT " */\n";
389 print OUT "\n";
391 print OUT "#define WINVER 0x0501\n";
392 print OUT "#define _WIN32_WINNT 0x0501\n";
393 print OUT "\n";
394 print OUT "#define WINE_NOWINSOCK\n";
395 print OUT "\n";
396 foreach my $test (@tests) {
397 my @includes = $tests->get_section($test_dir, $test, "include");
398 foreach my $include (@includes) {
399 print OUT "#include \"$include\"\n";
402 print OUT "\n";
403 print OUT "#include \"wine/test.h\"\n";
404 print OUT "\n";
406 print OUT "/***********************************************************************\n";
407 print OUT " * Compability macros\n";
408 print OUT " */\n";
409 print OUT "\n";
410 print OUT "#define DWORD_PTR UINT_PTR\n";
411 print OUT "#define LONG_PTR INT_PTR\n";
412 print OUT "#define ULONG_PTR UINT_PTR\n";
413 print OUT "\n";
415 print OUT "/***********************************************************************\n";
416 print OUT " * Windows API extension\n";
417 print OUT " */\n";
418 print OUT "\n";
419 print OUT "#if (_MSC_VER >= 1300) && defined(__cplusplus)\n";
420 print OUT "# define FIELD_ALIGNMENT(type, field) __alignof(((type*)0)->field)\n";
421 print OUT "#elif defined(__GNUC__)\n";
422 print OUT "# define FIELD_ALIGNMENT(type, field) __alignof__(((type*)0)->field)\n";
423 print OUT "#else\n";
424 print OUT "/* FIXME: Not sure if is possible to do without compiler extension */\n";
425 print OUT "#endif\n";
426 print OUT "\n";
427 print OUT "/***********************************************************************\n";
428 print OUT " * Test helper macros\n";
429 print OUT " */\n";
430 print OUT "\n";
431 print OUT "#ifdef FIELD_ALIGNMENT\n";
432 print OUT "# define TEST_FIELD_ALIGNMENT(type, field, align) \\\n";
433 print OUT " ok(FIELD_ALIGNMENT(type, field) == align, \\\n";
434 print OUT " \"FIELD_ALIGNMENT(\" #type \", \" #field \") == %d (expected \" #align \")\", \\\n";
435 print OUT " FIELD_ALIGNMENT(type, field))\n";
436 print OUT "#else\n";
437 print OUT "# define TEST_FIELD_ALIGNMENT(type, field, align) do { } while (0)\n";
438 print OUT "#endif\n";
439 print OUT "\n";
440 print OUT "#define TEST_FIELD_OFFSET(type, field, offset) \\\n";
441 print OUT " ok(FIELD_OFFSET(type, field) == offset, \\\n";
442 print OUT " \"FIELD_OFFSET(\" #type \", \" #field \") == %ld (expected \" #offset \")\", \\\n";
443 print OUT " FIELD_OFFSET(type, field))\n";
444 print OUT "\n";
445 print OUT "#define TEST_TYPE_ALIGNMENT(type, align) \\\n";
446 print OUT " ok(TYPE_ALIGNMENT(type) == align, \"TYPE_ALIGNMENT(\" #type \") == %d (expected \" #align \")\", TYPE_ALIGNMENT(type))\n";
447 print OUT "\n";
448 print OUT "#define TEST_TYPE_SIZE(type, size) \\\n";
449 print OUT " ok(sizeof(type) == size, \"sizeof(\" #type \") == %d (expected \" #size \")\", sizeof(type))\n";
450 print OUT "\n";
451 print OUT "/***********************************************************************\n";
452 print OUT " * Test macros\n";
453 print OUT " */\n";
454 print OUT "\n";
455 print OUT "#define TEST_FIELD(type, field_type, field_name, field_offset, field_size, field_align) \\\n";
456 print OUT " TEST_TYPE_SIZE(field_type, field_size); \\\n";
457 print OUT " TEST_FIELD_ALIGNMENT(type, field_name, field_align); \\\n";
458 print OUT " TEST_FIELD_OFFSET(type, field_name, field_offset); \\\n";
459 print OUT "\n";
460 print OUT "#define TEST_TYPE(type, size, align) \\\n";
461 print OUT " TEST_TYPE_ALIGNMENT(type, align); \\\n";
462 print OUT " TEST_TYPE_SIZE(type, size)\n";
463 print OUT "\n";
466 ########################################################################
467 # output_footer
469 sub output_footer {
470 local *OUT = shift;
472 my $test_dir = shift;
473 my @tests = @{(shift)};
475 print OUT "START_TEST(generated)\n";
476 print OUT "{\n";
477 foreach my $test (@tests) {
478 print OUT " test_$test();\n";
480 print OUT "}\n";
483 ########################################################################
484 # output_test_pack_type
486 sub output_test_pack_type {
487 local *OUT = shift;
489 my $types = shift;
490 my $type_name = shift;
491 my $type = shift;
493 my $type_align = $type->align;
494 my $type_pack = $type->pack;
495 my $type_size = $type->size;
497 print OUT " /* $type_name (pack $type_pack) */\n";
498 if (defined($type_align) && defined($type_size)) {
499 print OUT " TEST_TYPE($type_name, $type_size, $type_align);\n";
503 sub output_test_pack_fields {
504 local *OUT = shift;
506 my $types = shift;
507 my $type_name = shift;
508 my $type = shift;
509 my $offset = shift;
510 my $optional_field = shift;
512 foreach my $field ($type->fields()) {
513 my $field_type_name = $field->type_name;
514 my $field_name = $field->name;
515 my $field_size = $field->size;
516 my $field_offset = $field->offset;
517 my $field_align = $field->align;
519 next if $field_name eq "" || (defined($field_size) && $field_size < 0);
521 if ($$optional_field{$field_name}) {
522 # Nothing
523 } elsif (defined($field_size) && defined($field_offset)) {
524 $field_offset += $offset;
525 if ($field_name eq "DUMMYSTRUCTNAME") {
526 print OUT "#ifdef NONAMELESSSTRUCT\n";
527 print OUT " TEST_FIELD($type_name, $field_type_name, $field_name, ";
528 print OUT "$field_offset, $field_size, $field_align);\n";
529 print OUT "#else\n";
530 output_test_pack_fields(\*OUT, $types, $type_name, $$types{$field_type_name}, $field_offset, $optional_field);
531 print OUT "#endif\n";
532 } else {
533 print OUT " TEST_FIELD($type_name, $field_type_name, $field_name, ";
534 print OUT "$field_offset, $field_size, $field_align);\n";
536 } else {
537 $output->write("$type_name: $field_type_name: $field_name: test not generated (offset not defined)\n");
542 ########################################################################
543 # output_test_pack
545 sub output_test_pack {
546 local *OUT = shift;
548 my $test_dir = shift;
549 my $test = shift;
551 my @headers = $tests->get_section($test_dir, $test, "header");
552 my @type_names = $tests->get_section($test_dir, $test, "struct");
554 my %type_name_not_used;
556 foreach my $_type_name (@type_names) {
557 my $type_name = $_type_name;
559 $type_name =~ s/:.*?$//;
560 $type_name_not_used{$type_name} = 1;
563 foreach my $header (@headers) {
564 my $types = $file2types{"include/$header"};
566 foreach my $_type_name (@type_names) {
567 my $type_name = $_type_name;
569 my %optional_field = ();
570 if ($type_name =~ s/:\s*(.*?)$//) {
571 my @fields = split /\s+/, $1;
572 foreach my $field (@fields) {
573 if ($field =~ s/^!//) {
574 $optional_field{$field}++;
579 my $type = $$types{$type_name};
580 if (!defined($type)) {
581 next;
583 $type_name_not_used{$type_name} = 0;
585 if (!scalar(keys(%optional_field))) {
586 output_test_pack_type(\*OUT, $types, $type_name, $type);
587 } else {
588 print OUT " /* $type_name */\n";
590 output_test_pack_fields(\*OUT, $types, $type_name, $type, 0, \%optional_field);
591 print OUT "\n";
595 foreach my $_type_name (@type_names) {
596 my $type_name = $_type_name;
597 $type_name =~ s/:.*?$//;
598 if ($type_name_not_used{$type_name}) {
599 # $output->write("$test_dir: $test: $type_name: type not found (ignored)\n");
605 ########################################################################
606 # output_file
608 sub output_file {
609 local *OUT = shift;
611 my $test_dir = shift;
612 my @tests = @{(shift)};
614 output_header(\*OUT, $test_dir, \@tests);
616 foreach my $test (@tests) {
617 print OUT "void test_$test(void)\n";
618 print OUT "{\n";
620 if ($test eq "pack") {
621 output_test_pack(\*OUT, $test_dir, $test);
622 } else {
623 die "no such test ($test)\n";
626 print OUT "}\n";
627 print OUT "\n";
630 output_footer(\*OUT, $test_dir, \@tests);
632 return 1;
635 ########################################################################
636 # main
638 my @test_dirs = $tests->get_test_dirs();
639 foreach my $test_dir (@test_dirs) {
640 my $file = "$wine_dir/$test_dir/generated.c";
641 replace_file($file, \&output_file, $test_dir, \@tests);