badsymbols.pl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.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 $incdir = "$root/include/curl";
  50. my $verbose=0;
  51. my $summary=0;
  52. my $misses=0;
  53. my @syms;
  54. my %doc;
  55. my %rem;
  56. sub scanenums {
  57. my ($file)=@_;
  58. my $skipit = 0;
  59. open H_IN, "-|", "$Cpreprocessor $i$file" || die "Cannot preprocess $file";
  60. while ( <H_IN> ) {
  61. if( /^#(line|) (\d+) \"(.*)\"/) {
  62. # if the included file isn't in our incdir, then we skip this section
  63. # until next #line
  64. #
  65. if($3 !~ /^$incdir/) {
  66. $skipit = 1;
  67. next;
  68. }
  69. # parse this!
  70. $skipit = 0,
  71. }
  72. if($skipit) {
  73. next;
  74. }
  75. if ( /enum\s+(\S+\s+)?{/ .. /}/ ) {
  76. s/^\s+//;
  77. chomp;
  78. s/[,\s].*//;
  79. if(($_ !~ /\}(;|)/) &&
  80. ($_ ne "typedef") &&
  81. ($_ ne "enum") &&
  82. ($_ !~ /^[ \t]*$/) &&
  83. ($_ ne "#")) {
  84. push @syms, $_;
  85. }
  86. }
  87. }
  88. close H_IN || die "Error preprocessing $file";
  89. }
  90. sub scanheader {
  91. my ($f)=@_;
  92. scanenums($f);
  93. open H, "<$f";
  94. while(<H>) {
  95. if (/^#define +([^ \n]*)/) {
  96. push @syms, $1;
  97. }
  98. }
  99. close H;
  100. }
  101. opendir(my $dh, $incdir) || die "Can't opendir: $!";
  102. my @hfiles = grep { /\.h$/ } readdir($dh);
  103. closedir $dh;
  104. for(@hfiles) {
  105. scanheader("$incdir/$_");
  106. }
  107. my $errors = 0;
  108. for my $s (@syms) {
  109. if($s !~ /^(lib|)curl/i) {
  110. print "Bad symbols in public header files:\n" if(!$errors);
  111. $errors++;
  112. print " $s\n";
  113. }
  114. }
  115. if($errors) {
  116. exit 1;
  117. }
  118. printf "%d fine symbols found\n", scalar(@syms);