badsymbols.pl 3.9 KB

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