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