blob: dbdb240b4974c26dd38760d0d327a9d23e0c5924 [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 Kremenek7cba1122008-09-22 01:35:58 +000022use Cwd;
23use Sys::Hostname;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000024
25my $Verbose = 0; # Verbose output from this script.
26my $Prog = "scan-build";
Ted Kremenekf4cdf412008-05-23 18:17:05 +000027my $BuildName;
28my $BuildDate;
Ted Kremenek95aa1052008-09-04 17:52:41 +000029my $CXX; # Leave undefined initially.
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000030
Ted Kremenek0e689382008-09-11 18:17:51 +000031my $TERM = $ENV{'TERM'};
32my $UseColor = (defined $TERM and $TERM eq 'xterm-color' and -t STDOUT
33 and defined $ENV{'SCAN_BUILD_COLOR'});
Ted Kremenek23cfca32008-06-16 22:40:14 +000034
Ted Kremenek7cba1122008-09-22 01:35:58 +000035my $UserName = HtmlEscape(getpwuid($<) || 'unknown');
36my $HostName = HtmlEscape(hostname() || 'unknown');
37my $CurrentDir = HtmlEscape(getcwd());
38my $CurrentDirSuffix = basename($CurrentDir);
39
40my $CmdArgs;
41
42my $HtmlTitle;
43
44my $Date = localtime();
45
Ted Kremenekb7770c02008-07-15 17:06:13 +000046##----------------------------------------------------------------------------##
47# Diagnostics
48##----------------------------------------------------------------------------##
49
Ted Kremenek23cfca32008-06-16 22:40:14 +000050sub Diag {
51 if ($UseColor) {
52 print BOLD, MAGENTA "$Prog: @_";
53 print RESET;
54 }
55 else {
56 print "$Prog: @_";
57 }
58}
59
Ted Kremenek991c54b2008-08-08 20:46:42 +000060sub DiagCrashes {
61 my $Dir = shift;
62 Diag ("The analyzer crashed on some source files.\n");
Ted Kremenek386c6932008-09-03 17:59:35 +000063 Diag ("Preprocessed versions of crashed files were deposited in '$Dir/crashes'.\n");
Ted Kremenek991c54b2008-08-08 20:46:42 +000064 Diag ("Please consider submitting a bug report using these files:\n");
65 Diag (" http://clang.llvm.org/StaticAnalysisUsage.html#filingbugs\n")
66}
67
Ted Kremenek23cfca32008-06-16 22:40:14 +000068sub DieDiag {
69 if ($UseColor) {
70 print BOLD, RED "$Prog: ";
71 print RESET, RED @_;
72 print RESET;
73 }
74 else {
75 print "$Prog: ", @_;
76 }
77 exit(0);
78}
79
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000080##----------------------------------------------------------------------------##
Ted Kremenekb7770c02008-07-15 17:06:13 +000081# Some initial preprocessing of Clang options.
82##----------------------------------------------------------------------------##
83
Ted Kremeneke15fa272008-10-13 21:46:42 +000084my $ClangSB = Cwd::realpath("$RealBin/clang");
Ted Kremenekb7770c02008-07-15 17:06:13 +000085my $Clang = $ClangSB;
86
87if (! -x $ClangSB) {
88 $Clang = "clang";
89}
90
91my %AvailableAnalyses;
92
93# Query clang for analysis options.
Ted Kremenek63c20172008-08-04 17:34:06 +000094open(PIPE, "-|", $Clang, "--help") or
Ted Kremenek445fa772008-10-10 00:17:08 +000095 DieDiag("Cannot execute '$Clang'\n");
Ted Kremenek63c20172008-08-04 17:34:06 +000096
Ted Kremenekb7770c02008-07-15 17:06:13 +000097my $FoundAnalysis = 0;
98
99while(<PIPE>) {
100 if ($FoundAnalysis == 0) {
Ted Kremenekce1448b2008-10-24 15:11:58 +0000101 if (/SCA Checks\/Analyses/) {
Ted Kremenekb7770c02008-07-15 17:06:13 +0000102 $FoundAnalysis = 1;
103 }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000104
Ted Kremenekb7770c02008-07-15 17:06:13 +0000105 next;
106 }
107
108 if (/^\s\s\s\s([^\s]+)\s(.+)$/) {
109 next if ($1 =~ /-dump/ or $1 =~ /-view/
110 or $1 =~ /-checker-simple/ or $1 =~ /-warn-uninit/);
111
112 $AvailableAnalyses{$1} = $2;
113 next;
114 }
115
116 last;
117}
118
119close (PIPE);
120
121my %AnalysesDefaultEnabled = (
122 '-warn-dead-stores' => 1,
123 '-checker-cfref' => 1,
Ted Kremenek90125992008-07-15 23:41:32 +0000124 '-warn-objc-methodsigs' => 1,
Ted Kremenekbde3a052008-07-25 20:35:01 +0000125 '-warn-objc-missing-dealloc' => 1,
Ted Kremenek5d443492008-09-18 06:34:16 +0000126 '-warn-objc-unused-ivars' => 1,
Ted Kremenekb7770c02008-07-15 17:06:13 +0000127);
128
129##----------------------------------------------------------------------------##
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000130# GetHTMLRunDir - Construct an HTML directory name for the current sub-run.
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000131##----------------------------------------------------------------------------##
132
Sam Bishopa0e22662008-04-02 03:35:43 +0000133sub GetHTMLRunDir {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000134
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000135 die "Not enough arguments." if (@_ == 0);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000136 my $Dir = shift @_;
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000137
138 my $TmpMode = 0;
139 if (!defined $Dir) {
140 $Dir = "/tmp";
141 $TmpMode = 1;
142 }
143
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000144 # Get current date and time.
145
146 my @CurrentTime = localtime();
147
148 my $year = $CurrentTime[5] + 1900;
149 my $day = $CurrentTime[3];
150 my $month = $CurrentTime[4] + 1;
151
Ted Kremenek9d7405f2008-05-14 17:23:56 +0000152 my $DateString = sprintf("%d-%02d-%02d", $year, $month, $day);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000153
154 # Determine the run number.
155
156 my $RunNumber;
157
158 if (-d $Dir) {
159
160 if (! -r $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000161 DieDiag("directory '$Dir' exists but is not readable.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000162 }
163
164 # Iterate over all files in the specified directory.
165
166 my $max = 0;
167
168 opendir(DIR, $Dir);
Ted Kremenek29da6c52008-08-07 17:57:34 +0000169 my @FILES = grep { -d "$Dir/$_" } readdir(DIR);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000170 closedir(DIR);
171
172 foreach my $f (@FILES) {
173
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000174 # Strip the prefix '$Prog-' if we are dumping files to /tmp.
175 if ($TmpMode) {
176 next if (!($f =~ /^$Prog-(.+)/));
177 $f = $1;
178 }
179
Ted Kremenekebb74132008-09-21 06:58:09 +0000180
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000181 my @x = split/-/, $f;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000182 next if (scalar(@x) != 4);
183 next if ($x[0] != $year);
184 next if ($x[1] != $month);
185 next if ($x[2] != $day);
186
187 if ($x[3] > $max) {
188 $max = $x[3];
189 }
190 }
191
192 $RunNumber = $max + 1;
193 }
194 else {
195
196 if (-x $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000197 DieDiag("'$Dir' exists but is not a directory.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000198 }
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000199
200 if ($TmpMode) {
Ted Kremenek445fa772008-10-10 00:17:08 +0000201 DieDiag("The directory '/tmp' does not exist or cannot be accessed.\n");
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000202 }
203
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000204 # $Dir does not exist. It will be automatically created by the
205 # clang driver. Set the run number to 1.
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000206
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000207 $RunNumber = 1;
208 }
209
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000210 die "RunNumber must be defined!" if (!defined $RunNumber);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000211
212 # Append the run number.
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000213 my $NewDir;
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000214 if ($TmpMode) {
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000215 $NewDir = "$Dir/$Prog-$DateString-$RunNumber";
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000216 }
217 else {
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000218 $NewDir = "$Dir/$DateString-$RunNumber";
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000219 }
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000220 system 'mkdir','-p',$NewDir;
221 return $NewDir;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000222}
223
Sam Bishopa0e22662008-04-02 03:35:43 +0000224sub SetHtmlEnv {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000225
226 die "Wrong number of arguments." if (scalar(@_) != 2);
227
228 my $Args = shift;
229 my $Dir = shift;
230
231 die "No build command." if (scalar(@$Args) == 0);
232
233 my $Cmd = $$Args[0];
234
235 if ($Cmd =~ /configure/) {
236 return;
237 }
238
239 if ($Verbose) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000240 Diag("Emitting reports for this run to '$Dir'.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000241 }
242
243 $ENV{'CCC_ANALYZER_HTML'} = $Dir;
244}
245
246##----------------------------------------------------------------------------##
Ted Kremenek57cf4462008-04-18 15:09:30 +0000247# ComputeDigest - Compute a digest of the specified file.
248##----------------------------------------------------------------------------##
249
250sub ComputeDigest {
251 my $FName = shift;
Ted Kremenek23cfca32008-06-16 22:40:14 +0000252 DieDiag("Cannot read $FName to compute Digest.\n") if (! -r $FName);
Ted Kremeneka6e24812008-04-19 18:05:48 +0000253
254 # Use Digest::MD5. We don't have to be cryptographically secure. We're
Ted Kremenek7ea02e62008-04-19 18:07:44 +0000255 # just looking for duplicate files that come from a non-malicious source.
256 # We use Digest::MD5 because it is a standard Perl module that should
Ted Kremenek63c20172008-08-04 17:34:06 +0000257 # come bundled on most systems.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000258 open(FILE, $FName) or DieDiag("Cannot open $FName when computing Digest.\n");
Ted Kremeneka6e24812008-04-19 18:05:48 +0000259 binmode FILE;
260 my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest;
261 close(FILE);
262
Ted Kremenek63c20172008-08-04 17:34:06 +0000263 # Return the digest.
Ted Kremeneka6e24812008-04-19 18:05:48 +0000264 return $Result;
Ted Kremenek57cf4462008-04-18 15:09:30 +0000265}
266
267##----------------------------------------------------------------------------##
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000268# UpdatePrefix - Compute the common prefix of files.
269##----------------------------------------------------------------------------##
270
271my $Prefix;
272
273sub UpdatePrefix {
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000274 my $x = shift;
275 my $y = basename($x);
276 $x =~ s/\Q$y\E$//;
277
278 # Ignore /usr, /Library, /System, /Developer
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000279 return if ( $x =~ /^\/usr/ or $x =~ /^\/Library/
280 or $x =~ /^\/System/ or $x =~ /^\/Developer/);
281
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000282 if (!defined $Prefix) {
283 $Prefix = $x;
284 return;
285 }
286
Ted Kremenek20b2bae2008-09-11 21:15:10 +0000287 chop $Prefix while (!($x =~ /^\Q$Prefix/));
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000288}
289
290sub GetPrefix {
291 return $Prefix;
292}
293
294##----------------------------------------------------------------------------##
295# UpdateInFilePath - Update the path in the report file.
296##----------------------------------------------------------------------------##
297
298sub UpdateInFilePath {
299 my $fname = shift;
300 my $regex = shift;
301 my $newtext = shift;
Ted Kremenek63c20172008-08-04 17:34:06 +0000302
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000303 open (RIN, $fname) or die "cannot open $fname";
Ted Kremenek63c20172008-08-04 17:34:06 +0000304 open (ROUT, ">", "$fname.tmp") or die "cannot open $fname.tmp";
305
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000306 while (<RIN>) {
307 s/$regex/$newtext/;
308 print ROUT $_;
309 }
Ted Kremenek63c20172008-08-04 17:34:06 +0000310
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000311 close (ROUT);
312 close (RIN);
Ted Kremenek20161e92008-07-15 20:18:21 +0000313 system("mv", "$fname.tmp", $fname);
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000314}
315
316##----------------------------------------------------------------------------##
Ted Kremenek5744dc22008-04-02 18:03:36 +0000317# ScanFile - Scan a report file for various identifying attributes.
318##----------------------------------------------------------------------------##
319
Ted Kremenek57cf4462008-04-18 15:09:30 +0000320# Sometimes a source file is scanned more than once, and thus produces
321# multiple error reports. We use a cache to solve this problem.
322
323my %AlreadyScanned;
324
Ted Kremenek5744dc22008-04-02 18:03:36 +0000325sub ScanFile {
326
327 my $Index = shift;
328 my $Dir = shift;
329 my $FName = shift;
330
Ted Kremenek57cf4462008-04-18 15:09:30 +0000331 # Compute a digest for the report file. Determine if we have already
332 # scanned a file that looks just like it.
333
334 my $digest = ComputeDigest("$Dir/$FName");
335
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000336 if (defined $AlreadyScanned{$digest}) {
Ted Kremenek57cf4462008-04-18 15:09:30 +0000337 # Redundant file. Remove it.
Ted Kremenek20161e92008-07-15 20:18:21 +0000338 system ("rm", "-f", "$Dir/$FName");
Ted Kremenek57cf4462008-04-18 15:09:30 +0000339 return;
340 }
341
342 $AlreadyScanned{$digest} = 1;
343
Ted Kremenek809709f2008-04-18 16:58:34 +0000344 # At this point the report file is not world readable. Make it happen.
Ted Kremenek20161e92008-07-15 20:18:21 +0000345 system ("chmod", "644", "$Dir/$FName");
Ted Kremenek684bb092008-04-18 15:18:20 +0000346
347 # Scan the report file for tags.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000348 open(IN, "$Dir/$FName") or DieDiag("Cannot open '$Dir/$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000349
350 my $BugDesc = "";
Ted Kremenek22d6a632008-04-02 20:43:36 +0000351 my $BugFile = "";
Ted Kremenekebb74132008-09-21 06:58:09 +0000352 my $BugCategory;
Ted Kremenek22d6a632008-04-02 20:43:36 +0000353 my $BugPathLength = 1;
354 my $BugLine = 0;
Ted Kremenekebb74132008-09-21 06:58:09 +0000355 my $found = 0;
356
Ted Kremenek5744dc22008-04-02 18:03:36 +0000357 while (<IN>) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000358
359 last if ($found == 5);
360
Ted Kremenek5744dc22008-04-02 18:03:36 +0000361 if (/<!-- BUGDESC (.*) -->$/) {
362 $BugDesc = $1;
Ted Kremenekebb74132008-09-21 06:58:09 +0000363 ++$found;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000364 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000365 elsif (/<!-- BUGFILE (.*) -->$/) {
366 $BugFile = $1;
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000367 UpdatePrefix($BugFile);
Ted Kremenekebb74132008-09-21 06:58:09 +0000368 ++$found;
Ted Kremenek22d6a632008-04-02 20:43:36 +0000369 }
370 elsif (/<!-- BUGPATHLENGTH (.*) -->$/) {
371 $BugPathLength = $1;
Ted Kremenekebb74132008-09-21 06:58:09 +0000372 ++$found;
Ted Kremenek22d6a632008-04-02 20:43:36 +0000373 }
374 elsif (/<!-- BUGLINE (.*) -->$/) {
375 $BugLine = $1;
Ted Kremenekebb74132008-09-21 06:58:09 +0000376 ++$found;
377 }
378 elsif (/<!-- BUGCATEGORY (.*) -->$/) {
379 $BugCategory = $1;
380 ++$found;
Ted Kremenek22d6a632008-04-02 20:43:36 +0000381 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000382 }
383
384 close(IN);
Ted Kremenekebb74132008-09-21 06:58:09 +0000385
386 if (!defined $BugCategory) {
387 $BugCategory = "Other";
388 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000389
Ted Kremenek81983112008-09-28 04:13:09 +0000390 push @$Index,[ $FName, $BugCategory, $BugDesc, $BugFile, $BugLine,
391 $BugPathLength ];
Ted Kremenek22d6a632008-04-02 20:43:36 +0000392}
393
394##----------------------------------------------------------------------------##
Ted Kremenek3ce12072008-09-22 17:50:47 +0000395# CopyFiles - Copy resource files to target directory.
Ted Kremenek22d6a632008-04-02 20:43:36 +0000396##----------------------------------------------------------------------------##
397
Ted Kremenek3ce12072008-09-22 17:50:47 +0000398sub CopyFiles {
Ted Kremenek22d6a632008-04-02 20:43:36 +0000399
400 my $Dir = shift;
Ted Kremeneke15fa272008-10-13 21:46:42 +0000401
402 my $JS = Cwd::realpath("$RealBin/sorttable.js");
Ted Kremenek22d6a632008-04-02 20:43:36 +0000403
Ted Kremenek23cfca32008-06-16 22:40:14 +0000404 DieDiag("Cannot find 'sorttable.js'.\n")
Ted Kremeneke15fa272008-10-13 21:46:42 +0000405 if (! -r $JS);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000406
Ted Kremeneke15fa272008-10-13 21:46:42 +0000407 system ("cp", $JS, "$Dir");
Ted Kremenek22d6a632008-04-02 20:43:36 +0000408
Ted Kremenek23cfca32008-06-16 22:40:14 +0000409 DieDiag("Could not copy 'sorttable.js' to '$Dir'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000410 if (! -r "$Dir/sorttable.js");
Ted Kremenek3ce12072008-09-22 17:50:47 +0000411
Ted Kremeneke15fa272008-10-13 21:46:42 +0000412 my $CSS = Cwd::realpath("$RealBin/scanview.css");
413
Ted Kremenek3ce12072008-09-22 17:50:47 +0000414 DieDiag("Cannot find 'scanview.css'.\n")
Ted Kremeneke15fa272008-10-13 21:46:42 +0000415 if (! -r $CSS);
Ted Kremenek3ce12072008-09-22 17:50:47 +0000416
Ted Kremeneke15fa272008-10-13 21:46:42 +0000417 system ("cp", $CSS, "$Dir");
Ted Kremenek3ce12072008-09-22 17:50:47 +0000418
419 DieDiag("Could not copy 'scanview.css' to '$Dir'.\n")
Ted Kremeneke15fa272008-10-13 21:46:42 +0000420 if (! -r $CSS);
Ted Kremenek5744dc22008-04-02 18:03:36 +0000421}
422
423##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000424# Postprocess - Postprocess the results of an analysis scan.
425##----------------------------------------------------------------------------##
426
Sam Bishopa0e22662008-04-02 03:35:43 +0000427sub Postprocess {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000428
429 my $Dir = shift;
Ted Kremenek684bb092008-04-18 15:18:20 +0000430 my $BaseDir = shift;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000431
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000432 die "No directory specified." if (!defined $Dir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000433
434 if (! -d $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000435 Diag("No bugs found.\n");
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000436 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000437 }
438
439 opendir(DIR, $Dir);
Ted Kremenek991c54b2008-08-08 20:46:42 +0000440 my $Crashes = 0;
441 my @files = grep { if ($_ eq "crashes") { $Crashes++; }
442 /^report-.*\.html$/; } readdir(DIR);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000443 closedir(DIR);
444
Ted Kremenek991c54b2008-08-08 20:46:42 +0000445 if (scalar(@files) == 0 and $Crashes == 0) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000446 Diag("Removing directory '$Dir' because it contains no reports.\n");
Ted Kremenek20161e92008-07-15 20:18:21 +0000447 system ("rm", "-fR", $Dir);
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000448 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000449 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000450
Ted Kremenek991c54b2008-08-08 20:46:42 +0000451 # Scan each report file and build an index.
452 my @Index;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000453 foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); }
454
Ted Kremenekd52e4252008-08-25 20:45:07 +0000455 # Scan the crashes directory and use the information in the .info files
456 # to update the common prefix directory.
457 if (-d "$Dir/crashes") {
458 opendir(DIR, "$Dir/crashes");
Ted Kremenek82a12532008-09-25 00:25:16 +0000459 my @files = grep { /[.]info.txt$/; } readdir(DIR);
Ted Kremenekd52e4252008-08-25 20:45:07 +0000460 closedir(DIR);
461 foreach my $file (@files) {
462 open IN, "$Dir/crashes/$file" or DieDiag("cannot open $file\n");
463 my $Path = <IN>;
464 if (defined $Path) { UpdatePrefix($Path); }
465 close IN;
466 }
467 }
468
Ted Kremenek63c20172008-08-04 17:34:06 +0000469 # Generate an index.html file.
470 my $FName = "$Dir/index.html";
471 open(OUT, ">", $FName) or DieDiag("Cannot create file '$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000472
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000473 # Print out the header.
474
Ted Kremenek5744dc22008-04-02 18:03:36 +0000475print OUT <<ENDTEXT;
476<html>
477<head>
Ted Kremenek7cba1122008-09-22 01:35:58 +0000478<title>${HtmlTitle}</title>
Ted Kremenekf1435452008-09-23 22:34:51 +0000479<link type="text/css" rel="stylesheet" href="scanview.css"/>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000480<script src="sorttable.js"></script>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000481<script language='javascript' type="text/javascript">
482function SetDisplay(RowClass, DisplayVal)
483{
484 var Rows = document.getElementsByTagName("tr");
485 for ( var i = 0 ; i < Rows.length; ++i ) {
486 if (Rows[i].className == RowClass) {
487 Rows[i].style.display = DisplayVal;
488 }
489 }
490}
Ted Kremenekebb74132008-09-21 06:58:09 +0000491
Ted Kremenek2350a462008-10-28 19:56:52 +0000492function CopyCheckedStateToCheckButtons(SummaryCheckButton) {
493 var Inputs = document.getElementsByTagName("input");
494 for ( var i = 0 ; i < Inputs.length; ++i ) {
495 if (Inputs[i].type == "checkbox") {
496 if(Inputs[i] != SummaryCheckButton) {
497 Inputs[i].checked = SummaryCheckButton.checked;
498 Inputs[i].onclick();
499 }
500 }
501 }
502}
503
Ted Kremenek999e1202008-10-28 20:09:57 +0000504function returnObjById( id ) {
505 if (document.getElementById)
506 var returnVar = document.getElementById(id);
507 else if (document.all)
508 var returnVar = document.all[id];
509 else if (document.layers)
510 var returnVar = document.layers[id];
511 return returnVar;
512}
513
514var NumUnchecked = 0;
515
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000516function ToggleDisplay(CheckButton, ClassName) {
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000517 if (CheckButton.checked) {
518 SetDisplay(ClassName, "");
Ted Kremenek999e1202008-10-28 20:09:57 +0000519 if (--NumUnchecked == 0) {
520 returnObjById("AllBugsCheck").checked = true;
521 }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000522 }
523 else {
524 SetDisplay(ClassName, "none");
Ted Kremenek999e1202008-10-28 20:09:57 +0000525 NumUnchecked++;
526 returnObjById("AllBugsCheck").checked = false;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000527 }
528}
529</script>
Ted Kremenek1d1abb12008-09-22 17:52:58 +0000530<!-- SUMMARYENDHEAD -->
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000531</head>
532<body>
Ted Kremenek7cba1122008-09-22 01:35:58 +0000533<h1>${HtmlTitle}</h1>
534
535<table>
536<tr><th>User:</th><td>${UserName}\@${HostName}</td></tr>
537<tr><th>Working Directory:</th><td>${CurrentDir}</td></tr>
538<tr><th>Command Line:</th><td>${CmdArgs}</td></tr>
539<tr><th>Date:</th><td>${Date}</td></tr>
540ENDTEXT
541
542print OUT "<tr><th>Version:</th><td>${BuildName} (${BuildDate})</td></tr>\n"
543 if (defined($BuildName) && defined($BuildDate));
544
545print OUT <<ENDTEXT;
546</table>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000547ENDTEXT
548
Ted Kremenek991c54b2008-08-08 20:46:42 +0000549 if (scalar(@files)) {
550 # Print out the summary table.
551 my %Totals;
Ted Kremenekebb74132008-09-21 06:58:09 +0000552
Ted Kremenek991c54b2008-08-08 20:46:42 +0000553 for my $row ( @Index ) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000554 my $bug_type = ($row->[2]);
555 my $bug_category = ($row->[1]);
556 my $key = "$bug_category:$bug_type";
557
558 if (!defined $Totals{$key}) { $Totals{$key} = [1,$bug_category,$bug_type]; }
559 else { $Totals{$key}->[0]++; }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000560 }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000561
Ted Kremenek7cba1122008-09-22 01:35:58 +0000562 print OUT "<h2>Bug Summary</h2>";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000563
564 if (defined $BuildName) {
565 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 +0000566 }
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000567
Ted Kremenek2350a462008-10-28 19:56:52 +0000568
569 my $TotalBugs = scalar(@Index);
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000570print OUT <<ENDTEXT;
Ted Kremenekebb74132008-09-21 06:58:09 +0000571<table>
572<thead><tr><td>Bug Type</td><td>Quantity</td><td class="sorttable_nosort">Display?</td></tr></thead>
Ted Kremenek999e1202008-10-28 20:09:57 +0000573<tr style="font-weight:bold"><td class="SUMM_DESC">All Bugs</td><td class="Q">$TotalBugs</td><td><center><input type="checkbox" id="AllBugsCheck" onClick="CopyCheckedStateToCheckButtons(this);" checked/></center></td></tr>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000574ENDTEXT
575
Ted Kremenekebb74132008-09-21 06:58:09 +0000576 my $last_category;
577
578 for my $key (
579 sort {
580 my $x = $Totals{$a};
581 my $y = $Totals{$b};
582 my $res = $x->[1] cmp $y->[1];
583 $res = $x->[2] cmp $y->[2] if ($res == 0);
584 $res
585 } keys %Totals )
586 {
587 my $val = $Totals{$key};
588 my $category = $val->[1];
589 if (!defined $last_category or $last_category ne $category) {
590 $last_category = $category;
591 print OUT "<tr><th>$category</th><th colspan=2></th></tr>\n";
592 }
593 my $x = lc $key;
594 $x =~ s/[ ,'":\/()]+/_/g;
595 print OUT "<tr><td class=\"SUMM_DESC\">";
596 print OUT $val->[2];
Ted Kremenek2350a462008-10-28 19:56:52 +0000597 print OUT "</td><td class=\"Q\">";
Ted Kremenekebb74132008-09-21 06:58:09 +0000598 print OUT $val->[0];
599 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 +0000600 }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000601
602 # Print out the table of errors.
603
604print OUT <<ENDTEXT;
605</table>
Ted Kremenek7cba1122008-09-22 01:35:58 +0000606<h2>Reports</h2>
Ted Kremenekebb74132008-09-21 06:58:09 +0000607
608<table class="sortable" style="table-layout:automatic">
609<thead><tr>
610 <td>Bug Group</td>
611 <td class="sorttable_sorted">Bug Type<span id="sorttable_sortfwdind">&nbsp;&#x25BE;</span></td>
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000612 <td>File</td>
Ted Kremenekebb74132008-09-21 06:58:09 +0000613 <td class="Q">Line</td>
Ted Kremenek81983112008-09-28 04:13:09 +0000614 <td class="Q">Path Length</td>
Ted Kremenek2645c772008-07-07 16:58:44 +0000615 <td class="sorttable_nosort"></td>
Ted Kremenekebb74132008-09-21 06:58:09 +0000616 <!-- REPORTBUGCOL -->
617</tr></thead>
618<tbody>
Ted Kremenek5744dc22008-04-02 18:03:36 +0000619ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000620
Ted Kremenek991c54b2008-08-08 20:46:42 +0000621 my $prefix = GetPrefix();
622 my $regex;
623 my $InFileRegex;
624 my $InFilePrefix = "File:</td><td>";
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000625
Ted Kremenek991c54b2008-08-08 20:46:42 +0000626 if (defined $prefix) {
627 $regex = qr/^\Q$prefix\E/is;
628 $InFileRegex = qr/\Q$InFilePrefix$prefix\E/is;
629 }
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000630
Ted Kremenekebb74132008-09-21 06:58:09 +0000631 for my $row ( sort { $a->[2] cmp $b->[2] } @Index ) {
632 my $x = "$row->[1]:$row->[2]";
633 $x = lc $x;
634 $x =~ s/[ ,'":\/()]+/_/g;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000635
Ted Kremenek991c54b2008-08-08 20:46:42 +0000636 my $ReportFile = $row->[0];
Ted Kremenekebb74132008-09-21 06:58:09 +0000637
638 print OUT "<tr class=\"bt_$x\">";
639 print OUT "<td class=\"DESC\">";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000640 print OUT $row->[1];
Ted Kremenekebb74132008-09-21 06:58:09 +0000641 print OUT "</td>";
642 print OUT "<td class=\"DESC\">";
643 print OUT $row->[2];
644 print OUT "</td>";
645
646 # Update the file prefix.
647 my $fname = $row->[3];
Ted Kremenekebb74132008-09-21 06:58:09 +0000648
Ted Kremenek991c54b2008-08-08 20:46:42 +0000649 if (defined $regex) {
650 $fname =~ s/$regex//;
651 UpdateInFilePath("$Dir/$ReportFile", $InFileRegex, $InFilePrefix)
652 }
Ted Kremenekebb74132008-09-21 06:58:09 +0000653
Ted Kremenek91639ef2008-09-22 17:42:31 +0000654 print OUT "<td>";
Ted Kremenekebb74132008-09-21 06:58:09 +0000655 my @fname = split /\//,$fname;
656 if ($#fname > 0) {
657 while ($#fname >= 0) {
658 my $x = shift @fname;
659 print OUT $x;
660 if ($#fname >= 0) {
661 print OUT "<span class=\"W\"> </span>/";
662 }
663 }
664 }
665 else {
666 print OUT $fname;
Ted Kremenek91639ef2008-09-22 17:42:31 +0000667 }
Ted Kremenekebb74132008-09-21 06:58:09 +0000668 print OUT "</td>";
669
670 # Print out the quantities.
Ted Kremenek81983112008-09-28 04:13:09 +0000671 for my $j ( 4 .. 5 ) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000672 print OUT "<td class=\"Q\">$row->[$j]</td>";
673 }
674
Ted Kremenek991c54b2008-08-08 20:46:42 +0000675 # Print the rest of the columns.
Ted Kremenek81983112008-09-28 04:13:09 +0000676 for (my $j = 6; $j <= $#{$row}; ++$j) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000677 print OUT "<td>$row->[$j]</td>"
Ted Kremenek991c54b2008-08-08 20:46:42 +0000678 }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000679
Ted Kremenek991c54b2008-08-08 20:46:42 +0000680 # Emit the "View" link.
Ted Kremenek68005dd2008-09-22 17:39:18 +0000681 print OUT "<td><a href=\"$ReportFile#EndPath\">View Report</a></td>";
Ted Kremenek3cea9ee2008-07-30 17:58:08 +0000682
Daniel Dunbare43038e2008-09-19 23:18:44 +0000683 # Emit REPORTBUG markers.
Ted Kremenekebb74132008-09-21 06:58:09 +0000684 print OUT "\n<!-- REPORTBUG id=\"$ReportFile\" -->\n";
Daniel Dunbare43038e2008-09-19 23:18:44 +0000685
Ted Kremenek991c54b2008-08-08 20:46:42 +0000686 # End the row.
687 print OUT "</tr>\n";
688 }
689
Ted Kremenekebb74132008-09-21 06:58:09 +0000690 print OUT "</tbody>\n</table>\n\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000691 }
692
693 if ($Crashes) {
694 # Read the crash directory for files.
695 opendir(DIR, "$Dir/crashes");
Ted Kremenek82a12532008-09-25 00:25:16 +0000696 my @files = grep { /[.]info.txt$/ } readdir(DIR);
Ted Kremenek991c54b2008-08-08 20:46:42 +0000697 closedir(DIR);
698
699 if (scalar(@files)) {
700 print OUT <<ENDTEXT;
Ted Kremenek7cba1122008-09-22 01:35:58 +0000701<h2>Analyzer Failures</h2>
Ted Kremenek991c54b2008-08-08 20:46:42 +0000702
Ted Kremenek5d31f832008-08-18 18:38:29 +0000703<p>The analyzer had problems processing the following files:</p>
Ted Kremenek991c54b2008-08-08 20:46:42 +0000704
705<table>
Ted Kremenek9f9b1fd2008-09-12 22:49:36 +0000706<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 +0000707ENDTEXT
708
709 foreach my $file (sort @files) {
Ted Kremenek82a12532008-09-25 00:25:16 +0000710 $file =~ /(.+).info.txt$/;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000711 # Get the preprocessed file.
712 my $ppfile = $1;
713 # Open the info file and get the name of the source file.
714 open (INFO, "$Dir/crashes/$file") or
715 die "Cannot open $Dir/crashes/$file\n";
716 my $srcfile = <INFO>;
Ted Kremenek5d31f832008-08-18 18:38:29 +0000717 chomp $srcfile;
718 my $problem = <INFO>;
719 chomp $problem;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000720 close (INFO);
721 # Print the information in the table.
Ted Kremenekd52e4252008-08-25 20:45:07 +0000722 my $prefix = GetPrefix();
Ted Kremenek9f9b1fd2008-09-12 22:49:36 +0000723 if (defined $prefix) { $srcfile =~ s/^\Q$prefix//; }
724 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";
Daniel Dunbarce723ce2008-09-25 01:10:50 +0000725 my $ppfile_clang = $ppfile;
726 $ppfile_clang =~ s/[.](.+)$/.clang.$1/;
Daniel Dunbar7ebe0ed2008-09-25 06:05:31 +0000727 print OUT " <!-- REPORTPROBLEM src=\"$srcfile\" file=\"crashes/$ppfile\" clangfile=\"crashes/$ppfile_clang\" stderr=\"crashes/$ppfile.stderr.txt\" info=\"crashes/$ppfile.info.txt\" -->\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000728 }
729
730 print OUT <<ENDTEXT;
731</table>
Daniel Dunbarce723ce2008-09-25 01:10:50 +0000732<p>Please consider submitting preprocessed files as <a href="http://clang.llvm.org/StaticAnalysisUsage.html#filingbugs">bug reports</a>. <!-- REPORTCRASHES --> </p>
Ted Kremenek991c54b2008-08-08 20:46:42 +0000733ENDTEXT
734 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000735 }
736
Ted Kremenek991c54b2008-08-08 20:46:42 +0000737 print OUT "</body></html>\n";
Ted Kremenek5744dc22008-04-02 18:03:36 +0000738 close(OUT);
Ted Kremenek3ce12072008-09-22 17:50:47 +0000739 CopyFiles($Dir);
Ted Kremenek20161e92008-07-15 20:18:21 +0000740
741 # Make sure $Dir and $BaseDir are world readable/executable.
742 system("chmod", "755", $Dir);
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000743 if (defined $BaseDir) { system("chmod", "755", $BaseDir); }
Ted Kremenek20161e92008-07-15 20:18:21 +0000744
Ted Kremenek23cfca32008-06-16 22:40:14 +0000745 my $Num = scalar(@Index);
Ted Kremenek150c2122008-07-11 19:15:05 +0000746 Diag("$Num bugs found.\n");
747 if ($Num > 0 && -r "$Dir/index.html") {
Ted Kremenek5950b3f2008-09-22 06:47:01 +0000748 Diag("Run 'scan-view $Dir' to examine bug reports.\n");
Ted Kremenek150c2122008-07-11 19:15:05 +0000749 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000750
Ted Kremenek991c54b2008-08-08 20:46:42 +0000751 DiagCrashes($Dir) if ($Crashes);
752
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000753 return $Num;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000754}
755
756##----------------------------------------------------------------------------##
Ted Kremenekdab11102008-04-02 04:43:42 +0000757# RunBuildCommand - Run the build command.
758##----------------------------------------------------------------------------##
759
Ted Kremenek6b628982008-04-30 23:47:12 +0000760sub AddIfNotPresent {
761 my $Args = shift;
762 my $Arg = shift;
763 my $found = 0;
764
765 foreach my $k (@$Args) {
766 if ($k eq $Arg) {
767 $found = 1;
768 last;
769 }
770 }
771
772 if ($found == 0) {
773 push @$Args, $Arg;
774 }
775}
776
Ted Kremenekdab11102008-04-02 04:43:42 +0000777sub RunBuildCommand {
778
779 my $Args = shift;
Ted Kremenek7442ca62008-04-02 16:04:51 +0000780 my $IgnoreErrors = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000781 my $Cmd = $Args->[0];
Ted Kremenek6195c372008-06-02 21:52:47 +0000782 my $CCAnalyzer = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000783
Ted Kremenek3301cb12008-06-30 18:18:16 +0000784 # Get only the part of the command after the last '/'.
785 if ($Cmd =~ /\/([^\/]+)$/) {
786 $Cmd = $1;
787 }
788
Ted Kremenek63c20172008-08-04 17:34:06 +0000789 if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc"
790 or $Cmd eq "ccc-analyzer") {
Ted Kremenekdab11102008-04-02 04:43:42 +0000791 shift @$Args;
Ted Kremenek6195c372008-06-02 21:52:47 +0000792 unshift @$Args, $CCAnalyzer;
Ted Kremenekdab11102008-04-02 04:43:42 +0000793 }
Ted Kremenek7442ca62008-04-02 16:04:51 +0000794 elsif ($IgnoreErrors) {
795 if ($Cmd eq "make" or $Cmd eq "gmake") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000796 AddIfNotPresent($Args,"-k");
Ted Kremenek8912b542008-05-13 21:28:02 +0000797 AddIfNotPresent($Args,"-i");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000798 }
799 elsif ($Cmd eq "xcodebuild") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000800 AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000801 }
Ted Kremenek6b628982008-04-30 23:47:12 +0000802 }
803
Ted Kremenek6b628982008-04-30 23:47:12 +0000804 if ($Cmd eq "xcodebuild") {
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000805 # Disable distributed builds for xcodebuild.
Ted Kremenek6b628982008-04-30 23:47:12 +0000806 AddIfNotPresent($Args,"-nodistribute");
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000807
808 # Disable PCH files until clang supports them.
809 AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO");
Ted Kremenek915e9722008-05-27 23:18:07 +0000810
811 # When 'CC' is set, xcodebuild uses it to do all linking, even if we are
812 # linking C++ object files. Set 'LDPLUSPLUS' so that xcodebuild uses 'g++'
813 # when linking such files.
Ted Kremenek95aa1052008-09-04 17:52:41 +0000814 die if (!defined $CXX);
815 my $LDPLUSPLUS = `which $CXX`;
Ted Kremenek915e9722008-05-27 23:18:07 +0000816 $LDPLUSPLUS =~ s/\015?\012//; # strip newlines
817 $ENV{'LDPLUSPLUS'} = $LDPLUSPLUS;
Ted Kremenek6b628982008-04-30 23:47:12 +0000818 }
Ted Kremenekdab11102008-04-02 04:43:42 +0000819
Ted Kremenek5a4ddaf2008-08-25 20:10:45 +0000820 return (system(@$Args) >> 8);
Ted Kremenekdab11102008-04-02 04:43:42 +0000821}
822
Ted Kremenekdab11102008-04-02 04:43:42 +0000823##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000824# DisplayHelp - Utility function to display all help options.
825##----------------------------------------------------------------------------##
826
Sam Bishopa0e22662008-04-02 03:35:43 +0000827sub DisplayHelp {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000828
Ted Kremenek5744dc22008-04-02 18:03:36 +0000829print <<ENDTEXT;
Sam Bishopa0e22662008-04-02 03:35:43 +0000830USAGE: $Prog [options] <build command> [build options]
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000831
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000832ENDTEXT
833
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000834 if (defined $BuildName) {
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000835 print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n";
836 }
837
838print <<ENDTEXT;
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000839OPTIONS:
840
Ted Kremeneke15fa272008-10-13 21:46:42 +0000841 -analyze-headers - Also analyze functions in #included files.
842
Zhongxing Xu07c37672008-10-27 14:26:32 +0000843 -store [model] - Specify the store model used by the analyzer. By default,
844 the 'basic' store model is used. 'region' specifies a field-
845 sensitive store model. Be warned that the 'region' model
846 is still in very early testing phase and may often crash.
847
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000848 -o - Target directory for HTML report files. Subdirectories
Sam Bishopa0e22662008-04-02 03:35:43 +0000849 will be created as needed to represent separate "runs" of
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000850 the analyzer. If this option is not specified, a directory
851 is created in /tmp to store the reports.
Ted Kremenek1262fc42008-05-14 20:10:33 +0000852
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000853 -h - Display this message.
854 --help
Ted Kremenek1262fc42008-05-14 20:10:33 +0000855
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000856 -k - Add a "keep on going" option to the specified build command.
857 --keep-going This option currently supports make and xcodebuild.
Ted Kremenekf02e8db2008-04-02 16:41:25 +0000858 This is a convenience option; one can specify this
859 behavior directly using build options.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000860
Ted Kremenek7cba1122008-09-22 01:35:58 +0000861 --html-title [title] - Specify the title used on generated HTML pages.
862 --html-title=[title] If not specified, a default title will be used.
863
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000864 --status-bugs - By default, the exit status of $Prog is the same as the
865 executed build command. Specifying this option causes the
866 exit status of $Prog to be 1 if it found potential bugs
867 and 0 otherwise.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000868
Ted Kremenek386c6932008-09-03 17:59:35 +0000869 --use-cc [compiler path] - By default, $Prog uses 'gcc' to compile and link
870 --use-cc=[compiler path] your C and Objective-C code. Use this option
871 to specify an alternate compiler.
872
873 --use-c++ [compiler path] - By default, $Prog uses 'g++' to compile and link
874 --use-c++=[compiler path] your C++ and Objective-C++ code. Use this option
875 to specify an alternate compiler.
Ted Kremenekf17ef3c2008-08-21 21:47:09 +0000876
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000877 -v - Verbose output from $Prog and the analyzer.
Ted Kremenek386c6932008-09-03 17:59:35 +0000878 A second and third '-v' increases verbosity.
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000879
880 -V - View analysis results in a web browser when the build
881 --view completes.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000882
Ted Kremenekb7770c02008-07-15 17:06:13 +0000883
Ted Kremenek386c6932008-09-03 17:59:35 +0000884AVAILABLE ANALYSES (multiple analyses may be specified):
Ted Kremenekd52e4252008-08-25 20:45:07 +0000885
886ENDTEXT
Ted Kremenekb7770c02008-07-15 17:06:13 +0000887
888 foreach my $Analysis (sort keys %AvailableAnalyses) {
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000889 if (defined $AnalysesDefaultEnabled{$Analysis}) {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000890 print " (+)";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000891 }
892 else {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000893 print " ";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000894 }
895
896 print " $Analysis $AvailableAnalyses{$Analysis}\n";
897 }
898
899print <<ENDTEXT
900
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000901 NOTE: "(+)" indicates that an analysis is enabled by default unless one
902 or more analysis options are specified
Ted Kremenekb7770c02008-07-15 17:06:13 +0000903
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000904BUILD OPTIONS
905
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000906 You can specify any build option acceptable to the build command.
Ted Kremenek39eefde2008-04-02 16:47:27 +0000907
Ted Kremenek5744dc22008-04-02 18:03:36 +0000908EXAMPLE
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000909
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000910 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000911
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000912 The above example causes analysis reports to be deposited into
913 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
914 A different subdirectory is created each time $Prog analyzes a project.
915 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000916
917ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000918}
919
920##----------------------------------------------------------------------------##
Ted Kremenek7cba1122008-09-22 01:35:58 +0000921# HtmlEscape - HTML entity encode characters that are special in HTML
922##----------------------------------------------------------------------------##
923
924sub HtmlEscape {
925 # copy argument to new variable so we don't clobber the original
926 my $arg = shift || '';
927 my $tmp = $arg;
928
929 $tmp =~ s/([\<\>\'\"])/sprintf("&#%02x;", chr($1))/ge;
930
931 return $tmp;
932}
933
934##----------------------------------------------------------------------------##
935# ShellEscape - backslash escape characters that are special to the shell
936##----------------------------------------------------------------------------##
937
938sub ShellEscape {
939 # copy argument to new variable so we don't clobber the original
940 my $arg = shift || '';
941 my $tmp = $arg;
942
943 $tmp =~ s/([\!\;\\\'\"\`\<\>\|\s\(\)\[\]\?\#\$\^\&\*\=])/\\$1/g;
944
945 return $tmp;
946}
947
948##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000949# Process command-line arguments.
950##----------------------------------------------------------------------------##
951
Ted Kremeneke15fa272008-10-13 21:46:42 +0000952my $AnalyzeHeaders = 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000953my $HtmlDir; # Parent directory to store HTML files.
954my $IgnoreErrors = 0; # Ignore build errors.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000955my $ViewResults = 0; # View results when the build terminates.
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000956my $ExitStatusFoundBugs = 0; # Exit status reflects whether bugs were found
Ted Kremenekb7770c02008-07-15 17:06:13 +0000957my @AnalysesToRun;
Zhongxing Xu07c37672008-10-27 14:26:32 +0000958my $StoreModel;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000959
960if (!@ARGV) {
961 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000962 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000963}
964
965while (@ARGV) {
966
967 # Scan for options we recognize.
968
969 my $arg = $ARGV[0];
970
Sam Bishop2f2418e2008-04-03 14:29:47 +0000971 if ($arg eq "-h" or $arg eq "--help") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000972 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000973 exit 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000974 }
975
Ted Kremeneke15fa272008-10-13 21:46:42 +0000976 if ($arg eq '-analyze-headers') {
977 shift @ARGV;
978 $AnalyzeHeaders = 1;
979 next;
980 }
981
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000982 if (defined $AvailableAnalyses{$arg}) {
Ted Kremenek1262fc42008-05-14 20:10:33 +0000983 shift @ARGV;
Ted Kremenekb7770c02008-07-15 17:06:13 +0000984 push @AnalysesToRun, $arg;
Ted Kremenek1262fc42008-05-14 20:10:33 +0000985 next;
986 }
987
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000988 if ($arg eq "-o") {
989 shift @ARGV;
990
991 if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000992 DieDiag("'-o' option requires a target directory name.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000993 }
994
995 $HtmlDir = shift @ARGV;
996 next;
997 }
Ted Kremenek7cba1122008-09-22 01:35:58 +0000998
999 if ($arg =~ /^--html-title(=(.+))?$/) {
1000 shift @ARGV;
1001
1002 if ($2 eq '') {
1003 if (!@ARGV) {
1004 DieDiag("'--html-title' option requires a string.\n");
1005 }
1006
1007 $HtmlTitle = shift @ARGV;
1008 } else {
1009 $HtmlTitle = $2;
1010 }
1011
1012 next;
1013 }
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001014
Ted Kremenek2b74ab62008-04-01 21:22:03 +00001015 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001016 shift @ARGV;
1017 $IgnoreErrors = 1;
1018 next;
1019 }
1020
Ted Kremenekf17ef3c2008-08-21 21:47:09 +00001021 if ($arg =~ /^--use-cc(=(.+))?$/) {
1022 shift @ARGV;
1023 my $cc;
1024
1025 if ($2 eq "") {
1026 if (!@ARGV) {
1027 DieDiag("'--use-cc' option requires a compiler executable name.\n");
1028 }
1029 $cc = shift @ARGV;
1030 }
1031 else {
1032 $cc = $2;
1033 }
1034
1035 $ENV{"CCC_CC"} = $cc;
1036 next;
1037 }
1038
Ted Kremenek7cba1122008-09-22 01:35:58 +00001039 if ($arg =~ /^--use-c\+\+(=(.+))?$/) {
Ted Kremenek386c6932008-09-03 17:59:35 +00001040 shift @ARGV;
1041
1042 if ($2 eq "") {
1043 if (!@ARGV) {
1044 DieDiag("'--use-c++' option requires a compiler executable name.\n");
1045 }
1046 $CXX = shift @ARGV;
1047 }
1048 else {
1049 $CXX = $2;
1050 }
1051 next;
1052 }
1053
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001054 if ($arg eq "-v") {
1055 shift @ARGV;
1056 $Verbose++;
1057 next;
1058 }
1059
Ted Kremenek7f8a3252008-04-02 18:42:49 +00001060 if ($arg eq "-V" or $arg eq "--view") {
1061 shift @ARGV;
1062 $ViewResults = 1;
1063 next;
1064 }
1065
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001066 if ($arg eq "--status-bugs") {
1067 shift @ARGV;
1068 $ExitStatusFoundBugs = 1;
1069 next;
1070 }
Zhongxing Xu07c37672008-10-27 14:26:32 +00001071
1072 if ($arg eq "-store") {
1073 shift @ARGV;
1074 $StoreModel = '-analyzer-store-' . shift @ARGV;
1075 next;
1076 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001077
Ted Kremenek23cfca32008-06-16 22:40:14 +00001078 DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/);
Ted Kremenek0062ad42008-04-02 16:35:01 +00001079
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001080 last;
1081}
1082
1083if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +00001084 Diag("No build command specified.\n\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001085 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +00001086 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001087}
1088
Ted Kremenek7cba1122008-09-22 01:35:58 +00001089$CmdArgs = HtmlEscape(join(' ', map(ShellEscape($_), @ARGV)));
1090$HtmlTitle = "${CurrentDirSuffix} - scan-build results"
1091 unless (defined($HtmlTitle));
Ted Kremenek386c6932008-09-03 17:59:35 +00001092
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001093# Determine the output directory for the HTML reports.
Ted Kremenek684bb092008-04-18 15:18:20 +00001094my $BaseDir = $HtmlDir;
Sam Bishopa0e22662008-04-02 03:35:43 +00001095$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001096
1097# Set the appropriate environment variables.
Sam Bishopa0e22662008-04-02 03:35:43 +00001098SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001099
Ted Kremeneke15fa272008-10-13 21:46:42 +00001100my $Cmd = Cwd::realpath("$RealBin/ccc-analyzer");
Ted Kremenek0b6c1532008-04-08 20:22:12 +00001101
Ted Kremenek23cfca32008-06-16 22:40:14 +00001102DieDiag("Executable 'ccc-analyzer' does not exist at '$Cmd'\n")
Ted Kremenek0b6c1532008-04-08 20:22:12 +00001103 if (! -x $Cmd);
Ted Kremenekf22eacb2008-04-18 22:00:56 +00001104
Ted Kremenekb7770c02008-07-15 17:06:13 +00001105if (! -x $ClangSB) {
1106 Diag("'clang' executable not found in '$RealBin'.\n");
1107 Diag("Using 'clang' from path.\n");
Ted Kremenekf22eacb2008-04-18 22:00:56 +00001108}
Ted Kremenek0b6c1532008-04-08 20:22:12 +00001109
Ted Kremenek95aa1052008-09-04 17:52:41 +00001110if (defined $CXX) {
1111 $ENV{'CXX'} = $CXX;
1112}
1113else {
1114 $CXX = 'g++'; # This variable is used by other parts of scan-build
1115 # that need to know a default C++ compiler to fall back to.
1116}
1117
Ted Kremenek4f4b17d2008-04-03 20:08:18 +00001118$ENV{'CC'} = $Cmd;
Ted Kremenekf22eacb2008-04-18 22:00:56 +00001119$ENV{'CLANG'} = $Clang;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001120
1121if ($Verbose >= 2) {
1122 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
1123}
1124
Ted Kremeneka9525c92008-05-12 22:07:14 +00001125if ($Verbose >= 3) {
1126 $ENV{'CCC_ANALYZER_LOG'} = 1;
1127}
1128
Ted Kremenek90125992008-07-15 23:41:32 +00001129if (scalar(@AnalysesToRun) == 0) {
1130 foreach my $key (keys %AnalysesDefaultEnabled) {
1131 push @AnalysesToRun,$key;
1132 }
Ted Kremenek01006782008-07-02 23:16:10 +00001133}
Ted Kremenek1262fc42008-05-14 20:10:33 +00001134
Ted Kremeneke15fa272008-10-13 21:46:42 +00001135if ($AnalyzeHeaders) {
1136 push @AnalysesToRun,"-analyzer-opt-analyze-headers";
1137}
1138
Ted Kremenek90125992008-07-15 23:41:32 +00001139$ENV{'CCC_ANALYZER_ANALYSIS'} = join ' ',@AnalysesToRun;
1140
Zhongxing Xu07c37672008-10-27 14:26:32 +00001141if ($StoreModel) {
1142 $ENV{'CCC_ANALYZER_STORE_MODEL'} = $StoreModel;
1143}
1144
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001145# Run the build.
Ted Kremenek5656a982008-07-15 17:09:28 +00001146my $ExitStatus = RunBuildCommand(\@ARGV, $IgnoreErrors, $Cmd);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001147
1148# Postprocess the HTML directory.
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001149my $NumBugs = Postprocess($HtmlDir, $BaseDir);
Ted Kremenek7f8a3252008-04-02 18:42:49 +00001150
1151if ($ViewResults and -r "$HtmlDir/index.html") {
Ted Kremenek50534dc2008-09-22 17:38:23 +00001152 Diag "Analysis run complete.\n";
Ted Kremenek5950b3f2008-09-22 06:47:01 +00001153 Diag "Viewing analysis results in '$HtmlDir' using scan-view.\n";
Ted Kremeneke15fa272008-10-13 21:46:42 +00001154 my $ScanView = Cwd::realpath("$RealBin/scan-view");
Ted Kremenek5950b3f2008-09-22 06:47:01 +00001155 if (! -x $ScanView) { $ScanView = "scan-view"; }
1156 exec $ScanView, "$HtmlDir";
Ted Kremenek7f8a3252008-04-02 18:42:49 +00001157}
Ted Kremenek5656a982008-07-15 17:09:28 +00001158
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001159if ($ExitStatusFoundBugs) {
1160 exit 1 if ($NumBugs > 0);
1161 exit 0;
1162}
1163
Ted Kremenek5656a982008-07-15 17:09:28 +00001164exit $ExitStatus;
1165