blob: 441f63f1c1bee1b9c368670a987c34c249e6773b [file] [log] [blame]
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001#!/usr/bin/env perl
2#
3# The LLVM Compiler Infrastructure
4#
5# This file is distributed under the University of Illinois Open Source
6# License. See LICENSE.TXT for details.
7#
8##===----------------------------------------------------------------------===##
9#
10# A script designed to wrap a build so that all calls to gcc are intercepted
11# and piped to the static analyzer.
12#
13##===----------------------------------------------------------------------===##
14
15use strict;
16use warnings;
17use File::Temp qw/ :mktemp /;
Ted Kremenek22d6a632008-04-02 20:43:36 +000018use FindBin qw($RealBin);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000019
20my $Verbose = 0; # Verbose output from this script.
21my $Prog = "scan-build";
22
23##----------------------------------------------------------------------------##
24# GetHTMLRunDir - Construct an HTML directory name for the current run.
25##----------------------------------------------------------------------------##
26
Sam Bishopa0e22662008-04-02 03:35:43 +000027sub GetHTMLRunDir {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000028
29 die "Not enough arguments." if (@_ == 0);
30
31 my $Dir = shift @_;
32
33 # Get current date and time.
34
35 my @CurrentTime = localtime();
36
37 my $year = $CurrentTime[5] + 1900;
38 my $day = $CurrentTime[3];
39 my $month = $CurrentTime[4] + 1;
40
41 my $DateString = "$year-$month-$day";
42
43 # Determine the run number.
44
45 my $RunNumber;
46
47 if (-d $Dir) {
48
49 if (! -r $Dir) {
Sam Bishopa0e22662008-04-02 03:35:43 +000050 die "error: '$Dir' exists but is not readable.\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000051 }
52
53 # Iterate over all files in the specified directory.
54
55 my $max = 0;
56
57 opendir(DIR, $Dir);
58 my @FILES= readdir(DIR);
59 closedir(DIR);
60
61 foreach my $f (@FILES) {
62
63 my @x = split/-/, $f;
64
65 next if (scalar(@x) != 4);
66 next if ($x[0] != $year);
67 next if ($x[1] != $month);
68 next if ($x[2] != $day);
69
70 if ($x[3] > $max) {
71 $max = $x[3];
72 }
73 }
74
75 $RunNumber = $max + 1;
76 }
77 else {
78
79 if (-x $Dir) {
Sam Bishopa0e22662008-04-02 03:35:43 +000080 die "error: '$Dir' exists but is not a directory.\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000081 }
82
83 # $Dir does not exist. It will be automatically created by the
84 # clang driver. Set the run number to 1.
85
86 $RunNumber = 1;
87 }
88
89 die "RunNumber must be defined!" if (!defined($RunNumber));
90
91 # Append the run number.
92
93 return "$Dir/$DateString-$RunNumber";
94}
95
Sam Bishopa0e22662008-04-02 03:35:43 +000096sub SetHtmlEnv {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000097
98 die "Wrong number of arguments." if (scalar(@_) != 2);
99
100 my $Args = shift;
101 my $Dir = shift;
102
103 die "No build command." if (scalar(@$Args) == 0);
104
105 my $Cmd = $$Args[0];
106
107 if ($Cmd =~ /configure/) {
108 return;
109 }
110
111 if ($Verbose) {
112 print "$Prog: Emitting reports for this run to '$Dir'.\n";
113 }
114
115 $ENV{'CCC_ANALYZER_HTML'} = $Dir;
116}
117
118##----------------------------------------------------------------------------##
Ted Kremenek57cf4462008-04-18 15:09:30 +0000119# ComputeDigest - Compute a digest of the specified file.
120##----------------------------------------------------------------------------##
121
122sub ComputeDigest {
123 my $FName = shift;
124 die "Cannot read $FName" if (! -r $FName);
125 my $Result = `sha1sum -b $FName`;
126 my @Output = split /\s+/,$Result;
127 die "Bad output from sha1sum" if (scalar(@Output) != 2);
128 return $Output[0];
129}
130
131##----------------------------------------------------------------------------##
Ted Kremenek5744dc22008-04-02 18:03:36 +0000132# ScanFile - Scan a report file for various identifying attributes.
133##----------------------------------------------------------------------------##
134
Ted Kremenek57cf4462008-04-18 15:09:30 +0000135# Sometimes a source file is scanned more than once, and thus produces
136# multiple error reports. We use a cache to solve this problem.
137
138my %AlreadyScanned;
139
Ted Kremenek5744dc22008-04-02 18:03:36 +0000140sub ScanFile {
141
142 my $Index = shift;
143 my $Dir = shift;
144 my $FName = shift;
145
Ted Kremenek57cf4462008-04-18 15:09:30 +0000146 # Compute a digest for the report file. Determine if we have already
147 # scanned a file that looks just like it.
148
149 my $digest = ComputeDigest("$Dir/$FName");
150
151 if (defined($AlreadyScanned{$digest})) {
152 # Redundant file. Remove it.
153 `rm -f $Dir/$FName`;
154 return;
155 }
156
157 $AlreadyScanned{$digest} = 1;
158
Ted Kremenek5744dc22008-04-02 18:03:36 +0000159 open(IN, "$Dir/$FName") or die "$Prog: Cannot open '$Dir/$FName'\n";
160
161 my $BugDesc = "";
Ted Kremenek22d6a632008-04-02 20:43:36 +0000162 my $BugFile = "";
163 my $BugPathLength = 1;
164 my $BugLine = 0;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000165
166 while (<IN>) {
167
168 if (/<!-- BUGDESC (.*) -->$/) {
169 $BugDesc = $1;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000170 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000171 elsif (/<!-- BUGFILE (.*) -->$/) {
172 $BugFile = $1;
173 }
174 elsif (/<!-- BUGPATHLENGTH (.*) -->$/) {
175 $BugPathLength = $1;
176 }
177 elsif (/<!-- BUGLINE (.*) -->$/) {
178 $BugLine = $1;
179 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000180 }
181
182 close(IN);
183
Ted Kremenek22d6a632008-04-02 20:43:36 +0000184 push @$Index,[ $FName, $BugDesc, $BugFile, $BugLine, $BugPathLength ];
185}
186
187##----------------------------------------------------------------------------##
188# CopyJS - Copy JavaScript code to target directory.
189##----------------------------------------------------------------------------##
190
191sub CopyJS {
192
193 my $Dir = shift;
194
195 die "$Prog: Cannot find 'sorttable.js'.\n"
196 if (! -r "$RealBin/sorttable.js");
197
198 `cp $RealBin/sorttable.js $Dir`;
199
200 die "$Prog: Could not copy 'sorttable.js' to '$Dir'."
201 if (! -r "$Dir/sorttable.js");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000202}
203
204##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000205# Postprocess - Postprocess the results of an analysis scan.
206##----------------------------------------------------------------------------##
207
Sam Bishopa0e22662008-04-02 03:35:43 +0000208sub Postprocess {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000209
210 my $Dir = shift;
211
212 die "No directory specified." if (!defined($Dir));
213
214 if (! -d $Dir) {
215 return;
216 }
217
218 opendir(DIR, $Dir);
219 my @files = grep(/^report-.*\.html$/,readdir(DIR));
220 closedir(DIR);
221
222 if (scalar(@files) == 0) {
Ted Kremenek02493782008-04-02 07:05:07 +0000223 print "$Prog: Removing directory '$Dir' because it contains no reports.\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000224 `rm -fR $Dir`;
225 return;
226 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000227
228 # Scan each report file and build an index.
229
230 my @Index;
231
232 foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); }
233
234 # Generate an index.html file.
235
236 my $FName = "$Dir/index.html";
237
238 open(OUT, ">$FName") or die "$Prog: Cannot create file '$FName'\n";
239
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000240 # Print out the header.
241
Ted Kremenek5744dc22008-04-02 18:03:36 +0000242print OUT <<ENDTEXT;
243<html>
244<head>
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000245<style type="text/css">
246 body { color:#000000; background-color:#ffffff }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000247 body { font-family: Helvetica, sans-serif; font-size:9pt }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000248 h1 { font-size:12pt }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000249 table.sortable thead {
250 background-color:#eee; color:#666666;
251 font-weight: bold; cursor: default;
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000252 text-align:center;
253 border-top: 2px solid #000000;
254 border-bottom: 2px solid #000000;
255 font-weight: bold; font-family: Verdana
256 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000257 table.sortable { border: 1px #000000 solid }
258 table.sortable { border-collapse: collapse; border-spacing: 0px }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000259 td { border-bottom: 1px #000000 dotted }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000260 td { padding:5px; padding-left:8px; padding-right:8px }
Ted Kremenekd8c6d0c2008-04-07 23:50:07 +0000261 td { text-align:left; font-size:9pt }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000262 td.View { padding-left: 10px }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000263</style>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000264<script src="sorttable.js"></script>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000265<script language='javascript' type="text/javascript">
266function SetDisplay(RowClass, DisplayVal)
267{
268 var Rows = document.getElementsByTagName("tr");
269 for ( var i = 0 ; i < Rows.length; ++i ) {
270 if (Rows[i].className == RowClass) {
271 Rows[i].style.display = DisplayVal;
272 }
273 }
274}
275
276function ToggleDisplay(CheckButton, ClassName) {
277 window.console.log("writing");
278 if (CheckButton.checked) {
279 SetDisplay(ClassName, "");
280 }
281 else {
282 SetDisplay(ClassName, "none");
283 }
284}
285</script>
286</head>
287<body>
288ENDTEXT
289
290 # Print out the summary table.
291
292 my %Totals;
293
294 for my $row ( @Index ) {
295
296 my $bug_type = lc($row->[1]);
297
298 if (!defined($Totals{$bug_type})) {
299 $Totals{$bug_type} = 1;
300 }
301 else {
302 $Totals{$bug_type}++;
303 }
304 }
305
306print OUT <<ENDTEXT;
307<h3>Summary</h3>
308<table class="sortable">
309<tr>
310 <td>Bug Type</td>
311 <td>Quantity</td>
312 <td "sorttable_nosort">Display?</td>
313</tr>
314ENDTEXT
315
316 for my $key ( sort { $a cmp $b } keys %Totals ) {
317 my $x = $key;
318 $x =~ s/\s/_/g;
319 print OUT "<tr><td>$key</td><td>$Totals{$key}</td><td><input type=\"checkbox\" onClick=\"ToggleDisplay(this,'bt_$x');\" checked/></td></tr>\n";
320 }
321
322 # Print out the table of errors.
323
324print OUT <<ENDTEXT;
325</table>
326<h3>Reports</h3>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000327<table class="sortable">
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000328<tr>
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000329 <td>Bug Type</td>
330 <td>File</td>
331 <td>Line</td>
332 <td>Path Length</td>
333 <td "sorttable_nosort"></td>
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000334</tr>
Ted Kremenek5744dc22008-04-02 18:03:36 +0000335ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000336
Ted Kremenek5744dc22008-04-02 18:03:36 +0000337 for my $row ( sort { $a->[1] cmp $b->[1] } @Index ) {
338
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000339 my $x = lc($row->[1]);
340 $x =~ s/\s/_/g;
341
342 print OUT "<tr class=\"bt_$x\">\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000343
Ted Kremenek5744dc22008-04-02 18:03:36 +0000344 my $ReportFile = $row->[0];
345
Ted Kremenek22d6a632008-04-02 20:43:36 +0000346 print OUT " <td class=\"DESC\">";
347 print OUT lc($row->[1]);
348 print OUT "</td>\n";
Ted Kremenek5744dc22008-04-02 18:03:36 +0000349
350 for my $j ( 2 .. $#{$row} ) {
351 print OUT "<td>$row->[$j]</td>\n"
352 }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000353
354 # Emit the "View" link.
Ted Kremenek5744dc22008-04-02 18:03:36 +0000355
Ted Kremenek22d6a632008-04-02 20:43:36 +0000356 print OUT " <td class=\"View\"><a href=\"$ReportFile#EndPath\">View</a></td>\n";
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000357
358 # End the row.
Ted Kremenek5744dc22008-04-02 18:03:36 +0000359 print OUT "</tr>\n";
360 }
361
362 print OUT "</table>\n</body></html>\n";
363 close(OUT);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000364
Ted Kremenek22d6a632008-04-02 20:43:36 +0000365 CopyJS($Dir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000366}
367
368##----------------------------------------------------------------------------##
Ted Kremenekdab11102008-04-02 04:43:42 +0000369# RunBuildCommand - Run the build command.
370##----------------------------------------------------------------------------##
371
372sub RunBuildCommand {
373
374 my $Args = shift;
Ted Kremenek7442ca62008-04-02 16:04:51 +0000375 my $IgnoreErrors = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000376 my $Cmd = $Args->[0];
377
Ted Kremenek6a43ba92008-04-02 15:34:12 +0000378 if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc") {
Ted Kremenekdab11102008-04-02 04:43:42 +0000379 shift @$Args;
380 unshift @$Args, "ccc-analyzer"
381 }
Ted Kremenek7442ca62008-04-02 16:04:51 +0000382 elsif ($IgnoreErrors) {
383 if ($Cmd eq "make" or $Cmd eq "gmake") {
384 push @$Args, "-k";
385 }
386 elsif ($Cmd eq "xcodebuild") {
387 push @$Args, "-PBXBuildsContinueAfterErrors=YES";
388 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000389 }
Ted Kremenekdab11102008-04-02 04:43:42 +0000390
391 system(@$Args);
392}
393
Ted Kremenekdab11102008-04-02 04:43:42 +0000394##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000395# DisplayHelp - Utility function to display all help options.
396##----------------------------------------------------------------------------##
397
Sam Bishopa0e22662008-04-02 03:35:43 +0000398sub DisplayHelp {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000399
Ted Kremenek5744dc22008-04-02 18:03:36 +0000400print <<ENDTEXT;
Sam Bishopa0e22662008-04-02 03:35:43 +0000401USAGE: $Prog [options] <build command> [build options]
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000402
403OPTIONS:
404
405 -o - Target directory for HTML report files. Subdirectories
Sam Bishopa0e22662008-04-02 03:35:43 +0000406 will be created as needed to represent separate "runs" of
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000407 the analyzer. If this option is not specified, a directory
408 is created in /tmp to store the reports.
409
Ted Kremenek10f883f2008-04-03 07:11:44 +0000410 -h - Display this message.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000411 --help
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000412
Ted Kremenekaf08f642008-04-02 16:31:58 +0000413 -k - Add a "keep on going" option to the specified build command.
Ted Kremenekf02e8db2008-04-02 16:41:25 +0000414 --keep-going This option currently supports make and xcodebuild.
415 This is a convenience option; one can specify this
416 behavior directly using build options.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000417
Ted Kremenekdab11102008-04-02 04:43:42 +0000418 -v - Verbose output from $Prog and the analyzer.
419 A second "-v" increases verbosity.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000420
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000421 -V - View analysis results in a web browser when the build
422 --view completes.
423
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000424BUILD OPTIONS
425
Ted Kremenek39eefde2008-04-02 16:47:27 +0000426 You can specify any build option acceptable to the build command.
427
Ted Kremenek5744dc22008-04-02 18:03:36 +0000428EXAMPLE
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000429
Ted Kremenek5744dc22008-04-02 18:03:36 +0000430 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000431
Ted Kremenek39eefde2008-04-02 16:47:27 +0000432 The above example causes analysis reports to be deposited into
433 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
434 A different subdirectory is created each time $Prog analyzes a project.
435 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000436
437ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000438}
439
440##----------------------------------------------------------------------------##
441# Process command-line arguments.
442##----------------------------------------------------------------------------##
443
444my $HtmlDir; # Parent directory to store HTML files.
445my $IgnoreErrors = 0; # Ignore build errors.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000446my $ViewResults = 0; # View results when the build terminates.
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000447
448if (!@ARGV) {
449 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000450 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000451}
452
453while (@ARGV) {
454
455 # Scan for options we recognize.
456
457 my $arg = $ARGV[0];
458
Sam Bishop2f2418e2008-04-03 14:29:47 +0000459 if ($arg eq "-h" or $arg eq "--help") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000460 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000461 exit 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000462 }
463
464 if ($arg eq "-o") {
465 shift @ARGV;
466
467 if (!@ARGV) {
Ted Kremenek0062ad42008-04-02 16:35:01 +0000468 die "$Prog: '-o' option requires a target directory name.\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000469 }
470
471 $HtmlDir = shift @ARGV;
472 next;
473 }
474
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000475 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000476 shift @ARGV;
477 $IgnoreErrors = 1;
478 next;
479 }
480
481 if ($arg eq "-v") {
482 shift @ARGV;
483 $Verbose++;
484 next;
485 }
486
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000487 if ($arg eq "-V" or $arg eq "--view") {
488 shift @ARGV;
489 $ViewResults = 1;
490 next;
491 }
492
Ted Kremenek0062ad42008-04-02 16:35:01 +0000493 die "$Prog: unrecognized option '$arg'\n" if ($arg =~ /^-/);
494
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000495 last;
496}
497
498if (!@ARGV) {
499 print STDERR "$Prog: No build command specified.\n\n";
500 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000501 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000502}
503
504# Determine the output directory for the HTML reports.
505
506if (!defined($HtmlDir)) {
507
Sam Bishopa0e22662008-04-02 03:35:43 +0000508 $HtmlDir = mkdtemp("/tmp/$Prog-XXXXXX");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000509
510 if (!defined($HtmlDir)) {
Sam Bishopa0e22662008-04-02 03:35:43 +0000511 die "error: Cannot create HTML directory in /tmp.\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000512 }
513
514 if (!$Verbose) {
515 print "$Prog: Using '$HtmlDir' as base HTML report directory.\n";
516 }
517}
518
Sam Bishopa0e22662008-04-02 03:35:43 +0000519$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000520
521# Set the appropriate environment variables.
522
Sam Bishopa0e22662008-04-02 03:35:43 +0000523SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000524
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000525my $Cmd = "$RealBin/ccc-analyzer";
526
527die "$Prog: Executable 'ccc-analyzer' does not exist at '$Cmd'\n"
528 if (! -x $Cmd);
529
Ted Kremenek4f4b17d2008-04-03 20:08:18 +0000530$ENV{'CC'} = $Cmd;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000531
532if ($Verbose >= 2) {
533 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
534}
535
536# Run the build.
537
Ted Kremenek7442ca62008-04-02 16:04:51 +0000538RunBuildCommand(\@ARGV, $IgnoreErrors);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000539
540# Postprocess the HTML directory.
541
Sam Bishopa0e22662008-04-02 03:35:43 +0000542Postprocess($HtmlDir);
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000543
544if ($ViewResults and -r "$HtmlDir/index.html") {
545 # Only works on Mac OS X (for now).
546 print "Viewing analysis results: '$HtmlDir/index.html'\n";
547 `open $HtmlDir/index.html`
548}