blob: e23d1903a478d4727e65e5d13b3641661a9c9d8d [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);
272 `mv $fname.tmp $fname`;
273}
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.
297 `rm -f $Dir/$FName`;
298 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 Kremenek684bb092008-04-18 15:18:20 +0000304 `chmod 644 $Dir/$FName`;
305
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
347 `cp $RealBin/sorttable.js $Dir`;
348
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 Kremenek9cc8c2c2008-04-01 20:47:38 +0000367 return;
368 }
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 Kremenek9cc8c2c2008-04-01 20:47:38 +0000376 `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').
379 `rm -f $BaseDir`;
380
381 Diag("No bugs found.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000382 return;
383 }
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 Kremenek22d6a632008-04-02 20:43:36 +0000549
Ted Kremenek22d6a632008-04-02 20:43:36 +0000550 CopyJS($Dir);
Ted Kremenek684bb092008-04-18 15:18:20 +0000551
552 # Make sure $Dir and $BaseDir is world readable/executable.
553 `chmod 755 $Dir`;
554 `chmod 755 $BaseDir`;
Ted Kremenek23cfca32008-06-16 22:40:14 +0000555
556 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 Kremenek9cc8c2c2008-04-01 20:47:38 +0000561}
562
563##----------------------------------------------------------------------------##
Ted Kremenekdab11102008-04-02 04:43:42 +0000564# RunBuildCommand - Run the build command.
565##----------------------------------------------------------------------------##
566
Ted Kremenek6b628982008-04-30 23:47:12 +0000567sub AddIfNotPresent {
568 my $Args = shift;
569 my $Arg = shift;
570 my $found = 0;
571
572 foreach my $k (@$Args) {
573 if ($k eq $Arg) {
574 $found = 1;
575 last;
576 }
577 }
578
579 if ($found == 0) {
580 push @$Args, $Arg;
581 }
582}
583
Ted Kremenekdab11102008-04-02 04:43:42 +0000584sub RunBuildCommand {
585
586 my $Args = shift;
Ted Kremenek7442ca62008-04-02 16:04:51 +0000587 my $IgnoreErrors = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000588 my $Cmd = $Args->[0];
Ted Kremenek6195c372008-06-02 21:52:47 +0000589 my $CCAnalyzer = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000590
Ted Kremenek3301cb12008-06-30 18:18:16 +0000591 # Get only the part of the command after the last '/'.
592 if ($Cmd =~ /\/([^\/]+)$/) {
593 $Cmd = $1;
594 }
595
Ted Kremenek6a43ba92008-04-02 15:34:12 +0000596 if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc") {
Ted Kremenekdab11102008-04-02 04:43:42 +0000597 shift @$Args;
Ted Kremenek6195c372008-06-02 21:52:47 +0000598 unshift @$Args, $CCAnalyzer;
Ted Kremenekdab11102008-04-02 04:43:42 +0000599 }
Ted Kremenek7442ca62008-04-02 16:04:51 +0000600 elsif ($IgnoreErrors) {
601 if ($Cmd eq "make" or $Cmd eq "gmake") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000602 AddIfNotPresent($Args,"-k");
Ted Kremenek8912b542008-05-13 21:28:02 +0000603 AddIfNotPresent($Args,"-i");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000604 }
605 elsif ($Cmd eq "xcodebuild") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000606 AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000607 }
Ted Kremenek6b628982008-04-30 23:47:12 +0000608 }
609
Ted Kremenek6b628982008-04-30 23:47:12 +0000610 if ($Cmd eq "xcodebuild") {
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000611 # Disable distributed builds for xcodebuild.
Ted Kremenek6b628982008-04-30 23:47:12 +0000612 AddIfNotPresent($Args,"-nodistribute");
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000613
614 # Disable PCH files until clang supports them.
615 AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO");
Ted Kremenek915e9722008-05-27 23:18:07 +0000616
617 # When 'CC' is set, xcodebuild uses it to do all linking, even if we are
618 # linking C++ object files. Set 'LDPLUSPLUS' so that xcodebuild uses 'g++'
619 # when linking such files.
620 my $LDPLUSPLUS = `which g++`;
621 $LDPLUSPLUS =~ s/\015?\012//; # strip newlines
622 $ENV{'LDPLUSPLUS'} = $LDPLUSPLUS;
Ted Kremenek6b628982008-04-30 23:47:12 +0000623 }
Ted Kremenekdab11102008-04-02 04:43:42 +0000624
Ted Kremenek5656a982008-07-15 17:09:28 +0000625 return system(@$Args);
Ted Kremenekdab11102008-04-02 04:43:42 +0000626}
627
Ted Kremenekdab11102008-04-02 04:43:42 +0000628##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000629# DisplayHelp - Utility function to display all help options.
630##----------------------------------------------------------------------------##
631
Sam Bishopa0e22662008-04-02 03:35:43 +0000632sub DisplayHelp {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000633
Ted Kremenek5744dc22008-04-02 18:03:36 +0000634print <<ENDTEXT;
Sam Bishopa0e22662008-04-02 03:35:43 +0000635USAGE: $Prog [options] <build command> [build options]
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000636
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000637ENDTEXT
638
639 if (defined($BuildName)) {
640 print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n";
641 }
642
643print <<ENDTEXT;
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000644OPTIONS:
645
646 -o - Target directory for HTML report files. Subdirectories
Sam Bishopa0e22662008-04-02 03:35:43 +0000647 will be created as needed to represent separate "runs" of
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000648 the analyzer. If this option is not specified, a directory
649 is created in /tmp to store the reports.
Ted Kremenek1262fc42008-05-14 20:10:33 +0000650
Ted Kremenek10f883f2008-04-03 07:11:44 +0000651 -h - Display this message.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000652 --help
Ted Kremenek1262fc42008-05-14 20:10:33 +0000653
Ted Kremenekaf08f642008-04-02 16:31:58 +0000654 -k - Add a "keep on going" option to the specified build command.
Ted Kremenekf02e8db2008-04-02 16:41:25 +0000655 --keep-going This option currently supports make and xcodebuild.
656 This is a convenience option; one can specify this
657 behavior directly using build options.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000658
Ted Kremenekdab11102008-04-02 04:43:42 +0000659 -v - Verbose output from $Prog and the analyzer.
660 A second "-v" increases verbosity.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000661
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000662 -V - View analysis results in a web browser when the build
663 --view completes.
664
Ted Kremenekb7770c02008-07-15 17:06:13 +0000665ENDTEXT
666
667 print " Available Source Code Analyses (multiple analyses may be specified):\n\n";
668
669 foreach my $Analysis (sort keys %AvailableAnalyses) {
670 if (defined($AnalysesDefaultEnabled{$Analysis})) {
671 print " (+)";
672 }
673 else {
674 print " ";
675 }
676
677 print " $Analysis $AvailableAnalyses{$Analysis}\n";
678 }
679
680print <<ENDTEXT
681
682 (+) == analysis enabled by default unless one
683 or more analysis options are specified
684
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000685BUILD OPTIONS
686
Ted Kremenek39eefde2008-04-02 16:47:27 +0000687 You can specify any build option acceptable to the build command.
688
Ted Kremenek5744dc22008-04-02 18:03:36 +0000689EXAMPLE
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000690
Ted Kremenek5744dc22008-04-02 18:03:36 +0000691 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000692
Ted Kremenek39eefde2008-04-02 16:47:27 +0000693 The above example causes analysis reports to be deposited into
694 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
695 A different subdirectory is created each time $Prog analyzes a project.
696 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000697
698ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000699}
700
701##----------------------------------------------------------------------------##
702# Process command-line arguments.
703##----------------------------------------------------------------------------##
704
705my $HtmlDir; # Parent directory to store HTML files.
706my $IgnoreErrors = 0; # Ignore build errors.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000707my $ViewResults = 0; # View results when the build terminates.
Ted Kremenekb7770c02008-07-15 17:06:13 +0000708my @AnalysesToRun;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000709
710if (!@ARGV) {
711 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000712 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000713}
714
715while (@ARGV) {
716
717 # Scan for options we recognize.
718
719 my $arg = $ARGV[0];
720
Sam Bishop2f2418e2008-04-03 14:29:47 +0000721 if ($arg eq "-h" or $arg eq "--help") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000722 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000723 exit 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000724 }
725
Ted Kremenekb7770c02008-07-15 17:06:13 +0000726 if (defined($AvailableAnalyses{$arg})) {
Ted Kremenek1262fc42008-05-14 20:10:33 +0000727 shift @ARGV;
Ted Kremenekb7770c02008-07-15 17:06:13 +0000728 push @AnalysesToRun, $arg;
Ted Kremenek1262fc42008-05-14 20:10:33 +0000729 next;
730 }
731
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000732 if ($arg eq "-o") {
733 shift @ARGV;
734
735 if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000736 DieDiag("'-o' option requires a target directory name.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000737 }
738
739 $HtmlDir = shift @ARGV;
740 next;
741 }
742
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000743 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000744 shift @ARGV;
745 $IgnoreErrors = 1;
746 next;
747 }
748
749 if ($arg eq "-v") {
750 shift @ARGV;
751 $Verbose++;
752 next;
753 }
754
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000755 if ($arg eq "-V" or $arg eq "--view") {
756 shift @ARGV;
757 $ViewResults = 1;
758 next;
759 }
760
Ted Kremenek23cfca32008-06-16 22:40:14 +0000761 DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/);
Ted Kremenek0062ad42008-04-02 16:35:01 +0000762
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000763 last;
764}
765
766if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000767 Diag("No build command specified.\n\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000768 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000769 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000770}
771
772# Determine the output directory for the HTML reports.
773
774if (!defined($HtmlDir)) {
775
Sam Bishopa0e22662008-04-02 03:35:43 +0000776 $HtmlDir = mkdtemp("/tmp/$Prog-XXXXXX");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000777
778 if (!defined($HtmlDir)) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000779 DieDiag("Cannot create HTML directory in /tmp.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000780 }
781
782 if (!$Verbose) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000783 Diag("Using '$HtmlDir' as base HTML report directory.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000784 }
785}
786
Ted Kremenek684bb092008-04-18 15:18:20 +0000787my $BaseDir = $HtmlDir;
Sam Bishopa0e22662008-04-02 03:35:43 +0000788$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000789
790# Set the appropriate environment variables.
791
Sam Bishopa0e22662008-04-02 03:35:43 +0000792SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000793
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000794my $Cmd = "$RealBin/ccc-analyzer";
795
Ted Kremenek23cfca32008-06-16 22:40:14 +0000796DieDiag("Executable 'ccc-analyzer' does not exist at '$Cmd'\n")
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000797 if (! -x $Cmd);
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000798
Ted Kremenekb7770c02008-07-15 17:06:13 +0000799if (! -x $ClangSB) {
800 Diag("'clang' executable not found in '$RealBin'.\n");
801 Diag("Using 'clang' from path.\n");
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000802}
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000803
Ted Kremenek4f4b17d2008-04-03 20:08:18 +0000804$ENV{'CC'} = $Cmd;
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000805$ENV{'CLANG'} = $Clang;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000806
807if ($Verbose >= 2) {
808 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
809}
810
Ted Kremeneka9525c92008-05-12 22:07:14 +0000811if ($Verbose >= 3) {
812 $ENV{'CCC_ANALYZER_LOG'} = 1;
813}
814
Ted Kremenekb7770c02008-07-15 17:06:13 +0000815if (scalar(@AnalysesToRun)) {
816 $ENV{'CCC_ANALYZER_ANALYSIS'} = join ' ',@AnalysesToRun;
Ted Kremenek01006782008-07-02 23:16:10 +0000817}
Ted Kremenek1262fc42008-05-14 20:10:33 +0000818
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000819# Run the build.
820
Ted Kremenek5656a982008-07-15 17:09:28 +0000821my $ExitStatus = RunBuildCommand(\@ARGV, $IgnoreErrors, $Cmd);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000822
823# Postprocess the HTML directory.
824
Ted Kremenek684bb092008-04-18 15:18:20 +0000825Postprocess($HtmlDir, $BaseDir);
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000826
827if ($ViewResults and -r "$HtmlDir/index.html") {
828 # Only works on Mac OS X (for now).
829 print "Viewing analysis results: '$HtmlDir/index.html'\n";
830 `open $HtmlDir/index.html`
831}
Ted Kremenek5656a982008-07-15 17:09:28 +0000832
833exit $ExitStatus;
834