symbol-scan.pl 5.1 KB

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