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