3 # Copyright 1999-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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 # Note that winapi_check are using heuristics quite heavily.
21 # So always remember that:
23 # "Heuristics are bug ridden by definition.
24 # If they didn't have bugs, then they'd be algorithms."
26 # In other words, reported bugs are only potential bugs not
27 # real bugs, so they are called issues rather than bugs.
33 $0 =~ m%^(.*?/?tools)/winapi/winapi_check$%;
34 require "$1/winapi/setup.pm";
38 files_filter files_skip
40 $current_dir $wine_dir
42 use output qw($output);
43 use winapi_check_options qw($options);
46 if($options->progress) {
47 $output->enable_progress;
49 $output->disable_progress;
53 use modules qw($modules);
54 use nativeapi qw($nativeapi);
55 use winapi qw($win16api $win32api @winapis);
59 use util qw(is_subset);
60 use winapi_documentation;
68 if ($options->global) {
69 my @files = get_h_files("winelib");
71 my $progress_current = 0;
72 my $progress_max = scalar(@files);
74 foreach my $file (@files) {
76 $output->lazy_progress("$file: file $progress_current of $progress_max");
79 if(!($file_dir =~ s%(.*?)/[^/]+$%$1%)) {
83 $include2info{$file} = { name => $file };
85 open(IN, "< $wine_dir/$file");
87 if(/^\s*\#\s*include\s*\"(.*?)\"/) {
89 if(-e "$wine_dir/$file_dir/$header") {
90 $include2info{$file}{includes}{"$file_dir/$header"}++;
91 } elsif(-e "$wine_dir/$file_dir/../$header") {
92 if($file_dir =~ m%^(.*?)/[^/]+$%) {
93 $include2info{$file}{includes}{"$1/$header"}++;
95 $include2info{$file}{includes}{"$header"}++;
97 } elsif(-e "$wine_dir/include/$header") {
98 $include2info{$file}{includes}{"include/$header"}++;
99 } elsif ($header ne "config.h") {
100 $output->write("$file: #include \"$header\" is not a local include\n");
107 my @files2 = ("acconfig.h", "poppack.h", "pshpack1.h", "pshpack2.h", "pshpack4.h", "pshpack8.h",
108 "storage.h", "ver.h");
109 foreach my $file2 (@files2) {
110 $include2info{"include/$file2"}{used}++;
114 my @c_files = $options->c_files;
115 @c_files = files_skip(@c_files);
116 @c_files = files_filter("winelib", @c_files);
118 my @h_files = $options->h_files;
119 @h_files = files_skip(@h_files);
120 @h_files = files_filter("winelib", @h_files);
124 if($options->global) {
125 my @complete_modules = $modules->complete_modules(\@c_files);
127 foreach my $module (@complete_modules) {
128 $complete_module{$module}++;
132 foreach my $module ($modules->all_modules) {
133 if(!$complete_module{$module}) {
135 if($wine_dir eq ".") {
136 $output->write("*.c: module $module is not complete\n");
143 foreach my $winapi (@winapis) {
144 foreach my $broken_forward ($winapi->all_broken_forwards) {
145 (my $module, my $external_name, my $forward_module, my $forward_external_name) = @$broken_forward;
146 if($complete_module{$forward_module}) {
147 $output->write("$module.spec: forward is broken: $external_name => $forward_module.$forward_external_name\n");
153 my $progress_current = 0;
154 my $progress_max = scalar(@c_files);
156 my %declared_functions;
158 if($options->headers) {
159 $progress_max += scalar(@h_files);
161 foreach my $file (@h_files) {
165 $output->progress("$file: file $progress_current of $progress_max");
167 my $found_c_comment = sub {
168 my $begin_line = shift;
169 my $end_line = shift;
173 if($begin_line == $end_line) {
174 $output->write("$file:$begin_line: $comment\n");
176 $output->write("$file:$begin_line-$end_line: \\\n$comment\n");
181 my $found_cplusplus_comment = sub {
185 if($options->comments_cplusplus) {
186 $output->write("$file:$line: C++ comments not allowed: $comment\n");
190 my $create_function = sub {
191 return 'winapi_function'->new;
194 my $found_function = sub {
195 my $function = shift;
197 my $internal_name = $function->internal_name;
199 $output->progress("$file (file $progress_current of $progress_max): $internal_name");
200 $output->prefix_callback(sub { return $function->prefix; });
202 my $function_line = $function->function_line;
203 my $linkage = $function->linkage;
204 my $external_name = $function->external_name;
205 my $statements = $function->statements;
207 if($options->headers_misplaced &&
208 !($function->is_win16 && $function->is_win32) &&
209 (($function->is_win16 && $file =~ /^include\/[^\/]*$/) ||
210 ($function->is_win32 && $file =~ /^include\/wine\/[^\/]*$/)))
212 $output->write("declaration misplaced\n");
215 if(defined($external_name) && !defined($statements) &&
216 ($linkage eq "" || $linkage eq "extern"))
218 my $previous_function = $declared_functions{$internal_name};
219 if(!defined($previous_function)) {
220 $declared_functions{$internal_name} = $function;
221 } elsif($options->headers_duplicated) {
222 my $file = $previous_function->file;
223 my $function_line = $previous_function->function_line;
224 if($file =~ /\.h$/) {
225 $output->write("duplicate declaration (first declaration at $file:$function_line)\n");
232 my $create_type = sub {
236 my $found_type = sub {
240 my $found_preprocessor = sub {
241 my $directive = shift;
242 my $argument = shift;
245 winapi_parser::parse_c_file($file, {
246 c_comment_found => $found_c_comment,
247 cplusplus_comment_found => $found_cplusplus_comment,
248 function_create => $create_function,
249 function_found => $found_function,
250 type_create => $create_type,
251 type_found => $found_type,
252 preprocessor_found => $found_preprocessor
257 my %module2functions = ();
259 foreach my $file (@c_files) {
265 my $file_module16 = $modules->allowed_modules_in_file("$current_dir/$file");
266 my $file_module32 = $modules->allowed_modules_in_file("$current_dir/$file");
269 $output->progress("$file (file $progress_current of $progress_max)");
271 my $file_dir = $file;
272 if(!($file_dir =~ s/(.*?)\/[^\/]*$/$1/)) {
276 my $found_c_comment = sub {
277 my $begin_line = shift;
278 my $end_line = shift;
282 if($begin_line == $end_line) {
283 $output->write("$file:$begin_line: $comment\n");
285 $output->write("$file:$begin_line-$end_line: \\\n$comment\n");
290 my $found_cplusplus_comment = sub {
294 if($options->comments_cplusplus) {
295 $output->write("$file:$line: C++ comments not allowed: $comment\n");
299 my $create_function = sub {
300 return 'winapi_function'->new;
303 my $found_function = sub {
304 my $function = shift;
306 my $internal_name = $function->internal_name;
307 $functions{$internal_name} = $function;
309 $output->progress("$file (file $progress_current of $progress_max): $internal_name");
310 $output->prefix_callback(sub { return $function->prefix; });
312 my $declared_function = $declared_functions{$internal_name};
314 my $documentation_line = $function->documentation_line;
315 my $documentation = $function->documentation;
316 my $linkage = $function->linkage;
317 my $return_type = $function->return_type;
318 my $calling_convention = $function->calling_convention;
319 my $statements = $function->statements;
321 my $module16 = $function->module16;
322 my $module32 = $function->module32;
324 my $external_name = $function->external_name;
325 my $external_name16 = $function->external_name16;
326 my $external_name32 = $function->external_name32;
328 if(defined($external_name) && !defined($statements) &&
329 ($linkage eq "" || $linkage eq "extern"))
331 my $previous_function = $declared_functions{$internal_name};
332 if(!defined($previous_function)) {
333 $declared_functions{$internal_name} = $function;
335 my $file = $previous_function->file;
336 my $function_line = $previous_function->function_line;
339 $header =~ s%^(include|$file_dir)/%%;
340 if($header !~ m%^msvcrt/% || $file_dir =~ m%^dlls/msvcrt%) {
341 $output->write("duplicate declaration (first declaration at $file:$function_line)\n");
346 if ($options->global) {
347 foreach my $module ($function->modules) {
348 $module2functions{$module}{$internal_name} = $function;
352 foreach my $module ($function->modules) {
353 $modules->found_module_in_dir($module, $file_dir);
356 if($options->shared) {
357 if($win16api->is_shared_internal_function($internal_name) ||
358 $win32api->is_shared_internal_function($internal_name))
360 $output->write("is shared between Win16 and Win32\n");
364 if($options->headers && $options->headers_needed &&
365 defined($declared_function) && defined($external_name) &&
366 defined($statements))
368 my $needed_include = $declared_function->file;
370 if(!defined($includes{$needed_include})) {
371 my $header = $needed_include;
372 $header =~ s%^(include|$file_dir)/%%;
373 if($header !~ m%^msvcrt/% || $file_dir =~ m%^dlls/msvcrt%) {
374 $output->write("prototype not included: #include \"$header\" is needed\n");
379 if($options->local && $options->argument && defined($statements)) {
380 winapi_local::check_function($function);
383 if($options->local && $options->statements && defined($statements)) {
384 winapi_local::check_statements(\%functions, $function);
387 if($options->local && $options->documentation &&
388 (defined($module16) || defined($module32)) &&
389 $linkage eq "" && defined($statements))
391 winapi_documentation::check_documentation($function);
396 if(defined($external_name16)) {
397 $external_name16 = (split(/\s*&\s*/, $external_name16))[0];
401 if(defined($external_name32)) {
402 $external_name32 = (split(/\s*&\s*/, $external_name32))[0];
405 if($options->local && $options->misplaced &&
406 $linkage ne "extern" && defined($statements))
408 if($options->win16 && $options->report_module($module16))
410 if($file ne "library/port.c" &&
411 !$nativeapi->is_function($internal_name) &&
412 !is_subset($module16, $file_module16))
414 foreach my $module16 (split(/\s*&\s*/, $module16)) {
415 if(!$win16api->is_function_stub($module16, $internal_name)) {
416 $output->write("is misplaced ($module16)\n");
422 if($options->win32 && $options->report_module($module32))
424 if($file ne "library/port.c" &&
425 !$nativeapi->is_function($internal_name) &&
426 !is_subset($module32, $file_module32))
428 foreach my $module32 (split(/\s*&\s*/, $module32)) {
429 if(!$win32api->is_function_stub($module32, $internal_name)) {
430 $output->write("is misplaced ($module32)\n");
437 if($options->local && $options->headers && $options->prototype) {
438 if($options->win16 && $options->report_module($module16)) {
439 if(!$nativeapi->is_function($internal_name) &&
440 !defined($declared_functions{$internal_name}))
442 $output->write("no prototype\n");
446 if($options->win32 && $options->report_module($module32)) {
447 if(!defined($external_name32) || (!$nativeapi->is_function($external_name32) && !defined($declared_functions{$external_name32})))
449 if(!defined($external_name32) || ($external_name32 !~ /^Dll(?:
450 Install|CanUnloadNow|GetClassObject|GetVersion|
451 RegisterServer|RegisterServerEx|UnregisterServer)|DriverProc$/x &&
452 $internal_name !~ /^COMCTL32_Str/ &&
453 $internal_name !~ /^(?:\Q$module32\E|wine)_(?:\Q$external_name32\E|\d+)$/))
455 $output->write("no prototype\n");
467 my $found_include = sub {
469 if(/^\"(?:config\.h|wine\/port\.h)\"/) {
473 my $found_conditional = sub {
476 $nativeapi->found_conditional($_);
478 if($options->config) {
479 if(!$nativeapi->is_conditional($_)) {
480 if(/^HAVE_/ && !/^HAVE_(?:IPX|CORRECT_LINUXINPUT_H|OSS|OSS_MIDI|V4L2)$/)
482 $output->write("$file: $_ is not declared as a conditional\n");
487 $output->write("$file: conditional $_ used but config.h is not included\n");
493 my $create_type = sub {
497 my $found_type = sub {
501 my $preprocessor = 'preprocessor'->new($found_include, $found_conditional);
502 my $found_preprocessor = sub {
503 my $directive = shift;
504 my $argument = shift;
506 $preprocessor->directive($directive, $argument);
508 if($options->config) {
509 if($directive eq "include") {
511 my $check_protection;
513 if($argument =~ /^<(.*?)>$/) {
515 $check_protection = 1;
517 } elsif($argument =~ /^\"(.*?)\"$/) {
519 $check_protection = 0;
522 $output->write("$file: #$directive $argument: is unparsable\n");
525 $check_protection = 0;
529 if(defined($header)) {
531 if(-e "$wine_dir/include/$header") {
532 $include = "include/$header";
533 } elsif(-e "$wine_dir/include/msvcrt/$header") {
534 $include = "include/msvcrt/$header";
535 } elsif(-e "$file_dir/$header") {
536 $include = "$file_dir/$header";
537 } elsif(-e "$file_dir/../$header") {
538 if($file_dir =~ m%^(.*?)/[^/]+$%) {
539 $include = "$1/$header";
541 $include = "$header";
543 } elsif($check_local && $header ne "config.h") {
544 $output->write("$file: #include \"$header\": file not found\n");
547 if(defined($include)) {
548 $includes{$include}++;
549 foreach my $include (keys(%{$include2info{$include}{includes}})) {
550 $includes{$include}++;
555 if($check_protection && $header) {
556 if((-e "$wine_dir/include/$header" || -e "$wine_dir/$file_dir/$header")) {
557 if($header !~ /^(?:oleauto\.h|win(?:base|def|error|gdi|nls|nt|user)\.h)$/ &&
558 $file_dir !~ /tests$/)
560 $output->write("$file: #include \<$header\> is a local include\n");
564 my $macro = uc($header);
565 $macro =~ y/\.\//__/;
566 $macro = "HAVE_" . $macro;
568 if($nativeapi->is_conditional_header($header)) {
569 if(!$preprocessor->is_def($macro)) {
570 if($macro =~ /^HAVE_X11/) {
571 # Do nothing X Windows is handled differently
572 } elsif($macro =~ /^HAVE_(.*?)_H$/) {
574 if($header !~ /^alloca\.h$/ &&
575 $file_dir !~ /tests$/ &&
576 !$preprocessor->is_def("STATFS_DEFINED_BY_$name"))
578 $output->write("$file: #$directive $argument: is a conditional include, " .
579 "but is not protected\n");
583 } elsif($preprocessor->is_def($macro)) {
584 $output->write("$file: #$directive $argument: is protected, but there is no check for it in configure.ac\n");
588 if($check_local && $header) {
589 if(-e "$file_dir/$header") {
590 if($file_dir ne ".") {
591 $include2info{"$file_dir/$header"}{used}++;
592 foreach my $name (keys(%{$include2info{"$file_dir/$header"}{includes}})) {
593 $include2info{$name}{used}++;
596 $include2info{"$header"}{used}++;
597 foreach my $name (keys(%{$include2info{"$header"}{includes}})) {
598 $include2info{$name}{used}++;
601 } elsif(-e "$file_dir/../$header") {
602 if($file_dir =~ m%^(.*?)/[^/]+$%) {
603 $include2info{"$1/$header"}{used}++;
604 foreach my $name (keys(%{$include2info{"$1/$header"}{includes}})) {
605 $include2info{$name}{used}++;
608 $include2info{"$header"}{used}++;
609 foreach my $name (keys(%{$include2info{"$header"}{includes}})) {
610 $include2info{$name}{used}++;
613 } elsif(-e "$wine_dir/include/$header") {
614 $include2info{"include/$header"}{used}++;
615 foreach my $name (keys(%{$include2info{"include/$header"}{includes}})) {
616 $include2info{$name}{used}++;
618 } elsif(-e "$wine_dir/include/msvcrt/$header") {
619 $include2info{"include/msvcrt/$header"}{used}++;
620 foreach my $name (keys(%{$include2info{"include/msvcrt/$header"}{includes}})) {
621 $include2info{$name}{used}++;
623 } elsif ($header ne "config.h") {
624 $output->write("$file: #include \"$header\" is not a local include\n");
631 winapi_parser::parse_c_file($file, {
632 c_comment_found => $found_c_comment,
633 cplusplus_comment_found => $found_cplusplus_comment,
634 function_create => $create_function,
635 function_found => $found_function,
636 type_create => $create_type,
637 type_found => $found_type,
638 preprocessor_found => $found_preprocessor
641 if($options->config_unnecessary) {
642 if($config && $conditional == 0 && !exists($include2info{"include/wine/port.h"})) {
643 $output->write("$file: include2info config.h but do not use any conditionals\n");
647 winapi_local::check_file($file, \%functions);
650 if($options->global) {
651 winapi_global::check_modules(\%complete_module, \%module2functions);
654 winapi_global::check_all_modules(\%include2info);