blob: f6d894b2f239d415c6759a8bda27d86390a5674a [file] [log] [blame]
Ted Kremenekc9d8fde2008-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 Kremenekd5d43622008-04-02 20:43:36 +000018use FindBin qw($RealBin);
Ted Kremenekc9d8fde2008-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 Bishop479982e2008-04-02 03:35:43 +000027sub GetHTMLRunDir {
Ted Kremenekc9d8fde2008-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 Bishop479982e2008-04-02 03:35:43 +000050 die "error: '$Dir' exists but is not readable.\n";
Ted Kremenekc9d8fde2008-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 Bishop479982e2008-04-02 03:35:43 +000080 die "error: '$Dir' exists but is not a directory.\n";
Ted Kremenekc9d8fde2008-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 Bishop479982e2008-04-02 03:35:43 +000096sub SetHtmlEnv {
Ted Kremenekc9d8fde2008-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 Kremeneked757e02008-04-02 18:03:36 +0000119# ScanFile - Scan a report file for various identifying attributes.
120##----------------------------------------------------------------------------##
121
122sub ScanFile {
123
124 my $Index = shift;
125 my $Dir = shift;
126 my $FName = shift;
127
128 open(IN, "$Dir/$FName") or die "$Prog: Cannot open '$Dir/$FName'\n";
129
130 my $BugDesc = "";
Ted Kremenekd5d43622008-04-02 20:43:36 +0000131 my $BugFile = "";
132 my $BugPathLength = 1;
133 my $BugLine = 0;
Ted Kremeneked757e02008-04-02 18:03:36 +0000134
135 while (<IN>) {
136
137 if (/<!-- BUGDESC (.*) -->$/) {
138 $BugDesc = $1;
Ted Kremeneked757e02008-04-02 18:03:36 +0000139 }
Ted Kremenekd5d43622008-04-02 20:43:36 +0000140 elsif (/<!-- BUGFILE (.*) -->$/) {
141 $BugFile = $1;
142 }
143 elsif (/<!-- BUGPATHLENGTH (.*) -->$/) {
144 $BugPathLength = $1;
145 }
146 elsif (/<!-- BUGLINE (.*) -->$/) {
147 $BugLine = $1;
148 }
Ted Kremeneked757e02008-04-02 18:03:36 +0000149 }
150
151 close(IN);
152
Ted Kremenekd5d43622008-04-02 20:43:36 +0000153 push @$Index,[ $FName, $BugDesc, $BugFile, $BugLine, $BugPathLength ];
154}
155
156##----------------------------------------------------------------------------##
157# CopyJS - Copy JavaScript code to target directory.
158##----------------------------------------------------------------------------##
159
160sub CopyJS {
161
162 my $Dir = shift;
163
164 die "$Prog: Cannot find 'sorttable.js'.\n"
165 if (! -r "$RealBin/sorttable.js");
166
167 `cp $RealBin/sorttable.js $Dir`;
168
169 die "$Prog: Could not copy 'sorttable.js' to '$Dir'."
170 if (! -r "$Dir/sorttable.js");
Ted Kremeneked757e02008-04-02 18:03:36 +0000171}
172
173##----------------------------------------------------------------------------##
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000174# Postprocess - Postprocess the results of an analysis scan.
175##----------------------------------------------------------------------------##
176
Sam Bishop479982e2008-04-02 03:35:43 +0000177sub Postprocess {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000178
179 my $Dir = shift;
180
181 die "No directory specified." if (!defined($Dir));
182
183 if (! -d $Dir) {
184 return;
185 }
186
187 opendir(DIR, $Dir);
188 my @files = grep(/^report-.*\.html$/,readdir(DIR));
189 closedir(DIR);
190
191 if (scalar(@files) == 0) {
Ted Kremenek4efca192008-04-02 07:05:07 +0000192 print "$Prog: Removing directory '$Dir' because it contains no reports.\n";
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000193 `rm -fR $Dir`;
194 return;
195 }
Ted Kremeneked757e02008-04-02 18:03:36 +0000196
197 # Scan each report file and build an index.
198
199 my @Index;
200
201 foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); }
202
203 # Generate an index.html file.
204
205 my $FName = "$Dir/index.html";
206
207 open(OUT, ">$FName") or die "$Prog: Cannot create file '$FName'\n";
208
Ted Kremenek4de0c562008-04-15 20:47:02 +0000209 # Print out the header.
210
Ted Kremeneked757e02008-04-02 18:03:36 +0000211print OUT <<ENDTEXT;
212<html>
213<head>
Ted Kremenekb59b7542008-04-02 18:42:49 +0000214<style type="text/css">
215 body { color:#000000; background-color:#ffffff }
Ted Kremenekd5d43622008-04-02 20:43:36 +0000216 body { font-family: Helvetica, sans-serif; font-size:9pt }
Ted Kremenekb59b7542008-04-02 18:42:49 +0000217 h1 { font-size:12pt }
Ted Kremenekd5d43622008-04-02 20:43:36 +0000218 table.sortable thead {
219 background-color:#eee; color:#666666;
220 font-weight: bold; cursor: default;
Ted Kremenek38471832008-04-03 05:50:51 +0000221 text-align:center;
222 border-top: 2px solid #000000;
223 border-bottom: 2px solid #000000;
224 font-weight: bold; font-family: Verdana
225 }
Ted Kremenekd5d43622008-04-02 20:43:36 +0000226 table.sortable { border: 1px #000000 solid }
227 table.sortable { border-collapse: collapse; border-spacing: 0px }
Ted Kremenekb59b7542008-04-02 18:42:49 +0000228 td { border-bottom: 1px #000000 dotted }
Ted Kremenekd5d43622008-04-02 20:43:36 +0000229 td { padding:5px; padding-left:8px; padding-right:8px }
Ted Kremenek756515f2008-04-07 23:50:07 +0000230 td { text-align:left; font-size:9pt }
Ted Kremenekd5d43622008-04-02 20:43:36 +0000231 td.View { padding-left: 10px }
Ted Kremenekb59b7542008-04-02 18:42:49 +0000232</style>
Ted Kremenekd5d43622008-04-02 20:43:36 +0000233<script src="sorttable.js"></script>
Ted Kremenek4de0c562008-04-15 20:47:02 +0000234<script language='javascript' type="text/javascript">
235function SetDisplay(RowClass, DisplayVal)
236{
237 var Rows = document.getElementsByTagName("tr");
238 for ( var i = 0 ; i < Rows.length; ++i ) {
239 if (Rows[i].className == RowClass) {
240 Rows[i].style.display = DisplayVal;
241 }
242 }
243}
244
245function ToggleDisplay(CheckButton, ClassName) {
246 window.console.log("writing");
247 if (CheckButton.checked) {
248 SetDisplay(ClassName, "");
249 }
250 else {
251 SetDisplay(ClassName, "none");
252 }
253}
254</script>
255</head>
256<body>
257ENDTEXT
258
259 # Print out the summary table.
260
261 my %Totals;
262
263 for my $row ( @Index ) {
264
265 my $bug_type = lc($row->[1]);
266
267 if (!defined($Totals{$bug_type})) {
268 $Totals{$bug_type} = 1;
269 }
270 else {
271 $Totals{$bug_type}++;
272 }
273 }
274
275print OUT <<ENDTEXT;
276<h3>Summary</h3>
277<table class="sortable">
278<tr>
279 <td>Bug Type</td>
280 <td>Quantity</td>
281 <td "sorttable_nosort">Display?</td>
282</tr>
283ENDTEXT
284
285 for my $key ( sort { $a cmp $b } keys %Totals ) {
286 my $x = $key;
287 $x =~ s/\s/_/g;
288 print OUT "<tr><td>$key</td><td>$Totals{$key}</td><td><input type=\"checkbox\" onClick=\"ToggleDisplay(this,'bt_$x');\" checked/></td></tr>\n";
289 }
290
291 # Print out the table of errors.
292
293print OUT <<ENDTEXT;
294</table>
295<h3>Reports</h3>
Ted Kremenekd5d43622008-04-02 20:43:36 +0000296<table class="sortable">
Ted Kremenekb59b7542008-04-02 18:42:49 +0000297<tr>
Ted Kremenek38471832008-04-03 05:50:51 +0000298 <td>Bug Type</td>
299 <td>File</td>
300 <td>Line</td>
301 <td>Path Length</td>
302 <td "sorttable_nosort"></td>
Ted Kremenekb59b7542008-04-02 18:42:49 +0000303</tr>
Ted Kremeneked757e02008-04-02 18:03:36 +0000304ENDTEXT
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000305
Ted Kremeneked757e02008-04-02 18:03:36 +0000306 for my $row ( sort { $a->[1] cmp $b->[1] } @Index ) {
307
Ted Kremenek4de0c562008-04-15 20:47:02 +0000308 my $x = lc($row->[1]);
309 $x =~ s/\s/_/g;
310
311 print OUT "<tr class=\"bt_$x\">\n";
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000312
Ted Kremeneked757e02008-04-02 18:03:36 +0000313 my $ReportFile = $row->[0];
314
Ted Kremenekd5d43622008-04-02 20:43:36 +0000315 print OUT " <td class=\"DESC\">";
316 print OUT lc($row->[1]);
317 print OUT "</td>\n";
Ted Kremeneked757e02008-04-02 18:03:36 +0000318
319 for my $j ( 2 .. $#{$row} ) {
320 print OUT "<td>$row->[$j]</td>\n"
321 }
Ted Kremenekb59b7542008-04-02 18:42:49 +0000322
323 # Emit the "View" link.
Ted Kremeneked757e02008-04-02 18:03:36 +0000324
Ted Kremenekd5d43622008-04-02 20:43:36 +0000325 print OUT " <td class=\"View\"><a href=\"$ReportFile#EndPath\">View</a></td>\n";
Ted Kremenekb59b7542008-04-02 18:42:49 +0000326
327 # End the row.
Ted Kremeneked757e02008-04-02 18:03:36 +0000328 print OUT "</tr>\n";
329 }
330
331 print OUT "</table>\n</body></html>\n";
332 close(OUT);
Ted Kremenekd5d43622008-04-02 20:43:36 +0000333
Ted Kremenekd5d43622008-04-02 20:43:36 +0000334 CopyJS($Dir);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000335}
336
337##----------------------------------------------------------------------------##
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000338# RunBuildCommand - Run the build command.
339##----------------------------------------------------------------------------##
340
341sub RunBuildCommand {
342
343 my $Args = shift;
Ted Kremenek091f9372008-04-02 16:04:51 +0000344 my $IgnoreErrors = shift;
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000345 my $Cmd = $Args->[0];
346
Ted Kremenek404cb262008-04-02 15:34:12 +0000347 if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc") {
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000348 shift @$Args;
349 unshift @$Args, "ccc-analyzer"
350 }
Ted Kremenek091f9372008-04-02 16:04:51 +0000351 elsif ($IgnoreErrors) {
352 if ($Cmd eq "make" or $Cmd eq "gmake") {
353 push @$Args, "-k";
354 }
355 elsif ($Cmd eq "xcodebuild") {
356 push @$Args, "-PBXBuildsContinueAfterErrors=YES";
357 }
Ted Kremenekd5d43622008-04-02 20:43:36 +0000358 }
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000359
360 system(@$Args);
361}
362
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000363##----------------------------------------------------------------------------##
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000364# DisplayHelp - Utility function to display all help options.
365##----------------------------------------------------------------------------##
366
Sam Bishop479982e2008-04-02 03:35:43 +0000367sub DisplayHelp {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000368
Ted Kremeneked757e02008-04-02 18:03:36 +0000369print <<ENDTEXT;
Sam Bishop479982e2008-04-02 03:35:43 +0000370USAGE: $Prog [options] <build command> [build options]
Ted Kremenekb9177df2008-04-01 21:22:03 +0000371
372OPTIONS:
373
374 -o - Target directory for HTML report files. Subdirectories
Sam Bishop479982e2008-04-02 03:35:43 +0000375 will be created as needed to represent separate "runs" of
Ted Kremenekb9177df2008-04-01 21:22:03 +0000376 the analyzer. If this option is not specified, a directory
377 is created in /tmp to store the reports.
378
Ted Kremeneke9c91a82008-04-03 07:11:44 +0000379 -h - Display this message.
Ted Kremenekb9177df2008-04-01 21:22:03 +0000380 --help
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000381
Ted Kremenekd1843772008-04-02 16:31:58 +0000382 -k - Add a "keep on going" option to the specified build command.
Ted Kremenek707a9ad2008-04-02 16:41:25 +0000383 --keep-going This option currently supports make and xcodebuild.
384 This is a convenience option; one can specify this
385 behavior directly using build options.
Ted Kremenekb9177df2008-04-01 21:22:03 +0000386
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000387 -v - Verbose output from $Prog and the analyzer.
388 A second "-v" increases verbosity.
Ted Kremenekb9177df2008-04-01 21:22:03 +0000389
Ted Kremenekb59b7542008-04-02 18:42:49 +0000390 -V - View analysis results in a web browser when the build
391 --view completes.
392
Ted Kremenekb9177df2008-04-01 21:22:03 +0000393BUILD OPTIONS
394
Ted Kremenekf76812d2008-04-02 16:47:27 +0000395 You can specify any build option acceptable to the build command.
396
Ted Kremeneked757e02008-04-02 18:03:36 +0000397EXAMPLE
Ted Kremenekb9177df2008-04-01 21:22:03 +0000398
Ted Kremeneked757e02008-04-02 18:03:36 +0000399 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenekb9177df2008-04-01 21:22:03 +0000400
Ted Kremenekf76812d2008-04-02 16:47:27 +0000401 The above example causes analysis reports to be deposited into
402 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
403 A different subdirectory is created each time $Prog analyzes a project.
404 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenekb9177df2008-04-01 21:22:03 +0000405
406ENDTEXT
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000407}
408
409##----------------------------------------------------------------------------##
410# Process command-line arguments.
411##----------------------------------------------------------------------------##
412
413my $HtmlDir; # Parent directory to store HTML files.
414my $IgnoreErrors = 0; # Ignore build errors.
Ted Kremenekb59b7542008-04-02 18:42:49 +0000415my $ViewResults = 0; # View results when the build terminates.
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000416
417if (!@ARGV) {
418 DisplayHelp();
Sam Bishop479982e2008-04-02 03:35:43 +0000419 exit 1;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000420}
421
422while (@ARGV) {
423
424 # Scan for options we recognize.
425
426 my $arg = $ARGV[0];
427
Sam Bishopa3281512008-04-03 14:29:47 +0000428 if ($arg eq "-h" or $arg eq "--help") {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000429 DisplayHelp();
Sam Bishop479982e2008-04-02 03:35:43 +0000430 exit 0;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000431 }
432
433 if ($arg eq "-o") {
434 shift @ARGV;
435
436 if (!@ARGV) {
Ted Kremenek78965de2008-04-02 16:35:01 +0000437 die "$Prog: '-o' option requires a target directory name.\n";
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000438 }
439
440 $HtmlDir = shift @ARGV;
441 next;
442 }
443
Ted Kremenekb9177df2008-04-01 21:22:03 +0000444 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000445 shift @ARGV;
446 $IgnoreErrors = 1;
447 next;
448 }
449
450 if ($arg eq "-v") {
451 shift @ARGV;
452 $Verbose++;
453 next;
454 }
455
Ted Kremenekb59b7542008-04-02 18:42:49 +0000456 if ($arg eq "-V" or $arg eq "--view") {
457 shift @ARGV;
458 $ViewResults = 1;
459 next;
460 }
461
Ted Kremenek78965de2008-04-02 16:35:01 +0000462 die "$Prog: unrecognized option '$arg'\n" if ($arg =~ /^-/);
463
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000464 last;
465}
466
467if (!@ARGV) {
468 print STDERR "$Prog: No build command specified.\n\n";
469 DisplayHelp();
Sam Bishop479982e2008-04-02 03:35:43 +0000470 exit 1;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000471}
472
473# Determine the output directory for the HTML reports.
474
475if (!defined($HtmlDir)) {
476
Sam Bishop479982e2008-04-02 03:35:43 +0000477 $HtmlDir = mkdtemp("/tmp/$Prog-XXXXXX");
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000478
479 if (!defined($HtmlDir)) {
Sam Bishop479982e2008-04-02 03:35:43 +0000480 die "error: Cannot create HTML directory in /tmp.\n";
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000481 }
482
483 if (!$Verbose) {
484 print "$Prog: Using '$HtmlDir' as base HTML report directory.\n";
485 }
486}
487
Sam Bishop479982e2008-04-02 03:35:43 +0000488$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000489
490# Set the appropriate environment variables.
491
Sam Bishop479982e2008-04-02 03:35:43 +0000492SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000493
Ted Kremenekfc0f1c22008-04-08 20:22:12 +0000494my $Cmd = "$RealBin/ccc-analyzer";
495
496die "$Prog: Executable 'ccc-analyzer' does not exist at '$Cmd'\n"
497 if (! -x $Cmd);
498
Ted Kremenek47bf3892008-04-03 20:08:18 +0000499$ENV{'CC'} = $Cmd;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000500
501if ($Verbose >= 2) {
502 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
503}
504
505# Run the build.
506
Ted Kremenek091f9372008-04-02 16:04:51 +0000507RunBuildCommand(\@ARGV, $IgnoreErrors);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000508
509# Postprocess the HTML directory.
510
Sam Bishop479982e2008-04-02 03:35:43 +0000511Postprocess($HtmlDir);
Ted Kremenekb59b7542008-04-02 18:42:49 +0000512
513if ($ViewResults and -r "$HtmlDir/index.html") {
514 # Only works on Mac OS X (for now).
515 print "Viewing analysis results: '$HtmlDir/index.html'\n";
516 `open $HtmlDir/index.html`
517}