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