symbol-scan.pl 4.6 KB

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