mshtml: Added IHTMLScreen::get_availWidth implementation.
[wine] / tools / winapi / winapi_test
1 #!/usr/bin/perl -w
2
3 # Copyright 2002 Patrik Stridvall
4 #
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.
9 #
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.
14 #
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
18 #
19
20 use strict;
21
22 BEGIN {
23     $0 =~ m%^(.*?/?tools)/winapi/winapi_test$%;
24     require "$1/winapi/setup.pm";
25 }
26
27 use config qw(
28     file_type files_skip files_filter
29     $current_dir $wine_dir $winapi_dir
30 );
31 use output qw($output);
32 use winapi_test_options qw($options);
33
34 if($options->progress) {
35     $output->enable_progress;
36 } else {
37     $output->disable_progress;
38 }
39
40 use c_parser;
41 use tests qw($tests);
42 use type;
43 use util qw(replace_file);
44
45 my $pointer_size = 4;
46
47 my @tests = ();
48 if ($options->pack) {
49     push @tests, "pack";
50 }
51
52 my @files;
53 {
54     my %files;
55
56     foreach my $test (@tests)
57     {
58         foreach my $test_dir ($tests->get_test_dirs($test))
59         {
60             foreach my $header ($tests->get_section($test_dir, $test, "header"))
61             {
62                 if (!$files{$header})
63                 {
64                     push @files, "include/$header";
65                     $files{$header} = 1;
66                 }
67             }
68         }
69     }
70 }
71
72 if (0) {
73     my $file = "tests.dat";
74
75     $file .= "2"; # FIXME: For tests
76
77     open(OUT, "> $winapi_dir/$file") || die "Error: Can't open $winapi_dir/$file: $!\n";
78
79     my $x = 0;
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";
84         print OUT "\n";
85
86         my $y = 0;
87         my @tests = $tests->get_tests($test_dir);
88         foreach my $test (@tests) {
89             print OUT "\n" if $y++;
90             print OUT "%%$test\n";
91             print OUT "\n";
92
93             my @types;
94
95             my $z = 0;
96             my @sections = $tests->get_sections($test_dir, $test);
97             foreach my $section (@sections) {
98                 my @lines = $tests->get_section($test_dir, $test, $section);
99
100                 if ($section =~ /^(?:struct|type)$/) {
101                     foreach my $line (@lines) {
102                         push @types, $line;
103                     }
104                     next;
105                 }
106
107                 print OUT "\n" if $z++;
108                 print OUT "%$section\n";
109                 print OUT "\n";
110                 foreach my $line (@lines) {
111                         print OUT "$line\n";
112                 }
113             }
114
115             @types = sort { $x = $a; $y = $b; $x =~ s/^!//;  $y =~ s/^!//; $x cmp $y } @types;
116
117             print OUT "\n" if $z++;
118             print OUT "%type\n";
119             print OUT "\n";
120             foreach my $type (@types) {
121                 print OUT "$type\n";
122             }
123         }
124     }
125
126     close(OUT);
127     exit(0);
128 }
129
130
131 my %file2types;
132
133 my $progress_output;
134 my $progress_current = 0;
135 my $progress_max = scalar(@files);
136
137 ########################################################################
138 # find_type
139
140 my %type_name2type;
141
142 my %defines = (
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,
151     "LF_FACESIZE" => 32,
152     "LF_FULLFACESIZE" => 64,
153     "MAXIMUM_SUPPORTED_EXTENSION" => 512,
154     "MAX_GOPHER_DISPLAY_TEXT + 1" => 129,
155     "MAX_GOPHER_LOCATOR_LENGTH + 1" => 654,
156     "MAX_PATH" => 260,
157     "MAX_PROFILE_LEN" => 80,
158     "NUM_POINTS" => 3,
159     "OFS_MAXPATHNAME" => 128,
160     "SIZE_OF_80387_REGISTERS" => 80,
161     "TOKEN_SOURCE_LENGTH" => 8,
162 );
163
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;
167
168 sub _find_align_kind_size($) {
169     my $type_name = shift;
170
171     local $_ = $type_name;
172
173     my $align;
174     my $kind;
175     my $size;
176     if (/\*+$/) {
177         $align = $pointer_size;
178         $kind = "pointer";
179         $size = $pointer_size;
180     } elsif(/^char$/) {
181         $align = 1;
182         $kind = "char";
183         $size = 1;
184     } elsif(/^(?:(signed|unsigned)\s+)?(?:__int8|char|byte)$/) {
185         $align = 1;
186         $kind = defined($1) ? $1 : "signed";
187         $size = 1;
188     } elsif (/^(?:(signed|unsigned)\s+)?(?:__int16|short(?:\s+int)?)$/) {
189         $align = 2;
190         $kind = defined($1) ? $1 : "signed";
191         $size = 2;
192     } elsif (/^(?:wchar_t)$/) {
193         $align = 2;
194         $kind = "signed";
195         $size = 2;
196     } elsif (/^(signed|unsigned)$/) {
197         $align = 4;
198         $kind = defined($1) ? $1 : "signed";
199         $size = 4;
200     } elsif (/^(?:(signed|unsigned)\s+)?(?:__int32|int)$/) {
201         $align = 4;
202         $kind = defined($1) ? $1 : "signed";
203         $size = 4;
204     } elsif (/^(?:(signed|unsigned)\s+)?(?:long(?:\s+int)?)$/) {
205         $align = $pointer_size;
206         $kind = defined($1) ? $1 : "signed";
207         $size = $pointer_size;
208     } elsif (/^(?:float)$/) {
209         $align = 4;
210         $kind = "float";
211         $size = 4;
212     } elsif (/^(?:(signed|unsigned)\s+)?__int64$/) {
213         $align = 8;
214         $kind = defined($1) ? $1 : "signed";
215         $size = 8;
216     } elsif (/^(?:double|DOUBLE|DATE)$/) {
217         $align = 8;
218         $kind = "float";
219         $size = 8;
220     } elsif (/^(?:long\s+double)$/) {
221         $align = 4;
222         $kind = "float";
223         $size = 12;
224     } elsif (/^H(?:DC|BITMAP|BRUSH|ICON|INSTANCE|KEY|MENU|METAFILE|WND)$/) {
225         $align = $pointer_size;
226         $kind = "pointer";
227         $size = $pointer_size;
228     } elsif (/^LP(?:BYTE|CSTR|CWSTR|DWORD|STR|VOID|WSTR)$/) {
229         $align = $pointer_size;
230         $kind = "pointer";
231         $size = $pointer_size;
232     } elsif (/^(?:FILETIME)$/) {
233         $align = 4;
234         $kind = "struct";
235         $size = 8;
236     } elsif (/^GUID$/) {
237         $align = 4;
238         $kind = "struct";
239         $size = 16;
240     } elsif (/^(?:VOID)$/) {
241         $align = 4;
242         $kind = "signed";
243         $size = 4;
244     } elsif (/^(?:SHORT)$/) {
245         $align = 2;
246         $kind = "unsigned";
247         $size = 2;
248     } elsif (/^(?:BYTE)$/) {
249         $align = 1;
250         $kind = "unsigned";
251         $size = 1;
252     } elsif (/^(?:DWORD)$/) {
253         $align = 4;
254         $kind = "unsigned";
255         $size = 4;
256     } elsif (/^(?:WORD)$/) {
257         $align = 2;
258         $kind = "unsigned";
259         $size = 2;
260     } elsif (/^(?:INT64|LONG64|LONGLONG)$/) {
261         $align = 8;
262         $kind = "signed";
263         $size = 8;
264     } elsif (/^(?:UINT64|ULONG64|DWORD64|ULONGLONG|DWORDLONG)$/) {
265         $align = 8;
266         $kind = "unsigned";
267         $size = 8;
268     } elsif (/^(?:LARGE_INTEGER)$/) {
269         $align = 8;
270         $kind = "union";
271         $size = 8;
272     } elsif (/^(?:POINTS)$/) {
273         $align = 2;
274         $kind = "struct";
275         $size = 4;
276     } elsif (/^(struct|union)$/) {
277         $kind = $1;
278         if (!$size_parse_reported{$_}) {
279             $output->write("$type_name: can't parse type\n");
280             $size_parse_reported{$_} = 1;
281         }
282     } elsif (/^\w+\s*\((?:\s*CALLBACK|\s*NTAPI|\s*WINAPI)?\s*\*\s*\)\s*\(.*?\)$/) {
283         $align = $pointer_size;
284         $kind = "pointer";
285         $size = $pointer_size;
286     }
287
288     my $align2;
289     if (defined(my $type = $type_name2type{$pointer_size}{$_})) {
290         $align2 = $type->align;
291     }
292
293     if (!defined($align)) {
294         $align = $align2;
295     } elsif (defined($align2) && !$align_kludge_reported{$_}) {
296         $align_kludge_reported{$_} = 1;
297         $output->write("$type_name: type needn't be kludged\n");
298     }
299
300     if (!defined($align)) {
301         # $output->write("$type_name: can't find type\n");
302     }
303
304     my $size2;
305     if (defined(my $type = $type_name2type{$pointer_size}{$_})) {
306         $size2 = $type->size;
307     }
308
309     if (!defined($size)) {
310         $size = $size2;
311     } elsif (defined($size2) && !$size_kludge_reported{$_}) {
312         $size_kludge_reported{$_} = 1;
313         $output->write("$type_name: type needn't be kludged\n");
314     }
315
316     return ($align, $kind, $size);
317 }
318
319 sub find_align($) {
320     my $type_name = shift;
321     (my $align, my $kind, my $size) = _find_align_kind_size($type_name);
322     return $align;
323 }
324
325 sub find_kind($) {
326     my $type_name = shift;
327     (my $align, my $kind, my $size) = _find_align_kind_size($type_name);
328
329     return $kind;
330 }
331
332 sub find_size($) {
333     my $type_name = shift;
334     (my $align, my $kind, my $size) = _find_align_kind_size($type_name);
335     return $size;
336 }
337
338 sub find_count($) {
339     my $count = shift;
340     return $defines{$count};
341 }
342
343 foreach my $file (@files) {
344   $progress_current++;
345   foreach my $ptr (4, 8) {
346     $pointer_size = $ptr;
347
348     {
349         open(IN, "< $wine_dir/$file") || die "Error: Can't open $wine_dir/$file: $!\n";
350         local $/ = undef;
351         $_ = <IN>;
352         close(IN);
353     }
354
355     my $max_line = 0;
356     {
357       local $_ = $_;
358       while(s/^.*?\n//) { $max_line++; }
359       if($_) { $max_line++; }
360     }
361
362     my $parser = new c_parser($file);
363
364     my $line;
365     my $type;
366     my @packs = ();
367     my @ifdefs = ();
368
369     my $update_output = sub {
370         my $progress = "";
371         my $prefix = "";
372
373         $progress .= "$file (file $progress_current of $progress_max" . sprintf ", %u-bit)", $pointer_size * 8;
374         $prefix .= "$file: ";
375
376         if(defined($line)) {
377             $progress .= ": line $line of $max_line";
378         }
379
380         $output->progress($progress);
381         $output->prefix($prefix);
382     };
383
384     &$update_output();
385
386     my $found_line = sub {
387         $line = shift;
388
389         &$update_output;
390     };
391     $parser->set_found_line_callback($found_line);
392
393     my $found_preprocessor = sub {
394         my $begin_line = shift;
395         my $begin_column = shift;
396         my $preprocessor = shift;
397
398         #print "found_preprocessor: $begin_line: [$_]\n";
399         if ($preprocessor =~ /^\#\s*include\s+[\"<]pshpack(\d+)\.h[\">]$/) {
400             push @packs, $1 unless @ifdefs && !$ifdefs[$#ifdefs];
401             #print "found pack $1 on line $begin_line\n";
402         } elsif($preprocessor =~ /^\#\s*include\s+[\"<]poppack\.h[\">]$/) {
403             pop @packs unless @ifdefs && !$ifdefs[$#ifdefs];
404             #print "found poppack on line $begin_line\n";
405         } elsif ($preprocessor =~ /^\#\s*ifdef\s+_WIN64/) {
406             push @ifdefs, ($pointer_size == 8);
407         } elsif ($preprocessor =~ /^\#\s*ifndef\s+_WIN64/) {
408             push @ifdefs, ($pointer_size == 4);
409         } elsif ($preprocessor =~ /^\#\s*elif\s+defined(_WIN64)/) {
410             $ifdefs[$#ifdefs] = ($pointer_size == 8);
411         } elsif ($preprocessor =~ /^\#\s*ifdef\s/) {
412             push @ifdefs, 2;
413         } elsif ($preprocessor =~ /^\#\s*ifndef\s/) {
414             push @ifdefs, 2;
415         } elsif ($preprocessor =~ /^\#\s*if/) {
416             push @ifdefs, 2;
417         } elsif ($preprocessor =~ /^\#\s*else/) {
418             $ifdefs[$#ifdefs] = $ifdefs[$#ifdefs] ^ 1;
419         } elsif ($preprocessor =~ /^\#\s*elif/) {
420             $ifdefs[$#ifdefs] = 2;
421         } elsif ($preprocessor =~ /^\#\s*endif/) {
422             pop @ifdefs;
423         }
424         return 1;
425     };
426     $parser->set_found_preprocessor_callback($found_preprocessor);
427
428     my $found_type = sub {
429         $type = shift;
430         return if @ifdefs && !$ifdefs[$#ifdefs];
431
432         &$update_output();
433
434         my $name = $type->name;
435         $file2types{$pointer_size}{$file}{$name} = $type;
436
437         $type->set_find_align_callback(\&find_align);
438         $type->set_find_kind_callback(\&find_kind);
439         $type->set_find_size_callback(\&find_size);
440         $type->set_find_count_callback(\&find_count);
441
442         my $pack = $packs[$#packs];
443         if (!defined($type->pack) && $type->kind =~ /^(?:struct|union)$/) {
444             $type->pack($pack);
445         }
446         my $size = $type->size();
447         if (defined($size)) {
448             my $max_field_base_size = 0;
449
450             foreach my $field ($type->fields()) {
451                 my $field_type_name = $field->type_name;
452                 my $field_name = $field->name;
453                 my $field_size = $field->size;
454                 my $field_base_size = $field->base_size;
455                 my $field_offset = $field->offset;
456                 my $field_align = $field->align;
457
458                 # $output->write("$name: $field_type_name: $field_name: $field_offset: $field_size($field_base_size): $field_align\n");
459             }
460             # $output->write("$name: $size\n");
461
462             $type_name2type{$pointer_size}{$name} = $type;
463         } else {
464             # $output->write("$name: can't find size\n");
465         }
466
467         return 1;
468     };
469     $parser->set_found_type_callback($found_type);
470
471     {
472         my $line = 1;
473         my $column = 0;
474         if(!$parser->parse_c_file(\$_, \$line, \$column)) {
475             $output->write("can't parse file\n");
476         }
477     }
478
479     $output->prefix("");
480   }
481 }
482
483 ########################################################################
484 # output_header
485
486 sub output_header($$$) {
487     local *OUT = shift; 
488
489     my $test_dir = shift;
490     my @tests = @{(shift)};
491
492     print OUT "/* File generated automatically from tools/winapi/tests.dat; do not edit! */\n";
493     print OUT "/* This file can be copied, modified and distributed without restriction. */\n";
494     print OUT "\n";
495
496     print OUT "/*\n";
497     foreach my $test (@tests) {
498         my @description = $tests->get_section($test_dir, $test, "description");
499         foreach my $description (@description) {
500             print OUT " * $description\n";
501         }
502     }
503     print OUT " */\n";
504     print OUT "\n";
505
506     print OUT "#define WINVER 0x0501\n";
507     print OUT "#define _WIN32_IE 0x0501\n";
508     print OUT "#define _WIN32_WINNT 0x0501\n";
509     print OUT "\n";
510     print OUT "#define WINE_NOWINSOCK\n";
511     print OUT "\n";
512     foreach my $test (@tests) {
513         my @includes = $tests->get_section($test_dir, $test, "include");
514         foreach my $include (@includes) {
515             print OUT "#include $include\n";
516         }
517     }
518     print OUT "\n";
519     print OUT "#include \"wine/test.h\"\n";
520     print OUT "\n";
521
522     print OUT "/***********************************************************************\n";
523     print OUT " * Compatibility macros\n";
524     print OUT " */\n";
525     print OUT "\n";
526     print OUT "#define DWORD_PTR UINT_PTR\n";
527     print OUT "#define LONG_PTR INT_PTR\n";
528     print OUT "#define ULONG_PTR UINT_PTR\n";
529     print OUT "\n";
530
531     print OUT "/***********************************************************************\n";
532     print OUT " * Windows API extension\n";
533     print OUT " */\n";
534     print OUT "\n";
535     print OUT "#if defined(_MSC_VER) && (_MSC_VER >= 1300) && defined(__cplusplus)\n";
536     print OUT "# define _TYPE_ALIGNMENT(type) __alignof(type)\n";
537     print OUT "#elif defined(__GNUC__)\n";
538     print OUT "# define _TYPE_ALIGNMENT(type) __alignof__(type)\n";
539     print OUT "#else\n";
540     print OUT "/*\n";
541     print OUT " * FIXME: May not be possible without a compiler extension\n";
542     print OUT " *        (if type is not just a name that is, otherwise the normal\n";
543     print OUT " *         TYPE_ALIGNMENT can be used)\n";
544     print OUT " */\n";
545     print OUT "#endif\n";
546     print OUT "\n";
547     print OUT "#if defined(TYPE_ALIGNMENT) && defined(_MSC_VER) && _MSC_VER >= 800 && !defined(__cplusplus)\n";
548     print OUT "#pragma warning(disable:4116)\n";
549     print OUT "#endif\n";
550     print OUT "\n";
551     print OUT "#if !defined(TYPE_ALIGNMENT) && defined(_TYPE_ALIGNMENT)\n";
552     print OUT "# define TYPE_ALIGNMENT _TYPE_ALIGNMENT\n";
553     print OUT "#endif\n";
554     print OUT "\n";
555
556     print OUT "/***********************************************************************\n";
557     print OUT " * Test helper macros\n";
558     print OUT " */\n";
559     print OUT "\n";
560     print OUT "#define TEST_TYPE_SIZE(type, size)             C_ASSERT(sizeof(type) == size);\n";
561     print OUT "\n";
562     print OUT "#ifdef TYPE_ALIGNMENT\n";
563     print OUT "# define TEST_TYPE_ALIGN(type, align)          C_ASSERT(TYPE_ALIGNMENT(type) == align);\n";
564     print OUT "#else\n";
565     print OUT "# define TEST_TYPE_ALIGN(type, align)\n";
566     print OUT "#endif\n";
567     print OUT "\n";
568     print OUT "#ifdef _TYPE_ALIGNMENT\n";
569     print OUT "# define TEST_TARGET_ALIGN(type, align)        C_ASSERT(_TYPE_ALIGNMENT(*(type)0) == align);\n";
570     print OUT "# define TEST_FIELD_ALIGN(type, field, align)  C_ASSERT(_TYPE_ALIGNMENT(((type*)0)->field) == align);\n";
571     print OUT "#else\n";
572     print OUT "# define TEST_TARGET_ALIGN(type, align)\n";
573     print OUT "# define TEST_FIELD_ALIGN(type, field, align)\n";
574     print OUT "#endif\n";
575     print OUT "\n";
576     print OUT "#define TEST_FIELD_OFFSET(type, field, offset) C_ASSERT(FIELD_OFFSET(type, field) == offset);\n";
577     print OUT "\n";
578     print OUT "#define TEST_TARGET_SIZE(type, size)            TEST_TYPE_SIZE(*(type)0, size)\n";
579     print OUT "#define TEST_FIELD_SIZE(type, field, size)      TEST_TYPE_SIZE((((type*)0)->field), size)\n";
580     print OUT "#define TEST_TYPE_SIGNED(type)                  C_ASSERT((type) -1 < 0);\n";
581     print OUT "#define TEST_TYPE_UNSIGNED(type)                C_ASSERT((type) -1 > 0);\n";
582     print OUT "\n";
583     print OUT "\n";
584 }
585
586 ########################################################################
587 # output_footer
588
589 sub output_footer($$$) {
590     local *OUT = shift; 
591
592     my $test_dir = shift;
593     my @tests = @{(shift)};
594
595     print OUT "START_TEST(generated)\n";
596     print OUT "{\n";
597     foreach my $test (@tests) {
598         print OUT "    test_$test();\n";
599     }
600     print OUT "}\n";
601 }
602
603 ########################################################################
604 # output_test_pack_type
605
606 sub output_test_pack_type($$$$$$) {
607     local *OUT = shift;
608
609     my $type_name2type = shift;
610     my $type_name2optional = shift;
611     my $type_name2optional_fields = shift;
612     my $type_name = shift;
613     my $type = shift;
614
615     my $optional_fields = $$type_name2optional_fields{$type_name};
616
617     my $type_align = $type->align;
618     my $type_pack = $type->pack;
619     my $type_size = $type->size;
620     my $type_kind = $type->kind;
621
622     if (defined($type_pack)) {
623         print OUT "    /* $type_name (pack $type_pack) */\n";
624     } else {
625         print OUT "    /* $type_name */\n";
626     }
627
628     if (!scalar(keys(%$optional_fields)) && defined($type_align) && defined($type_size)) {
629         print OUT "    TEST_TYPE_SIZE   ($type_name, $type_size)\n";
630         print OUT "    TEST_TYPE_ALIGN  ($type_name, $type_align)\n";
631     }
632
633     if ($type_kind eq "float") {
634         # Nothing
635     } elsif ($type_kind eq "pointer") {
636         my $dereference_type;
637         $dereference_type = sub {
638             my $type = shift;
639
640             my @fields = $type->fields;
641             my $type_name2 =$fields[0]->type_name;
642
643             if ($type_name2 =~ s/\s*\*$//) {
644                 my $type2 = $$type_name2type{$type_name2};
645                 if (defined($type2)) {
646                     return $type2;
647                 } else {
648                     if ($type_name2 !~ /^(?:PVOID|VOID|void)$/) {
649                         $output->write("$type_name2: warning: type not found 1\n");
650                     }
651                     return undef;
652                 }
653             } elsif ($type_name2 =~ /^\w+$/) {
654                 my $type2 = $$type_name2type{$type_name2};
655                 if (defined($type2)) {
656                     return &$dereference_type($type2);
657                 } else {
658                     $output->write("$type_name2: warning: type not found\n");
659                     return undef;
660                 }
661             } elsif ($type_name2 =~ /^\w+\s*\((?:\s*CALLBACK|\s*NTAPI|\s*WINAPI)?\s*\*\s*\)\s*\(.*?\)$/) {
662                 return undef;
663             } else {
664                 $output->write("$type_name2: warning: type can't be parsed\n");
665                 return undef;
666             }
667         };
668
669         my $type2 = &$dereference_type($type);
670         if (defined($type2)) {
671             my $type_name2 = $type2->name;
672             my $type_align2 = $type2->align;
673             my $type_size2 = $type2->size;
674
675             my $optional = $$type_name2optional{$type_name};
676             my $optional_fields2 = $$type_name2optional_fields{$type_name2};
677
678             if (!$optional && !scalar(keys(%$optional_fields2)) && defined($type_align2) && defined($type_size2)) {
679                 print OUT "    TEST_TARGET_SIZE ($type_name, $type_size2)\n";
680                 print OUT "    TEST_TARGET_ALIGN($type_name, $type_align2)\n";
681             } else {
682                 # $output->write("$type_name: warning: type size not found\n");
683             }
684         }
685     } elsif ($type_kind eq "signed") {
686         print OUT "    TEST_TYPE_SIGNED($type_name)\n";
687     } elsif ($type_kind eq "unsigned") {
688         print OUT "    TEST_TYPE_UNSIGNED($type_name)\n";
689     }
690 }
691
692 sub output_test_pack_fields($$$$$$$);
693 sub output_test_pack_fields($$$$$$$) {
694     local *OUT = shift;
695
696     my $type_name2type = shift;
697     my $type_name2optional = shift;
698     my $type_name2optional_fields = shift;
699     my $type_name = shift;
700     my $type = shift;
701     my $offset = shift;
702
703     my $optional_fields = $$type_name2optional_fields{$type_name};
704
705     foreach my $field ($type->fields()) {
706         my $field_type_name = $field->type_name;
707         $field_type_name =~ s/\s+DECLSPEC_ALIGN\(\d+\)//;
708         my $field_name = $field->name;
709         my $field_size = $field->size;
710         my $field_offset = $field->offset;
711         my $field_align = $field->align;
712
713         next if $field_name eq "" || (defined($field_size) && $field_size < 0);
714         # We cannot take the address of a bitfield with MSVC
715         next if ($field_type_name =~ /:/);
716
717         if ($$optional_fields{$field_name}) {
718             # Nothing
719         } elsif (defined($field_size) && defined($field_offset)) {
720             $field_offset += $offset;
721             if ($field_name eq "DUMMYSTRUCTNAME") {
722                 print OUT "#ifdef NONAMELESSSTRUCT\n";
723                 print OUT "    TEST_TYPE_SIZE   ($type_name.$field_name, $field_size)\n";
724                 print OUT "    TEST_FIELD_ALIGN ($type_name, $field_name, $field_align)\n";
725                 print OUT "    TEST_FIELD_OFFSET($type_name, $field_name, $field_offset)\n";
726                 print OUT "#else\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";
730             } else {
731                 print OUT "    TEST_FIELD_SIZE  ($type_name, $field_name, $field_size)\n";
732                 print OUT "    TEST_FIELD_ALIGN ($type_name, $field_name, $field_align)\n";
733                 print OUT "    TEST_FIELD_OFFSET($type_name, $field_name, $field_offset)\n";
734             }
735         } else {
736             # $output->write("$type_name: $field_type_name: $field_name: test not generated (offset not defined)\n");
737         }
738     }
739 }
740
741 ########################################################################
742 # output_test_pack
743
744 sub output_test_pack($$$$) {
745     local *OUT = shift;
746
747     my $test_dir = shift;
748     my $test = shift;
749
750     my $type_names_used = shift;
751
752     $output->prefix("$test_dir: $test: ");
753
754     my @headers = $tests->get_section($test_dir, $test, "header");
755     my @type_names = $tests->get_section($test_dir, $test, "type");
756
757     my %type_name2optional;
758     my %type_name2optional_fields;
759
760     foreach my $_type_name (@type_names) {
761         my $type_name = $_type_name;
762
763         if ($type_name =~ s/^!//) {
764             $type_name2optional{$type_name}++;
765         }
766
767         my $optional_fields = {};
768         if ($type_name =~ s/:\s*(.*?)$//) {
769             my @fields = split /\s+/, $1;
770             foreach my $field (@fields) {
771                 if ($field =~ s/^!//) {
772                     $$optional_fields{$field}++;
773                 }
774             }
775         } 
776
777         $type_name2optional_fields{$type_name} = $optional_fields;
778     }
779
780     foreach my $header (@headers) {
781         my $type_name2type = $file2types{$pointer_size}{"include/$header"};
782         
783         foreach my $_type_name (@type_names) {
784             my $type_name = $_type_name;
785
786             my $skip = ($type_name =~ s/^!//);
787             $type_name =~ s/:.*?$//;
788             my $type = $$type_name2type{$type_name};
789             if (!defined($type)) {
790                 next;
791             }
792             $$type_names_used{$type_name} = $skip ? -1 : 1;
793             next if $skip;
794
795             print OUT "static void test_${test}_$type_name(void)\n";
796             print OUT "{\n";
797             output_test_pack_type(\*OUT, $type_name2type, \%type_name2optional, \%type_name2optional_fields,
798                                   $type_name, $type);
799             output_test_pack_fields(\*OUT, $type_name2type, \%type_name2optional, \%type_name2optional_fields, 
800                                     $type_name, $type, 0);
801             print OUT "}\n";
802             print OUT "\n";
803         }
804     }
805 }
806
807
808 ########################################################################
809 # output_file
810
811 sub output_file($$$$) {
812     local *OUT = shift;
813
814     my $test_dir = shift;
815     my @tests = @{(shift)};
816
817     my $type_names_used = shift;
818
819     output_header(\*OUT, $test_dir, \@tests);
820
821     foreach my $test (@tests) {
822         my %type_names_used2;
823
824         if ($test eq "pack") {
825             print OUT "#ifdef _WIN64\n\n";
826             $pointer_size = 8;
827             output_test_pack(\*OUT, $test_dir, $test, \%type_names_used2);
828             print OUT "#else /* _WIN64 */\n\n";
829             $pointer_size = 4;
830             output_test_pack(\*OUT, $test_dir, $test, \%type_names_used2);
831             print OUT "#endif /* _WIN64 */\n\n";
832         } else {
833             die "no such test ($test)\n";
834         }
835
836         print OUT "static void test_$test(void)\n";
837         print OUT "{\n";
838         foreach my $type_name (sort(keys(%type_names_used2))) {
839             $$type_names_used{$type_name} = $type_names_used2{$type_name};
840             if ($type_names_used2{$type_name} > 0) {
841                 print OUT "    test_${test}_$type_name();\n";
842             }
843         }
844         print OUT "}\n";
845         print OUT "\n";
846     }
847
848     output_footer(\*OUT, $test_dir, \@tests);
849
850     return 1;
851 }
852
853 ########################################################################
854 # main
855
856 my %type_names_used = ();
857
858 my @test_dirs = $tests->get_test_dirs();
859 foreach my $test_dir (@test_dirs) {
860     my $file = "$wine_dir/$test_dir/generated.c";
861     replace_file($file, \&output_file, $test_dir, \@tests, \%type_names_used);
862 }
863
864 foreach my $header (sort(keys(%{$file2types{$pointer_size}}))) {
865     $output->prefix("$header: ");
866     $header =~ s%^include/%%;
867     my $type_name2type = $file2types{$pointer_size}{"include/$header"};
868     foreach my $_type_name (sort(keys(%$type_name2type))) {
869         my $type_name = $_type_name;
870
871         if (!exists($type_names_used{$type_name})) {
872             $output->write("$type_name: type not used\n");
873         }
874     }
875 }
876
877 $output->prefix("$winapi_dir/tests.dat: ");
878 foreach my $type_name (sort(keys(%type_names_used))) {
879     my $found = 0;
880     foreach my $header (sort(keys(%{$file2types{$pointer_size}}))) {
881         my $type_name2type = $file2types{$pointer_size}{"include/$header"};
882         if (exists($type_name2type{$type_name})) {
883             $found = 1;
884         }
885     }
886
887     if (!$found) {
888         $output->write("$type_name: type not used\n");
889     }
890 }