blob: 11c3bff88122c2f65923df1abd23a4cc1d57a1df [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 Kremenek63c20172008-08-04 17:34:06 +000072open(PIPE, "-|", $Clang, "--help") or
Ted Kremenekb7770c02008-07-15 17:06:13 +000073 DieDiag("Cannot execute '$Clang'");
Ted Kremenek63c20172008-08-04 17:34:06 +000074
Ted Kremenekb7770c02008-07-15 17:06:13 +000075my $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 Kremenek63c20172008-08-04 17:34:06 +0000213 # come bundled on most systems.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000214 open(FILE, $FName) or DieDiag("Cannot open $FName when computing Digest.\n");
Ted Kremeneka6e24812008-04-19 18:05:48 +0000215 binmode FILE;
216 my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest;
217 close(FILE);
218
Ted Kremenek63c20172008-08-04 17:34:06 +0000219 # Return the digest.
Ted Kremeneka6e24812008-04-19 18:05:48 +0000220 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 {
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000230 my $x = shift;
231 my $y = basename($x);
232 $x =~ s/\Q$y\E$//;
233
234 # Ignore /usr, /Library, /System, /Developer
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000235 return if ( $x =~ /^\/usr/ or $x =~ /^\/Library/
236 or $x =~ /^\/System/ or $x =~ /^\/Developer/);
237
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000238 if (!defined $Prefix) {
239 $Prefix = $x;
240 return;
241 }
242
243 chop $Prefix while (!($x =~ /^$Prefix/));
244}
245
246sub GetPrefix {
247 return $Prefix;
248}
249
250##----------------------------------------------------------------------------##
251# UpdateInFilePath - Update the path in the report file.
252##----------------------------------------------------------------------------##
253
254sub UpdateInFilePath {
255 my $fname = shift;
256 my $regex = shift;
257 my $newtext = shift;
Ted Kremenek63c20172008-08-04 17:34:06 +0000258
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000259 open (RIN, $fname) or die "cannot open $fname";
Ted Kremenek63c20172008-08-04 17:34:06 +0000260 open (ROUT, ">", "$fname.tmp") or die "cannot open $fname.tmp";
261
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000262 while (<RIN>) {
263 s/$regex/$newtext/;
264 print ROUT $_;
265 }
Ted Kremenek63c20172008-08-04 17:34:06 +0000266
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000267 close (ROUT);
268 close (RIN);
Ted Kremenek20161e92008-07-15 20:18:21 +0000269 system("mv", "$fname.tmp", $fname);
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000270}
271
272##----------------------------------------------------------------------------##
Ted Kremenek5744dc22008-04-02 18:03:36 +0000273# ScanFile - Scan a report file for various identifying attributes.
274##----------------------------------------------------------------------------##
275
Ted Kremenek57cf4462008-04-18 15:09:30 +0000276# Sometimes a source file is scanned more than once, and thus produces
277# multiple error reports. We use a cache to solve this problem.
278
279my %AlreadyScanned;
280
Ted Kremenek5744dc22008-04-02 18:03:36 +0000281sub ScanFile {
282
283 my $Index = shift;
284 my $Dir = shift;
285 my $FName = shift;
286
Ted Kremenek57cf4462008-04-18 15:09:30 +0000287 # Compute a digest for the report file. Determine if we have already
288 # scanned a file that looks just like it.
289
290 my $digest = ComputeDigest("$Dir/$FName");
291
292 if (defined($AlreadyScanned{$digest})) {
293 # Redundant file. Remove it.
Ted Kremenek20161e92008-07-15 20:18:21 +0000294 system ("rm", "-f", "$Dir/$FName");
Ted Kremenek57cf4462008-04-18 15:09:30 +0000295 return;
296 }
297
298 $AlreadyScanned{$digest} = 1;
299
Ted Kremenek809709f2008-04-18 16:58:34 +0000300 # At this point the report file is not world readable. Make it happen.
Ted Kremenek20161e92008-07-15 20:18:21 +0000301 system ("chmod", "644", "$Dir/$FName");
Ted Kremenek684bb092008-04-18 15:18:20 +0000302
303 # Scan the report file for tags.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000304 open(IN, "$Dir/$FName") or DieDiag("Cannot open '$Dir/$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000305
306 my $BugDesc = "";
Ted Kremenek22d6a632008-04-02 20:43:36 +0000307 my $BugFile = "";
308 my $BugPathLength = 1;
309 my $BugLine = 0;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000310
311 while (<IN>) {
312
313 if (/<!-- BUGDESC (.*) -->$/) {
314 $BugDesc = $1;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000315 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000316 elsif (/<!-- BUGFILE (.*) -->$/) {
317 $BugFile = $1;
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000318 UpdatePrefix($BugFile);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000319 }
320 elsif (/<!-- BUGPATHLENGTH (.*) -->$/) {
321 $BugPathLength = $1;
322 }
323 elsif (/<!-- BUGLINE (.*) -->$/) {
324 $BugLine = $1;
325 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000326 }
327
328 close(IN);
329
Ted Kremenek22d6a632008-04-02 20:43:36 +0000330 push @$Index,[ $FName, $BugDesc, $BugFile, $BugLine, $BugPathLength ];
331}
332
333##----------------------------------------------------------------------------##
334# CopyJS - Copy JavaScript code to target directory.
335##----------------------------------------------------------------------------##
336
337sub CopyJS {
338
339 my $Dir = shift;
340
Ted Kremenek23cfca32008-06-16 22:40:14 +0000341 DieDiag("Cannot find 'sorttable.js'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000342 if (! -r "$RealBin/sorttable.js");
343
Ted Kremenek20161e92008-07-15 20:18:21 +0000344 system ("cp", "$RealBin/sorttable.js", "$Dir");
Ted Kremenek22d6a632008-04-02 20:43:36 +0000345
Ted Kremenek23cfca32008-06-16 22:40:14 +0000346 DieDiag("Could not copy 'sorttable.js' to '$Dir'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000347 if (! -r "$Dir/sorttable.js");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000348}
349
350##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000351# Postprocess - Postprocess the results of an analysis scan.
352##----------------------------------------------------------------------------##
353
Sam Bishopa0e22662008-04-02 03:35:43 +0000354sub Postprocess {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000355
356 my $Dir = shift;
Ted Kremenek684bb092008-04-18 15:18:20 +0000357 my $BaseDir = shift;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000358
359 die "No directory specified." if (!defined($Dir));
Ted Kremenek684bb092008-04-18 15:18:20 +0000360 die "No base directory specified." if (!defined($BaseDir));
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000361
362 if (! -d $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000363 Diag("No bugs found.\n");
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000364 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000365 }
366
367 opendir(DIR, $Dir);
368 my @files = grep(/^report-.*\.html$/,readdir(DIR));
369 closedir(DIR);
370
371 if (scalar(@files) == 0) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000372 Diag("Removing directory '$Dir' because it contains no reports.\n");
Ted Kremenek20161e92008-07-15 20:18:21 +0000373 system ("rm", "-fR", $Dir);
Ted Kremenek23cfca32008-06-16 22:40:14 +0000374
375 # Remove the base directory if it contains no files (don't use '-R').
Ted Kremenek20161e92008-07-15 20:18:21 +0000376 system ("rm", "-f", $BaseDir);
Ted Kremenek23cfca32008-06-16 22:40:14 +0000377
378 Diag("No bugs found.\n");
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000379 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000380 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000381
382 # Scan each report file and build an index.
383
384 my @Index;
385
386 foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); }
387
Ted Kremenek63c20172008-08-04 17:34:06 +0000388 # Generate an index.html file.
389 my $FName = "$Dir/index.html";
390 open(OUT, ">", $FName) or DieDiag("Cannot create file '$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000391
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000392 # Print out the header.
393
Ted Kremenek5744dc22008-04-02 18:03:36 +0000394print OUT <<ENDTEXT;
395<html>
396<head>
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000397<style type="text/css">
398 body { color:#000000; background-color:#ffffff }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000399 body { font-family: Helvetica, sans-serif; font-size:9pt }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000400 h1 { font-size:12pt }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000401 table.sortable thead {
402 background-color:#eee; color:#666666;
403 font-weight: bold; cursor: default;
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000404 text-align:center;
405 border-top: 2px solid #000000;
406 border-bottom: 2px solid #000000;
407 font-weight: bold; font-family: Verdana
408 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000409 table.sortable { border: 1px #000000 solid }
410 table.sortable { border-collapse: collapse; border-spacing: 0px }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000411 td { border-bottom: 1px #000000 dotted }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000412 td { padding:5px; padding-left:8px; padding-right:8px }
Ted Kremenekd8c6d0c2008-04-07 23:50:07 +0000413 td { text-align:left; font-size:9pt }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000414 td.View { padding-left: 10px }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000415</style>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000416<script src="sorttable.js"></script>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000417<script language='javascript' type="text/javascript">
418function SetDisplay(RowClass, DisplayVal)
419{
420 var Rows = document.getElementsByTagName("tr");
421 for ( var i = 0 ; i < Rows.length; ++i ) {
422 if (Rows[i].className == RowClass) {
423 Rows[i].style.display = DisplayVal;
424 }
425 }
426}
427
428function ToggleDisplay(CheckButton, ClassName) {
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000429 if (CheckButton.checked) {
430 SetDisplay(ClassName, "");
431 }
432 else {
433 SetDisplay(ClassName, "none");
434 }
435}
436</script>
437</head>
438<body>
439ENDTEXT
440
441 # Print out the summary table.
442
443 my %Totals;
444
445 for my $row ( @Index ) {
446
Ted Kremenek432af592008-05-06 18:11:36 +0000447 #my $bug_type = lc($row->[1]);
448 my $bug_type = ($row->[1]);
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000449
450 if (!defined($Totals{$bug_type})) {
451 $Totals{$bug_type} = 1;
452 }
453 else {
454 $Totals{$bug_type}++;
455 }
456 }
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000457
458 print OUT "<h3>Summary</h3>";
459
460 if (defined($BuildName)) {
461 print OUT "\n<p>Results in this analysis run are based on analyzer build <b>$BuildName</b>.</p>\n"
462 }
463
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000464print OUT <<ENDTEXT;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000465<table class="sortable">
466<tr>
467 <td>Bug Type</td>
468 <td>Quantity</td>
Ted Kremenek2645c772008-07-07 16:58:44 +0000469 <td class="sorttable_nosort">Display?</td>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000470</tr>
471ENDTEXT
472
473 for my $key ( sort { $a cmp $b } keys %Totals ) {
Ted Kremenekbdf66c72008-05-06 23:51:45 +0000474 my $x = lc($key);
Ted Kremenek3cea9ee2008-07-30 17:58:08 +0000475 $x =~ s/[ ,'"]+/_/g;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000476 print OUT "<tr><td>$key</td><td>$Totals{$key}</td><td><input type=\"checkbox\" onClick=\"ToggleDisplay(this,'bt_$x');\" checked/></td></tr>\n";
477 }
478
479 # Print out the table of errors.
480
481print OUT <<ENDTEXT;
482</table>
483<h3>Reports</h3>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000484<table class="sortable">
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000485<tr>
Ted Kremenek88a96d62008-07-07 17:23:32 +0000486 <td class="sorttable_sorted">Bug Type<span id="sorttable_sortfwdind">&nbsp;&#x25BE;</span>
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000487 <td>File</td>
488 <td>Line</td>
489 <td>Path Length</td>
Ted Kremenek2645c772008-07-07 16:58:44 +0000490 <td class="sorttable_nosort"></td>
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000491</tr>
Ted Kremenek5744dc22008-04-02 18:03:36 +0000492ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000493
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000494 my $prefix = GetPrefix();
495 my $regex;
496 my $InFileRegex;
497 my $InFilePrefix = "File:</td><td>";
498
499 if (defined($prefix)) {
500 $regex = qr/^\Q$prefix\E/is;
501 $InFileRegex = qr/\Q$InFilePrefix$prefix\E/is;
502 }
503
Ted Kremenek5744dc22008-04-02 18:03:36 +0000504 for my $row ( sort { $a->[1] cmp $b->[1] } @Index ) {
505
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000506 my $x = lc($row->[1]);
Ted Kremenek3cea9ee2008-07-30 17:58:08 +0000507 $x =~ s/[ ,'"]+/_/g;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000508
509 print OUT "<tr class=\"bt_$x\">\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000510
Ted Kremenek5744dc22008-04-02 18:03:36 +0000511 my $ReportFile = $row->[0];
512
Ted Kremenek22d6a632008-04-02 20:43:36 +0000513 print OUT " <td class=\"DESC\">";
Ted Kremenek432af592008-05-06 18:11:36 +0000514 #print OUT lc($row->[1]);
515 print OUT $row->[1];
Ted Kremenek22d6a632008-04-02 20:43:36 +0000516 print OUT "</td>\n";
Ted Kremenek5744dc22008-04-02 18:03:36 +0000517
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000518 # Update the file prefix.
519
520 my $fname = $row->[2];
521 if (defined($regex)) {
522 $fname =~ s/$regex//;
523 UpdateInFilePath("$Dir/$ReportFile", $InFileRegex, $InFilePrefix)
524 }
Ted Kremenek3e56e0b2008-05-02 23:40:49 +0000525
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000526 print OUT "<td>$fname</td>\n";
527
528 # Print the rest of the columns.
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000529 for my $j ( 3 .. $#{$row} ) {
Ted Kremenek5744dc22008-04-02 18:03:36 +0000530 print OUT "<td>$row->[$j]</td>\n"
531 }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000532
533 # Emit the "View" link.
Ted Kremenek22d6a632008-04-02 20:43:36 +0000534 print OUT " <td class=\"View\"><a href=\"$ReportFile#EndPath\">View</a></td>\n";
Ted Kremenek3cea9ee2008-07-30 17:58:08 +0000535
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000536 # End the row.
Ted Kremenek5744dc22008-04-02 18:03:36 +0000537 print OUT "</tr>\n";
538 }
539
540 print OUT "</table>\n</body></html>\n";
541 close(OUT);
Ted Kremenek20161e92008-07-15 20:18:21 +0000542
Ted Kremenek22d6a632008-04-02 20:43:36 +0000543 CopyJS($Dir);
Ted Kremenek20161e92008-07-15 20:18:21 +0000544
545 # Make sure $Dir and $BaseDir are world readable/executable.
546 system("chmod", "755", $Dir);
547 system("chmod", "755", $BaseDir);
548
Ted Kremenek23cfca32008-06-16 22:40:14 +0000549 my $Num = scalar(@Index);
Ted Kremenek150c2122008-07-11 19:15:05 +0000550 Diag("$Num bugs found.\n");
551 if ($Num > 0 && -r "$Dir/index.html") {
552 Diag("Open '$Dir/index.html' to examine bug reports.\n");
553 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000554
555 return $Num;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000556}
557
558##----------------------------------------------------------------------------##
Ted Kremenekdab11102008-04-02 04:43:42 +0000559# RunBuildCommand - Run the build command.
560##----------------------------------------------------------------------------##
561
Ted Kremenek6b628982008-04-30 23:47:12 +0000562sub AddIfNotPresent {
563 my $Args = shift;
564 my $Arg = shift;
565 my $found = 0;
566
567 foreach my $k (@$Args) {
568 if ($k eq $Arg) {
569 $found = 1;
570 last;
571 }
572 }
573
574 if ($found == 0) {
575 push @$Args, $Arg;
576 }
577}
578
Ted Kremenekdab11102008-04-02 04:43:42 +0000579sub RunBuildCommand {
580
581 my $Args = shift;
Ted Kremenek7442ca62008-04-02 16:04:51 +0000582 my $IgnoreErrors = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000583 my $Cmd = $Args->[0];
Ted Kremenek6195c372008-06-02 21:52:47 +0000584 my $CCAnalyzer = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000585
Ted Kremenek3301cb12008-06-30 18:18:16 +0000586 # Get only the part of the command after the last '/'.
587 if ($Cmd =~ /\/([^\/]+)$/) {
588 $Cmd = $1;
589 }
590
Ted Kremenek63c20172008-08-04 17:34:06 +0000591 if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc"
592 or $Cmd eq "ccc-analyzer") {
Ted Kremenekdab11102008-04-02 04:43:42 +0000593 shift @$Args;
Ted Kremenek6195c372008-06-02 21:52:47 +0000594 unshift @$Args, $CCAnalyzer;
Ted Kremenekdab11102008-04-02 04:43:42 +0000595 }
Ted Kremenek7442ca62008-04-02 16:04:51 +0000596 elsif ($IgnoreErrors) {
597 if ($Cmd eq "make" or $Cmd eq "gmake") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000598 AddIfNotPresent($Args,"-k");
Ted Kremenek8912b542008-05-13 21:28:02 +0000599 AddIfNotPresent($Args,"-i");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000600 }
601 elsif ($Cmd eq "xcodebuild") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000602 AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000603 }
Ted Kremenek6b628982008-04-30 23:47:12 +0000604 }
605
Ted Kremenek6b628982008-04-30 23:47:12 +0000606 if ($Cmd eq "xcodebuild") {
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000607 # Disable distributed builds for xcodebuild.
Ted Kremenek6b628982008-04-30 23:47:12 +0000608 AddIfNotPresent($Args,"-nodistribute");
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000609
610 # Disable PCH files until clang supports them.
611 AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO");
Ted Kremenek915e9722008-05-27 23:18:07 +0000612
613 # When 'CC' is set, xcodebuild uses it to do all linking, even if we are
614 # linking C++ object files. Set 'LDPLUSPLUS' so that xcodebuild uses 'g++'
615 # when linking such files.
616 my $LDPLUSPLUS = `which g++`;
617 $LDPLUSPLUS =~ s/\015?\012//; # strip newlines
618 $ENV{'LDPLUSPLUS'} = $LDPLUSPLUS;
Ted Kremenek6b628982008-04-30 23:47:12 +0000619 }
Ted Kremenekdab11102008-04-02 04:43:42 +0000620
Ted Kremenek5656a982008-07-15 17:09:28 +0000621 return system(@$Args);
Ted Kremenekdab11102008-04-02 04:43:42 +0000622}
623
Ted Kremenekdab11102008-04-02 04:43:42 +0000624##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000625# DisplayHelp - Utility function to display all help options.
626##----------------------------------------------------------------------------##
627
Sam Bishopa0e22662008-04-02 03:35:43 +0000628sub DisplayHelp {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000629
Ted Kremenek5744dc22008-04-02 18:03:36 +0000630print <<ENDTEXT;
Sam Bishopa0e22662008-04-02 03:35:43 +0000631USAGE: $Prog [options] <build command> [build options]
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000632
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000633ENDTEXT
634
635 if (defined($BuildName)) {
636 print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n";
637 }
638
639print <<ENDTEXT;
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000640OPTIONS:
641
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000642 -o - Target directory for HTML report files. Subdirectories
Sam Bishopa0e22662008-04-02 03:35:43 +0000643 will be created as needed to represent separate "runs" of
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000644 the analyzer. If this option is not specified, a directory
645 is created in /tmp to store the reports.
Ted Kremenek1262fc42008-05-14 20:10:33 +0000646
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000647 -h - Display this message.
648 --help
Ted Kremenek1262fc42008-05-14 20:10:33 +0000649
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000650 -k - Add a "keep on going" option to the specified build command.
651 --keep-going This option currently supports make and xcodebuild.
Ted Kremenekf02e8db2008-04-02 16:41:25 +0000652 This is a convenience option; one can specify this
653 behavior directly using build options.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000654
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000655 --status-bugs - By default, the exit status of $Prog is the same as the
656 executed build command. Specifying this option causes the
657 exit status of $Prog to be 1 if it found potential bugs
658 and 0 otherwise.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000659
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000660 -v - Verbose output from $Prog and the analyzer.
661 A second and third "-v" increases verbosity.
662
663 -V - View analysis results in a web browser when the build
664 --view completes.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000665
Ted Kremenekb7770c02008-07-15 17:06:13 +0000666ENDTEXT
667
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000668 print " Available Source Code Analyses (multiple analyses may be specified):\n\n";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000669
670 foreach my $Analysis (sort keys %AvailableAnalyses) {
671 if (defined($AnalysesDefaultEnabled{$Analysis})) {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000672 print " (+)";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000673 }
674 else {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000675 print " ";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000676 }
677
678 print " $Analysis $AvailableAnalyses{$Analysis}\n";
679 }
680
681print <<ENDTEXT
682
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000683 NOTE: "(+)" indicates that an analysis is enabled by default unless one
684 or more analysis options are specified
Ted Kremenekb7770c02008-07-15 17:06:13 +0000685
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000686BUILD OPTIONS
687
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000688 You can specify any build option acceptable to the build command.
Ted Kremenek39eefde2008-04-02 16:47:27 +0000689
Ted Kremenek5744dc22008-04-02 18:03:36 +0000690EXAMPLE
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000691
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000692 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000693
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000694 The above example causes analysis reports to be deposited into
695 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
696 A different subdirectory is created each time $Prog analyzes a project.
697 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000698
699ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000700}
701
702##----------------------------------------------------------------------------##
703# Process command-line arguments.
704##----------------------------------------------------------------------------##
705
706my $HtmlDir; # Parent directory to store HTML files.
707my $IgnoreErrors = 0; # Ignore build errors.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000708my $ViewResults = 0; # View results when the build terminates.
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000709my $ExitStatusFoundBugs = 0; # Exit status reflects whether bugs were found
Ted Kremenekb7770c02008-07-15 17:06:13 +0000710my @AnalysesToRun;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000711
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000712
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000713if (!@ARGV) {
714 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000715 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000716}
717
718while (@ARGV) {
719
720 # Scan for options we recognize.
721
722 my $arg = $ARGV[0];
723
Sam Bishop2f2418e2008-04-03 14:29:47 +0000724 if ($arg eq "-h" or $arg eq "--help") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000725 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000726 exit 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000727 }
728
Ted Kremenekb7770c02008-07-15 17:06:13 +0000729 if (defined($AvailableAnalyses{$arg})) {
Ted Kremenek1262fc42008-05-14 20:10:33 +0000730 shift @ARGV;
Ted Kremenekb7770c02008-07-15 17:06:13 +0000731 push @AnalysesToRun, $arg;
Ted Kremenek1262fc42008-05-14 20:10:33 +0000732 next;
733 }
734
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000735 if ($arg eq "-o") {
736 shift @ARGV;
737
738 if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000739 DieDiag("'-o' option requires a target directory name.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000740 }
741
742 $HtmlDir = shift @ARGV;
743 next;
744 }
745
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000746 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000747 shift @ARGV;
748 $IgnoreErrors = 1;
749 next;
750 }
751
752 if ($arg eq "-v") {
753 shift @ARGV;
754 $Verbose++;
755 next;
756 }
757
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000758 if ($arg eq "-V" or $arg eq "--view") {
759 shift @ARGV;
760 $ViewResults = 1;
761 next;
762 }
763
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000764 if ($arg eq "--status-bugs") {
765 shift @ARGV;
766 $ExitStatusFoundBugs = 1;
767 next;
768 }
769
Ted Kremenek23cfca32008-06-16 22:40:14 +0000770 DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/);
Ted Kremenek0062ad42008-04-02 16:35:01 +0000771
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000772 last;
773}
774
775if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000776 Diag("No build command specified.\n\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000777 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000778 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000779}
780
781# Determine the output directory for the HTML reports.
782
783if (!defined($HtmlDir)) {
784
Sam Bishopa0e22662008-04-02 03:35:43 +0000785 $HtmlDir = mkdtemp("/tmp/$Prog-XXXXXX");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000786
787 if (!defined($HtmlDir)) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000788 DieDiag("Cannot create HTML directory in /tmp.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000789 }
790
791 if (!$Verbose) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000792 Diag("Using '$HtmlDir' as base HTML report directory.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000793 }
794}
795
Ted Kremenek684bb092008-04-18 15:18:20 +0000796my $BaseDir = $HtmlDir;
Sam Bishopa0e22662008-04-02 03:35:43 +0000797$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000798
799# Set the appropriate environment variables.
800
Sam Bishopa0e22662008-04-02 03:35:43 +0000801SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000802
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000803my $Cmd = "$RealBin/ccc-analyzer";
804
Ted Kremenek23cfca32008-06-16 22:40:14 +0000805DieDiag("Executable 'ccc-analyzer' does not exist at '$Cmd'\n")
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000806 if (! -x $Cmd);
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000807
Ted Kremenekb7770c02008-07-15 17:06:13 +0000808if (! -x $ClangSB) {
809 Diag("'clang' executable not found in '$RealBin'.\n");
810 Diag("Using 'clang' from path.\n");
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000811}
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000812
Ted Kremenek4f4b17d2008-04-03 20:08:18 +0000813$ENV{'CC'} = $Cmd;
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000814$ENV{'CLANG'} = $Clang;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000815
816if ($Verbose >= 2) {
817 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
818}
819
Ted Kremeneka9525c92008-05-12 22:07:14 +0000820if ($Verbose >= 3) {
821 $ENV{'CCC_ANALYZER_LOG'} = 1;
822}
823
Ted Kremenek90125992008-07-15 23:41:32 +0000824if (scalar(@AnalysesToRun) == 0) {
825 foreach my $key (keys %AnalysesDefaultEnabled) {
826 push @AnalysesToRun,$key;
827 }
Ted Kremenek01006782008-07-02 23:16:10 +0000828}
Ted Kremenek1262fc42008-05-14 20:10:33 +0000829
Ted Kremenek90125992008-07-15 23:41:32 +0000830$ENV{'CCC_ANALYZER_ANALYSIS'} = join ' ',@AnalysesToRun;
831
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000832# Run the build.
833
Ted Kremenek5656a982008-07-15 17:09:28 +0000834my $ExitStatus = RunBuildCommand(\@ARGV, $IgnoreErrors, $Cmd);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000835
836# Postprocess the HTML directory.
837
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000838my $NumBugs = Postprocess($HtmlDir, $BaseDir);
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000839
840if ($ViewResults and -r "$HtmlDir/index.html") {
841 # Only works on Mac OS X (for now).
842 print "Viewing analysis results: '$HtmlDir/index.html'\n";
Ted Kremenek20161e92008-07-15 20:18:21 +0000843 system("open", "$HtmlDir/index.html");
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000844}
Ted Kremenek5656a982008-07-15 17:09:28 +0000845
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000846if ($ExitStatusFoundBugs) {
847 exit 1 if ($NumBugs > 0);
848 exit 0;
849}
850
Ted Kremenek5656a982008-07-15 17:09:28 +0000851exit $ExitStatus;
852