blob: fd196ba67c0b4cd99451ef1c3cf33725f12ef4a0 [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.
Ted Kremenek2be18532008-07-18 23:13:03 +000072open(PIPE, "'$Clang' --help |") or
Ted Kremenekb7770c02008-07-15 17:06:13 +000073 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,
Ted Kremenekbde3a052008-07-25 20:35:01 +0000103 '-warn-objc-missing-dealloc' => 1,
104 '-warn-objc-unused-ivars' => 1
Ted Kremenekb7770c02008-07-15 17:06:13 +0000105);
106
107##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000108# GetHTMLRunDir - Construct an HTML directory name for the current run.
109##----------------------------------------------------------------------------##
110
Sam Bishopa0e22662008-04-02 03:35:43 +0000111sub GetHTMLRunDir {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000112
113 die "Not enough arguments." if (@_ == 0);
114
115 my $Dir = shift @_;
116
117 # Get current date and time.
118
119 my @CurrentTime = localtime();
120
121 my $year = $CurrentTime[5] + 1900;
122 my $day = $CurrentTime[3];
123 my $month = $CurrentTime[4] + 1;
124
Ted Kremenek9d7405f2008-05-14 17:23:56 +0000125 my $DateString = sprintf("%d-%02d-%02d", $year, $month, $day);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000126
127 # Determine the run number.
128
129 my $RunNumber;
130
131 if (-d $Dir) {
132
133 if (! -r $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000134 DieDiag("directory '$Dir' exists but is not readable.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000135 }
136
137 # Iterate over all files in the specified directory.
138
139 my $max = 0;
140
141 opendir(DIR, $Dir);
142 my @FILES= readdir(DIR);
143 closedir(DIR);
144
145 foreach my $f (@FILES) {
146
147 my @x = split/-/, $f;
148
149 next if (scalar(@x) != 4);
150 next if ($x[0] != $year);
151 next if ($x[1] != $month);
152 next if ($x[2] != $day);
153
154 if ($x[3] > $max) {
155 $max = $x[3];
156 }
157 }
158
159 $RunNumber = $max + 1;
160 }
161 else {
162
163 if (-x $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000164 DieDiag("'$Dir' exists but is not a directory.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000165 }
166
167 # $Dir does not exist. It will be automatically created by the
168 # clang driver. Set the run number to 1.
169
170 $RunNumber = 1;
171 }
172
173 die "RunNumber must be defined!" if (!defined($RunNumber));
174
175 # Append the run number.
176
177 return "$Dir/$DateString-$RunNumber";
178}
179
Sam Bishopa0e22662008-04-02 03:35:43 +0000180sub SetHtmlEnv {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000181
182 die "Wrong number of arguments." if (scalar(@_) != 2);
183
184 my $Args = shift;
185 my $Dir = shift;
186
187 die "No build command." if (scalar(@$Args) == 0);
188
189 my $Cmd = $$Args[0];
190
191 if ($Cmd =~ /configure/) {
192 return;
193 }
194
195 if ($Verbose) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000196 Diag("Emitting reports for this run to '$Dir'.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000197 }
198
199 $ENV{'CCC_ANALYZER_HTML'} = $Dir;
200}
201
202##----------------------------------------------------------------------------##
Ted Kremenek57cf4462008-04-18 15:09:30 +0000203# ComputeDigest - Compute a digest of the specified file.
204##----------------------------------------------------------------------------##
205
206sub ComputeDigest {
207 my $FName = shift;
Ted Kremenek23cfca32008-06-16 22:40:14 +0000208 DieDiag("Cannot read $FName to compute Digest.\n") if (! -r $FName);
Ted Kremeneka6e24812008-04-19 18:05:48 +0000209
210 # Use Digest::MD5. We don't have to be cryptographically secure. We're
Ted Kremenek7ea02e62008-04-19 18:07:44 +0000211 # just looking for duplicate files that come from a non-malicious source.
212 # We use Digest::MD5 because it is a standard Perl module that should
Ted Kremeneka6e24812008-04-19 18:05:48 +0000213 # come bundled on most systems.
214
Ted Kremenek23cfca32008-06-16 22:40:14 +0000215 open(FILE, $FName) or DieDiag("Cannot open $FName when computing Digest.\n");
Ted Kremeneka6e24812008-04-19 18:05:48 +0000216 binmode FILE;
217 my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest;
218 close(FILE);
219
220 # Return the digest.
221
222 return $Result;
Ted Kremenek57cf4462008-04-18 15:09:30 +0000223}
224
225##----------------------------------------------------------------------------##
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000226# UpdatePrefix - Compute the common prefix of files.
227##----------------------------------------------------------------------------##
228
229my $Prefix;
230
231sub UpdatePrefix {
232
233 my $x = shift;
234 my $y = basename($x);
235 $x =~ s/\Q$y\E$//;
236
237 # Ignore /usr, /Library, /System, /Developer
238
239 return if ( $x =~ /^\/usr/ or $x =~ /^\/Library/
240 or $x =~ /^\/System/ or $x =~ /^\/Developer/);
241
242
243 if (!defined $Prefix) {
244 $Prefix = $x;
245 return;
246 }
247
248 chop $Prefix while (!($x =~ /^$Prefix/));
249}
250
251sub GetPrefix {
252 return $Prefix;
253}
254
255##----------------------------------------------------------------------------##
256# UpdateInFilePath - Update the path in the report file.
257##----------------------------------------------------------------------------##
258
259sub UpdateInFilePath {
260 my $fname = shift;
261 my $regex = shift;
262 my $newtext = shift;
263
264 open (RIN, $fname) or die "cannot open $fname";
265 open (ROUT, ">$fname.tmp") or die "cannot open $fname.tmp";
266
267 while (<RIN>) {
268 s/$regex/$newtext/;
269 print ROUT $_;
270 }
271
272 close (ROUT);
273 close (RIN);
Ted Kremenek20161e92008-07-15 20:18:21 +0000274 system("mv", "$fname.tmp", $fname);
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000275}
276
277##----------------------------------------------------------------------------##
Ted Kremenek5744dc22008-04-02 18:03:36 +0000278# ScanFile - Scan a report file for various identifying attributes.
279##----------------------------------------------------------------------------##
280
Ted Kremenek57cf4462008-04-18 15:09:30 +0000281# Sometimes a source file is scanned more than once, and thus produces
282# multiple error reports. We use a cache to solve this problem.
283
284my %AlreadyScanned;
285
Ted Kremenek5744dc22008-04-02 18:03:36 +0000286sub ScanFile {
287
288 my $Index = shift;
289 my $Dir = shift;
290 my $FName = shift;
291
Ted Kremenek57cf4462008-04-18 15:09:30 +0000292 # Compute a digest for the report file. Determine if we have already
293 # scanned a file that looks just like it.
294
295 my $digest = ComputeDigest("$Dir/$FName");
296
297 if (defined($AlreadyScanned{$digest})) {
298 # Redundant file. Remove it.
Ted Kremenek20161e92008-07-15 20:18:21 +0000299 system ("rm", "-f", "$Dir/$FName");
Ted Kremenek57cf4462008-04-18 15:09:30 +0000300 return;
301 }
302
303 $AlreadyScanned{$digest} = 1;
304
Ted Kremenek809709f2008-04-18 16:58:34 +0000305 # At this point the report file is not world readable. Make it happen.
Ted Kremenek20161e92008-07-15 20:18:21 +0000306 system ("chmod", "644", "$Dir/$FName");
Ted Kremenek684bb092008-04-18 15:18:20 +0000307
308 # Scan the report file for tags.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000309 open(IN, "$Dir/$FName") or DieDiag("Cannot open '$Dir/$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000310
311 my $BugDesc = "";
Ted Kremenek22d6a632008-04-02 20:43:36 +0000312 my $BugFile = "";
313 my $BugPathLength = 1;
314 my $BugLine = 0;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000315
316 while (<IN>) {
317
318 if (/<!-- BUGDESC (.*) -->$/) {
319 $BugDesc = $1;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000320 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000321 elsif (/<!-- BUGFILE (.*) -->$/) {
322 $BugFile = $1;
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000323 UpdatePrefix($BugFile);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000324 }
325 elsif (/<!-- BUGPATHLENGTH (.*) -->$/) {
326 $BugPathLength = $1;
327 }
328 elsif (/<!-- BUGLINE (.*) -->$/) {
329 $BugLine = $1;
330 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000331 }
332
333 close(IN);
334
Ted Kremenek22d6a632008-04-02 20:43:36 +0000335 push @$Index,[ $FName, $BugDesc, $BugFile, $BugLine, $BugPathLength ];
336}
337
338##----------------------------------------------------------------------------##
339# CopyJS - Copy JavaScript code to target directory.
340##----------------------------------------------------------------------------##
341
342sub CopyJS {
343
344 my $Dir = shift;
345
Ted Kremenek23cfca32008-06-16 22:40:14 +0000346 DieDiag("Cannot find 'sorttable.js'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000347 if (! -r "$RealBin/sorttable.js");
348
Ted Kremenek20161e92008-07-15 20:18:21 +0000349 system ("cp", "$RealBin/sorttable.js", "$Dir");
Ted Kremenek22d6a632008-04-02 20:43:36 +0000350
Ted Kremenek23cfca32008-06-16 22:40:14 +0000351 DieDiag("Could not copy 'sorttable.js' to '$Dir'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000352 if (! -r "$Dir/sorttable.js");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000353}
354
355##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000356# Postprocess - Postprocess the results of an analysis scan.
357##----------------------------------------------------------------------------##
358
Sam Bishopa0e22662008-04-02 03:35:43 +0000359sub Postprocess {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000360
361 my $Dir = shift;
Ted Kremenek684bb092008-04-18 15:18:20 +0000362 my $BaseDir = shift;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000363
364 die "No directory specified." if (!defined($Dir));
Ted Kremenek684bb092008-04-18 15:18:20 +0000365 die "No base directory specified." if (!defined($BaseDir));
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000366
367 if (! -d $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000368 Diag("No bugs found.\n");
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000369 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000370 }
371
372 opendir(DIR, $Dir);
373 my @files = grep(/^report-.*\.html$/,readdir(DIR));
374 closedir(DIR);
375
376 if (scalar(@files) == 0) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000377 Diag("Removing directory '$Dir' because it contains no reports.\n");
Ted Kremenek20161e92008-07-15 20:18:21 +0000378 system ("rm", "-fR", $Dir);
Ted Kremenek23cfca32008-06-16 22:40:14 +0000379
380 # Remove the base directory if it contains no files (don't use '-R').
Ted Kremenek20161e92008-07-15 20:18:21 +0000381 system ("rm", "-f", $BaseDir);
Ted Kremenek23cfca32008-06-16 22:40:14 +0000382
383 Diag("No bugs found.\n");
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000384 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000385 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000386
387 # Scan each report file and build an index.
388
389 my @Index;
390
391 foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); }
392
393 # Generate an index.html file.
394
395 my $FName = "$Dir/index.html";
396
Ted Kremenek23cfca32008-06-16 22:40:14 +0000397 open(OUT, ">$FName") or DieDiag("Cannot create file '$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000398
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000399 # Print out the header.
400
Ted Kremenek5744dc22008-04-02 18:03:36 +0000401print OUT <<ENDTEXT;
402<html>
403<head>
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000404<style type="text/css">
405 body { color:#000000; background-color:#ffffff }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000406 body { font-family: Helvetica, sans-serif; font-size:9pt }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000407 h1 { font-size:12pt }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000408 table.sortable thead {
409 background-color:#eee; color:#666666;
410 font-weight: bold; cursor: default;
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000411 text-align:center;
412 border-top: 2px solid #000000;
413 border-bottom: 2px solid #000000;
414 font-weight: bold; font-family: Verdana
415 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000416 table.sortable { border: 1px #000000 solid }
417 table.sortable { border-collapse: collapse; border-spacing: 0px }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000418 td { border-bottom: 1px #000000 dotted }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000419 td { padding:5px; padding-left:8px; padding-right:8px }
Ted Kremenekd8c6d0c2008-04-07 23:50:07 +0000420 td { text-align:left; font-size:9pt }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000421 td.View { padding-left: 10px }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000422</style>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000423<script src="sorttable.js"></script>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000424<script language='javascript' type="text/javascript">
425function SetDisplay(RowClass, DisplayVal)
426{
427 var Rows = document.getElementsByTagName("tr");
428 for ( var i = 0 ; i < Rows.length; ++i ) {
429 if (Rows[i].className == RowClass) {
430 Rows[i].style.display = DisplayVal;
431 }
432 }
433}
434
435function ToggleDisplay(CheckButton, ClassName) {
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000436 if (CheckButton.checked) {
437 SetDisplay(ClassName, "");
438 }
439 else {
440 SetDisplay(ClassName, "none");
441 }
442}
443</script>
444</head>
445<body>
446ENDTEXT
447
448 # Print out the summary table.
449
450 my %Totals;
451
452 for my $row ( @Index ) {
453
Ted Kremenek432af592008-05-06 18:11:36 +0000454 #my $bug_type = lc($row->[1]);
455 my $bug_type = ($row->[1]);
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000456
457 if (!defined($Totals{$bug_type})) {
458 $Totals{$bug_type} = 1;
459 }
460 else {
461 $Totals{$bug_type}++;
462 }
463 }
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000464
465 print OUT "<h3>Summary</h3>";
466
467 if (defined($BuildName)) {
468 print OUT "\n<p>Results in this analysis run are based on analyzer build <b>$BuildName</b>.</p>\n"
469 }
470
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000471print OUT <<ENDTEXT;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000472<table class="sortable">
473<tr>
474 <td>Bug Type</td>
475 <td>Quantity</td>
Ted Kremenek2645c772008-07-07 16:58:44 +0000476 <td class="sorttable_nosort">Display?</td>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000477</tr>
478ENDTEXT
479
480 for my $key ( sort { $a cmp $b } keys %Totals ) {
Ted Kremenekbdf66c72008-05-06 23:51:45 +0000481 my $x = lc($key);
482 $x =~ s/\s[,]/_/g;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000483 print OUT "<tr><td>$key</td><td>$Totals{$key}</td><td><input type=\"checkbox\" onClick=\"ToggleDisplay(this,'bt_$x');\" checked/></td></tr>\n";
484 }
485
486 # Print out the table of errors.
487
488print OUT <<ENDTEXT;
489</table>
490<h3>Reports</h3>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000491<table class="sortable">
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000492<tr>
Ted Kremenek88a96d62008-07-07 17:23:32 +0000493 <td class="sorttable_sorted">Bug Type<span id="sorttable_sortfwdind">&nbsp;&#x25BE;</span>
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000494 <td>File</td>
495 <td>Line</td>
496 <td>Path Length</td>
Ted Kremenek2645c772008-07-07 16:58:44 +0000497 <td class="sorttable_nosort"></td>
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000498</tr>
Ted Kremenek5744dc22008-04-02 18:03:36 +0000499ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000500
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000501 my $prefix = GetPrefix();
502 my $regex;
503 my $InFileRegex;
504 my $InFilePrefix = "File:</td><td>";
505
506 if (defined($prefix)) {
507 $regex = qr/^\Q$prefix\E/is;
508 $InFileRegex = qr/\Q$InFilePrefix$prefix\E/is;
509 }
510
Ted Kremenek5744dc22008-04-02 18:03:36 +0000511 for my $row ( sort { $a->[1] cmp $b->[1] } @Index ) {
512
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000513 my $x = lc($row->[1]);
Ted Kremenekbdf66c72008-05-06 23:51:45 +0000514 $x =~ s/\s[,]/_/g;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000515
516 print OUT "<tr class=\"bt_$x\">\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000517
Ted Kremenek5744dc22008-04-02 18:03:36 +0000518 my $ReportFile = $row->[0];
519
Ted Kremenek22d6a632008-04-02 20:43:36 +0000520 print OUT " <td class=\"DESC\">";
Ted Kremenek432af592008-05-06 18:11:36 +0000521 #print OUT lc($row->[1]);
522 print OUT $row->[1];
Ted Kremenek22d6a632008-04-02 20:43:36 +0000523 print OUT "</td>\n";
Ted Kremenek5744dc22008-04-02 18:03:36 +0000524
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000525 # Update the file prefix.
526
527 my $fname = $row->[2];
528 if (defined($regex)) {
529 $fname =~ s/$regex//;
530 UpdateInFilePath("$Dir/$ReportFile", $InFileRegex, $InFilePrefix)
531 }
Ted Kremenek3e56e0b2008-05-02 23:40:49 +0000532
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000533 print OUT "<td>$fname</td>\n";
534
535 # Print the rest of the columns.
536
537 for my $j ( 3 .. $#{$row} ) {
Ted Kremenek5744dc22008-04-02 18:03:36 +0000538 print OUT "<td>$row->[$j]</td>\n"
539 }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000540
541 # Emit the "View" link.
Ted Kremenek5744dc22008-04-02 18:03:36 +0000542
Ted Kremenek22d6a632008-04-02 20:43:36 +0000543 print OUT " <td class=\"View\"><a href=\"$ReportFile#EndPath\">View</a></td>\n";
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000544
545 # End the row.
Ted Kremenek5744dc22008-04-02 18:03:36 +0000546 print OUT "</tr>\n";
547 }
548
549 print OUT "</table>\n</body></html>\n";
550 close(OUT);
Ted Kremenek20161e92008-07-15 20:18:21 +0000551
Ted Kremenek22d6a632008-04-02 20:43:36 +0000552 CopyJS($Dir);
Ted Kremenek20161e92008-07-15 20:18:21 +0000553
554 # Make sure $Dir and $BaseDir are world readable/executable.
555 system("chmod", "755", $Dir);
556 system("chmod", "755", $BaseDir);
557
Ted Kremenek23cfca32008-06-16 22:40:14 +0000558 my $Num = scalar(@Index);
Ted Kremenek150c2122008-07-11 19:15:05 +0000559 Diag("$Num bugs found.\n");
560 if ($Num > 0 && -r "$Dir/index.html") {
561 Diag("Open '$Dir/index.html' to examine bug reports.\n");
562 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000563
564 return $Num;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000565}
566
567##----------------------------------------------------------------------------##
Ted Kremenekdab11102008-04-02 04:43:42 +0000568# RunBuildCommand - Run the build command.
569##----------------------------------------------------------------------------##
570
Ted Kremenek6b628982008-04-30 23:47:12 +0000571sub AddIfNotPresent {
572 my $Args = shift;
573 my $Arg = shift;
574 my $found = 0;
575
576 foreach my $k (@$Args) {
577 if ($k eq $Arg) {
578 $found = 1;
579 last;
580 }
581 }
582
583 if ($found == 0) {
584 push @$Args, $Arg;
585 }
586}
587
Ted Kremenekdab11102008-04-02 04:43:42 +0000588sub RunBuildCommand {
589
590 my $Args = shift;
Ted Kremenek7442ca62008-04-02 16:04:51 +0000591 my $IgnoreErrors = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000592 my $Cmd = $Args->[0];
Ted Kremenek6195c372008-06-02 21:52:47 +0000593 my $CCAnalyzer = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000594
Ted Kremenek3301cb12008-06-30 18:18:16 +0000595 # Get only the part of the command after the last '/'.
596 if ($Cmd =~ /\/([^\/]+)$/) {
597 $Cmd = $1;
598 }
599
Ted Kremenek6a43ba92008-04-02 15:34:12 +0000600 if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc") {
Ted Kremenekdab11102008-04-02 04:43:42 +0000601 shift @$Args;
Ted Kremenek6195c372008-06-02 21:52:47 +0000602 unshift @$Args, $CCAnalyzer;
Ted Kremenekdab11102008-04-02 04:43:42 +0000603 }
Ted Kremenek7442ca62008-04-02 16:04:51 +0000604 elsif ($IgnoreErrors) {
605 if ($Cmd eq "make" or $Cmd eq "gmake") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000606 AddIfNotPresent($Args,"-k");
Ted Kremenek8912b542008-05-13 21:28:02 +0000607 AddIfNotPresent($Args,"-i");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000608 }
609 elsif ($Cmd eq "xcodebuild") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000610 AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000611 }
Ted Kremenek6b628982008-04-30 23:47:12 +0000612 }
613
Ted Kremenek6b628982008-04-30 23:47:12 +0000614 if ($Cmd eq "xcodebuild") {
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000615 # Disable distributed builds for xcodebuild.
Ted Kremenek6b628982008-04-30 23:47:12 +0000616 AddIfNotPresent($Args,"-nodistribute");
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000617
618 # Disable PCH files until clang supports them.
619 AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO");
Ted Kremenek915e9722008-05-27 23:18:07 +0000620
621 # When 'CC' is set, xcodebuild uses it to do all linking, even if we are
622 # linking C++ object files. Set 'LDPLUSPLUS' so that xcodebuild uses 'g++'
623 # when linking such files.
624 my $LDPLUSPLUS = `which g++`;
625 $LDPLUSPLUS =~ s/\015?\012//; # strip newlines
626 $ENV{'LDPLUSPLUS'} = $LDPLUSPLUS;
Ted Kremenek6b628982008-04-30 23:47:12 +0000627 }
Ted Kremenekdab11102008-04-02 04:43:42 +0000628
Ted Kremenek5656a982008-07-15 17:09:28 +0000629 return system(@$Args);
Ted Kremenekdab11102008-04-02 04:43:42 +0000630}
631
Ted Kremenekdab11102008-04-02 04:43:42 +0000632##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000633# DisplayHelp - Utility function to display all help options.
634##----------------------------------------------------------------------------##
635
Sam Bishopa0e22662008-04-02 03:35:43 +0000636sub DisplayHelp {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000637
Ted Kremenek5744dc22008-04-02 18:03:36 +0000638print <<ENDTEXT;
Sam Bishopa0e22662008-04-02 03:35:43 +0000639USAGE: $Prog [options] <build command> [build options]
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000640
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000641ENDTEXT
642
643 if (defined($BuildName)) {
644 print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n";
645 }
646
647print <<ENDTEXT;
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000648OPTIONS:
649
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000650 -o - Target directory for HTML report files. Subdirectories
Sam Bishopa0e22662008-04-02 03:35:43 +0000651 will be created as needed to represent separate "runs" of
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000652 the analyzer. If this option is not specified, a directory
653 is created in /tmp to store the reports.
Ted Kremenek1262fc42008-05-14 20:10:33 +0000654
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000655 -h - Display this message.
656 --help
Ted Kremenek1262fc42008-05-14 20:10:33 +0000657
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000658 -k - Add a "keep on going" option to the specified build command.
659 --keep-going This option currently supports make and xcodebuild.
Ted Kremenekf02e8db2008-04-02 16:41:25 +0000660 This is a convenience option; one can specify this
661 behavior directly using build options.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000662
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000663 --status-bugs - By default, the exit status of $Prog is the same as the
664 executed build command. Specifying this option causes the
665 exit status of $Prog to be 1 if it found potential bugs
666 and 0 otherwise.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000667
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000668 -v - Verbose output from $Prog and the analyzer.
669 A second and third "-v" increases verbosity.
670
671 -V - View analysis results in a web browser when the build
672 --view completes.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000673
Ted Kremenekb7770c02008-07-15 17:06:13 +0000674ENDTEXT
675
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000676 print " Available Source Code Analyses (multiple analyses may be specified):\n\n";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000677
678 foreach my $Analysis (sort keys %AvailableAnalyses) {
679 if (defined($AnalysesDefaultEnabled{$Analysis})) {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000680 print " (+)";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000681 }
682 else {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000683 print " ";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000684 }
685
686 print " $Analysis $AvailableAnalyses{$Analysis}\n";
687 }
688
689print <<ENDTEXT
690
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000691 NOTE: "(+)" indicates that an analysis is enabled by default unless one
692 or more analysis options are specified
Ted Kremenekb7770c02008-07-15 17:06:13 +0000693
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000694BUILD OPTIONS
695
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000696 You can specify any build option acceptable to the build command.
Ted Kremenek39eefde2008-04-02 16:47:27 +0000697
Ted Kremenek5744dc22008-04-02 18:03:36 +0000698EXAMPLE
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000699
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000700 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000701
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000702 The above example causes analysis reports to be deposited into
703 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
704 A different subdirectory is created each time $Prog analyzes a project.
705 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000706
707ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000708}
709
710##----------------------------------------------------------------------------##
711# Process command-line arguments.
712##----------------------------------------------------------------------------##
713
714my $HtmlDir; # Parent directory to store HTML files.
715my $IgnoreErrors = 0; # Ignore build errors.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000716my $ViewResults = 0; # View results when the build terminates.
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000717my $ExitStatusFoundBugs = 0; # Exit status reflects whether bugs were found
Ted Kremenekb7770c02008-07-15 17:06:13 +0000718my @AnalysesToRun;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000719
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000720
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000721if (!@ARGV) {
722 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000723 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000724}
725
726while (@ARGV) {
727
728 # Scan for options we recognize.
729
730 my $arg = $ARGV[0];
731
Sam Bishop2f2418e2008-04-03 14:29:47 +0000732 if ($arg eq "-h" or $arg eq "--help") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000733 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000734 exit 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000735 }
736
Ted Kremenekb7770c02008-07-15 17:06:13 +0000737 if (defined($AvailableAnalyses{$arg})) {
Ted Kremenek1262fc42008-05-14 20:10:33 +0000738 shift @ARGV;
Ted Kremenekb7770c02008-07-15 17:06:13 +0000739 push @AnalysesToRun, $arg;
Ted Kremenek1262fc42008-05-14 20:10:33 +0000740 next;
741 }
742
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000743 if ($arg eq "-o") {
744 shift @ARGV;
745
746 if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000747 DieDiag("'-o' option requires a target directory name.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000748 }
749
750 $HtmlDir = shift @ARGV;
751 next;
752 }
753
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000754 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000755 shift @ARGV;
756 $IgnoreErrors = 1;
757 next;
758 }
759
760 if ($arg eq "-v") {
761 shift @ARGV;
762 $Verbose++;
763 next;
764 }
765
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000766 if ($arg eq "-V" or $arg eq "--view") {
767 shift @ARGV;
768 $ViewResults = 1;
769 next;
770 }
771
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000772 if ($arg eq "--status-bugs") {
773 shift @ARGV;
774 $ExitStatusFoundBugs = 1;
775 next;
776 }
777
Ted Kremenek23cfca32008-06-16 22:40:14 +0000778 DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/);
Ted Kremenek0062ad42008-04-02 16:35:01 +0000779
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000780 last;
781}
782
783if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000784 Diag("No build command specified.\n\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000785 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000786 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000787}
788
789# Determine the output directory for the HTML reports.
790
791if (!defined($HtmlDir)) {
792
Sam Bishopa0e22662008-04-02 03:35:43 +0000793 $HtmlDir = mkdtemp("/tmp/$Prog-XXXXXX");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000794
795 if (!defined($HtmlDir)) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000796 DieDiag("Cannot create HTML directory in /tmp.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000797 }
798
799 if (!$Verbose) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000800 Diag("Using '$HtmlDir' as base HTML report directory.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000801 }
802}
803
Ted Kremenek684bb092008-04-18 15:18:20 +0000804my $BaseDir = $HtmlDir;
Sam Bishopa0e22662008-04-02 03:35:43 +0000805$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000806
807# Set the appropriate environment variables.
808
Sam Bishopa0e22662008-04-02 03:35:43 +0000809SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000810
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000811my $Cmd = "$RealBin/ccc-analyzer";
812
Ted Kremenek23cfca32008-06-16 22:40:14 +0000813DieDiag("Executable 'ccc-analyzer' does not exist at '$Cmd'\n")
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000814 if (! -x $Cmd);
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000815
Ted Kremenekb7770c02008-07-15 17:06:13 +0000816if (! -x $ClangSB) {
817 Diag("'clang' executable not found in '$RealBin'.\n");
818 Diag("Using 'clang' from path.\n");
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000819}
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000820
Ted Kremenek4f4b17d2008-04-03 20:08:18 +0000821$ENV{'CC'} = $Cmd;
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000822$ENV{'CLANG'} = $Clang;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000823
824if ($Verbose >= 2) {
825 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
826}
827
Ted Kremeneka9525c92008-05-12 22:07:14 +0000828if ($Verbose >= 3) {
829 $ENV{'CCC_ANALYZER_LOG'} = 1;
830}
831
Ted Kremenek90125992008-07-15 23:41:32 +0000832if (scalar(@AnalysesToRun) == 0) {
833 foreach my $key (keys %AnalysesDefaultEnabled) {
834 push @AnalysesToRun,$key;
835 }
Ted Kremenek01006782008-07-02 23:16:10 +0000836}
Ted Kremenek1262fc42008-05-14 20:10:33 +0000837
Ted Kremenek90125992008-07-15 23:41:32 +0000838$ENV{'CCC_ANALYZER_ANALYSIS'} = join ' ',@AnalysesToRun;
839
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000840# Run the build.
841
Ted Kremenek5656a982008-07-15 17:09:28 +0000842my $ExitStatus = RunBuildCommand(\@ARGV, $IgnoreErrors, $Cmd);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000843
844# Postprocess the HTML directory.
845
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000846my $NumBugs = Postprocess($HtmlDir, $BaseDir);
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000847
848if ($ViewResults and -r "$HtmlDir/index.html") {
849 # Only works on Mac OS X (for now).
850 print "Viewing analysis results: '$HtmlDir/index.html'\n";
Ted Kremenek20161e92008-07-15 20:18:21 +0000851 system("open", "$HtmlDir/index.html");
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000852}
Ted Kremenek5656a982008-07-15 17:09:28 +0000853
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000854if ($ExitStatusFoundBugs) {
855 exit 1 if ($NumBugs > 0);
856 exit 0;
857}
858
Ted Kremenek5656a982008-07-15 17:09:28 +0000859exit $ExitStatus;
860