blob: a58751caf7c2d591d0a599ca0add2c5cb3db65c2 [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 Kremeneka6e24812008-04-19 18:05:48 +000019use Digest::MD5;
Ted Kremenek7a4648d2008-05-02 22:04:53 +000020use File::Basename;
Ted Kremenek23cfca32008-06-16 22:40:14 +000021use Term::ANSIColor;
22use Term::ANSIColor qw(:constants);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000023
24my $Verbose = 0; # Verbose output from this script.
25my $Prog = "scan-build";
Ted Kremenekf4cdf412008-05-23 18:17:05 +000026my $BuildName;
27my $BuildDate;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000028
Ted Kremenekf2f8d6c2008-06-17 03:06:59 +000029my $UseColor = ((($ENV{'TERM'} eq 'xterm-color') and -t STDOUT)
30 and defined($ENV{'SCAN_BUILD_COLOR'}));
Ted Kremenek23cfca32008-06-16 22:40:14 +000031
Ted Kremenekb7770c02008-07-15 17:06:13 +000032##----------------------------------------------------------------------------##
33# Diagnostics
34##----------------------------------------------------------------------------##
35
Ted Kremenek23cfca32008-06-16 22:40:14 +000036sub Diag {
37 if ($UseColor) {
38 print BOLD, MAGENTA "$Prog: @_";
39 print RESET;
40 }
41 else {
42 print "$Prog: @_";
43 }
44}
45
46sub DieDiag {
47 if ($UseColor) {
48 print BOLD, RED "$Prog: ";
49 print RESET, RED @_;
50 print RESET;
51 }
52 else {
53 print "$Prog: ", @_;
54 }
55 exit(0);
56}
57
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000058##----------------------------------------------------------------------------##
Ted Kremenekb7770c02008-07-15 17:06:13 +000059# Some initial preprocessing of Clang options.
60##----------------------------------------------------------------------------##
61
62my $ClangSB = "$RealBin/clang";
63my $Clang = $ClangSB;
64
65if (! -x $ClangSB) {
66 $Clang = "clang";
67}
68
69my %AvailableAnalyses;
70
71# Query clang for analysis options.
72open(PIPE, "$Clang --help |") or
73 DieDiag("Cannot execute '$Clang'");
74
75my $FoundAnalysis = 0;
76
77while(<PIPE>) {
78 if ($FoundAnalysis == 0) {
79 if (/Available Source Code Analyses/) {
80 $FoundAnalysis = 1;
81 }
82
83 next;
84 }
85
86 if (/^\s\s\s\s([^\s]+)\s(.+)$/) {
87 next if ($1 =~ /-dump/ or $1 =~ /-view/
88 or $1 =~ /-checker-simple/ or $1 =~ /-warn-uninit/);
89
90 $AvailableAnalyses{$1} = $2;
91 next;
92 }
93
94 last;
95}
96
97close (PIPE);
98
99my %AnalysesDefaultEnabled = (
100 '-warn-dead-stores' => 1,
101 '-checker-cfref' => 1,
Ted Kremenek90125992008-07-15 23:41:32 +0000102 '-warn-objc-methodsigs' => 1,
103 '-warn-objc-missing-dealloc' => 1
Ted Kremenekb7770c02008-07-15 17:06:13 +0000104);
105
106##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000107# GetHTMLRunDir - Construct an HTML directory name for the current run.
108##----------------------------------------------------------------------------##
109
Sam Bishopa0e22662008-04-02 03:35:43 +0000110sub GetHTMLRunDir {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000111
112 die "Not enough arguments." if (@_ == 0);
113
114 my $Dir = shift @_;
115
116 # Get current date and time.
117
118 my @CurrentTime = localtime();
119
120 my $year = $CurrentTime[5] + 1900;
121 my $day = $CurrentTime[3];
122 my $month = $CurrentTime[4] + 1;
123
Ted Kremenek9d7405f2008-05-14 17:23:56 +0000124 my $DateString = sprintf("%d-%02d-%02d", $year, $month, $day);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000125
126 # Determine the run number.
127
128 my $RunNumber;
129
130 if (-d $Dir) {
131
132 if (! -r $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000133 DieDiag("directory '$Dir' exists but is not readable.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000134 }
135
136 # Iterate over all files in the specified directory.
137
138 my $max = 0;
139
140 opendir(DIR, $Dir);
141 my @FILES= readdir(DIR);
142 closedir(DIR);
143
144 foreach my $f (@FILES) {
145
146 my @x = split/-/, $f;
147
148 next if (scalar(@x) != 4);
149 next if ($x[0] != $year);
150 next if ($x[1] != $month);
151 next if ($x[2] != $day);
152
153 if ($x[3] > $max) {
154 $max = $x[3];
155 }
156 }
157
158 $RunNumber = $max + 1;
159 }
160 else {
161
162 if (-x $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000163 DieDiag("'$Dir' exists but is not a directory.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000164 }
165
166 # $Dir does not exist. It will be automatically created by the
167 # clang driver. Set the run number to 1.
168
169 $RunNumber = 1;
170 }
171
172 die "RunNumber must be defined!" if (!defined($RunNumber));
173
174 # Append the run number.
175
176 return "$Dir/$DateString-$RunNumber";
177}
178
Sam Bishopa0e22662008-04-02 03:35:43 +0000179sub SetHtmlEnv {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000180
181 die "Wrong number of arguments." if (scalar(@_) != 2);
182
183 my $Args = shift;
184 my $Dir = shift;
185
186 die "No build command." if (scalar(@$Args) == 0);
187
188 my $Cmd = $$Args[0];
189
190 if ($Cmd =~ /configure/) {
191 return;
192 }
193
194 if ($Verbose) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000195 Diag("Emitting reports for this run to '$Dir'.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000196 }
197
198 $ENV{'CCC_ANALYZER_HTML'} = $Dir;
199}
200
201##----------------------------------------------------------------------------##
Ted Kremenek57cf4462008-04-18 15:09:30 +0000202# ComputeDigest - Compute a digest of the specified file.
203##----------------------------------------------------------------------------##
204
205sub ComputeDigest {
206 my $FName = shift;
Ted Kremenek23cfca32008-06-16 22:40:14 +0000207 DieDiag("Cannot read $FName to compute Digest.\n") if (! -r $FName);
Ted Kremeneka6e24812008-04-19 18:05:48 +0000208
209 # Use Digest::MD5. We don't have to be cryptographically secure. We're
Ted Kremenek7ea02e62008-04-19 18:07:44 +0000210 # just looking for duplicate files that come from a non-malicious source.
211 # We use Digest::MD5 because it is a standard Perl module that should
Ted Kremeneka6e24812008-04-19 18:05:48 +0000212 # come bundled on most systems.
213
Ted Kremenek23cfca32008-06-16 22:40:14 +0000214 open(FILE, $FName) or DieDiag("Cannot open $FName when computing Digest.\n");
Ted Kremeneka6e24812008-04-19 18:05:48 +0000215 binmode FILE;
216 my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest;
217 close(FILE);
218
219 # Return the digest.
220
221 return $Result;
Ted Kremenek57cf4462008-04-18 15:09:30 +0000222}
223
224##----------------------------------------------------------------------------##
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000225# UpdatePrefix - Compute the common prefix of files.
226##----------------------------------------------------------------------------##
227
228my $Prefix;
229
230sub UpdatePrefix {
231
232 my $x = shift;
233 my $y = basename($x);
234 $x =~ s/\Q$y\E$//;
235
236 # Ignore /usr, /Library, /System, /Developer
237
238 return if ( $x =~ /^\/usr/ or $x =~ /^\/Library/
239 or $x =~ /^\/System/ or $x =~ /^\/Developer/);
240
241
242 if (!defined $Prefix) {
243 $Prefix = $x;
244 return;
245 }
246
247 chop $Prefix while (!($x =~ /^$Prefix/));
248}
249
250sub GetPrefix {
251 return $Prefix;
252}
253
254##----------------------------------------------------------------------------##
255# UpdateInFilePath - Update the path in the report file.
256##----------------------------------------------------------------------------##
257
258sub UpdateInFilePath {
259 my $fname = shift;
260 my $regex = shift;
261 my $newtext = shift;
262
263 open (RIN, $fname) or die "cannot open $fname";
264 open (ROUT, ">$fname.tmp") or die "cannot open $fname.tmp";
265
266 while (<RIN>) {
267 s/$regex/$newtext/;
268 print ROUT $_;
269 }
270
271 close (ROUT);
272 close (RIN);
Ted Kremenek20161e92008-07-15 20:18:21 +0000273 system("mv", "$fname.tmp", $fname);
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000274}
275
276##----------------------------------------------------------------------------##
Ted Kremenek5744dc22008-04-02 18:03:36 +0000277# ScanFile - Scan a report file for various identifying attributes.
278##----------------------------------------------------------------------------##
279
Ted Kremenek57cf4462008-04-18 15:09:30 +0000280# Sometimes a source file is scanned more than once, and thus produces
281# multiple error reports. We use a cache to solve this problem.
282
283my %AlreadyScanned;
284
Ted Kremenek5744dc22008-04-02 18:03:36 +0000285sub ScanFile {
286
287 my $Index = shift;
288 my $Dir = shift;
289 my $FName = shift;
290
Ted Kremenek57cf4462008-04-18 15:09:30 +0000291 # Compute a digest for the report file. Determine if we have already
292 # scanned a file that looks just like it.
293
294 my $digest = ComputeDigest("$Dir/$FName");
295
296 if (defined($AlreadyScanned{$digest})) {
297 # Redundant file. Remove it.
Ted Kremenek20161e92008-07-15 20:18:21 +0000298 system ("rm", "-f", "$Dir/$FName");
Ted Kremenek57cf4462008-04-18 15:09:30 +0000299 return;
300 }
301
302 $AlreadyScanned{$digest} = 1;
303
Ted Kremenek809709f2008-04-18 16:58:34 +0000304 # At this point the report file is not world readable. Make it happen.
Ted Kremenek20161e92008-07-15 20:18:21 +0000305 system ("chmod", "644", "$Dir/$FName");
Ted Kremenek684bb092008-04-18 15:18:20 +0000306
307 # Scan the report file for tags.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000308 open(IN, "$Dir/$FName") or DieDiag("Cannot open '$Dir/$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000309
310 my $BugDesc = "";
Ted Kremenek22d6a632008-04-02 20:43:36 +0000311 my $BugFile = "";
312 my $BugPathLength = 1;
313 my $BugLine = 0;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000314
315 while (<IN>) {
316
317 if (/<!-- BUGDESC (.*) -->$/) {
318 $BugDesc = $1;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000319 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000320 elsif (/<!-- BUGFILE (.*) -->$/) {
321 $BugFile = $1;
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000322 UpdatePrefix($BugFile);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000323 }
324 elsif (/<!-- BUGPATHLENGTH (.*) -->$/) {
325 $BugPathLength = $1;
326 }
327 elsif (/<!-- BUGLINE (.*) -->$/) {
328 $BugLine = $1;
329 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000330 }
331
332 close(IN);
333
Ted Kremenek22d6a632008-04-02 20:43:36 +0000334 push @$Index,[ $FName, $BugDesc, $BugFile, $BugLine, $BugPathLength ];
335}
336
337##----------------------------------------------------------------------------##
338# CopyJS - Copy JavaScript code to target directory.
339##----------------------------------------------------------------------------##
340
341sub CopyJS {
342
343 my $Dir = shift;
344
Ted Kremenek23cfca32008-06-16 22:40:14 +0000345 DieDiag("Cannot find 'sorttable.js'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000346 if (! -r "$RealBin/sorttable.js");
347
Ted Kremenek20161e92008-07-15 20:18:21 +0000348 system ("cp", "$RealBin/sorttable.js", "$Dir");
Ted Kremenek22d6a632008-04-02 20:43:36 +0000349
Ted Kremenek23cfca32008-06-16 22:40:14 +0000350 DieDiag("Could not copy 'sorttable.js' to '$Dir'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000351 if (! -r "$Dir/sorttable.js");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000352}
353
354##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000355# Postprocess - Postprocess the results of an analysis scan.
356##----------------------------------------------------------------------------##
357
Sam Bishopa0e22662008-04-02 03:35:43 +0000358sub Postprocess {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000359
360 my $Dir = shift;
Ted Kremenek684bb092008-04-18 15:18:20 +0000361 my $BaseDir = shift;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000362
363 die "No directory specified." if (!defined($Dir));
Ted Kremenek684bb092008-04-18 15:18:20 +0000364 die "No base directory specified." if (!defined($BaseDir));
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000365
366 if (! -d $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000367 Diag("No bugs found.\n");
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000368 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000369 }
370
371 opendir(DIR, $Dir);
372 my @files = grep(/^report-.*\.html$/,readdir(DIR));
373 closedir(DIR);
374
375 if (scalar(@files) == 0) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000376 Diag("Removing directory '$Dir' because it contains no reports.\n");
Ted Kremenek20161e92008-07-15 20:18:21 +0000377 system ("rm", "-fR", $Dir);
Ted Kremenek23cfca32008-06-16 22:40:14 +0000378
379 # Remove the base directory if it contains no files (don't use '-R').
Ted Kremenek20161e92008-07-15 20:18:21 +0000380 system ("rm", "-f", $BaseDir);
Ted Kremenek23cfca32008-06-16 22:40:14 +0000381
382 Diag("No bugs found.\n");
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000383 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000384 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000385
386 # Scan each report file and build an index.
387
388 my @Index;
389
390 foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); }
391
392 # Generate an index.html file.
393
394 my $FName = "$Dir/index.html";
395
Ted Kremenek23cfca32008-06-16 22:40:14 +0000396 open(OUT, ">$FName") or DieDiag("Cannot create file '$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000397
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000398 # Print out the header.
399
Ted Kremenek5744dc22008-04-02 18:03:36 +0000400print OUT <<ENDTEXT;
401<html>
402<head>
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000403<style type="text/css">
404 body { color:#000000; background-color:#ffffff }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000405 body { font-family: Helvetica, sans-serif; font-size:9pt }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000406 h1 { font-size:12pt }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000407 table.sortable thead {
408 background-color:#eee; color:#666666;
409 font-weight: bold; cursor: default;
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000410 text-align:center;
411 border-top: 2px solid #000000;
412 border-bottom: 2px solid #000000;
413 font-weight: bold; font-family: Verdana
414 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000415 table.sortable { border: 1px #000000 solid }
416 table.sortable { border-collapse: collapse; border-spacing: 0px }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000417 td { border-bottom: 1px #000000 dotted }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000418 td { padding:5px; padding-left:8px; padding-right:8px }
Ted Kremenekd8c6d0c2008-04-07 23:50:07 +0000419 td { text-align:left; font-size:9pt }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000420 td.View { padding-left: 10px }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000421</style>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000422<script src="sorttable.js"></script>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000423<script language='javascript' type="text/javascript">
424function SetDisplay(RowClass, DisplayVal)
425{
426 var Rows = document.getElementsByTagName("tr");
427 for ( var i = 0 ; i < Rows.length; ++i ) {
428 if (Rows[i].className == RowClass) {
429 Rows[i].style.display = DisplayVal;
430 }
431 }
432}
433
434function ToggleDisplay(CheckButton, ClassName) {
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000435 if (CheckButton.checked) {
436 SetDisplay(ClassName, "");
437 }
438 else {
439 SetDisplay(ClassName, "none");
440 }
441}
442</script>
443</head>
444<body>
445ENDTEXT
446
447 # Print out the summary table.
448
449 my %Totals;
450
451 for my $row ( @Index ) {
452
Ted Kremenek432af592008-05-06 18:11:36 +0000453 #my $bug_type = lc($row->[1]);
454 my $bug_type = ($row->[1]);
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000455
456 if (!defined($Totals{$bug_type})) {
457 $Totals{$bug_type} = 1;
458 }
459 else {
460 $Totals{$bug_type}++;
461 }
462 }
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000463
464 print OUT "<h3>Summary</h3>";
465
466 if (defined($BuildName)) {
467 print OUT "\n<p>Results in this analysis run are based on analyzer build <b>$BuildName</b>.</p>\n"
468 }
469
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000470print OUT <<ENDTEXT;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000471<table class="sortable">
472<tr>
473 <td>Bug Type</td>
474 <td>Quantity</td>
Ted Kremenek2645c772008-07-07 16:58:44 +0000475 <td class="sorttable_nosort">Display?</td>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000476</tr>
477ENDTEXT
478
479 for my $key ( sort { $a cmp $b } keys %Totals ) {
Ted Kremenekbdf66c72008-05-06 23:51:45 +0000480 my $x = lc($key);
481 $x =~ s/\s[,]/_/g;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000482 print OUT "<tr><td>$key</td><td>$Totals{$key}</td><td><input type=\"checkbox\" onClick=\"ToggleDisplay(this,'bt_$x');\" checked/></td></tr>\n";
483 }
484
485 # Print out the table of errors.
486
487print OUT <<ENDTEXT;
488</table>
489<h3>Reports</h3>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000490<table class="sortable">
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000491<tr>
Ted Kremenek88a96d62008-07-07 17:23:32 +0000492 <td class="sorttable_sorted">Bug Type<span id="sorttable_sortfwdind">&nbsp;&#x25BE;</span>
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000493 <td>File</td>
494 <td>Line</td>
495 <td>Path Length</td>
Ted Kremenek2645c772008-07-07 16:58:44 +0000496 <td class="sorttable_nosort"></td>
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000497</tr>
Ted Kremenek5744dc22008-04-02 18:03:36 +0000498ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000499
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000500 my $prefix = GetPrefix();
501 my $regex;
502 my $InFileRegex;
503 my $InFilePrefix = "File:</td><td>";
504
505 if (defined($prefix)) {
506 $regex = qr/^\Q$prefix\E/is;
507 $InFileRegex = qr/\Q$InFilePrefix$prefix\E/is;
508 }
509
Ted Kremenek5744dc22008-04-02 18:03:36 +0000510 for my $row ( sort { $a->[1] cmp $b->[1] } @Index ) {
511
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000512 my $x = lc($row->[1]);
Ted Kremenekbdf66c72008-05-06 23:51:45 +0000513 $x =~ s/\s[,]/_/g;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000514
515 print OUT "<tr class=\"bt_$x\">\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000516
Ted Kremenek5744dc22008-04-02 18:03:36 +0000517 my $ReportFile = $row->[0];
518
Ted Kremenek22d6a632008-04-02 20:43:36 +0000519 print OUT " <td class=\"DESC\">";
Ted Kremenek432af592008-05-06 18:11:36 +0000520 #print OUT lc($row->[1]);
521 print OUT $row->[1];
Ted Kremenek22d6a632008-04-02 20:43:36 +0000522 print OUT "</td>\n";
Ted Kremenek5744dc22008-04-02 18:03:36 +0000523
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000524 # Update the file prefix.
525
526 my $fname = $row->[2];
527 if (defined($regex)) {
528 $fname =~ s/$regex//;
529 UpdateInFilePath("$Dir/$ReportFile", $InFileRegex, $InFilePrefix)
530 }
Ted Kremenek3e56e0b2008-05-02 23:40:49 +0000531
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000532 print OUT "<td>$fname</td>\n";
533
534 # Print the rest of the columns.
535
536 for my $j ( 3 .. $#{$row} ) {
Ted Kremenek5744dc22008-04-02 18:03:36 +0000537 print OUT "<td>$row->[$j]</td>\n"
538 }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000539
540 # Emit the "View" link.
Ted Kremenek5744dc22008-04-02 18:03:36 +0000541
Ted Kremenek22d6a632008-04-02 20:43:36 +0000542 print OUT " <td class=\"View\"><a href=\"$ReportFile#EndPath\">View</a></td>\n";
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000543
544 # End the row.
Ted Kremenek5744dc22008-04-02 18:03:36 +0000545 print OUT "</tr>\n";
546 }
547
548 print OUT "</table>\n</body></html>\n";
549 close(OUT);
Ted Kremenek20161e92008-07-15 20:18:21 +0000550
Ted Kremenek22d6a632008-04-02 20:43:36 +0000551 CopyJS($Dir);
Ted Kremenek20161e92008-07-15 20:18:21 +0000552
553 # Make sure $Dir and $BaseDir are world readable/executable.
554 system("chmod", "755", $Dir);
555 system("chmod", "755", $BaseDir);
556
Ted Kremenek23cfca32008-06-16 22:40:14 +0000557 my $Num = scalar(@Index);
Ted Kremenek150c2122008-07-11 19:15:05 +0000558 Diag("$Num bugs found.\n");
559 if ($Num > 0 && -r "$Dir/index.html") {
560 Diag("Open '$Dir/index.html' to examine bug reports.\n");
561 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000562
563 return $Num;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000564}
565
566##----------------------------------------------------------------------------##
Ted Kremenekdab11102008-04-02 04:43:42 +0000567# RunBuildCommand - Run the build command.
568##----------------------------------------------------------------------------##
569
Ted Kremenek6b628982008-04-30 23:47:12 +0000570sub AddIfNotPresent {
571 my $Args = shift;
572 my $Arg = shift;
573 my $found = 0;
574
575 foreach my $k (@$Args) {
576 if ($k eq $Arg) {
577 $found = 1;
578 last;
579 }
580 }
581
582 if ($found == 0) {
583 push @$Args, $Arg;
584 }
585}
586
Ted Kremenekdab11102008-04-02 04:43:42 +0000587sub RunBuildCommand {
588
589 my $Args = shift;
Ted Kremenek7442ca62008-04-02 16:04:51 +0000590 my $IgnoreErrors = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000591 my $Cmd = $Args->[0];
Ted Kremenek6195c372008-06-02 21:52:47 +0000592 my $CCAnalyzer = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000593
Ted Kremenek3301cb12008-06-30 18:18:16 +0000594 # Get only the part of the command after the last '/'.
595 if ($Cmd =~ /\/([^\/]+)$/) {
596 $Cmd = $1;
597 }
598
Ted Kremenek6a43ba92008-04-02 15:34:12 +0000599 if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc") {
Ted Kremenekdab11102008-04-02 04:43:42 +0000600 shift @$Args;
Ted Kremenek6195c372008-06-02 21:52:47 +0000601 unshift @$Args, $CCAnalyzer;
Ted Kremenekdab11102008-04-02 04:43:42 +0000602 }
Ted Kremenek7442ca62008-04-02 16:04:51 +0000603 elsif ($IgnoreErrors) {
604 if ($Cmd eq "make" or $Cmd eq "gmake") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000605 AddIfNotPresent($Args,"-k");
Ted Kremenek8912b542008-05-13 21:28:02 +0000606 AddIfNotPresent($Args,"-i");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000607 }
608 elsif ($Cmd eq "xcodebuild") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000609 AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000610 }
Ted Kremenek6b628982008-04-30 23:47:12 +0000611 }
612
Ted Kremenek6b628982008-04-30 23:47:12 +0000613 if ($Cmd eq "xcodebuild") {
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000614 # Disable distributed builds for xcodebuild.
Ted Kremenek6b628982008-04-30 23:47:12 +0000615 AddIfNotPresent($Args,"-nodistribute");
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000616
617 # Disable PCH files until clang supports them.
618 AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO");
Ted Kremenek915e9722008-05-27 23:18:07 +0000619
620 # When 'CC' is set, xcodebuild uses it to do all linking, even if we are
621 # linking C++ object files. Set 'LDPLUSPLUS' so that xcodebuild uses 'g++'
622 # when linking such files.
623 my $LDPLUSPLUS = `which g++`;
624 $LDPLUSPLUS =~ s/\015?\012//; # strip newlines
625 $ENV{'LDPLUSPLUS'} = $LDPLUSPLUS;
Ted Kremenek6b628982008-04-30 23:47:12 +0000626 }
Ted Kremenekdab11102008-04-02 04:43:42 +0000627
Ted Kremenek5656a982008-07-15 17:09:28 +0000628 return system(@$Args);
Ted Kremenekdab11102008-04-02 04:43:42 +0000629}
630
Ted Kremenekdab11102008-04-02 04:43:42 +0000631##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000632# DisplayHelp - Utility function to display all help options.
633##----------------------------------------------------------------------------##
634
Sam Bishopa0e22662008-04-02 03:35:43 +0000635sub DisplayHelp {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000636
Ted Kremenek5744dc22008-04-02 18:03:36 +0000637print <<ENDTEXT;
Sam Bishopa0e22662008-04-02 03:35:43 +0000638USAGE: $Prog [options] <build command> [build options]
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000639
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000640ENDTEXT
641
642 if (defined($BuildName)) {
643 print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n";
644 }
645
646print <<ENDTEXT;
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000647OPTIONS:
648
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000649 -o - Target directory for HTML report files. Subdirectories
Sam Bishopa0e22662008-04-02 03:35:43 +0000650 will be created as needed to represent separate "runs" of
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000651 the analyzer. If this option is not specified, a directory
652 is created in /tmp to store the reports.
Ted Kremenek1262fc42008-05-14 20:10:33 +0000653
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000654 -h - Display this message.
655 --help
Ted Kremenek1262fc42008-05-14 20:10:33 +0000656
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000657 -k - Add a "keep on going" option to the specified build command.
658 --keep-going This option currently supports make and xcodebuild.
Ted Kremenekf02e8db2008-04-02 16:41:25 +0000659 This is a convenience option; one can specify this
660 behavior directly using build options.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000661
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000662 --status-bugs - By default, the exit status of $Prog is the same as the
663 executed build command. Specifying this option causes the
664 exit status of $Prog to be 1 if it found potential bugs
665 and 0 otherwise.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000666
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000667 -v - Verbose output from $Prog and the analyzer.
668 A second and third "-v" increases verbosity.
669
670 -V - View analysis results in a web browser when the build
671 --view completes.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000672
Ted Kremenekb7770c02008-07-15 17:06:13 +0000673ENDTEXT
674
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000675 print " Available Source Code Analyses (multiple analyses may be specified):\n\n";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000676
677 foreach my $Analysis (sort keys %AvailableAnalyses) {
678 if (defined($AnalysesDefaultEnabled{$Analysis})) {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000679 print " (+)";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000680 }
681 else {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000682 print " ";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000683 }
684
685 print " $Analysis $AvailableAnalyses{$Analysis}\n";
686 }
687
688print <<ENDTEXT
689
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000690 NOTE: "(+)" indicates that an analysis is enabled by default unless one
691 or more analysis options are specified
Ted Kremenekb7770c02008-07-15 17:06:13 +0000692
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000693BUILD OPTIONS
694
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000695 You can specify any build option acceptable to the build command.
Ted Kremenek39eefde2008-04-02 16:47:27 +0000696
Ted Kremenek5744dc22008-04-02 18:03:36 +0000697EXAMPLE
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000698
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000699 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000700
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000701 The above example causes analysis reports to be deposited into
702 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
703 A different subdirectory is created each time $Prog analyzes a project.
704 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000705
706ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000707}
708
709##----------------------------------------------------------------------------##
710# Process command-line arguments.
711##----------------------------------------------------------------------------##
712
713my $HtmlDir; # Parent directory to store HTML files.
714my $IgnoreErrors = 0; # Ignore build errors.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000715my $ViewResults = 0; # View results when the build terminates.
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000716my $ExitStatusFoundBugs = 0; # Exit status reflects whether bugs were found
Ted Kremenekb7770c02008-07-15 17:06:13 +0000717my @AnalysesToRun;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000718
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000719
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000720if (!@ARGV) {
721 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000722 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000723}
724
725while (@ARGV) {
726
727 # Scan for options we recognize.
728
729 my $arg = $ARGV[0];
730
Sam Bishop2f2418e2008-04-03 14:29:47 +0000731 if ($arg eq "-h" or $arg eq "--help") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000732 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000733 exit 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000734 }
735
Ted Kremenekb7770c02008-07-15 17:06:13 +0000736 if (defined($AvailableAnalyses{$arg})) {
Ted Kremenek1262fc42008-05-14 20:10:33 +0000737 shift @ARGV;
Ted Kremenekb7770c02008-07-15 17:06:13 +0000738 push @AnalysesToRun, $arg;
Ted Kremenek1262fc42008-05-14 20:10:33 +0000739 next;
740 }
741
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000742 if ($arg eq "-o") {
743 shift @ARGV;
744
745 if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000746 DieDiag("'-o' option requires a target directory name.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000747 }
748
749 $HtmlDir = shift @ARGV;
750 next;
751 }
752
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000753 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000754 shift @ARGV;
755 $IgnoreErrors = 1;
756 next;
757 }
758
759 if ($arg eq "-v") {
760 shift @ARGV;
761 $Verbose++;
762 next;
763 }
764
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000765 if ($arg eq "-V" or $arg eq "--view") {
766 shift @ARGV;
767 $ViewResults = 1;
768 next;
769 }
770
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000771 if ($arg eq "--status-bugs") {
772 shift @ARGV;
773 $ExitStatusFoundBugs = 1;
774 next;
775 }
776
Ted Kremenek23cfca32008-06-16 22:40:14 +0000777 DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/);
Ted Kremenek0062ad42008-04-02 16:35:01 +0000778
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000779 last;
780}
781
782if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000783 Diag("No build command specified.\n\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000784 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000785 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000786}
787
788# Determine the output directory for the HTML reports.
789
790if (!defined($HtmlDir)) {
791
Sam Bishopa0e22662008-04-02 03:35:43 +0000792 $HtmlDir = mkdtemp("/tmp/$Prog-XXXXXX");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000793
794 if (!defined($HtmlDir)) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000795 DieDiag("Cannot create HTML directory in /tmp.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000796 }
797
798 if (!$Verbose) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000799 Diag("Using '$HtmlDir' as base HTML report directory.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000800 }
801}
802
Ted Kremenek684bb092008-04-18 15:18:20 +0000803my $BaseDir = $HtmlDir;
Sam Bishopa0e22662008-04-02 03:35:43 +0000804$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000805
806# Set the appropriate environment variables.
807
Sam Bishopa0e22662008-04-02 03:35:43 +0000808SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000809
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000810my $Cmd = "$RealBin/ccc-analyzer";
811
Ted Kremenek23cfca32008-06-16 22:40:14 +0000812DieDiag("Executable 'ccc-analyzer' does not exist at '$Cmd'\n")
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000813 if (! -x $Cmd);
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000814
Ted Kremenekb7770c02008-07-15 17:06:13 +0000815if (! -x $ClangSB) {
816 Diag("'clang' executable not found in '$RealBin'.\n");
817 Diag("Using 'clang' from path.\n");
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000818}
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000819
Ted Kremenek4f4b17d2008-04-03 20:08:18 +0000820$ENV{'CC'} = $Cmd;
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000821$ENV{'CLANG'} = $Clang;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000822
823if ($Verbose >= 2) {
824 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
825}
826
Ted Kremeneka9525c92008-05-12 22:07:14 +0000827if ($Verbose >= 3) {
828 $ENV{'CCC_ANALYZER_LOG'} = 1;
829}
830
Ted Kremenek90125992008-07-15 23:41:32 +0000831if (scalar(@AnalysesToRun) == 0) {
832 foreach my $key (keys %AnalysesDefaultEnabled) {
833 push @AnalysesToRun,$key;
834 }
Ted Kremenek01006782008-07-02 23:16:10 +0000835}
Ted Kremenek1262fc42008-05-14 20:10:33 +0000836
Ted Kremenek90125992008-07-15 23:41:32 +0000837$ENV{'CCC_ANALYZER_ANALYSIS'} = join ' ',@AnalysesToRun;
838
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000839# Run the build.
840
Ted Kremenek5656a982008-07-15 17:09:28 +0000841my $ExitStatus = RunBuildCommand(\@ARGV, $IgnoreErrors, $Cmd);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000842
843# Postprocess the HTML directory.
844
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000845my $NumBugs = Postprocess($HtmlDir, $BaseDir);
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000846
847if ($ViewResults and -r "$HtmlDir/index.html") {
848 # Only works on Mac OS X (for now).
849 print "Viewing analysis results: '$HtmlDir/index.html'\n";
Ted Kremenek20161e92008-07-15 20:18:21 +0000850 system("open", "$HtmlDir/index.html");
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000851}
Ted Kremenek5656a982008-07-15 17:09:28 +0000852
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000853if ($ExitStatusFoundBugs) {
854 exit 1 if ($NumBugs > 0);
855 exit 0;
856}
857
Ted Kremenek5656a982008-07-15 17:09:28 +0000858exit $ExitStatus;
859