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