Weed out cruft that should not be counted and add in extensions we
[oota-llvm.git] / utils / userloc.pl
1 #!/usr/bin/perl -w
2 #
3 # Program:  userloc.pl
4 #
5 # Synopsis: This program uses "cvs annotate" to get a summary of how many lines
6 #           of code the various developres are responsible for. It takes one
7 #           argument, the directory to process. If the argument is not specified
8 #           then the cwd is used. The directory must be an LLVM tree checked out
9 #           from cvs. 
10 #
11 # Syntax:   userloc.pl [-tag=tag|-html... <directory>...
12 #
13 # Options:
14 #           -tag=tag
15 #               Use "tag" to select the revision (as per cvs -r option)
16 #           -filedetails
17 #               Report details about lines of code in each file for each user
18 #           -html
19 #               Generate HTML output instead of text output
20 # Directories:
21 #   The directories passed after the options should be relative paths to
22 #   directories of interest from the top of the llvm source tree, e.g. "lib"
23 #   or "include", etc.
24
25 die "Usage userloc.pl [-tag=tag|-html] <directories>..." 
26   if ($#ARGV < 0);
27
28 my $tag = "";
29 my $html = 0;
30 my $debug = 0;
31 my $filedetails = "";
32 while ( defined($ARGV[0]) && substr($ARGV[0],0,1) eq '-' )
33 {
34   if ($ARGV[0] =~ /-tag=.*/) {
35     $tag = $ARGV[0];
36     $tag =~ s#-tag=(.*)#$1#;
37   } elsif ($ARGV[0] =~ /-filedetails/) {
38     $filedetails = 1;
39   } elsif ($ARGV[0] eq "-html") {
40     $html = 1;
41   } elsif ($ARGV[0] eq "-debug") {
42     $debug = 1;
43   } else {
44     die "Invalid option: $ARGV[0]";
45   }
46   shift;
47 }
48
49 chomp(my $srcroot = `llvm-config --src-root`);
50 my $llvmdo = "$srcroot/utils/llvmdo";
51 my %Stats;
52 my %FileStats;
53
54 my $annotate = "cvs -z6 annotate -lf ";
55 if (length($tag) > 0)
56 {
57   $annotate = $annotate . " -r" . $tag;
58 }
59
60 sub GetCVSFiles
61 {
62   my $d = $_[0];
63   my $files ="";
64   open FILELIST, 
65     "$llvmdo -dirs \"$d\" echo |" || die "Can't get list of files with llvmdo";
66   while ( defined($line = <FILELIST>) ) {
67     chomp($file = $line);
68     $files = "$files $file";
69   }
70   return $files;
71 }
72
73 sub ScanDir
74 {
75   my $Dir = $_[0];
76   my $files = GetCVSFiles($Dir);
77
78   open (DATA,"$annotate $files 2>&1 |")
79     || die "Can't read cvs annotation data";
80
81   my $curfile = "";
82   while ( defined($line = <DATA>) )
83   {
84     chomp($line);
85     if ($line =~ '^Annotations for.*') {
86       $curfile = $line;
87       $curfile =~ s#^Annotations for ([[:print:]]*)#$1#;
88     } elsif ($line =~ /^[0-9.]*[ \t]*\([^)]*\):/) {
89       $uname = $line;
90       $uname =~ s#^[0-9.]*[ \t]*\(([a-zA-Z0-9_.-]*) [^)]*\):.*#$1#;
91       $Stats{$uname}++;
92       if ($filedetails) {
93         $FileStats{$uname} = {} unless exists $FileStats{$uname};
94         ${$FileStats{$uname}}{$curfile}++;
95       }
96     }
97   }
98   close DATA;
99 }
100
101 sub printStats
102 {
103   my $dir = $_[0];
104   my $hash = $_[1];
105   my $user;
106   my $total = 0;
107
108   foreach $user (keys %Stats) { $total += $Stats{$user}; }
109
110   if ($html) { 
111     print "<table>";
112     print " <tr><th style=\"text-align:right\">LOC</th>\n";
113     print " <th style=\"text-align:right\">\%LOC</th>\n";
114     print " <th style=\"text-align:left\">User</th>\n";
115     print "</tr>\n";
116   }
117
118   foreach $user ( sort keys %Stats )
119   {
120     my $v = $Stats{$user};
121     if (defined($v))
122     {
123       if ($html) {
124         printf "<tr><td style=\"text-align:right\">%d</td><td style=\"text-align:right\">(%4.1f%%)</td><td style=\"text-align:left\">", $v, (100.0/$total)*$v;
125         if ($filedetails) {
126           print "<a href=\"#$user\">$user</a></td></tr>";
127         } else {
128           print $user,"</td></tr>";
129         }
130       } else {
131         printf "%8d  (%4.1f%%)  %s\n", $v, (100.0/$total)*$v, $user;
132       }
133     }
134   }
135   print "</table>\n" if ($html);
136
137   if ($filedetails) {
138     foreach $user (sort keys %FileStats) {
139       my $total = 0;
140       foreach $file (sort keys %{$FileStats{$user}}) { 
141         $total += ${$FileStats{$user}}{$file}
142       }
143       if ($html) {
144         print "<table><tr><th style=\"text-align:left\" colspan=\"3\"><a name=\"$user\">$user</a></th></tr>\n";
145       } else {
146         print $user,":\n";
147       }
148       foreach $file (sort keys %{$FileStats{$user}}) {
149         my $v = ${$FileStats{$user}}{$file};
150         if ($html) { 
151           printf "<tr><td style=\"text-align:right\">&nbsp;&nbsp;%d</td><td
152           style=\"text-align:right\">&nbsp;%4.1f%%</td><td
153           style=\"text-align:left\">%s</td></tr>",$v, (100.0/$total)*$v,$file;
154         } else {
155           printf "%8d  (%4.1f%%)  %s\n", $v, (100.0/$total)*$v, $file;
156         }
157       }
158       if ($html) { print "</table>\n"; }
159     }
160   }
161 }
162
163
164 if ($html)
165 {
166 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n";
167 print "<html>\n<head>\n";
168 print "  <title>LLVM LOC Based On CVS Annotation</title>\n";
169 print "  <link rel=\"stylesheet\" href=\"llvm.css\" type=\"text/css\"/>\n";
170 print "</head>\n";
171 print "<body><div class=\"doc_title\">LLVM LOC Based On CVS Annotation</div>\n";
172 print "<p>This document shows the total lines of code per user in each\n";
173 print "LLVM directory. Lines of code are attributed by the user that last\n";
174 print "committed the line. This does not necessarily reflect authorship.</p>\n";
175 }
176
177 my @DIRS;
178 if ($#ARGV > 0) {
179   @DIRS = @ARGV;
180 } else {
181   push @DIRS, 'include';
182   push @DIRS, 'lib';
183   push @DIRS, 'tools';
184   push @DIRS, 'runtime';
185   push @DIRS, 'docs';
186   push @DIRS, 'test';
187   push @DIRS, 'utils';
188   push @DIRS, 'examples';
189   push @DIRS, 'projects/Stacker';
190   push @DIRS, 'projects/sample';
191   push @DIRS, 'autoconf';
192 }
193   
194 for $Index ( 0 .. $#DIRS) { 
195   ScanDir($DIRS[$Index]); 
196 }
197
198 printStats;
199
200 print "</body></html>\n" if ($html) ;