blob: 6ee89decc979b020c13a2ad3fa7997a835c28381 [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,
102 '-warn-objc-methodsigs' => 1
103);
104
105##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000106# GetHTMLRunDir - Construct an HTML directory name for the current run.
107##----------------------------------------------------------------------------##
108
Sam Bishopa0e22662008-04-02 03:35:43 +0000109sub GetHTMLRunDir {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000110
111 die "Not enough arguments." if (@_ == 0);
112
113 my $Dir = shift @_;
114
115 # Get current date and time.
116
117 my @CurrentTime = localtime();
118
119 my $year = $CurrentTime[5] + 1900;
120 my $day = $CurrentTime[3];
121 my $month = $CurrentTime[4] + 1;
122
Ted Kremenek9d7405f2008-05-14 17:23:56 +0000123 my $DateString = sprintf("%d-%02d-%02d", $year, $month, $day);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000124
125 # Determine the run number.
126
127 my $RunNumber;
128
129 if (-d $Dir) {
130
131 if (! -r $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000132 DieDiag("directory '$Dir' exists but is not readable.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000133 }
134
135 # Iterate over all files in the specified directory.
136
137 my $max = 0;
138
139 opendir(DIR, $Dir);
140 my @FILES= readdir(DIR);
141 closedir(DIR);
142
143 foreach my $f (@FILES) {
144
145 my @x = split/-/, $f;
146
147 next if (scalar(@x) != 4);
148 next if ($x[0] != $year);
149 next if ($x[1] != $month);
150 next if ($x[2] != $day);
151
152 if ($x[3] > $max) {
153 $max = $x[3];
154 }
155 }
156
157 $RunNumber = $max + 1;
158 }
159 else {
160
161 if (-x $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000162 DieDiag("'$Dir' exists but is not a directory.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000163 }
164
165 # $Dir does not exist. It will be automatically created by the
166 # clang driver. Set the run number to 1.
167
168 $RunNumber = 1;
169 }
170
171 die "RunNumber must be defined!" if (!defined($RunNumber));
172
173 # Append the run number.
174
175 return "$Dir/$DateString-$RunNumber";
176}
177
Sam Bishopa0e22662008-04-02 03:35:43 +0000178sub SetHtmlEnv {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000179
180 die "Wrong number of arguments." if (scalar(@_) != 2);
181
182 my $Args = shift;
183 my $Dir = shift;
184
185 die "No build command." if (scalar(@$Args) == 0);
186
187 my $Cmd = $$Args[0];
188
189 if ($Cmd =~ /configure/) {
190 return;
191 }
192
193 if ($Verbose) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000194 Diag("Emitting reports for this run to '$Dir'.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000195 }
196
197 $ENV{'CCC_ANALYZER_HTML'} = $Dir;
198}
199
200##----------------------------------------------------------------------------##
Ted Kremenek57cf4462008-04-18 15:09:30 +0000201# ComputeDigest - Compute a digest of the specified file.
202##----------------------------------------------------------------------------##
203
204sub ComputeDigest {
205 my $FName = shift;
Ted Kremenek23cfca32008-06-16 22:40:14 +0000206 DieDiag("Cannot read $FName to compute Digest.\n") if (! -r $FName);
Ted Kremeneka6e24812008-04-19 18:05:48 +0000207
208 # Use Digest::MD5. We don't have to be cryptographically secure. We're
Ted Kremenek7ea02e62008-04-19 18:07:44 +0000209 # just looking for duplicate files that come from a non-malicious source.
210 # We use Digest::MD5 because it is a standard Perl module that should
Ted Kremeneka6e24812008-04-19 18:05:48 +0000211 # come bundled on most systems.
212
Ted Kremenek23cfca32008-06-16 22:40:14 +0000213 open(FILE, $FName) or DieDiag("Cannot open $FName when computing Digest.\n");
Ted Kremeneka6e24812008-04-19 18:05:48 +0000214 binmode FILE;
215 my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest;
216 close(FILE);
217
218 # Return the digest.
219
220 return $Result;
Ted Kremenek57cf4462008-04-18 15:09:30 +0000221}
222
223##----------------------------------------------------------------------------##
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000224# UpdatePrefix - Compute the common prefix of files.
225##----------------------------------------------------------------------------##
226
227my $Prefix;
228
229sub UpdatePrefix {
230
231 my $x = shift;
232 my $y = basename($x);
233 $x =~ s/\Q$y\E$//;
234
235 # Ignore /usr, /Library, /System, /Developer
236
237 return if ( $x =~ /^\/usr/ or $x =~ /^\/Library/
238 or $x =~ /^\/System/ or $x =~ /^\/Developer/);
239
240
241 if (!defined $Prefix) {
242 $Prefix = $x;
243 return;
244 }
245
246 chop $Prefix while (!($x =~ /^$Prefix/));
247}
248
249sub GetPrefix {
250 return $Prefix;
251}
252
253##----------------------------------------------------------------------------##
254# UpdateInFilePath - Update the path in the report file.
255##----------------------------------------------------------------------------##
256
257sub UpdateInFilePath {
258 my $fname = shift;
259 my $regex = shift;
260 my $newtext = shift;
261
262 open (RIN, $fname) or die "cannot open $fname";
263 open (ROUT, ">$fname.tmp") or die "cannot open $fname.tmp";
264
265 while (<RIN>) {
266 s/$regex/$newtext/;
267 print ROUT $_;
268 }
269
270 close (ROUT);
271 close (RIN);
Ted Kremenek20161e92008-07-15 20:18:21 +0000272 system("mv", "$fname.tmp", $fname);
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000273}
274
275##----------------------------------------------------------------------------##
Ted Kremenek5744dc22008-04-02 18:03:36 +0000276# ScanFile - Scan a report file for various identifying attributes.
277##----------------------------------------------------------------------------##
278
Ted Kremenek57cf4462008-04-18 15:09:30 +0000279# Sometimes a source file is scanned more than once, and thus produces
280# multiple error reports. We use a cache to solve this problem.
281
282my %AlreadyScanned;
283
Ted Kremenek5744dc22008-04-02 18:03:36 +0000284sub ScanFile {
285
286 my $Index = shift;
287 my $Dir = shift;
288 my $FName = shift;
289
Ted Kremenek57cf4462008-04-18 15:09:30 +0000290 # Compute a digest for the report file. Determine if we have already
291 # scanned a file that looks just like it.
292
293 my $digest = ComputeDigest("$Dir/$FName");
294
295 if (defined($AlreadyScanned{$digest})) {
296 # Redundant file. Remove it.
Ted Kremenek20161e92008-07-15 20:18:21 +0000297 system ("rm", "-f", "$Dir/$FName");
Ted Kremenek57cf4462008-04-18 15:09:30 +0000298 return;
299 }
300
301 $AlreadyScanned{$digest} = 1;
302
Ted Kremenek809709f2008-04-18 16:58:34 +0000303 # At this point the report file is not world readable. Make it happen.
Ted Kremenek20161e92008-07-15 20:18:21 +0000304 system ("chmod", "644", "$Dir/$FName");
Ted Kremenek684bb092008-04-18 15:18:20 +0000305
306 # Scan the report file for tags.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000307 open(IN, "$Dir/$FName") or DieDiag("Cannot open '$Dir/$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000308
309 my $BugDesc = "";
Ted Kremenek22d6a632008-04-02 20:43:36 +0000310 my $BugFile = "";
311 my $BugPathLength = 1;
312 my $BugLine = 0;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000313
314 while (<IN>) {
315
316 if (/<!-- BUGDESC (.*) -->$/) {
317 $BugDesc = $1;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000318 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000319 elsif (/<!-- BUGFILE (.*) -->$/) {
320 $BugFile = $1;
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000321 UpdatePrefix($BugFile);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000322 }
323 elsif (/<!-- BUGPATHLENGTH (.*) -->$/) {
324 $BugPathLength = $1;
325 }
326 elsif (/<!-- BUGLINE (.*) -->$/) {
327 $BugLine = $1;
328 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000329 }
330
331 close(IN);
332
Ted Kremenek22d6a632008-04-02 20:43:36 +0000333 push @$Index,[ $FName, $BugDesc, $BugFile, $BugLine, $BugPathLength ];
334}
335
336##----------------------------------------------------------------------------##
337# CopyJS - Copy JavaScript code to target directory.
338##----------------------------------------------------------------------------##
339
340sub CopyJS {
341
342 my $Dir = shift;
343
Ted Kremenek23cfca32008-06-16 22:40:14 +0000344 DieDiag("Cannot find 'sorttable.js'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000345 if (! -r "$RealBin/sorttable.js");
346
Ted Kremenek20161e92008-07-15 20:18:21 +0000347 system ("cp", "$RealBin/sorttable.js", "$Dir");
Ted Kremenek22d6a632008-04-02 20:43:36 +0000348
Ted Kremenek23cfca32008-06-16 22:40:14 +0000349 DieDiag("Could not copy 'sorttable.js' to '$Dir'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000350 if (! -r "$Dir/sorttable.js");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000351}
352
353##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000354# Postprocess - Postprocess the results of an analysis scan.
355##----------------------------------------------------------------------------##
356
Sam Bishopa0e22662008-04-02 03:35:43 +0000357sub Postprocess {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000358
359 my $Dir = shift;
Ted Kremenek684bb092008-04-18 15:18:20 +0000360 my $BaseDir = shift;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000361
362 die "No directory specified." if (!defined($Dir));
Ted Kremenek684bb092008-04-18 15:18:20 +0000363 die "No base directory specified." if (!defined($BaseDir));
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000364
365 if (! -d $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000366 Diag("No bugs found.\n");
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000367 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000368 }
369
370 opendir(DIR, $Dir);
371 my @files = grep(/^report-.*\.html$/,readdir(DIR));
372 closedir(DIR);
373
374 if (scalar(@files) == 0) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000375 Diag("Removing directory '$Dir' because it contains no reports.\n");
Ted Kremenek20161e92008-07-15 20:18:21 +0000376 system ("rm", "-fR", $Dir);
Ted Kremenek23cfca32008-06-16 22:40:14 +0000377
378 # Remove the base directory if it contains no files (don't use '-R').
Ted Kremenek20161e92008-07-15 20:18:21 +0000379 system ("rm", "-f", $BaseDir);
Ted Kremenek23cfca32008-06-16 22:40:14 +0000380
381 Diag("No bugs found.\n");
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000382 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000383 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000384
385 # Scan each report file and build an index.
386
387 my @Index;
388
389 foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); }
390
391 # Generate an index.html file.
392
393 my $FName = "$Dir/index.html";
394
Ted Kremenek23cfca32008-06-16 22:40:14 +0000395 open(OUT, ">$FName") or DieDiag("Cannot create file '$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000396
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000397 # Print out the header.
398
Ted Kremenek5744dc22008-04-02 18:03:36 +0000399print OUT <<ENDTEXT;
400<html>
401<head>
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000402<style type="text/css">
403 body { color:#000000; background-color:#ffffff }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000404 body { font-family: Helvetica, sans-serif; font-size:9pt }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000405 h1 { font-size:12pt }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000406 table.sortable thead {
407 background-color:#eee; color:#666666;
408 font-weight: bold; cursor: default;
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000409 text-align:center;
410 border-top: 2px solid #000000;
411 border-bottom: 2px solid #000000;
412 font-weight: bold; font-family: Verdana
413 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000414 table.sortable { border: 1px #000000 solid }
415 table.sortable { border-collapse: collapse; border-spacing: 0px }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000416 td { border-bottom: 1px #000000 dotted }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000417 td { padding:5px; padding-left:8px; padding-right:8px }
Ted Kremenekd8c6d0c2008-04-07 23:50:07 +0000418 td { text-align:left; font-size:9pt }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000419 td.View { padding-left: 10px }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000420</style>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000421<script src="sorttable.js"></script>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000422<script language='javascript' type="text/javascript">
423function SetDisplay(RowClass, DisplayVal)
424{
425 var Rows = document.getElementsByTagName("tr");
426 for ( var i = 0 ; i < Rows.length; ++i ) {
427 if (Rows[i].className == RowClass) {
428 Rows[i].style.display = DisplayVal;
429 }
430 }
431}
432
433function ToggleDisplay(CheckButton, ClassName) {
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000434 if (CheckButton.checked) {
435 SetDisplay(ClassName, "");
436 }
437 else {
438 SetDisplay(ClassName, "none");
439 }
440}
441</script>
442</head>
443<body>
444ENDTEXT
445
446 # Print out the summary table.
447
448 my %Totals;
449
450 for my $row ( @Index ) {
451
Ted Kremenek432af592008-05-06 18:11:36 +0000452 #my $bug_type = lc($row->[1]);
453 my $bug_type = ($row->[1]);
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000454
455 if (!defined($Totals{$bug_type})) {
456 $Totals{$bug_type} = 1;
457 }
458 else {
459 $Totals{$bug_type}++;
460 }
461 }
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000462
463 print OUT "<h3>Summary</h3>";
464
465 if (defined($BuildName)) {
466 print OUT "\n<p>Results in this analysis run are based on analyzer build <b>$BuildName</b>.</p>\n"
467 }
468
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000469print OUT <<ENDTEXT;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000470<table class="sortable">
471<tr>
472 <td>Bug Type</td>
473 <td>Quantity</td>
Ted Kremenek2645c772008-07-07 16:58:44 +0000474 <td class="sorttable_nosort">Display?</td>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000475</tr>
476ENDTEXT
477
478 for my $key ( sort { $a cmp $b } keys %Totals ) {
Ted Kremenekbdf66c72008-05-06 23:51:45 +0000479 my $x = lc($key);
480 $x =~ s/\s[,]/_/g;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000481 print OUT "<tr><td>$key</td><td>$Totals{$key}</td><td><input type=\"checkbox\" onClick=\"ToggleDisplay(this,'bt_$x');\" checked/></td></tr>\n";
482 }
483
484 # Print out the table of errors.
485
486print OUT <<ENDTEXT;
487</table>
488<h3>Reports</h3>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000489<table class="sortable">
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000490<tr>
Ted Kremenek88a96d62008-07-07 17:23:32 +0000491 <td class="sorttable_sorted">Bug Type<span id="sorttable_sortfwdind">&nbsp;&#x25BE;</span>
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000492 <td>File</td>
493 <td>Line</td>
494 <td>Path Length</td>
Ted Kremenek2645c772008-07-07 16:58:44 +0000495 <td class="sorttable_nosort"></td>
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000496</tr>
Ted Kremenek5744dc22008-04-02 18:03:36 +0000497ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000498
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000499 my $prefix = GetPrefix();
500 my $regex;
501 my $InFileRegex;
502 my $InFilePrefix = "File:</td><td>";
503
504 if (defined($prefix)) {
505 $regex = qr/^\Q$prefix\E/is;
506 $InFileRegex = qr/\Q$InFilePrefix$prefix\E/is;
507 }
508
Ted Kremenek5744dc22008-04-02 18:03:36 +0000509 for my $row ( sort { $a->[1] cmp $b->[1] } @Index ) {
510
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000511 my $x = lc($row->[1]);
Ted Kremenekbdf66c72008-05-06 23:51:45 +0000512 $x =~ s/\s[,]/_/g;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000513
514 print OUT "<tr class=\"bt_$x\">\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000515
Ted Kremenek5744dc22008-04-02 18:03:36 +0000516 my $ReportFile = $row->[0];
517
Ted Kremenek22d6a632008-04-02 20:43:36 +0000518 print OUT " <td class=\"DESC\">";
Ted Kremenek432af592008-05-06 18:11:36 +0000519 #print OUT lc($row->[1]);
520 print OUT $row->[1];
Ted Kremenek22d6a632008-04-02 20:43:36 +0000521 print OUT "</td>\n";
Ted Kremenek5744dc22008-04-02 18:03:36 +0000522
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000523 # Update the file prefix.
524
525 my $fname = $row->[2];
526 if (defined($regex)) {
527 $fname =~ s/$regex//;
528 UpdateInFilePath("$Dir/$ReportFile", $InFileRegex, $InFilePrefix)
529 }
Ted Kremenek3e56e0b2008-05-02 23:40:49 +0000530
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000531 print OUT "<td>$fname</td>\n";
532
533 # Print the rest of the columns.
534
535 for my $j ( 3 .. $#{$row} ) {
Ted Kremenek5744dc22008-04-02 18:03:36 +0000536 print OUT "<td>$row->[$j]</td>\n"
537 }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000538
539 # Emit the "View" link.
Ted Kremenek5744dc22008-04-02 18:03:36 +0000540
Ted Kremenek22d6a632008-04-02 20:43:36 +0000541 print OUT " <td class=\"View\"><a href=\"$ReportFile#EndPath\">View</a></td>\n";
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000542
543 # End the row.
Ted Kremenek5744dc22008-04-02 18:03:36 +0000544 print OUT "</tr>\n";
545 }
546
547 print OUT "</table>\n</body></html>\n";
548 close(OUT);
Ted Kremenek20161e92008-07-15 20:18:21 +0000549
Ted Kremenek22d6a632008-04-02 20:43:36 +0000550 CopyJS($Dir);
Ted Kremenek20161e92008-07-15 20:18:21 +0000551
552 # Make sure $Dir and $BaseDir are world readable/executable.
553 system("chmod", "755", $Dir);
554 system("chmod", "755", $BaseDir);
555
Ted Kremenek23cfca32008-06-16 22:40:14 +0000556 my $Num = scalar(@Index);
Ted Kremenek150c2122008-07-11 19:15:05 +0000557 Diag("$Num bugs found.\n");
558 if ($Num > 0 && -r "$Dir/index.html") {
559 Diag("Open '$Dir/index.html' to examine bug reports.\n");
560 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000561
562 return $Num;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000563}
564
565##----------------------------------------------------------------------------##
Ted Kremenekdab11102008-04-02 04:43:42 +0000566# RunBuildCommand - Run the build command.
567##----------------------------------------------------------------------------##
568
Ted Kremenek6b628982008-04-30 23:47:12 +0000569sub AddIfNotPresent {
570 my $Args = shift;
571 my $Arg = shift;
572 my $found = 0;
573
574 foreach my $k (@$Args) {
575 if ($k eq $Arg) {
576 $found = 1;
577 last;
578 }
579 }
580
581 if ($found == 0) {
582 push @$Args, $Arg;
583 }
584}
585
Ted Kremenekdab11102008-04-02 04:43:42 +0000586sub RunBuildCommand {
587
588 my $Args = shift;
Ted Kremenek7442ca62008-04-02 16:04:51 +0000589 my $IgnoreErrors = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000590 my $Cmd = $Args->[0];
Ted Kremenek6195c372008-06-02 21:52:47 +0000591 my $CCAnalyzer = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000592
Ted Kremenek3301cb12008-06-30 18:18:16 +0000593 # Get only the part of the command after the last '/'.
594 if ($Cmd =~ /\/([^\/]+)$/) {
595 $Cmd = $1;
596 }
597
Ted Kremenek6a43ba92008-04-02 15:34:12 +0000598 if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc") {
Ted Kremenekdab11102008-04-02 04:43:42 +0000599 shift @$Args;
Ted Kremenek6195c372008-06-02 21:52:47 +0000600 unshift @$Args, $CCAnalyzer;
Ted Kremenekdab11102008-04-02 04:43:42 +0000601 }
Ted Kremenek7442ca62008-04-02 16:04:51 +0000602 elsif ($IgnoreErrors) {
603 if ($Cmd eq "make" or $Cmd eq "gmake") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000604 AddIfNotPresent($Args,"-k");
Ted Kremenek8912b542008-05-13 21:28:02 +0000605 AddIfNotPresent($Args,"-i");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000606 }
607 elsif ($Cmd eq "xcodebuild") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000608 AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000609 }
Ted Kremenek6b628982008-04-30 23:47:12 +0000610 }
611
Ted Kremenek6b628982008-04-30 23:47:12 +0000612 if ($Cmd eq "xcodebuild") {
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000613 # Disable distributed builds for xcodebuild.
Ted Kremenek6b628982008-04-30 23:47:12 +0000614 AddIfNotPresent($Args,"-nodistribute");
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000615
616 # Disable PCH files until clang supports them.
617 AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO");
Ted Kremenek915e9722008-05-27 23:18:07 +0000618
619 # When 'CC' is set, xcodebuild uses it to do all linking, even if we are
620 # linking C++ object files. Set 'LDPLUSPLUS' so that xcodebuild uses 'g++'
621 # when linking such files.
622 my $LDPLUSPLUS = `which g++`;
623 $LDPLUSPLUS =~ s/\015?\012//; # strip newlines
624 $ENV{'LDPLUSPLUS'} = $LDPLUSPLUS;
Ted Kremenek6b628982008-04-30 23:47:12 +0000625 }
Ted Kremenekdab11102008-04-02 04:43:42 +0000626
Ted Kremenek5656a982008-07-15 17:09:28 +0000627 return system(@$Args);
Ted Kremenekdab11102008-04-02 04:43:42 +0000628}
629
Ted Kremenekdab11102008-04-02 04:43:42 +0000630##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000631# DisplayHelp - Utility function to display all help options.
632##----------------------------------------------------------------------------##
633
Sam Bishopa0e22662008-04-02 03:35:43 +0000634sub DisplayHelp {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000635
Ted Kremenek5744dc22008-04-02 18:03:36 +0000636print <<ENDTEXT;
Sam Bishopa0e22662008-04-02 03:35:43 +0000637USAGE: $Prog [options] <build command> [build options]
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000638
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000639ENDTEXT
640
641 if (defined($BuildName)) {
642 print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n";
643 }
644
645print <<ENDTEXT;
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000646OPTIONS:
647
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000648 -o - Target directory for HTML report files. Subdirectories
Sam Bishopa0e22662008-04-02 03:35:43 +0000649 will be created as needed to represent separate "runs" of
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000650 the analyzer. If this option is not specified, a directory
651 is created in /tmp to store the reports.
Ted Kremenek1262fc42008-05-14 20:10:33 +0000652
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000653 -h - Display this message.
654 --help
Ted Kremenek1262fc42008-05-14 20:10:33 +0000655
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000656 -k - Add a "keep on going" option to the specified build command.
657 --keep-going This option currently supports make and xcodebuild.
Ted Kremenekf02e8db2008-04-02 16:41:25 +0000658 This is a convenience option; one can specify this
659 behavior directly using build options.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000660
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000661 --status-bugs - By default, the exit status of $Prog is the same as the
662 executed build command. Specifying this option causes the
663 exit status of $Prog to be 1 if it found potential bugs
664 and 0 otherwise.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000665
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000666 -v - Verbose output from $Prog and the analyzer.
667 A second and third "-v" increases verbosity.
668
669 -V - View analysis results in a web browser when the build
670 --view completes.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000671
Ted Kremenekb7770c02008-07-15 17:06:13 +0000672ENDTEXT
673
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000674 print " Available Source Code Analyses (multiple analyses may be specified):\n\n";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000675
676 foreach my $Analysis (sort keys %AvailableAnalyses) {
677 if (defined($AnalysesDefaultEnabled{$Analysis})) {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000678 print " (+)";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000679 }
680 else {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000681 print " ";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000682 }
683
684 print " $Analysis $AvailableAnalyses{$Analysis}\n";
685 }
686
687print <<ENDTEXT
688
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000689 NOTE: "(+)" indicates that an analysis is enabled by default unless one
690 or more analysis options are specified
Ted Kremenekb7770c02008-07-15 17:06:13 +0000691
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000692BUILD OPTIONS
693
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000694 You can specify any build option acceptable to the build command.
Ted Kremenek39eefde2008-04-02 16:47:27 +0000695
Ted Kremenek5744dc22008-04-02 18:03:36 +0000696EXAMPLE
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000697
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000698 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000699
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000700 The above example causes analysis reports to be deposited into
701 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
702 A different subdirectory is created each time $Prog analyzes a project.
703 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000704
705ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000706}
707
708##----------------------------------------------------------------------------##
709# Process command-line arguments.
710##----------------------------------------------------------------------------##
711
712my $HtmlDir; # Parent directory to store HTML files.
713my $IgnoreErrors = 0; # Ignore build errors.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000714my $ViewResults = 0; # View results when the build terminates.
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000715my $ExitStatusFoundBugs = 0; # Exit status reflects whether bugs were found
Ted Kremenekb7770c02008-07-15 17:06:13 +0000716my @AnalysesToRun;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000717
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000718
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000719if (!@ARGV) {
720 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000721 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000722}
723
724while (@ARGV) {
725
726 # Scan for options we recognize.
727
728 my $arg = $ARGV[0];
729
Sam Bishop2f2418e2008-04-03 14:29:47 +0000730 if ($arg eq "-h" or $arg eq "--help") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000731 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000732 exit 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000733 }
734
Ted Kremenekb7770c02008-07-15 17:06:13 +0000735 if (defined($AvailableAnalyses{$arg})) {
Ted Kremenek1262fc42008-05-14 20:10:33 +0000736 shift @ARGV;
Ted Kremenekb7770c02008-07-15 17:06:13 +0000737 push @AnalysesToRun, $arg;
Ted Kremenek1262fc42008-05-14 20:10:33 +0000738 next;
739 }
740
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000741 if ($arg eq "-o") {
742 shift @ARGV;
743
744 if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000745 DieDiag("'-o' option requires a target directory name.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000746 }
747
748 $HtmlDir = shift @ARGV;
749 next;
750 }
751
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000752 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000753 shift @ARGV;
754 $IgnoreErrors = 1;
755 next;
756 }
757
758 if ($arg eq "-v") {
759 shift @ARGV;
760 $Verbose++;
761 next;
762 }
763
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000764 if ($arg eq "-V" or $arg eq "--view") {
765 shift @ARGV;
766 $ViewResults = 1;
767 next;
768 }
769
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000770 if ($arg eq "--status-bugs") {
771 shift @ARGV;
772 $ExitStatusFoundBugs = 1;
773 next;
774 }
775
Ted Kremenek23cfca32008-06-16 22:40:14 +0000776 DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/);
Ted Kremenek0062ad42008-04-02 16:35:01 +0000777
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000778 last;
779}
780
781if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000782 Diag("No build command specified.\n\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000783 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000784 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000785}
786
787# Determine the output directory for the HTML reports.
788
789if (!defined($HtmlDir)) {
790
Sam Bishopa0e22662008-04-02 03:35:43 +0000791 $HtmlDir = mkdtemp("/tmp/$Prog-XXXXXX");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000792
793 if (!defined($HtmlDir)) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000794 DieDiag("Cannot create HTML directory in /tmp.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000795 }
796
797 if (!$Verbose) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000798 Diag("Using '$HtmlDir' as base HTML report directory.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000799 }
800}
801
Ted Kremenek684bb092008-04-18 15:18:20 +0000802my $BaseDir = $HtmlDir;
Sam Bishopa0e22662008-04-02 03:35:43 +0000803$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000804
805# Set the appropriate environment variables.
806
Sam Bishopa0e22662008-04-02 03:35:43 +0000807SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000808
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000809my $Cmd = "$RealBin/ccc-analyzer";
810
Ted Kremenek23cfca32008-06-16 22:40:14 +0000811DieDiag("Executable 'ccc-analyzer' does not exist at '$Cmd'\n")
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000812 if (! -x $Cmd);
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000813
Ted Kremenekb7770c02008-07-15 17:06:13 +0000814if (! -x $ClangSB) {
815 Diag("'clang' executable not found in '$RealBin'.\n");
816 Diag("Using 'clang' from path.\n");
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000817}
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000818
Ted Kremenek4f4b17d2008-04-03 20:08:18 +0000819$ENV{'CC'} = $Cmd;
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000820$ENV{'CLANG'} = $Clang;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000821
822if ($Verbose >= 2) {
823 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
824}
825
Ted Kremeneka9525c92008-05-12 22:07:14 +0000826if ($Verbose >= 3) {
827 $ENV{'CCC_ANALYZER_LOG'} = 1;
828}
829
Ted Kremenekb7770c02008-07-15 17:06:13 +0000830if (scalar(@AnalysesToRun)) {
831 $ENV{'CCC_ANALYZER_ANALYSIS'} = join ' ',@AnalysesToRun;
Ted Kremenek01006782008-07-02 23:16:10 +0000832}
Ted Kremenek1262fc42008-05-14 20:10:33 +0000833
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000834# Run the build.
835
Ted Kremenek5656a982008-07-15 17:09:28 +0000836my $ExitStatus = RunBuildCommand(\@ARGV, $IgnoreErrors, $Cmd);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000837
838# Postprocess the HTML directory.
839
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000840my $NumBugs = Postprocess($HtmlDir, $BaseDir);
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000841
842if ($ViewResults and -r "$HtmlDir/index.html") {
843 # Only works on Mac OS X (for now).
844 print "Viewing analysis results: '$HtmlDir/index.html'\n";
Ted Kremenek20161e92008-07-15 20:18:21 +0000845 system("open", "$HtmlDir/index.html");
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000846}
Ted Kremenek5656a982008-07-15 17:09:28 +0000847
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000848if ($ExitStatusFoundBugs) {
849 exit 1 if ($NumBugs > 0);
850 exit 0;
851}
852
Ted Kremenek5656a982008-07-15 17:09:28 +0000853exit $ExitStatus;
854