badsymbols.pl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/usr/bin/env perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) 2010-2021, 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. my $verbose=0;
  46. # verbose mode when -v is the first argument
  47. if($ARGV[0] eq "-v") {
  48. $verbose=1;
  49. shift;
  50. }
  51. # we may get the dir root pointed out
  52. my $root=$ARGV[0] || ".";
  53. # need an include directory when building out-of-tree
  54. my $i = ($ARGV[1]) ? "-I$ARGV[1] " : '';
  55. my $incdir = "$root/include/curl";
  56. my $summary=0;
  57. my $misses=0;
  58. my @syms;
  59. my %doc;
  60. my %rem;
  61. sub scanenums {
  62. my ($file)=@_;
  63. my $skipit = 0;
  64. open H_IN, "-|", "$Cpreprocessor $i$file" || die "Cannot preprocess $file";
  65. while ( <H_IN> ) {
  66. my ($line, $linenum) = ($_, $.);
  67. if( /^#(line|) (\d+) \"(.*)\"/) {
  68. # if the included file isn't in our incdir, then we skip this section
  69. # until next #line
  70. #
  71. if($3 !~ /^$incdir/) {
  72. $skipit = 1;
  73. next;
  74. }
  75. # parse this!
  76. $skipit = 0;
  77. next;
  78. }
  79. if($skipit) {
  80. next;
  81. }
  82. if (/^#/) {
  83. next;
  84. }
  85. if ( /enum\s+(\S+\s+)?{/ .. /}/ ) {
  86. s/^\s+//;
  87. chomp;
  88. s/[,\s].*//;
  89. if(($_ !~ /\}(;|)/) &&
  90. ($_ ne "typedef") &&
  91. ($_ ne "enum") &&
  92. ($_ !~ /^[ \t]*$/)) {
  93. if($verbose) {
  94. print "Source: $Cpreprocessor $i$file\n";
  95. print "Symbol: $_\n";
  96. print "Line #$linenum: $line\n\n";
  97. }
  98. push @syms, $_;
  99. }
  100. }
  101. }
  102. close H_IN || die "Error preprocessing $file";
  103. }
  104. sub scanheader {
  105. my ($f)=@_;
  106. scanenums($f);
  107. open H, "<$f";
  108. while(<H>) {
  109. my ($line, $linenum) = ($_, $.);
  110. if (/^#define +([^ \n]*)/) {
  111. if($verbose) {
  112. print "Source: $f\n";
  113. print "Symbol: $1\n";
  114. print "Line #$linenum: $line\n\n";
  115. }
  116. push @syms, $1;
  117. }
  118. }
  119. close H;
  120. }
  121. opendir(my $dh, $incdir) || die "Can't opendir $incdir: $!";
  122. my @hfiles = grep { /\.h$/ } readdir($dh);
  123. closedir $dh;
  124. for(@hfiles) {
  125. scanheader("$incdir/$_");
  126. }
  127. my $errors = 0;
  128. for my $s (@syms) {
  129. if($s !~ /^(lib|)curl/i) {
  130. print "Bad symbols in public header files:\n" if(!$errors);
  131. $errors++;
  132. print " $s\n";
  133. }
  134. }
  135. if($errors) {
  136. exit 1;
  137. }
  138. printf "%d fine symbols found\n", scalar(@syms);