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