finddeclarations.pl 985 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. if ($ARGV[0] eq '--help') {
  5. print << "EOF";
  6. Usage:
  7. $0 definitions.c
  8. EOF
  9. exit;
  10. }
  11. my ($cfname, $sfname, $gfname, $cpp) = @ARGV;
  12. my $F;
  13. open $F, "<", $cfname;
  14. my $text = join "", <$F>;
  15. close $F;
  16. my $s = qr/(?>\s*)/aso;
  17. my $w = qr/(?>\w+)/aso;
  18. my $argname = qr/$w(?:\[(?>\w+)\])?/aso;
  19. my $type_regex = qr/(?:$w$s\**$s)+/aso;
  20. my $arg_regex = qr/(?:$type_regex$s$argname)/aso;
  21. while ($text =~ /
  22. (?<=\n) # Definition starts at the start of line
  23. $type_regex # Return type
  24. $s$w # Function name
  25. $s\($s
  26. (?:
  27. $arg_regex(?:$s,$s$arg_regex)*+
  28. ($s,$s\.\.\.)? # varargs function
  29. |void
  30. )?
  31. $s\)
  32. (?:$s FUNC_ATTR_$w(?:\((?>[^)]*)\))?)*+ # Optional attributes
  33. (?=$s;) # Ending semicolon
  34. /axsogp) {
  35. my $match = "${^MATCH}";
  36. my $s = "${^PREMATCH}";
  37. $s =~ s/[^\n]++//g;
  38. my $line = 1 + length $s;
  39. print "${cfname}:${line}: $match\n";
  40. }