Update tests.dat to include the various #include fixes.
[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 = 12;
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         #print "found_preprocessor: $begin_line: [$_]\n";
372         if ($preprocessor =~ /^\#\s*include\s+[\"<]pshpack(\d+)\.h[\">]$/) {
373             push @packs, $1;
374             #print "found pack $1 on line $begin_line\n";
375         } elsif($preprocessor =~ /^\#\s*include\s+[\"<]poppack\.h[\">]$/) {
376             pop @packs;
377             #print "found poppack on line $begin_line\n";
378         }
379
380         return 1;
381     };
382     $parser->set_found_preprocessor_callback($found_preprocessor);
383
384     my $found_type = sub {
385         $type = shift;
386
387         &$update_output();
388
389         my $name = $type->name;
390         $file2types{$file}{$name} = $type;
391
392         $type->set_find_align_callback(\&find_align);
393         $type->set_find_kind_callback(\&find_kind);
394         $type->set_find_size_callback(\&find_size);
395
396         my $pack = $packs[$#packs];
397         if (!defined($type->pack) && $type->kind =~ /^(?:struct|union)$/) {
398             $type->pack($pack);
399         }
400         my $size = $type->size();
401         if (defined($size)) {
402             my $max_field_base_size = 0;
403
404             foreach my $field ($type->fields()) {
405                 my $field_type_name = $field->type_name;
406                 my $field_name = $field->name;
407                 my $field_size = $field->size;
408                 my $field_base_size = $field->base_size;
409                 my $field_offset = $field->offset;
410                 my $field_align = $field->align;
411
412                 # $output->write("$name: $field_type_name: $field_name: $field_offset: $field_size($field_base_size): $field_align\n");
413             }
414             # $output->write("$name: $size\n");
415
416             $type_name2type{$name} = $type;
417         } else {
418             # $output->write("$name: can't find size\n");
419         }
420
421         return 1;
422     };
423     $parser->set_found_type_callback($found_type);
424
425     {
426         my $line = 1;
427         my $column = 0;
428         if(!$parser->parse_c_file(\$_, \$line, \$column)) {
429             $output->write("can't parse file\n");
430         }
431     }
432
433     $output->prefix("");
434 }
435
436 ########################################################################
437 # output_header
438
439 sub output_header {
440     local *OUT = shift; 
441
442     my $test_dir = shift;
443     my @tests = @{(shift)};
444
445     print OUT "/* File generated automatically from tools/winapi/test.dat; do not edit! */\n";
446     print OUT "/* This file can be copied, modified and distributed without restriction. */\n";
447     print OUT "\n";
448
449     print OUT "/*\n";
450     foreach my $test (@tests) {
451         my @description = $tests->get_section($test_dir, $test, "description");
452         foreach my $description (@description) {
453             print OUT " * $description\n";
454         }
455     }
456     print OUT " */\n";
457     print OUT "\n";
458
459     print OUT "#define WINVER 0x0501\n";
460     print OUT "#define _WIN32_IE 0x0501\n";
461     print OUT "#define _WIN32_WINNT 0x0501\n";
462     print OUT "\n";
463     print OUT "#define WINE_NOWINSOCK\n";
464     print OUT "\n";
465     foreach my $test (@tests) {
466         my @includes = $tests->get_section($test_dir, $test, "include");
467         foreach my $include (@includes) {
468             print OUT "#include \"$include\"\n";
469         }
470     }
471     print OUT "\n";
472     print OUT "#include \"wine/test.h\"\n";
473     print OUT "\n";
474
475     print OUT "/***********************************************************************\n";
476     print OUT " * Compability macros\n";
477     print OUT " */\n";
478     print OUT "\n";
479     print OUT "#define DWORD_PTR UINT_PTR\n";
480     print OUT "#define LONG_PTR INT_PTR\n";
481     print OUT "#define ULONG_PTR UINT_PTR\n";
482     print OUT "\n";
483
484     print OUT "/***********************************************************************\n";
485     print OUT " * Windows API extension\n";
486     print OUT " */\n";
487     print OUT "\n";
488     print OUT "#if defined(_MSC_VER) && (_MSC_VER >= 1300) && defined(__cplusplus)\n";
489     print OUT "# define FIELD_ALIGNMENT(type, field) __alignof(((type*)0)->field)\n";
490     print OUT "#elif defined(__GNUC__)\n";
491     print OUT "# define FIELD_ALIGNMENT(type, field) __alignof__(((type*)0)->field)\n";
492     print OUT "#else\n";
493     print OUT "/* FIXME: Not sure if is possible to do without compiler extension */\n";
494     print OUT "#endif\n";
495     print OUT "\n";
496     print OUT "#if defined(_MSC_VER) && (_MSC_VER >= 1300) && defined(__cplusplus)\n";
497     print OUT "# define _TYPE_ALIGNMENT(type) __alignof(type)\n";
498     print OUT "#elif defined(__GNUC__)\n";
499     print OUT "# define _TYPE_ALIGNMENT(type) __alignof__(type)\n";
500     print OUT "#else\n";
501     print OUT "/*\n";
502     print OUT " * FIXME: Not sure if is possible to do without compiler extension\n";
503     print OUT " *        (if type is not just a name that is, if so the normal)\n";
504     print OUT " *         TYPE_ALIGNMENT can be used)\n";
505     print OUT " */\n";
506     print OUT "#endif\n";
507     print OUT "\n";
508     print OUT "#if !defined(TYPE_ALIGNMENT) && defined(_TYPE_ALIGNMENT)\n";
509     print OUT "# define TYPE_ALIGNMENT _TYPE_ALIGNMENT\n";
510     print OUT "#endif\n";
511     print OUT "\n";
512
513     print OUT "/***********************************************************************\n";
514     print OUT " * Test helper macros\n";
515     print OUT " */\n";
516     print OUT "\n";
517     print OUT "#ifdef FIELD_ALIGNMENT\n";
518     print OUT "# define TEST_FIELD_ALIGNMENT(type, field, align) \\\n";
519     print OUT "   ok(FIELD_ALIGNMENT(type, field) == align, \\\n";
520     print OUT "       \"FIELD_ALIGNMENT(\" #type \", \" #field \") == %d (expected \" #align \")\\n\", \\\n";
521     print OUT "           FIELD_ALIGNMENT(type, field))\n";
522     print OUT "#else\n";
523     print OUT "# define TEST_FIELD_ALIGNMENT(type, field, align) do { } while (0)\n";
524     print OUT "#endif\n";
525     print OUT "\n";
526     print OUT "#define TEST_FIELD_OFFSET(type, field, offset) \\\n";
527     print OUT "    ok(FIELD_OFFSET(type, field) == offset, \\\n";
528     print OUT "        \"FIELD_OFFSET(\" #type \", \" #field \") == %ld (expected \" #offset \")\\n\", \\\n";
529     print OUT "             FIELD_OFFSET(type, field))\n";
530     print OUT "\n";
531     print OUT "#ifdef _TYPE_ALIGNMENT\n";
532     print OUT "#define TEST__TYPE_ALIGNMENT(type, align) \\\n";
533     print OUT "    ok(_TYPE_ALIGNMENT(type) == align, \"TYPE_ALIGNMENT(\" #type \") == %d (expected \" #align \")\", _TYPE_ALIGNMENT(type))\n";
534     print OUT "#else\n";
535     print OUT "# define TEST__TYPE_ALIGNMENT(type, align) do { } while (0)\n";
536     print OUT "#endif\n";
537     print OUT "\n";
538     print OUT "#ifdef TYPE_ALIGNMENT\n";
539     print OUT "#define TEST_TYPE_ALIGNMENT(type, align) \\\n";
540     print OUT "    ok(TYPE_ALIGNMENT(type) == align, \"TYPE_ALIGNMENT(\" #type \") == %d (expected \" #align \")\\n\", TYPE_ALIGNMENT(type))\n";
541     print OUT "#else\n";
542     print OUT "# define TEST_TYPE_ALIGNMENT(type, align) do { } while (0)\n";
543     print OUT "#endif\n";
544     print OUT "\n";
545     print OUT "#define TEST_TYPE_SIZE(type, size) \\\n";
546     print OUT "    ok(sizeof(type) == size, \"sizeof(\" #type \") == %d (expected \" #size \")\\n\", sizeof(type))\n";
547     print OUT "\n";
548     print OUT "/***********************************************************************\n";
549     print OUT " * Test macros\n";
550     print OUT " */\n";
551     print OUT "\n";
552     print OUT "#define TEST_FIELD(type, field_type, field_name, field_offset, field_size, field_align) \\\n";
553     print OUT "  TEST_TYPE_SIZE(field_type, field_size); \\\n";
554     print OUT "  TEST_FIELD_ALIGNMENT(type, field_name, field_align); \\\n";
555     print OUT "  TEST_FIELD_OFFSET(type, field_name, field_offset); \\\n";
556     print OUT "\n";
557     print OUT "#define TEST_TYPE(type, size, align) \\\n";
558     print OUT "  TEST_TYPE_ALIGNMENT(type, align); \\\n";
559     print OUT "  TEST_TYPE_SIZE(type, size)\n";
560     print OUT "\n";
561     print OUT "#define TEST_TYPE_POINTER(type, size, align) \\\n";
562     print OUT "    TEST__TYPE_ALIGNMENT(*(type)0, align); \\\n";
563     print OUT "    TEST_TYPE_SIZE(*(type)0, size)\n";
564     print OUT "\n";
565     print OUT "#define TEST_TYPE_SIGNED(type) \\\n";
566     print OUT "    ok((type) -1 < 0, \"(\" #type \") -1 < 0\\n\");\n";
567     print OUT "\n";
568     print OUT "#define TEST_TYPE_UNSIGNED(type) \\\n";
569     print OUT "     ok((type) -1 > 0, \"(\" #type \") -1 > 0\\n\");\n";
570     print OUT "\n";
571 }
572
573 ########################################################################
574 # output_footer
575
576 sub output_footer {
577     local *OUT = shift; 
578
579     my $test_dir = shift;
580     my @tests = @{(shift)};
581
582     print OUT "START_TEST(generated)\n";
583     print OUT "{\n";
584     foreach my $test (@tests) {
585         print OUT "    test_$test();\n";
586     }
587     print OUT "}\n";
588 }
589
590 ########################################################################
591 # output_test_pack_type
592
593 sub output_test_pack_type {
594     local *OUT = shift;
595
596     my $type_name2type = shift;
597     my $type_name2optional = shift;
598     my $type_name2optional_fields = shift;
599     my $type_name = shift;
600     my $type = shift;
601
602     my $optional_fields = $$type_name2optional_fields{$type_name};
603
604     my $type_align = $type->align;
605     my $type_pack = $type->pack;
606     my $type_size = $type->size;
607     my $type_kind = $type->kind;
608
609     if (defined($type_pack)) {
610         print OUT "    /* $type_name (pack $type_pack) */\n";
611     } else {
612         print OUT "    /* $type_name */\n";
613     }
614
615     if (!scalar(keys(%$optional_fields)) && defined($type_align) && defined($type_size)) {
616         print OUT "    TEST_TYPE($type_name, $type_size, $type_align);\n";
617     }
618
619     if ($type_kind eq "float") {
620         # Nothing
621     } elsif ($type_kind eq "pointer") {
622         my $dereference_type;
623         $dereference_type = sub {
624             my $type = shift;
625
626             my @fields = $type->fields;
627             my $type_name2 =$fields[0]->type_name;
628
629             if ($type_name2 =~ s/\s*\*$//) {
630                 my $type2 = $$type_name2type{$type_name2};
631                 if (defined($type2)) {
632                     return $type2;
633                 } else {
634                     if ($type_name2 !~ /^(?:PVOID|VOID|void)$/) {
635                         $output->write("$type_name2: warning: type not found 1\n");
636                     }
637                     return undef;
638                 }
639             } elsif ($type_name2 =~ /^\w+$/) {
640                 my $type2 = $$type_name2type{$type_name2};
641                 if (defined($type2)) {
642                     return &$dereference_type($type2);
643                 } else {
644                     $output->write("$type_name2: warning: type not found\n");
645                     return undef;
646                 }
647             } elsif ($type_name2 =~ /^\w+\s*\((?:\s*CALLBACK|\s*NTAPI|\s*WINAPI)?\s*\*\s*\)\s*\(.*?\)$/) {
648                 return undef;
649             } else {
650                 $output->write("$type_name2: warning: type can't be parsed\n");
651                 return undef;
652             }
653         };
654
655         my $type2 = &$dereference_type($type);
656         if (defined($type2)) {
657             my $type_name2 = $type2->name;
658             my $type_align2 = $type2->align;
659             my $type_size2 = $type2->size;
660
661             my $optional = $$type_name2optional{$type_name};
662             my $optional_fields2 = $$type_name2optional_fields{$type_name2};
663
664             if (!$optional && !scalar(keys(%$optional_fields2)) && defined($type_align2) && defined($type_size2)) {
665                 print OUT "    TEST_TYPE_POINTER($type_name, $type_size2, $type_align2);\n";
666             } else {
667                 # $output->write("$type_name: warning: type size not found\n");
668             }
669         }
670     } elsif ($type_kind eq "signed") {
671         print OUT "    TEST_TYPE_SIGNED($type_name);\n";
672     } elsif ($type_kind eq "unsigned") {
673         print OUT "    TEST_TYPE_UNSIGNED($type_name);\n";
674     }
675 }
676
677 sub output_test_pack_fields {
678     local *OUT = shift;
679
680     my $type_name2type = shift;
681     my $type_name2optional = shift;
682     my $type_name2optional_fields = shift;
683     my $type_name = shift;
684     my $type = shift;
685     my $offset = shift;
686
687     my $optional_fields = $$type_name2optional_fields{$type_name};
688
689     foreach my $field ($type->fields()) {
690         my $field_type_name = $field->type_name;
691         my $field_name = $field->name;
692         my $field_size = $field->size;
693         my $field_offset = $field->offset;
694         my $field_align = $field->align;
695
696         next if $field_name eq "" || (defined($field_size) && $field_size < 0);
697
698         if ($$optional_fields{$field_name}) {
699             # Nothing
700         } elsif (defined($field_size) && defined($field_offset)) {
701             $field_offset += $offset;
702             if ($field_name eq "DUMMYSTRUCTNAME") {
703                 print OUT "#ifdef NONAMELESSSTRUCT\n";
704                 print OUT "    TEST_FIELD($type_name, $field_type_name, $field_name, ";
705                 print OUT "$field_offset, $field_size, $field_align);\n";
706                 print OUT "#else\n";
707                 output_test_pack_fields(\*OUT, $type_name2type, $type_name2optional, $type_name2optional_fields,
708                                         $type_name, $$type_name2type{$field_type_name}, $field_offset); 
709                 print OUT "#endif\n";
710             } else {
711                 print OUT "    TEST_FIELD($type_name, $field_type_name, $field_name, ";
712                 print OUT "$field_offset, $field_size, $field_align);\n";
713             }
714         } else {
715             # $output->write("$type_name: $field_type_name: $field_name: test not generated (offset not defined)\n");
716         }
717     }
718 }
719
720 ########################################################################
721 # output_test_pack
722
723 sub output_test_pack {
724     local *OUT = shift;
725
726     my $test_dir = shift;
727     my $test = shift;
728
729     my $type_names_used = shift;
730
731     $output->prefix("$test_dir: $test: ");
732
733     my @headers = $tests->get_section($test_dir, $test, "header");
734     my @type_names = $tests->get_section($test_dir, $test, "type");
735
736     my %type_name2optional;
737     my %type_name2optional_fields;
738
739     foreach my $_type_name (@type_names) {
740         my $type_name = $_type_name;
741
742         if ($type_name =~ s/^!//) {
743             $type_name2optional{$type_name}++;
744         }
745
746         my $optional_fields = {};
747         if ($type_name =~ s/:\s*(.*?)$//) {
748             my @fields = split /\s+/, $1;
749             foreach my $field (@fields) {
750                 if ($field =~ s/^!//) {
751                     $$optional_fields{$field}++;
752                 }
753             }
754         } 
755
756         $type_name2optional_fields{$type_name} = $optional_fields;
757     }
758
759     foreach my $header (@headers) {
760         my $type_name2type = $file2types{"include/$header"};
761         
762         foreach my $_type_name (@type_names) {
763             my $type_name = $_type_name;
764
765             my $skip = ($type_name =~ s/^!//);
766             $type_name =~ s/:.*?$//;
767             my $type = $$type_name2type{$type_name};
768             if (!defined($type)) {
769                 next;
770             }
771             $$type_names_used{$type_name} = $skip ? -1 : 1;
772             next if $skip;
773
774             print OUT "static void test_${test}_$type_name(void)\n";
775             print OUT "{\n";
776             output_test_pack_type(\*OUT, $type_name2type, \%type_name2optional, \%type_name2optional_fields,
777                                   $type_name, $type);
778             output_test_pack_fields(\*OUT, $type_name2type, \%type_name2optional, \%type_name2optional_fields, 
779                                     $type_name, $type, 0);
780             print OUT "}\n";
781             print OUT "\n";
782         }
783     }
784 }
785
786
787 ########################################################################
788 # output_file
789
790 sub output_file {
791     local *OUT = shift;
792
793     my $test_dir = shift;
794     my @tests = @{(shift)};
795
796     my $type_names_used = shift;
797
798     output_header(\*OUT, $test_dir, \@tests);
799
800     foreach my $test (@tests) {
801         my %type_names_used2;
802
803         if ($test eq "pack") {
804             output_test_pack(\*OUT, $test_dir, $test, \%type_names_used2);
805         } else {
806             die "no such test ($test)\n";
807         }
808
809         print OUT "static void test_$test(void)\n";
810         print OUT "{\n";
811         foreach my $type_name (sort(keys(%type_names_used2))) {
812             $$type_names_used{$type_name} = $type_names_used2{$type_name};
813             if ($type_names_used2{$type_name} > 0) {
814                 print OUT "    test_${test}_$type_name();\n";
815             }
816         }
817         print OUT "}\n";
818         print OUT "\n";
819     }
820
821     output_footer(\*OUT, $test_dir, \@tests);
822
823     return 1;
824 }
825
826 ########################################################################
827 # main
828
829 my %type_names_used = ();
830
831 my @test_dirs = $tests->get_test_dirs();
832 foreach my $test_dir (@test_dirs) {
833     my $file = "$wine_dir/$test_dir/generated.c";
834     replace_file($file, \&output_file, $test_dir, \@tests, \%type_names_used);
835 }
836
837 foreach my $header (sort(keys(%file2types))) {
838     $output->prefix("$header: ");
839     $header =~ s%^include/%%;
840     my $type_name2type = $file2types{"include/$header"};
841     foreach my $_type_name (sort(keys(%$type_name2type))) {
842         my $type_name = $_type_name;
843
844         if (!exists($type_names_used{$type_name})) {
845             $output->write("$type_name: type not used\n");
846         }
847     }
848 }
849
850 $output->prefix("$winapi_dir/tests.dat: ");
851 foreach my $type_name (sort(keys(%type_names_used))) {
852     my $found = 0;
853     foreach my $header (sort(keys(%file2types))) {
854         my $type_name2type = $file2types{"include/$header"};
855         if (exists($type_name2type{$type_name})) {
856             $found = 1;
857         }
858     }
859
860     if (!$found) {
861         $output->write("$type_name: type not used\n");
862     }
863 }