singleuse.pl 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #!/usr/bin/env perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  10. #
  11. # This software is licensed as described in the file COPYING, which
  12. # you should have received as part of this distribution. The terms
  13. # are also available at https://curl.se/docs/copyright.html.
  14. #
  15. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. # copies of the Software, and permit persons to whom the Software is
  17. # furnished to do so, under the terms of the COPYING file.
  18. #
  19. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. # KIND, either express or implied.
  21. #
  22. # SPDX-License-Identifier: curl
  23. #
  24. ###########################################################################
  25. #
  26. # This script is aimed to help scan for and detect globally declared functions
  27. # that are not used from other source files.
  28. #
  29. # Use it like this:
  30. #
  31. # $ ./scripts/singleuse.pl [--unit] lib/.libs/libcurl.a
  32. #
  33. # --unit : built to support unit tests
  34. #
  35. my $unittests;
  36. if($ARGV[0] eq "--unit") {
  37. $unittests = "tests/unit ";
  38. shift @ARGV;
  39. }
  40. my $file = $ARGV[0];
  41. my %wl = (
  42. 'curlx_uztoso' => 'cmdline tool use',
  43. 'Curl_xfer_write_resp' => 'internal api',
  44. 'Curl_creader_def_init' => 'internal api',
  45. 'Curl_creader_def_close' => 'internal api',
  46. 'Curl_creader_def_read' => 'internal api',
  47. 'Curl_creader_def_total_length' => 'internal api',
  48. );
  49. my %api = (
  50. 'curl_easy_cleanup' => 'API',
  51. 'curl_easy_duphandle' => 'API',
  52. 'curl_easy_escape' => 'API',
  53. 'curl_easy_getinfo' => 'API',
  54. 'curl_easy_init' => 'API',
  55. 'curl_easy_pause' => 'API',
  56. 'curl_easy_perform' => 'API',
  57. 'curl_easy_recv' => 'API',
  58. 'curl_easy_reset' => 'API',
  59. 'curl_easy_send' => 'API',
  60. 'curl_easy_setopt' => 'API',
  61. 'curl_easy_strerror' => 'API',
  62. 'curl_easy_unescape' => 'API',
  63. 'curl_easy_upkeep' => 'API',
  64. 'curl_easy_option_by_id' => 'API',
  65. 'curl_easy_option_by_name' => 'API',
  66. 'curl_easy_option_next' => 'API',
  67. 'curl_escape' => 'API',
  68. 'curl_formadd' => 'API',
  69. 'curl_formfree' => 'API',
  70. 'curl_formget' => 'API',
  71. 'curl_free' => 'API',
  72. 'curl_getdate' => 'API',
  73. 'curl_getenv' => 'API',
  74. 'curl_global_cleanup' => 'API',
  75. 'curl_global_init' => 'API',
  76. 'curl_global_init_mem' => 'API',
  77. 'curl_global_sslset' => 'API',
  78. 'curl_global_trace' => 'API',
  79. 'curl_maprintf' => 'API',
  80. 'curl_mfprintf' => 'API',
  81. 'curl_mime_addpart' => 'API',
  82. 'curl_mime_data' => 'API',
  83. 'curl_mime_data_cb' => 'API',
  84. 'curl_mime_encoder' => 'API',
  85. 'curl_mime_filedata' => 'API',
  86. 'curl_mime_filename' => 'API',
  87. 'curl_mime_free' => 'API',
  88. 'curl_mime_headers' => 'API',
  89. 'curl_mime_init' => 'API',
  90. 'curl_mime_name' => 'API',
  91. 'curl_mime_subparts' => 'API',
  92. 'curl_mime_type' => 'API',
  93. 'curl_mprintf' => 'API',
  94. 'curl_msnprintf' => 'API',
  95. 'curl_msprintf' => 'API',
  96. 'curl_multi_add_handle' => 'API',
  97. 'curl_multi_assign' => 'API',
  98. 'curl_multi_cleanup' => 'API',
  99. 'curl_multi_fdset' => 'API',
  100. 'curl_multi_get_handles' => 'API',
  101. 'curl_multi_info_read' => 'API',
  102. 'curl_multi_init' => 'API',
  103. 'curl_multi_perform' => 'API',
  104. 'curl_multi_remove_handle' => 'API',
  105. 'curl_multi_setopt' => 'API',
  106. 'curl_multi_socket' => 'API',
  107. 'curl_multi_socket_action' => 'API',
  108. 'curl_multi_socket_all' => 'API',
  109. 'curl_multi_poll' => 'API',
  110. 'curl_multi_strerror' => 'API',
  111. 'curl_multi_timeout' => 'API',
  112. 'curl_multi_wait' => 'API',
  113. 'curl_multi_waitfds' => 'API',
  114. 'curl_multi_wakeup' => 'API',
  115. 'curl_mvaprintf' => 'API',
  116. 'curl_mvfprintf' => 'API',
  117. 'curl_mvprintf' => 'API',
  118. 'curl_mvsnprintf' => 'API',
  119. 'curl_mvsprintf' => 'API',
  120. 'curl_pushheader_byname' => 'API',
  121. 'curl_pushheader_bynum' => 'API',
  122. 'curl_share_cleanup' => 'API',
  123. 'curl_share_init' => 'API',
  124. 'curl_share_setopt' => 'API',
  125. 'curl_share_strerror' => 'API',
  126. 'curl_slist_append' => 'API',
  127. 'curl_slist_free_all' => 'API',
  128. 'curl_strequal' => 'API',
  129. 'curl_strnequal' => 'API',
  130. 'curl_unescape' => 'API',
  131. 'curl_url' => 'API',
  132. 'curl_url_cleanup' => 'API',
  133. 'curl_url_dup' => 'API',
  134. 'curl_url_get' => 'API',
  135. 'curl_url_set' => 'API',
  136. 'curl_url_strerror' => 'API',
  137. 'curl_version' => 'API',
  138. 'curl_version_info' => 'API',
  139. 'curl_easy_header' => 'API',
  140. 'curl_easy_nextheader' => 'API',
  141. 'curl_ws_meta' => 'API',
  142. 'curl_ws_recv' => 'API',
  143. 'curl_ws_send' => 'API',
  144. # the following functions are provided globally in debug builds
  145. 'curl_easy_perform_ev' => 'debug-build',
  146. );
  147. sub doublecheck {
  148. my ($f, $used) = @_;
  149. open(F, "git grep -le '$f\\W' -- lib ${unittests}packages|");
  150. my @also;
  151. while(<F>) {
  152. my $e = $_;
  153. chomp $e;
  154. if($e =~ /\.[c]$/) {
  155. if($e !~ /^lib\/${used}\.c/) {
  156. push @also, $e;
  157. }
  158. }
  159. }
  160. close(F);
  161. return @also;
  162. }
  163. open(N, "nm $file|") ||
  164. die;
  165. my %exist;
  166. my %uses;
  167. my $file;
  168. while (<N>) {
  169. my $l = $_;
  170. chomp $l;
  171. if($l =~ /^([0-9a-z_-]+)\.o:/) {
  172. $file = $1;
  173. }
  174. if($l =~ /^([0-9a-f]+) T (.*)/) {
  175. my ($name)=($2);
  176. #print "Define $name in $file\n";
  177. $file =~ s/^libcurl_la-//;
  178. $exist{$name} = $file;
  179. }
  180. elsif($l =~ /^ U (.*)/) {
  181. my ($name)=($1);
  182. #print "Uses $name in $file\n";
  183. $uses{$name} .= "$file, ";
  184. }
  185. }
  186. close(N);
  187. my $err;
  188. for(sort keys %exist) {
  189. #printf "%s is defined in %s, used by: %s\n", $_, $exist{$_}, $uses{$_};
  190. if(!$uses{$_}) {
  191. # this is a symbol with no "global" user
  192. if($_ =~ /^curl_dbg_/) {
  193. # we ignore the memdebug symbols
  194. }
  195. elsif($_ =~ /^curl_/) {
  196. if(!$api{$_}) {
  197. # not present in the API, or for debug-builds
  198. print STDERR "Bad curl-prefix: $_\n";
  199. $err++;
  200. }
  201. }
  202. elsif($wl{$_}) {
  203. #print "$_ is WL\n";
  204. }
  205. else {
  206. my $c = $_;
  207. my @also = doublecheck($c, $exist{$c});
  208. if(!scalar(@also)) {
  209. printf "%s in %s\n", $c, $exist{$c};
  210. $err++;
  211. }
  212. # foreach my $a (@also) {
  213. # print " $a\n";
  214. # }
  215. }
  216. }
  217. elsif($_ =~ /^curl_/) {
  218. # global prefix, make sure it is "blessed"
  219. if(!$api{$_}) {
  220. # not present in the API, or for debug-builds
  221. if($_ !~ /^curl_dbg_/) {
  222. # ignore the memdebug symbols
  223. print STDERR "Bad curl-prefix $_\n";
  224. $err++;
  225. }
  226. }
  227. }
  228. }
  229. exit $err;