winapi: Add some support for handling ifdefs, particularly ifdef _WIN64.
[wine/multimedia.git] / tools / winapi / winapi_test
blobcafbafb5423f268ee6677c42f04c6021f6fad91e
1 #!/usr/bin/perl -w
3 # Copyright 2002 Patrik Stridvall
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.
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.
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
20 use strict;
22 BEGIN {
23 $0 =~ m%^(.*?/?tools)/winapi/winapi_test$%;
24 require "$1/winapi/setup.pm";
27 use config qw(
28 file_type files_skip files_filter
29 $current_dir $wine_dir $winapi_dir
31 use output qw($output);
32 use winapi_test_options qw($options);
34 if($options->progress) {
35 $output->enable_progress;
36 } else {
37 $output->disable_progress;
40 use c_parser;
41 use tests qw($tests);
42 use type;
43 use util qw(replace_file);
45 my @tests = ();
46 if ($options->pack) {
47 push @tests, "pack";
50 my @files;
52 my %files;
54 foreach my $test (@tests)
56 foreach my $test_dir ($tests->get_test_dirs($test))
58 foreach my $header ($tests->get_section($test_dir, $test, "header"))
60 if (!$files{$header})
62 push @files, "include/$header";
63 $files{$header} = 1;
70 if (0) {
71 my $file = "tests.dat";
73 $file .= "2"; # FIXME: For tests
75 open(OUT, "> $winapi_dir/$file") || die "Error: Can't open $winapi_dir/$file: $!\n";
77 my $x = 0;
78 my @test_dirs = $tests->get_test_dirs();
79 foreach my $test_dir (@test_dirs) {
80 print OUT "\n" if $x++;
81 print OUT "%%%$test_dir\n";
82 print OUT "\n";
84 my $y = 0;
85 my @tests = $tests->get_tests($test_dir);
86 foreach my $test (@tests) {
87 print OUT "\n" if $y++;
88 print OUT "%%$test\n";
89 print OUT "\n";
91 my @types;
93 my $z = 0;
94 my @sections = $tests->get_sections($test_dir, $test);
95 foreach my $section (@sections) {
96 my @lines = $tests->get_section($test_dir, $test, $section);
98 if ($section =~ /^(?:struct|type)$/) {
99 foreach my $line (@lines) {
100 push @types, $line;
102 next;
105 print OUT "\n" if $z++;
106 print OUT "%$section\n";
107 print OUT "\n";
108 foreach my $line (@lines) {
109 print OUT "$line\n";
113 @types = sort { $x = $a; $y = $b; $x =~ s/^!//; $y =~ s/^!//; $x cmp $y } @types;
115 print OUT "\n" if $z++;
116 print OUT "%type\n";
117 print OUT "\n";
118 foreach my $type (@types) {
119 print OUT "$type\n";
124 close(OUT);
125 exit(0);
129 my %file2types;
131 my $progress_output;
132 my $progress_current = 0;
133 my $progress_max = scalar(@files);
135 ########################################################################
136 # find_type
138 my %type_name2type;
140 my %defines = (
141 "ANYSIZE_ARRAY" => 1,
142 "CCHDEVICENAME" => 32,
143 "CCHILDREN_TITLEBAR+1" => 6,
144 "ELF_VENDOR_SIZE" => 4,
145 "EXCEPTION_MAXIMUM_PARAMETERS" => 15,
146 "HW_PROFILE_GUIDLEN" => 39,
147 "IMAGE_NUMBEROF_DIRECTORY_ENTRIES" => 16,
148 "IMAGE_SIZEOF_SHORT_NAME" => 8,
149 "LF_FACESIZE" => 32,
150 "LF_FULLFACESIZE" => 64,
151 "MAXIMUM_SUPPORTED_EXTENSION" => 512,
152 "MAX_GOPHER_DISPLAY_TEXT + 1" => 129,
153 "MAX_GOPHER_LOCATOR_LENGTH + 1" => 654,
154 "MAX_PATH" => 260,
155 "MAX_PROFILE_LEN" => 80,
156 "NUM_POINTS" => 3,
157 "OFS_MAXPATHNAME" => 128,
158 "SIZE_OF_80387_REGISTERS" => 80,
159 "TOKEN_SOURCE_LENGTH" => 8,
162 my %align_kludge_reported = ("FILETIME" => 1, "LARGE_INTEGER" => 1);
163 my %size_kludge_reported = ("FILETIME" => 1, "LARGE_INTEGER" => 1);
164 my %size_parse_reported;
166 sub _find_align_kind_size($) {
167 my $type_name = shift;
169 local $_ = $type_name;
171 my $align;
172 my $kind;
173 my $size;
174 if (/\*+$/) {
175 $align = 4;
176 $kind = "pointer";
177 $size = 4;
178 } elsif(/^(?:(signed|unsigned)\s+)?(?:__int8|char|byte)$/) {
179 $align = 1;
180 $kind = defined($1) ? $1 : "signed";
181 $size = 1;
182 } elsif (/^(?:(signed|unsigned)\s+)?(?:__int16|short(?:\s+int)?)$/) {
183 $align = 2;
184 $kind = defined($1) ? $1 : "signed";
185 $size = 2;
186 } elsif (/^(?:wchar_t)$/) {
187 $align = 2;
188 $kind = "signed";
189 $size = 2;
190 } elsif (/^(signed|unsigned)$/) {
191 $align = 4;
192 $kind = defined($1) ? $1 : "signed";
193 $size = 4;
194 } elsif (/^(?:(signed|unsigned)\s+)?(?:__int32|int|long(?:\s+int)?)$/) {
195 $align = 4;
196 $kind = defined($1) ? $1 : "signed";
197 $size = 4;
198 } elsif (/^(?:float)$/) {
199 $align = 4;
200 $kind = "float";
201 $size = 4;
202 } elsif (/^(?:(signed|unsigned)\s+)?__int64$/) {
203 $align = 8;
204 $kind = defined($1) ? $1 : "signed";
205 $size = 8;
206 } elsif (/^(?:double|DOUBLE|DATE)$/) {
207 $align = 8;
208 $kind = "float";
209 $size = 8;
210 } elsif (/^(?:long\s+double)$/) {
211 $align = 4;
212 $kind = "float";
213 $size = 12;
214 } elsif (/^H(?:DC|BITMAP|BRUSH|ICON|INSTANCE|KEY|MENU|METAFILE|WND)$/) {
215 $align = 4;
216 $kind = "pointer";
217 $size = 4;
218 } elsif (/^LP(?:BYTE|CSTR|CWSTR|DWORD|STR|VOID|WSTR)$/) {
219 $align = 4;
220 $kind = "pointer";
221 $size = 4;
222 } elsif (/^(?:FILETIME)$/) {
223 $align = 4;
224 $kind = "struct";
225 $size = 8;
226 } elsif (/^GUID$/) {
227 $align = 4;
228 $kind = "struct";
229 $size = 16;
230 } elsif (/^(?:VOID)$/) {
231 $align = 4;
232 $kind = "signed";
233 $size = 4;
234 } elsif (/^(?:SHORT)$/) {
235 $align = 2;
236 $kind = "unsigned";
237 $size = 2;
238 } elsif (/^(?:BYTE)$/) {
239 $align = 1;
240 $kind = "unsigned";
241 $size = 1;
242 } elsif (/^(?:DWORD)$/) {
243 $align = 4;
244 $kind = "unsigned";
245 $size = 4;
246 } elsif (/^(?:WORD)$/) {
247 $align = 2;
248 $kind = "unsigned";
249 $size = 2;
250 } elsif (/^(?:INT64|LONG64|LONGLONG)$/) {
251 $align = 8;
252 $kind = "signed";
253 $size = 8;
254 } elsif (/^(?:UINT64|ULONG64|DWORD64|ULONGLONG|DWORDLONG)$/) {
255 $align = 8;
256 $kind = "unsigned";
257 $size = 8;
258 } elsif (/^(?:LARGE_INTEGER)$/) {
259 $align = 8;
260 $kind = "union";
261 $size = 8;
262 } elsif (/^(?:POINTS)$/) {
263 $align = 2;
264 $kind = "struct";
265 $size = 4;
266 } elsif (/^(struct|union)$/) {
267 $kind = $1;
268 if (!$size_parse_reported{$_}) {
269 $output->write("$type_name: can't parse type\n");
270 $size_parse_reported{$_} = 1;
272 } elsif (/^\w+\s*\((?:\s*CALLBACK|\s*NTAPI|\s*WINAPI)?\s*\*\s*\)\s*\(.*?\)$/) {
273 $align = 4;
274 $kind = "pointer";
275 $size = 4;
278 my $align2;
279 if (defined(my $type = $type_name2type{$_})) {
280 $align2 = $type->align;
283 if (!defined($align)) {
284 $align = $align2;
285 } elsif (defined($align2) && !$align_kludge_reported{$_}) {
286 $align_kludge_reported{$_} = 1;
287 $output->write("$type_name: type needn't be kludged\n");
290 if (!defined($align)) {
291 # $output->write("$type_name: can't find type\n");
294 my $size2;
295 if (defined(my $type = $type_name2type{$_})) {
296 $size2 = $type->size;
299 if (!defined($size)) {
300 $size = $size2;
301 } elsif (defined($size2) && !$size_kludge_reported{$_}) {
302 $size_kludge_reported{$_} = 1;
303 $output->write("$type_name: type needn't be kludged\n");
306 return ($align, $kind, $size);
309 sub find_align($) {
310 my $type_name = shift;
311 (my $align, my $kind, my $size) = _find_align_kind_size($type_name);
312 return $align;
315 sub find_kind($) {
316 my $type_name = shift;
317 (my $align, my $kind, my $size) = _find_align_kind_size($type_name);
319 return $kind;
322 sub find_size($) {
323 my $type_name = shift;
324 (my $align, my $kind, my $size) = _find_align_kind_size($type_name);
325 return $size;
328 sub find_count($) {
329 my $count = shift;
330 return $defines{$count};
333 foreach my $file (@files) {
334 $progress_current++;
337 open(IN, "< $wine_dir/$file") || die "Error: Can't open $wine_dir/$file: $!\n";
338 local $/ = undef;
339 $_ = <IN>;
340 close(IN);
343 my $max_line = 0;
345 local $_ = $_;
346 while(s/^.*?\n//) { $max_line++; }
347 if($_) { $max_line++; }
350 my $parser = new c_parser($file);
352 my $line;
353 my $type;
354 my @packs = (4);
355 my @ifdefs = ();
357 my $update_output = sub {
358 my $progress = "";
359 my $prefix = "";
361 $progress .= "$file (file $progress_current of $progress_max)";
362 $prefix .= "$file: ";
364 if(defined($line)) {
365 $progress .= ": line $line of $max_line";
368 $output->progress($progress);
369 $output->prefix($prefix);
372 &$update_output();
374 my $found_line = sub {
375 $line = shift;
377 &$update_output;
379 $parser->set_found_line_callback($found_line);
381 my $found_preprocessor = sub {
382 my $begin_line = shift;
383 my $begin_column = shift;
384 my $preprocessor = shift;
386 #print "found_preprocessor: $begin_line: [$_]\n";
387 if ($preprocessor =~ /^\#\s*include\s+[\"<]pshpack(\d+)\.h[\">]$/) {
388 push @packs, $1 unless @ifdefs && !$ifdefs[$#ifdefs];
389 #print "found pack $1 on line $begin_line\n";
390 } elsif($preprocessor =~ /^\#\s*include\s+[\"<]poppack\.h[\">]$/) {
391 pop @packs unless @ifdefs && !$ifdefs[$#ifdefs];
392 #print "found poppack on line $begin_line\n";
393 } elsif ($preprocessor =~ /^\#\s*ifdef\s+_WIN64/) {
394 push @ifdefs, 0;
395 } elsif ($preprocessor =~ /^\#\s*ifndef\s+_WIN64/) {
396 push @ifdefs, 1;
397 } elsif ($preprocessor =~ /^\#\s*elif\s+defined(_WIN64)/) {
398 $ifdefs[$#ifdefs] = 0;
399 } elsif ($preprocessor =~ /^\#\s*ifdef\s/) {
400 push @ifdefs, 2;
401 } elsif ($preprocessor =~ /^\#\s*ifndef\s/) {
402 push @ifdefs, 2;
403 } elsif ($preprocessor =~ /^\#\s*if/) {
404 push @ifdefs, 2;
405 } elsif ($preprocessor =~ /^\#\s*else/) {
406 $ifdefs[$#ifdefs] = $ifdefs[$#ifdefs] ^ 1;
407 } elsif ($preprocessor =~ /^\#\s*elif/) {
408 $ifdefs[$#ifdefs] = 2;
409 } elsif ($preprocessor =~ /^\#\s*endif/) {
410 pop @ifdefs;
412 return 1;
414 $parser->set_found_preprocessor_callback($found_preprocessor);
416 my $found_type = sub {
417 $type = shift;
418 return if @ifdefs && !$ifdefs[$#ifdefs];
420 &$update_output();
422 my $name = $type->name;
423 $file2types{$file}{$name} = $type;
425 $type->set_find_align_callback(\&find_align);
426 $type->set_find_kind_callback(\&find_kind);
427 $type->set_find_size_callback(\&find_size);
428 $type->set_find_count_callback(\&find_count);
430 my $pack = $packs[$#packs];
431 if (!defined($type->pack) && $type->kind =~ /^(?:struct|union)$/) {
432 $type->pack($pack);
434 my $size = $type->size();
435 if (defined($size)) {
436 my $max_field_base_size = 0;
438 foreach my $field ($type->fields()) {
439 my $field_type_name = $field->type_name;
440 my $field_name = $field->name;
441 my $field_size = $field->size;
442 my $field_base_size = $field->base_size;
443 my $field_offset = $field->offset;
444 my $field_align = $field->align;
446 # $output->write("$name: $field_type_name: $field_name: $field_offset: $field_size($field_base_size): $field_align\n");
448 # $output->write("$name: $size\n");
450 $type_name2type{$name} = $type;
451 } else {
452 # $output->write("$name: can't find size\n");
455 return 1;
457 $parser->set_found_type_callback($found_type);
460 my $line = 1;
461 my $column = 0;
462 if(!$parser->parse_c_file(\$_, \$line, \$column)) {
463 $output->write("can't parse file\n");
467 $output->prefix("");
470 ########################################################################
471 # output_header
473 sub output_header($$$) {
474 local *OUT = shift;
476 my $test_dir = shift;
477 my @tests = @{(shift)};
479 print OUT "/* File generated automatically from tools/winapi/tests.dat; do not edit! */\n";
480 print OUT "/* This file can be copied, modified and distributed without restriction. */\n";
481 print OUT "\n";
483 print OUT "/*\n";
484 foreach my $test (@tests) {
485 my @description = $tests->get_section($test_dir, $test, "description");
486 foreach my $description (@description) {
487 print OUT " * $description\n";
490 print OUT " */\n";
491 print OUT "\n";
493 print OUT "#define WINVER 0x0501\n";
494 print OUT "#define _WIN32_IE 0x0501\n";
495 print OUT "#define _WIN32_WINNT 0x0501\n";
496 print OUT "\n";
497 print OUT "#define WINE_NOWINSOCK\n";
498 print OUT "\n";
499 foreach my $test (@tests) {
500 my @includes = $tests->get_section($test_dir, $test, "include");
501 foreach my $include (@includes) {
502 print OUT "#include $include\n";
505 print OUT "\n";
506 print OUT "#include \"wine/test.h\"\n";
507 print OUT "\n";
509 print OUT "/***********************************************************************\n";
510 print OUT " * Compatibility macros\n";
511 print OUT " */\n";
512 print OUT "\n";
513 print OUT "#define DWORD_PTR UINT_PTR\n";
514 print OUT "#define LONG_PTR INT_PTR\n";
515 print OUT "#define ULONG_PTR UINT_PTR\n";
516 print OUT "\n";
518 print OUT "/***********************************************************************\n";
519 print OUT " * Windows API extension\n";
520 print OUT " */\n";
521 print OUT "\n";
522 print OUT "#if defined(_MSC_VER) && (_MSC_VER >= 1300) && defined(__cplusplus)\n";
523 print OUT "# define _TYPE_ALIGNMENT(type) __alignof(type)\n";
524 print OUT "#elif defined(__GNUC__)\n";
525 print OUT "# define _TYPE_ALIGNMENT(type) __alignof__(type)\n";
526 print OUT "#else\n";
527 print OUT "/*\n";
528 print OUT " * FIXME: May not be possible without a compiler extension\n";
529 print OUT " * (if type is not just a name that is, otherwise the normal\n";
530 print OUT " * TYPE_ALIGNMENT can be used)\n";
531 print OUT " */\n";
532 print OUT "#endif\n";
533 print OUT "\n";
534 print OUT "#if defined(TYPE_ALIGNMENT) && defined(_MSC_VER) && _MSC_VER >= 800 && !defined(__cplusplus)\n";
535 print OUT "#pragma warning(disable:4116)\n";
536 print OUT "#endif\n";
537 print OUT "\n";
538 print OUT "#if !defined(TYPE_ALIGNMENT) && defined(_TYPE_ALIGNMENT)\n";
539 print OUT "# define TYPE_ALIGNMENT _TYPE_ALIGNMENT\n";
540 print OUT "#endif\n";
541 print OUT "\n";
543 print OUT "/***********************************************************************\n";
544 print OUT " * Test helper macros\n";
545 print OUT " */\n";
546 print OUT "\n";
547 print OUT "#ifdef _WIN64\n";
548 print OUT "\n";
549 print OUT "# define TEST_TYPE_SIZE(type, size)\n";
550 print OUT "# define TEST_TYPE_ALIGN(type, align)\n";
551 print OUT "# define TEST_TARGET_ALIGN(type, align)\n";
552 print OUT "# define TEST_FIELD_ALIGN(type, field, align)\n";
553 print OUT "# define TEST_FIELD_OFFSET(type, field, offset)\n";
554 print OUT "\n";
555 print OUT "#else\n";
556 print OUT "\n";
557 print OUT "# define TEST_TYPE_SIZE(type, size) C_ASSERT(sizeof(type) == size);\n";
558 print OUT "\n";
559 print OUT "# ifdef TYPE_ALIGNMENT\n";
560 print OUT "# define TEST_TYPE_ALIGN(type, align) C_ASSERT(TYPE_ALIGNMENT(type) == align);\n";
561 print OUT "# else\n";
562 print OUT "# define TEST_TYPE_ALIGN(type, align)\n";
563 print OUT "# endif\n";
564 print OUT "\n";
565 print OUT "# ifdef _TYPE_ALIGNMENT\n";
566 print OUT "# define TEST_TARGET_ALIGN(type, align) C_ASSERT(_TYPE_ALIGNMENT(*(type)0) == align);\n";
567 print OUT "# define TEST_FIELD_ALIGN(type, field, align) C_ASSERT(_TYPE_ALIGNMENT(((type*)0)->field) == align);\n";
568 print OUT "# else\n";
569 print OUT "# define TEST_TARGET_ALIGN(type, align)\n";
570 print OUT "# define TEST_FIELD_ALIGN(type, field, align)\n";
571 print OUT "# endif\n";
572 print OUT "\n";
573 print OUT "# define TEST_FIELD_OFFSET(type, field, offset) C_ASSERT(FIELD_OFFSET(type, field) == offset);\n";
574 print OUT "\n";
575 print OUT "#endif\n";
576 print OUT "\n";
577 print OUT "#define TEST_TARGET_SIZE(type, size) TEST_TYPE_SIZE(*(type)0, size)\n";
578 print OUT "#define TEST_FIELD_SIZE(type, field, size) TEST_TYPE_SIZE((((type*)0)->field), size)\n";
579 print OUT "#define TEST_TYPE_SIGNED(type) C_ASSERT((type) -1 < 0);\n";
580 print OUT "#define TEST_TYPE_UNSIGNED(type) C_ASSERT((type) -1 > 0);\n";
581 print OUT "\n";
582 print OUT "\n";
585 ########################################################################
586 # output_footer
588 sub output_footer($$$) {
589 local *OUT = shift;
591 my $test_dir = shift;
592 my @tests = @{(shift)};
594 print OUT "START_TEST(generated)\n";
595 print OUT "{\n";
596 foreach my $test (@tests) {
597 print OUT "#ifdef _WIN64\n";
598 print OUT " ok(0, \"The type size / alignment tests don't support Win64 yet\\n\");\n";
599 print OUT "#else\n";
600 print OUT " test_$test();\n";
601 print OUT "#endif\n";
603 print OUT "}\n";
606 ########################################################################
607 # output_test_pack_type
609 sub output_test_pack_type($$$$$$) {
610 local *OUT = shift;
612 my $type_name2type = shift;
613 my $type_name2optional = shift;
614 my $type_name2optional_fields = shift;
615 my $type_name = shift;
616 my $type = shift;
618 my $optional_fields = $$type_name2optional_fields{$type_name};
620 my $type_align = $type->align;
621 my $type_pack = $type->pack;
622 my $type_size = $type->size;
623 my $type_kind = $type->kind;
625 if (defined($type_pack)) {
626 print OUT " /* $type_name (pack $type_pack) */\n";
627 } else {
628 print OUT " /* $type_name */\n";
631 if (!scalar(keys(%$optional_fields)) && defined($type_align) && defined($type_size)) {
632 print OUT " TEST_TYPE_SIZE ($type_name, $type_size)\n";
633 print OUT " TEST_TYPE_ALIGN ($type_name, $type_align)\n";
636 if ($type_kind eq "float") {
637 # Nothing
638 } elsif ($type_kind eq "pointer") {
639 my $dereference_type;
640 $dereference_type = sub {
641 my $type = shift;
643 my @fields = $type->fields;
644 my $type_name2 =$fields[0]->type_name;
646 if ($type_name2 =~ s/\s*\*$//) {
647 my $type2 = $$type_name2type{$type_name2};
648 if (defined($type2)) {
649 return $type2;
650 } else {
651 if ($type_name2 !~ /^(?:PVOID|VOID|void)$/) {
652 $output->write("$type_name2: warning: type not found 1\n");
654 return undef;
656 } elsif ($type_name2 =~ /^\w+$/) {
657 my $type2 = $$type_name2type{$type_name2};
658 if (defined($type2)) {
659 return &$dereference_type($type2);
660 } else {
661 $output->write("$type_name2: warning: type not found\n");
662 return undef;
664 } elsif ($type_name2 =~ /^\w+\s*\((?:\s*CALLBACK|\s*NTAPI|\s*WINAPI)?\s*\*\s*\)\s*\(.*?\)$/) {
665 return undef;
666 } else {
667 $output->write("$type_name2: warning: type can't be parsed\n");
668 return undef;
672 my $type2 = &$dereference_type($type);
673 if (defined($type2)) {
674 my $type_name2 = $type2->name;
675 my $type_align2 = $type2->align;
676 my $type_size2 = $type2->size;
678 my $optional = $$type_name2optional{$type_name};
679 my $optional_fields2 = $$type_name2optional_fields{$type_name2};
681 if (!$optional && !scalar(keys(%$optional_fields2)) && defined($type_align2) && defined($type_size2)) {
682 print OUT " TEST_TARGET_SIZE ($type_name, $type_size2)\n";
683 print OUT " TEST_TARGET_ALIGN($type_name, $type_align2)\n";
684 } else {
685 # $output->write("$type_name: warning: type size not found\n");
688 } elsif ($type_kind eq "signed") {
689 print OUT " TEST_TYPE_SIGNED($type_name)\n";
690 } elsif ($type_kind eq "unsigned") {
691 print OUT " TEST_TYPE_UNSIGNED($type_name)\n";
695 sub output_test_pack_fields($$$$$$$);
696 sub output_test_pack_fields($$$$$$$) {
697 local *OUT = shift;
699 my $type_name2type = shift;
700 my $type_name2optional = shift;
701 my $type_name2optional_fields = shift;
702 my $type_name = shift;
703 my $type = shift;
704 my $offset = shift;
706 my $optional_fields = $$type_name2optional_fields{$type_name};
708 foreach my $field ($type->fields()) {
709 my $field_type_name = $field->type_name;
710 $field_type_name =~ s/\s+DECLSPEC_ALIGN\(\d+\)//;
711 my $field_name = $field->name;
712 my $field_size = $field->size;
713 my $field_offset = $field->offset;
714 my $field_align = $field->align;
716 next if $field_name eq "" || (defined($field_size) && $field_size < 0);
717 # We cannot take the address of a bitfield with MSVC
718 next if ($field_type_name =~ /:/);
720 if ($$optional_fields{$field_name}) {
721 # Nothing
722 } elsif (defined($field_size) && defined($field_offset)) {
723 $field_offset += $offset;
724 if ($field_name eq "DUMMYSTRUCTNAME") {
725 print OUT "#ifdef NONAMELESSSTRUCT\n";
726 print OUT " TEST_TYPE_SIZE ($type_name.$field_name, $field_size)\n";
727 print OUT " TEST_FIELD_ALIGN ($type_name, $field_name, $field_align)\n";
728 print OUT " TEST_FIELD_OFFSET($type_name, $field_name, $field_offset)\n";
729 print OUT "#else\n";
730 output_test_pack_fields(\*OUT, $type_name2type, $type_name2optional, $type_name2optional_fields,
731 $type_name, $$type_name2type{$field_type_name}, $field_offset);
732 print OUT "#endif\n";
733 } else {
734 print OUT " TEST_FIELD_SIZE ($type_name, $field_name, $field_size)\n";
735 print OUT " TEST_FIELD_ALIGN ($type_name, $field_name, $field_align)\n";
736 print OUT " TEST_FIELD_OFFSET($type_name, $field_name, $field_offset)\n";
738 } else {
739 # $output->write("$type_name: $field_type_name: $field_name: test not generated (offset not defined)\n");
744 ########################################################################
745 # output_test_pack
747 sub output_test_pack($$$$) {
748 local *OUT = shift;
750 my $test_dir = shift;
751 my $test = shift;
753 my $type_names_used = shift;
755 $output->prefix("$test_dir: $test: ");
757 my @headers = $tests->get_section($test_dir, $test, "header");
758 my @type_names = $tests->get_section($test_dir, $test, "type");
760 my %type_name2optional;
761 my %type_name2optional_fields;
763 foreach my $_type_name (@type_names) {
764 my $type_name = $_type_name;
766 if ($type_name =~ s/^!//) {
767 $type_name2optional{$type_name}++;
770 my $optional_fields = {};
771 if ($type_name =~ s/:\s*(.*?)$//) {
772 my @fields = split /\s+/, $1;
773 foreach my $field (@fields) {
774 if ($field =~ s/^!//) {
775 $$optional_fields{$field}++;
780 $type_name2optional_fields{$type_name} = $optional_fields;
783 foreach my $header (@headers) {
784 my $type_name2type = $file2types{"include/$header"};
786 foreach my $_type_name (@type_names) {
787 my $type_name = $_type_name;
789 my $skip = ($type_name =~ s/^!//);
790 $type_name =~ s/:.*?$//;
791 my $type = $$type_name2type{$type_name};
792 if (!defined($type)) {
793 next;
795 $$type_names_used{$type_name} = $skip ? -1 : 1;
796 next if $skip;
798 print OUT "static void test_${test}_$type_name(void)\n";
799 print OUT "{\n";
800 output_test_pack_type(\*OUT, $type_name2type, \%type_name2optional, \%type_name2optional_fields,
801 $type_name, $type);
802 output_test_pack_fields(\*OUT, $type_name2type, \%type_name2optional, \%type_name2optional_fields,
803 $type_name, $type, 0);
804 print OUT "}\n";
805 print OUT "\n";
811 ########################################################################
812 # output_file
814 sub output_file($$$$) {
815 local *OUT = shift;
817 my $test_dir = shift;
818 my @tests = @{(shift)};
820 my $type_names_used = shift;
822 output_header(\*OUT, $test_dir, \@tests);
824 foreach my $test (@tests) {
825 my %type_names_used2;
827 if ($test eq "pack") {
828 output_test_pack(\*OUT, $test_dir, $test, \%type_names_used2);
829 } else {
830 die "no such test ($test)\n";
833 print OUT "static void test_$test(void)\n";
834 print OUT "{\n";
835 foreach my $type_name (sort(keys(%type_names_used2))) {
836 $$type_names_used{$type_name} = $type_names_used2{$type_name};
837 if ($type_names_used2{$type_name} > 0) {
838 print OUT " test_${test}_$type_name();\n";
841 print OUT "}\n";
842 print OUT "\n";
845 output_footer(\*OUT, $test_dir, \@tests);
847 return 1;
850 ########################################################################
851 # main
853 my %type_names_used = ();
855 my @test_dirs = $tests->get_test_dirs();
856 foreach my $test_dir (@test_dirs) {
857 my $file = "$wine_dir/$test_dir/generated.c";
858 replace_file($file, \&output_file, $test_dir, \@tests, \%type_names_used);
861 foreach my $header (sort(keys(%file2types))) {
862 $output->prefix("$header: ");
863 $header =~ s%^include/%%;
864 my $type_name2type = $file2types{"include/$header"};
865 foreach my $_type_name (sort(keys(%$type_name2type))) {
866 my $type_name = $_type_name;
868 if (!exists($type_names_used{$type_name})) {
869 $output->write("$type_name: type not used\n");
874 $output->prefix("$winapi_dir/tests.dat: ");
875 foreach my $type_name (sort(keys(%type_names_used))) {
876 my $found = 0;
877 foreach my $header (sort(keys(%file2types))) {
878 my $type_name2type = $file2types{"include/$header"};
879 if (exists($type_name2type{$type_name})) {
880 $found = 1;
884 if (!$found) {
885 $output->write("$type_name: type not used\n");