winapi_check 18 KB

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