PageRenderTime 73ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/source4/script/findstatic.pl

http://github.com/ccrisan/samba
Perl | 70 lines | 52 code | 9 blank | 9 comment | 9 complexity | 20371234525f14ed8ed014b3b1c6adad MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, LGPL-2.1
  1. #!/usr/bin/perl -w
  2. # find a list of fns and variables in the code that could be static
  3. # usually called with something like this:
  4. # findstatic.pl `find . -name "*.o"`
  5. # Andrew Tridgell <tridge@samba.org>
  6. use strict;
  7. # use nm to find the symbols
  8. my($saved_delim) = $/;
  9. undef $/;
  10. my($syms) = `nm -o @ARGV`;
  11. $/ = $saved_delim;
  12. my(@lines) = split(/\n/s, $syms);
  13. my(%def);
  14. my(%undef);
  15. my(%stype);
  16. my(%typemap) = (
  17. "T" => "function",
  18. "C" => "uninitialised variable",
  19. "D" => "initialised variable"
  20. );
  21. # parse the symbols into defined and undefined
  22. for (my($i)=0; $i <= $#{@lines}; $i++) {
  23. my($line) = $lines[$i];
  24. if ($line =~ /(.*):[a-f0-9]* ([TCD]) (.*)/) {
  25. my($fname) = $1;
  26. my($symbol) = $3;
  27. push(@{$def{$fname}}, $symbol);
  28. $stype{$symbol} = $2;
  29. }
  30. if ($line =~ /(.*):\s* U (.*)/) {
  31. my($fname) = $1;
  32. my($symbol) = $2;
  33. push(@{$undef{$fname}}, $symbol);
  34. }
  35. }
  36. # look for defined symbols that are never referenced outside the place they
  37. # are defined
  38. foreach my $f (keys %def) {
  39. print "Checking $f\n";
  40. my($found_one) = 0;
  41. foreach my $s (@{$def{$f}}) {
  42. my($found) = 0;
  43. foreach my $f2 (keys %undef) {
  44. if ($f2 ne $f) {
  45. foreach my $s2 (@{$undef{$f2}}) {
  46. if ($s2 eq $s) {
  47. $found = 1;
  48. $found_one = 1;
  49. }
  50. }
  51. }
  52. }
  53. if ($found == 0) {
  54. my($t) = $typemap{$stype{$s}};
  55. print " '$s' is unique to $f ($t)\n";
  56. }
  57. }
  58. if ($found_one == 0) {
  59. print " all symbols in '$f' are unused (main program?)\n";
  60. }
  61. }