2
0

symbol-scan.pl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/usr/bin/env perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) 2010 - 2022, Daniel Stenberg, <[email protected]>, 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 grew out of help from Przemyslaw Iskra and Balint Szilakszi
  27. # a late evening in the #curl IRC channel.
  28. #
  29. use strict;
  30. use warnings;
  31. use vars qw($Cpreprocessor);
  32. #
  33. # configurehelp perl module is generated by configure script
  34. #
  35. my $rc = eval {
  36. require configurehelp;
  37. configurehelp->import(qw(
  38. $Cpreprocessor
  39. ));
  40. 1;
  41. };
  42. # Set default values if configure has not generated a configurehelp.pm file.
  43. # This is the case with cmake.
  44. if (!$rc) {
  45. $Cpreprocessor = 'cpp';
  46. }
  47. # we may get the dir root pointed out
  48. my $root=$ARGV[0] || ".";
  49. # need an include directory when building out-of-tree
  50. my $i = ($ARGV[1]) ? "-I$ARGV[1] " : '';
  51. my $h = "$root/include/curl/curl.h";
  52. my $mh = "$root/include/curl/multi.h";
  53. my $ua = "$root/include/curl/urlapi.h";
  54. my $hd = "$root/include/curl/header.h";
  55. my $verbose=0;
  56. my $summary=0;
  57. my $misses=0;
  58. my @syms;
  59. my %doc;
  60. my %rem;
  61. # scanenum runs the preprocessor on curl.h so it will process all enums
  62. # included by it, which *should* be all headers
  63. sub scanenum {
  64. my ($file) = @_;
  65. open H_IN, "-|", "$Cpreprocessor $i$file" || die "Cannot preprocess $file";
  66. while ( <H_IN> ) {
  67. if ( /enum\s+(\S+\s+)?{/ .. /}/ ) {
  68. s/^\s+//;
  69. next unless /^CURL/;
  70. chomp;
  71. s/[,\s].*//;
  72. push @syms, $_;
  73. print STDERR "$_\n";
  74. }
  75. }
  76. close H_IN || die "Error preprocessing $file";
  77. }
  78. sub scanheader {
  79. my ($f)=@_;
  80. open H, "<$f";
  81. while(<H>) {
  82. if (/^#define (CURL[A-Za-z0-9_]*)/) {
  83. push @syms, $1;
  84. }
  85. }
  86. close H;
  87. }
  88. scanenum($h);
  89. scanheader($h);
  90. scanheader($mh);
  91. scanheader($ua);
  92. scanheader($hd);
  93. open S, "<$root/docs/libcurl/symbols-in-versions";
  94. while(<S>) {
  95. if(/(^CURL[^ \n]*) *(.*)/) {
  96. my ($sym, $rest)=($1, $2);
  97. if($doc{$sym}) {
  98. print "Detected duplicate symbol: $sym\n";
  99. $misses++;
  100. next;
  101. }
  102. $doc{$sym}=$sym;
  103. my @a=split(/ +/, $rest);
  104. if($a[2]) {
  105. # this symbol is documented to have been present the last time
  106. # in this release
  107. $rem{$sym}=$a[2];
  108. }
  109. }
  110. }
  111. close S;
  112. my $ignored=0;
  113. for my $e (sort @syms) {
  114. # OBSOLETE - names that are just placeholders for a position where we
  115. # previously had a name, that is now removed. The OBSOLETE names should
  116. # never be used for anything.
  117. #
  118. # CURL_EXTERN - is a define used for libcurl functions that are external,
  119. # public. No app or other code should ever use it.
  120. #
  121. # CURLINC_ - defines for header dual-include prevention, ignore those.
  122. #
  123. # *_LAST and *_LASTENTRY are just prefix for the placeholders used for the
  124. # last entry in many enum series.
  125. #
  126. if($e =~ /(OBSOLETE|^CURL_EXTERN|^CURLINC_|_LAST\z|_LASTENTRY\z)/) {
  127. $ignored++;
  128. next;
  129. }
  130. if($doc{$e}) {
  131. if($verbose) {
  132. print $e."\n";
  133. }
  134. $doc{$e}="used";
  135. next;
  136. }
  137. else {
  138. print $e."\n";
  139. $misses++;
  140. }
  141. }
  142. #
  143. # now scan through all symbols that were present in the symbols-in-versions
  144. # but not in the headers
  145. #
  146. # If the symbols were marked 'removed' in symbols-in-versions we don't output
  147. # anything about it since that is perfectly fine.
  148. #
  149. my $anyremoved;
  150. for my $e (sort keys %doc) {
  151. if(($doc{$e} ne "used") && !$rem{$e}) {
  152. if(!$anyremoved++) {
  153. print "Missing symbols mentioned in symbols-in-versions\n";
  154. print "Add them to a header, or mark them as removed.\n";
  155. }
  156. print "$e\n";
  157. $misses++;
  158. }
  159. }
  160. if($summary) {
  161. print "Summary:\n";
  162. printf "%d symbols in headers (out of which %d are ignored)\n", scalar(@syms),
  163. $ignored;
  164. printf "%d symbols in headers are interesting\n",
  165. scalar(@syms)- $ignored;
  166. printf "%d symbols are listed in symbols-in-versions\n (out of which %d are listed as removed)\n", scalar(keys %doc), scalar(keys %rem);
  167. printf "%d symbols in symbols-in-versions should match the ones in headers\n", scalar(keys %doc) - scalar(keys %rem);
  168. }
  169. if($misses) {
  170. exit 0; # there are stuff to attend to!
  171. }
  172. else {
  173. print "OK\n";
  174. }