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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 $0 =~ m%^(.*?/?tools)/winapi/winapi_test$%;
24 require "$1/winapi/setup.pm";
28 file_type files_skip files_filter
29 $current_dir $wine_dir $winapi_dir
31 use output qw($output);
32 use winapi_test_options qw($options);
34 if($options->progress) {
35 $output->enable_progress;
37 $output->disable_progress;
43 use util qw(replace_file);
56 foreach my $test (@tests)
58 foreach my $test_dir ($tests->get_test_dirs($test))
60 foreach my $header ($tests->get_section($test_dir, $test, "header"))
64 push @files, "include/$header";
73 my $file = "tests.dat";
75 $file .= "2"; # FIXME: For tests
77 open(OUT, "> $winapi_dir/$file") || die "Error: Can't open $winapi_dir/$file: $!\n";
80 my @test_dirs = $tests->get_test_dirs();
81 foreach my $test_dir (@test_dirs) {
82 print OUT "\n" if $x++;
83 print OUT "%%%$test_dir\n";
87 my @tests = $tests->get_tests($test_dir);
88 foreach my $test (@tests) {
89 print OUT "\n" if $y++;
90 print OUT "%%$test\n";
96 my @sections = $tests->get_sections($test_dir, $test);
97 foreach my $section (@sections) {
98 my @lines = $tests->get_section($test_dir, $test, $section);
100 if ($section =~ /^(?:struct|type)$/) {
101 foreach my $line (@lines) {
107 print OUT "\n" if $z++;
108 print OUT "%$section\n";
110 foreach my $line (@lines) {
115 @types = sort { $x = $a; $y = $b; $x =~ s/^!//; $y =~ s/^!//; $x cmp $y } @types;
117 print OUT "\n" if $z++;
120 foreach my $type (@types) {
134 my $progress_current = 0;
135 my $progress_max = scalar(@files);
137 ########################################################################
143 "ANYSIZE_ARRAY" => 1,
144 "CCHDEVICENAME" => 32,
145 "CCHILDREN_TITLEBAR+1" => 6,
146 "ELF_VENDOR_SIZE" => 4,
147 "EXCEPTION_MAXIMUM_PARAMETERS" => 15,
148 "HW_PROFILE_GUIDLEN" => 39,
149 "IMAGE_NUMBEROF_DIRECTORY_ENTRIES" => 16,
150 "IMAGE_SIZEOF_SHORT_NAME" => 8,
152 "LF_FULLFACESIZE" => 64,
153 "MAXIMUM_SUPPORTED_EXTENSION" => 512,
154 "MAX_GOPHER_DISPLAY_TEXT + 1" => 129,
155 "MAX_GOPHER_LOCATOR_LENGTH + 1" => 654,
157 "MAX_PROFILE_LEN" => 80,
159 "OFS_MAXPATHNAME" => 128,
160 "SIZE_OF_80387_REGISTERS" => 80,
161 "TOKEN_SOURCE_LENGTH" => 8,
164 my %align_kludge_reported = ("FILETIME" => 1, "LARGE_INTEGER" => 1);
165 my %size_kludge_reported = ("FILETIME" => 1, "LARGE_INTEGER" => 1);
166 my %size_parse_reported;
168 sub _find_align_kind_size($) {
169 my $type_name = shift;
171 local $_ = $type_name;
177 $align = $pointer_size;
179 $size = $pointer_size;
180 } elsif(/^(?:(signed|unsigned)\s+)?(?:__int8|char|byte)$/) {
182 $kind = defined($1) ? $1 : "signed";
184 } elsif (/^(?:(signed|unsigned)\s+)?(?:__int16|short(?:\s+int)?)$/) {
186 $kind = defined($1) ? $1 : "signed";
188 } elsif (/^(?:wchar_t)$/) {
192 } elsif (/^(signed|unsigned)$/) {
194 $kind = defined($1) ? $1 : "signed";
196 } elsif (/^(?:(signed|unsigned)\s+)?(?:__int32|int)$/) {
198 $kind = defined($1) ? $1 : "signed";
200 } elsif (/^(?:(signed|unsigned)\s+)?(?:long(?:\s+int)?)$/) {
201 $align = $pointer_size;
202 $kind = defined($1) ? $1 : "signed";
203 $size = $pointer_size;
204 } elsif (/^(?:float)$/) {
208 } elsif (/^(?:(signed|unsigned)\s+)?__int64$/) {
210 $kind = defined($1) ? $1 : "signed";
212 } elsif (/^(?:double|DOUBLE|DATE)$/) {
216 } elsif (/^(?:long\s+double)$/) {
220 } elsif (/^H(?:DC|BITMAP|BRUSH|ICON|INSTANCE|KEY|MENU|METAFILE|WND)$/) {
221 $align = $pointer_size;
223 $size = $pointer_size;
224 } elsif (/^LP(?:BYTE|CSTR|CWSTR|DWORD|STR|VOID|WSTR)$/) {
225 $align = $pointer_size;
227 $size = $pointer_size;
228 } elsif (/^(?:FILETIME)$/) {
236 } elsif (/^(?:VOID)$/) {
240 } elsif (/^(?:SHORT)$/) {
244 } elsif (/^(?:BYTE)$/) {
248 } elsif (/^(?:DWORD)$/) {
252 } elsif (/^(?:WORD)$/) {
256 } elsif (/^(?:INT64|LONG64|LONGLONG)$/) {
260 } elsif (/^(?:UINT64|ULONG64|DWORD64|ULONGLONG|DWORDLONG)$/) {
264 } elsif (/^(?:LARGE_INTEGER)$/) {
268 } elsif (/^(?:POINTS)$/) {
272 } elsif (/^(struct|union)$/) {
274 if (!$size_parse_reported{$_}) {
275 $output->write("$type_name: can't parse type\n");
276 $size_parse_reported{$_} = 1;
278 } elsif (/^\w+\s*\((?:\s*CALLBACK|\s*NTAPI|\s*WINAPI)?\s*\*\s*\)\s*\(.*?\)$/) {
279 $align = $pointer_size;
281 $size = $pointer_size;
285 if (defined(my $type = $type_name2type{$pointer_size}{$_})) {
286 $align2 = $type->align;
289 if (!defined($align)) {
291 } elsif (defined($align2) && !$align_kludge_reported{$_}) {
292 $align_kludge_reported{$_} = 1;
293 $output->write("$type_name: type needn't be kludged\n");
296 if (!defined($align)) {
297 # $output->write("$type_name: can't find type\n");
301 if (defined(my $type = $type_name2type{$pointer_size}{$_})) {
302 $size2 = $type->size;
305 if (!defined($size)) {
307 } elsif (defined($size2) && !$size_kludge_reported{$_}) {
308 $size_kludge_reported{$_} = 1;
309 $output->write("$type_name: type needn't be kludged\n");
312 return ($align, $kind, $size);
316 my $type_name = shift;
317 (my $align, my $kind, my $size) = _find_align_kind_size($type_name);
322 my $type_name = shift;
323 (my $align, my $kind, my $size) = _find_align_kind_size($type_name);
329 my $type_name = shift;
330 (my $align, my $kind, my $size) = _find_align_kind_size($type_name);
336 return $defines{$count};
339 foreach my $file (@files) {
341 foreach my $ptr (4, 8) {
342 $pointer_size = $ptr;
345 open(IN, "< $wine_dir/$file") || die "Error: Can't open $wine_dir/$file: $!\n";
354 while(s/^.*?\n//) { $max_line++; }
355 if($_) { $max_line++; }
358 my $parser = new c_parser($file);
365 my $update_output = sub {
369 $progress .= "$file (file $progress_current of $progress_max" . sprintf ", %u-bit)", $pointer_size * 8;
370 $prefix .= "$file: ";
373 $progress .= ": line $line of $max_line";
376 $output->progress($progress);
377 $output->prefix($prefix);
382 my $found_line = sub {
387 $parser->set_found_line_callback($found_line);
389 my $found_preprocessor = sub {
390 my $begin_line = shift;
391 my $begin_column = shift;
392 my $preprocessor = shift;
394 #print "found_preprocessor: $begin_line: [$_]\n";
395 if ($preprocessor =~ /^\#\s*include\s+[\"<]pshpack(\d+)\.h[\">]$/) {
396 push @packs, $1 unless @ifdefs && !$ifdefs[$#ifdefs];
397 #print "found pack $1 on line $begin_line\n";
398 } elsif($preprocessor =~ /^\#\s*include\s+[\"<]poppack\.h[\">]$/) {
399 pop @packs unless @ifdefs && !$ifdefs[$#ifdefs];
400 #print "found poppack on line $begin_line\n";
401 } elsif ($preprocessor =~ /^\#\s*ifdef\s+_WIN64/) {
402 push @ifdefs, ($pointer_size == 8);
403 } elsif ($preprocessor =~ /^\#\s*ifndef\s+_WIN64/) {
404 push @ifdefs, ($pointer_size == 4);
405 } elsif ($preprocessor =~ /^\#\s*elif\s+defined(_WIN64)/) {
406 $ifdefs[$#ifdefs] = ($pointer_size == 8);
407 } elsif ($preprocessor =~ /^\#\s*ifdef\s/) {
409 } elsif ($preprocessor =~ /^\#\s*ifndef\s/) {
411 } elsif ($preprocessor =~ /^\#\s*if/) {
413 } elsif ($preprocessor =~ /^\#\s*else/) {
414 $ifdefs[$#ifdefs] = $ifdefs[$#ifdefs] ^ 1;
415 } elsif ($preprocessor =~ /^\#\s*elif/) {
416 $ifdefs[$#ifdefs] = 2;
417 } elsif ($preprocessor =~ /^\#\s*endif/) {
422 $parser->set_found_preprocessor_callback($found_preprocessor);
424 my $found_type = sub {
426 return if @ifdefs && !$ifdefs[$#ifdefs];
430 my $name = $type->name;
431 $file2types{$pointer_size}{$file}{$name} = $type;
433 $type->set_find_align_callback(\&find_align);
434 $type->set_find_kind_callback(\&find_kind);
435 $type->set_find_size_callback(\&find_size);
436 $type->set_find_count_callback(\&find_count);
438 my $pack = $packs[$#packs];
439 if (!defined($type->pack) && $type->kind =~ /^(?:struct|union)$/) {
442 my $size = $type->size();
443 if (defined($size)) {
444 my $max_field_base_size = 0;
446 foreach my $field ($type->fields()) {
447 my $field_type_name = $field->type_name;
448 my $field_name = $field->name;
449 my $field_size = $field->size;
450 my $field_base_size = $field->base_size;
451 my $field_offset = $field->offset;
452 my $field_align = $field->align;
454 # $output->write("$name: $field_type_name: $field_name: $field_offset: $field_size($field_base_size): $field_align\n");
456 # $output->write("$name: $size\n");
458 $type_name2type{$pointer_size}{$name} = $type;
460 # $output->write("$name: can't find size\n");
465 $parser->set_found_type_callback($found_type);
470 if(!$parser->parse_c_file(\$_, \$line, \$column)) {
471 $output->write("can't parse file\n");
479 ########################################################################
482 sub output_header($$$) {
485 my $test_dir = shift;
486 my @tests = @{(shift)};
488 print OUT "/* File generated automatically from tools/winapi/tests.dat; do not edit! */\n";
489 print OUT "/* This file can be copied, modified and distributed without restriction. */\n";
493 foreach my $test (@tests) {
494 my @description = $tests->get_section($test_dir, $test, "description");
495 foreach my $description (@description) {
496 print OUT " * $description\n";
502 print OUT "#define WINVER 0x0501\n";
503 print OUT "#define _WIN32_IE 0x0501\n";
504 print OUT "#define _WIN32_WINNT 0x0501\n";
506 print OUT "#define WINE_NOWINSOCK\n";
508 foreach my $test (@tests) {
509 my @includes = $tests->get_section($test_dir, $test, "include");
510 foreach my $include (@includes) {
511 print OUT "#include $include\n";
515 print OUT "#include \"wine/test.h\"\n";
518 print OUT "/***********************************************************************\n";
519 print OUT " * Compatibility macros\n";
522 print OUT "#define DWORD_PTR UINT_PTR\n";
523 print OUT "#define LONG_PTR INT_PTR\n";
524 print OUT "#define ULONG_PTR UINT_PTR\n";
527 print OUT "/***********************************************************************\n";
528 print OUT " * Windows API extension\n";
531 print OUT "#if defined(_MSC_VER) && (_MSC_VER >= 1300) && defined(__cplusplus)\n";
532 print OUT "# define _TYPE_ALIGNMENT(type) __alignof(type)\n";
533 print OUT "#elif defined(__GNUC__)\n";
534 print OUT "# define _TYPE_ALIGNMENT(type) __alignof__(type)\n";
537 print OUT " * FIXME: May not be possible without a compiler extension\n";
538 print OUT " * (if type is not just a name that is, otherwise the normal\n";
539 print OUT " * TYPE_ALIGNMENT can be used)\n";
541 print OUT "#endif\n";
543 print OUT "#if defined(TYPE_ALIGNMENT) && defined(_MSC_VER) && _MSC_VER >= 800 && !defined(__cplusplus)\n";
544 print OUT "#pragma warning(disable:4116)\n";
545 print OUT "#endif\n";
547 print OUT "#if !defined(TYPE_ALIGNMENT) && defined(_TYPE_ALIGNMENT)\n";
548 print OUT "# define TYPE_ALIGNMENT _TYPE_ALIGNMENT\n";
549 print OUT "#endif\n";
552 print OUT "/***********************************************************************\n";
553 print OUT " * Test helper macros\n";
556 print OUT "#define TEST_TYPE_SIZE(type, size) C_ASSERT(sizeof(type) == size);\n";
558 print OUT "#ifdef TYPE_ALIGNMENT\n";
559 print OUT "# define TEST_TYPE_ALIGN(type, align) C_ASSERT(TYPE_ALIGNMENT(type) == align);\n";
561 print OUT "# define TEST_TYPE_ALIGN(type, align)\n";
562 print OUT "#endif\n";
564 print OUT "#ifdef _TYPE_ALIGNMENT\n";
565 print OUT "# define TEST_TARGET_ALIGN(type, align) C_ASSERT(_TYPE_ALIGNMENT(*(type)0) == align);\n";
566 print OUT "# define TEST_FIELD_ALIGN(type, field, align) C_ASSERT(_TYPE_ALIGNMENT(((type*)0)->field) == align);\n";
568 print OUT "# define TEST_TARGET_ALIGN(type, align)\n";
569 print OUT "# define TEST_FIELD_ALIGN(type, field, align)\n";
570 print OUT "#endif\n";
572 print OUT "#define TEST_FIELD_OFFSET(type, field, offset) C_ASSERT(FIELD_OFFSET(type, field) == offset);\n";
574 print OUT "#define TEST_TARGET_SIZE(type, size) TEST_TYPE_SIZE(*(type)0, size)\n";
575 print OUT "#define TEST_FIELD_SIZE(type, field, size) TEST_TYPE_SIZE((((type*)0)->field), size)\n";
576 print OUT "#define TEST_TYPE_SIGNED(type) C_ASSERT((type) -1 < 0);\n";
577 print OUT "#define TEST_TYPE_UNSIGNED(type) C_ASSERT((type) -1 > 0);\n";
582 ########################################################################
585 sub output_footer($$$) {
588 my $test_dir = shift;
589 my @tests = @{(shift)};
591 print OUT "START_TEST(generated)\n";
593 foreach my $test (@tests) {
594 print OUT " test_$test();\n";
599 ########################################################################
600 # output_test_pack_type
602 sub output_test_pack_type($$$$$$) {
605 my $type_name2type = shift;
606 my $type_name2optional = shift;
607 my $type_name2optional_fields = shift;
608 my $type_name = shift;
611 my $optional_fields = $$type_name2optional_fields{$type_name};
613 my $type_align = $type->align;
614 my $type_pack = $type->pack;
615 my $type_size = $type->size;
616 my $type_kind = $type->kind;
618 if (defined($type_pack)) {
619 print OUT " /* $type_name (pack $type_pack) */\n";
621 print OUT " /* $type_name */\n";
624 if (!scalar(keys(%$optional_fields)) && defined($type_align) && defined($type_size)) {
625 print OUT " TEST_TYPE_SIZE ($type_name, $type_size)\n";
626 print OUT " TEST_TYPE_ALIGN ($type_name, $type_align)\n";
629 if ($type_kind eq "float") {
631 } elsif ($type_kind eq "pointer") {
632 my $dereference_type;
633 $dereference_type = sub {
636 my @fields = $type->fields;
637 my $type_name2 =$fields[0]->type_name;
639 if ($type_name2 =~ s/\s*\*$//) {
640 my $type2 = $$type_name2type{$type_name2};
641 if (defined($type2)) {
644 if ($type_name2 !~ /^(?:PVOID|VOID|void)$/) {
645 $output->write("$type_name2: warning: type not found 1\n");
649 } elsif ($type_name2 =~ /^\w+$/) {
650 my $type2 = $$type_name2type{$type_name2};
651 if (defined($type2)) {
652 return &$dereference_type($type2);
654 $output->write("$type_name2: warning: type not found\n");
657 } elsif ($type_name2 =~ /^\w+\s*\((?:\s*CALLBACK|\s*NTAPI|\s*WINAPI)?\s*\*\s*\)\s*\(.*?\)$/) {
660 $output->write("$type_name2: warning: type can't be parsed\n");
665 my $type2 = &$dereference_type($type);
666 if (defined($type2)) {
667 my $type_name2 = $type2->name;
668 my $type_align2 = $type2->align;
669 my $type_size2 = $type2->size;
671 my $optional = $$type_name2optional{$type_name};
672 my $optional_fields2 = $$type_name2optional_fields{$type_name2};
674 if (!$optional && !scalar(keys(%$optional_fields2)) && defined($type_align2) && defined($type_size2)) {
675 print OUT " TEST_TARGET_SIZE ($type_name, $type_size2)\n";
676 print OUT " TEST_TARGET_ALIGN($type_name, $type_align2)\n";
678 # $output->write("$type_name: warning: type size not found\n");
681 } elsif ($type_kind eq "signed") {
682 print OUT " TEST_TYPE_SIGNED($type_name)\n";
683 } elsif ($type_kind eq "unsigned") {
684 print OUT " TEST_TYPE_UNSIGNED($type_name)\n";
688 sub output_test_pack_fields($$$$$$$);
689 sub output_test_pack_fields($$$$$$$) {
692 my $type_name2type = shift;
693 my $type_name2optional = shift;
694 my $type_name2optional_fields = shift;
695 my $type_name = shift;
699 my $optional_fields = $$type_name2optional_fields{$type_name};
701 foreach my $field ($type->fields()) {
702 my $field_type_name = $field->type_name;
703 $field_type_name =~ s/\s+DECLSPEC_ALIGN\(\d+\)//;
704 my $field_name = $field->name;
705 my $field_size = $field->size;
706 my $field_offset = $field->offset;
707 my $field_align = $field->align;
709 next if $field_name eq "" || (defined($field_size) && $field_size < 0);
710 # We cannot take the address of a bitfield with MSVC
711 next if ($field_type_name =~ /:/);
713 if ($$optional_fields{$field_name}) {
715 } elsif (defined($field_size) && defined($field_offset)) {
716 $field_offset += $offset;
717 if ($field_name eq "DUMMYSTRUCTNAME") {
718 print OUT "#ifdef NONAMELESSSTRUCT\n";
719 print OUT " TEST_TYPE_SIZE ($type_name.$field_name, $field_size)\n";
720 print OUT " TEST_FIELD_ALIGN ($type_name, $field_name, $field_align)\n";
721 print OUT " TEST_FIELD_OFFSET($type_name, $field_name, $field_offset)\n";
723 output_test_pack_fields(\*OUT, $type_name2type, $type_name2optional, $type_name2optional_fields,
724 $type_name, $$type_name2type{$field_type_name}, $field_offset);
725 print OUT "#endif\n";
727 print OUT " TEST_FIELD_SIZE ($type_name, $field_name, $field_size)\n";
728 print OUT " TEST_FIELD_ALIGN ($type_name, $field_name, $field_align)\n";
729 print OUT " TEST_FIELD_OFFSET($type_name, $field_name, $field_offset)\n";
732 # $output->write("$type_name: $field_type_name: $field_name: test not generated (offset not defined)\n");
737 ########################################################################
740 sub output_test_pack($$$$) {
743 my $test_dir = shift;
746 my $type_names_used = shift;
748 $output->prefix("$test_dir: $test: ");
750 my @headers = $tests->get_section($test_dir, $test, "header");
751 my @type_names = $tests->get_section($test_dir, $test, "type");
753 my %type_name2optional;
754 my %type_name2optional_fields;
756 foreach my $_type_name (@type_names) {
757 my $type_name = $_type_name;
759 if ($type_name =~ s/^!//) {
760 $type_name2optional{$type_name}++;
763 my $optional_fields = {};
764 if ($type_name =~ s/:\s*(.*?)$//) {
765 my @fields = split /\s+/, $1;
766 foreach my $field (@fields) {
767 if ($field =~ s/^!//) {
768 $$optional_fields{$field}++;
773 $type_name2optional_fields{$type_name} = $optional_fields;
776 foreach my $header (@headers) {
777 my $type_name2type = $file2types{$pointer_size}{"include/$header"};
779 foreach my $_type_name (@type_names) {
780 my $type_name = $_type_name;
782 my $skip = ($type_name =~ s/^!//);
783 $type_name =~ s/:.*?$//;
784 my $type = $$type_name2type{$type_name};
785 if (!defined($type)) {
788 $$type_names_used{$type_name} = $skip ? -1 : 1;
791 print OUT "static void test_${test}_$type_name(void)\n";
793 output_test_pack_type(\*OUT, $type_name2type, \%type_name2optional, \%type_name2optional_fields,
795 output_test_pack_fields(\*OUT, $type_name2type, \%type_name2optional, \%type_name2optional_fields,
796 $type_name, $type, 0);
804 ########################################################################
807 sub output_file($$$$) {
810 my $test_dir = shift;
811 my @tests = @{(shift)};
813 my $type_names_used = shift;
815 output_header(\*OUT, $test_dir, \@tests);
817 foreach my $test (@tests) {
818 my %type_names_used2;
820 if ($test eq "pack") {
821 print OUT "#ifdef _WIN64\n\n";
823 output_test_pack(\*OUT, $test_dir, $test, \%type_names_used2);
824 print OUT "#else /* _WIN64 */\n\n";
826 output_test_pack(\*OUT, $test_dir, $test, \%type_names_used2);
827 print OUT "#endif /* _WIN64 */\n\n";
829 die "no such test ($test)\n";
832 print OUT "static void test_$test(void)\n";
834 foreach my $type_name (sort(keys(%type_names_used2))) {
835 $$type_names_used{$type_name} = $type_names_used2{$type_name};
836 if ($type_names_used2{$type_name} > 0) {
837 print OUT " test_${test}_$type_name();\n";
844 output_footer(\*OUT, $test_dir, \@tests);
849 ########################################################################
852 my %type_names_used = ();
854 my @test_dirs = $tests->get_test_dirs();
855 foreach my $test_dir (@test_dirs) {
856 my $file = "$wine_dir/$test_dir/generated.c";
857 replace_file($file, \&output_file, $test_dir, \@tests, \%type_names_used);
860 foreach my $header (sort(keys(%{$file2types{$pointer_size}}))) {
861 $output->prefix("$header: ");
862 $header =~ s%^include/%%;
863 my $type_name2type = $file2types{$pointer_size}{"include/$header"};
864 foreach my $_type_name (sort(keys(%$type_name2type))) {
865 my $type_name = $_type_name;
867 if (!exists($type_names_used{$type_name})) {
868 $output->write("$type_name: type not used\n");
873 $output->prefix("$winapi_dir/tests.dat: ");
874 foreach my $type_name (sort(keys(%type_names_used))) {
876 foreach my $header (sort(keys(%{$file2types{$pointer_size}}))) {
877 my $type_name2type = $file2types{$pointer_size}{"include/$header"};
878 if (exists($type_name2type{$type_name})) {
884 $output->write("$type_name: type not used\n");