singleuse.pl 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 -Fwle '$f' -- 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. # libcurl.a(unity_0_c.c.o):
  175. elsif($l =~ /\(([0-9a-z_.-]+)\.o\):/) { # Apple nm
  176. $file = $1;
  177. }
  178. if($l =~ /^([0-9a-f]+) T _?(.*)/) {
  179. my ($name)=($2);
  180. #print "Define $name in $file\n";
  181. $file =~ s/^libcurl_la-//;
  182. $exist{$name} = $file;
  183. }
  184. elsif($l =~ /^ U _?(.*)/) {
  185. my ($name)=($1);
  186. #print "Uses $name in $file\n";
  187. $uses{$name} .= "$file, ";
  188. }
  189. }
  190. close(N);
  191. my $err;
  192. for(sort keys %exist) {
  193. #printf "%s is defined in %s, used by: %s\n", $_, $exist{$_}, $uses{$_};
  194. if(!$uses{$_}) {
  195. # this is a symbol with no "global" user
  196. if($_ =~ /^curl_dbg_/) {
  197. # we ignore the memdebug symbols
  198. }
  199. elsif($_ =~ /^curl_/) {
  200. if(!$api{$_}) {
  201. # not present in the API, or for debug-builds
  202. print STDERR "Bad curl-prefix: $_\n";
  203. $err++;
  204. }
  205. }
  206. elsif($wl{$_}) {
  207. #print "$_ is WL\n";
  208. }
  209. else {
  210. my $c = $_;
  211. my @also = doublecheck($c, $exist{$c});
  212. if(!scalar(@also)) {
  213. printf "%s in %s\n", $c, $exist{$c};
  214. $err++;
  215. }
  216. # foreach my $a (@also) {
  217. # print " $a\n";
  218. # }
  219. }
  220. }
  221. elsif($_ =~ /^curl_/) {
  222. # global prefix, make sure it is "blessed"
  223. if(!$api{$_}) {
  224. # not present in the API, or for debug-builds
  225. if($_ !~ /^curl_dbg_/) {
  226. # ignore the memdebug symbols
  227. print STDERR "Bad curl-prefix $_\n";
  228. $err++;
  229. }
  230. }
  231. }
  232. }
  233. exit $err;