blob: 908795e281a2c00f205eefbfffde12b7af3e1f97 [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 Kremenekfc0898a2008-09-04 23:56:36 +0000199 my $NewDir;
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000200 if ($TmpMode) {
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000201 $NewDir = "$Dir/$Prog-$DateString-$RunNumber";
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000202 }
203 else {
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000204 $NewDir = "$Dir/$DateString-$RunNumber";
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000205 }
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000206 system 'mkdir','-p',$NewDir;
207 return $NewDir;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000208}
209
Sam Bishopa0e22662008-04-02 03:35:43 +0000210sub SetHtmlEnv {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000211
212 die "Wrong number of arguments." if (scalar(@_) != 2);
213
214 my $Args = shift;
215 my $Dir = shift;
216
217 die "No build command." if (scalar(@$Args) == 0);
218
219 my $Cmd = $$Args[0];
220
221 if ($Cmd =~ /configure/) {
222 return;
223 }
224
225 if ($Verbose) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000226 Diag("Emitting reports for this run to '$Dir'.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000227 }
228
229 $ENV{'CCC_ANALYZER_HTML'} = $Dir;
230}
231
232##----------------------------------------------------------------------------##
Ted Kremenek57cf4462008-04-18 15:09:30 +0000233# ComputeDigest - Compute a digest of the specified file.
234##----------------------------------------------------------------------------##
235
236sub ComputeDigest {
237 my $FName = shift;
Ted Kremenek23cfca32008-06-16 22:40:14 +0000238 DieDiag("Cannot read $FName to compute Digest.\n") if (! -r $FName);
Ted Kremeneka6e24812008-04-19 18:05:48 +0000239
240 # Use Digest::MD5. We don't have to be cryptographically secure. We're
Ted Kremenek7ea02e62008-04-19 18:07:44 +0000241 # just looking for duplicate files that come from a non-malicious source.
242 # We use Digest::MD5 because it is a standard Perl module that should
Ted Kremenek63c20172008-08-04 17:34:06 +0000243 # come bundled on most systems.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000244 open(FILE, $FName) or DieDiag("Cannot open $FName when computing Digest.\n");
Ted Kremeneka6e24812008-04-19 18:05:48 +0000245 binmode FILE;
246 my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest;
247 close(FILE);
248
Ted Kremenek63c20172008-08-04 17:34:06 +0000249 # Return the digest.
Ted Kremeneka6e24812008-04-19 18:05:48 +0000250 return $Result;
Ted Kremenek57cf4462008-04-18 15:09:30 +0000251}
252
253##----------------------------------------------------------------------------##
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000254# UpdatePrefix - Compute the common prefix of files.
255##----------------------------------------------------------------------------##
256
257my $Prefix;
258
259sub UpdatePrefix {
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000260 my $x = shift;
261 my $y = basename($x);
262 $x =~ s/\Q$y\E$//;
263
264 # Ignore /usr, /Library, /System, /Developer
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000265 return if ( $x =~ /^\/usr/ or $x =~ /^\/Library/
266 or $x =~ /^\/System/ or $x =~ /^\/Developer/);
267
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000268 if (!defined $Prefix) {
269 $Prefix = $x;
270 return;
271 }
272
273 chop $Prefix while (!($x =~ /^$Prefix/));
274}
275
276sub GetPrefix {
277 return $Prefix;
278}
279
280##----------------------------------------------------------------------------##
281# UpdateInFilePath - Update the path in the report file.
282##----------------------------------------------------------------------------##
283
284sub UpdateInFilePath {
285 my $fname = shift;
286 my $regex = shift;
287 my $newtext = shift;
Ted Kremenek63c20172008-08-04 17:34:06 +0000288
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000289 open (RIN, $fname) or die "cannot open $fname";
Ted Kremenek63c20172008-08-04 17:34:06 +0000290 open (ROUT, ">", "$fname.tmp") or die "cannot open $fname.tmp";
291
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000292 while (<RIN>) {
293 s/$regex/$newtext/;
294 print ROUT $_;
295 }
Ted Kremenek63c20172008-08-04 17:34:06 +0000296
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000297 close (ROUT);
298 close (RIN);
Ted Kremenek20161e92008-07-15 20:18:21 +0000299 system("mv", "$fname.tmp", $fname);
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000300}
301
302##----------------------------------------------------------------------------##
Ted Kremenek5744dc22008-04-02 18:03:36 +0000303# ScanFile - Scan a report file for various identifying attributes.
304##----------------------------------------------------------------------------##
305
Ted Kremenek57cf4462008-04-18 15:09:30 +0000306# Sometimes a source file is scanned more than once, and thus produces
307# multiple error reports. We use a cache to solve this problem.
308
309my %AlreadyScanned;
310
Ted Kremenek5744dc22008-04-02 18:03:36 +0000311sub ScanFile {
312
313 my $Index = shift;
314 my $Dir = shift;
315 my $FName = shift;
316
Ted Kremenek57cf4462008-04-18 15:09:30 +0000317 # Compute a digest for the report file. Determine if we have already
318 # scanned a file that looks just like it.
319
320 my $digest = ComputeDigest("$Dir/$FName");
321
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000322 if (defined $AlreadyScanned{$digest}) {
Ted Kremenek57cf4462008-04-18 15:09:30 +0000323 # Redundant file. Remove it.
Ted Kremenek20161e92008-07-15 20:18:21 +0000324 system ("rm", "-f", "$Dir/$FName");
Ted Kremenek57cf4462008-04-18 15:09:30 +0000325 return;
326 }
327
328 $AlreadyScanned{$digest} = 1;
329
Ted Kremenek809709f2008-04-18 16:58:34 +0000330 # At this point the report file is not world readable. Make it happen.
Ted Kremenek20161e92008-07-15 20:18:21 +0000331 system ("chmod", "644", "$Dir/$FName");
Ted Kremenek684bb092008-04-18 15:18:20 +0000332
333 # Scan the report file for tags.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000334 open(IN, "$Dir/$FName") or DieDiag("Cannot open '$Dir/$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000335
336 my $BugDesc = "";
Ted Kremenek22d6a632008-04-02 20:43:36 +0000337 my $BugFile = "";
338 my $BugPathLength = 1;
339 my $BugLine = 0;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000340
341 while (<IN>) {
342
343 if (/<!-- BUGDESC (.*) -->$/) {
344 $BugDesc = $1;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000345 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000346 elsif (/<!-- BUGFILE (.*) -->$/) {
347 $BugFile = $1;
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000348 UpdatePrefix($BugFile);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000349 }
350 elsif (/<!-- BUGPATHLENGTH (.*) -->$/) {
351 $BugPathLength = $1;
352 }
353 elsif (/<!-- BUGLINE (.*) -->$/) {
354 $BugLine = $1;
355 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000356 }
357
358 close(IN);
359
Ted Kremenek22d6a632008-04-02 20:43:36 +0000360 push @$Index,[ $FName, $BugDesc, $BugFile, $BugLine, $BugPathLength ];
361}
362
363##----------------------------------------------------------------------------##
364# CopyJS - Copy JavaScript code to target directory.
365##----------------------------------------------------------------------------##
366
367sub CopyJS {
368
369 my $Dir = shift;
370
Ted Kremenek23cfca32008-06-16 22:40:14 +0000371 DieDiag("Cannot find 'sorttable.js'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000372 if (! -r "$RealBin/sorttable.js");
373
Ted Kremenek20161e92008-07-15 20:18:21 +0000374 system ("cp", "$RealBin/sorttable.js", "$Dir");
Ted Kremenek22d6a632008-04-02 20:43:36 +0000375
Ted Kremenek23cfca32008-06-16 22:40:14 +0000376 DieDiag("Could not copy 'sorttable.js' to '$Dir'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000377 if (! -r "$Dir/sorttable.js");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000378}
379
380##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000381# Postprocess - Postprocess the results of an analysis scan.
382##----------------------------------------------------------------------------##
383
Sam Bishopa0e22662008-04-02 03:35:43 +0000384sub Postprocess {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000385
386 my $Dir = shift;
Ted Kremenek684bb092008-04-18 15:18:20 +0000387 my $BaseDir = shift;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000388
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000389 die "No directory specified." if (!defined $Dir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000390
391 if (! -d $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000392 Diag("No bugs found.\n");
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000393 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000394 }
395
396 opendir(DIR, $Dir);
Ted Kremenek991c54b2008-08-08 20:46:42 +0000397 my $Crashes = 0;
398 my @files = grep { if ($_ eq "crashes") { $Crashes++; }
399 /^report-.*\.html$/; } readdir(DIR);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000400 closedir(DIR);
401
Ted Kremenek991c54b2008-08-08 20:46:42 +0000402 if (scalar(@files) == 0 and $Crashes == 0) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000403 Diag("Removing directory '$Dir' because it contains no reports.\n");
Ted Kremenek20161e92008-07-15 20:18:21 +0000404 system ("rm", "-fR", $Dir);
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000405 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000406 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000407
Ted Kremenek991c54b2008-08-08 20:46:42 +0000408 # Scan each report file and build an index.
409 my @Index;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000410 foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); }
411
Ted Kremenekd52e4252008-08-25 20:45:07 +0000412 # Scan the crashes directory and use the information in the .info files
413 # to update the common prefix directory.
414 if (-d "$Dir/crashes") {
415 opendir(DIR, "$Dir/crashes");
416 my @files = grep { /[.]info$/; } readdir(DIR);
417 closedir(DIR);
418 foreach my $file (@files) {
419 open IN, "$Dir/crashes/$file" or DieDiag("cannot open $file\n");
420 my $Path = <IN>;
421 if (defined $Path) { UpdatePrefix($Path); }
422 close IN;
423 }
424 }
425
Ted Kremenek63c20172008-08-04 17:34:06 +0000426 # Generate an index.html file.
427 my $FName = "$Dir/index.html";
428 open(OUT, ">", $FName) or DieDiag("Cannot create file '$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000429
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000430 # Print out the header.
431
Ted Kremenek5744dc22008-04-02 18:03:36 +0000432print OUT <<ENDTEXT;
433<html>
434<head>
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000435<style type="text/css">
436 body { color:#000000; background-color:#ffffff }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000437 body { font-family: Helvetica, sans-serif; font-size:9pt }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000438 h1 { font-size:12pt }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000439 table thead {
Ted Kremenek22d6a632008-04-02 20:43:36 +0000440 background-color:#eee; color:#666666;
441 font-weight: bold; cursor: default;
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000442 text-align:center;
443 border-top: 2px solid #000000;
444 border-bottom: 2px solid #000000;
445 font-weight: bold; font-family: Verdana
446 }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000447 table { border: 1px #000000 solid }
448 table { border-collapse: collapse; border-spacing: 0px }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000449 td { border-bottom: 1px #000000 dotted }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000450 td { padding:5px; padding-left:8px; padding-right:8px }
Ted Kremenekd8c6d0c2008-04-07 23:50:07 +0000451 td { text-align:left; font-size:9pt }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000452 td.View { padding-left: 10px }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000453</style>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000454<script src="sorttable.js"></script>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000455<script language='javascript' type="text/javascript">
456function SetDisplay(RowClass, DisplayVal)
457{
458 var Rows = document.getElementsByTagName("tr");
459 for ( var i = 0 ; i < Rows.length; ++i ) {
460 if (Rows[i].className == RowClass) {
461 Rows[i].style.display = DisplayVal;
462 }
463 }
464}
465
466function ToggleDisplay(CheckButton, ClassName) {
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000467 if (CheckButton.checked) {
468 SetDisplay(ClassName, "");
469 }
470 else {
471 SetDisplay(ClassName, "none");
472 }
473}
474</script>
475</head>
476<body>
477ENDTEXT
478
Ted Kremenek991c54b2008-08-08 20:46:42 +0000479 if (scalar(@files)) {
480 # Print out the summary table.
481 my %Totals;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000482
Ted Kremenek991c54b2008-08-08 20:46:42 +0000483 for my $row ( @Index ) {
484 #my $bug_type = lc($row->[1]);
485 my $bug_type = ($row->[1]);
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000486
Ted Kremenek991c54b2008-08-08 20:46:42 +0000487 if (!defined $Totals{$bug_type}) { $Totals{$bug_type} = 1; }
488 else { $Totals{$bug_type}++; }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000489 }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000490
491 print OUT "<h3>Bug Summary</h3>";
492
493 if (defined $BuildName) {
494 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 +0000495 }
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000496
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000497print OUT <<ENDTEXT;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000498<table class="sortable">
499<tr>
500 <td>Bug Type</td>
501 <td>Quantity</td>
Ted Kremenek2645c772008-07-07 16:58:44 +0000502 <td class="sorttable_nosort">Display?</td>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000503</tr>
504ENDTEXT
505
Ted Kremenek991c54b2008-08-08 20:46:42 +0000506 for my $key ( sort { $a cmp $b } keys %Totals ) {
507 my $x = lc($key);
508 $x =~ s/[ ,'"]+/_/g;
509 print OUT "<tr><td>$key</td><td>$Totals{$key}</td><td><input type=\"checkbox\" onClick=\"ToggleDisplay(this,'bt_$x');\" checked/></td></tr>\n";
510 }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000511
512 # Print out the table of errors.
513
514print OUT <<ENDTEXT;
515</table>
516<h3>Reports</h3>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000517<table class="sortable">
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000518<tr>
Ted Kremenek88a96d62008-07-07 17:23:32 +0000519 <td class="sorttable_sorted">Bug Type<span id="sorttable_sortfwdind">&nbsp;&#x25BE;</span>
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000520 <td>File</td>
521 <td>Line</td>
522 <td>Path Length</td>
Ted Kremenek2645c772008-07-07 16:58:44 +0000523 <td class="sorttable_nosort"></td>
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000524</tr>
Ted Kremenek5744dc22008-04-02 18:03:36 +0000525ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000526
Ted Kremenek991c54b2008-08-08 20:46:42 +0000527 my $prefix = GetPrefix();
528 my $regex;
529 my $InFileRegex;
530 my $InFilePrefix = "File:</td><td>";
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000531
Ted Kremenek991c54b2008-08-08 20:46:42 +0000532 if (defined $prefix) {
533 $regex = qr/^\Q$prefix\E/is;
534 $InFileRegex = qr/\Q$InFilePrefix$prefix\E/is;
535 }
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000536
Ted Kremenek991c54b2008-08-08 20:46:42 +0000537 for my $row ( sort { $a->[1] cmp $b->[1] } @Index ) {
Ted Kremenek5744dc22008-04-02 18:03:36 +0000538
Ted Kremenek991c54b2008-08-08 20:46:42 +0000539 my $x = lc($row->[1]);
540 $x =~ s/[ ,'"]+/_/g;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000541
Ted Kremenek991c54b2008-08-08 20:46:42 +0000542 print OUT "<tr class=\"bt_$x\">\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000543
Ted Kremenek991c54b2008-08-08 20:46:42 +0000544 my $ReportFile = $row->[0];
Ted Kremenek5744dc22008-04-02 18:03:36 +0000545
Ted Kremenek991c54b2008-08-08 20:46:42 +0000546 print OUT " <td class=\"DESC\">";
547 #print OUT lc($row->[1]);
548 print OUT $row->[1];
549 print OUT "</td>\n";
Ted Kremenek5744dc22008-04-02 18:03:36 +0000550
Ted Kremenek991c54b2008-08-08 20:46:42 +0000551 # Update the file prefix.
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000552
Ted Kremenek991c54b2008-08-08 20:46:42 +0000553 my $fname = $row->[2];
554 if (defined $regex) {
555 $fname =~ s/$regex//;
556 UpdateInFilePath("$Dir/$ReportFile", $InFileRegex, $InFilePrefix)
557 }
Ted Kremenek3e56e0b2008-05-02 23:40:49 +0000558
Ted Kremenek991c54b2008-08-08 20:46:42 +0000559 print OUT "<td>$fname</td>\n";
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000560
Ted Kremenek991c54b2008-08-08 20:46:42 +0000561 # Print the rest of the columns.
562 for my $j ( 3 .. $#{$row} ) {
563 print OUT "<td>$row->[$j]</td>\n"
564 }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000565
Ted Kremenek991c54b2008-08-08 20:46:42 +0000566 # Emit the "View" link.
567 print OUT " <td class=\"View\"><a href=\"$ReportFile#EndPath\">View</a></td>\n";
Ted Kremenek3cea9ee2008-07-30 17:58:08 +0000568
Ted Kremenek991c54b2008-08-08 20:46:42 +0000569 # End the row.
570 print OUT "</tr>\n";
571 }
572
573 print OUT "</table>\n";
574 }
575
576 if ($Crashes) {
577 # Read the crash directory for files.
578 opendir(DIR, "$Dir/crashes");
579 my @files = grep { /[.]info$/ } readdir(DIR);
580 closedir(DIR);
581
582 if (scalar(@files)) {
583 print OUT <<ENDTEXT;
Ted Kremenek5d31f832008-08-18 18:38:29 +0000584<h3>Analyzer Failures</h3>
Ted Kremenek991c54b2008-08-08 20:46:42 +0000585
Ted Kremenek5d31f832008-08-18 18:38:29 +0000586<p>The analyzer had problems processing the following files:</p>
Ted Kremenek991c54b2008-08-08 20:46:42 +0000587
588<table>
Ted Kremenek5d31f832008-08-18 18:38:29 +0000589<thead><tr><td>Problem</td><td>Source File</td><td>Preprocessed File</td></tr></thead>
Ted Kremenek991c54b2008-08-08 20:46:42 +0000590ENDTEXT
591
592 foreach my $file (sort @files) {
593 $file =~ /(.+).info$/;
594 # Get the preprocessed file.
595 my $ppfile = $1;
596 # Open the info file and get the name of the source file.
597 open (INFO, "$Dir/crashes/$file") or
598 die "Cannot open $Dir/crashes/$file\n";
599 my $srcfile = <INFO>;
Ted Kremenek5d31f832008-08-18 18:38:29 +0000600 chomp $srcfile;
601 my $problem = <INFO>;
602 chomp $problem;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000603 close (INFO);
604 # Print the information in the table.
Ted Kremenekd52e4252008-08-25 20:45:07 +0000605 my $prefix = GetPrefix();
606 if (defined $prefix) { $srcfile =~ s/^$prefix//; }
Ted Kremenek5d31f832008-08-18 18:38:29 +0000607 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 +0000608 }
609
610 print OUT <<ENDTEXT;
611</table>
612<p>Please consider submitting preprocessed files as <a href="http://clang.llvm.org/StaticAnalysisUsage.html#filingbugs">bug reports</a>.</p>
613ENDTEXT
614 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000615 }
616
Ted Kremenek991c54b2008-08-08 20:46:42 +0000617 print OUT "</body></html>\n";
Ted Kremenek5744dc22008-04-02 18:03:36 +0000618 close(OUT);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000619 CopyJS($Dir);
Ted Kremenek20161e92008-07-15 20:18:21 +0000620
621 # Make sure $Dir and $BaseDir are world readable/executable.
622 system("chmod", "755", $Dir);
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000623 if (defined $BaseDir) { system("chmod", "755", $BaseDir); }
Ted Kremenek20161e92008-07-15 20:18:21 +0000624
Ted Kremenek23cfca32008-06-16 22:40:14 +0000625 my $Num = scalar(@Index);
Ted Kremenek150c2122008-07-11 19:15:05 +0000626 Diag("$Num bugs found.\n");
627 if ($Num > 0 && -r "$Dir/index.html") {
628 Diag("Open '$Dir/index.html' to examine bug reports.\n");
629 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000630
Ted Kremenek991c54b2008-08-08 20:46:42 +0000631 DiagCrashes($Dir) if ($Crashes);
632
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000633 return $Num;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000634}
635
636##----------------------------------------------------------------------------##
Ted Kremenekdab11102008-04-02 04:43:42 +0000637# RunBuildCommand - Run the build command.
638##----------------------------------------------------------------------------##
639
Ted Kremenek6b628982008-04-30 23:47:12 +0000640sub AddIfNotPresent {
641 my $Args = shift;
642 my $Arg = shift;
643 my $found = 0;
644
645 foreach my $k (@$Args) {
646 if ($k eq $Arg) {
647 $found = 1;
648 last;
649 }
650 }
651
652 if ($found == 0) {
653 push @$Args, $Arg;
654 }
655}
656
Ted Kremenekdab11102008-04-02 04:43:42 +0000657sub RunBuildCommand {
658
659 my $Args = shift;
Ted Kremenek7442ca62008-04-02 16:04:51 +0000660 my $IgnoreErrors = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000661 my $Cmd = $Args->[0];
Ted Kremenek6195c372008-06-02 21:52:47 +0000662 my $CCAnalyzer = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000663
Ted Kremenek3301cb12008-06-30 18:18:16 +0000664 # Get only the part of the command after the last '/'.
665 if ($Cmd =~ /\/([^\/]+)$/) {
666 $Cmd = $1;
667 }
668
Ted Kremenek63c20172008-08-04 17:34:06 +0000669 if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc"
670 or $Cmd eq "ccc-analyzer") {
Ted Kremenekdab11102008-04-02 04:43:42 +0000671 shift @$Args;
Ted Kremenek6195c372008-06-02 21:52:47 +0000672 unshift @$Args, $CCAnalyzer;
Ted Kremenekdab11102008-04-02 04:43:42 +0000673 }
Ted Kremenek7442ca62008-04-02 16:04:51 +0000674 elsif ($IgnoreErrors) {
675 if ($Cmd eq "make" or $Cmd eq "gmake") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000676 AddIfNotPresent($Args,"-k");
Ted Kremenek8912b542008-05-13 21:28:02 +0000677 AddIfNotPresent($Args,"-i");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000678 }
679 elsif ($Cmd eq "xcodebuild") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000680 AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000681 }
Ted Kremenek6b628982008-04-30 23:47:12 +0000682 }
683
Ted Kremenek6b628982008-04-30 23:47:12 +0000684 if ($Cmd eq "xcodebuild") {
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000685 # Disable distributed builds for xcodebuild.
Ted Kremenek6b628982008-04-30 23:47:12 +0000686 AddIfNotPresent($Args,"-nodistribute");
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000687
688 # Disable PCH files until clang supports them.
689 AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO");
Ted Kremenek915e9722008-05-27 23:18:07 +0000690
691 # When 'CC' is set, xcodebuild uses it to do all linking, even if we are
692 # linking C++ object files. Set 'LDPLUSPLUS' so that xcodebuild uses 'g++'
693 # when linking such files.
Ted Kremenek95aa1052008-09-04 17:52:41 +0000694 die if (!defined $CXX);
695 my $LDPLUSPLUS = `which $CXX`;
Ted Kremenek915e9722008-05-27 23:18:07 +0000696 $LDPLUSPLUS =~ s/\015?\012//; # strip newlines
697 $ENV{'LDPLUSPLUS'} = $LDPLUSPLUS;
Ted Kremenek6b628982008-04-30 23:47:12 +0000698 }
Ted Kremenekdab11102008-04-02 04:43:42 +0000699
Ted Kremenek5a4ddaf2008-08-25 20:10:45 +0000700 return (system(@$Args) >> 8);
Ted Kremenekdab11102008-04-02 04:43:42 +0000701}
702
Ted Kremenekdab11102008-04-02 04:43:42 +0000703##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000704# DisplayHelp - Utility function to display all help options.
705##----------------------------------------------------------------------------##
706
Sam Bishopa0e22662008-04-02 03:35:43 +0000707sub DisplayHelp {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000708
Ted Kremenek5744dc22008-04-02 18:03:36 +0000709print <<ENDTEXT;
Sam Bishopa0e22662008-04-02 03:35:43 +0000710USAGE: $Prog [options] <build command> [build options]
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000711
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000712ENDTEXT
713
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000714 if (defined $BuildName) {
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000715 print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n";
716 }
717
718print <<ENDTEXT;
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000719OPTIONS:
720
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000721 -o - Target directory for HTML report files. Subdirectories
Sam Bishopa0e22662008-04-02 03:35:43 +0000722 will be created as needed to represent separate "runs" of
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000723 the analyzer. If this option is not specified, a directory
724 is created in /tmp to store the reports.
Ted Kremenek1262fc42008-05-14 20:10:33 +0000725
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000726 -h - Display this message.
727 --help
Ted Kremenek1262fc42008-05-14 20:10:33 +0000728
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000729 -k - Add a "keep on going" option to the specified build command.
730 --keep-going This option currently supports make and xcodebuild.
Ted Kremenekf02e8db2008-04-02 16:41:25 +0000731 This is a convenience option; one can specify this
732 behavior directly using build options.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000733
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000734 --status-bugs - By default, the exit status of $Prog is the same as the
735 executed build command. Specifying this option causes the
736 exit status of $Prog to be 1 if it found potential bugs
737 and 0 otherwise.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000738
Ted Kremenek386c6932008-09-03 17:59:35 +0000739 --use-cc [compiler path] - By default, $Prog uses 'gcc' to compile and link
740 --use-cc=[compiler path] your C and Objective-C code. Use this option
741 to specify an alternate compiler.
742
743 --use-c++ [compiler path] - By default, $Prog uses 'g++' to compile and link
744 --use-c++=[compiler path] your C++ and Objective-C++ code. Use this option
745 to specify an alternate compiler.
Ted Kremenekf17ef3c2008-08-21 21:47:09 +0000746
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000747 -v - Verbose output from $Prog and the analyzer.
Ted Kremenek386c6932008-09-03 17:59:35 +0000748 A second and third '-v' increases verbosity.
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000749
750 -V - View analysis results in a web browser when the build
751 --view completes.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000752
Ted Kremenekb7770c02008-07-15 17:06:13 +0000753
Ted Kremenek386c6932008-09-03 17:59:35 +0000754AVAILABLE ANALYSES (multiple analyses may be specified):
Ted Kremenekd52e4252008-08-25 20:45:07 +0000755
756ENDTEXT
Ted Kremenekb7770c02008-07-15 17:06:13 +0000757
758 foreach my $Analysis (sort keys %AvailableAnalyses) {
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000759 if (defined $AnalysesDefaultEnabled{$Analysis}) {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000760 print " (+)";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000761 }
762 else {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000763 print " ";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000764 }
765
766 print " $Analysis $AvailableAnalyses{$Analysis}\n";
767 }
768
769print <<ENDTEXT
770
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000771 NOTE: "(+)" indicates that an analysis is enabled by default unless one
772 or more analysis options are specified
Ted Kremenekb7770c02008-07-15 17:06:13 +0000773
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000774BUILD OPTIONS
775
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000776 You can specify any build option acceptable to the build command.
Ted Kremenek39eefde2008-04-02 16:47:27 +0000777
Ted Kremenek5744dc22008-04-02 18:03:36 +0000778EXAMPLE
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000779
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000780 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000781
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000782 The above example causes analysis reports to be deposited into
783 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
784 A different subdirectory is created each time $Prog analyzes a project.
785 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000786
787ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000788}
789
790##----------------------------------------------------------------------------##
791# Process command-line arguments.
792##----------------------------------------------------------------------------##
793
794my $HtmlDir; # Parent directory to store HTML files.
795my $IgnoreErrors = 0; # Ignore build errors.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000796my $ViewResults = 0; # View results when the build terminates.
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000797my $ExitStatusFoundBugs = 0; # Exit status reflects whether bugs were found
Ted Kremenekb7770c02008-07-15 17:06:13 +0000798my @AnalysesToRun;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000799
800if (!@ARGV) {
801 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000802 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000803}
804
805while (@ARGV) {
806
807 # Scan for options we recognize.
808
809 my $arg = $ARGV[0];
810
Sam Bishop2f2418e2008-04-03 14:29:47 +0000811 if ($arg eq "-h" or $arg eq "--help") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000812 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000813 exit 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000814 }
815
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000816 if (defined $AvailableAnalyses{$arg}) {
Ted Kremenek1262fc42008-05-14 20:10:33 +0000817 shift @ARGV;
Ted Kremenekb7770c02008-07-15 17:06:13 +0000818 push @AnalysesToRun, $arg;
Ted Kremenek1262fc42008-05-14 20:10:33 +0000819 next;
820 }
821
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000822 if ($arg eq "-o") {
823 shift @ARGV;
824
825 if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000826 DieDiag("'-o' option requires a target directory name.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000827 }
828
829 $HtmlDir = shift @ARGV;
830 next;
831 }
832
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000833 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000834 shift @ARGV;
835 $IgnoreErrors = 1;
836 next;
837 }
838
Ted Kremenekf17ef3c2008-08-21 21:47:09 +0000839 if ($arg =~ /^--use-cc(=(.+))?$/) {
840 shift @ARGV;
841 my $cc;
842
843 if ($2 eq "") {
844 if (!@ARGV) {
845 DieDiag("'--use-cc' option requires a compiler executable name.\n");
846 }
847 $cc = shift @ARGV;
848 }
849 else {
850 $cc = $2;
851 }
852
853 $ENV{"CCC_CC"} = $cc;
854 next;
855 }
856
Ted Kremenek386c6932008-09-03 17:59:35 +0000857 if ($arg =~ /^--use-c[+][+](=(.+))?$/) {
858 shift @ARGV;
859
860 if ($2 eq "") {
861 if (!@ARGV) {
862 DieDiag("'--use-c++' option requires a compiler executable name.\n");
863 }
864 $CXX = shift @ARGV;
865 }
866 else {
867 $CXX = $2;
868 }
869 next;
870 }
871
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000872 if ($arg eq "-v") {
873 shift @ARGV;
874 $Verbose++;
875 next;
876 }
877
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000878 if ($arg eq "-V" or $arg eq "--view") {
879 shift @ARGV;
880 $ViewResults = 1;
881 next;
882 }
883
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000884 if ($arg eq "--status-bugs") {
885 shift @ARGV;
886 $ExitStatusFoundBugs = 1;
887 next;
888 }
889
Ted Kremenek23cfca32008-06-16 22:40:14 +0000890 DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/);
Ted Kremenek0062ad42008-04-02 16:35:01 +0000891
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000892 last;
893}
894
895if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000896 Diag("No build command specified.\n\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000897 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000898 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000899}
900
Ted Kremenek386c6932008-09-03 17:59:35 +0000901
902
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000903# Determine the output directory for the HTML reports.
Ted Kremenek684bb092008-04-18 15:18:20 +0000904my $BaseDir = $HtmlDir;
Sam Bishopa0e22662008-04-02 03:35:43 +0000905$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000906
907# Set the appropriate environment variables.
Sam Bishopa0e22662008-04-02 03:35:43 +0000908SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000909
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000910my $Cmd = "$RealBin/ccc-analyzer";
911
Ted Kremenek23cfca32008-06-16 22:40:14 +0000912DieDiag("Executable 'ccc-analyzer' does not exist at '$Cmd'\n")
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000913 if (! -x $Cmd);
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000914
Ted Kremenekb7770c02008-07-15 17:06:13 +0000915if (! -x $ClangSB) {
916 Diag("'clang' executable not found in '$RealBin'.\n");
917 Diag("Using 'clang' from path.\n");
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000918}
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000919
Ted Kremenek95aa1052008-09-04 17:52:41 +0000920if (defined $CXX) {
921 $ENV{'CXX'} = $CXX;
922}
923else {
924 $CXX = 'g++'; # This variable is used by other parts of scan-build
925 # that need to know a default C++ compiler to fall back to.
926}
927
Ted Kremenek4f4b17d2008-04-03 20:08:18 +0000928$ENV{'CC'} = $Cmd;
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000929$ENV{'CLANG'} = $Clang;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000930
931if ($Verbose >= 2) {
932 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
933}
934
Ted Kremeneka9525c92008-05-12 22:07:14 +0000935if ($Verbose >= 3) {
936 $ENV{'CCC_ANALYZER_LOG'} = 1;
937}
938
Ted Kremenek90125992008-07-15 23:41:32 +0000939if (scalar(@AnalysesToRun) == 0) {
940 foreach my $key (keys %AnalysesDefaultEnabled) {
941 push @AnalysesToRun,$key;
942 }
Ted Kremenek01006782008-07-02 23:16:10 +0000943}
Ted Kremenek1262fc42008-05-14 20:10:33 +0000944
Ted Kremenek90125992008-07-15 23:41:32 +0000945$ENV{'CCC_ANALYZER_ANALYSIS'} = join ' ',@AnalysesToRun;
946
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000947# Run the build.
Ted Kremenek5656a982008-07-15 17:09:28 +0000948my $ExitStatus = RunBuildCommand(\@ARGV, $IgnoreErrors, $Cmd);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000949
950# Postprocess the HTML directory.
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000951my $NumBugs = Postprocess($HtmlDir, $BaseDir);
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000952
953if ($ViewResults and -r "$HtmlDir/index.html") {
954 # Only works on Mac OS X (for now).
955 print "Viewing analysis results: '$HtmlDir/index.html'\n";
Ted Kremenek20161e92008-07-15 20:18:21 +0000956 system("open", "$HtmlDir/index.html");
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000957}
Ted Kremenek5656a982008-07-15 17:09:28 +0000958
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000959if ($ExitStatusFoundBugs) {
960 exit 1 if ($NumBugs > 0);
961 exit 0;
962}
963
Ted Kremenek5656a982008-07-15 17:09:28 +0000964exit $ExitStatus;
965