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|byte)$/) {
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|KEY|MENU|METAFILE|RESULT|WND)$/) {
230 } elsif (/^LP(?:BYTE|CSTR|CWSTR|DWORD|STR|VOID|WSTR)$/) {
234 } elsif (/^(?:FILETIME)$/) {
238 } elsif (/^(?:VOID)$/) {
242 } elsif (/^(?:SHORT)$/) {
246 } elsif (/^(?:BYTE)$/) {
250 } elsif (/^(?:DWORD)$/) {
254 } elsif (/^(?:WORD)$/) {
258 } elsif (/^(?:LARGE_INTEGER)$/) {
262 } elsif (/^(struct|union)$/) {
264 if (!$size_parse_reported{$_}) {
265 $output->write("$type_name: can't parse type\n");
266 $size_parse_reported{$_} = 1;
268 } elsif (/^\w+\s*\((?:\s*CALLBACK|\s*NTAPI|\s*WINAPI)?\s*\*\s*\)\s*\(.*?\)$/) {
275 if (defined(my $type = $type_name2type{$_})) {
276 $align2 = $type->align;
279 if (!defined($align)) {
281 } elsif (defined($align2) && !$align_kludge_reported{$_}) {
282 $align_kludge_reported{$_} = 1;
283 $output->write("$type_name: type needn't be kludged\n");
286 if (!defined($align)) {
287 # $output->write("$type_name: can't find type\n");
291 if (defined(my $type = $type_name2type{$_})) {
292 $size2 = $type->size;
295 if (!defined($size)) {
297 } elsif (defined($size2) && !$size_kludge_reported{$_}) {
298 $size_kludge_reported{$_} = 1;
299 $output->write("$type_name: type needn't be kludged\n");
302 if (!defined($size)) {
303 # $output->write("$type_name: can't find type\n");
304 } elsif (defined($count)) {
305 if ($count =~ /^\d+$/) {
306 $size *= int($count);
307 } elsif (defined(my $count2 = $defines{$count})) {
308 $size *= int($count2);
310 $output->write("$type_name: can't parse type ('$_') ('$count')\n");
313 } elsif (defined($bits)) {
317 return ($align, $kind, $size);
321 my $type_name = shift;
322 (my $align, my $kind, my $size) = _find_align_kind_size
($type_name);
327 my $type_name = shift;
328 (my $align, my $kind, my $size) = _find_align_kind_size
($type_name);
334 my $type_name = shift;
335 (my $align, my $kind, my $size) = _find_align_kind_size
($type_name);
339 foreach my $file (@files) {
343 open(IN
, "< $wine_dir/$file");
352 while(s/^.*?\n//) { $max_line++; }
353 if($_) { $max_line++; }
356 my $parser = new c_parser
($file);
362 my $update_output = sub {
366 $progress .= "$file (file $progress_current of $progress_max)";
367 $prefix .= "$file: ";
370 $progress .= ": line $line of $max_line";
373 $output->progress($progress);
374 $output->prefix($prefix);
379 my $found_line = sub {
384 $parser->set_found_line_callback($found_line);
386 my $found_preprocessor = sub {
387 my $begin_line = shift;
388 my $begin_column = shift;
389 my $preprocessor = shift;
391 #print "found_preprocessor: $begin_line: [$_]\n";
392 if ($preprocessor =~ /^\#\s*include\s+[\"<]pshpack(\d+)\.h[\">]$/) {
394 #print "found pack $1 on line $begin_line\n";
395 } elsif($preprocessor =~ /^\#\s*include\s+[\"<]poppack\.h[\">]$/) {
397 #print "found poppack on line $begin_line\n";
402 $parser->set_found_preprocessor_callback($found_preprocessor);
404 my $found_type = sub {
409 my $name = $type->name;
410 $file2types{$file}{$name} = $type;
412 $type->set_find_align_callback(\
&find_align
);
413 $type->set_find_kind_callback(\
&find_kind
);
414 $type->set_find_size_callback(\
&find_size
);
416 my $pack = $packs[$#packs];
417 if (!defined($type->pack) && $type->kind =~ /^(?:struct|union)$/) {
420 my $size = $type->size();
421 if (defined($size)) {
422 my $max_field_base_size = 0;
424 foreach my $field ($type->fields()) {
425 my $field_type_name = $field->type_name;
426 my $field_name = $field->name;
427 my $field_size = $field->size;
428 my $field_base_size = $field->base_size;
429 my $field_offset = $field->offset;
430 my $field_align = $field->align;
432 # $output->write("$name: $field_type_name: $field_name: $field_offset: $field_size($field_base_size): $field_align\n");
434 # $output->write("$name: $size\n");
436 $type_name2type{$name} = $type;
438 # $output->write("$name: can't find size\n");
443 $parser->set_found_type_callback($found_type);
448 if(!$parser->parse_c_file(\
$_, \
$line, \
$column)) {
449 $output->write("can't parse file\n");
456 ########################################################################
462 my $test_dir = shift;
463 my @tests = @
{(shift)};
465 print OUT
"/* File generated automatically from tools/winapi/test.dat; do not edit! */\n";
466 print OUT
"/* This file can be copied, modified and distributed without restriction. */\n";
470 foreach my $test (@tests) {
471 my @description = $tests->get_section($test_dir, $test, "description");
472 foreach my $description (@description) {
473 print OUT
" * $description\n";
479 print OUT
"#define WINVER 0x0501\n";
480 print OUT
"#define _WIN32_IE 0x0501\n";
481 print OUT
"#define _WIN32_WINNT 0x0501\n";
483 print OUT
"#define WINE_NOWINSOCK\n";
485 foreach my $test (@tests) {
486 my @includes = $tests->get_section($test_dir, $test, "include");
487 foreach my $include (@includes) {
488 print OUT
"#include $include\n";
492 print OUT
"#include \"wine/test.h\"\n";
495 print OUT
"/***********************************************************************\n";
496 print OUT
" * Compability macros\n";
499 print OUT
"#define DWORD_PTR UINT_PTR\n";
500 print OUT
"#define LONG_PTR INT_PTR\n";
501 print OUT
"#define ULONG_PTR UINT_PTR\n";
504 print OUT
"/***********************************************************************\n";
505 print OUT
" * Windows API extension\n";
508 print OUT
"#if defined(_MSC_VER) && (_MSC_VER >= 1300) && defined(__cplusplus)\n";
509 print OUT
"# define FIELD_ALIGNMENT(type, field) __alignof(((type*)0)->field)\n";
510 print OUT
"#elif defined(__GNUC__)\n";
511 print OUT
"# define FIELD_ALIGNMENT(type, field) __alignof__(((type*)0)->field)\n";
513 print OUT
"/* FIXME: Not sure if is possible to do without compiler extension */\n";
514 print OUT
"#endif\n";
516 print OUT
"#if defined(_MSC_VER) && (_MSC_VER >= 1300) && defined(__cplusplus)\n";
517 print OUT
"# define _TYPE_ALIGNMENT(type) __alignof(type)\n";
518 print OUT
"#elif defined(__GNUC__)\n";
519 print OUT
"# define _TYPE_ALIGNMENT(type) __alignof__(type)\n";
522 print OUT
" * FIXME: Not sure if is possible to do without compiler extension\n";
523 print OUT
" * (if type is not just a name that is, if so the normal)\n";
524 print OUT
" * TYPE_ALIGNMENT can be used)\n";
526 print OUT
"#endif\n";
528 print OUT
"#if !defined(TYPE_ALIGNMENT) && defined(_TYPE_ALIGNMENT)\n";
529 print OUT
"# define TYPE_ALIGNMENT _TYPE_ALIGNMENT\n";
530 print OUT
"#endif\n";
533 print OUT
"/***********************************************************************\n";
534 print OUT
" * Test helper macros\n";
537 print OUT
"#ifdef FIELD_ALIGNMENT\n";
538 print OUT
"# define TEST_FIELD_ALIGNMENT(type, field, align) \\\n";
539 print OUT
" ok(FIELD_ALIGNMENT(type, field) == align, \\\n";
540 print OUT
" \"FIELD_ALIGNMENT(\" #type \", \" #field \") == %d (expected \" #align \")\\n\", \\\n";
541 print OUT
" FIELD_ALIGNMENT(type, field))\n";
543 print OUT
"# define TEST_FIELD_ALIGNMENT(type, field, align) do { } while (0)\n";
544 print OUT
"#endif\n";
546 print OUT
"#define TEST_FIELD_OFFSET(type, field, offset) \\\n";
547 print OUT
" ok(FIELD_OFFSET(type, field) == offset, \\\n";
548 print OUT
" \"FIELD_OFFSET(\" #type \", \" #field \") == %ld (expected \" #offset \")\\n\", \\\n";
549 print OUT
" FIELD_OFFSET(type, field))\n";
551 print OUT
"#ifdef _TYPE_ALIGNMENT\n";
552 print OUT
"#define TEST__TYPE_ALIGNMENT(type, align) \\\n";
553 print OUT
" ok(_TYPE_ALIGNMENT(type) == align, \"TYPE_ALIGNMENT(\" #type \") == %d (expected \" #align \")\\n\", _TYPE_ALIGNMENT(type))\n";
555 print OUT
"# define TEST__TYPE_ALIGNMENT(type, align) do { } while (0)\n";
556 print OUT
"#endif\n";
558 print OUT
"#ifdef TYPE_ALIGNMENT\n";
559 print OUT
"#define TEST_TYPE_ALIGNMENT(type, align) \\\n";
560 print OUT
" ok(TYPE_ALIGNMENT(type) == align, \"TYPE_ALIGNMENT(\" #type \") == %d (expected \" #align \")\\n\", TYPE_ALIGNMENT(type))\n";
562 print OUT
"# define TEST_TYPE_ALIGNMENT(type, align) do { } while (0)\n";
563 print OUT
"#endif\n";
565 print OUT
"#define TEST_TYPE_SIZE(type, size) \\\n";
566 print OUT
" ok(sizeof(type) == size, \"sizeof(\" #type \") == %d (expected \" #size \")\\n\", sizeof(type))\n";
568 print OUT
"/***********************************************************************\n";
569 print OUT
" * Test macros\n";
572 print OUT
"#define TEST_FIELD(type, field_type, field_name, field_offset, field_size, field_align) \\\n";
573 print OUT
" TEST_TYPE_SIZE(field_type, field_size); \\\n";
574 print OUT
" TEST_FIELD_ALIGNMENT(type, field_name, field_align); \\\n";
575 print OUT
" TEST_FIELD_OFFSET(type, field_name, field_offset); \\\n";
577 print OUT
"#define TEST_TYPE(type, size, align) \\\n";
578 print OUT
" TEST_TYPE_ALIGNMENT(type, align); \\\n";
579 print OUT
" TEST_TYPE_SIZE(type, size)\n";
581 print OUT
"#define TEST_TYPE_POINTER(type, size, align) \\\n";
582 print OUT
" TEST__TYPE_ALIGNMENT(*(type)0, align); \\\n";
583 print OUT
" TEST_TYPE_SIZE(*(type)0, size)\n";
585 print OUT
"#define TEST_TYPE_SIGNED(type) \\\n";
586 print OUT
" ok((type) -1 < 0, \"(\" #type \") -1 < 0\\n\");\n";
588 print OUT
"#define TEST_TYPE_UNSIGNED(type) \\\n";
589 print OUT
" ok((type) -1 > 0, \"(\" #type \") -1 > 0\\n\");\n";
593 ########################################################################
599 my $test_dir = shift;
600 my @tests = @
{(shift)};
602 print OUT
"START_TEST(generated)\n";
604 foreach my $test (@tests) {
605 print OUT
" test_$test();\n";
610 ########################################################################
611 # output_test_pack_type
613 sub output_test_pack_type
{
616 my $type_name2type = shift;
617 my $type_name2optional = shift;
618 my $type_name2optional_fields = shift;
619 my $type_name = shift;
622 my $optional_fields = $$type_name2optional_fields{$type_name};
624 my $type_align = $type->align;
625 my $type_pack = $type->pack;
626 my $type_size = $type->size;
627 my $type_kind = $type->kind;
629 if (defined($type_pack)) {
630 print OUT
" /* $type_name (pack $type_pack) */\n";
632 print OUT
" /* $type_name */\n";
635 if (!scalar(keys(%$optional_fields)) && defined($type_align) && defined($type_size)) {
636 print OUT
" TEST_TYPE($type_name, $type_size, $type_align);\n";
639 if ($type_kind eq "float") {
641 } elsif ($type_kind eq "pointer") {
642 my $dereference_type;
643 $dereference_type = sub {
646 my @fields = $type->fields;
647 my $type_name2 =$fields[0]->type_name;
649 if ($type_name2 =~ s/\s*\*$//) {
650 my $type2 = $$type_name2type{$type_name2};
651 if (defined($type2)) {
654 if ($type_name2 !~ /^(?:PVOID|VOID|void)$/) {
655 $output->write("$type_name2: warning: type not found 1\n");
659 } elsif ($type_name2 =~ /^\w+$/) {
660 my $type2 = $$type_name2type{$type_name2};
661 if (defined($type2)) {
662 return &$dereference_type($type2);
664 $output->write("$type_name2: warning: type not found\n");
667 } elsif ($type_name2 =~ /^\w+\s*\((?:\s*CALLBACK|\s*NTAPI|\s*WINAPI)?\s*\*\s*\)\s*\(.*?\)$/) {
670 $output->write("$type_name2: warning: type can't be parsed\n");
675 my $type2 = &$dereference_type($type);
676 if (defined($type2)) {
677 my $type_name2 = $type2->name;
678 my $type_align2 = $type2->align;
679 my $type_size2 = $type2->size;
681 my $optional = $$type_name2optional{$type_name};
682 my $optional_fields2 = $$type_name2optional_fields{$type_name2};
684 if (!$optional && !scalar(keys(%$optional_fields2)) && defined($type_align2) && defined($type_size2)) {
685 print OUT
" TEST_TYPE_POINTER($type_name, $type_size2, $type_align2);\n";
687 # $output->write("$type_name: warning: type size not found\n");
690 } elsif ($type_kind eq "signed") {
691 print OUT
" TEST_TYPE_SIGNED($type_name);\n";
692 } elsif ($type_kind eq "unsigned") {
693 print OUT
" TEST_TYPE_UNSIGNED($type_name);\n";
697 sub output_test_pack_fields
{
700 my $type_name2type = shift;
701 my $type_name2optional = shift;
702 my $type_name2optional_fields = shift;
703 my $type_name = shift;
707 my $optional_fields = $$type_name2optional_fields{$type_name};
709 foreach my $field ($type->fields()) {
710 my $field_type_name = $field->type_name;
711 my $field_name = $field->name;
712 my $field_size = $field->size;
713 my $field_offset = $field->offset;
714 my $field_align = $field->align;
716 next if $field_name eq "" || (defined($field_size) && $field_size < 0);
718 if ($$optional_fields{$field_name}) {
720 } elsif (defined($field_size) && defined($field_offset)) {
721 $field_offset += $offset;
722 if ($field_name eq "DUMMYSTRUCTNAME") {
723 print OUT
"#ifdef NONAMELESSSTRUCT\n";
724 print OUT
" TEST_FIELD($type_name, $field_type_name, $field_name, ";
725 print OUT
"$field_offset, $field_size, $field_align);\n";
727 output_test_pack_fields
(\
*OUT
, $type_name2type, $type_name2optional, $type_name2optional_fields,
728 $type_name, $$type_name2type{$field_type_name}, $field_offset);
729 print OUT
"#endif\n";
731 print OUT
" TEST_FIELD($type_name, $field_type_name, $field_name, ";
732 print OUT
"$field_offset, $field_size, $field_align);\n";
735 # $output->write("$type_name: $field_type_name: $field_name: test not generated (offset not defined)\n");
740 ########################################################################
743 sub output_test_pack
{
746 my $test_dir = shift;
749 my $type_names_used = shift;
751 $output->prefix("$test_dir: $test: ");
753 my @headers = $tests->get_section($test_dir, $test, "header");
754 my @type_names = $tests->get_section($test_dir, $test, "type");
756 my %type_name2optional;
757 my %type_name2optional_fields;
759 foreach my $_type_name (@type_names) {
760 my $type_name = $_type_name;
762 if ($type_name =~ s/^!//) {
763 $type_name2optional{$type_name}++;
766 my $optional_fields = {};
767 if ($type_name =~ s/:\s*(.*?)$//) {
768 my @fields = split /\s+/, $1;
769 foreach my $field (@fields) {
770 if ($field =~ s/^!//) {
771 $$optional_fields{$field}++;
776 $type_name2optional_fields{$type_name} = $optional_fields;
779 foreach my $header (@headers) {
780 my $type_name2type = $file2types{"include/$header"};
782 foreach my $_type_name (@type_names) {
783 my $type_name = $_type_name;
785 my $skip = ($type_name =~ s/^!//);
786 $type_name =~ s/:.*?$//;
787 my $type = $$type_name2type{$type_name};
788 if (!defined($type)) {
791 $$type_names_used{$type_name} = $skip ?
-1 : 1;
794 print OUT
"static void test_${test}_$type_name(void)\n";
796 output_test_pack_type
(\
*OUT
, $type_name2type, \
%type_name2optional, \
%type_name2optional_fields,
798 output_test_pack_fields
(\
*OUT
, $type_name2type, \
%type_name2optional, \
%type_name2optional_fields,
799 $type_name, $type, 0);
807 ########################################################################
813 my $test_dir = shift;
814 my @tests = @
{(shift)};
816 my $type_names_used = shift;
818 output_header
(\
*OUT
, $test_dir, \
@tests);
820 foreach my $test (@tests) {
821 my %type_names_used2;
823 if ($test eq "pack") {
824 output_test_pack
(\
*OUT
, $test_dir, $test, \
%type_names_used2);
826 die "no such test ($test)\n";
829 print OUT
"static void test_$test(void)\n";
831 foreach my $type_name (sort(keys(%type_names_used2))) {
832 $$type_names_used{$type_name} = $type_names_used2{$type_name};
833 if ($type_names_used2{$type_name} > 0) {
834 print OUT
" test_${test}_$type_name();\n";
841 output_footer
(\
*OUT
, $test_dir, \
@tests);
846 ########################################################################
849 my %type_names_used = ();
851 my @test_dirs = $tests->get_test_dirs();
852 foreach my $test_dir (@test_dirs) {
853 my $file = "$wine_dir/$test_dir/generated.c";
854 replace_file
($file, \
&output_file
, $test_dir, \
@tests, \
%type_names_used);
857 foreach my $header (sort(keys(%file2types))) {
858 $output->prefix("$header: ");
859 $header =~ s
%^include
/%%;
860 my $type_name2type = $file2types{"include/$header"};
861 foreach my $_type_name (sort(keys(%$type_name2type))) {
862 my $type_name = $_type_name;
864 if (!exists($type_names_used{$type_name})) {
865 $output->write("$type_name: type not used\n");
870 $output->prefix("$winapi_dir/tests.dat: ");
871 foreach my $type_name (sort(keys(%type_names_used))) {
873 foreach my $header (sort(keys(%file2types))) {
874 my $type_name2type = $file2types{"include/$header"};
875 if (exists($type_name2type{$type_name})) {
881 $output->write("$type_name: type not used\n");