singleuse.pl 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. );
  44. my %api = (
  45. 'curl_easy_cleanup' => 'API',
  46. 'curl_easy_duphandle' => 'API',
  47. 'curl_easy_escape' => 'API',
  48. 'curl_easy_getinfo' => 'API',
  49. 'curl_easy_init' => 'API',
  50. 'curl_easy_pause' => 'API',
  51. 'curl_easy_perform' => 'API',
  52. 'curl_easy_recv' => 'API',
  53. 'curl_easy_reset' => 'API',
  54. 'curl_easy_send' => 'API',
  55. 'curl_easy_setopt' => 'API',
  56. 'curl_easy_strerror' => 'API',
  57. 'curl_easy_unescape' => 'API',
  58. 'curl_easy_upkeep' => 'API',
  59. 'curl_easy_option_by_id' => 'API',
  60. 'curl_easy_option_by_name' => 'API',
  61. 'curl_easy_option_next' => 'API',
  62. 'curl_escape' => 'API',
  63. 'curl_formadd' => 'API',
  64. 'curl_formfree' => 'API',
  65. 'curl_formget' => 'API',
  66. 'curl_free' => 'API',
  67. 'curl_getdate' => 'API',
  68. 'curl_getenv' => 'API',
  69. 'curl_global_cleanup' => 'API',
  70. 'curl_global_init' => 'API',
  71. 'curl_global_init_mem' => 'API',
  72. 'curl_global_sslset' => 'API',
  73. 'curl_global_trace' => 'API',
  74. 'curl_maprintf' => 'API',
  75. 'curl_mfprintf' => 'API',
  76. 'curl_mime_addpart' => 'API',
  77. 'curl_mime_data' => 'API',
  78. 'curl_mime_data_cb' => 'API',
  79. 'curl_mime_encoder' => 'API',
  80. 'curl_mime_filedata' => 'API',
  81. 'curl_mime_filename' => 'API',
  82. 'curl_mime_free' => 'API',
  83. 'curl_mime_headers' => 'API',
  84. 'curl_mime_init' => 'API',
  85. 'curl_mime_name' => 'API',
  86. 'curl_mime_subparts' => 'API',
  87. 'curl_mime_type' => 'API',
  88. 'curl_mprintf' => 'API',
  89. 'curl_msnprintf' => 'API',
  90. 'curl_msprintf' => 'API',
  91. 'curl_multi_add_handle' => 'API',
  92. 'curl_multi_assign' => 'API',
  93. 'curl_multi_cleanup' => 'API',
  94. 'curl_multi_fdset' => 'API',
  95. 'curl_multi_get_handles' => 'API',
  96. 'curl_multi_info_read' => 'API',
  97. 'curl_multi_init' => 'API',
  98. 'curl_multi_perform' => 'API',
  99. 'curl_multi_remove_handle' => 'API',
  100. 'curl_multi_setopt' => 'API',
  101. 'curl_multi_socket' => 'API',
  102. 'curl_multi_socket_action' => 'API',
  103. 'curl_multi_socket_all' => 'API',
  104. 'curl_multi_poll' => 'API',
  105. 'curl_multi_strerror' => 'API',
  106. 'curl_multi_timeout' => 'API',
  107. 'curl_multi_wait' => 'API',
  108. 'curl_multi_wakeup' => 'API',
  109. 'curl_mvaprintf' => 'API',
  110. 'curl_mvfprintf' => 'API',
  111. 'curl_mvprintf' => 'API',
  112. 'curl_mvsnprintf' => 'API',
  113. 'curl_mvsprintf' => 'API',
  114. 'curl_pushheader_byname' => 'API',
  115. 'curl_pushheader_bynum' => 'API',
  116. 'curl_share_cleanup' => 'API',
  117. 'curl_share_init' => 'API',
  118. 'curl_share_setopt' => 'API',
  119. 'curl_share_strerror' => 'API',
  120. 'curl_slist_append' => 'API',
  121. 'curl_slist_free_all' => 'API',
  122. 'curl_strequal' => 'API',
  123. 'curl_strnequal' => 'API',
  124. 'curl_unescape' => 'API',
  125. 'curl_url' => 'API',
  126. 'curl_url_cleanup' => 'API',
  127. 'curl_url_dup' => 'API',
  128. 'curl_url_get' => 'API',
  129. 'curl_url_set' => 'API',
  130. 'curl_url_strerror' => 'API',
  131. 'curl_version' => 'API',
  132. 'curl_version_info' => 'API',
  133. 'curl_easy_header' => 'API',
  134. 'curl_easy_nextheader' => 'API',
  135. 'curl_ws_meta' => 'API',
  136. 'curl_ws_recv' => 'API',
  137. 'curl_ws_send' => 'API',
  138. # the following functions are provided globally in debug builds
  139. 'curl_easy_perform_ev' => 'debug-build',
  140. );
  141. sub doublecheck {
  142. my ($f, $used) = @_;
  143. open(F, "git grep -le '$f\\W' -- lib ${unittests}packages|");
  144. my @also;
  145. while(<F>) {
  146. my $e = $_;
  147. chomp $e;
  148. if($e =~ /\.[c]$/) {
  149. if($e !~ /^lib\/${used}\.c/) {
  150. push @also, $e;
  151. }
  152. }
  153. }
  154. close(F);
  155. return @also;
  156. }
  157. open(N, "nm $file|") ||
  158. die;
  159. my %exist;
  160. my %uses;
  161. my $file;
  162. while (<N>) {
  163. my $l = $_;
  164. chomp $l;
  165. if($l =~ /^([0-9a-z_-]+)\.o:/) {
  166. $file = $1;
  167. }
  168. if($l =~ /^([0-9a-f]+) T (.*)/) {
  169. my ($name)=($2);
  170. #print "Define $name in $file\n";
  171. $file =~ s/^libcurl_la-//;
  172. $exist{$name} = $file;
  173. }
  174. elsif($l =~ /^ U (.*)/) {
  175. my ($name)=($1);
  176. #print "Uses $name in $file\n";
  177. $uses{$name} .= "$file, ";
  178. }
  179. }
  180. close(N);
  181. my $err;
  182. for(sort keys %exist) {
  183. #printf "%s is defined in %s, used by: %s\n", $_, $exist{$_}, $uses{$_};
  184. if(!$uses{$_}) {
  185. # this is a symbol with no "global" user
  186. if($_ =~ /^curl_dbg_/) {
  187. # we ignore the memdebug symbols
  188. }
  189. elsif($_ =~ /^curl_/) {
  190. if(!$api{$_}) {
  191. # not present in the API, or for debug-builds
  192. print STDERR "Bad curl-prefix: $_\n";
  193. $err++;
  194. }
  195. }
  196. elsif($wl{$_}) {
  197. #print "$_ is WL\n";
  198. }
  199. else {
  200. my $c = $_;
  201. my @also = doublecheck($c, $exist{$c});
  202. if(!scalar(@also)) {
  203. printf "%s in %s\n", $c, $exist{$c};
  204. $err++;
  205. }
  206. # foreach my $a (@also) {
  207. # print " $a\n";
  208. # }
  209. }
  210. }
  211. elsif($_ =~ /^curl_/) {
  212. # global prefix, make sure it is "blessed"
  213. if(!$api{$_}) {
  214. # not present in the API, or for debug-builds
  215. if($_ !~ /^curl_dbg_/) {
  216. # ignore the memdebug symbols
  217. print STDERR "Bad curl-prefix $_\n";
  218. $err++;
  219. }
  220. }
  221. }
  222. }
  223. exit $err;