blob: b9d3910d73b4e0237d472d59d7fd4efc00c40f55 [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;
Ted Kremenek22d6a632008-04-02 20:43:36 +000017use FindBin qw($RealBin);
Ted Kremeneka6e24812008-04-19 18:05:48 +000018use Digest::MD5;
Ted Kremenek7a4648d2008-05-02 22:04:53 +000019use File::Basename;
Ted Kremenek23cfca32008-06-16 22:40:14 +000020use Term::ANSIColor;
21use Term::ANSIColor qw(:constants);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000022
23my $Verbose = 0; # Verbose output from this script.
24my $Prog = "scan-build";
Ted Kremenekf4cdf412008-05-23 18:17:05 +000025my $BuildName;
26my $BuildDate;
Ted Kremenek95aa1052008-09-04 17:52:41 +000027my $CXX; # Leave undefined initially.
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000028
Ted Kremenek0e689382008-09-11 18:17:51 +000029my $TERM = $ENV{'TERM'};
30my $UseColor = (defined $TERM and $TERM eq 'xterm-color' and -t STDOUT
31 and defined $ENV{'SCAN_BUILD_COLOR'});
Ted Kremenek23cfca32008-06-16 22:40:14 +000032
Ted Kremenekb7770c02008-07-15 17:06:13 +000033##----------------------------------------------------------------------------##
34# Diagnostics
35##----------------------------------------------------------------------------##
36
Ted Kremenek23cfca32008-06-16 22:40:14 +000037sub Diag {
38 if ($UseColor) {
39 print BOLD, MAGENTA "$Prog: @_";
40 print RESET;
41 }
42 else {
43 print "$Prog: @_";
44 }
45}
46
Ted Kremenek991c54b2008-08-08 20:46:42 +000047sub DiagCrashes {
48 my $Dir = shift;
49 Diag ("The analyzer crashed on some source files.\n");
Ted Kremenek386c6932008-09-03 17:59:35 +000050 Diag ("Preprocessed versions of crashed files were deposited in '$Dir/crashes'.\n");
Ted Kremenek991c54b2008-08-08 20:46:42 +000051 Diag ("Please consider submitting a bug report using these files:\n");
52 Diag (" http://clang.llvm.org/StaticAnalysisUsage.html#filingbugs\n")
53}
54
Ted Kremenek23cfca32008-06-16 22:40:14 +000055sub DieDiag {
56 if ($UseColor) {
57 print BOLD, RED "$Prog: ";
58 print RESET, RED @_;
59 print RESET;
60 }
61 else {
62 print "$Prog: ", @_;
63 }
64 exit(0);
65}
66
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000067##----------------------------------------------------------------------------##
Ted Kremenekb7770c02008-07-15 17:06:13 +000068# Some initial preprocessing of Clang options.
69##----------------------------------------------------------------------------##
70
71my $ClangSB = "$RealBin/clang";
72my $Clang = $ClangSB;
73
74if (! -x $ClangSB) {
75 $Clang = "clang";
76}
77
78my %AvailableAnalyses;
79
80# Query clang for analysis options.
Ted Kremenek63c20172008-08-04 17:34:06 +000081open(PIPE, "-|", $Clang, "--help") or
Ted Kremenekb7770c02008-07-15 17:06:13 +000082 DieDiag("Cannot execute '$Clang'");
Ted Kremenek63c20172008-08-04 17:34:06 +000083
Ted Kremenekb7770c02008-07-15 17:06:13 +000084my $FoundAnalysis = 0;
85
86while(<PIPE>) {
87 if ($FoundAnalysis == 0) {
88 if (/Available Source Code Analyses/) {
89 $FoundAnalysis = 1;
90 }
Ted Kremenek991c54b2008-08-08 20:46:42 +000091
Ted Kremenekb7770c02008-07-15 17:06:13 +000092 next;
93 }
94
95 if (/^\s\s\s\s([^\s]+)\s(.+)$/) {
96 next if ($1 =~ /-dump/ or $1 =~ /-view/
97 or $1 =~ /-checker-simple/ or $1 =~ /-warn-uninit/);
98
99 $AvailableAnalyses{$1} = $2;
100 next;
101 }
102
103 last;
104}
105
106close (PIPE);
107
108my %AnalysesDefaultEnabled = (
109 '-warn-dead-stores' => 1,
110 '-checker-cfref' => 1,
Ted Kremenek90125992008-07-15 23:41:32 +0000111 '-warn-objc-methodsigs' => 1,
Ted Kremenekbde3a052008-07-25 20:35:01 +0000112 '-warn-objc-missing-dealloc' => 1,
Ted Kremenek5d443492008-09-18 06:34:16 +0000113 '-warn-objc-unused-ivars' => 1,
Ted Kremenekb7770c02008-07-15 17:06:13 +0000114);
115
116##----------------------------------------------------------------------------##
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000117# GetHTMLRunDir - Construct an HTML directory name for the current sub-run.
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000118##----------------------------------------------------------------------------##
119
Sam Bishopa0e22662008-04-02 03:35:43 +0000120sub GetHTMLRunDir {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000121
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000122 die "Not enough arguments." if (@_ == 0);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000123 my $Dir = shift @_;
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000124
125 my $TmpMode = 0;
126 if (!defined $Dir) {
127 $Dir = "/tmp";
128 $TmpMode = 1;
129 }
130
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000131 # Get current date and time.
132
133 my @CurrentTime = localtime();
134
135 my $year = $CurrentTime[5] + 1900;
136 my $day = $CurrentTime[3];
137 my $month = $CurrentTime[4] + 1;
138
Ted Kremenek9d7405f2008-05-14 17:23:56 +0000139 my $DateString = sprintf("%d-%02d-%02d", $year, $month, $day);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000140
141 # Determine the run number.
142
143 my $RunNumber;
144
145 if (-d $Dir) {
146
147 if (! -r $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000148 DieDiag("directory '$Dir' exists but is not readable.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000149 }
150
151 # Iterate over all files in the specified directory.
152
153 my $max = 0;
154
155 opendir(DIR, $Dir);
Ted Kremenek29da6c52008-08-07 17:57:34 +0000156 my @FILES = grep { -d "$Dir/$_" } readdir(DIR);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000157 closedir(DIR);
158
159 foreach my $f (@FILES) {
160
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000161 # Strip the prefix '$Prog-' if we are dumping files to /tmp.
162 if ($TmpMode) {
163 next if (!($f =~ /^$Prog-(.+)/));
164 $f = $1;
165 }
166
Ted Kremenekebb74132008-09-21 06:58:09 +0000167
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000168 my @x = split/-/, $f;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000169 next if (scalar(@x) != 4);
170 next if ($x[0] != $year);
171 next if ($x[1] != $month);
172 next if ($x[2] != $day);
173
174 if ($x[3] > $max) {
175 $max = $x[3];
176 }
177 }
178
179 $RunNumber = $max + 1;
180 }
181 else {
182
183 if (-x $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000184 DieDiag("'$Dir' exists but is not a directory.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000185 }
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000186
187 if ($TmpMode) {
188 DieDiag("The directory '/tmp' does not exist or cannot be accessed.");
189 }
190
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000191 # $Dir does not exist. It will be automatically created by the
192 # clang driver. Set the run number to 1.
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000193
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000194 $RunNumber = 1;
195 }
196
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000197 die "RunNumber must be defined!" if (!defined $RunNumber);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000198
199 # Append the run number.
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000200 my $NewDir;
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000201 if ($TmpMode) {
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000202 $NewDir = "$Dir/$Prog-$DateString-$RunNumber";
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000203 }
204 else {
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000205 $NewDir = "$Dir/$DateString-$RunNumber";
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000206 }
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000207 system 'mkdir','-p',$NewDir;
208 return $NewDir;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000209}
210
Sam Bishopa0e22662008-04-02 03:35:43 +0000211sub SetHtmlEnv {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000212
213 die "Wrong number of arguments." if (scalar(@_) != 2);
214
215 my $Args = shift;
216 my $Dir = shift;
217
218 die "No build command." if (scalar(@$Args) == 0);
219
220 my $Cmd = $$Args[0];
221
222 if ($Cmd =~ /configure/) {
223 return;
224 }
225
226 if ($Verbose) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000227 Diag("Emitting reports for this run to '$Dir'.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000228 }
229
230 $ENV{'CCC_ANALYZER_HTML'} = $Dir;
231}
232
233##----------------------------------------------------------------------------##
Ted Kremenek57cf4462008-04-18 15:09:30 +0000234# ComputeDigest - Compute a digest of the specified file.
235##----------------------------------------------------------------------------##
236
237sub ComputeDigest {
238 my $FName = shift;
Ted Kremenek23cfca32008-06-16 22:40:14 +0000239 DieDiag("Cannot read $FName to compute Digest.\n") if (! -r $FName);
Ted Kremeneka6e24812008-04-19 18:05:48 +0000240
241 # Use Digest::MD5. We don't have to be cryptographically secure. We're
Ted Kremenek7ea02e62008-04-19 18:07:44 +0000242 # just looking for duplicate files that come from a non-malicious source.
243 # We use Digest::MD5 because it is a standard Perl module that should
Ted Kremenek63c20172008-08-04 17:34:06 +0000244 # come bundled on most systems.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000245 open(FILE, $FName) or DieDiag("Cannot open $FName when computing Digest.\n");
Ted Kremeneka6e24812008-04-19 18:05:48 +0000246 binmode FILE;
247 my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest;
248 close(FILE);
249
Ted Kremenek63c20172008-08-04 17:34:06 +0000250 # Return the digest.
Ted Kremeneka6e24812008-04-19 18:05:48 +0000251 return $Result;
Ted Kremenek57cf4462008-04-18 15:09:30 +0000252}
253
254##----------------------------------------------------------------------------##
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000255# UpdatePrefix - Compute the common prefix of files.
256##----------------------------------------------------------------------------##
257
258my $Prefix;
259
260sub UpdatePrefix {
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000261 my $x = shift;
262 my $y = basename($x);
263 $x =~ s/\Q$y\E$//;
264
265 # Ignore /usr, /Library, /System, /Developer
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000266 return if ( $x =~ /^\/usr/ or $x =~ /^\/Library/
267 or $x =~ /^\/System/ or $x =~ /^\/Developer/);
268
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000269 if (!defined $Prefix) {
270 $Prefix = $x;
271 return;
272 }
273
Ted Kremenek20b2bae2008-09-11 21:15:10 +0000274 chop $Prefix while (!($x =~ /^\Q$Prefix/));
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000275}
276
277sub GetPrefix {
278 return $Prefix;
279}
280
281##----------------------------------------------------------------------------##
282# UpdateInFilePath - Update the path in the report file.
283##----------------------------------------------------------------------------##
284
285sub UpdateInFilePath {
286 my $fname = shift;
287 my $regex = shift;
288 my $newtext = shift;
Ted Kremenek63c20172008-08-04 17:34:06 +0000289
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000290 open (RIN, $fname) or die "cannot open $fname";
Ted Kremenek63c20172008-08-04 17:34:06 +0000291 open (ROUT, ">", "$fname.tmp") or die "cannot open $fname.tmp";
292
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000293 while (<RIN>) {
294 s/$regex/$newtext/;
295 print ROUT $_;
296 }
Ted Kremenek63c20172008-08-04 17:34:06 +0000297
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000298 close (ROUT);
299 close (RIN);
Ted Kremenek20161e92008-07-15 20:18:21 +0000300 system("mv", "$fname.tmp", $fname);
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000301}
302
303##----------------------------------------------------------------------------##
Ted Kremenek5744dc22008-04-02 18:03:36 +0000304# ScanFile - Scan a report file for various identifying attributes.
305##----------------------------------------------------------------------------##
306
Ted Kremenek57cf4462008-04-18 15:09:30 +0000307# Sometimes a source file is scanned more than once, and thus produces
308# multiple error reports. We use a cache to solve this problem.
309
310my %AlreadyScanned;
311
Ted Kremenek5744dc22008-04-02 18:03:36 +0000312sub ScanFile {
313
314 my $Index = shift;
315 my $Dir = shift;
316 my $FName = shift;
317
Ted Kremenek57cf4462008-04-18 15:09:30 +0000318 # Compute a digest for the report file. Determine if we have already
319 # scanned a file that looks just like it.
320
321 my $digest = ComputeDigest("$Dir/$FName");
322
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000323 if (defined $AlreadyScanned{$digest}) {
Ted Kremenek57cf4462008-04-18 15:09:30 +0000324 # Redundant file. Remove it.
Ted Kremenek20161e92008-07-15 20:18:21 +0000325 system ("rm", "-f", "$Dir/$FName");
Ted Kremenek57cf4462008-04-18 15:09:30 +0000326 return;
327 }
328
329 $AlreadyScanned{$digest} = 1;
330
Ted Kremenek809709f2008-04-18 16:58:34 +0000331 # At this point the report file is not world readable. Make it happen.
Ted Kremenek20161e92008-07-15 20:18:21 +0000332 system ("chmod", "644", "$Dir/$FName");
Ted Kremenek684bb092008-04-18 15:18:20 +0000333
334 # Scan the report file for tags.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000335 open(IN, "$Dir/$FName") or DieDiag("Cannot open '$Dir/$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000336
337 my $BugDesc = "";
Ted Kremenek22d6a632008-04-02 20:43:36 +0000338 my $BugFile = "";
Ted Kremenekebb74132008-09-21 06:58:09 +0000339 my $BugCategory;
Ted Kremenek22d6a632008-04-02 20:43:36 +0000340 my $BugPathLength = 1;
341 my $BugLine = 0;
Ted Kremenekebb74132008-09-21 06:58:09 +0000342 my $found = 0;
343
Ted Kremenek5744dc22008-04-02 18:03:36 +0000344 while (<IN>) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000345
346 last if ($found == 5);
347
Ted Kremenek5744dc22008-04-02 18:03:36 +0000348 if (/<!-- BUGDESC (.*) -->$/) {
349 $BugDesc = $1;
Ted Kremenekebb74132008-09-21 06:58:09 +0000350 ++$found;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000351 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000352 elsif (/<!-- BUGFILE (.*) -->$/) {
353 $BugFile = $1;
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000354 UpdatePrefix($BugFile);
Ted Kremenekebb74132008-09-21 06:58:09 +0000355 ++$found;
Ted Kremenek22d6a632008-04-02 20:43:36 +0000356 }
357 elsif (/<!-- BUGPATHLENGTH (.*) -->$/) {
358 $BugPathLength = $1;
Ted Kremenekebb74132008-09-21 06:58:09 +0000359 ++$found;
Ted Kremenek22d6a632008-04-02 20:43:36 +0000360 }
361 elsif (/<!-- BUGLINE (.*) -->$/) {
362 $BugLine = $1;
Ted Kremenekebb74132008-09-21 06:58:09 +0000363 ++$found;
364 }
365 elsif (/<!-- BUGCATEGORY (.*) -->$/) {
366 $BugCategory = $1;
367 ++$found;
Ted Kremenek22d6a632008-04-02 20:43:36 +0000368 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000369 }
370
371 close(IN);
Ted Kremenekebb74132008-09-21 06:58:09 +0000372
373 if (!defined $BugCategory) {
374 $BugCategory = "Other";
375 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000376
Ted Kremenekebb74132008-09-21 06:58:09 +0000377 push @$Index,[ $FName, $BugCategory, $BugDesc, $BugFile, $BugLine,
378 $BugPathLength ];
Ted Kremenek22d6a632008-04-02 20:43:36 +0000379}
380
381##----------------------------------------------------------------------------##
382# CopyJS - Copy JavaScript code to target directory.
383##----------------------------------------------------------------------------##
384
385sub CopyJS {
386
387 my $Dir = shift;
388
Ted Kremenek23cfca32008-06-16 22:40:14 +0000389 DieDiag("Cannot find 'sorttable.js'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000390 if (! -r "$RealBin/sorttable.js");
391
Ted Kremenek20161e92008-07-15 20:18:21 +0000392 system ("cp", "$RealBin/sorttable.js", "$Dir");
Ted Kremenek22d6a632008-04-02 20:43:36 +0000393
Ted Kremenek23cfca32008-06-16 22:40:14 +0000394 DieDiag("Could not copy 'sorttable.js' to '$Dir'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000395 if (! -r "$Dir/sorttable.js");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000396}
397
398##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000399# Postprocess - Postprocess the results of an analysis scan.
400##----------------------------------------------------------------------------##
401
Sam Bishopa0e22662008-04-02 03:35:43 +0000402sub Postprocess {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000403
404 my $Dir = shift;
Ted Kremenek684bb092008-04-18 15:18:20 +0000405 my $BaseDir = shift;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000406
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000407 die "No directory specified." if (!defined $Dir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000408
409 if (! -d $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000410 Diag("No bugs found.\n");
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000411 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000412 }
413
414 opendir(DIR, $Dir);
Ted Kremenek991c54b2008-08-08 20:46:42 +0000415 my $Crashes = 0;
416 my @files = grep { if ($_ eq "crashes") { $Crashes++; }
417 /^report-.*\.html$/; } readdir(DIR);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000418 closedir(DIR);
419
Ted Kremenek991c54b2008-08-08 20:46:42 +0000420 if (scalar(@files) == 0 and $Crashes == 0) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000421 Diag("Removing directory '$Dir' because it contains no reports.\n");
Ted Kremenek20161e92008-07-15 20:18:21 +0000422 system ("rm", "-fR", $Dir);
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000423 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000424 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000425
Ted Kremenek991c54b2008-08-08 20:46:42 +0000426 # Scan each report file and build an index.
427 my @Index;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000428 foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); }
429
Ted Kremenekd52e4252008-08-25 20:45:07 +0000430 # Scan the crashes directory and use the information in the .info files
431 # to update the common prefix directory.
432 if (-d "$Dir/crashes") {
433 opendir(DIR, "$Dir/crashes");
434 my @files = grep { /[.]info$/; } readdir(DIR);
435 closedir(DIR);
436 foreach my $file (@files) {
437 open IN, "$Dir/crashes/$file" or DieDiag("cannot open $file\n");
438 my $Path = <IN>;
439 if (defined $Path) { UpdatePrefix($Path); }
440 close IN;
441 }
442 }
443
Ted Kremenek63c20172008-08-04 17:34:06 +0000444 # Generate an index.html file.
445 my $FName = "$Dir/index.html";
446 open(OUT, ">", $FName) or DieDiag("Cannot create file '$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000447
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000448 # Print out the header.
449
Ted Kremenek5744dc22008-04-02 18:03:36 +0000450print OUT <<ENDTEXT;
451<html>
452<head>
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000453<style type="text/css">
454 body { color:#000000; background-color:#ffffff }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000455 body { font-family: Helvetica, sans-serif; font-size:9pt }
Ted Kremenekebb74132008-09-21 06:58:09 +0000456 h3 { font-size:12pt }
457 table { font-size:9pt }
458 table { border-spacing: 0px; border: 1px solid black }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000459 table thead {
Ted Kremenek22d6a632008-04-02 20:43:36 +0000460 background-color:#eee; color:#666666;
461 font-weight: bold; cursor: default;
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000462 text-align:center;
Ted Kremenekebb74132008-09-21 06:58:09 +0000463 font-weight: bold; font-family: Verdana;
464 white-space:nowrap;
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000465 }
Ted Kremenekebb74132008-09-21 06:58:09 +0000466 .W { font-size:0px }
467 th, td { padding:5px; padding-left:8px; text-align:left }
468 td.SUMM_DESC { padding-left:12px }
469 td.DESC { white-space:pre }
470 td.Q { text-align:right }
471 td { text-align:left }
472 td.View a { white-space: nowrap; -webkit-appearance:square-button; padding-left:1em; padding-right:1em; padding-top:0.5ex; padding-bottom:0.5ex; text-decoration:none; color:black }
473 tbody.scrollContent { overflow:auto }
474}
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000475</style>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000476<script src="sorttable.js"></script>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000477<script language='javascript' type="text/javascript">
478function SetDisplay(RowClass, DisplayVal)
479{
480 var Rows = document.getElementsByTagName("tr");
481 for ( var i = 0 ; i < Rows.length; ++i ) {
482 if (Rows[i].className == RowClass) {
483 Rows[i].style.display = DisplayVal;
484 }
485 }
486}
Ted Kremenekebb74132008-09-21 06:58:09 +0000487
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000488function ToggleDisplay(CheckButton, ClassName) {
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000489 if (CheckButton.checked) {
490 SetDisplay(ClassName, "");
491 }
492 else {
493 SetDisplay(ClassName, "none");
494 }
495}
496</script>
497</head>
498<body>
499ENDTEXT
500
Ted Kremenek991c54b2008-08-08 20:46:42 +0000501 if (scalar(@files)) {
502 # Print out the summary table.
503 my %Totals;
Ted Kremenekebb74132008-09-21 06:58:09 +0000504
Ted Kremenek991c54b2008-08-08 20:46:42 +0000505 for my $row ( @Index ) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000506 my $bug_type = ($row->[2]);
507 my $bug_category = ($row->[1]);
508 my $key = "$bug_category:$bug_type";
509
510 if (!defined $Totals{$key}) { $Totals{$key} = [1,$bug_category,$bug_type]; }
511 else { $Totals{$key}->[0]++; }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000512 }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000513
514 print OUT "<h3>Bug Summary</h3>";
515
516 if (defined $BuildName) {
517 print OUT "\n<p>Results in this analysis run are based on analyzer build <b>$BuildName</b>.</p>\n"
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000518 }
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000519
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000520print OUT <<ENDTEXT;
Ted Kremenekebb74132008-09-21 06:58:09 +0000521<table>
522<thead><tr><td>Bug Type</td><td>Quantity</td><td class="sorttable_nosort">Display?</td></tr></thead>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000523ENDTEXT
524
Ted Kremenekebb74132008-09-21 06:58:09 +0000525 my $last_category;
526
527 for my $key (
528 sort {
529 my $x = $Totals{$a};
530 my $y = $Totals{$b};
531 my $res = $x->[1] cmp $y->[1];
532 $res = $x->[2] cmp $y->[2] if ($res == 0);
533 $res
534 } keys %Totals )
535 {
536 my $val = $Totals{$key};
537 my $category = $val->[1];
538 if (!defined $last_category or $last_category ne $category) {
539 $last_category = $category;
540 print OUT "<tr><th>$category</th><th colspan=2></th></tr>\n";
541 }
542 my $x = lc $key;
543 $x =~ s/[ ,'":\/()]+/_/g;
544 print OUT "<tr><td class=\"SUMM_DESC\">";
545 print OUT $val->[2];
546 print OUT "</td><td>";
547 print OUT $val->[0];
548 print OUT "</td><td><center><input type=\"checkbox\" onClick=\"ToggleDisplay(this,'bt_$x');\" checked/></center></td></tr>\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000549 }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000550
551 # Print out the table of errors.
552
553print OUT <<ENDTEXT;
554</table>
555<h3>Reports</h3>
Ted Kremenekebb74132008-09-21 06:58:09 +0000556
557<table class="sortable" style="table-layout:automatic">
558<thead><tr>
559 <td>Bug Group</td>
560 <td class="sorttable_sorted">Bug Type<span id="sorttable_sortfwdind">&nbsp;&#x25BE;</span></td>
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000561 <td>File</td>
Ted Kremenekebb74132008-09-21 06:58:09 +0000562 <td class="Q">Line</td>
563 <td class="Q">Path Length</td>
Ted Kremenek2645c772008-07-07 16:58:44 +0000564 <td class="sorttable_nosort"></td>
Ted Kremenekebb74132008-09-21 06:58:09 +0000565 <!-- REPORTBUGCOL -->
566</tr></thead>
567<tbody>
Ted Kremenek5744dc22008-04-02 18:03:36 +0000568ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000569
Ted Kremenek991c54b2008-08-08 20:46:42 +0000570 my $prefix = GetPrefix();
571 my $regex;
572 my $InFileRegex;
573 my $InFilePrefix = "File:</td><td>";
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000574
Ted Kremenek991c54b2008-08-08 20:46:42 +0000575 if (defined $prefix) {
576 $regex = qr/^\Q$prefix\E/is;
577 $InFileRegex = qr/\Q$InFilePrefix$prefix\E/is;
578 }
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000579
Ted Kremenekebb74132008-09-21 06:58:09 +0000580 for my $row ( sort { $a->[2] cmp $b->[2] } @Index ) {
581 my $x = "$row->[1]:$row->[2]";
582 $x = lc $x;
583 $x =~ s/[ ,'":\/()]+/_/g;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000584
Ted Kremenek991c54b2008-08-08 20:46:42 +0000585 my $ReportFile = $row->[0];
Ted Kremenekebb74132008-09-21 06:58:09 +0000586
587 print OUT "<tr class=\"bt_$x\">";
588 print OUT "<td class=\"DESC\">";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000589 print OUT $row->[1];
Ted Kremenekebb74132008-09-21 06:58:09 +0000590 print OUT "</td>";
591 print OUT "<td class=\"DESC\">";
592 print OUT $row->[2];
593 print OUT "</td>";
594
595 # Update the file prefix.
596 my $fname = $row->[3];
597 my $full_fname = $fname;
598
Ted Kremenek991c54b2008-08-08 20:46:42 +0000599 if (defined $regex) {
600 $fname =~ s/$regex//;
601 UpdateInFilePath("$Dir/$ReportFile", $InFileRegex, $InFilePrefix)
602 }
Ted Kremenekebb74132008-09-21 06:58:09 +0000603
604 print OUT "<td>";
605 my $has_fname = 0;
606 if (-r $full_fname) {
607 $has_fname = 1;
608 print OUT "<a href=\"$full_fname\">"
609 }
610
611 my @fname = split /\//,$fname;
612 if ($#fname > 0) {
613 while ($#fname >= 0) {
614 my $x = shift @fname;
615 print OUT $x;
616 if ($#fname >= 0) {
617 print OUT "<span class=\"W\"> </span>/";
618 }
619 }
620 }
621 else {
622 print OUT $fname;
623 }
624
625 if ($has_fname) {
626 print OUT "</a>"
627 }
628 print OUT "</td>";
629
630 # Print out the quantities.
631 for my $j ( 4 .. 5 ) {
632 print OUT "<td class=\"Q\">$row->[$j]</td>";
633 }
634
Ted Kremenek991c54b2008-08-08 20:46:42 +0000635 # Print the rest of the columns.
Ted Kremenekebb74132008-09-21 06:58:09 +0000636 for (my $j = 6; $j <= $#{$row}; ++$j) {
637 print OUT "<td>$row->[$j]</td>"
Ted Kremenek991c54b2008-08-08 20:46:42 +0000638 }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000639
Ted Kremenek991c54b2008-08-08 20:46:42 +0000640 # Emit the "View" link.
Ted Kremenekebb74132008-09-21 06:58:09 +0000641 print OUT "<td class=\"View\"><a href=\"$ReportFile#EndPath\">View Report</a></td>";
Ted Kremenek3cea9ee2008-07-30 17:58:08 +0000642
Daniel Dunbare43038e2008-09-19 23:18:44 +0000643 # Emit REPORTBUG markers.
Ted Kremenekebb74132008-09-21 06:58:09 +0000644 print OUT "\n<!-- REPORTBUG id=\"$ReportFile\" -->\n";
Daniel Dunbare43038e2008-09-19 23:18:44 +0000645
Ted Kremenek991c54b2008-08-08 20:46:42 +0000646 # End the row.
647 print OUT "</tr>\n";
648 }
649
Ted Kremenekebb74132008-09-21 06:58:09 +0000650 print OUT "</tbody>\n</table>\n\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000651 }
652
653 if ($Crashes) {
654 # Read the crash directory for files.
655 opendir(DIR, "$Dir/crashes");
656 my @files = grep { /[.]info$/ } readdir(DIR);
657 closedir(DIR);
658
659 if (scalar(@files)) {
660 print OUT <<ENDTEXT;
Ted Kremenek5d31f832008-08-18 18:38:29 +0000661<h3>Analyzer Failures</h3>
Ted Kremenek991c54b2008-08-08 20:46:42 +0000662
Ted Kremenek5d31f832008-08-18 18:38:29 +0000663<p>The analyzer had problems processing the following files:</p>
Ted Kremenek991c54b2008-08-08 20:46:42 +0000664
665<table>
Ted Kremenek9f9b1fd2008-09-12 22:49:36 +0000666<thead><tr><td>Problem</td><td>Source File</td><td>Preprocessed File</td><td>STDERR Output</td></tr></thead>
Ted Kremenek991c54b2008-08-08 20:46:42 +0000667ENDTEXT
668
669 foreach my $file (sort @files) {
670 $file =~ /(.+).info$/;
671 # Get the preprocessed file.
672 my $ppfile = $1;
673 # Open the info file and get the name of the source file.
674 open (INFO, "$Dir/crashes/$file") or
675 die "Cannot open $Dir/crashes/$file\n";
676 my $srcfile = <INFO>;
Ted Kremenek5d31f832008-08-18 18:38:29 +0000677 chomp $srcfile;
678 my $problem = <INFO>;
679 chomp $problem;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000680 close (INFO);
681 # Print the information in the table.
Ted Kremenekd52e4252008-08-25 20:45:07 +0000682 my $prefix = GetPrefix();
Ted Kremenek9f9b1fd2008-09-12 22:49:36 +0000683 if (defined $prefix) { $srcfile =~ s/^\Q$prefix//; }
684 print OUT "<tr><td>$problem</td><td>$srcfile</td><td><a href=\"crashes/$ppfile\">$ppfile</a></td><td><a href=\"crashes/$ppfile.stderr.txt\">$ppfile.stderr.txt</a></td></tr>\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000685 }
686
687 print OUT <<ENDTEXT;
688</table>
689<p>Please consider submitting preprocessed files as <a href="http://clang.llvm.org/StaticAnalysisUsage.html#filingbugs">bug reports</a>.</p>
690ENDTEXT
691 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000692 }
693
Ted Kremenek991c54b2008-08-08 20:46:42 +0000694 print OUT "</body></html>\n";
Ted Kremenek5744dc22008-04-02 18:03:36 +0000695 close(OUT);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000696 CopyJS($Dir);
Ted Kremenek20161e92008-07-15 20:18:21 +0000697
698 # Make sure $Dir and $BaseDir are world readable/executable.
699 system("chmod", "755", $Dir);
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000700 if (defined $BaseDir) { system("chmod", "755", $BaseDir); }
Ted Kremenek20161e92008-07-15 20:18:21 +0000701
Ted Kremenek23cfca32008-06-16 22:40:14 +0000702 my $Num = scalar(@Index);
Ted Kremenek150c2122008-07-11 19:15:05 +0000703 Diag("$Num bugs found.\n");
704 if ($Num > 0 && -r "$Dir/index.html") {
705 Diag("Open '$Dir/index.html' to examine bug reports.\n");
706 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000707
Ted Kremenek991c54b2008-08-08 20:46:42 +0000708 DiagCrashes($Dir) if ($Crashes);
709
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000710 return $Num;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000711}
712
713##----------------------------------------------------------------------------##
Ted Kremenekdab11102008-04-02 04:43:42 +0000714# RunBuildCommand - Run the build command.
715##----------------------------------------------------------------------------##
716
Ted Kremenek6b628982008-04-30 23:47:12 +0000717sub AddIfNotPresent {
718 my $Args = shift;
719 my $Arg = shift;
720 my $found = 0;
721
722 foreach my $k (@$Args) {
723 if ($k eq $Arg) {
724 $found = 1;
725 last;
726 }
727 }
728
729 if ($found == 0) {
730 push @$Args, $Arg;
731 }
732}
733
Ted Kremenekdab11102008-04-02 04:43:42 +0000734sub RunBuildCommand {
735
736 my $Args = shift;
Ted Kremenek7442ca62008-04-02 16:04:51 +0000737 my $IgnoreErrors = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000738 my $Cmd = $Args->[0];
Ted Kremenek6195c372008-06-02 21:52:47 +0000739 my $CCAnalyzer = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000740
Ted Kremenek3301cb12008-06-30 18:18:16 +0000741 # Get only the part of the command after the last '/'.
742 if ($Cmd =~ /\/([^\/]+)$/) {
743 $Cmd = $1;
744 }
745
Ted Kremenek63c20172008-08-04 17:34:06 +0000746 if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc"
747 or $Cmd eq "ccc-analyzer") {
Ted Kremenekdab11102008-04-02 04:43:42 +0000748 shift @$Args;
Ted Kremenek6195c372008-06-02 21:52:47 +0000749 unshift @$Args, $CCAnalyzer;
Ted Kremenekdab11102008-04-02 04:43:42 +0000750 }
Ted Kremenek7442ca62008-04-02 16:04:51 +0000751 elsif ($IgnoreErrors) {
752 if ($Cmd eq "make" or $Cmd eq "gmake") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000753 AddIfNotPresent($Args,"-k");
Ted Kremenek8912b542008-05-13 21:28:02 +0000754 AddIfNotPresent($Args,"-i");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000755 }
756 elsif ($Cmd eq "xcodebuild") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000757 AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000758 }
Ted Kremenek6b628982008-04-30 23:47:12 +0000759 }
760
Ted Kremenek6b628982008-04-30 23:47:12 +0000761 if ($Cmd eq "xcodebuild") {
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000762 # Disable distributed builds for xcodebuild.
Ted Kremenek6b628982008-04-30 23:47:12 +0000763 AddIfNotPresent($Args,"-nodistribute");
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000764
765 # Disable PCH files until clang supports them.
766 AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO");
Ted Kremenek915e9722008-05-27 23:18:07 +0000767
768 # When 'CC' is set, xcodebuild uses it to do all linking, even if we are
769 # linking C++ object files. Set 'LDPLUSPLUS' so that xcodebuild uses 'g++'
770 # when linking such files.
Ted Kremenek95aa1052008-09-04 17:52:41 +0000771 die if (!defined $CXX);
772 my $LDPLUSPLUS = `which $CXX`;
Ted Kremenek915e9722008-05-27 23:18:07 +0000773 $LDPLUSPLUS =~ s/\015?\012//; # strip newlines
774 $ENV{'LDPLUSPLUS'} = $LDPLUSPLUS;
Ted Kremenek6b628982008-04-30 23:47:12 +0000775 }
Ted Kremenekdab11102008-04-02 04:43:42 +0000776
Ted Kremenek5a4ddaf2008-08-25 20:10:45 +0000777 return (system(@$Args) >> 8);
Ted Kremenekdab11102008-04-02 04:43:42 +0000778}
779
Ted Kremenekdab11102008-04-02 04:43:42 +0000780##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000781# DisplayHelp - Utility function to display all help options.
782##----------------------------------------------------------------------------##
783
Sam Bishopa0e22662008-04-02 03:35:43 +0000784sub DisplayHelp {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000785
Ted Kremenek5744dc22008-04-02 18:03:36 +0000786print <<ENDTEXT;
Sam Bishopa0e22662008-04-02 03:35:43 +0000787USAGE: $Prog [options] <build command> [build options]
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000788
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000789ENDTEXT
790
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000791 if (defined $BuildName) {
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000792 print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n";
793 }
794
795print <<ENDTEXT;
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000796OPTIONS:
797
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000798 -o - Target directory for HTML report files. Subdirectories
Sam Bishopa0e22662008-04-02 03:35:43 +0000799 will be created as needed to represent separate "runs" of
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000800 the analyzer. If this option is not specified, a directory
801 is created in /tmp to store the reports.
Ted Kremenek1262fc42008-05-14 20:10:33 +0000802
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000803 -h - Display this message.
804 --help
Ted Kremenek1262fc42008-05-14 20:10:33 +0000805
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000806 -k - Add a "keep on going" option to the specified build command.
807 --keep-going This option currently supports make and xcodebuild.
Ted Kremenekf02e8db2008-04-02 16:41:25 +0000808 This is a convenience option; one can specify this
809 behavior directly using build options.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000810
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000811 --status-bugs - By default, the exit status of $Prog is the same as the
812 executed build command. Specifying this option causes the
813 exit status of $Prog to be 1 if it found potential bugs
814 and 0 otherwise.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000815
Ted Kremenek386c6932008-09-03 17:59:35 +0000816 --use-cc [compiler path] - By default, $Prog uses 'gcc' to compile and link
817 --use-cc=[compiler path] your C and Objective-C code. Use this option
818 to specify an alternate compiler.
819
820 --use-c++ [compiler path] - By default, $Prog uses 'g++' to compile and link
821 --use-c++=[compiler path] your C++ and Objective-C++ code. Use this option
822 to specify an alternate compiler.
Ted Kremenekf17ef3c2008-08-21 21:47:09 +0000823
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000824 -v - Verbose output from $Prog and the analyzer.
Ted Kremenek386c6932008-09-03 17:59:35 +0000825 A second and third '-v' increases verbosity.
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000826
827 -V - View analysis results in a web browser when the build
828 --view completes.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000829
Ted Kremenekb7770c02008-07-15 17:06:13 +0000830
Ted Kremenek386c6932008-09-03 17:59:35 +0000831AVAILABLE ANALYSES (multiple analyses may be specified):
Ted Kremenekd52e4252008-08-25 20:45:07 +0000832
833ENDTEXT
Ted Kremenekb7770c02008-07-15 17:06:13 +0000834
835 foreach my $Analysis (sort keys %AvailableAnalyses) {
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000836 if (defined $AnalysesDefaultEnabled{$Analysis}) {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000837 print " (+)";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000838 }
839 else {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000840 print " ";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000841 }
842
843 print " $Analysis $AvailableAnalyses{$Analysis}\n";
844 }
845
846print <<ENDTEXT
847
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000848 NOTE: "(+)" indicates that an analysis is enabled by default unless one
849 or more analysis options are specified
Ted Kremenekb7770c02008-07-15 17:06:13 +0000850
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000851BUILD OPTIONS
852
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000853 You can specify any build option acceptable to the build command.
Ted Kremenek39eefde2008-04-02 16:47:27 +0000854
Ted Kremenek5744dc22008-04-02 18:03:36 +0000855EXAMPLE
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000856
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000857 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000858
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000859 The above example causes analysis reports to be deposited into
860 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
861 A different subdirectory is created each time $Prog analyzes a project.
862 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000863
864ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000865}
866
867##----------------------------------------------------------------------------##
868# Process command-line arguments.
869##----------------------------------------------------------------------------##
870
871my $HtmlDir; # Parent directory to store HTML files.
872my $IgnoreErrors = 0; # Ignore build errors.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000873my $ViewResults = 0; # View results when the build terminates.
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000874my $ExitStatusFoundBugs = 0; # Exit status reflects whether bugs were found
Ted Kremenekb7770c02008-07-15 17:06:13 +0000875my @AnalysesToRun;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000876
877if (!@ARGV) {
878 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000879 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000880}
881
882while (@ARGV) {
883
884 # Scan for options we recognize.
885
886 my $arg = $ARGV[0];
887
Sam Bishop2f2418e2008-04-03 14:29:47 +0000888 if ($arg eq "-h" or $arg eq "--help") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000889 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000890 exit 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000891 }
892
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000893 if (defined $AvailableAnalyses{$arg}) {
Ted Kremenek1262fc42008-05-14 20:10:33 +0000894 shift @ARGV;
Ted Kremenekb7770c02008-07-15 17:06:13 +0000895 push @AnalysesToRun, $arg;
Ted Kremenek1262fc42008-05-14 20:10:33 +0000896 next;
897 }
898
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000899 if ($arg eq "-o") {
900 shift @ARGV;
901
902 if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000903 DieDiag("'-o' option requires a target directory name.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000904 }
905
906 $HtmlDir = shift @ARGV;
907 next;
908 }
909
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000910 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000911 shift @ARGV;
912 $IgnoreErrors = 1;
913 next;
914 }
915
Ted Kremenekf17ef3c2008-08-21 21:47:09 +0000916 if ($arg =~ /^--use-cc(=(.+))?$/) {
917 shift @ARGV;
918 my $cc;
919
920 if ($2 eq "") {
921 if (!@ARGV) {
922 DieDiag("'--use-cc' option requires a compiler executable name.\n");
923 }
924 $cc = shift @ARGV;
925 }
926 else {
927 $cc = $2;
928 }
929
930 $ENV{"CCC_CC"} = $cc;
931 next;
932 }
933
Ted Kremenek386c6932008-09-03 17:59:35 +0000934 if ($arg =~ /^--use-c[+][+](=(.+))?$/) {
935 shift @ARGV;
936
937 if ($2 eq "") {
938 if (!@ARGV) {
939 DieDiag("'--use-c++' option requires a compiler executable name.\n");
940 }
941 $CXX = shift @ARGV;
942 }
943 else {
944 $CXX = $2;
945 }
946 next;
947 }
948
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000949 if ($arg eq "-v") {
950 shift @ARGV;
951 $Verbose++;
952 next;
953 }
954
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000955 if ($arg eq "-V" or $arg eq "--view") {
956 shift @ARGV;
957 $ViewResults = 1;
958 next;
959 }
960
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000961 if ($arg eq "--status-bugs") {
962 shift @ARGV;
963 $ExitStatusFoundBugs = 1;
964 next;
965 }
966
Ted Kremenek23cfca32008-06-16 22:40:14 +0000967 DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/);
Ted Kremenek0062ad42008-04-02 16:35:01 +0000968
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000969 last;
970}
971
972if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000973 Diag("No build command specified.\n\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000974 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000975 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000976}
977
Ted Kremenek386c6932008-09-03 17:59:35 +0000978
979
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000980# Determine the output directory for the HTML reports.
Ted Kremenek684bb092008-04-18 15:18:20 +0000981my $BaseDir = $HtmlDir;
Sam Bishopa0e22662008-04-02 03:35:43 +0000982$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000983
984# Set the appropriate environment variables.
Sam Bishopa0e22662008-04-02 03:35:43 +0000985SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000986
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000987my $Cmd = "$RealBin/ccc-analyzer";
988
Ted Kremenek23cfca32008-06-16 22:40:14 +0000989DieDiag("Executable 'ccc-analyzer' does not exist at '$Cmd'\n")
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000990 if (! -x $Cmd);
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000991
Ted Kremenekb7770c02008-07-15 17:06:13 +0000992if (! -x $ClangSB) {
993 Diag("'clang' executable not found in '$RealBin'.\n");
994 Diag("Using 'clang' from path.\n");
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000995}
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000996
Ted Kremenek95aa1052008-09-04 17:52:41 +0000997if (defined $CXX) {
998 $ENV{'CXX'} = $CXX;
999}
1000else {
1001 $CXX = 'g++'; # This variable is used by other parts of scan-build
1002 # that need to know a default C++ compiler to fall back to.
1003}
1004
Ted Kremenek4f4b17d2008-04-03 20:08:18 +00001005$ENV{'CC'} = $Cmd;
Ted Kremenekf22eacb2008-04-18 22:00:56 +00001006$ENV{'CLANG'} = $Clang;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001007
1008if ($Verbose >= 2) {
1009 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
1010}
1011
Ted Kremeneka9525c92008-05-12 22:07:14 +00001012if ($Verbose >= 3) {
1013 $ENV{'CCC_ANALYZER_LOG'} = 1;
1014}
1015
Ted Kremenek90125992008-07-15 23:41:32 +00001016if (scalar(@AnalysesToRun) == 0) {
1017 foreach my $key (keys %AnalysesDefaultEnabled) {
1018 push @AnalysesToRun,$key;
1019 }
Ted Kremenek01006782008-07-02 23:16:10 +00001020}
Ted Kremenek1262fc42008-05-14 20:10:33 +00001021
Ted Kremenek90125992008-07-15 23:41:32 +00001022$ENV{'CCC_ANALYZER_ANALYSIS'} = join ' ',@AnalysesToRun;
1023
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001024# Run the build.
Ted Kremenek5656a982008-07-15 17:09:28 +00001025my $ExitStatus = RunBuildCommand(\@ARGV, $IgnoreErrors, $Cmd);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001026
1027# Postprocess the HTML directory.
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001028my $NumBugs = Postprocess($HtmlDir, $BaseDir);
Ted Kremenek7f8a3252008-04-02 18:42:49 +00001029
1030if ($ViewResults and -r "$HtmlDir/index.html") {
1031 # Only works on Mac OS X (for now).
1032 print "Viewing analysis results: '$HtmlDir/index.html'\n";
Ted Kremenek20161e92008-07-15 20:18:21 +00001033 system("open", "$HtmlDir/index.html");
Ted Kremenek7f8a3252008-04-02 18:42:49 +00001034}
Ted Kremenek5656a982008-07-15 17:09:28 +00001035
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001036if ($ExitStatusFoundBugs) {
1037 exit 1 if ($NumBugs > 0);
1038 exit 0;
1039}
1040
Ted Kremenek5656a982008-07-15 17:09:28 +00001041exit $ExitStatus;
1042