blob: bef022ba3d2e0a0d298e970f903d7ac79a7a99ee [file] [log] [blame]
Ted Kremenekc9d8fde2008-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 Kremenekd5d43622008-04-02 20:43:36 +000017use FindBin qw($RealBin);
Ted Kremenek422b0ac2008-04-19 18:05:48 +000018use Digest::MD5;
Ted Kremenek32280862008-05-02 22:04:53 +000019use File::Basename;
Ted Kremenekc2007a92008-06-16 22:40:14 +000020use Term::ANSIColor;
21use Term::ANSIColor qw(:constants);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +000022
23my $Verbose = 0; # Verbose output from this script.
24my $Prog = "scan-build";
Ted Kremenekf54e8882008-05-23 18:17:05 +000025my $BuildName;
26my $BuildDate;
Ted Kremenek39490aa2008-09-04 17:52:41 +000027my $CXX; # Leave undefined initially.
Ted Kremenekc9d8fde2008-04-01 20:47:38 +000028
Ted Kremenek2a28c7b2008-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 Kremenekc2007a92008-06-16 22:40:14 +000032
Ted Kremenekadcf72b2008-07-15 17:06:13 +000033##----------------------------------------------------------------------------##
34# Diagnostics
35##----------------------------------------------------------------------------##
36
Ted Kremenekc2007a92008-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 Kremenek61c656b2008-08-08 20:46:42 +000047sub DiagCrashes {
48 my $Dir = shift;
49 Diag ("The analyzer crashed on some source files.\n");
Ted Kremenek66e470b2008-09-03 17:59:35 +000050 Diag ("Preprocessed versions of crashed files were deposited in '$Dir/crashes'.\n");
Ted Kremenek61c656b2008-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 Kremenekc2007a92008-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 Kremenekc9d8fde2008-04-01 20:47:38 +000067##----------------------------------------------------------------------------##
Ted Kremenekadcf72b2008-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 Kremenek559e37e2008-08-04 17:34:06 +000081open(PIPE, "-|", $Clang, "--help") or
Ted Kremenekadcf72b2008-07-15 17:06:13 +000082 DieDiag("Cannot execute '$Clang'");
Ted Kremenek559e37e2008-08-04 17:34:06 +000083
Ted Kremenekadcf72b2008-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 Kremenek61c656b2008-08-08 20:46:42 +000091
Ted Kremenekadcf72b2008-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 Kremenek47219912008-07-15 23:41:32 +0000111 '-warn-objc-methodsigs' => 1,
Ted Kremenek6a985062008-07-25 20:35:01 +0000112 '-warn-objc-missing-dealloc' => 1,
113 '-warn-objc-unused-ivars' => 1
Ted Kremenekadcf72b2008-07-15 17:06:13 +0000114);
115
116##----------------------------------------------------------------------------##
Ted Kremenek9cfd0502008-08-04 18:15:26 +0000117# GetHTMLRunDir - Construct an HTML directory name for the current sub-run.
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000118##----------------------------------------------------------------------------##
119
Sam Bishop479982e2008-04-02 03:35:43 +0000120sub GetHTMLRunDir {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000121
Ted Kremenek9cfd0502008-08-04 18:15:26 +0000122 die "Not enough arguments." if (@_ == 0);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000123 my $Dir = shift @_;
Ted Kremenek9cfd0502008-08-04 18:15:26 +0000124
125 my $TmpMode = 0;
126 if (!defined $Dir) {
127 $Dir = "/tmp";
128 $TmpMode = 1;
129 }
130
Ted Kremenekc9d8fde2008-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 Kremenek573d80b2008-05-14 17:23:56 +0000139 my $DateString = sprintf("%d-%02d-%02d", $year, $month, $day);
Ted Kremenekc9d8fde2008-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 Kremenekc2007a92008-06-16 22:40:14 +0000148 DieDiag("directory '$Dir' exists but is not readable.\n");
Ted Kremenekc9d8fde2008-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 Kremenek118be0b2008-08-07 17:57:34 +0000156 my @FILES = grep { -d "$Dir/$_" } readdir(DIR);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000157 closedir(DIR);
158
159 foreach my $f (@FILES) {
160
Ted Kremenek9cfd0502008-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 Kremenekc9d8fde2008-04-01 20:47:38 +0000167 my @x = split/-/, $f;
Ted Kremenek9cfd0502008-08-04 18:15:26 +0000168
Ted Kremenekc9d8fde2008-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 Kremenekc2007a92008-06-16 22:40:14 +0000184 DieDiag("'$Dir' exists but is not a directory.\n");
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000185 }
Ted Kremenek9cfd0502008-08-04 18:15:26 +0000186
187 if ($TmpMode) {
188 DieDiag("The directory '/tmp' does not exist or cannot be accessed.");
189 }
190
Ted Kremenekc9d8fde2008-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 Kremenek9cfd0502008-08-04 18:15:26 +0000193
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000194 $RunNumber = 1;
195 }
196
Ted Kremenek9cfd0502008-08-04 18:15:26 +0000197 die "RunNumber must be defined!" if (!defined $RunNumber);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000198
199 # Append the run number.
Ted Kremenek8e7207f2008-09-04 23:56:36 +0000200 my $NewDir;
Ted Kremenek9cfd0502008-08-04 18:15:26 +0000201 if ($TmpMode) {
Ted Kremenek8e7207f2008-09-04 23:56:36 +0000202 $NewDir = "$Dir/$Prog-$DateString-$RunNumber";
Ted Kremenek9cfd0502008-08-04 18:15:26 +0000203 }
204 else {
Ted Kremenek8e7207f2008-09-04 23:56:36 +0000205 $NewDir = "$Dir/$DateString-$RunNumber";
Ted Kremenek9cfd0502008-08-04 18:15:26 +0000206 }
Ted Kremenek8e7207f2008-09-04 23:56:36 +0000207 system 'mkdir','-p',$NewDir;
208 return $NewDir;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000209}
210
Sam Bishop479982e2008-04-02 03:35:43 +0000211sub SetHtmlEnv {
Ted Kremenekc9d8fde2008-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 Kremenekc2007a92008-06-16 22:40:14 +0000227 Diag("Emitting reports for this run to '$Dir'.\n");
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000228 }
229
230 $ENV{'CCC_ANALYZER_HTML'} = $Dir;
231}
232
233##----------------------------------------------------------------------------##
Ted Kremenekf0a08a52008-04-18 15:09:30 +0000234# ComputeDigest - Compute a digest of the specified file.
235##----------------------------------------------------------------------------##
236
237sub ComputeDigest {
238 my $FName = shift;
Ted Kremenekc2007a92008-06-16 22:40:14 +0000239 DieDiag("Cannot read $FName to compute Digest.\n") if (! -r $FName);
Ted Kremenek422b0ac2008-04-19 18:05:48 +0000240
241 # Use Digest::MD5. We don't have to be cryptographically secure. We're
Ted Kremenek7c400a02008-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 Kremenek559e37e2008-08-04 17:34:06 +0000244 # come bundled on most systems.
Ted Kremenekc2007a92008-06-16 22:40:14 +0000245 open(FILE, $FName) or DieDiag("Cannot open $FName when computing Digest.\n");
Ted Kremenek422b0ac2008-04-19 18:05:48 +0000246 binmode FILE;
247 my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest;
248 close(FILE);
249
Ted Kremenek559e37e2008-08-04 17:34:06 +0000250 # Return the digest.
Ted Kremenek422b0ac2008-04-19 18:05:48 +0000251 return $Result;
Ted Kremenekf0a08a52008-04-18 15:09:30 +0000252}
253
254##----------------------------------------------------------------------------##
Ted Kremenek32280862008-05-02 22:04:53 +0000255# UpdatePrefix - Compute the common prefix of files.
256##----------------------------------------------------------------------------##
257
258my $Prefix;
259
260sub UpdatePrefix {
Ted Kremenek32280862008-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 Kremenek32280862008-05-02 22:04:53 +0000266 return if ( $x =~ /^\/usr/ or $x =~ /^\/Library/
267 or $x =~ /^\/System/ or $x =~ /^\/Developer/);
268
Ted Kremenek32280862008-05-02 22:04:53 +0000269 if (!defined $Prefix) {
270 $Prefix = $x;
271 return;
272 }
273
Ted Kremenekb0354f92008-09-11 21:15:10 +0000274 chop $Prefix while (!($x =~ /^\Q$Prefix/));
Ted Kremenek32280862008-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 Kremenek559e37e2008-08-04 17:34:06 +0000289
Ted Kremenek32280862008-05-02 22:04:53 +0000290 open (RIN, $fname) or die "cannot open $fname";
Ted Kremenek559e37e2008-08-04 17:34:06 +0000291 open (ROUT, ">", "$fname.tmp") or die "cannot open $fname.tmp";
292
Ted Kremenek32280862008-05-02 22:04:53 +0000293 while (<RIN>) {
294 s/$regex/$newtext/;
295 print ROUT $_;
296 }
Ted Kremenek559e37e2008-08-04 17:34:06 +0000297
Ted Kremenek32280862008-05-02 22:04:53 +0000298 close (ROUT);
299 close (RIN);
Ted Kremenekbd5e3282008-07-15 20:18:21 +0000300 system("mv", "$fname.tmp", $fname);
Ted Kremenek32280862008-05-02 22:04:53 +0000301}
302
303##----------------------------------------------------------------------------##
Ted Kremeneked757e02008-04-02 18:03:36 +0000304# ScanFile - Scan a report file for various identifying attributes.
305##----------------------------------------------------------------------------##
306
Ted Kremenekf0a08a52008-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 Kremeneked757e02008-04-02 18:03:36 +0000312sub ScanFile {
313
314 my $Index = shift;
315 my $Dir = shift;
316 my $FName = shift;
317
Ted Kremenekf0a08a52008-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 Kremenek9cfd0502008-08-04 18:15:26 +0000323 if (defined $AlreadyScanned{$digest}) {
Ted Kremenekf0a08a52008-04-18 15:09:30 +0000324 # Redundant file. Remove it.
Ted Kremenekbd5e3282008-07-15 20:18:21 +0000325 system ("rm", "-f", "$Dir/$FName");
Ted Kremenekf0a08a52008-04-18 15:09:30 +0000326 return;
327 }
328
329 $AlreadyScanned{$digest} = 1;
330
Ted Kremenekebd0bb72008-04-18 16:58:34 +0000331 # At this point the report file is not world readable. Make it happen.
Ted Kremenekbd5e3282008-07-15 20:18:21 +0000332 system ("chmod", "644", "$Dir/$FName");
Ted Kremenekc5b38202008-04-18 15:18:20 +0000333
334 # Scan the report file for tags.
Ted Kremenekc2007a92008-06-16 22:40:14 +0000335 open(IN, "$Dir/$FName") or DieDiag("Cannot open '$Dir/$FName'\n");
Ted Kremeneked757e02008-04-02 18:03:36 +0000336
337 my $BugDesc = "";
Ted Kremenekd5d43622008-04-02 20:43:36 +0000338 my $BugFile = "";
339 my $BugPathLength = 1;
340 my $BugLine = 0;
Ted Kremeneked757e02008-04-02 18:03:36 +0000341
342 while (<IN>) {
343
344 if (/<!-- BUGDESC (.*) -->$/) {
345 $BugDesc = $1;
Ted Kremeneked757e02008-04-02 18:03:36 +0000346 }
Ted Kremenekd5d43622008-04-02 20:43:36 +0000347 elsif (/<!-- BUGFILE (.*) -->$/) {
348 $BugFile = $1;
Ted Kremenek32280862008-05-02 22:04:53 +0000349 UpdatePrefix($BugFile);
Ted Kremenekd5d43622008-04-02 20:43:36 +0000350 }
351 elsif (/<!-- BUGPATHLENGTH (.*) -->$/) {
352 $BugPathLength = $1;
353 }
354 elsif (/<!-- BUGLINE (.*) -->$/) {
355 $BugLine = $1;
356 }
Ted Kremeneked757e02008-04-02 18:03:36 +0000357 }
358
359 close(IN);
360
Ted Kremenekd5d43622008-04-02 20:43:36 +0000361 push @$Index,[ $FName, $BugDesc, $BugFile, $BugLine, $BugPathLength ];
362}
363
364##----------------------------------------------------------------------------##
365# CopyJS - Copy JavaScript code to target directory.
366##----------------------------------------------------------------------------##
367
368sub CopyJS {
369
370 my $Dir = shift;
371
Ted Kremenekc2007a92008-06-16 22:40:14 +0000372 DieDiag("Cannot find 'sorttable.js'.\n")
Ted Kremenekd5d43622008-04-02 20:43:36 +0000373 if (! -r "$RealBin/sorttable.js");
374
Ted Kremenekbd5e3282008-07-15 20:18:21 +0000375 system ("cp", "$RealBin/sorttable.js", "$Dir");
Ted Kremenekd5d43622008-04-02 20:43:36 +0000376
Ted Kremenekc2007a92008-06-16 22:40:14 +0000377 DieDiag("Could not copy 'sorttable.js' to '$Dir'.\n")
Ted Kremenekd5d43622008-04-02 20:43:36 +0000378 if (! -r "$Dir/sorttable.js");
Ted Kremeneked757e02008-04-02 18:03:36 +0000379}
380
381##----------------------------------------------------------------------------##
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000382# Postprocess - Postprocess the results of an analysis scan.
383##----------------------------------------------------------------------------##
384
Sam Bishop479982e2008-04-02 03:35:43 +0000385sub Postprocess {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000386
387 my $Dir = shift;
Ted Kremenekc5b38202008-04-18 15:18:20 +0000388 my $BaseDir = shift;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000389
Ted Kremenek9cfd0502008-08-04 18:15:26 +0000390 die "No directory specified." if (!defined $Dir);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000391
392 if (! -d $Dir) {
Ted Kremenekc2007a92008-06-16 22:40:14 +0000393 Diag("No bugs found.\n");
Ted Kremenekfea39522008-07-15 22:03:09 +0000394 return 0;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000395 }
396
397 opendir(DIR, $Dir);
Ted Kremenek61c656b2008-08-08 20:46:42 +0000398 my $Crashes = 0;
399 my @files = grep { if ($_ eq "crashes") { $Crashes++; }
400 /^report-.*\.html$/; } readdir(DIR);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000401 closedir(DIR);
402
Ted Kremenek61c656b2008-08-08 20:46:42 +0000403 if (scalar(@files) == 0 and $Crashes == 0) {
Ted Kremenekc2007a92008-06-16 22:40:14 +0000404 Diag("Removing directory '$Dir' because it contains no reports.\n");
Ted Kremenekbd5e3282008-07-15 20:18:21 +0000405 system ("rm", "-fR", $Dir);
Ted Kremenekfea39522008-07-15 22:03:09 +0000406 return 0;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000407 }
Ted Kremeneked757e02008-04-02 18:03:36 +0000408
Ted Kremenek61c656b2008-08-08 20:46:42 +0000409 # Scan each report file and build an index.
410 my @Index;
Ted Kremeneked757e02008-04-02 18:03:36 +0000411 foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); }
412
Ted Kremenekcdb2ae02008-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 Kremenek559e37e2008-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 Kremeneked757e02008-04-02 18:03:36 +0000430
Ted Kremenek4de0c562008-04-15 20:47:02 +0000431 # Print out the header.
432
Ted Kremeneked757e02008-04-02 18:03:36 +0000433print OUT <<ENDTEXT;
434<html>
435<head>
Ted Kremenekb59b7542008-04-02 18:42:49 +0000436<style type="text/css">
437 body { color:#000000; background-color:#ffffff }
Ted Kremenekd5d43622008-04-02 20:43:36 +0000438 body { font-family: Helvetica, sans-serif; font-size:9pt }
Ted Kremenekb59b7542008-04-02 18:42:49 +0000439 h1 { font-size:12pt }
Ted Kremenek61c656b2008-08-08 20:46:42 +0000440 table thead {
Ted Kremenekd5d43622008-04-02 20:43:36 +0000441 background-color:#eee; color:#666666;
442 font-weight: bold; cursor: default;
Ted Kremenek38471832008-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 Kremenek61c656b2008-08-08 20:46:42 +0000448 table { border: 1px #000000 solid }
449 table { border-collapse: collapse; border-spacing: 0px }
Ted Kremenekb59b7542008-04-02 18:42:49 +0000450 td { border-bottom: 1px #000000 dotted }
Ted Kremenekd5d43622008-04-02 20:43:36 +0000451 td { padding:5px; padding-left:8px; padding-right:8px }
Ted Kremenek756515f2008-04-07 23:50:07 +0000452 td { text-align:left; font-size:9pt }
Ted Kremenekd5d43622008-04-02 20:43:36 +0000453 td.View { padding-left: 10px }
Ted Kremenekb59b7542008-04-02 18:42:49 +0000454</style>
Ted Kremenekd5d43622008-04-02 20:43:36 +0000455<script src="sorttable.js"></script>
Ted Kremenek4de0c562008-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 Kremenek4de0c562008-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 Kremenek61c656b2008-08-08 20:46:42 +0000480 if (scalar(@files)) {
481 # Print out the summary table.
482 my %Totals;
Ted Kremenek4de0c562008-04-15 20:47:02 +0000483
Ted Kremenek61c656b2008-08-08 20:46:42 +0000484 for my $row ( @Index ) {
485 #my $bug_type = lc($row->[1]);
486 my $bug_type = ($row->[1]);
Ted Kremenek4de0c562008-04-15 20:47:02 +0000487
Ted Kremenek61c656b2008-08-08 20:46:42 +0000488 if (!defined $Totals{$bug_type}) { $Totals{$bug_type} = 1; }
489 else { $Totals{$bug_type}++; }
Ted Kremenek4de0c562008-04-15 20:47:02 +0000490 }
Ted Kremenek61c656b2008-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 Kremenek4de0c562008-04-15 20:47:02 +0000496 }
Ted Kremenekf54e8882008-05-23 18:17:05 +0000497
Ted Kremenek4de0c562008-04-15 20:47:02 +0000498print OUT <<ENDTEXT;
Ted Kremenek4de0c562008-04-15 20:47:02 +0000499<table class="sortable">
500<tr>
501 <td>Bug Type</td>
502 <td>Quantity</td>
Ted Kremenek3e2abdc2008-07-07 16:58:44 +0000503 <td class="sorttable_nosort">Display?</td>
Ted Kremenek4de0c562008-04-15 20:47:02 +0000504</tr>
505ENDTEXT
506
Ted Kremenek61c656b2008-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 Kremenek4de0c562008-04-15 20:47:02 +0000512
513 # Print out the table of errors.
514
515print OUT <<ENDTEXT;
516</table>
517<h3>Reports</h3>
Ted Kremenekd5d43622008-04-02 20:43:36 +0000518<table class="sortable">
Ted Kremenekb59b7542008-04-02 18:42:49 +0000519<tr>
Ted Kremeneke51290d2008-07-07 17:23:32 +0000520 <td class="sorttable_sorted">Bug Type<span id="sorttable_sortfwdind">&nbsp;&#x25BE;</span>
Ted Kremenek38471832008-04-03 05:50:51 +0000521 <td>File</td>
522 <td>Line</td>
523 <td>Path Length</td>
Ted Kremenek3e2abdc2008-07-07 16:58:44 +0000524 <td class="sorttable_nosort"></td>
Ted Kremenekb59b7542008-04-02 18:42:49 +0000525</tr>
Ted Kremeneked757e02008-04-02 18:03:36 +0000526ENDTEXT
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000527
Ted Kremenek61c656b2008-08-08 20:46:42 +0000528 my $prefix = GetPrefix();
529 my $regex;
530 my $InFileRegex;
531 my $InFilePrefix = "File:</td><td>";
Ted Kremenek32280862008-05-02 22:04:53 +0000532
Ted Kremenek61c656b2008-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 Kremenek32280862008-05-02 22:04:53 +0000537
Ted Kremenek61c656b2008-08-08 20:46:42 +0000538 for my $row ( sort { $a->[1] cmp $b->[1] } @Index ) {
Ted Kremeneked757e02008-04-02 18:03:36 +0000539
Ted Kremenek61c656b2008-08-08 20:46:42 +0000540 my $x = lc($row->[1]);
541 $x =~ s/[ ,'"]+/_/g;
Ted Kremenek4de0c562008-04-15 20:47:02 +0000542
Ted Kremenek61c656b2008-08-08 20:46:42 +0000543 print OUT "<tr class=\"bt_$x\">\n";
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000544
Ted Kremenek61c656b2008-08-08 20:46:42 +0000545 my $ReportFile = $row->[0];
Ted Kremeneked757e02008-04-02 18:03:36 +0000546
Ted Kremenek61c656b2008-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 Kremeneked757e02008-04-02 18:03:36 +0000551
Ted Kremenek61c656b2008-08-08 20:46:42 +0000552 # Update the file prefix.
Ted Kremenek32280862008-05-02 22:04:53 +0000553
Ted Kremenek61c656b2008-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 Kremenek80897002008-05-02 23:40:49 +0000559
Ted Kremenek61c656b2008-08-08 20:46:42 +0000560 print OUT "<td>$fname</td>\n";
Ted Kremenek32280862008-05-02 22:04:53 +0000561
Ted Kremenek61c656b2008-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 Kremenekb59b7542008-04-02 18:42:49 +0000566
Ted Kremenek61c656b2008-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 Kremeneka5a7da82008-07-30 17:58:08 +0000569
Ted Kremenek61c656b2008-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 Kremenekcd7c9202008-08-18 18:38:29 +0000585<h3>Analyzer Failures</h3>
Ted Kremenek61c656b2008-08-08 20:46:42 +0000586
Ted Kremenekcd7c9202008-08-18 18:38:29 +0000587<p>The analyzer had problems processing the following files:</p>
Ted Kremenek61c656b2008-08-08 20:46:42 +0000588
589<table>
Ted Kremenek4622abf2008-09-12 22:49:36 +0000590<thead><tr><td>Problem</td><td>Source File</td><td>Preprocessed File</td><td>STDERR Output</td></tr></thead>
Ted Kremenek61c656b2008-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 Kremenekcd7c9202008-08-18 18:38:29 +0000601 chomp $srcfile;
602 my $problem = <INFO>;
603 chomp $problem;
Ted Kremenek61c656b2008-08-08 20:46:42 +0000604 close (INFO);
605 # Print the information in the table.
Ted Kremenekcdb2ae02008-08-25 20:45:07 +0000606 my $prefix = GetPrefix();
Ted Kremenek4622abf2008-09-12 22:49:36 +0000607 if (defined $prefix) { $srcfile =~ s/^\Q$prefix//; }
608 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 Kremenek61c656b2008-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 Kremeneked757e02008-04-02 18:03:36 +0000616 }
617
Ted Kremenek61c656b2008-08-08 20:46:42 +0000618 print OUT "</body></html>\n";
Ted Kremeneked757e02008-04-02 18:03:36 +0000619 close(OUT);
Ted Kremenekd5d43622008-04-02 20:43:36 +0000620 CopyJS($Dir);
Ted Kremenekbd5e3282008-07-15 20:18:21 +0000621
622 # Make sure $Dir and $BaseDir are world readable/executable.
623 system("chmod", "755", $Dir);
Ted Kremenek9cfd0502008-08-04 18:15:26 +0000624 if (defined $BaseDir) { system("chmod", "755", $BaseDir); }
Ted Kremenekbd5e3282008-07-15 20:18:21 +0000625
Ted Kremenekc2007a92008-06-16 22:40:14 +0000626 my $Num = scalar(@Index);
Ted Kremenek49226ac2008-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 Kremenekfea39522008-07-15 22:03:09 +0000631
Ted Kremenek61c656b2008-08-08 20:46:42 +0000632 DiagCrashes($Dir) if ($Crashes);
633
Ted Kremenekfea39522008-07-15 22:03:09 +0000634 return $Num;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000635}
636
637##----------------------------------------------------------------------------##
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000638# RunBuildCommand - Run the build command.
639##----------------------------------------------------------------------------##
640
Ted Kremenekbcdd3312008-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 Kremenekd6c03a82008-04-02 04:43:42 +0000658sub RunBuildCommand {
659
660 my $Args = shift;
Ted Kremenek091f9372008-04-02 16:04:51 +0000661 my $IgnoreErrors = shift;
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000662 my $Cmd = $Args->[0];
Ted Kremenek0caa57d2008-06-02 21:52:47 +0000663 my $CCAnalyzer = shift;
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000664
Ted Kremenek900b0522008-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 Kremenek559e37e2008-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 Kremenekd6c03a82008-04-02 04:43:42 +0000672 shift @$Args;
Ted Kremenek0caa57d2008-06-02 21:52:47 +0000673 unshift @$Args, $CCAnalyzer;
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000674 }
Ted Kremenek091f9372008-04-02 16:04:51 +0000675 elsif ($IgnoreErrors) {
676 if ($Cmd eq "make" or $Cmd eq "gmake") {
Ted Kremenekbcdd3312008-04-30 23:47:12 +0000677 AddIfNotPresent($Args,"-k");
Ted Kremenek135ef8d2008-05-13 21:28:02 +0000678 AddIfNotPresent($Args,"-i");
Ted Kremenek091f9372008-04-02 16:04:51 +0000679 }
680 elsif ($Cmd eq "xcodebuild") {
Ted Kremenekbcdd3312008-04-30 23:47:12 +0000681 AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES");
Ted Kremenek091f9372008-04-02 16:04:51 +0000682 }
Ted Kremenekbcdd3312008-04-30 23:47:12 +0000683 }
684
Ted Kremenekbcdd3312008-04-30 23:47:12 +0000685 if ($Cmd eq "xcodebuild") {
Ted Kremenek23df1d22008-05-23 22:18:16 +0000686 # Disable distributed builds for xcodebuild.
Ted Kremenekbcdd3312008-04-30 23:47:12 +0000687 AddIfNotPresent($Args,"-nodistribute");
Ted Kremenek23df1d22008-05-23 22:18:16 +0000688
689 # Disable PCH files until clang supports them.
690 AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO");
Ted Kremenekaa0d3722008-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 Kremenek39490aa2008-09-04 17:52:41 +0000695 die if (!defined $CXX);
696 my $LDPLUSPLUS = `which $CXX`;
Ted Kremenekaa0d3722008-05-27 23:18:07 +0000697 $LDPLUSPLUS =~ s/\015?\012//; # strip newlines
698 $ENV{'LDPLUSPLUS'} = $LDPLUSPLUS;
Ted Kremenekbcdd3312008-04-30 23:47:12 +0000699 }
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000700
Ted Kremenek8094b412008-08-25 20:10:45 +0000701 return (system(@$Args) >> 8);
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000702}
703
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000704##----------------------------------------------------------------------------##
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000705# DisplayHelp - Utility function to display all help options.
706##----------------------------------------------------------------------------##
707
Sam Bishop479982e2008-04-02 03:35:43 +0000708sub DisplayHelp {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000709
Ted Kremeneked757e02008-04-02 18:03:36 +0000710print <<ENDTEXT;
Sam Bishop479982e2008-04-02 03:35:43 +0000711USAGE: $Prog [options] <build command> [build options]
Ted Kremenekb9177df2008-04-01 21:22:03 +0000712
Ted Kremenekf54e8882008-05-23 18:17:05 +0000713ENDTEXT
714
Ted Kremenek9cfd0502008-08-04 18:15:26 +0000715 if (defined $BuildName) {
Ted Kremenekf54e8882008-05-23 18:17:05 +0000716 print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n";
717 }
718
719print <<ENDTEXT;
Ted Kremenekb9177df2008-04-01 21:22:03 +0000720OPTIONS:
721
Ted Kremenekfea39522008-07-15 22:03:09 +0000722 -o - Target directory for HTML report files. Subdirectories
Sam Bishop479982e2008-04-02 03:35:43 +0000723 will be created as needed to represent separate "runs" of
Ted Kremenekb9177df2008-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 Kremenek5979b082008-05-14 20:10:33 +0000726
Ted Kremenekfea39522008-07-15 22:03:09 +0000727 -h - Display this message.
728 --help
Ted Kremenek5979b082008-05-14 20:10:33 +0000729
Ted Kremenekfea39522008-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 Kremenek707a9ad2008-04-02 16:41:25 +0000732 This is a convenience option; one can specify this
733 behavior directly using build options.
Ted Kremenekb9177df2008-04-01 21:22:03 +0000734
Ted Kremenekfea39522008-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 Kremenekb9177df2008-04-01 21:22:03 +0000739
Ted Kremenek66e470b2008-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 Kremenek91ec36e2008-08-21 21:47:09 +0000747
Ted Kremenekfea39522008-07-15 22:03:09 +0000748 -v - Verbose output from $Prog and the analyzer.
Ted Kremenek66e470b2008-09-03 17:59:35 +0000749 A second and third '-v' increases verbosity.
Ted Kremenekfea39522008-07-15 22:03:09 +0000750
751 -V - View analysis results in a web browser when the build
752 --view completes.
Ted Kremenekb59b7542008-04-02 18:42:49 +0000753
Ted Kremenekadcf72b2008-07-15 17:06:13 +0000754
Ted Kremenek66e470b2008-09-03 17:59:35 +0000755AVAILABLE ANALYSES (multiple analyses may be specified):
Ted Kremenekcdb2ae02008-08-25 20:45:07 +0000756
757ENDTEXT
Ted Kremenekadcf72b2008-07-15 17:06:13 +0000758
759 foreach my $Analysis (sort keys %AvailableAnalyses) {
Ted Kremenek9cfd0502008-08-04 18:15:26 +0000760 if (defined $AnalysesDefaultEnabled{$Analysis}) {
Ted Kremenekfea39522008-07-15 22:03:09 +0000761 print " (+)";
Ted Kremenekadcf72b2008-07-15 17:06:13 +0000762 }
763 else {
Ted Kremenekfea39522008-07-15 22:03:09 +0000764 print " ";
Ted Kremenekadcf72b2008-07-15 17:06:13 +0000765 }
766
767 print " $Analysis $AvailableAnalyses{$Analysis}\n";
768 }
769
770print <<ENDTEXT
771
Ted Kremenekfea39522008-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 Kremenekadcf72b2008-07-15 17:06:13 +0000774
Ted Kremenekb9177df2008-04-01 21:22:03 +0000775BUILD OPTIONS
776
Ted Kremenekfea39522008-07-15 22:03:09 +0000777 You can specify any build option acceptable to the build command.
Ted Kremenekf76812d2008-04-02 16:47:27 +0000778
Ted Kremeneked757e02008-04-02 18:03:36 +0000779EXAMPLE
Ted Kremenekb9177df2008-04-01 21:22:03 +0000780
Ted Kremenekfea39522008-07-15 22:03:09 +0000781 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenekb9177df2008-04-01 21:22:03 +0000782
Ted Kremenekfea39522008-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 Kremenekb9177df2008-04-01 21:22:03 +0000787
788ENDTEXT
Ted Kremenekc9d8fde2008-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 Kremenekb59b7542008-04-02 18:42:49 +0000797my $ViewResults = 0; # View results when the build terminates.
Ted Kremenekfea39522008-07-15 22:03:09 +0000798my $ExitStatusFoundBugs = 0; # Exit status reflects whether bugs were found
Ted Kremenekadcf72b2008-07-15 17:06:13 +0000799my @AnalysesToRun;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000800
801if (!@ARGV) {
802 DisplayHelp();
Sam Bishop479982e2008-04-02 03:35:43 +0000803 exit 1;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000804}
805
806while (@ARGV) {
807
808 # Scan for options we recognize.
809
810 my $arg = $ARGV[0];
811
Sam Bishopa3281512008-04-03 14:29:47 +0000812 if ($arg eq "-h" or $arg eq "--help") {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000813 DisplayHelp();
Sam Bishop479982e2008-04-02 03:35:43 +0000814 exit 0;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000815 }
816
Ted Kremenek9cfd0502008-08-04 18:15:26 +0000817 if (defined $AvailableAnalyses{$arg}) {
Ted Kremenek5979b082008-05-14 20:10:33 +0000818 shift @ARGV;
Ted Kremenekadcf72b2008-07-15 17:06:13 +0000819 push @AnalysesToRun, $arg;
Ted Kremenek5979b082008-05-14 20:10:33 +0000820 next;
821 }
822
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000823 if ($arg eq "-o") {
824 shift @ARGV;
825
826 if (!@ARGV) {
Ted Kremenekc2007a92008-06-16 22:40:14 +0000827 DieDiag("'-o' option requires a target directory name.\n");
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000828 }
829
830 $HtmlDir = shift @ARGV;
831 next;
832 }
833
Ted Kremenekb9177df2008-04-01 21:22:03 +0000834 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000835 shift @ARGV;
836 $IgnoreErrors = 1;
837 next;
838 }
839
Ted Kremenek91ec36e2008-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 Kremenek66e470b2008-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 Kremenekc9d8fde2008-04-01 20:47:38 +0000873 if ($arg eq "-v") {
874 shift @ARGV;
875 $Verbose++;
876 next;
877 }
878
Ted Kremenekb59b7542008-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 Kremenekfea39522008-07-15 22:03:09 +0000885 if ($arg eq "--status-bugs") {
886 shift @ARGV;
887 $ExitStatusFoundBugs = 1;
888 next;
889 }
890
Ted Kremenekc2007a92008-06-16 22:40:14 +0000891 DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/);
Ted Kremenek78965de2008-04-02 16:35:01 +0000892
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000893 last;
894}
895
896if (!@ARGV) {
Ted Kremenekc2007a92008-06-16 22:40:14 +0000897 Diag("No build command specified.\n\n");
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000898 DisplayHelp();
Sam Bishop479982e2008-04-02 03:35:43 +0000899 exit 1;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000900}
901
Ted Kremenek66e470b2008-09-03 17:59:35 +0000902
903
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000904# Determine the output directory for the HTML reports.
Ted Kremenekc5b38202008-04-18 15:18:20 +0000905my $BaseDir = $HtmlDir;
Sam Bishop479982e2008-04-02 03:35:43 +0000906$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000907
908# Set the appropriate environment variables.
Sam Bishop479982e2008-04-02 03:35:43 +0000909SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000910
Ted Kremenekfc0f1c22008-04-08 20:22:12 +0000911my $Cmd = "$RealBin/ccc-analyzer";
912
Ted Kremenekc2007a92008-06-16 22:40:14 +0000913DieDiag("Executable 'ccc-analyzer' does not exist at '$Cmd'\n")
Ted Kremenekfc0f1c22008-04-08 20:22:12 +0000914 if (! -x $Cmd);
Ted Kremenek056171b2008-04-18 22:00:56 +0000915
Ted Kremenekadcf72b2008-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 Kremenek056171b2008-04-18 22:00:56 +0000919}
Ted Kremenekfc0f1c22008-04-08 20:22:12 +0000920
Ted Kremenek39490aa2008-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 Kremenek47bf3892008-04-03 20:08:18 +0000929$ENV{'CC'} = $Cmd;
Ted Kremenek056171b2008-04-18 22:00:56 +0000930$ENV{'CLANG'} = $Clang;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000931
932if ($Verbose >= 2) {
933 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
934}
935
Ted Kremenek62e737d2008-05-12 22:07:14 +0000936if ($Verbose >= 3) {
937 $ENV{'CCC_ANALYZER_LOG'} = 1;
938}
939
Ted Kremenek47219912008-07-15 23:41:32 +0000940if (scalar(@AnalysesToRun) == 0) {
941 foreach my $key (keys %AnalysesDefaultEnabled) {
942 push @AnalysesToRun,$key;
943 }
Ted Kremenek70633652008-07-02 23:16:10 +0000944}
Ted Kremenek5979b082008-05-14 20:10:33 +0000945
Ted Kremenek47219912008-07-15 23:41:32 +0000946$ENV{'CCC_ANALYZER_ANALYSIS'} = join ' ',@AnalysesToRun;
947
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000948# Run the build.
Ted Kremenek46035572008-07-15 17:09:28 +0000949my $ExitStatus = RunBuildCommand(\@ARGV, $IgnoreErrors, $Cmd);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000950
951# Postprocess the HTML directory.
Ted Kremenekfea39522008-07-15 22:03:09 +0000952my $NumBugs = Postprocess($HtmlDir, $BaseDir);
Ted Kremenekb59b7542008-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 Kremenekbd5e3282008-07-15 20:18:21 +0000957 system("open", "$HtmlDir/index.html");
Ted Kremenekb59b7542008-04-02 18:42:49 +0000958}
Ted Kremenek46035572008-07-15 17:09:28 +0000959
Ted Kremenekfea39522008-07-15 22:03:09 +0000960if ($ExitStatusFoundBugs) {
961 exit 1 if ($NumBugs > 0);
962 exit 0;
963}
964
Ted Kremenek46035572008-07-15 17:09:28 +0000965exit $ExitStatus;
966