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