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
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 $winapi_check_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);
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;
80 my $file = "tests.dat";
82 $file .= "2"; # FIXME: For tests
84 open(OUT
, "> $winapi_dir/$file") || die "$winapi_dir/$file: $!\n";
87 my @test_dirs = $tests->get_test_dirs();
88 foreach my $test_dir (@test_dirs) {
89 print OUT
"\n" if $x++;
90 print OUT
"%%%$test_dir\n";
94 my @tests = $tests->get_tests($test_dir);
95 foreach my $test (@tests) {
96 print OUT
"\n" if $y++;
97 print OUT
"%%$test\n";
103 my @sections = $tests->get_sections($test_dir, $test);
104 foreach my $section (@sections) {
105 my @lines = $tests->get_section($test_dir, $test, $section);
107 if ($section =~ /^(?:struct|type)$/) {
108 foreach my $line (@lines) {
114 print OUT
"\n" if $z++;
115 print OUT
"%$section\n";
117 foreach my $line (@lines) {
122 @types = sort { $x = $a; $y = $b; $x =~ s/^!//; $y =~ s/^!//; $x cmp $y } @types;
124 print OUT
"\n" if $z++;
127 foreach my $type (@types) {
141 my $progress_current = 0;
142 my $progress_max = scalar(@files);
144 ########################################################################
150 "ANYSIZE_ARRAY" => 1,
151 "CCHDEVICENAME" => 32,
152 "ELF_VENDOR_SIZE" => 4,
153 "EXCEPTION_MAXIMUM_PARAMETERS" => 15,
154 "HW_PROFILE_GUIDLEN" => 39,
155 "IMAGE_NUMBEROF_DIRECTORY_ENTRIES" => 16,
156 "IMAGE_SIZEOF_SHORT_NAME" => 8,
158 "LF_FULLFACESIZE" => 64,
159 "MAXIMUM_SUPPORTED_EXTENSION" => 512,
160 "MAX_GOPHER_DISPLAY_TEXT + 1" => 129,
161 "MAX_GOPHER_LOCATOR_LENGTH + 1" => 654,
163 "MAX_PROFILE_LEN" => 80,
164 "OFS_MAXPATHNAME" => 128,
165 "SIZE_OF_80387_REGISTERS" => 80,
166 "TOKEN_SOURCE_LENGTH" => 8,
169 my %align_kludge_reported = ("FILETIME" => 1, "LARGE_INTEGER" => 1);
170 my %size_kludge_reported = ("FILETIME" => 1, "LARGE_INTEGER" => 1);
171 my %size_parse_reported;
173 sub _find_align_kind_size
{
174 my $type_name = shift;
176 local $_ = $type_name;
180 if (s/^(.*?)\s*(?:\[\s*(.*?)\s*\]|:(\d+))?$/$1/) {
194 } elsif(/^(?:(signed|unsigned)\s+)?(?:__int8|char)$/) {
196 $kind = defined($1) ?
$1 : "signed";
198 } elsif (/^(?:(signed|unsigned)\s+)?(?:__int16|short(?:\s+int)?)$/) {
200 $kind = defined($1) ?
$1 : "signed";
202 } elsif (/^(?:wchar_t)$/) {
206 } elsif (!/^$/ && /^(?:(signed|unsigned)\s+)?(?:__int32|int|long(?:\s+int)?)?$/) {
208 $kind = defined($1) ?
$1 : "signed";
210 } elsif (/^(?:float)$/) {
214 } elsif (/^(?:(signed|unsigned)\s+)?__int64$/) {
216 $kind = defined($1) ?
$1 : "signed";
218 } elsif (/^(?:double)$/) {
222 } elsif (/^(?:long\s+double)$/) {
226 } elsif (/^H(?:DC|BITMAP|BRUSH|ICON|INSTANCE|MENU|METAFILE|WND)$/) {
230 } elsif (/^LP(?:CSTR|CWSTR|DWORD|VOID|WSTR)$/) {
234 } elsif (/^(?:FILETIME)$/) {
238 } elsif (/^(?:LARGE_INTEGER)$/) {
242 } elsif (/^(struct|union)$/) {
244 if (!$size_parse_reported{$_}) {
245 $output->write("$type_name: can't parse type\n");
246 $size_parse_reported{$_} = 1;
248 } elsif (/^\w+\s*\((?:\s*CALLBACK|\s*NTAPI|\s*WINAPI)?\s*\*\s*\)\s*\(.*?\)$/) {
255 if (defined(my $type = $type_name2type{$_})) {
256 $align2 = $type->align;
259 if (!defined($align)) {
261 } elsif (defined($align2) && !$align_kludge_reported{$_}) {
262 $align_kludge_reported{$_} = 1;
263 $output->write("$type_name: type needn't be kludged\n");
266 if (!defined($align)) {
267 # $output->write("$type_name: can't find type\n");
271 if (defined(my $type = $type_name2type{$_})) {
272 $size2 = $type->size;
275 if (!defined($size)) {
277 } elsif (defined($size2) && !$size_kludge_reported{$_}) {
278 $size_kludge_reported{$_} = 1;
279 $output->write("$type_name: type needn't be kludged\n");
282 if (!defined($size)) {
283 # $output->write("$type_name: can't find type\n");
284 } elsif (defined($count)) {
285 if ($count =~ /^\d+$/) {
286 $size *= int($count);
287 } elsif (defined(my $count2 = $defines{$count})) {
288 $size *= int($count2);
290 $output->write("$type_name: can't parse type ('$_') ('$count')\n");
293 } elsif (defined($bits)) {
297 return ($align, $kind, $size);
301 my $type_name = shift;
302 (my $align, my $kind, my $size) = _find_align_kind_size
($type_name);
307 my $type_name = shift;
308 (my $align, my $kind, my $size) = _find_align_kind_size
($type_name);
314 my $type_name = shift;
315 (my $align, my $kind, my $size) = _find_align_kind_size
($type_name);
319 foreach my $file (@files) {
323 open(IN
, "< $wine_dir/$file");
332 while(s/^.*?\n//) { $max_line++; }
333 if($_) { $max_line++; }
336 my $parser = new c_parser
($file);
342 my $update_output = sub {
346 $progress .= "$file (file $progress_current of $progress_max)";
347 $prefix .= "$file: ";
350 $progress .= ": line $line of $max_line";
353 $output->progress($progress);
354 $output->prefix($prefix);
359 my $found_line = sub {
364 $parser->set_found_line_callback($found_line);
366 my $found_preprocessor = sub {
367 my $begin_line = shift;
368 my $begin_column = shift;
369 my $preprocessor = shift;
371 #print "found_preprocessor: $begin_line: [$_]\n";
372 if ($preprocessor =~ /^\#\s*include\s+[\"<]pshpack(\d+)\.h[\">]$/) {
374 #print "found pack $1 on line $begin_line\n";
375 } elsif($preprocessor =~ /^\#\s*include\s+[\"<]poppack\.h[\">]$/) {
377 #print "found poppack on line $begin_line\n";
382 $parser->set_found_preprocessor_callback($found_preprocessor);
384 my $found_type = sub {
389 my $name = $type->name;
390 $file2types{$file}{$name} = $type;
392 $type->set_find_align_callback(\
&find_align
);
393 $type->set_find_kind_callback(\
&find_kind
);
394 $type->set_find_size_callback(\
&find_size
);
396 my $pack = $packs[$#packs];
397 if (!defined($type->pack) && $type->kind =~ /^(?:struct|union)$/) {
400 my $size = $type->size();
401 if (defined($size)) {
402 my $max_field_base_size = 0;
404 foreach my $field ($type->fields()) {
405 my $field_type_name = $field->type_name;
406 my $field_name = $field->name;
407 my $field_size = $field->size;
408 my $field_base_size = $field->base_size;
409 my $field_offset = $field->offset;
410 my $field_align = $field->align;
412 # $output->write("$name: $field_type_name: $field_name: $field_offset: $field_size($field_base_size): $field_align\n");
414 # $output->write("$name: $size\n");
416 $type_name2type{$name} = $type;
418 # $output->write("$name: can't find size\n");
423 $parser->set_found_type_callback($found_type);
428 if(!$parser->parse_c_file(\
$_, \
$line, \
$column)) {
429 $output->write("can't parse file\n");
436 ########################################################################
442 my $test_dir = shift;
443 my @tests = @
{(shift)};
445 print OUT
"/* File generated automatically from tools/winapi/test.dat; do not edit! */\n";
446 print OUT
"/* This file can be copied, modified and distributed without restriction. */\n";
450 foreach my $test (@tests) {
451 my @description = $tests->get_section($test_dir, $test, "description");
452 foreach my $description (@description) {
453 print OUT
" * $description\n";
459 print OUT
"#define WINVER 0x0501\n";
460 print OUT
"#define _WIN32_IE 0x0501\n";
461 print OUT
"#define _WIN32_WINNT 0x0501\n";
463 print OUT
"#define WINE_NOWINSOCK\n";
465 foreach my $test (@tests) {
466 my @includes = $tests->get_section($test_dir, $test, "include");
467 foreach my $include (@includes) {
468 print OUT
"#include $include\n";
472 print OUT
"#include \"wine/test.h\"\n";
475 print OUT
"/***********************************************************************\n";
476 print OUT
" * Compability macros\n";
479 print OUT
"#define DWORD_PTR UINT_PTR\n";
480 print OUT
"#define LONG_PTR INT_PTR\n";
481 print OUT
"#define ULONG_PTR UINT_PTR\n";
484 print OUT
"/***********************************************************************\n";
485 print OUT
" * Windows API extension\n";
488 print OUT
"#if defined(_MSC_VER) && (_MSC_VER >= 1300) && defined(__cplusplus)\n";
489 print OUT
"# define FIELD_ALIGNMENT(type, field) __alignof(((type*)0)->field)\n";
490 print OUT
"#elif defined(__GNUC__)\n";
491 print OUT
"# define FIELD_ALIGNMENT(type, field) __alignof__(((type*)0)->field)\n";
493 print OUT
"/* FIXME: Not sure if is possible to do without compiler extension */\n";
494 print OUT
"#endif\n";
496 print OUT
"#if defined(_MSC_VER) && (_MSC_VER >= 1300) && defined(__cplusplus)\n";
497 print OUT
"# define _TYPE_ALIGNMENT(type) __alignof(type)\n";
498 print OUT
"#elif defined(__GNUC__)\n";
499 print OUT
"# define _TYPE_ALIGNMENT(type) __alignof__(type)\n";
502 print OUT
" * FIXME: Not sure if is possible to do without compiler extension\n";
503 print OUT
" * (if type is not just a name that is, if so the normal)\n";
504 print OUT
" * TYPE_ALIGNMENT can be used)\n";
506 print OUT
"#endif\n";
508 print OUT
"#if !defined(TYPE_ALIGNMENT) && defined(_TYPE_ALIGNMENT)\n";
509 print OUT
"# define TYPE_ALIGNMENT _TYPE_ALIGNMENT\n";
510 print OUT
"#endif\n";
513 print OUT
"/***********************************************************************\n";
514 print OUT
" * Test helper macros\n";
517 print OUT
"#ifdef FIELD_ALIGNMENT\n";
518 print OUT
"# define TEST_FIELD_ALIGNMENT(type, field, align) \\\n";
519 print OUT
" ok(FIELD_ALIGNMENT(type, field) == align, \\\n";
520 print OUT
" \"FIELD_ALIGNMENT(\" #type \", \" #field \") == %d (expected \" #align \")\\n\", \\\n";
521 print OUT
" FIELD_ALIGNMENT(type, field))\n";
523 print OUT
"# define TEST_FIELD_ALIGNMENT(type, field, align) do { } while (0)\n";
524 print OUT
"#endif\n";
526 print OUT
"#define TEST_FIELD_OFFSET(type, field, offset) \\\n";
527 print OUT
" ok(FIELD_OFFSET(type, field) == offset, \\\n";
528 print OUT
" \"FIELD_OFFSET(\" #type \", \" #field \") == %ld (expected \" #offset \")\\n\", \\\n";
529 print OUT
" FIELD_OFFSET(type, field))\n";
531 print OUT
"#ifdef _TYPE_ALIGNMENT\n";
532 print OUT
"#define TEST__TYPE_ALIGNMENT(type, align) \\\n";
533 print OUT
" ok(_TYPE_ALIGNMENT(type) == align, \"TYPE_ALIGNMENT(\" #type \") == %d (expected \" #align \")\\n\", _TYPE_ALIGNMENT(type))\n";
535 print OUT
"# define TEST__TYPE_ALIGNMENT(type, align) do { } while (0)\n";
536 print OUT
"#endif\n";
538 print OUT
"#ifdef TYPE_ALIGNMENT\n";
539 print OUT
"#define TEST_TYPE_ALIGNMENT(type, align) \\\n";
540 print OUT
" ok(TYPE_ALIGNMENT(type) == align, \"TYPE_ALIGNMENT(\" #type \") == %d (expected \" #align \")\\n\", TYPE_ALIGNMENT(type))\n";
542 print OUT
"# define TEST_TYPE_ALIGNMENT(type, align) do { } while (0)\n";
543 print OUT
"#endif\n";
545 print OUT
"#define TEST_TYPE_SIZE(type, size) \\\n";
546 print OUT
" ok(sizeof(type) == size, \"sizeof(\" #type \") == %d (expected \" #size \")\\n\", sizeof(type))\n";
548 print OUT
"/***********************************************************************\n";
549 print OUT
" * Test macros\n";
552 print OUT
"#define TEST_FIELD(type, field_type, field_name, field_offset, field_size, field_align) \\\n";
553 print OUT
" TEST_TYPE_SIZE(field_type, field_size); \\\n";
554 print OUT
" TEST_FIELD_ALIGNMENT(type, field_name, field_align); \\\n";
555 print OUT
" TEST_FIELD_OFFSET(type, field_name, field_offset); \\\n";
557 print OUT
"#define TEST_TYPE(type, size, align) \\\n";
558 print OUT
" TEST_TYPE_ALIGNMENT(type, align); \\\n";
559 print OUT
" TEST_TYPE_SIZE(type, size)\n";
561 print OUT
"#define TEST_TYPE_POINTER(type, size, align) \\\n";
562 print OUT
" TEST__TYPE_ALIGNMENT(*(type)0, align); \\\n";
563 print OUT
" TEST_TYPE_SIZE(*(type)0, size)\n";
565 print OUT
"#define TEST_TYPE_SIGNED(type) \\\n";
566 print OUT
" ok((type) -1 < 0, \"(\" #type \") -1 < 0\\n\");\n";
568 print OUT
"#define TEST_TYPE_UNSIGNED(type) \\\n";
569 print OUT
" ok((type) -1 > 0, \"(\" #type \") -1 > 0\\n\");\n";
573 ########################################################################
579 my $test_dir = shift;
580 my @tests = @
{(shift)};
582 print OUT
"START_TEST(generated)\n";
584 foreach my $test (@tests) {
585 print OUT
" test_$test();\n";
590 ########################################################################
591 # output_test_pack_type
593 sub output_test_pack_type
{
596 my $type_name2type = shift;
597 my $type_name2optional = shift;
598 my $type_name2optional_fields = shift;
599 my $type_name = shift;
602 my $optional_fields = $$type_name2optional_fields{$type_name};
604 my $type_align = $type->align;
605 my $type_pack = $type->pack;
606 my $type_size = $type->size;
607 my $type_kind = $type->kind;
609 if (defined($type_pack)) {
610 print OUT
" /* $type_name (pack $type_pack) */\n";
612 print OUT
" /* $type_name */\n";
615 if (!scalar(keys(%$optional_fields)) && defined($type_align) && defined($type_size)) {
616 print OUT
" TEST_TYPE($type_name, $type_size, $type_align);\n";
619 if ($type_kind eq "float") {
621 } elsif ($type_kind eq "pointer") {
622 my $dereference_type;
623 $dereference_type = sub {
626 my @fields = $type->fields;
627 my $type_name2 =$fields[0]->type_name;
629 if ($type_name2 =~ s/\s*\*$//) {
630 my $type2 = $$type_name2type{$type_name2};
631 if (defined($type2)) {
634 if ($type_name2 !~ /^(?:PVOID|VOID|void)$/) {
635 $output->write("$type_name2: warning: type not found 1\n");
639 } elsif ($type_name2 =~ /^\w+$/) {
640 my $type2 = $$type_name2type{$type_name2};
641 if (defined($type2)) {
642 return &$dereference_type($type2);
644 $output->write("$type_name2: warning: type not found\n");
647 } elsif ($type_name2 =~ /^\w+\s*\((?:\s*CALLBACK|\s*NTAPI|\s*WINAPI)?\s*\*\s*\)\s*\(.*?\)$/) {
650 $output->write("$type_name2: warning: type can't be parsed\n");
655 my $type2 = &$dereference_type($type);
656 if (defined($type2)) {
657 my $type_name2 = $type2->name;
658 my $type_align2 = $type2->align;
659 my $type_size2 = $type2->size;
661 my $optional = $$type_name2optional{$type_name};
662 my $optional_fields2 = $$type_name2optional_fields{$type_name2};
664 if (!$optional && !scalar(keys(%$optional_fields2)) && defined($type_align2) && defined($type_size2)) {
665 print OUT
" TEST_TYPE_POINTER($type_name, $type_size2, $type_align2);\n";
667 # $output->write("$type_name: warning: type size not found\n");
670 } elsif ($type_kind eq "signed") {
671 print OUT
" TEST_TYPE_SIGNED($type_name);\n";
672 } elsif ($type_kind eq "unsigned") {
673 print OUT
" TEST_TYPE_UNSIGNED($type_name);\n";
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 my $field_name = $field->name;
692 my $field_size = $field->size;
693 my $field_offset = $field->offset;
694 my $field_align = $field->align;
696 next if $field_name eq "" || (defined($field_size) && $field_size < 0);
698 if ($$optional_fields{$field_name}) {
700 } elsif (defined($field_size) && defined($field_offset)) {
701 $field_offset += $offset;
702 if ($field_name eq "DUMMYSTRUCTNAME") {
703 print OUT
"#ifdef NONAMELESSSTRUCT\n";
704 print OUT
" TEST_FIELD($type_name, $field_type_name, $field_name, ";
705 print OUT
"$field_offset, $field_size, $field_align);\n";
707 output_test_pack_fields
(\
*OUT
, $type_name2type, $type_name2optional, $type_name2optional_fields,
708 $type_name, $$type_name2type{$field_type_name}, $field_offset);
709 print OUT
"#endif\n";
711 print OUT
" TEST_FIELD($type_name, $field_type_name, $field_name, ";
712 print OUT
"$field_offset, $field_size, $field_align);\n";
715 # $output->write("$type_name: $field_type_name: $field_name: test not generated (offset not defined)\n");
720 ########################################################################
723 sub output_test_pack
{
726 my $test_dir = shift;
729 my $type_names_used = shift;
731 $output->prefix("$test_dir: $test: ");
733 my @headers = $tests->get_section($test_dir, $test, "header");
734 my @type_names = $tests->get_section($test_dir, $test, "type");
736 my %type_name2optional;
737 my %type_name2optional_fields;
739 foreach my $_type_name (@type_names) {
740 my $type_name = $_type_name;
742 if ($type_name =~ s/^!//) {
743 $type_name2optional{$type_name}++;
746 my $optional_fields = {};
747 if ($type_name =~ s/:\s*(.*?)$//) {
748 my @fields = split /\s+/, $1;
749 foreach my $field (@fields) {
750 if ($field =~ s/^!//) {
751 $$optional_fields{$field}++;
756 $type_name2optional_fields{$type_name} = $optional_fields;
759 foreach my $header (@headers) {
760 my $type_name2type = $file2types{"include/$header"};
762 foreach my $_type_name (@type_names) {
763 my $type_name = $_type_name;
765 my $skip = ($type_name =~ s/^!//);
766 $type_name =~ s/:.*?$//;
767 my $type = $$type_name2type{$type_name};
768 if (!defined($type)) {
771 $$type_names_used{$type_name} = $skip ?
-1 : 1;
774 print OUT
"static void test_${test}_$type_name(void)\n";
776 output_test_pack_type
(\
*OUT
, $type_name2type, \
%type_name2optional, \
%type_name2optional_fields,
778 output_test_pack_fields
(\
*OUT
, $type_name2type, \
%type_name2optional, \
%type_name2optional_fields,
779 $type_name, $type, 0);
787 ########################################################################
793 my $test_dir = shift;
794 my @tests = @
{(shift)};
796 my $type_names_used = shift;
798 output_header
(\
*OUT
, $test_dir, \
@tests);
800 foreach my $test (@tests) {
801 my %type_names_used2;
803 if ($test eq "pack") {
804 output_test_pack
(\
*OUT
, $test_dir, $test, \
%type_names_used2);
806 die "no such test ($test)\n";
809 print OUT
"static void test_$test(void)\n";
811 foreach my $type_name (sort(keys(%type_names_used2))) {
812 $$type_names_used{$type_name} = $type_names_used2{$type_name};
813 if ($type_names_used2{$type_name} > 0) {
814 print OUT
" test_${test}_$type_name();\n";
821 output_footer
(\
*OUT
, $test_dir, \
@tests);
826 ########################################################################
829 my %type_names_used = ();
831 my @test_dirs = $tests->get_test_dirs();
832 foreach my $test_dir (@test_dirs) {
833 my $file = "$wine_dir/$test_dir/generated.c";
834 replace_file
($file, \
&output_file
, $test_dir, \
@tests, \
%type_names_used);
837 foreach my $header (sort(keys(%file2types))) {
838 $output->prefix("$header: ");
839 $header =~ s
%^include
/%%;
840 my $type_name2type = $file2types{"include/$header"};
841 foreach my $_type_name (sort(keys(%$type_name2type))) {
842 my $type_name = $_type_name;
844 if (!exists($type_names_used{$type_name})) {
845 $output->write("$type_name: type not used\n");
850 $output->prefix("$winapi_dir/tests.dat: ");
851 foreach my $type_name (sort(keys(%type_names_used))) {
853 foreach my $header (sort(keys(%file2types))) {
854 my $type_name2type = $file2types{"include/$header"};
855 if (exists($type_name2type{$type_name})) {
861 $output->write("$type_name: type not used\n");