winapi_check 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. #!/usr/bin/perl
  2. # Copyright 1999-2002 Patrik Stridvall
  3. #
  4. # This library is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Lesser General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 2.1 of the License, or (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # Lesser General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Lesser General Public
  15. # License along with this library; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  17. #
  18. # Note that winapi_check are using heuristics quite heavily.
  19. # So always remember that:
  20. #
  21. # "Heuristics are bug ridden by definition.
  22. # If they didn't have bugs, then they'd be algorithms."
  23. #
  24. # In other words, reported bugs are only potential bugs not
  25. # real bugs, so they are called issues rather than bugs.
  26. #
  27. use strict;
  28. use warnings 'all';
  29. BEGIN {
  30. $0 =~ m%^(.*?/?tools)/winapi/winapi_check$%;
  31. require "$1/winapi/setup.pm";
  32. }
  33. use config qw(
  34. files_filter files_skip
  35. get_h_files
  36. $current_dir $wine_dir
  37. );
  38. use output qw($output);
  39. use winapi_check_options qw($options);
  40. BEGIN {
  41. if($options->progress) {
  42. $output->enable_progress;
  43. } else {
  44. $output->disable_progress;
  45. }
  46. }
  47. use modules qw($modules);
  48. use nativeapi qw($nativeapi);
  49. use winapi qw($win16api $win32api @winapis);
  50. use preprocessor;
  51. use type;
  52. use util qw(is_subset);
  53. use winapi_documentation;
  54. use winapi_function;
  55. use winapi_local;
  56. use winapi_global;
  57. use winapi_parser;
  58. my %include2info;
  59. if ($options->global) {
  60. my @files = get_h_files("winelib");
  61. my $progress_current = 0;
  62. my $progress_max = scalar(@files);
  63. foreach my $file (@files) {
  64. $progress_current++;
  65. $output->lazy_progress("$file: file $progress_current of $progress_max");
  66. my $file_dir = $file;
  67. if(!($file_dir =~ s%(.*?)/[^/]+$%$1%)) {
  68. $file_dir = ".";
  69. }
  70. $include2info{$file} = { name => $file };
  71. open(IN, "< $wine_dir/$file") || die "Error: Can't open $wine_dir/$file: $!\n";
  72. while(<IN>) {
  73. if(/^\s*\#\s*include\s*\"(.*?)\"/) {
  74. my $header = $1;
  75. if(-e "$wine_dir/$file_dir/$header") {
  76. $include2info{$file}{includes}{"$file_dir/$header"}++;
  77. } elsif(-e "$wine_dir/$file_dir/../$header") {
  78. if($file_dir =~ m%^(.*?)/[^/]+$%) {
  79. $include2info{$file}{includes}{"$1/$header"}++;
  80. } else {
  81. $include2info{$file}{includes}{"$header"}++;
  82. }
  83. } elsif(-e "$wine_dir/include/$header") {
  84. $include2info{$file}{includes}{"include/$header"}++;
  85. } elsif ($header ne "config.h") {
  86. $output->write("$file: #include \"$header\" is not a local include\n");
  87. }
  88. }
  89. }
  90. close(IN);
  91. }
  92. my @files2 = ("acconfig.h", "poppack.h", "pshpack1.h", "pshpack2.h", "pshpack4.h", "pshpack8.h",
  93. "storage.h", "ver.h");
  94. foreach my $file2 (@files2) {
  95. $include2info{"include/$file2"}{used}++;
  96. }
  97. }
  98. my @c_files = $options->c_files;
  99. @c_files = files_skip(@c_files);
  100. @c_files = files_filter("winelib", @c_files);
  101. my @h_files = $options->h_files;
  102. @h_files = files_skip(@h_files);
  103. @h_files = files_filter("winelib", @h_files);
  104. my $all_modules = 0;
  105. my %complete_module;
  106. if($options->global) {
  107. my @complete_modules = $modules->complete_modules(\@c_files);
  108. foreach my $module (@complete_modules) {
  109. $complete_module{$module}++;
  110. }
  111. $all_modules = 1;
  112. foreach my $module ($modules->all_modules) {
  113. if(!$complete_module{$module}) {
  114. $all_modules = 0;
  115. if($wine_dir eq ".") {
  116. $output->write("*.c: module $module is not complete\n");
  117. }
  118. }
  119. }
  120. }
  121. if(1) {
  122. foreach my $winapi (@winapis) {
  123. foreach my $broken_forward ($winapi->all_broken_forwards) {
  124. (my $module, my $external_name, my $forward_module, my $forward_external_name) = @$broken_forward;
  125. if($complete_module{$forward_module}) {
  126. $output->write("$module.spec: forward is broken: $external_name => $forward_module.$forward_external_name\n");
  127. }
  128. }
  129. }
  130. }
  131. my $progress_current = 0;
  132. my $progress_max = scalar(@c_files);
  133. my %declared_functions;
  134. if($options->headers) {
  135. $progress_max += scalar(@h_files);
  136. foreach my $file (@h_files) {
  137. my %functions;
  138. $progress_current++;
  139. $output->progress("$file: file $progress_current of $progress_max");
  140. my $found_c_comment = sub {
  141. my $begin_line = shift;
  142. my $end_line = shift;
  143. my $comment = shift;
  144. if(0) {
  145. if($begin_line == $end_line) {
  146. $output->write("$file:$begin_line: $comment\n");
  147. } else {
  148. $output->write("$file:$begin_line-$end_line: \\\n$comment\n");
  149. }
  150. }
  151. };
  152. my $found_cplusplus_comment = sub {
  153. my $line = shift;
  154. my $comment = shift;
  155. if($options->comments_cplusplus) {
  156. $output->write("$file:$line: C++ comments not allowed: $comment\n");
  157. }
  158. };
  159. my $create_function = sub {
  160. return 'winapi_function'->new;
  161. };
  162. my $found_function = sub {
  163. my $function = shift;
  164. my $internal_name = $function->internal_name;
  165. $output->progress("$file (file $progress_current of $progress_max): $internal_name");
  166. $output->prefix_callback(sub { return $function->prefix; });
  167. my $function_line = $function->function_line;
  168. my $linkage = $function->linkage;
  169. my $external_name = $function->external_name;
  170. my $statements = $function->statements;
  171. if($options->headers_misplaced &&
  172. !($function->is_win16 && $function->is_win32) &&
  173. (($function->is_win16 && $file =~ /^include\/[^\/]*$/) ||
  174. ($function->is_win32 && $file =~ /^include\/wine\/[^\/]*$/)))
  175. {
  176. $output->write("declaration misplaced\n");
  177. }
  178. if(defined($external_name) && !defined($statements) &&
  179. ($linkage eq "" || $linkage eq "extern"))
  180. {
  181. my $previous_function = $declared_functions{$internal_name};
  182. if(!defined($previous_function)) {
  183. $declared_functions{$internal_name} = $function;
  184. } elsif($options->headers_duplicated) {
  185. my $file = $previous_function->file;
  186. my $function_line = $previous_function->function_line;
  187. if($file =~ /\.h$/) {
  188. $output->write("duplicate declaration (first declaration at $file:$function_line)\n");
  189. }
  190. }
  191. }
  192. $output->prefix("");
  193. };
  194. my $create_type = sub {
  195. return 'type'->new;
  196. };
  197. my $found_type = sub {
  198. my $type = shift;
  199. };
  200. my $found_preprocessor = sub {
  201. my $directive = shift;
  202. my $argument = shift;
  203. };
  204. winapi_parser::parse_c_file($file, {
  205. c_comment_found => $found_c_comment,
  206. cplusplus_comment_found => $found_cplusplus_comment,
  207. function_create => $create_function,
  208. function_found => $found_function,
  209. type_create => $create_type,
  210. type_found => $found_type,
  211. preprocessor_found => $found_preprocessor
  212. });
  213. }
  214. }
  215. my %module2functions = ();
  216. foreach my $file (@c_files) {
  217. my %functions = ();
  218. my %includes = ();
  219. $includes{$file}++;
  220. my $file_module16 = $modules->allowed_modules_in_file("$current_dir/$file");
  221. my $file_module32 = $modules->allowed_modules_in_file("$current_dir/$file");
  222. $progress_current++;
  223. $output->progress("$file (file $progress_current of $progress_max)");
  224. my $file_dir = $file;
  225. if(!($file_dir =~ s/(.*?)\/[^\/]*$/$1/)) {
  226. $file_dir = ".";
  227. }
  228. my $found_c_comment = sub {
  229. my $begin_line = shift;
  230. my $end_line = shift;
  231. my $comment = shift;
  232. if(0) {
  233. if($begin_line == $end_line) {
  234. $output->write("$file:$begin_line: $comment\n");
  235. } else {
  236. $output->write("$file:$begin_line-$end_line: \\\n$comment\n");
  237. }
  238. }
  239. };
  240. my $found_cplusplus_comment = sub {
  241. my $line = shift;
  242. my $comment = shift;
  243. if($options->comments_cplusplus) {
  244. $output->write("$file:$line: C++ comments not allowed: $comment\n");
  245. }
  246. };
  247. my $create_function = sub {
  248. return 'winapi_function'->new;
  249. };
  250. my $found_function = sub {
  251. my $function = shift;
  252. my $internal_name = $function->internal_name;
  253. $functions{$internal_name} = $function;
  254. $output->progress("$file (file $progress_current of $progress_max): $internal_name");
  255. $output->prefix_callback(sub { return $function->prefix; });
  256. my $declared_function = $declared_functions{$internal_name};
  257. my $documentation_line = $function->documentation_line;
  258. my $documentation = $function->documentation;
  259. my $linkage = $function->linkage;
  260. my $return_type = $function->return_type;
  261. my $calling_convention = $function->calling_convention;
  262. my $statements = $function->statements;
  263. my $module16 = $function->module16;
  264. my $module32 = $function->module32;
  265. my $external_name = $function->external_name;
  266. my $external_name16 = $function->external_name16;
  267. my $external_name32 = $function->external_name32;
  268. if(defined($external_name) && !defined($statements) &&
  269. ($linkage eq "" || $linkage eq "extern"))
  270. {
  271. my $previous_function = $declared_functions{$internal_name};
  272. if(!defined($previous_function)) {
  273. $declared_functions{$internal_name} = $function;
  274. } else {
  275. my $file = $previous_function->file;
  276. my $function_line = $previous_function->function_line;
  277. my $header = $file;
  278. $header =~ s%^(include|$file_dir)/%%;
  279. if($header !~ m%^msvcrt/% || $file_dir =~ m%^dlls/msvcrt%) {
  280. $output->write("duplicate declaration (first declaration at $file:$function_line)\n");
  281. }
  282. }
  283. }
  284. if ($options->global) {
  285. foreach my $module ($function->modules) {
  286. $module2functions{$module}{$internal_name} = $function;
  287. }
  288. }
  289. foreach my $module ($function->modules) {
  290. $modules->found_module_in_dir($module, $file_dir);
  291. }
  292. if($options->shared) {
  293. if($win16api->is_shared_internal_function($internal_name) ||
  294. $win32api->is_shared_internal_function($internal_name))
  295. {
  296. $output->write("is shared between Win16 and Win32\n");
  297. }
  298. }
  299. if($options->headers && $options->headers_needed &&
  300. defined($declared_function) && defined($external_name) &&
  301. defined($statements))
  302. {
  303. my $needed_include = $declared_function->file;
  304. if(!defined($includes{$needed_include})) {
  305. my $header = $needed_include;
  306. $header =~ s%^(include|$file_dir)/%%;
  307. if($header !~ m%^msvcrt/% || $file_dir =~ m%^dlls/msvcrt%) {
  308. $output->write("prototype not included: #include \"$header\" is needed\n");
  309. }
  310. }
  311. }
  312. if($options->local && $options->argument && defined($statements)) {
  313. winapi_local::check_function($function);
  314. }
  315. if($options->local && $options->statements && defined($statements)) {
  316. winapi_local::check_statements(\%functions, $function);
  317. }
  318. if($options->local && $options->documentation &&
  319. (defined($module16) || defined($module32)) &&
  320. $linkage eq "" && defined($statements))
  321. {
  322. winapi_documentation::check_documentation($function);
  323. }
  324. if(1) {
  325. # FIXME: Not correct
  326. if(defined($external_name16)) {
  327. $external_name16 = (split(/\s*&\s*/, $external_name16))[0];
  328. }
  329. # FIXME: Not correct
  330. if(defined($external_name32)) {
  331. $external_name32 = (split(/\s*&\s*/, $external_name32))[0];
  332. }
  333. if($options->local && $options->misplaced &&
  334. $linkage ne "extern" && defined($statements))
  335. {
  336. if($options->win16 && $options->report_module($module16))
  337. {
  338. if($file ne "library/port.c" &&
  339. !$nativeapi->is_function($internal_name) &&
  340. !is_subset($module16, $file_module16))
  341. {
  342. foreach my $module16 (split(/\s*&\s*/, $module16)) {
  343. if(!$win16api->is_function_stub($module16, $internal_name)) {
  344. $output->write("is misplaced ($module16)\n");
  345. }
  346. }
  347. }
  348. }
  349. if($options->win32 && $options->report_module($module32))
  350. {
  351. if($file ne "library/port.c" &&
  352. !$nativeapi->is_function($internal_name) &&
  353. !is_subset($module32, $file_module32))
  354. {
  355. foreach my $module32 (split(/\s*&\s*/, $module32)) {
  356. if(!$win32api->is_function_stub($module32, $internal_name)) {
  357. $output->write("is misplaced ($module32)\n");
  358. }
  359. }
  360. }
  361. }
  362. }
  363. if($options->local && $options->headers && $options->prototype) {
  364. if($options->win16 && $options->report_module($module16)) {
  365. if(!$nativeapi->is_function($internal_name) &&
  366. !defined($declared_functions{$internal_name}))
  367. {
  368. $output->write("no prototype\n");
  369. }
  370. }
  371. if($options->win32 && $options->report_module($module32)) {
  372. if(!defined($external_name32) || (!$nativeapi->is_function($external_name32) && !defined($declared_functions{$external_name32})))
  373. {
  374. if(!defined($external_name32) || ($external_name32 !~ /^Dll(?:
  375. Install|CanUnloadNow|GetClassObject|GetVersion|
  376. RegisterServer|RegisterServerEx|UnregisterServer)|DriverProc$/x &&
  377. $internal_name !~ /^COMCTL32_Str/ &&
  378. $internal_name !~ /^(?:\Q$module32\E|wine)_(?:\Q$external_name32\E|\d+)$/))
  379. {
  380. $output->write("no prototype\n");
  381. }
  382. }
  383. }
  384. }
  385. }
  386. $output->prefix("");
  387. };
  388. my $config = 0;
  389. my $conditional = 0;
  390. my $found_include = sub {
  391. local $_ = shift;
  392. if(/^\"(?:config\.h|wine\/port\.h)\"/) {
  393. $config++;
  394. }
  395. };
  396. my $found_conditional = sub {
  397. local $_ = shift;
  398. $nativeapi->found_conditional($_);
  399. if($options->config) {
  400. if(!$nativeapi->is_conditional($_)) {
  401. if(/^HAVE_/ && !/^HAVE_(?:IPX|CORRECT_LINUXINPUT_H|OSS|OSS_MIDI|V4L2)$/)
  402. {
  403. $output->write("$file: $_ is not declared as a conditional\n");
  404. }
  405. } else {
  406. $conditional++;
  407. if(!$config) {
  408. $output->write("$file: conditional $_ used but config.h is not included\n");
  409. }
  410. }
  411. }
  412. };
  413. my $create_type = sub {
  414. return 'type'->new;
  415. };
  416. my $found_type = sub {
  417. my $type = shift;
  418. };
  419. sub recursive_include {
  420. my $include = shift;
  421. my $includes = shift;
  422. if(!defined($includes->{$include})) {
  423. $includes->{$include}++;
  424. foreach my $include (keys(%{$include2info{$include}{includes}})) {
  425. recursive_include($include, \%$includes);
  426. }
  427. }
  428. };
  429. my $preprocessor = 'preprocessor'->new($found_include, $found_conditional);
  430. my $found_preprocessor = sub {
  431. my $directive = shift;
  432. my $argument = shift;
  433. $preprocessor->directive($directive, $argument);
  434. if($options->config) {
  435. if($directive eq "include") {
  436. my $header;
  437. my $check_protection;
  438. my $check_local;
  439. if($argument =~ /^<(.*?)>$/) {
  440. $header = $1;
  441. $check_protection = 1;
  442. $check_local = 0;
  443. } elsif($argument =~ /^\"(.*?)\"$/) {
  444. $header = $1;
  445. $check_protection = 0;
  446. $check_local = 1;
  447. } else {
  448. $output->write("$file: #$directive $argument: is unparsable\n");
  449. $header = undef;
  450. $check_protection = 0;
  451. $check_local = 0;
  452. }
  453. if(defined($header)) {
  454. my $include;
  455. if(-e "$wine_dir/include/$header") {
  456. $include = "include/$header";
  457. } elsif(-e "$wine_dir/include/msvcrt/$header") {
  458. $include = "include/msvcrt/$header";
  459. } elsif(-e "$file_dir/$header") {
  460. $include = "$file_dir/$header";
  461. } elsif(-e "$file_dir/../$header") {
  462. if($file_dir =~ m%^(.*?)/[^/]+$%) {
  463. $include = "$1/$header";
  464. } else {
  465. $include = "$header";
  466. }
  467. } elsif($check_local && $header ne "config.h") {
  468. $output->write("$file: #include \"$header\": file not found\n");
  469. }
  470. if(defined($include)) {
  471. recursive_include($include, \%includes);
  472. }
  473. }
  474. if($check_protection && $header) {
  475. if((-e "$wine_dir/include/$header" || -e "$wine_dir/$file_dir/$header")) {
  476. if($header !~ /^(?:oleauto\.h|win(?:base|def|error|gdi|nls|nt|user)\.h)$/ &&
  477. $file_dir !~ /tests$/)
  478. {
  479. $output->write("$file: #include \<$header\> is a local include\n");
  480. }
  481. }
  482. my $macro = uc($header);
  483. $macro =~ y/\.\//__/;
  484. $macro = "HAVE_" . $macro;
  485. if($nativeapi->is_conditional_header($header)) {
  486. if(!$preprocessor->is_def($macro)) {
  487. if($macro =~ /^HAVE_X11/) {
  488. # Do nothing X Windows is handled differently
  489. } elsif($macro =~ /^HAVE_(.*?)_H$/) {
  490. my $name = $1;
  491. if($header !~ /^alloca\.h$/ &&
  492. $file_dir !~ /tests$/)
  493. {
  494. $output->write("$file: #$directive $argument: is a conditional include, " .
  495. "but is not protected\n");
  496. }
  497. }
  498. }
  499. } elsif($preprocessor->is_def($macro)) {
  500. $output->write("$file: #$directive $argument: is protected, but there is no check for it in configure.ac\n");
  501. }
  502. }
  503. if($check_local && $header) {
  504. if(-e "$file_dir/$header") {
  505. if($file_dir ne ".") {
  506. $include2info{"$file_dir/$header"}{used}++;
  507. foreach my $name (keys(%{$include2info{"$file_dir/$header"}{includes}})) {
  508. $include2info{$name}{used}++;
  509. }
  510. } else {
  511. $include2info{"$header"}{used}++;
  512. foreach my $name (keys(%{$include2info{"$header"}{includes}})) {
  513. $include2info{$name}{used}++;
  514. }
  515. }
  516. } elsif(-e "$file_dir/../$header") {
  517. if($file_dir =~ m%^(.*?)/[^/]+$%) {
  518. $include2info{"$1/$header"}{used}++;
  519. foreach my $name (keys(%{$include2info{"$1/$header"}{includes}})) {
  520. $include2info{$name}{used}++;
  521. }
  522. } else {
  523. $include2info{"$header"}{used}++;
  524. foreach my $name (keys(%{$include2info{"$header"}{includes}})) {
  525. $include2info{$name}{used}++;
  526. }
  527. }
  528. } elsif(-e "$wine_dir/include/$header") {
  529. $include2info{"include/$header"}{used}++;
  530. foreach my $name (keys(%{$include2info{"include/$header"}{includes}})) {
  531. $include2info{$name}{used}++;
  532. }
  533. } elsif(-e "$wine_dir/include/msvcrt/$header") {
  534. $include2info{"include/msvcrt/$header"}{used}++;
  535. foreach my $name (keys(%{$include2info{"include/msvcrt/$header"}{includes}})) {
  536. $include2info{$name}{used}++;
  537. }
  538. } elsif ($header ne "config.h") {
  539. $output->write("$file: #include \"$header\" is not a local include\n");
  540. }
  541. }
  542. }
  543. }
  544. };
  545. winapi_parser::parse_c_file($file, {
  546. c_comment_found => $found_c_comment,
  547. cplusplus_comment_found => $found_cplusplus_comment,
  548. function_create => $create_function,
  549. function_found => $found_function,
  550. type_create => $create_type,
  551. type_found => $found_type,
  552. preprocessor_found => $found_preprocessor
  553. });
  554. if($options->config_unnecessary) {
  555. if($config && $conditional == 0 && !exists($include2info{"include/wine/port.h"})) {
  556. $output->write("$file: include2info config.h but do not use any conditionals\n");
  557. }
  558. }
  559. winapi_local::check_file($file, \%functions);
  560. }
  561. if($options->global) {
  562. winapi_global::check_modules(\%complete_module, \%module2functions);
  563. if($all_modules) {
  564. winapi_global::check_all_modules(\%include2info);
  565. }
  566. }