blob: 540a60b2f384f6ffe3b1cbadf1b8b672309d28c0 [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 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
Ted Kremenek991c54b2008-08-08 20:46:42 +000046sub DiagCrashes {
47 my $Dir = shift;
48 Diag ("The analyzer crashed on some source files.\n");
Ted Kremenek386c6932008-09-03 17:59:35 +000049 Diag ("Preprocessed versions of crashed files were deposited in '$Dir/crashes'.\n");
Ted Kremenek991c54b2008-08-08 20:46:42 +000050 Diag ("Please consider submitting a bug report using these files:\n");
51 Diag (" http://clang.llvm.org/StaticAnalysisUsage.html#filingbugs\n")
52}
53
Ted Kremenek23cfca32008-06-16 22:40:14 +000054sub DieDiag {
55 if ($UseColor) {
56 print BOLD, RED "$Prog: ";
57 print RESET, RED @_;
58 print RESET;
59 }
60 else {
61 print "$Prog: ", @_;
62 }
63 exit(0);
64}
65
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000066##----------------------------------------------------------------------------##
Ted Kremenekb7770c02008-07-15 17:06:13 +000067# Some initial preprocessing of Clang options.
68##----------------------------------------------------------------------------##
69
70my $ClangSB = "$RealBin/clang";
71my $Clang = $ClangSB;
72
73if (! -x $ClangSB) {
74 $Clang = "clang";
75}
76
77my %AvailableAnalyses;
78
79# Query clang for analysis options.
Ted Kremenek63c20172008-08-04 17:34:06 +000080open(PIPE, "-|", $Clang, "--help") or
Ted Kremenekb7770c02008-07-15 17:06:13 +000081 DieDiag("Cannot execute '$Clang'");
Ted Kremenek63c20172008-08-04 17:34:06 +000082
Ted Kremenekb7770c02008-07-15 17:06:13 +000083my $FoundAnalysis = 0;
84
85while(<PIPE>) {
86 if ($FoundAnalysis == 0) {
87 if (/Available Source Code Analyses/) {
88 $FoundAnalysis = 1;
89 }
Ted Kremenek991c54b2008-08-08 20:46:42 +000090
Ted Kremenekb7770c02008-07-15 17:06:13 +000091 next;
92 }
93
94 if (/^\s\s\s\s([^\s]+)\s(.+)$/) {
95 next if ($1 =~ /-dump/ or $1 =~ /-view/
96 or $1 =~ /-checker-simple/ or $1 =~ /-warn-uninit/);
97
98 $AvailableAnalyses{$1} = $2;
99 next;
100 }
101
102 last;
103}
104
105close (PIPE);
106
107my %AnalysesDefaultEnabled = (
108 '-warn-dead-stores' => 1,
109 '-checker-cfref' => 1,
Ted Kremenek90125992008-07-15 23:41:32 +0000110 '-warn-objc-methodsigs' => 1,
Ted Kremenekbde3a052008-07-25 20:35:01 +0000111 '-warn-objc-missing-dealloc' => 1,
112 '-warn-objc-unused-ivars' => 1
Ted Kremenekb7770c02008-07-15 17:06:13 +0000113);
114
115##----------------------------------------------------------------------------##
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000116# GetHTMLRunDir - Construct an HTML directory name for the current sub-run.
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000117##----------------------------------------------------------------------------##
118
Sam Bishopa0e22662008-04-02 03:35:43 +0000119sub GetHTMLRunDir {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000120
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000121 die "Not enough arguments." if (@_ == 0);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000122 my $Dir = shift @_;
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000123
124 my $TmpMode = 0;
125 if (!defined $Dir) {
126 $Dir = "/tmp";
127 $TmpMode = 1;
128 }
129
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000130 # Get current date and time.
131
132 my @CurrentTime = localtime();
133
134 my $year = $CurrentTime[5] + 1900;
135 my $day = $CurrentTime[3];
136 my $month = $CurrentTime[4] + 1;
137
Ted Kremenek9d7405f2008-05-14 17:23:56 +0000138 my $DateString = sprintf("%d-%02d-%02d", $year, $month, $day);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000139
140 # Determine the run number.
141
142 my $RunNumber;
143
144 if (-d $Dir) {
145
146 if (! -r $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000147 DieDiag("directory '$Dir' exists but is not readable.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000148 }
149
150 # Iterate over all files in the specified directory.
151
152 my $max = 0;
153
154 opendir(DIR, $Dir);
Ted Kremenek29da6c52008-08-07 17:57:34 +0000155 my @FILES = grep { -d "$Dir/$_" } readdir(DIR);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000156 closedir(DIR);
157
158 foreach my $f (@FILES) {
159
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000160 # Strip the prefix '$Prog-' if we are dumping files to /tmp.
161 if ($TmpMode) {
162 next if (!($f =~ /^$Prog-(.+)/));
163 $f = $1;
164 }
165
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000166 my @x = split/-/, $f;
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000167
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000168 next if (scalar(@x) != 4);
169 next if ($x[0] != $year);
170 next if ($x[1] != $month);
171 next if ($x[2] != $day);
172
173 if ($x[3] > $max) {
174 $max = $x[3];
175 }
176 }
177
178 $RunNumber = $max + 1;
179 }
180 else {
181
182 if (-x $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000183 DieDiag("'$Dir' exists but is not a directory.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000184 }
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000185
186 if ($TmpMode) {
187 DieDiag("The directory '/tmp' does not exist or cannot be accessed.");
188 }
189
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000190 # $Dir does not exist. It will be automatically created by the
191 # clang driver. Set the run number to 1.
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000192
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000193 $RunNumber = 1;
194 }
195
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000196 die "RunNumber must be defined!" if (!defined $RunNumber);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000197
198 # Append the run number.
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000199 if ($TmpMode) {
200 my $NewDir = "$Dir/$Prog-$DateString-$RunNumber";
201 mkdir $NewDir;
202 return $NewDir;
203 }
204 else {
205 return "$Dir/$DateString-$RunNumber";
206 }
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000207}
208
Sam Bishopa0e22662008-04-02 03:35:43 +0000209sub SetHtmlEnv {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000210
211 die "Wrong number of arguments." if (scalar(@_) != 2);
212
213 my $Args = shift;
214 my $Dir = shift;
215
216 die "No build command." if (scalar(@$Args) == 0);
217
218 my $Cmd = $$Args[0];
219
220 if ($Cmd =~ /configure/) {
221 return;
222 }
223
224 if ($Verbose) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000225 Diag("Emitting reports for this run to '$Dir'.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000226 }
227
228 $ENV{'CCC_ANALYZER_HTML'} = $Dir;
229}
230
231##----------------------------------------------------------------------------##
Ted Kremenek57cf4462008-04-18 15:09:30 +0000232# ComputeDigest - Compute a digest of the specified file.
233##----------------------------------------------------------------------------##
234
235sub ComputeDigest {
236 my $FName = shift;
Ted Kremenek23cfca32008-06-16 22:40:14 +0000237 DieDiag("Cannot read $FName to compute Digest.\n") if (! -r $FName);
Ted Kremeneka6e24812008-04-19 18:05:48 +0000238
239 # Use Digest::MD5. We don't have to be cryptographically secure. We're
Ted Kremenek7ea02e62008-04-19 18:07:44 +0000240 # just looking for duplicate files that come from a non-malicious source.
241 # We use Digest::MD5 because it is a standard Perl module that should
Ted Kremenek63c20172008-08-04 17:34:06 +0000242 # come bundled on most systems.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000243 open(FILE, $FName) or DieDiag("Cannot open $FName when computing Digest.\n");
Ted Kremeneka6e24812008-04-19 18:05:48 +0000244 binmode FILE;
245 my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest;
246 close(FILE);
247
Ted Kremenek63c20172008-08-04 17:34:06 +0000248 # Return the digest.
Ted Kremeneka6e24812008-04-19 18:05:48 +0000249 return $Result;
Ted Kremenek57cf4462008-04-18 15:09:30 +0000250}
251
252##----------------------------------------------------------------------------##
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000253# UpdatePrefix - Compute the common prefix of files.
254##----------------------------------------------------------------------------##
255
256my $Prefix;
257
258sub UpdatePrefix {
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000259 my $x = shift;
260 my $y = basename($x);
261 $x =~ s/\Q$y\E$//;
262
263 # Ignore /usr, /Library, /System, /Developer
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000264 return if ( $x =~ /^\/usr/ or $x =~ /^\/Library/
265 or $x =~ /^\/System/ or $x =~ /^\/Developer/);
266
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000267 if (!defined $Prefix) {
268 $Prefix = $x;
269 return;
270 }
271
272 chop $Prefix while (!($x =~ /^$Prefix/));
273}
274
275sub GetPrefix {
276 return $Prefix;
277}
278
279##----------------------------------------------------------------------------##
280# UpdateInFilePath - Update the path in the report file.
281##----------------------------------------------------------------------------##
282
283sub UpdateInFilePath {
284 my $fname = shift;
285 my $regex = shift;
286 my $newtext = shift;
Ted Kremenek63c20172008-08-04 17:34:06 +0000287
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000288 open (RIN, $fname) or die "cannot open $fname";
Ted Kremenek63c20172008-08-04 17:34:06 +0000289 open (ROUT, ">", "$fname.tmp") or die "cannot open $fname.tmp";
290
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000291 while (<RIN>) {
292 s/$regex/$newtext/;
293 print ROUT $_;
294 }
Ted Kremenek63c20172008-08-04 17:34:06 +0000295
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000296 close (ROUT);
297 close (RIN);
Ted Kremenek20161e92008-07-15 20:18:21 +0000298 system("mv", "$fname.tmp", $fname);
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000299}
300
301##----------------------------------------------------------------------------##
Ted Kremenek5744dc22008-04-02 18:03:36 +0000302# ScanFile - Scan a report file for various identifying attributes.
303##----------------------------------------------------------------------------##
304
Ted Kremenek57cf4462008-04-18 15:09:30 +0000305# Sometimes a source file is scanned more than once, and thus produces
306# multiple error reports. We use a cache to solve this problem.
307
308my %AlreadyScanned;
309
Ted Kremenek5744dc22008-04-02 18:03:36 +0000310sub ScanFile {
311
312 my $Index = shift;
313 my $Dir = shift;
314 my $FName = shift;
315
Ted Kremenek57cf4462008-04-18 15:09:30 +0000316 # Compute a digest for the report file. Determine if we have already
317 # scanned a file that looks just like it.
318
319 my $digest = ComputeDigest("$Dir/$FName");
320
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000321 if (defined $AlreadyScanned{$digest}) {
Ted Kremenek57cf4462008-04-18 15:09:30 +0000322 # Redundant file. Remove it.
Ted Kremenek20161e92008-07-15 20:18:21 +0000323 system ("rm", "-f", "$Dir/$FName");
Ted Kremenek57cf4462008-04-18 15:09:30 +0000324 return;
325 }
326
327 $AlreadyScanned{$digest} = 1;
328
Ted Kremenek809709f2008-04-18 16:58:34 +0000329 # At this point the report file is not world readable. Make it happen.
Ted Kremenek20161e92008-07-15 20:18:21 +0000330 system ("chmod", "644", "$Dir/$FName");
Ted Kremenek684bb092008-04-18 15:18:20 +0000331
332 # Scan the report file for tags.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000333 open(IN, "$Dir/$FName") or DieDiag("Cannot open '$Dir/$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000334
335 my $BugDesc = "";
Ted Kremenek22d6a632008-04-02 20:43:36 +0000336 my $BugFile = "";
337 my $BugPathLength = 1;
338 my $BugLine = 0;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000339
340 while (<IN>) {
341
342 if (/<!-- BUGDESC (.*) -->$/) {
343 $BugDesc = $1;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000344 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000345 elsif (/<!-- BUGFILE (.*) -->$/) {
346 $BugFile = $1;
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000347 UpdatePrefix($BugFile);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000348 }
349 elsif (/<!-- BUGPATHLENGTH (.*) -->$/) {
350 $BugPathLength = $1;
351 }
352 elsif (/<!-- BUGLINE (.*) -->$/) {
353 $BugLine = $1;
354 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000355 }
356
357 close(IN);
358
Ted Kremenek22d6a632008-04-02 20:43:36 +0000359 push @$Index,[ $FName, $BugDesc, $BugFile, $BugLine, $BugPathLength ];
360}
361
362##----------------------------------------------------------------------------##
363# CopyJS - Copy JavaScript code to target directory.
364##----------------------------------------------------------------------------##
365
366sub CopyJS {
367
368 my $Dir = shift;
369
Ted Kremenek23cfca32008-06-16 22:40:14 +0000370 DieDiag("Cannot find 'sorttable.js'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000371 if (! -r "$RealBin/sorttable.js");
372
Ted Kremenek20161e92008-07-15 20:18:21 +0000373 system ("cp", "$RealBin/sorttable.js", "$Dir");
Ted Kremenek22d6a632008-04-02 20:43:36 +0000374
Ted Kremenek23cfca32008-06-16 22:40:14 +0000375 DieDiag("Could not copy 'sorttable.js' to '$Dir'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000376 if (! -r "$Dir/sorttable.js");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000377}
378
379##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000380# Postprocess - Postprocess the results of an analysis scan.
381##----------------------------------------------------------------------------##
382
Sam Bishopa0e22662008-04-02 03:35:43 +0000383sub Postprocess {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000384
385 my $Dir = shift;
Ted Kremenek684bb092008-04-18 15:18:20 +0000386 my $BaseDir = shift;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000387
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000388 die "No directory specified." if (!defined $Dir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000389
390 if (! -d $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000391 Diag("No bugs found.\n");
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000392 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000393 }
394
395 opendir(DIR, $Dir);
Ted Kremenek991c54b2008-08-08 20:46:42 +0000396 my $Crashes = 0;
397 my @files = grep { if ($_ eq "crashes") { $Crashes++; }
398 /^report-.*\.html$/; } readdir(DIR);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000399 closedir(DIR);
400
Ted Kremenek991c54b2008-08-08 20:46:42 +0000401 if (scalar(@files) == 0 and $Crashes == 0) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000402 Diag("Removing directory '$Dir' because it contains no reports.\n");
Ted Kremenek20161e92008-07-15 20:18:21 +0000403 system ("rm", "-fR", $Dir);
Ted Kremenek23cfca32008-06-16 22:40:14 +0000404 # Remove the base directory if it contains no files (don't use '-R').
Ted Kremenek991c54b2008-08-08 20:46:42 +0000405 system ("rm", "-f", $BaseDir) if (defined $BaseDir);
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000406 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000407 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000408
Ted Kremenek991c54b2008-08-08 20:46:42 +0000409 # Scan each report file and build an index.
410 my @Index;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000411 foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); }
412
Ted Kremenekd52e4252008-08-25 20:45:07 +0000413 # Scan the crashes directory and use the information in the .info files
414 # to update the common prefix directory.
415 if (-d "$Dir/crashes") {
416 opendir(DIR, "$Dir/crashes");
417 my @files = grep { /[.]info$/; } readdir(DIR);
418 closedir(DIR);
419 foreach my $file (@files) {
420 open IN, "$Dir/crashes/$file" or DieDiag("cannot open $file\n");
421 my $Path = <IN>;
422 if (defined $Path) { UpdatePrefix($Path); }
423 close IN;
424 }
425 }
426
Ted Kremenek63c20172008-08-04 17:34:06 +0000427 # Generate an index.html file.
428 my $FName = "$Dir/index.html";
429 open(OUT, ">", $FName) or DieDiag("Cannot create file '$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000430
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000431 # Print out the header.
432
Ted Kremenek5744dc22008-04-02 18:03:36 +0000433print OUT <<ENDTEXT;
434<html>
435<head>
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000436<style type="text/css">
437 body { color:#000000; background-color:#ffffff }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000438 body { font-family: Helvetica, sans-serif; font-size:9pt }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000439 h1 { font-size:12pt }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000440 table thead {
Ted Kremenek22d6a632008-04-02 20:43:36 +0000441 background-color:#eee; color:#666666;
442 font-weight: bold; cursor: default;
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000443 text-align:center;
444 border-top: 2px solid #000000;
445 border-bottom: 2px solid #000000;
446 font-weight: bold; font-family: Verdana
447 }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000448 table { border: 1px #000000 solid }
449 table { border-collapse: collapse; border-spacing: 0px }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000450 td { border-bottom: 1px #000000 dotted }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000451 td { padding:5px; padding-left:8px; padding-right:8px }
Ted Kremenekd8c6d0c2008-04-07 23:50:07 +0000452 td { text-align:left; font-size:9pt }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000453 td.View { padding-left: 10px }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000454</style>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000455<script src="sorttable.js"></script>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000456<script language='javascript' type="text/javascript">
457function SetDisplay(RowClass, DisplayVal)
458{
459 var Rows = document.getElementsByTagName("tr");
460 for ( var i = 0 ; i < Rows.length; ++i ) {
461 if (Rows[i].className == RowClass) {
462 Rows[i].style.display = DisplayVal;
463 }
464 }
465}
466
467function ToggleDisplay(CheckButton, ClassName) {
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000468 if (CheckButton.checked) {
469 SetDisplay(ClassName, "");
470 }
471 else {
472 SetDisplay(ClassName, "none");
473 }
474}
475</script>
476</head>
477<body>
478ENDTEXT
479
Ted Kremenek991c54b2008-08-08 20:46:42 +0000480 if (scalar(@files)) {
481 # Print out the summary table.
482 my %Totals;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000483
Ted Kremenek991c54b2008-08-08 20:46:42 +0000484 for my $row ( @Index ) {
485 #my $bug_type = lc($row->[1]);
486 my $bug_type = ($row->[1]);
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000487
Ted Kremenek991c54b2008-08-08 20:46:42 +0000488 if (!defined $Totals{$bug_type}) { $Totals{$bug_type} = 1; }
489 else { $Totals{$bug_type}++; }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000490 }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000491
492 print OUT "<h3>Bug Summary</h3>";
493
494 if (defined $BuildName) {
495 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 +0000496 }
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000497
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000498print OUT <<ENDTEXT;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000499<table class="sortable">
500<tr>
501 <td>Bug Type</td>
502 <td>Quantity</td>
Ted Kremenek2645c772008-07-07 16:58:44 +0000503 <td class="sorttable_nosort">Display?</td>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000504</tr>
505ENDTEXT
506
Ted Kremenek991c54b2008-08-08 20:46:42 +0000507 for my $key ( sort { $a cmp $b } keys %Totals ) {
508 my $x = lc($key);
509 $x =~ s/[ ,'"]+/_/g;
510 print OUT "<tr><td>$key</td><td>$Totals{$key}</td><td><input type=\"checkbox\" onClick=\"ToggleDisplay(this,'bt_$x');\" checked/></td></tr>\n";
511 }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000512
513 # Print out the table of errors.
514
515print OUT <<ENDTEXT;
516</table>
517<h3>Reports</h3>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000518<table class="sortable">
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000519<tr>
Ted Kremenek88a96d62008-07-07 17:23:32 +0000520 <td class="sorttable_sorted">Bug Type<span id="sorttable_sortfwdind">&nbsp;&#x25BE;</span>
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000521 <td>File</td>
522 <td>Line</td>
523 <td>Path Length</td>
Ted Kremenek2645c772008-07-07 16:58:44 +0000524 <td class="sorttable_nosort"></td>
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000525</tr>
Ted Kremenek5744dc22008-04-02 18:03:36 +0000526ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000527
Ted Kremenek991c54b2008-08-08 20:46:42 +0000528 my $prefix = GetPrefix();
529 my $regex;
530 my $InFileRegex;
531 my $InFilePrefix = "File:</td><td>";
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000532
Ted Kremenek991c54b2008-08-08 20:46:42 +0000533 if (defined $prefix) {
534 $regex = qr/^\Q$prefix\E/is;
535 $InFileRegex = qr/\Q$InFilePrefix$prefix\E/is;
536 }
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000537
Ted Kremenek991c54b2008-08-08 20:46:42 +0000538 for my $row ( sort { $a->[1] cmp $b->[1] } @Index ) {
Ted Kremenek5744dc22008-04-02 18:03:36 +0000539
Ted Kremenek991c54b2008-08-08 20:46:42 +0000540 my $x = lc($row->[1]);
541 $x =~ s/[ ,'"]+/_/g;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000542
Ted Kremenek991c54b2008-08-08 20:46:42 +0000543 print OUT "<tr class=\"bt_$x\">\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000544
Ted Kremenek991c54b2008-08-08 20:46:42 +0000545 my $ReportFile = $row->[0];
Ted Kremenek5744dc22008-04-02 18:03:36 +0000546
Ted Kremenek991c54b2008-08-08 20:46:42 +0000547 print OUT " <td class=\"DESC\">";
548 #print OUT lc($row->[1]);
549 print OUT $row->[1];
550 print OUT "</td>\n";
Ted Kremenek5744dc22008-04-02 18:03:36 +0000551
Ted Kremenek991c54b2008-08-08 20:46:42 +0000552 # Update the file prefix.
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000553
Ted Kremenek991c54b2008-08-08 20:46:42 +0000554 my $fname = $row->[2];
555 if (defined $regex) {
556 $fname =~ s/$regex//;
557 UpdateInFilePath("$Dir/$ReportFile", $InFileRegex, $InFilePrefix)
558 }
Ted Kremenek3e56e0b2008-05-02 23:40:49 +0000559
Ted Kremenek991c54b2008-08-08 20:46:42 +0000560 print OUT "<td>$fname</td>\n";
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000561
Ted Kremenek991c54b2008-08-08 20:46:42 +0000562 # Print the rest of the columns.
563 for my $j ( 3 .. $#{$row} ) {
564 print OUT "<td>$row->[$j]</td>\n"
565 }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000566
Ted Kremenek991c54b2008-08-08 20:46:42 +0000567 # Emit the "View" link.
568 print OUT " <td class=\"View\"><a href=\"$ReportFile#EndPath\">View</a></td>\n";
Ted Kremenek3cea9ee2008-07-30 17:58:08 +0000569
Ted Kremenek991c54b2008-08-08 20:46:42 +0000570 # End the row.
571 print OUT "</tr>\n";
572 }
573
574 print OUT "</table>\n";
575 }
576
577 if ($Crashes) {
578 # Read the crash directory for files.
579 opendir(DIR, "$Dir/crashes");
580 my @files = grep { /[.]info$/ } readdir(DIR);
581 closedir(DIR);
582
583 if (scalar(@files)) {
584 print OUT <<ENDTEXT;
Ted Kremenek5d31f832008-08-18 18:38:29 +0000585<h3>Analyzer Failures</h3>
Ted Kremenek991c54b2008-08-08 20:46:42 +0000586
Ted Kremenek5d31f832008-08-18 18:38:29 +0000587<p>The analyzer had problems processing the following files:</p>
Ted Kremenek991c54b2008-08-08 20:46:42 +0000588
589<table>
Ted Kremenek5d31f832008-08-18 18:38:29 +0000590<thead><tr><td>Problem</td><td>Source File</td><td>Preprocessed File</td></tr></thead>
Ted Kremenek991c54b2008-08-08 20:46:42 +0000591ENDTEXT
592
593 foreach my $file (sort @files) {
594 $file =~ /(.+).info$/;
595 # Get the preprocessed file.
596 my $ppfile = $1;
597 # Open the info file and get the name of the source file.
598 open (INFO, "$Dir/crashes/$file") or
599 die "Cannot open $Dir/crashes/$file\n";
600 my $srcfile = <INFO>;
Ted Kremenek5d31f832008-08-18 18:38:29 +0000601 chomp $srcfile;
602 my $problem = <INFO>;
603 chomp $problem;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000604 close (INFO);
605 # Print the information in the table.
Ted Kremenekd52e4252008-08-25 20:45:07 +0000606 my $prefix = GetPrefix();
607 if (defined $prefix) { $srcfile =~ s/^$prefix//; }
Ted Kremenek5d31f832008-08-18 18:38:29 +0000608 print OUT "<tr><td>$problem</td><td>$srcfile</td><td class=\"View\"><a href=\"crashes/$ppfile\">View</a></td></tr>\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000609 }
610
611 print OUT <<ENDTEXT;
612</table>
613<p>Please consider submitting preprocessed files as <a href="http://clang.llvm.org/StaticAnalysisUsage.html#filingbugs">bug reports</a>.</p>
614ENDTEXT
615 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000616 }
617
Ted Kremenek991c54b2008-08-08 20:46:42 +0000618 print OUT "</body></html>\n";
Ted Kremenek5744dc22008-04-02 18:03:36 +0000619 close(OUT);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000620 CopyJS($Dir);
Ted Kremenek20161e92008-07-15 20:18:21 +0000621
622 # Make sure $Dir and $BaseDir are world readable/executable.
623 system("chmod", "755", $Dir);
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000624 if (defined $BaseDir) { system("chmod", "755", $BaseDir); }
Ted Kremenek20161e92008-07-15 20:18:21 +0000625
Ted Kremenek23cfca32008-06-16 22:40:14 +0000626 my $Num = scalar(@Index);
Ted Kremenek150c2122008-07-11 19:15:05 +0000627 Diag("$Num bugs found.\n");
628 if ($Num > 0 && -r "$Dir/index.html") {
629 Diag("Open '$Dir/index.html' to examine bug reports.\n");
630 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000631
Ted Kremenek991c54b2008-08-08 20:46:42 +0000632 DiagCrashes($Dir) if ($Crashes);
633
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000634 return $Num;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000635}
636
637##----------------------------------------------------------------------------##
Ted Kremenekdab11102008-04-02 04:43:42 +0000638# RunBuildCommand - Run the build command.
639##----------------------------------------------------------------------------##
640
Ted Kremenek6b628982008-04-30 23:47:12 +0000641sub AddIfNotPresent {
642 my $Args = shift;
643 my $Arg = shift;
644 my $found = 0;
645
646 foreach my $k (@$Args) {
647 if ($k eq $Arg) {
648 $found = 1;
649 last;
650 }
651 }
652
653 if ($found == 0) {
654 push @$Args, $Arg;
655 }
656}
657
Ted Kremenekdab11102008-04-02 04:43:42 +0000658sub RunBuildCommand {
659
660 my $Args = shift;
Ted Kremenek7442ca62008-04-02 16:04:51 +0000661 my $IgnoreErrors = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000662 my $Cmd = $Args->[0];
Ted Kremenek6195c372008-06-02 21:52:47 +0000663 my $CCAnalyzer = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000664
Ted Kremenek3301cb12008-06-30 18:18:16 +0000665 # Get only the part of the command after the last '/'.
666 if ($Cmd =~ /\/([^\/]+)$/) {
667 $Cmd = $1;
668 }
669
Ted Kremenek63c20172008-08-04 17:34:06 +0000670 if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc"
671 or $Cmd eq "ccc-analyzer") {
Ted Kremenekdab11102008-04-02 04:43:42 +0000672 shift @$Args;
Ted Kremenek6195c372008-06-02 21:52:47 +0000673 unshift @$Args, $CCAnalyzer;
Ted Kremenekdab11102008-04-02 04:43:42 +0000674 }
Ted Kremenek7442ca62008-04-02 16:04:51 +0000675 elsif ($IgnoreErrors) {
676 if ($Cmd eq "make" or $Cmd eq "gmake") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000677 AddIfNotPresent($Args,"-k");
Ted Kremenek8912b542008-05-13 21:28:02 +0000678 AddIfNotPresent($Args,"-i");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000679 }
680 elsif ($Cmd eq "xcodebuild") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000681 AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000682 }
Ted Kremenek6b628982008-04-30 23:47:12 +0000683 }
684
Ted Kremenek6b628982008-04-30 23:47:12 +0000685 if ($Cmd eq "xcodebuild") {
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000686 # Disable distributed builds for xcodebuild.
Ted Kremenek6b628982008-04-30 23:47:12 +0000687 AddIfNotPresent($Args,"-nodistribute");
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000688
689 # Disable PCH files until clang supports them.
690 AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO");
Ted Kremenek915e9722008-05-27 23:18:07 +0000691
692 # When 'CC' is set, xcodebuild uses it to do all linking, even if we are
693 # linking C++ object files. Set 'LDPLUSPLUS' so that xcodebuild uses 'g++'
694 # when linking such files.
Ted Kremenek95aa1052008-09-04 17:52:41 +0000695 die if (!defined $CXX);
696 my $LDPLUSPLUS = `which $CXX`;
Ted Kremenek915e9722008-05-27 23:18:07 +0000697 $LDPLUSPLUS =~ s/\015?\012//; # strip newlines
698 $ENV{'LDPLUSPLUS'} = $LDPLUSPLUS;
Ted Kremenek6b628982008-04-30 23:47:12 +0000699 }
Ted Kremenekdab11102008-04-02 04:43:42 +0000700
Ted Kremenek5a4ddaf2008-08-25 20:10:45 +0000701 return (system(@$Args) >> 8);
Ted Kremenekdab11102008-04-02 04:43:42 +0000702}
703
Ted Kremenekdab11102008-04-02 04:43:42 +0000704##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000705# DisplayHelp - Utility function to display all help options.
706##----------------------------------------------------------------------------##
707
Sam Bishopa0e22662008-04-02 03:35:43 +0000708sub DisplayHelp {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000709
Ted Kremenek5744dc22008-04-02 18:03:36 +0000710print <<ENDTEXT;
Sam Bishopa0e22662008-04-02 03:35:43 +0000711USAGE: $Prog [options] <build command> [build options]
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000712
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000713ENDTEXT
714
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000715 if (defined $BuildName) {
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000716 print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n";
717 }
718
719print <<ENDTEXT;
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000720OPTIONS:
721
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000722 -o - Target directory for HTML report files. Subdirectories
Sam Bishopa0e22662008-04-02 03:35:43 +0000723 will be created as needed to represent separate "runs" of
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000724 the analyzer. If this option is not specified, a directory
725 is created in /tmp to store the reports.
Ted Kremenek1262fc42008-05-14 20:10:33 +0000726
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000727 -h - Display this message.
728 --help
Ted Kremenek1262fc42008-05-14 20:10:33 +0000729
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000730 -k - Add a "keep on going" option to the specified build command.
731 --keep-going This option currently supports make and xcodebuild.
Ted Kremenekf02e8db2008-04-02 16:41:25 +0000732 This is a convenience option; one can specify this
733 behavior directly using build options.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000734
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000735 --status-bugs - By default, the exit status of $Prog is the same as the
736 executed build command. Specifying this option causes the
737 exit status of $Prog to be 1 if it found potential bugs
738 and 0 otherwise.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000739
Ted Kremenek386c6932008-09-03 17:59:35 +0000740 --use-cc [compiler path] - By default, $Prog uses 'gcc' to compile and link
741 --use-cc=[compiler path] your C and Objective-C code. Use this option
742 to specify an alternate compiler.
743
744 --use-c++ [compiler path] - By default, $Prog uses 'g++' to compile and link
745 --use-c++=[compiler path] your C++ and Objective-C++ code. Use this option
746 to specify an alternate compiler.
Ted Kremenekf17ef3c2008-08-21 21:47:09 +0000747
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000748 -v - Verbose output from $Prog and the analyzer.
Ted Kremenek386c6932008-09-03 17:59:35 +0000749 A second and third '-v' increases verbosity.
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000750
751 -V - View analysis results in a web browser when the build
752 --view completes.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000753
Ted Kremenekb7770c02008-07-15 17:06:13 +0000754
Ted Kremenek386c6932008-09-03 17:59:35 +0000755AVAILABLE ANALYSES (multiple analyses may be specified):
Ted Kremenekd52e4252008-08-25 20:45:07 +0000756
757ENDTEXT
Ted Kremenekb7770c02008-07-15 17:06:13 +0000758
759 foreach my $Analysis (sort keys %AvailableAnalyses) {
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000760 if (defined $AnalysesDefaultEnabled{$Analysis}) {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000761 print " (+)";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000762 }
763 else {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000764 print " ";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000765 }
766
767 print " $Analysis $AvailableAnalyses{$Analysis}\n";
768 }
769
770print <<ENDTEXT
771
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000772 NOTE: "(+)" indicates that an analysis is enabled by default unless one
773 or more analysis options are specified
Ted Kremenekb7770c02008-07-15 17:06:13 +0000774
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000775BUILD OPTIONS
776
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000777 You can specify any build option acceptable to the build command.
Ted Kremenek39eefde2008-04-02 16:47:27 +0000778
Ted Kremenek5744dc22008-04-02 18:03:36 +0000779EXAMPLE
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000780
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000781 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000782
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000783 The above example causes analysis reports to be deposited into
784 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
785 A different subdirectory is created each time $Prog analyzes a project.
786 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000787
788ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000789}
790
791##----------------------------------------------------------------------------##
792# Process command-line arguments.
793##----------------------------------------------------------------------------##
794
795my $HtmlDir; # Parent directory to store HTML files.
796my $IgnoreErrors = 0; # Ignore build errors.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000797my $ViewResults = 0; # View results when the build terminates.
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000798my $ExitStatusFoundBugs = 0; # Exit status reflects whether bugs were found
Ted Kremenekb7770c02008-07-15 17:06:13 +0000799my @AnalysesToRun;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000800
801if (!@ARGV) {
802 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000803 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000804}
805
806while (@ARGV) {
807
808 # Scan for options we recognize.
809
810 my $arg = $ARGV[0];
811
Sam Bishop2f2418e2008-04-03 14:29:47 +0000812 if ($arg eq "-h" or $arg eq "--help") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000813 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000814 exit 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000815 }
816
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000817 if (defined $AvailableAnalyses{$arg}) {
Ted Kremenek1262fc42008-05-14 20:10:33 +0000818 shift @ARGV;
Ted Kremenekb7770c02008-07-15 17:06:13 +0000819 push @AnalysesToRun, $arg;
Ted Kremenek1262fc42008-05-14 20:10:33 +0000820 next;
821 }
822
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000823 if ($arg eq "-o") {
824 shift @ARGV;
825
826 if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000827 DieDiag("'-o' option requires a target directory name.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000828 }
829
830 $HtmlDir = shift @ARGV;
831 next;
832 }
833
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000834 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000835 shift @ARGV;
836 $IgnoreErrors = 1;
837 next;
838 }
839
Ted Kremenekf17ef3c2008-08-21 21:47:09 +0000840 if ($arg =~ /^--use-cc(=(.+))?$/) {
841 shift @ARGV;
842 my $cc;
843
844 if ($2 eq "") {
845 if (!@ARGV) {
846 DieDiag("'--use-cc' option requires a compiler executable name.\n");
847 }
848 $cc = shift @ARGV;
849 }
850 else {
851 $cc = $2;
852 }
853
854 $ENV{"CCC_CC"} = $cc;
855 next;
856 }
857
Ted Kremenek386c6932008-09-03 17:59:35 +0000858 if ($arg =~ /^--use-c[+][+](=(.+))?$/) {
859 shift @ARGV;
860
861 if ($2 eq "") {
862 if (!@ARGV) {
863 DieDiag("'--use-c++' option requires a compiler executable name.\n");
864 }
865 $CXX = shift @ARGV;
866 }
867 else {
868 $CXX = $2;
869 }
870 next;
871 }
872
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000873 if ($arg eq "-v") {
874 shift @ARGV;
875 $Verbose++;
876 next;
877 }
878
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000879 if ($arg eq "-V" or $arg eq "--view") {
880 shift @ARGV;
881 $ViewResults = 1;
882 next;
883 }
884
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000885 if ($arg eq "--status-bugs") {
886 shift @ARGV;
887 $ExitStatusFoundBugs = 1;
888 next;
889 }
890
Ted Kremenek23cfca32008-06-16 22:40:14 +0000891 DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/);
Ted Kremenek0062ad42008-04-02 16:35:01 +0000892
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000893 last;
894}
895
896if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000897 Diag("No build command specified.\n\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000898 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000899 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000900}
901
Ted Kremenek386c6932008-09-03 17:59:35 +0000902
903
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000904# Determine the output directory for the HTML reports.
Ted Kremenek684bb092008-04-18 15:18:20 +0000905my $BaseDir = $HtmlDir;
Sam Bishopa0e22662008-04-02 03:35:43 +0000906$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000907
908# Set the appropriate environment variables.
Sam Bishopa0e22662008-04-02 03:35:43 +0000909SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000910
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000911my $Cmd = "$RealBin/ccc-analyzer";
912
Ted Kremenek23cfca32008-06-16 22:40:14 +0000913DieDiag("Executable 'ccc-analyzer' does not exist at '$Cmd'\n")
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000914 if (! -x $Cmd);
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000915
Ted Kremenekb7770c02008-07-15 17:06:13 +0000916if (! -x $ClangSB) {
917 Diag("'clang' executable not found in '$RealBin'.\n");
918 Diag("Using 'clang' from path.\n");
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000919}
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000920
Ted Kremenek95aa1052008-09-04 17:52:41 +0000921if (defined $CXX) {
922 $ENV{'CXX'} = $CXX;
923}
924else {
925 $CXX = 'g++'; # This variable is used by other parts of scan-build
926 # that need to know a default C++ compiler to fall back to.
927}
928
Ted Kremenek4f4b17d2008-04-03 20:08:18 +0000929$ENV{'CC'} = $Cmd;
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000930$ENV{'CLANG'} = $Clang;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000931
932if ($Verbose >= 2) {
933 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
934}
935
Ted Kremeneka9525c92008-05-12 22:07:14 +0000936if ($Verbose >= 3) {
937 $ENV{'CCC_ANALYZER_LOG'} = 1;
938}
939
Ted Kremenek90125992008-07-15 23:41:32 +0000940if (scalar(@AnalysesToRun) == 0) {
941 foreach my $key (keys %AnalysesDefaultEnabled) {
942 push @AnalysesToRun,$key;
943 }
Ted Kremenek01006782008-07-02 23:16:10 +0000944}
Ted Kremenek1262fc42008-05-14 20:10:33 +0000945
Ted Kremenek90125992008-07-15 23:41:32 +0000946$ENV{'CCC_ANALYZER_ANALYSIS'} = join ' ',@AnalysesToRun;
947
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000948# Run the build.
Ted Kremenek5656a982008-07-15 17:09:28 +0000949my $ExitStatus = RunBuildCommand(\@ARGV, $IgnoreErrors, $Cmd);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000950
951# Postprocess the HTML directory.
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000952my $NumBugs = Postprocess($HtmlDir, $BaseDir);
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000953
954if ($ViewResults and -r "$HtmlDir/index.html") {
955 # Only works on Mac OS X (for now).
956 print "Viewing analysis results: '$HtmlDir/index.html'\n";
Ted Kremenek20161e92008-07-15 20:18:21 +0000957 system("open", "$HtmlDir/index.html");
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000958}
Ted Kremenek5656a982008-07-15 17:09:28 +0000959
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000960if ($ExitStatusFoundBugs) {
961 exit 1 if ($NumBugs > 0);
962 exit 0;
963}
964
Ted Kremenek5656a982008-07-15 17:09:28 +0000965exit $ExitStatus;
966