symbol-scan.pl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #!/usr/bin/env perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) 2010 - 2020, 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 on freenode.
  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 $verbose=0;
  53. my $summary=0;
  54. my $misses=0;
  55. my @syms;
  56. my %doc;
  57. my %rem;
  58. open H_IN, "-|", "$Cpreprocessor $i$h" || die "Cannot preprocess curl.h";
  59. while ( <H_IN> ) {
  60. if ( /enum\s+(\S+\s+)?{/ .. /}/ ) {
  61. s/^\s+//;
  62. next unless /^CURL/;
  63. chomp;
  64. s/[,\s].*//;
  65. push @syms, $_;
  66. }
  67. }
  68. close H_IN || die "Error preprocessing curl.h";
  69. sub scanheader {
  70. my ($f)=@_;
  71. open H, "<$f";
  72. while(<H>) {
  73. if (/^#define (CURL[A-Za-z0-9_]*)/) {
  74. push @syms, $1;
  75. }
  76. }
  77. close H;
  78. }
  79. scanheader($h);
  80. scanheader($mh);
  81. scanheader($ua);
  82. open S, "<$root/docs/libcurl/symbols-in-versions";
  83. while(<S>) {
  84. if(/(^CURL[^ \n]*) *(.*)/) {
  85. my ($sym, $rest)=($1, $2);
  86. if($doc{$sym}) {
  87. print "Detected duplicate symbol: $sym\n";
  88. $misses++;
  89. next;
  90. }
  91. $doc{$sym}=$sym;
  92. my @a=split(/ +/, $rest);
  93. if($a[2]) {
  94. # this symbol is documented to have been present the last time
  95. # in this release
  96. $rem{$sym}=$a[2];
  97. }
  98. }
  99. }
  100. close S;
  101. my $ignored=0;
  102. for my $e (sort @syms) {
  103. # OBSOLETE - names that are just placeholders for a position where we
  104. # previously had a name, that is now removed. The OBSOLETE names should
  105. # never be used for anything.
  106. #
  107. # CURL_EXTERN - is a define used for libcurl functions that are external,
  108. # public. No app or other code should ever use it.
  109. #
  110. # CURLINC_ - defines for header dual-include prevention, ignore those.
  111. #
  112. # *_LAST and *_LASTENTRY are just prefix for the placeholders used for the
  113. # last entry in many enum series.
  114. #
  115. if($e =~ /(OBSOLETE|^CURL_EXTERN|^CURLINC_|_LAST\z|_LASTENTRY\z)/) {
  116. $ignored++;
  117. next;
  118. }
  119. if($doc{$e}) {
  120. if($verbose) {
  121. print $e."\n";
  122. }
  123. $doc{$e}="used";
  124. next;
  125. }
  126. else {
  127. print $e."\n";
  128. $misses++;
  129. }
  130. }
  131. #
  132. # now scan through all symbols that were present in the symbols-in-versions
  133. # but not in the headers
  134. #
  135. # If the symbols were marked 'removed' in symbols-in-versions we don't output
  136. # anything about it since that is perfectly fine.
  137. #
  138. my $anyremoved;
  139. for my $e (sort keys %doc) {
  140. if(($doc{$e} ne "used") && !$rem{$e}) {
  141. if(!$anyremoved++) {
  142. print "Missing symbols mentioned in symbols-in-versions\n";
  143. print "Add them to a header, or mark them as removed.\n";
  144. }
  145. print "$e\n";
  146. $misses++;
  147. }
  148. }
  149. if($summary) {
  150. print "Summary:\n";
  151. printf "%d symbols in headers (out of which %d are ignored)\n", scalar(@syms),
  152. $ignored;
  153. printf "%d symbols in headers are interesting\n",
  154. scalar(@syms)- $ignored;
  155. 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);
  156. printf "%d symbols in symbols-in-versions should match the ones in headers\n", scalar(keys %doc) - scalar(keys %rem);
  157. }
  158. if($misses) {
  159. exit 0; # there are stuff to attend to!
  160. }
  161. else {
  162. print "OK\n";
  163. }