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);
54 foreach my $test (@tests)
56 foreach my $test_dir ($tests->get_test_dirs($test))
58 foreach my $header ($tests->get_section($test_dir, $test, "header"))
62 push @files, "include/$header";
71 my $file = "tests.dat";
73 $file .= "2"; # FIXME: For tests
75 open(OUT, "> $winapi_dir/$file") || die "Error: Can't open $winapi_dir/$file: $!\n";
78 my @test_dirs = $tests->get_test_dirs();
79 foreach my $test_dir (@test_dirs) {
80 print OUT "\n" if $x++;
81 print OUT "%%%$test_dir\n";
85 my @tests = $tests->get_tests($test_dir);
86 foreach my $test (@tests) {
87 print OUT "\n" if $y++;
88 print OUT "%%$test\n";
94 my @sections = $tests->get_sections($test_dir, $test);
95 foreach my $section (@sections) {
96 my @lines = $tests->get_section($test_dir, $test, $section);
98 if ($section =~ /^(?:struct|type)$/) {
99 foreach my $line (@lines) {
105 print OUT "\n" if $z++;
106 print OUT "%$section\n";
108 foreach my $line (@lines) {
113 @types = sort { $x = $a; $y = $b; $x =~ s/^!//; $y =~ s/^!//; $x cmp $y } @types;
115 print OUT "\n" if $z++;
118 foreach my $type (@types) {
132 my $progress_current = 0;
133 my $progress_max = scalar(@files);
135 ########################################################################
141 "ANYSIZE_ARRAY" => 1,
142 "CCHDEVICENAME" => 32,
143 "CCHILDREN_TITLEBAR+1" => 6,
144 "ELF_VENDOR_SIZE" => 4,
145 "EXCEPTION_MAXIMUM_PARAMETERS" => 15,
146 "HW_PROFILE_GUIDLEN" => 39,
147 "IMAGE_NUMBEROF_DIRECTORY_ENTRIES" => 16,
148 "IMAGE_SIZEOF_SHORT_NAME" => 8,
150 "LF_FULLFACESIZE" => 64,
151 "MAXIMUM_SUPPORTED_EXTENSION" => 512,
152 "MAX_GOPHER_DISPLAY_TEXT + 1" => 129,
153 "MAX_GOPHER_LOCATOR_LENGTH + 1" => 654,
155 "MAX_PROFILE_LEN" => 80,
157 "OFS_MAXPATHNAME" => 128,
158 "SIZE_OF_80387_REGISTERS" => 80,
159 "TOKEN_SOURCE_LENGTH" => 8,
162 my %align_kludge_reported = ("FILETIME" => 1, "LARGE_INTEGER" => 1);
163 my %size_kludge_reported = ("FILETIME" => 1, "LARGE_INTEGER" => 1);
164 my %size_parse_reported;
166 sub _find_align_kind_size($) {
167 my $type_name = shift;
169 local $_ = $type_name;
178 } elsif(/^(?:(signed|unsigned)\s+)?(?:__int8|char|byte)$/) {
180 $kind = defined($1) ? $1 : "signed";
182 } elsif (/^(?:(signed|unsigned)\s+)?(?:__int16|short(?:\s+int)?)$/) {
184 $kind = defined($1) ? $1 : "signed";
186 } elsif (/^(?:wchar_t)$/) {
190 } elsif (/^(signed|unsigned)$/) {
192 $kind = defined($1) ? $1 : "signed";
194 } elsif (/^(?:(signed|unsigned)\s+)?(?:__int32|int|long(?:\s+int)?)$/) {
196 $kind = defined($1) ? $1 : "signed";
198 } elsif (/^(?:float)$/) {
202 } elsif (/^(?:(signed|unsigned)\s+)?__int64$/) {
204 $kind = defined($1) ? $1 : "signed";
206 } elsif (/^(?:double|DOUBLE|DATE)$/) {
210 } elsif (/^(?:long\s+double)$/) {
214 } elsif (/^H(?:DC|BITMAP|BRUSH|ICON|INSTANCE|KEY|MENU|METAFILE|WND)$/) {
218 } elsif (/^LP(?:BYTE|CSTR|CWSTR|DWORD|STR|VOID|WSTR)$/) {
222 } elsif (/^(?:FILETIME)$/) {
230 } elsif (/^(?:VOID)$/) {
234 } elsif (/^(?:SHORT)$/) {
238 } elsif (/^(?:BYTE)$/) {
242 } elsif (/^(?:DWORD)$/) {
246 } elsif (/^(?:WORD)$/) {
250 } elsif (/^(?:INT64|LONG64|LONGLONG)$/) {
254 } elsif (/^(?:UINT64|ULONG64|DWORD64|ULONGLONG|DWORDLONG)$/) {
258 } elsif (/^(?:LARGE_INTEGER)$/) {
262 } elsif (/^(?:POINTS)$/) {
266 } elsif (/^(struct|union)$/) {
268 if (!$size_parse_reported{$_}) {
269 $output->write("$type_name: can't parse type\n");
270 $size_parse_reported{$_} = 1;
272 } elsif (/^\w+\s*\((?:\s*CALLBACK|\s*NTAPI|\s*WINAPI)?\s*\*\s*\)\s*\(.*?\)$/) {
279 if (defined(my $type = $type_name2type{$_})) {
280 $align2 = $type->align;
283 if (!defined($align)) {
285 } elsif (defined($align2) && !$align_kludge_reported{$_}) {
286 $align_kludge_reported{$_} = 1;
287 $output->write("$type_name: type needn't be kludged\n");
290 if (!defined($align)) {
291 # $output->write("$type_name: can't find type\n");
295 if (defined(my $type = $type_name2type{$_})) {
296 $size2 = $type->size;
299 if (!defined($size)) {
301 } elsif (defined($size2) && !$size_kludge_reported{$_}) {
302 $size_kludge_reported{$_} = 1;
303 $output->write("$type_name: type needn't be kludged\n");
306 return ($align, $kind, $size);
310 my $type_name = shift;
311 (my $align, my $kind, my $size) = _find_align_kind_size($type_name);
316 my $type_name = shift;
317 (my $align, my $kind, my $size) = _find_align_kind_size($type_name);
323 my $type_name = shift;
324 (my $align, my $kind, my $size) = _find_align_kind_size($type_name);
330 return $defines{$count};
333 foreach my $file (@files) {
337 open(IN, "< $wine_dir/$file") || die "Error: Can't open $wine_dir/$file: $!\n";
346 while(s/^.*?\n//) { $max_line++; }
347 if($_) { $max_line++; }
350 my $parser = new c_parser($file);
356 my $update_output = sub {
360 $progress .= "$file (file $progress_current of $progress_max)";
361 $prefix .= "$file: ";
364 $progress .= ": line $line of $max_line";
367 $output->progress($progress);
368 $output->prefix($prefix);
373 my $found_line = sub {
378 $parser->set_found_line_callback($found_line);
380 my $found_preprocessor = sub {
381 my $begin_line = shift;
382 my $begin_column = shift;
383 my $preprocessor = shift;
385 #print "found_preprocessor: $begin_line: [$_]\n";
386 if ($preprocessor =~ /^\#\s*include\s+[\"<]pshpack(\d+)\.h[\">]$/) {
388 #print "found pack $1 on line $begin_line\n";
389 } elsif($preprocessor =~ /^\#\s*include\s+[\"<]poppack\.h[\">]$/) {
391 #print "found poppack on line $begin_line\n";
396 $parser->set_found_preprocessor_callback($found_preprocessor);
398 my $found_type = sub {
403 my $name = $type->name;
404 $file2types{$file}{$name} = $type;
406 $type->set_find_align_callback(\&find_align);
407 $type->set_find_kind_callback(\&find_kind);
408 $type->set_find_size_callback(\&find_size);
409 $type->set_find_count_callback(\&find_count);
411 my $pack = $packs[$#packs];
412 if (!defined($type->pack) && $type->kind =~ /^(?:struct|union)$/) {
415 my $size = $type->size();
416 if (defined($size)) {
417 my $max_field_base_size = 0;
419 foreach my $field ($type->fields()) {
420 my $field_type_name = $field->type_name;
421 my $field_name = $field->name;
422 my $field_size = $field->size;
423 my $field_base_size = $field->base_size;
424 my $field_offset = $field->offset;
425 my $field_align = $field->align;
427 # $output->write("$name: $field_type_name: $field_name: $field_offset: $field_size($field_base_size): $field_align\n");
429 # $output->write("$name: $size\n");
431 $type_name2type{$name} = $type;
433 # $output->write("$name: can't find size\n");
438 $parser->set_found_type_callback($found_type);
443 if(!$parser->parse_c_file(\$_, \$line, \$column)) {
444 $output->write("can't parse file\n");
451 ########################################################################
454 sub output_header($$$) {
457 my $test_dir = shift;
458 my @tests = @{(shift)};
460 print OUT "/* File generated automatically from tools/winapi/tests.dat; do not edit! */\n";
461 print OUT "/* This file can be copied, modified and distributed without restriction. */\n";
465 foreach my $test (@tests) {
466 my @description = $tests->get_section($test_dir, $test, "description");
467 foreach my $description (@description) {
468 print OUT " * $description\n";
474 print OUT "#define WINVER 0x0501\n";
475 print OUT "#define _WIN32_IE 0x0501\n";
476 print OUT "#define _WIN32_WINNT 0x0501\n";
478 print OUT "#define WINE_NOWINSOCK\n";
480 foreach my $test (@tests) {
481 my @includes = $tests->get_section($test_dir, $test, "include");
482 foreach my $include (@includes) {
483 print OUT "#include $include\n";
487 print OUT "#include \"wine/test.h\"\n";
490 print OUT "/***********************************************************************\n";
491 print OUT " * Compatibility macros\n";
494 print OUT "#define DWORD_PTR UINT_PTR\n";
495 print OUT "#define LONG_PTR INT_PTR\n";
496 print OUT "#define ULONG_PTR UINT_PTR\n";
499 print OUT "/***********************************************************************\n";
500 print OUT " * Windows API extension\n";
503 print OUT "#if defined(_MSC_VER) && (_MSC_VER >= 1300) && defined(__cplusplus)\n";
504 print OUT "# define _TYPE_ALIGNMENT(type) __alignof(type)\n";
505 print OUT "#elif defined(__GNUC__)\n";
506 print OUT "# define _TYPE_ALIGNMENT(type) __alignof__(type)\n";
509 print OUT " * FIXME: May not be possible without a compiler extension\n";
510 print OUT " * (if type is not just a name that is, otherwise the normal\n";
511 print OUT " * TYPE_ALIGNMENT can be used)\n";
513 print OUT "#endif\n";
515 print OUT "#if defined(TYPE_ALIGNMENT) && defined(_MSC_VER) && _MSC_VER >= 800 && !defined(__cplusplus)\n";
516 print OUT "#pragma warning(disable:4116)\n";
517 print OUT "#endif\n";
519 print OUT "#if !defined(TYPE_ALIGNMENT) && defined(_TYPE_ALIGNMENT)\n";
520 print OUT "# define TYPE_ALIGNMENT _TYPE_ALIGNMENT\n";
521 print OUT "#endif\n";
524 print OUT "/***********************************************************************\n";
525 print OUT " * Test helper macros\n";
528 print OUT "#ifdef _WIN64\n";
530 print OUT "# define TEST_TYPE_SIZE(type, size)\n";
531 print OUT "# define TEST_TYPE_ALIGN(type, align)\n";
532 print OUT "# define TEST_TARGET_ALIGN(type, align)\n";
533 print OUT "# define TEST_FIELD_ALIGN(type, field, align)\n";
534 print OUT "# define TEST_FIELD_OFFSET(type, field, offset)\n";
538 print OUT "# define TEST_TYPE_SIZE(type, size) C_ASSERT(sizeof(type) == size);\n";
540 print OUT "# ifdef TYPE_ALIGNMENT\n";
541 print OUT "# define TEST_TYPE_ALIGN(type, align) C_ASSERT(TYPE_ALIGNMENT(type) == align);\n";
542 print OUT "# else\n";
543 print OUT "# define TEST_TYPE_ALIGN(type, align)\n";
544 print OUT "# endif\n";
546 print OUT "# ifdef _TYPE_ALIGNMENT\n";
547 print OUT "# define TEST_TARGET_ALIGN(type, align) C_ASSERT(_TYPE_ALIGNMENT(*(type)0) == align);\n";
548 print OUT "# define TEST_FIELD_ALIGN(type, field, align) C_ASSERT(_TYPE_ALIGNMENT(((type*)0)->field) == align);\n";
549 print OUT "# else\n";
550 print OUT "# define TEST_TARGET_ALIGN(type, align)\n";
551 print OUT "# define TEST_FIELD_ALIGN(type, field, align)\n";
552 print OUT "# endif\n";
554 print OUT "# define TEST_FIELD_OFFSET(type, field, offset) C_ASSERT(FIELD_OFFSET(type, field) == offset);\n";
556 print OUT "#endif\n";
558 print OUT "#define TEST_TARGET_SIZE(type, size) TEST_TYPE_SIZE(*(type)0, size)\n";
559 print OUT "#define TEST_FIELD_SIZE(type, field, size) TEST_TYPE_SIZE((((type*)0)->field), size)\n";
560 print OUT "#define TEST_TYPE_SIGNED(type) C_ASSERT((type) -1 < 0);\n";
561 print OUT "#define TEST_TYPE_UNSIGNED(type) C_ASSERT((type) -1 > 0);\n";
566 ########################################################################
569 sub output_footer($$$) {
572 my $test_dir = shift;
573 my @tests = @{(shift)};
575 print OUT "START_TEST(generated)\n";
577 foreach my $test (@tests) {
578 print OUT "#ifdef _WIN64\n";
579 print OUT " ok(0, \"The type size / alignment tests don't support Win64 yet\\n\");\n";
581 print OUT " test_$test();\n";
582 print OUT "#endif\n";
587 ########################################################################
588 # output_test_pack_type
590 sub output_test_pack_type($$$$$$) {
593 my $type_name2type = shift;
594 my $type_name2optional = shift;
595 my $type_name2optional_fields = shift;
596 my $type_name = shift;
599 my $optional_fields = $$type_name2optional_fields{$type_name};
601 my $type_align = $type->align;
602 my $type_pack = $type->pack;
603 my $type_size = $type->size;
604 my $type_kind = $type->kind;
606 if (defined($type_pack)) {
607 print OUT " /* $type_name (pack $type_pack) */\n";
609 print OUT " /* $type_name */\n";
612 if (!scalar(keys(%$optional_fields)) && defined($type_align) && defined($type_size)) {
613 print OUT " TEST_TYPE_SIZE ($type_name, $type_size)\n";
614 print OUT " TEST_TYPE_ALIGN ($type_name, $type_align)\n";
617 if ($type_kind eq "float") {
619 } elsif ($type_kind eq "pointer") {
620 my $dereference_type;
621 $dereference_type = sub {
624 my @fields = $type->fields;
625 my $type_name2 =$fields[0]->type_name;
627 if ($type_name2 =~ s/\s*\*$//) {
628 my $type2 = $$type_name2type{$type_name2};
629 if (defined($type2)) {
632 if ($type_name2 !~ /^(?:PVOID|VOID|void)$/) {
633 $output->write("$type_name2: warning: type not found 1\n");
637 } elsif ($type_name2 =~ /^\w+$/) {
638 my $type2 = $$type_name2type{$type_name2};
639 if (defined($type2)) {
640 return &$dereference_type($type2);
642 $output->write("$type_name2: warning: type not found\n");
645 } elsif ($type_name2 =~ /^\w+\s*\((?:\s*CALLBACK|\s*NTAPI|\s*WINAPI)?\s*\*\s*\)\s*\(.*?\)$/) {
648 $output->write("$type_name2: warning: type can't be parsed\n");
653 my $type2 = &$dereference_type($type);
654 if (defined($type2)) {
655 my $type_name2 = $type2->name;
656 my $type_align2 = $type2->align;
657 my $type_size2 = $type2->size;
659 my $optional = $$type_name2optional{$type_name};
660 my $optional_fields2 = $$type_name2optional_fields{$type_name2};
662 if (!$optional && !scalar(keys(%$optional_fields2)) && defined($type_align2) && defined($type_size2)) {
663 print OUT " TEST_TARGET_SIZE ($type_name, $type_size2)\n";
664 print OUT " TEST_TARGET_ALIGN($type_name, $type_align2)\n";
666 # $output->write("$type_name: warning: type size not found\n");
669 } elsif ($type_kind eq "signed") {
670 print OUT " TEST_TYPE_SIGNED($type_name)\n";
671 } elsif ($type_kind eq "unsigned") {
672 print OUT " TEST_TYPE_UNSIGNED($type_name)\n";
676 sub output_test_pack_fields($$$$$$$);
677 sub output_test_pack_fields($$$$$$$) {
680 my $type_name2type = shift;
681 my $type_name2optional = shift;
682 my $type_name2optional_fields = shift;
683 my $type_name = shift;
687 my $optional_fields = $$type_name2optional_fields{$type_name};
689 foreach my $field ($type->fields()) {
690 my $field_type_name = $field->type_name;
691 $field_type_name =~ s/\s+DECLSPEC_ALIGN\(\d+\)//;
692 my $field_name = $field->name;
693 my $field_size = $field->size;
694 my $field_offset = $field->offset;
695 my $field_align = $field->align;
697 next if $field_name eq "" || (defined($field_size) && $field_size < 0);
698 # We cannot take the address of a bitfield with MSVC
699 next if ($field_type_name =~ /:/);
701 if ($$optional_fields{$field_name}) {
703 } elsif (defined($field_size) && defined($field_offset)) {
704 $field_offset += $offset;
705 if ($field_name eq "DUMMYSTRUCTNAME") {
706 print OUT "#ifdef NONAMELESSSTRUCT\n";
707 print OUT " TEST_TYPE_SIZE ($type_name.$field_name, $field_size)\n";
708 print OUT " TEST_FIELD_ALIGN ($type_name, $field_name, $field_align)\n";
709 print OUT " TEST_FIELD_OFFSET($type_name, $field_name, $field_offset)\n";
711 output_test_pack_fields(\*OUT, $type_name2type, $type_name2optional, $type_name2optional_fields,
712 $type_name, $$type_name2type{$field_type_name}, $field_offset);
713 print OUT "#endif\n";
715 print OUT " TEST_FIELD_SIZE ($type_name, $field_name, $field_size)\n";
716 print OUT " TEST_FIELD_ALIGN ($type_name, $field_name, $field_align)\n";
717 print OUT " TEST_FIELD_OFFSET($type_name, $field_name, $field_offset)\n";
720 # $output->write("$type_name: $field_type_name: $field_name: test not generated (offset not defined)\n");
725 ########################################################################
728 sub output_test_pack($$$$) {
731 my $test_dir = shift;
734 my $type_names_used = shift;
736 $output->prefix("$test_dir: $test: ");
738 my @headers = $tests->get_section($test_dir, $test, "header");
739 my @type_names = $tests->get_section($test_dir, $test, "type");
741 my %type_name2optional;
742 my %type_name2optional_fields;
744 foreach my $_type_name (@type_names) {
745 my $type_name = $_type_name;
747 if ($type_name =~ s/^!//) {
748 $type_name2optional{$type_name}++;
751 my $optional_fields = {};
752 if ($type_name =~ s/:\s*(.*?)$//) {
753 my @fields = split /\s+/, $1;
754 foreach my $field (@fields) {
755 if ($field =~ s/^!//) {
756 $$optional_fields{$field}++;
761 $type_name2optional_fields{$type_name} = $optional_fields;
764 foreach my $header (@headers) {
765 my $type_name2type = $file2types{"include/$header"};
767 foreach my $_type_name (@type_names) {
768 my $type_name = $_type_name;
770 my $skip = ($type_name =~ s/^!//);
771 $type_name =~ s/:.*?$//;
772 my $type = $$type_name2type{$type_name};
773 if (!defined($type)) {
776 $$type_names_used{$type_name} = $skip ? -1 : 1;
779 print OUT "static void test_${test}_$type_name(void)\n";
781 output_test_pack_type(\*OUT, $type_name2type, \%type_name2optional, \%type_name2optional_fields,
783 output_test_pack_fields(\*OUT, $type_name2type, \%type_name2optional, \%type_name2optional_fields,
784 $type_name, $type, 0);
792 ########################################################################
795 sub output_file($$$$) {
798 my $test_dir = shift;
799 my @tests = @{(shift)};
801 my $type_names_used = shift;
803 output_header(\*OUT, $test_dir, \@tests);
805 foreach my $test (@tests) {
806 my %type_names_used2;
808 if ($test eq "pack") {
809 output_test_pack(\*OUT, $test_dir, $test, \%type_names_used2);
811 die "no such test ($test)\n";
814 print OUT "static void test_$test(void)\n";
816 foreach my $type_name (sort(keys(%type_names_used2))) {
817 $$type_names_used{$type_name} = $type_names_used2{$type_name};
818 if ($type_names_used2{$type_name} > 0) {
819 print OUT " test_${test}_$type_name();\n";
826 output_footer(\*OUT, $test_dir, \@tests);
831 ########################################################################
834 my %type_names_used = ();
836 my @test_dirs = $tests->get_test_dirs();
837 foreach my $test_dir (@test_dirs) {
838 my $file = "$wine_dir/$test_dir/generated.c";
839 replace_file($file, \&output_file, $test_dir, \@tests, \%type_names_used);
842 foreach my $header (sort(keys(%file2types))) {
843 $output->prefix("$header: ");
844 $header =~ s%^include/%%;
845 my $type_name2type = $file2types{"include/$header"};
846 foreach my $_type_name (sort(keys(%$type_name2type))) {
847 my $type_name = $_type_name;
849 if (!exists($type_names_used{$type_name})) {
850 $output->write("$type_name: type not used\n");
855 $output->prefix("$winapi_dir/tests.dat: ");
856 foreach my $type_name (sort(keys(%type_names_used))) {
858 foreach my $header (sort(keys(%file2types))) {
859 my $type_name2type = $file2types{"include/$header"};
860 if (exists($type_name2type{$type_name})) {
866 $output->write("$type_name: type not used\n");