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 local $_ = $preprocessor;
372 if (/^\#\s*include\s+\"pshpack(\d+)\.h\"$/) {
374 } elsif(/^\#\s*include\s+\"poppack\.h\"$/) {
380 $parser->set_found_preprocessor_callback($found_preprocessor);
382 my $found_type = sub {
387 my $name = $type->name;
388 $file2types{$file}{$name} = $type;
390 $type->set_find_align_callback(\
&find_align
);
391 $type->set_find_kind_callback(\
&find_kind
);
392 $type->set_find_size_callback(\
&find_size
);
394 my $pack = $packs[$#packs];
395 if (!defined($type->pack) && $type->kind =~ /^(?:struct|union)$/) {
398 my $size = $type->size();
399 if (defined($size)) {
400 my $max_field_base_size = 0;
402 foreach my $field ($type->fields()) {
403 my $field_type_name = $field->type_name;
404 my $field_name = $field->name;
405 my $field_size = $field->size;
406 my $field_base_size = $field->base_size;
407 my $field_offset = $field->offset;
408 my $field_align = $field->align;
410 # $output->write("$name: $field_type_name: $field_name: $field_offset: $field_size($field_base_size): $field_align\n");
412 # $output->write("$name: $size\n");
414 $type_name2type{$name} = $type;
416 # $output->write("$name: can't find size\n");
421 $parser->set_found_type_callback($found_type);
426 if(!$parser->parse_c_file(\
$_, \
$line, \
$column)) {
427 $output->write("can't parse file\n");
434 ########################################################################
440 my $test_dir = shift;
441 my @tests = @
{(shift)};
443 print OUT
"/* File generated automatically from tools/winapi/test.dat; do not edit! */\n";
444 print OUT
"/* This file can be copied, modified and distributed without restriction. */\n";
448 foreach my $test (@tests) {
449 my @description = $tests->get_section($test_dir, $test, "description");
450 foreach my $description (@description) {
451 print OUT
" * $description\n";
457 print OUT
"#define WINVER 0x0501\n";
458 print OUT
"#define _WIN32_IE 0x0501\n";
459 print OUT
"#define _WIN32_WINNT 0x0501\n";
461 print OUT
"#define WINE_NOWINSOCK\n";
463 foreach my $test (@tests) {
464 my @includes = $tests->get_section($test_dir, $test, "include");
465 foreach my $include (@includes) {
466 print OUT
"#include \"$include\"\n";
470 print OUT
"#include \"wine/test.h\"\n";
473 print OUT
"/***********************************************************************\n";
474 print OUT
" * Compability macros\n";
477 print OUT
"#define DWORD_PTR UINT_PTR\n";
478 print OUT
"#define LONG_PTR INT_PTR\n";
479 print OUT
"#define ULONG_PTR UINT_PTR\n";
482 print OUT
"/***********************************************************************\n";
483 print OUT
" * Windows API extension\n";
486 print OUT
"#if (_MSC_VER >= 1300) && defined(__cplusplus)\n";
487 print OUT
"# define FIELD_ALIGNMENT(type, field) __alignof(((type*)0)->field)\n";
488 print OUT
"#elif defined(__GNUC__)\n";
489 print OUT
"# define FIELD_ALIGNMENT(type, field) __alignof__(((type*)0)->field)\n";
491 print OUT
"/* FIXME: Not sure if is possible to do without compiler extension */\n";
492 print OUT
"#endif\n";
494 print OUT
"#if (_MSC_VER >= 1300) && defined(__cplusplus)\n";
495 print OUT
"# define _TYPE_ALIGNMENT(type) __alignof(type)\n";
496 print OUT
"#elif defined(__GNUC__)\n";
497 print OUT
"# define _TYPE_ALIGNMENT(type) __alignof__(type)\n";
500 print OUT
" * FIXME: Not sure if is possible to do without compiler extension\n";
501 print OUT
" * (if type is not just a name that is, if so the normal)\n";
502 print OUT
" * TYPE_ALIGNMENT can be used)\n";
504 print OUT
"#endif\n";
506 print OUT
"#if !defined(TYPE_ALIGNMENT) && defined(_TYPE_ALIGNMENT)\n";
507 print OUT
"# define TYPE_ALIGNMENT _TYPE_ALIGNMENT\n";
508 print OUT
"#endif\n";
511 print OUT
"/***********************************************************************\n";
512 print OUT
" * Test helper macros\n";
515 print OUT
"#ifdef FIELD_ALIGNMENT\n";
516 print OUT
"# define TEST_FIELD_ALIGNMENT(type, field, align) \\\n";
517 print OUT
" ok(FIELD_ALIGNMENT(type, field) == align, \\\n";
518 print OUT
" \"FIELD_ALIGNMENT(\" #type \", \" #field \") == %d (expected \" #align \")\", \\\n";
519 print OUT
" FIELD_ALIGNMENT(type, field))\n";
521 print OUT
"# define TEST_FIELD_ALIGNMENT(type, field, align) do { } while (0)\n";
522 print OUT
"#endif\n";
524 print OUT
"#define TEST_FIELD_OFFSET(type, field, offset) \\\n";
525 print OUT
" ok(FIELD_OFFSET(type, field) == offset, \\\n";
526 print OUT
" \"FIELD_OFFSET(\" #type \", \" #field \") == %ld (expected \" #offset \")\", \\\n";
527 print OUT
" FIELD_OFFSET(type, field))\n";
529 print OUT
"#ifdef _TYPE_ALIGNMENT\n";
530 print OUT
"#define TEST__TYPE_ALIGNMENT(type, align) \\\n";
531 print OUT
" ok(_TYPE_ALIGNMENT(type) == align, \"TYPE_ALIGNMENT(\" #type \") == %d (expected \" #align \")\", _TYPE_ALIGNMENT(type))\n";
533 print OUT
"# define TEST__TYPE_ALIGNMENT(type, align) do { } while (0)\n";
534 print OUT
"#endif\n";
536 print OUT
"#ifdef TYPE_ALIGNMENT\n";
537 print OUT
"#define TEST_TYPE_ALIGNMENT(type, align) \\\n";
538 print OUT
" ok(TYPE_ALIGNMENT(type) == align, \"TYPE_ALIGNMENT(\" #type \") == %d (expected \" #align \")\", TYPE_ALIGNMENT(type))\n";
540 print OUT
"# define TEST_TYPE_ALIGNMENT(type, align) do { } while (0)\n";
541 print OUT
"#endif\n";
543 print OUT
"#define TEST_TYPE_SIZE(type, size) \\\n";
544 print OUT
" ok(sizeof(type) == size, \"sizeof(\" #type \") == %d (expected \" #size \")\", sizeof(type))\n";
546 print OUT
"/***********************************************************************\n";
547 print OUT
" * Test macros\n";
550 print OUT
"#define TEST_FIELD(type, field_type, field_name, field_offset, field_size, field_align) \\\n";
551 print OUT
" TEST_TYPE_SIZE(field_type, field_size); \\\n";
552 print OUT
" TEST_FIELD_ALIGNMENT(type, field_name, field_align); \\\n";
553 print OUT
" TEST_FIELD_OFFSET(type, field_name, field_offset); \\\n";
555 print OUT
"#define TEST_TYPE(type, size, align) \\\n";
556 print OUT
" TEST_TYPE_ALIGNMENT(type, align); \\\n";
557 print OUT
" TEST_TYPE_SIZE(type, size)\n";
559 print OUT
"#define TEST_TYPE_POINTER(type, size, align) \\\n";
560 print OUT
" TEST__TYPE_ALIGNMENT(*(type)0, align); \\\n";
561 print OUT
" TEST_TYPE_SIZE(*(type)0, size)\n";
563 print OUT
"#define TEST_TYPE_SIGNED(type) \\\n";
564 print OUT
" ok((type) -1 < 0, \"(\" #type \") -1 < 0\");\n";
566 print OUT
"#define TEST_TYPE_UNSIGNED(type) \\\n";
567 print OUT
" ok((type) -1 > 0, \"(\" #type \") -1 > 0\");\n";
571 ########################################################################
577 my $test_dir = shift;
578 my @tests = @
{(shift)};
580 print OUT
"START_TEST(generated)\n";
582 foreach my $test (@tests) {
583 print OUT
" test_$test();\n";
588 ########################################################################
589 # output_test_pack_type
591 sub output_test_pack_type
{
594 my $type_name2type = shift;
595 my $type_name2optional = shift;
596 my $type_name2optional_fields = shift;
597 my $type_name = shift;
600 my $optional_fields = $$type_name2optional_fields{$type_name};
602 my $type_align = $type->align;
603 my $type_pack = $type->pack;
604 my $type_size = $type->size;
605 my $type_kind = $type->kind;
607 if (defined($type_pack)) {
608 print OUT
" /* $type_name (pack $type_pack) */\n";
610 print OUT
" /* $type_name */\n";
613 if (!scalar(keys(%$optional_fields)) && defined($type_align) && defined($type_size)) {
614 print OUT
" TEST_TYPE($type_name, $type_size, $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_TYPE_POINTER($type_name, $type_size2, $type_align2);\n";
665 # $output->write("$type_name: warning: type size not found\n");
668 } elsif ($type_kind eq "signed") {
669 print OUT
" TEST_TYPE_SIGNED($type_name);\n";
670 } elsif ($type_kind eq "unsigned") {
671 print OUT
" TEST_TYPE_UNSIGNED($type_name);\n";
675 sub output_test_pack_fields
{
678 my $type_name2type = shift;
679 my $type_name2optional = shift;
680 my $type_name2optional_fields = shift;
681 my $type_name = shift;
685 my $optional_fields = $$type_name2optional_fields{$type_name};
687 foreach my $field ($type->fields()) {
688 my $field_type_name = $field->type_name;
689 my $field_name = $field->name;
690 my $field_size = $field->size;
691 my $field_offset = $field->offset;
692 my $field_align = $field->align;
694 next if $field_name eq "" || (defined($field_size) && $field_size < 0);
696 if ($$optional_fields{$field_name}) {
698 } elsif (defined($field_size) && defined($field_offset)) {
699 $field_offset += $offset;
700 if ($field_name eq "DUMMYSTRUCTNAME") {
701 print OUT
"#ifdef NONAMELESSSTRUCT\n";
702 print OUT
" TEST_FIELD($type_name, $field_type_name, $field_name, ";
703 print OUT
"$field_offset, $field_size, $field_align);\n";
705 output_test_pack_fields
(\
*OUT
, $type_name2type, $type_name2optional, $type_name2optional_fields,
706 $type_name, $$type_name2type{$field_type_name}, $field_offset);
707 print OUT
"#endif\n";
709 print OUT
" TEST_FIELD($type_name, $field_type_name, $field_name, ";
710 print OUT
"$field_offset, $field_size, $field_align);\n";
713 # $output->write("$type_name: $field_type_name: $field_name: test not generated (offset not defined)\n");
718 ########################################################################
721 sub output_test_pack
{
724 my $test_dir = shift;
727 my $type_names_used = shift;
729 $output->prefix("$test_dir: $test: ");
731 my @headers = $tests->get_section($test_dir, $test, "header");
732 my @type_names = $tests->get_section($test_dir, $test, "type");
734 my %type_name2optional;
735 my %type_name2optional_fields;
737 foreach my $_type_name (@type_names) {
738 my $type_name = $_type_name;
740 if ($type_name =~ s/^!//) {
741 $type_name2optional{$type_name}++;
744 my $optional_fields = {};
745 if ($type_name =~ s/:\s*(.*?)$//) {
746 my @fields = split /\s+/, $1;
747 foreach my $field (@fields) {
748 if ($field =~ s/^!//) {
749 $$optional_fields{$field}++;
754 $type_name2optional_fields{$type_name} = $optional_fields;
757 foreach my $header (@headers) {
758 my $type_name2type = $file2types{"include/$header"};
760 foreach my $_type_name (@type_names) {
761 my $type_name = $_type_name;
763 my $skip = ($type_name =~ s/^!//);
764 $type_name =~ s/:.*?$//;
765 my $type = $$type_name2type{$type_name};
766 if (!defined($type)) {
769 $$type_names_used{$type_name} = $skip ?
-1 : 1;
772 print OUT
"static void test_${test}_$type_name(void)\n";
774 output_test_pack_type
(\
*OUT
, $type_name2type, \
%type_name2optional, \
%type_name2optional_fields,
776 output_test_pack_fields
(\
*OUT
, $type_name2type, \
%type_name2optional, \
%type_name2optional_fields,
777 $type_name, $type, 0);
785 ########################################################################
791 my $test_dir = shift;
792 my @tests = @
{(shift)};
794 my $type_names_used = shift;
796 output_header
(\
*OUT
, $test_dir, \
@tests);
798 foreach my $test (@tests) {
799 my %type_names_used2;
801 if ($test eq "pack") {
802 output_test_pack
(\
*OUT
, $test_dir, $test, \
%type_names_used2);
804 die "no such test ($test)\n";
807 print OUT
"static void test_$test(void)\n";
809 foreach my $type_name (sort(keys(%type_names_used2))) {
810 $$type_names_used{$type_name} = $type_names_used2{$type_name};
811 if ($type_names_used2{$type_name} > 0) {
812 print OUT
" test_${test}_$type_name();\n";
819 output_footer
(\
*OUT
, $test_dir, \
@tests);
824 ########################################################################
827 my %type_names_used = ();
829 my @test_dirs = $tests->get_test_dirs();
830 foreach my $test_dir (@test_dirs) {
831 my $file = "$wine_dir/$test_dir/generated.c";
832 replace_file
($file, \
&output_file
, $test_dir, \
@tests, \
%type_names_used);
835 foreach my $header (sort(keys(%file2types))) {
836 $output->prefix("$header: ");
837 $header =~ s
%^include
/%%;
838 my $type_name2type = $file2types{"include/$header"};
839 foreach my $_type_name (sort(keys(%$type_name2type))) {
840 my $type_name = $_type_name;
842 if (!exists($type_names_used{$type_name})) {
843 $output->write("$type_name: type not used\n");
848 $output->prefix("$winapi_dir/tests.dat: ");
849 foreach my $type_name (sort(keys(%type_names_used))) {
851 foreach my $header (sort(keys(%file2types))) {
852 my $type_name2type = $file2types{"include/$header"};
853 if (exists($type_name2type{$type_name})) {
859 $output->write("$type_name: type not used\n");