blob: 0cf170f39bf6aba04d23a44c755bc47b46d803dd [file] [log] [blame]
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001#!/usr/bin/env perl
2#
3# The LLVM Compiler Infrastructure
4#
5# This file is distributed under the University of Illinois Open Source
6# License. See LICENSE.TXT for details.
7#
8##===----------------------------------------------------------------------===##
9#
10# A script designed to wrap a build so that all calls to gcc are intercepted
11# and piped to the static analyzer.
12#
13##===----------------------------------------------------------------------===##
14
15use strict;
16use warnings;
Ted Kremenek22d6a632008-04-02 20:43:36 +000017use FindBin qw($RealBin);
Ted Kremeneka6e24812008-04-19 18:05:48 +000018use Digest::MD5;
Ted Kremenek7a4648d2008-05-02 22:04:53 +000019use File::Basename;
Ted Kremenek23cfca32008-06-16 22:40:14 +000020use Term::ANSIColor;
21use Term::ANSIColor qw(:constants);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000022
23my $Verbose = 0; # Verbose output from this script.
24my $Prog = "scan-build";
Ted Kremenekf4cdf412008-05-23 18:17:05 +000025my $BuildName;
26my $BuildDate;
Ted Kremenek95aa1052008-09-04 17:52:41 +000027my $CXX; # Leave undefined initially.
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000028
Ted Kremenek0e689382008-09-11 18:17:51 +000029my $TERM = $ENV{'TERM'};
30my $UseColor = (defined $TERM and $TERM eq 'xterm-color' and -t STDOUT
31 and defined $ENV{'SCAN_BUILD_COLOR'});
Ted Kremenek23cfca32008-06-16 22:40:14 +000032
Ted Kremenekb7770c02008-07-15 17:06:13 +000033##----------------------------------------------------------------------------##
34# Diagnostics
35##----------------------------------------------------------------------------##
36
Ted Kremenek23cfca32008-06-16 22:40:14 +000037sub Diag {
38 if ($UseColor) {
39 print BOLD, MAGENTA "$Prog: @_";
40 print RESET;
41 }
42 else {
43 print "$Prog: @_";
44 }
45}
46
Ted Kremenek991c54b2008-08-08 20:46:42 +000047sub DiagCrashes {
48 my $Dir = shift;
49 Diag ("The analyzer crashed on some source files.\n");
Ted Kremenek386c6932008-09-03 17:59:35 +000050 Diag ("Preprocessed versions of crashed files were deposited in '$Dir/crashes'.\n");
Ted Kremenek991c54b2008-08-08 20:46:42 +000051 Diag ("Please consider submitting a bug report using these files:\n");
52 Diag (" http://clang.llvm.org/StaticAnalysisUsage.html#filingbugs\n")
53}
54
Ted Kremenek23cfca32008-06-16 22:40:14 +000055sub DieDiag {
56 if ($UseColor) {
57 print BOLD, RED "$Prog: ";
58 print RESET, RED @_;
59 print RESET;
60 }
61 else {
62 print "$Prog: ", @_;
63 }
64 exit(0);
65}
66
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000067##----------------------------------------------------------------------------##
Ted Kremenekb7770c02008-07-15 17:06:13 +000068# Some initial preprocessing of Clang options.
69##----------------------------------------------------------------------------##
70
71my $ClangSB = "$RealBin/clang";
72my $Clang = $ClangSB;
73
74if (! -x $ClangSB) {
75 $Clang = "clang";
76}
77
78my %AvailableAnalyses;
79
80# Query clang for analysis options.
Ted Kremenek63c20172008-08-04 17:34:06 +000081open(PIPE, "-|", $Clang, "--help") or
Ted Kremenekb7770c02008-07-15 17:06:13 +000082 DieDiag("Cannot execute '$Clang'");
Ted Kremenek63c20172008-08-04 17:34:06 +000083
Ted Kremenekb7770c02008-07-15 17:06:13 +000084my $FoundAnalysis = 0;
85
86while(<PIPE>) {
87 if ($FoundAnalysis == 0) {
88 if (/Available Source Code Analyses/) {
89 $FoundAnalysis = 1;
90 }
Ted Kremenek991c54b2008-08-08 20:46:42 +000091
Ted Kremenekb7770c02008-07-15 17:06:13 +000092 next;
93 }
94
95 if (/^\s\s\s\s([^\s]+)\s(.+)$/) {
96 next if ($1 =~ /-dump/ or $1 =~ /-view/
97 or $1 =~ /-checker-simple/ or $1 =~ /-warn-uninit/);
98
99 $AvailableAnalyses{$1} = $2;
100 next;
101 }
102
103 last;
104}
105
106close (PIPE);
107
108my %AnalysesDefaultEnabled = (
109 '-warn-dead-stores' => 1,
110 '-checker-cfref' => 1,
Ted Kremenek90125992008-07-15 23:41:32 +0000111 '-warn-objc-methodsigs' => 1,
Ted Kremenekbde3a052008-07-25 20:35:01 +0000112 '-warn-objc-missing-dealloc' => 1,
Ted Kremenek5d443492008-09-18 06:34:16 +0000113 '-warn-objc-unused-ivars' => 1,
Ted Kremenekb7770c02008-07-15 17:06:13 +0000114);
115
116##----------------------------------------------------------------------------##
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000117# GetHTMLRunDir - Construct an HTML directory name for the current sub-run.
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000118##----------------------------------------------------------------------------##
119
Sam Bishopa0e22662008-04-02 03:35:43 +0000120sub GetHTMLRunDir {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000121
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000122 die "Not enough arguments." if (@_ == 0);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000123 my $Dir = shift @_;
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000124
125 my $TmpMode = 0;
126 if (!defined $Dir) {
127 $Dir = "/tmp";
128 $TmpMode = 1;
129 }
130
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000131 # Get current date and time.
132
133 my @CurrentTime = localtime();
134
135 my $year = $CurrentTime[5] + 1900;
136 my $day = $CurrentTime[3];
137 my $month = $CurrentTime[4] + 1;
138
Ted Kremenek9d7405f2008-05-14 17:23:56 +0000139 my $DateString = sprintf("%d-%02d-%02d", $year, $month, $day);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000140
141 # Determine the run number.
142
143 my $RunNumber;
144
145 if (-d $Dir) {
146
147 if (! -r $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000148 DieDiag("directory '$Dir' exists but is not readable.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000149 }
150
151 # Iterate over all files in the specified directory.
152
153 my $max = 0;
154
155 opendir(DIR, $Dir);
Ted Kremenek29da6c52008-08-07 17:57:34 +0000156 my @FILES = grep { -d "$Dir/$_" } readdir(DIR);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000157 closedir(DIR);
158
159 foreach my $f (@FILES) {
160
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000161 # Strip the prefix '$Prog-' if we are dumping files to /tmp.
162 if ($TmpMode) {
163 next if (!($f =~ /^$Prog-(.+)/));
164 $f = $1;
165 }
166
Ted Kremenekebb74132008-09-21 06:58:09 +0000167
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000168 my @x = split/-/, $f;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000169 next if (scalar(@x) != 4);
170 next if ($x[0] != $year);
171 next if ($x[1] != $month);
172 next if ($x[2] != $day);
173
174 if ($x[3] > $max) {
175 $max = $x[3];
176 }
177 }
178
179 $RunNumber = $max + 1;
180 }
181 else {
182
183 if (-x $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000184 DieDiag("'$Dir' exists but is not a directory.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000185 }
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000186
187 if ($TmpMode) {
188 DieDiag("The directory '/tmp' does not exist or cannot be accessed.");
189 }
190
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000191 # $Dir does not exist. It will be automatically created by the
192 # clang driver. Set the run number to 1.
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000193
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000194 $RunNumber = 1;
195 }
196
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000197 die "RunNumber must be defined!" if (!defined $RunNumber);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000198
199 # Append the run number.
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000200 my $NewDir;
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000201 if ($TmpMode) {
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000202 $NewDir = "$Dir/$Prog-$DateString-$RunNumber";
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000203 }
204 else {
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000205 $NewDir = "$Dir/$DateString-$RunNumber";
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000206 }
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000207 system 'mkdir','-p',$NewDir;
208 return $NewDir;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000209}
210
Sam Bishopa0e22662008-04-02 03:35:43 +0000211sub SetHtmlEnv {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000212
213 die "Wrong number of arguments." if (scalar(@_) != 2);
214
215 my $Args = shift;
216 my $Dir = shift;
217
218 die "No build command." if (scalar(@$Args) == 0);
219
220 my $Cmd = $$Args[0];
221
222 if ($Cmd =~ /configure/) {
223 return;
224 }
225
226 if ($Verbose) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000227 Diag("Emitting reports for this run to '$Dir'.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000228 }
229
230 $ENV{'CCC_ANALYZER_HTML'} = $Dir;
231}
232
233##----------------------------------------------------------------------------##
Ted Kremenek57cf4462008-04-18 15:09:30 +0000234# ComputeDigest - Compute a digest of the specified file.
235##----------------------------------------------------------------------------##
236
237sub ComputeDigest {
238 my $FName = shift;
Ted Kremenek23cfca32008-06-16 22:40:14 +0000239 DieDiag("Cannot read $FName to compute Digest.\n") if (! -r $FName);
Ted Kremeneka6e24812008-04-19 18:05:48 +0000240
241 # Use Digest::MD5. We don't have to be cryptographically secure. We're
Ted Kremenek7ea02e62008-04-19 18:07:44 +0000242 # just looking for duplicate files that come from a non-malicious source.
243 # We use Digest::MD5 because it is a standard Perl module that should
Ted Kremenek63c20172008-08-04 17:34:06 +0000244 # come bundled on most systems.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000245 open(FILE, $FName) or DieDiag("Cannot open $FName when computing Digest.\n");
Ted Kremeneka6e24812008-04-19 18:05:48 +0000246 binmode FILE;
247 my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest;
248 close(FILE);
249
Ted Kremenek63c20172008-08-04 17:34:06 +0000250 # Return the digest.
Ted Kremeneka6e24812008-04-19 18:05:48 +0000251 return $Result;
Ted Kremenek57cf4462008-04-18 15:09:30 +0000252}
253
254##----------------------------------------------------------------------------##
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000255# UpdatePrefix - Compute the common prefix of files.
256##----------------------------------------------------------------------------##
257
258my $Prefix;
259
260sub UpdatePrefix {
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000261 my $x = shift;
262 my $y = basename($x);
263 $x =~ s/\Q$y\E$//;
264
265 # Ignore /usr, /Library, /System, /Developer
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000266 return if ( $x =~ /^\/usr/ or $x =~ /^\/Library/
267 or $x =~ /^\/System/ or $x =~ /^\/Developer/);
268
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000269 if (!defined $Prefix) {
270 $Prefix = $x;
271 return;
272 }
273
Ted Kremenek20b2bae2008-09-11 21:15:10 +0000274 chop $Prefix while (!($x =~ /^\Q$Prefix/));
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000275}
276
277sub GetPrefix {
278 return $Prefix;
279}
280
281##----------------------------------------------------------------------------##
282# UpdateInFilePath - Update the path in the report file.
283##----------------------------------------------------------------------------##
284
285sub UpdateInFilePath {
286 my $fname = shift;
287 my $regex = shift;
288 my $newtext = shift;
Ted Kremenek63c20172008-08-04 17:34:06 +0000289
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000290 open (RIN, $fname) or die "cannot open $fname";
Ted Kremenek63c20172008-08-04 17:34:06 +0000291 open (ROUT, ">", "$fname.tmp") or die "cannot open $fname.tmp";
292
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000293 while (<RIN>) {
294 s/$regex/$newtext/;
295 print ROUT $_;
296 }
Ted Kremenek63c20172008-08-04 17:34:06 +0000297
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000298 close (ROUT);
299 close (RIN);
Ted Kremenek20161e92008-07-15 20:18:21 +0000300 system("mv", "$fname.tmp", $fname);
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000301}
302
303##----------------------------------------------------------------------------##
Ted Kremenek5744dc22008-04-02 18:03:36 +0000304# ScanFile - Scan a report file for various identifying attributes.
305##----------------------------------------------------------------------------##
306
Ted Kremenek57cf4462008-04-18 15:09:30 +0000307# Sometimes a source file is scanned more than once, and thus produces
308# multiple error reports. We use a cache to solve this problem.
309
310my %AlreadyScanned;
311
Ted Kremenek5744dc22008-04-02 18:03:36 +0000312sub ScanFile {
313
314 my $Index = shift;
315 my $Dir = shift;
316 my $FName = shift;
317
Ted Kremenek57cf4462008-04-18 15:09:30 +0000318 # Compute a digest for the report file. Determine if we have already
319 # scanned a file that looks just like it.
320
321 my $digest = ComputeDigest("$Dir/$FName");
322
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000323 if (defined $AlreadyScanned{$digest}) {
Ted Kremenek57cf4462008-04-18 15:09:30 +0000324 # Redundant file. Remove it.
Ted Kremenek20161e92008-07-15 20:18:21 +0000325 system ("rm", "-f", "$Dir/$FName");
Ted Kremenek57cf4462008-04-18 15:09:30 +0000326 return;
327 }
328
329 $AlreadyScanned{$digest} = 1;
330
Ted Kremenek809709f2008-04-18 16:58:34 +0000331 # At this point the report file is not world readable. Make it happen.
Ted Kremenek20161e92008-07-15 20:18:21 +0000332 system ("chmod", "644", "$Dir/$FName");
Ted Kremenek684bb092008-04-18 15:18:20 +0000333
334 # Scan the report file for tags.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000335 open(IN, "$Dir/$FName") or DieDiag("Cannot open '$Dir/$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000336
337 my $BugDesc = "";
Ted Kremenek22d6a632008-04-02 20:43:36 +0000338 my $BugFile = "";
Ted Kremenekebb74132008-09-21 06:58:09 +0000339 my $BugCategory;
Ted Kremenek22d6a632008-04-02 20:43:36 +0000340 my $BugPathLength = 1;
341 my $BugLine = 0;
Ted Kremenekebb74132008-09-21 06:58:09 +0000342 my $found = 0;
343
Ted Kremenek5744dc22008-04-02 18:03:36 +0000344 while (<IN>) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000345
346 last if ($found == 5);
347
Ted Kremenek5744dc22008-04-02 18:03:36 +0000348 if (/<!-- BUGDESC (.*) -->$/) {
349 $BugDesc = $1;
Ted Kremenekebb74132008-09-21 06:58:09 +0000350 print "Desc in $BugDesc\n";
351 ++$found;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000352 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000353 elsif (/<!-- BUGFILE (.*) -->$/) {
354 $BugFile = $1;
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000355 UpdatePrefix($BugFile);
Ted Kremenekebb74132008-09-21 06:58:09 +0000356 ++$found;
Ted Kremenek22d6a632008-04-02 20:43:36 +0000357 }
358 elsif (/<!-- BUGPATHLENGTH (.*) -->$/) {
359 $BugPathLength = $1;
Ted Kremenekebb74132008-09-21 06:58:09 +0000360 ++$found;
Ted Kremenek22d6a632008-04-02 20:43:36 +0000361 }
362 elsif (/<!-- BUGLINE (.*) -->$/) {
363 $BugLine = $1;
Ted Kremenekebb74132008-09-21 06:58:09 +0000364 ++$found;
365 }
366 elsif (/<!-- BUGCATEGORY (.*) -->$/) {
367 $BugCategory = $1;
368 ++$found;
Ted Kremenek22d6a632008-04-02 20:43:36 +0000369 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000370 }
371
372 close(IN);
Ted Kremenekebb74132008-09-21 06:58:09 +0000373
374 if (!defined $BugCategory) {
375 $BugCategory = "Other";
376 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000377
Ted Kremenekebb74132008-09-21 06:58:09 +0000378 push @$Index,[ $FName, $BugCategory, $BugDesc, $BugFile, $BugLine,
379 $BugPathLength ];
Ted Kremenek22d6a632008-04-02 20:43:36 +0000380}
381
382##----------------------------------------------------------------------------##
383# CopyJS - Copy JavaScript code to target directory.
384##----------------------------------------------------------------------------##
385
386sub CopyJS {
387
388 my $Dir = shift;
389
Ted Kremenek23cfca32008-06-16 22:40:14 +0000390 DieDiag("Cannot find 'sorttable.js'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000391 if (! -r "$RealBin/sorttable.js");
392
Ted Kremenek20161e92008-07-15 20:18:21 +0000393 system ("cp", "$RealBin/sorttable.js", "$Dir");
Ted Kremenek22d6a632008-04-02 20:43:36 +0000394
Ted Kremenek23cfca32008-06-16 22:40:14 +0000395 DieDiag("Could not copy 'sorttable.js' to '$Dir'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000396 if (! -r "$Dir/sorttable.js");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000397}
398
399##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000400# Postprocess - Postprocess the results of an analysis scan.
401##----------------------------------------------------------------------------##
402
Sam Bishopa0e22662008-04-02 03:35:43 +0000403sub Postprocess {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000404
405 my $Dir = shift;
Ted Kremenek684bb092008-04-18 15:18:20 +0000406 my $BaseDir = shift;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000407
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000408 die "No directory specified." if (!defined $Dir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000409
410 if (! -d $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000411 Diag("No bugs found.\n");
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000412 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000413 }
414
415 opendir(DIR, $Dir);
Ted Kremenek991c54b2008-08-08 20:46:42 +0000416 my $Crashes = 0;
417 my @files = grep { if ($_ eq "crashes") { $Crashes++; }
418 /^report-.*\.html$/; } readdir(DIR);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000419 closedir(DIR);
420
Ted Kremenek991c54b2008-08-08 20:46:42 +0000421 if (scalar(@files) == 0 and $Crashes == 0) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000422 Diag("Removing directory '$Dir' because it contains no reports.\n");
Ted Kremenek20161e92008-07-15 20:18:21 +0000423 system ("rm", "-fR", $Dir);
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000424 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000425 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000426
Ted Kremenek991c54b2008-08-08 20:46:42 +0000427 # Scan each report file and build an index.
428 my @Index;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000429 foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); }
430
Ted Kremenekd52e4252008-08-25 20:45:07 +0000431 # Scan the crashes directory and use the information in the .info files
432 # to update the common prefix directory.
433 if (-d "$Dir/crashes") {
434 opendir(DIR, "$Dir/crashes");
435 my @files = grep { /[.]info$/; } readdir(DIR);
436 closedir(DIR);
437 foreach my $file (@files) {
438 open IN, "$Dir/crashes/$file" or DieDiag("cannot open $file\n");
439 my $Path = <IN>;
440 if (defined $Path) { UpdatePrefix($Path); }
441 close IN;
442 }
443 }
444
Ted Kremenek63c20172008-08-04 17:34:06 +0000445 # Generate an index.html file.
446 my $FName = "$Dir/index.html";
447 open(OUT, ">", $FName) or DieDiag("Cannot create file '$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000448
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000449 # Print out the header.
450
Ted Kremenek5744dc22008-04-02 18:03:36 +0000451print OUT <<ENDTEXT;
452<html>
453<head>
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000454<style type="text/css">
455 body { color:#000000; background-color:#ffffff }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000456 body { font-family: Helvetica, sans-serif; font-size:9pt }
Ted Kremenekebb74132008-09-21 06:58:09 +0000457 h3 { font-size:12pt }
458 table { font-size:9pt }
459 table { border-spacing: 0px; border: 1px solid black }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000460 table thead {
Ted Kremenek22d6a632008-04-02 20:43:36 +0000461 background-color:#eee; color:#666666;
462 font-weight: bold; cursor: default;
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000463 text-align:center;
Ted Kremenekebb74132008-09-21 06:58:09 +0000464 font-weight: bold; font-family: Verdana;
465 white-space:nowrap;
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000466 }
Ted Kremenekebb74132008-09-21 06:58:09 +0000467 .W { font-size:0px }
468 th, td { padding:5px; padding-left:8px; text-align:left }
469 td.SUMM_DESC { padding-left:12px }
470 td.DESC { white-space:pre }
471 td.Q { text-align:right }
472 td { text-align:left }
473 td.View a { white-space: nowrap; -webkit-appearance:square-button; padding-left:1em; padding-right:1em; padding-top:0.5ex; padding-bottom:0.5ex; text-decoration:none; color:black }
474 tbody.scrollContent { overflow:auto }
475}
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000476</style>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000477<script src="sorttable.js"></script>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000478<script language='javascript' type="text/javascript">
479function SetDisplay(RowClass, DisplayVal)
480{
481 var Rows = document.getElementsByTagName("tr");
482 for ( var i = 0 ; i < Rows.length; ++i ) {
483 if (Rows[i].className == RowClass) {
484 Rows[i].style.display = DisplayVal;
485 }
486 }
487}
Ted Kremenekebb74132008-09-21 06:58:09 +0000488
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000489function ToggleDisplay(CheckButton, ClassName) {
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000490 if (CheckButton.checked) {
491 SetDisplay(ClassName, "");
492 }
493 else {
494 SetDisplay(ClassName, "none");
495 }
496}
497</script>
498</head>
499<body>
500ENDTEXT
501
Ted Kremenek991c54b2008-08-08 20:46:42 +0000502 if (scalar(@files)) {
503 # Print out the summary table.
504 my %Totals;
Ted Kremenekebb74132008-09-21 06:58:09 +0000505
Ted Kremenek991c54b2008-08-08 20:46:42 +0000506 for my $row ( @Index ) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000507 my $bug_type = ($row->[2]);
508 my $bug_category = ($row->[1]);
509 my $key = "$bug_category:$bug_type";
510
511 if (!defined $Totals{$key}) { $Totals{$key} = [1,$bug_category,$bug_type]; }
512 else { $Totals{$key}->[0]++; }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000513 }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000514
515 print OUT "<h3>Bug Summary</h3>";
516
517 if (defined $BuildName) {
518 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 +0000519 }
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000520
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000521print OUT <<ENDTEXT;
Ted Kremenekebb74132008-09-21 06:58:09 +0000522<table>
523<thead><tr><td>Bug Type</td><td>Quantity</td><td class="sorttable_nosort">Display?</td></tr></thead>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000524ENDTEXT
525
Ted Kremenekebb74132008-09-21 06:58:09 +0000526 my $last_category;
527
528 for my $key (
529 sort {
530 my $x = $Totals{$a};
531 my $y = $Totals{$b};
532 my $res = $x->[1] cmp $y->[1];
533 $res = $x->[2] cmp $y->[2] if ($res == 0);
534 $res
535 } keys %Totals )
536 {
537 my $val = $Totals{$key};
538 my $category = $val->[1];
539 if (!defined $last_category or $last_category ne $category) {
540 $last_category = $category;
541 print OUT "<tr><th>$category</th><th colspan=2></th></tr>\n";
542 }
543 my $x = lc $key;
544 $x =~ s/[ ,'":\/()]+/_/g;
545 print OUT "<tr><td class=\"SUMM_DESC\">";
546 print OUT $val->[2];
547 print OUT "</td><td>";
548 print OUT $val->[0];
549 print OUT "</td><td><center><input type=\"checkbox\" onClick=\"ToggleDisplay(this,'bt_$x');\" checked/></center></td></tr>\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000550 }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000551
552 # Print out the table of errors.
553
554print OUT <<ENDTEXT;
555</table>
556<h3>Reports</h3>
Ted Kremenekebb74132008-09-21 06:58:09 +0000557
558<table class="sortable" style="table-layout:automatic">
559<thead><tr>
560 <td>Bug Group</td>
561 <td class="sorttable_sorted">Bug Type<span id="sorttable_sortfwdind">&nbsp;&#x25BE;</span></td>
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000562 <td>File</td>
Ted Kremenekebb74132008-09-21 06:58:09 +0000563 <td class="Q">Line</td>
564 <td class="Q">Path Length</td>
Ted Kremenek2645c772008-07-07 16:58:44 +0000565 <td class="sorttable_nosort"></td>
Ted Kremenekebb74132008-09-21 06:58:09 +0000566 <!-- REPORTBUGCOL -->
567</tr></thead>
568<tbody>
Ted Kremenek5744dc22008-04-02 18:03:36 +0000569ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000570
Ted Kremenek991c54b2008-08-08 20:46:42 +0000571 my $prefix = GetPrefix();
572 my $regex;
573 my $InFileRegex;
574 my $InFilePrefix = "File:</td><td>";
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000575
Ted Kremenek991c54b2008-08-08 20:46:42 +0000576 if (defined $prefix) {
577 $regex = qr/^\Q$prefix\E/is;
578 $InFileRegex = qr/\Q$InFilePrefix$prefix\E/is;
579 }
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000580
Ted Kremenekebb74132008-09-21 06:58:09 +0000581 for my $row ( sort { $a->[2] cmp $b->[2] } @Index ) {
582 my $x = "$row->[1]:$row->[2]";
583 $x = lc $x;
584 $x =~ s/[ ,'":\/()]+/_/g;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000585
Ted Kremenek991c54b2008-08-08 20:46:42 +0000586 my $ReportFile = $row->[0];
Ted Kremenekebb74132008-09-21 06:58:09 +0000587
588 print OUT "<tr class=\"bt_$x\">";
589 print OUT "<td class=\"DESC\">";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000590 print OUT $row->[1];
Ted Kremenekebb74132008-09-21 06:58:09 +0000591 print OUT "</td>";
592 print OUT "<td class=\"DESC\">";
593 print OUT $row->[2];
594 print OUT "</td>";
595
596 # Update the file prefix.
597 my $fname = $row->[3];
598 my $full_fname = $fname;
599
Ted Kremenek991c54b2008-08-08 20:46:42 +0000600 if (defined $regex) {
601 $fname =~ s/$regex//;
602 UpdateInFilePath("$Dir/$ReportFile", $InFileRegex, $InFilePrefix)
603 }
Ted Kremenekebb74132008-09-21 06:58:09 +0000604
605 print OUT "<td>";
606 my $has_fname = 0;
607 if (-r $full_fname) {
608 $has_fname = 1;
609 print OUT "<a href=\"$full_fname\">"
610 }
611
612 my @fname = split /\//,$fname;
613 if ($#fname > 0) {
614 while ($#fname >= 0) {
615 my $x = shift @fname;
616 print OUT $x;
617 if ($#fname >= 0) {
618 print OUT "<span class=\"W\"> </span>/";
619 }
620 }
621 }
622 else {
623 print OUT $fname;
624 }
625
626 if ($has_fname) {
627 print OUT "</a>"
628 }
629 print OUT "</td>";
630
631 # Print out the quantities.
632 for my $j ( 4 .. 5 ) {
633 print OUT "<td class=\"Q\">$row->[$j]</td>";
634 }
635
Ted Kremenek991c54b2008-08-08 20:46:42 +0000636 # Print the rest of the columns.
Ted Kremenekebb74132008-09-21 06:58:09 +0000637 for (my $j = 6; $j <= $#{$row}; ++$j) {
638 print OUT "<td>$row->[$j]</td>"
Ted Kremenek991c54b2008-08-08 20:46:42 +0000639 }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000640
Ted Kremenek991c54b2008-08-08 20:46:42 +0000641 # Emit the "View" link.
Ted Kremenekebb74132008-09-21 06:58:09 +0000642 print OUT "<td class=\"View\"><a href=\"$ReportFile#EndPath\">View Report</a></td>";
Ted Kremenek3cea9ee2008-07-30 17:58:08 +0000643
Daniel Dunbare43038e2008-09-19 23:18:44 +0000644 # Emit REPORTBUG markers.
Ted Kremenekebb74132008-09-21 06:58:09 +0000645 print OUT "\n<!-- REPORTBUG id=\"$ReportFile\" -->\n";
Daniel Dunbare43038e2008-09-19 23:18:44 +0000646
Ted Kremenek991c54b2008-08-08 20:46:42 +0000647 # End the row.
648 print OUT "</tr>\n";
649 }
650
Ted Kremenekebb74132008-09-21 06:58:09 +0000651 print OUT "</tbody>\n</table>\n\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000652 }
653
654 if ($Crashes) {
655 # Read the crash directory for files.
656 opendir(DIR, "$Dir/crashes");
657 my @files = grep { /[.]info$/ } readdir(DIR);
658 closedir(DIR);
659
660 if (scalar(@files)) {
661 print OUT <<ENDTEXT;
Ted Kremenek5d31f832008-08-18 18:38:29 +0000662<h3>Analyzer Failures</h3>
Ted Kremenek991c54b2008-08-08 20:46:42 +0000663
Ted Kremenek5d31f832008-08-18 18:38:29 +0000664<p>The analyzer had problems processing the following files:</p>
Ted Kremenek991c54b2008-08-08 20:46:42 +0000665
666<table>
Ted Kremenek9f9b1fd2008-09-12 22:49:36 +0000667<thead><tr><td>Problem</td><td>Source File</td><td>Preprocessed File</td><td>STDERR Output</td></tr></thead>
Ted Kremenek991c54b2008-08-08 20:46:42 +0000668ENDTEXT
669
670 foreach my $file (sort @files) {
671 $file =~ /(.+).info$/;
672 # Get the preprocessed file.
673 my $ppfile = $1;
674 # Open the info file and get the name of the source file.
675 open (INFO, "$Dir/crashes/$file") or
676 die "Cannot open $Dir/crashes/$file\n";
677 my $srcfile = <INFO>;
Ted Kremenek5d31f832008-08-18 18:38:29 +0000678 chomp $srcfile;
679 my $problem = <INFO>;
680 chomp $problem;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000681 close (INFO);
682 # Print the information in the table.
Ted Kremenekd52e4252008-08-25 20:45:07 +0000683 my $prefix = GetPrefix();
Ted Kremenek9f9b1fd2008-09-12 22:49:36 +0000684 if (defined $prefix) { $srcfile =~ s/^\Q$prefix//; }
685 print OUT "<tr><td>$problem</td><td>$srcfile</td><td><a href=\"crashes/$ppfile\">$ppfile</a></td><td><a href=\"crashes/$ppfile.stderr.txt\">$ppfile.stderr.txt</a></td></tr>\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000686 }
687
688 print OUT <<ENDTEXT;
689</table>
690<p>Please consider submitting preprocessed files as <a href="http://clang.llvm.org/StaticAnalysisUsage.html#filingbugs">bug reports</a>.</p>
691ENDTEXT
692 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000693 }
694
Ted Kremenek991c54b2008-08-08 20:46:42 +0000695 print OUT "</body></html>\n";
Ted Kremenek5744dc22008-04-02 18:03:36 +0000696 close(OUT);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000697 CopyJS($Dir);
Ted Kremenek20161e92008-07-15 20:18:21 +0000698
699 # Make sure $Dir and $BaseDir are world readable/executable.
700 system("chmod", "755", $Dir);
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000701 if (defined $BaseDir) { system("chmod", "755", $BaseDir); }
Ted Kremenek20161e92008-07-15 20:18:21 +0000702
Ted Kremenek23cfca32008-06-16 22:40:14 +0000703 my $Num = scalar(@Index);
Ted Kremenek150c2122008-07-11 19:15:05 +0000704 Diag("$Num bugs found.\n");
705 if ($Num > 0 && -r "$Dir/index.html") {
706 Diag("Open '$Dir/index.html' to examine bug reports.\n");
707 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000708
Ted Kremenek991c54b2008-08-08 20:46:42 +0000709 DiagCrashes($Dir) if ($Crashes);
710
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000711 return $Num;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000712}
713
714##----------------------------------------------------------------------------##
Ted Kremenekdab11102008-04-02 04:43:42 +0000715# RunBuildCommand - Run the build command.
716##----------------------------------------------------------------------------##
717
Ted Kremenek6b628982008-04-30 23:47:12 +0000718sub AddIfNotPresent {
719 my $Args = shift;
720 my $Arg = shift;
721 my $found = 0;
722
723 foreach my $k (@$Args) {
724 if ($k eq $Arg) {
725 $found = 1;
726 last;
727 }
728 }
729
730 if ($found == 0) {
731 push @$Args, $Arg;
732 }
733}
734
Ted Kremenekdab11102008-04-02 04:43:42 +0000735sub RunBuildCommand {
736
737 my $Args = shift;
Ted Kremenek7442ca62008-04-02 16:04:51 +0000738 my $IgnoreErrors = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000739 my $Cmd = $Args->[0];
Ted Kremenek6195c372008-06-02 21:52:47 +0000740 my $CCAnalyzer = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000741
Ted Kremenek3301cb12008-06-30 18:18:16 +0000742 # Get only the part of the command after the last '/'.
743 if ($Cmd =~ /\/([^\/]+)$/) {
744 $Cmd = $1;
745 }
746
Ted Kremenek63c20172008-08-04 17:34:06 +0000747 if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc"
748 or $Cmd eq "ccc-analyzer") {
Ted Kremenekdab11102008-04-02 04:43:42 +0000749 shift @$Args;
Ted Kremenek6195c372008-06-02 21:52:47 +0000750 unshift @$Args, $CCAnalyzer;
Ted Kremenekdab11102008-04-02 04:43:42 +0000751 }
Ted Kremenek7442ca62008-04-02 16:04:51 +0000752 elsif ($IgnoreErrors) {
753 if ($Cmd eq "make" or $Cmd eq "gmake") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000754 AddIfNotPresent($Args,"-k");
Ted Kremenek8912b542008-05-13 21:28:02 +0000755 AddIfNotPresent($Args,"-i");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000756 }
757 elsif ($Cmd eq "xcodebuild") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000758 AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000759 }
Ted Kremenek6b628982008-04-30 23:47:12 +0000760 }
761
Ted Kremenek6b628982008-04-30 23:47:12 +0000762 if ($Cmd eq "xcodebuild") {
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000763 # Disable distributed builds for xcodebuild.
Ted Kremenek6b628982008-04-30 23:47:12 +0000764 AddIfNotPresent($Args,"-nodistribute");
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000765
766 # Disable PCH files until clang supports them.
767 AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO");
Ted Kremenek915e9722008-05-27 23:18:07 +0000768
769 # When 'CC' is set, xcodebuild uses it to do all linking, even if we are
770 # linking C++ object files. Set 'LDPLUSPLUS' so that xcodebuild uses 'g++'
771 # when linking such files.
Ted Kremenek95aa1052008-09-04 17:52:41 +0000772 die if (!defined $CXX);
773 my $LDPLUSPLUS = `which $CXX`;
Ted Kremenek915e9722008-05-27 23:18:07 +0000774 $LDPLUSPLUS =~ s/\015?\012//; # strip newlines
775 $ENV{'LDPLUSPLUS'} = $LDPLUSPLUS;
Ted Kremenek6b628982008-04-30 23:47:12 +0000776 }
Ted Kremenekdab11102008-04-02 04:43:42 +0000777
Ted Kremenek5a4ddaf2008-08-25 20:10:45 +0000778 return (system(@$Args) >> 8);
Ted Kremenekdab11102008-04-02 04:43:42 +0000779}
780
Ted Kremenekdab11102008-04-02 04:43:42 +0000781##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000782# DisplayHelp - Utility function to display all help options.
783##----------------------------------------------------------------------------##
784
Sam Bishopa0e22662008-04-02 03:35:43 +0000785sub DisplayHelp {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000786
Ted Kremenek5744dc22008-04-02 18:03:36 +0000787print <<ENDTEXT;
Sam Bishopa0e22662008-04-02 03:35:43 +0000788USAGE: $Prog [options] <build command> [build options]
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000789
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000790ENDTEXT
791
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000792 if (defined $BuildName) {
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000793 print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n";
794 }
795
796print <<ENDTEXT;
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000797OPTIONS:
798
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000799 -o - Target directory for HTML report files. Subdirectories
Sam Bishopa0e22662008-04-02 03:35:43 +0000800 will be created as needed to represent separate "runs" of
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000801 the analyzer. If this option is not specified, a directory
802 is created in /tmp to store the reports.
Ted Kremenek1262fc42008-05-14 20:10:33 +0000803
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000804 -h - Display this message.
805 --help
Ted Kremenek1262fc42008-05-14 20:10:33 +0000806
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000807 -k - Add a "keep on going" option to the specified build command.
808 --keep-going This option currently supports make and xcodebuild.
Ted Kremenekf02e8db2008-04-02 16:41:25 +0000809 This is a convenience option; one can specify this
810 behavior directly using build options.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000811
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000812 --status-bugs - By default, the exit status of $Prog is the same as the
813 executed build command. Specifying this option causes the
814 exit status of $Prog to be 1 if it found potential bugs
815 and 0 otherwise.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000816
Ted Kremenek386c6932008-09-03 17:59:35 +0000817 --use-cc [compiler path] - By default, $Prog uses 'gcc' to compile and link
818 --use-cc=[compiler path] your C and Objective-C code. Use this option
819 to specify an alternate compiler.
820
821 --use-c++ [compiler path] - By default, $Prog uses 'g++' to compile and link
822 --use-c++=[compiler path] your C++ and Objective-C++ code. Use this option
823 to specify an alternate compiler.
Ted Kremenekf17ef3c2008-08-21 21:47:09 +0000824
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000825 -v - Verbose output from $Prog and the analyzer.
Ted Kremenek386c6932008-09-03 17:59:35 +0000826 A second and third '-v' increases verbosity.
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000827
828 -V - View analysis results in a web browser when the build
829 --view completes.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000830
Ted Kremenekb7770c02008-07-15 17:06:13 +0000831
Ted Kremenek386c6932008-09-03 17:59:35 +0000832AVAILABLE ANALYSES (multiple analyses may be specified):
Ted Kremenekd52e4252008-08-25 20:45:07 +0000833
834ENDTEXT
Ted Kremenekb7770c02008-07-15 17:06:13 +0000835
836 foreach my $Analysis (sort keys %AvailableAnalyses) {
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000837 if (defined $AnalysesDefaultEnabled{$Analysis}) {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000838 print " (+)";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000839 }
840 else {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000841 print " ";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000842 }
843
844 print " $Analysis $AvailableAnalyses{$Analysis}\n";
845 }
846
847print <<ENDTEXT
848
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000849 NOTE: "(+)" indicates that an analysis is enabled by default unless one
850 or more analysis options are specified
Ted Kremenekb7770c02008-07-15 17:06:13 +0000851
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000852BUILD OPTIONS
853
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000854 You can specify any build option acceptable to the build command.
Ted Kremenek39eefde2008-04-02 16:47:27 +0000855
Ted Kremenek5744dc22008-04-02 18:03:36 +0000856EXAMPLE
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000857
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000858 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000859
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000860 The above example causes analysis reports to be deposited into
861 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
862 A different subdirectory is created each time $Prog analyzes a project.
863 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000864
865ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000866}
867
868##----------------------------------------------------------------------------##
869# Process command-line arguments.
870##----------------------------------------------------------------------------##
871
872my $HtmlDir; # Parent directory to store HTML files.
873my $IgnoreErrors = 0; # Ignore build errors.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000874my $ViewResults = 0; # View results when the build terminates.
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000875my $ExitStatusFoundBugs = 0; # Exit status reflects whether bugs were found
Ted Kremenekb7770c02008-07-15 17:06:13 +0000876my @AnalysesToRun;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000877
878if (!@ARGV) {
879 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000880 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000881}
882
883while (@ARGV) {
884
885 # Scan for options we recognize.
886
887 my $arg = $ARGV[0];
888
Sam Bishop2f2418e2008-04-03 14:29:47 +0000889 if ($arg eq "-h" or $arg eq "--help") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000890 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000891 exit 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000892 }
893
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000894 if (defined $AvailableAnalyses{$arg}) {
Ted Kremenek1262fc42008-05-14 20:10:33 +0000895 shift @ARGV;
Ted Kremenekb7770c02008-07-15 17:06:13 +0000896 push @AnalysesToRun, $arg;
Ted Kremenek1262fc42008-05-14 20:10:33 +0000897 next;
898 }
899
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000900 if ($arg eq "-o") {
901 shift @ARGV;
902
903 if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000904 DieDiag("'-o' option requires a target directory name.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000905 }
906
907 $HtmlDir = shift @ARGV;
908 next;
909 }
910
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000911 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000912 shift @ARGV;
913 $IgnoreErrors = 1;
914 next;
915 }
916
Ted Kremenekf17ef3c2008-08-21 21:47:09 +0000917 if ($arg =~ /^--use-cc(=(.+))?$/) {
918 shift @ARGV;
919 my $cc;
920
921 if ($2 eq "") {
922 if (!@ARGV) {
923 DieDiag("'--use-cc' option requires a compiler executable name.\n");
924 }
925 $cc = shift @ARGV;
926 }
927 else {
928 $cc = $2;
929 }
930
931 $ENV{"CCC_CC"} = $cc;
932 next;
933 }
934
Ted Kremenek386c6932008-09-03 17:59:35 +0000935 if ($arg =~ /^--use-c[+][+](=(.+))?$/) {
936 shift @ARGV;
937
938 if ($2 eq "") {
939 if (!@ARGV) {
940 DieDiag("'--use-c++' option requires a compiler executable name.\n");
941 }
942 $CXX = shift @ARGV;
943 }
944 else {
945 $CXX = $2;
946 }
947 next;
948 }
949
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000950 if ($arg eq "-v") {
951 shift @ARGV;
952 $Verbose++;
953 next;
954 }
955
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000956 if ($arg eq "-V" or $arg eq "--view") {
957 shift @ARGV;
958 $ViewResults = 1;
959 next;
960 }
961
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000962 if ($arg eq "--status-bugs") {
963 shift @ARGV;
964 $ExitStatusFoundBugs = 1;
965 next;
966 }
967
Ted Kremenek23cfca32008-06-16 22:40:14 +0000968 DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/);
Ted Kremenek0062ad42008-04-02 16:35:01 +0000969
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000970 last;
971}
972
973if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000974 Diag("No build command specified.\n\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000975 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000976 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000977}
978
Ted Kremenek386c6932008-09-03 17:59:35 +0000979
980
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000981# Determine the output directory for the HTML reports.
Ted Kremenek684bb092008-04-18 15:18:20 +0000982my $BaseDir = $HtmlDir;
Sam Bishopa0e22662008-04-02 03:35:43 +0000983$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000984
985# Set the appropriate environment variables.
Sam Bishopa0e22662008-04-02 03:35:43 +0000986SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000987
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000988my $Cmd = "$RealBin/ccc-analyzer";
989
Ted Kremenek23cfca32008-06-16 22:40:14 +0000990DieDiag("Executable 'ccc-analyzer' does not exist at '$Cmd'\n")
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000991 if (! -x $Cmd);
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000992
Ted Kremenekb7770c02008-07-15 17:06:13 +0000993if (! -x $ClangSB) {
994 Diag("'clang' executable not found in '$RealBin'.\n");
995 Diag("Using 'clang' from path.\n");
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000996}
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000997
Ted Kremenek95aa1052008-09-04 17:52:41 +0000998if (defined $CXX) {
999 $ENV{'CXX'} = $CXX;
1000}
1001else {
1002 $CXX = 'g++'; # This variable is used by other parts of scan-build
1003 # that need to know a default C++ compiler to fall back to.
1004}
1005
Ted Kremenek4f4b17d2008-04-03 20:08:18 +00001006$ENV{'CC'} = $Cmd;
Ted Kremenekf22eacb2008-04-18 22:00:56 +00001007$ENV{'CLANG'} = $Clang;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001008
1009if ($Verbose >= 2) {
1010 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
1011}
1012
Ted Kremeneka9525c92008-05-12 22:07:14 +00001013if ($Verbose >= 3) {
1014 $ENV{'CCC_ANALYZER_LOG'} = 1;
1015}
1016
Ted Kremenek90125992008-07-15 23:41:32 +00001017if (scalar(@AnalysesToRun) == 0) {
1018 foreach my $key (keys %AnalysesDefaultEnabled) {
1019 push @AnalysesToRun,$key;
1020 }
Ted Kremenek01006782008-07-02 23:16:10 +00001021}
Ted Kremenek1262fc42008-05-14 20:10:33 +00001022
Ted Kremenek90125992008-07-15 23:41:32 +00001023$ENV{'CCC_ANALYZER_ANALYSIS'} = join ' ',@AnalysesToRun;
1024
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001025# Run the build.
Ted Kremenek5656a982008-07-15 17:09:28 +00001026my $ExitStatus = RunBuildCommand(\@ARGV, $IgnoreErrors, $Cmd);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001027
1028# Postprocess the HTML directory.
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001029my $NumBugs = Postprocess($HtmlDir, $BaseDir);
Ted Kremenek7f8a3252008-04-02 18:42:49 +00001030
1031if ($ViewResults and -r "$HtmlDir/index.html") {
1032 # Only works on Mac OS X (for now).
1033 print "Viewing analysis results: '$HtmlDir/index.html'\n";
Ted Kremenek20161e92008-07-15 20:18:21 +00001034 system("open", "$HtmlDir/index.html");
Ted Kremenek7f8a3252008-04-02 18:42:49 +00001035}
Ted Kremenek5656a982008-07-15 17:09:28 +00001036
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001037if ($ExitStatusFoundBugs) {
1038 exit 1 if ($NumBugs > 0);
1039 exit 0;
1040}
1041
Ted Kremenek5656a982008-07-15 17:09:28 +00001042exit $ExitStatus;
1043