2
0

badsymbols.pl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/usr/bin/env perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) 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 -DCURL_DISABLE_DEPRECATION $i$file" ||
  67. die "Cannot preprocess $file";
  68. while ( <H_IN> ) {
  69. my ($line, $linenum) = ($_, $.);
  70. if( /^#(line|) (\d+) \"(.*)\"/) {
  71. # if the included file isn't in our incdir, then we skip this section
  72. # until next #line
  73. #
  74. if($3 !~ /^$incdir/) {
  75. $skipit = 1;
  76. next;
  77. }
  78. # parse this!
  79. $skipit = 0;
  80. next;
  81. }
  82. if($skipit) {
  83. next;
  84. }
  85. if (/^#/) {
  86. next;
  87. }
  88. if ( /enum\s+(\S+\s+)?{/ .. /}/ ) {
  89. s/^\s+//;
  90. chomp;
  91. s/[,\s].*//;
  92. if(($_ !~ /\}(;|)/) &&
  93. ($_ ne "typedef") &&
  94. ($_ ne "enum") &&
  95. ($_ ne "=") &&
  96. ($_ !~ /^[ \t]*$/)) {
  97. if($verbose) {
  98. print "Source: $Cpreprocessor $i$file\n";
  99. print "Symbol: $_\n";
  100. print "Line #$linenum: $line\n\n";
  101. }
  102. push @syms, $_;
  103. }
  104. }
  105. }
  106. close H_IN || die "Error preprocessing $file";
  107. }
  108. sub scanheader {
  109. my ($f)=@_;
  110. scanenums($f);
  111. open H, "<$f";
  112. while(<H>) {
  113. my ($line, $linenum) = ($_, $.);
  114. if (/^#define +([^ \n]*)/) {
  115. if($verbose) {
  116. print "Source: $f\n";
  117. print "Symbol: $1\n";
  118. print "Line #$linenum: $line\n\n";
  119. }
  120. push @syms, $1;
  121. }
  122. }
  123. close H;
  124. }
  125. opendir(my $dh, $incdir) || die "Can't opendir $incdir: $!";
  126. my @hfiles = grep { /\.h$/ } readdir($dh);
  127. closedir $dh;
  128. for(@hfiles) {
  129. scanheader("$incdir/$_");
  130. }
  131. my $errors = 0;
  132. for my $s (@syms) {
  133. if($s !~ /^(lib|)curl/i) {
  134. print "Bad symbols in public header files:\n" if(!$errors);
  135. $errors++;
  136. print " $s\n";
  137. }
  138. }
  139. if($errors) {
  140. exit 1;
  141. }
  142. printf "%d fine symbols found\n", scalar(@syms);