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