Several bug fixes and additions.
[wine/testsucceed.git] / tools / winapi_check / winapi_local.pm
blobc4f79727ad7a1605a936c446dc402a18266c8e5e
1 package winapi_local;
3 use strict;
5 sub check_function {
6 my $options = shift;
7 my $output = shift;
8 my $return_type = shift;
9 my $calling_convention = shift;
10 my $external_name = shift;
11 my $internal_name = shift;
12 my $refargument_types = shift;
13 my @argument_types = @$refargument_types;
14 my $nativeapi = shift;
15 my $winapi = shift;
17 my $module = $winapi->function_internal_module($internal_name);
19 if($winapi->name eq "win16") {
20 if($winapi->function_stub($internal_name)) {
21 if($options->implemented) {
22 $output->write("function implemented but declared as stub in .spec file\n");
24 return;
25 } elsif($winapi->function_stub($internal_name)) {
26 if($options->implemented_win32) {
27 $output->write("32-bit variant of function implemented but declared as stub in .spec file\n");
29 return;
31 } elsif($winapi->function_stub($internal_name)) {
32 if($options->implemented) {
33 $output->write("function implemented but declared as stub in .spec file\n");
35 return;
38 my $forbidden_return_type = 0;
39 my $implemented_return_kind;
40 $winapi->type_used_in_module($return_type,$module);
41 if(!defined($implemented_return_kind = $winapi->translate_argument($return_type))) {
42 if($return_type ne "") {
43 $output->write("no translation defined: " . $return_type . "\n");
45 } elsif(!$winapi->is_allowed_kind($implemented_return_kind) || !$winapi->allowed_type_in_module($return_type,$module)) {
46 $forbidden_return_type = 1;
47 if($options->report_argument_forbidden($return_type)) {
48 $output->write("return type is forbidden: $return_type ($implemented_return_kind)\n");
52 my $segmented = 0;
53 if(defined($implemented_return_kind) && $implemented_return_kind =~ /^segptr|segstr$/) {
54 $segmented = 1;
57 my $implemented_calling_convention;
58 if($winapi->name eq "win16") {
59 if($calling_convention =~ /^__cdecl$/) {
60 $implemented_calling_convention = "cdecl";
61 } elsif($calling_convention =~ /^VFWAPIV|WINAPIV$/) {
62 $implemented_calling_convention = "varargs";
63 } elsif($calling_convention =~ /^__stdcall|VFWAPI|WINAPI|CALLBACK$/) {
64 if($implemented_return_kind =~ /^s_word|word|void$/) {
65 $implemented_calling_convention = "pascal16";
66 } else {
67 $implemented_calling_convention = "pascal";
69 } elsif($calling_convention =~ /^__asm$/) {
70 $implemented_calling_convention = "asm";
71 } else {
72 $implemented_calling_convention = "cdecl";
74 } elsif($winapi->name eq "win32") {
75 if($calling_convention =~ /^__cdecl$/) {
76 $implemented_calling_convention = "cdecl";
77 } elsif($calling_convention =~ /^VFWAPIV|WINAPIV$/) {
78 $implemented_calling_convention = "varargs";
79 } elsif($calling_convention =~ /^__stdcall|VFWAPI|WINAPI|CALLBACK$/) {
80 if(defined($implemented_return_kind) && $implemented_return_kind =~ /^longlong$/) {
81 $implemented_calling_convention = "stdcall"; # FIXME: Check entry flags
82 } else {
83 $implemented_calling_convention = "stdcall";
85 } elsif($calling_convention =~ /^__asm$/) {
86 $implemented_calling_convention = "asm";
87 } else {
88 $implemented_calling_convention = "cdecl";
92 my $declared_calling_convention = $winapi->function_internal_calling_convention($internal_name);
93 my @declared_argument_kinds = split(/\s+/, $winapi->function_internal_arguments($internal_name));
95 if($declared_calling_convention =~ /^register|interrupt$/) {
96 push @declared_argument_kinds, "ptr";
99 if($declared_calling_convention =~ /^register|interupt$/ &&
100 (($winapi->name eq "win32" && $implemented_calling_convention eq "stdcall") ||
101 (($winapi->name eq "win16" && $implemented_calling_convention =~ /^pascal/))))
103 # correct
104 } elsif($implemented_calling_convention ne $declared_calling_convention &&
105 $implemented_calling_convention ne "asm" &&
106 !($declared_calling_convention =~ /^pascal/ && $forbidden_return_type) &&
107 !($implemented_calling_convention =~ /^cdecl|varargs$/ && $declared_calling_convention =~ /^cdecl|varargs$/))
109 if($options->calling_convention && (
110 ($options->calling_convention_win16 && $winapi->name eq "win16") ||
111 ($options->calling_convention_win32 && $winapi->name eq "win32")) &&
112 !$nativeapi->is_function($internal_name))
114 $output->write("calling convention mismatch: $implemented_calling_convention != $declared_calling_convention\n");
118 if($declared_calling_convention eq "varargs") {
119 if($#argument_types != -1 && $argument_types[$#argument_types] eq "...") {
120 pop @argument_types;
121 } else {
122 $output->write("function not implemented as vararg\n");
124 } elsif($#argument_types != -1 && $argument_types[$#argument_types] eq "...") {
125 if($#argument_types == 0 || $winapi->name eq "win16") {
126 pop @argument_types;
127 } else {
128 $output->write("function not declared as vararg\n");
132 if($#argument_types != -1 && $argument_types[$#argument_types] eq "CONTEXT *" &&
133 $internal_name !~ /^(Get|Set)ThreadContext$/) # FIXME: Kludge
135 $#argument_types--;
138 if($internal_name =~ /^NTDLL__ftol|NTDLL__CIpow$/) { # FIXME: Kludge
139 # ignore
140 } else {
141 my $n = 0;
142 my @argument_kinds = map {
143 my $type = $_;
144 my $kind = "unknown";
145 $winapi->type_used_in_module($type,$module);
146 if(!defined($kind = $winapi->translate_argument($type))) {
147 $output->write("no translation defined: " . $type . "\n");
148 } elsif(!$winapi->is_allowed_kind($kind) ||
149 !$winapi->allowed_type_in_module($type, $module)) {
150 if($options->report_argument_forbidden($type)) {
151 $output->write("forbidden argument " . ($n + 1) . " type " . $type . " (" . $kind . ")\n");
155 # FIXME: Kludge
156 if(defined($kind) && $kind eq "longlong") {
157 $n+=2;
158 ("long", "long");
159 } else {
160 $n++;
161 $kind;
163 } @argument_types;
165 for my $n (0..$#argument_kinds) {
166 if(!defined($argument_kinds[$n]) || !defined($declared_argument_kinds[$n])) { next; }
168 if($argument_kinds[$n] =~ /^segptr|segstr$/ ||
169 $declared_argument_kinds[$n] =~ /^segptr|segstr$/)
171 $segmented = 1;
174 # FIXME: Kludge
175 if(!defined($argument_types[$n])) {
176 $argument_types[$n] = "";
179 if(!$winapi->is_allowed_kind($argument_kinds[$n]) ||
180 !$winapi->allowed_type_in_module($argument_types[$n], $module))
182 if($options->report_argument_forbidden($argument_types[$n])) {
183 $output->write("argument " . ($n + 1) . " type is forbidden: " .
184 "$argument_types[$n] ($argument_kinds[$n])\n");
186 } elsif($argument_kinds[$n] ne $declared_argument_kinds[$n]) {
187 if($options->report_argument_kind($argument_kinds[$n]) ||
188 $options->report_argument_kind($declared_argument_kinds[$n]))
190 $output->write("argument " . ($n + 1) . " type mismatch: " .
191 $argument_types[$n] . " ($argument_kinds[$n]) != " .
192 $declared_argument_kinds[$n] . "\n");
197 if($#argument_kinds != $#declared_argument_kinds &&
198 $implemented_calling_convention ne "asm")
200 if($options->argument_count) {
201 $output->write("argument count differs: " .
202 ($#argument_types + 1) . " != " .
203 ($#declared_argument_kinds + 1) . "\n");
209 if($segmented && $options->shared_segmented && $winapi->is_shared_function($internal_name)) {
210 $output->write("function using segmented pointers shared between Win16 och Win32\n");
214 sub check_statements {
215 my $options = shift;
216 my $output = shift;
217 my $winapi = shift;
218 my $functions = shift;
219 my $function = shift;
221 my $module = $function->module;
222 my $internal_name = $function->internal_name;
224 my $first_debug_message = 1;
225 local $_ = $function->statements;
226 while(defined($_)) {
227 if(s/(\w+)\s*(?:\(\s*(\w+)\s*\))?\s*\(\s*((?:\"[^\"]*\"|\([^\)]*\)|[^\)])*?)\s*\)//) {
228 my $called_name = $1;
229 my $channel = $2;
230 my $called_arguments = $3;
231 if($called_name =~ /^if|for|while|switch|sizeof$/) {
232 # Nothing
233 } elsif($called_name =~ /^ERR|FIXME|MSG|TRACE|WARN$/) {
234 if($first_debug_message && $called_name =~ /^FIXME|TRACE$/) {
235 $first_debug_message = 0;
236 if($called_arguments =~ /^\"\((.*?)\)(.*?)\"\s*,\s*(.*?)$/) {
237 my $formating = $1;
238 my $extra = $2;
239 my $arguments = $3;
241 my $format;
242 my $argument;
243 my $n = 0;
244 while($formating && ($formating =~ s/^([^,]*),?//, $format = $1, $format =~ s/^\s*(.*?)\s*$/$1/) &&
245 $arguments && ($arguments =~ s/^([^,]*),?//, $argument = $1, $argument =~ s/^\s*(.*?)\s*$/$1/))
247 my $type = @{$function->argument_types}[$n];
248 my $name = @{$function->argument_names}[$n];
250 $n++;
252 if(!defined($type)) { last; }
254 $format =~ s/^\w+\s*[:=]?\s*//;
255 $format =~ s/\s*\{[^\{\}]*\}$//;
256 $format =~ s/\s*\[[^\[\]]*\]$//;
257 $format =~ s/^\'(.*?)\'$/$1/;
258 $format =~ s/^\\\"(.*?)\\\"$/$1/;
260 if($options->debug_messages) {
261 if($argument !~ /$name/) {
262 $output->write("$called_name: argument $n is wrong ($name != '$argument')\n");
263 } elsif(!$winapi->is_allowed_type_format($module, $type, $format)) {
264 $output->write("$called_name: argument $n ($type $name) has illegal format ($format)\n");
269 if($options->debug_messages) {
270 my $count = $#{$function->argument_types} + 1;
271 if($n != $count) {
272 $output->write("$called_name: argument count mismatch ($n != $count)\n");
277 } else {
278 $$functions{$internal_name}->function_called($called_name);
279 if(!defined($$functions{$called_name})) {
280 $$functions{$called_name} = 'winapi_function'->new;
282 $$functions{$called_name}->function_called_by($internal_name);
284 } else {
285 undef $_;
290 sub check_file {
291 my $options = shift;
292 my $output = shift;
293 my $file = shift;
294 my $functions = shift;
296 if($options->cross_call) {
297 my @names = sort(keys(%$functions));
298 for my $name (@names) {
299 my @called_names = $$functions{$name}->called_function_names;
300 my @called_by_names = $$functions{$name}->called_by_function_names;
301 my $module = $$functions{$name}->module;
303 if($options->cross_call_win32_win16) {
304 my $module16 = $$functions{$name}->module16;
305 my $module32 = $$functions{$name}->module32;
307 if($#called_names >= 0 && (defined($module16) || defined($module32)) ) {
308 for my $called_name (@called_names) {
309 my $called_module16 = $$functions{$called_name}->module16;
310 my $called_module32 = $$functions{$called_name}->module32;
311 if(defined($module32) &&
312 defined($called_module16) && !defined($called_module32) &&
313 $name ne $called_name)
315 $output->write("$file: $module: $name: illegal call to $called_name (Win32 -> Win16)\n");
321 if($options->cross_call_unicode_ascii) {
322 if($name =~ /W$/) {
323 for my $called_name (@called_names) {
324 if($called_name =~ /A$/) {
325 $output->write("$file: $module: $name: illegal call to $called_name (Unicode -> ASCII)\n");