blob: f1e26b71a8df766b64d1327f29bf41cd982fb7ff [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) {
Ted Kremenekffda0b42008-10-31 05:48:42 +0000140 if (`uname` =~ /Darwin/) {
141 $Dir = $ENV{'TMPDIR'};
142 if (!defined $Dir) { $Dir = "/tmp"; }
143 }
144 else {
145 $Dir = "/tmp";
146 }
147
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000148 $TmpMode = 1;
149 }
150
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000151 # Get current date and time.
152
153 my @CurrentTime = localtime();
154
155 my $year = $CurrentTime[5] + 1900;
156 my $day = $CurrentTime[3];
157 my $month = $CurrentTime[4] + 1;
158
Ted Kremenek9d7405f2008-05-14 17:23:56 +0000159 my $DateString = sprintf("%d-%02d-%02d", $year, $month, $day);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000160
161 # Determine the run number.
162
163 my $RunNumber;
164
165 if (-d $Dir) {
166
167 if (! -r $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000168 DieDiag("directory '$Dir' exists but is not readable.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000169 }
170
171 # Iterate over all files in the specified directory.
172
173 my $max = 0;
174
175 opendir(DIR, $Dir);
Ted Kremenek29da6c52008-08-07 17:57:34 +0000176 my @FILES = grep { -d "$Dir/$_" } readdir(DIR);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000177 closedir(DIR);
178
179 foreach my $f (@FILES) {
180
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000181 # Strip the prefix '$Prog-' if we are dumping files to /tmp.
182 if ($TmpMode) {
183 next if (!($f =~ /^$Prog-(.+)/));
184 $f = $1;
185 }
186
Ted Kremenekebb74132008-09-21 06:58:09 +0000187
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000188 my @x = split/-/, $f;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000189 next if (scalar(@x) != 4);
190 next if ($x[0] != $year);
191 next if ($x[1] != $month);
192 next if ($x[2] != $day);
193
194 if ($x[3] > $max) {
195 $max = $x[3];
196 }
197 }
198
199 $RunNumber = $max + 1;
200 }
201 else {
202
203 if (-x $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000204 DieDiag("'$Dir' exists but is not a directory.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000205 }
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000206
207 if ($TmpMode) {
Ted Kremenek445fa772008-10-10 00:17:08 +0000208 DieDiag("The directory '/tmp' does not exist or cannot be accessed.\n");
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000209 }
210
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000211 # $Dir does not exist. It will be automatically created by the
212 # clang driver. Set the run number to 1.
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000213
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000214 $RunNumber = 1;
215 }
216
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000217 die "RunNumber must be defined!" if (!defined $RunNumber);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000218
219 # Append the run number.
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000220 my $NewDir;
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000221 if ($TmpMode) {
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000222 $NewDir = "$Dir/$Prog-$DateString-$RunNumber";
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000223 }
224 else {
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000225 $NewDir = "$Dir/$DateString-$RunNumber";
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000226 }
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000227 system 'mkdir','-p',$NewDir;
228 return $NewDir;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000229}
230
Sam Bishopa0e22662008-04-02 03:35:43 +0000231sub SetHtmlEnv {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000232
233 die "Wrong number of arguments." if (scalar(@_) != 2);
234
235 my $Args = shift;
236 my $Dir = shift;
237
238 die "No build command." if (scalar(@$Args) == 0);
239
240 my $Cmd = $$Args[0];
241
242 if ($Cmd =~ /configure/) {
243 return;
244 }
245
246 if ($Verbose) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000247 Diag("Emitting reports for this run to '$Dir'.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000248 }
249
250 $ENV{'CCC_ANALYZER_HTML'} = $Dir;
251}
252
253##----------------------------------------------------------------------------##
Ted Kremenek57cf4462008-04-18 15:09:30 +0000254# ComputeDigest - Compute a digest of the specified file.
255##----------------------------------------------------------------------------##
256
257sub ComputeDigest {
258 my $FName = shift;
Ted Kremenek23cfca32008-06-16 22:40:14 +0000259 DieDiag("Cannot read $FName to compute Digest.\n") if (! -r $FName);
Ted Kremeneka6e24812008-04-19 18:05:48 +0000260
261 # Use Digest::MD5. We don't have to be cryptographically secure. We're
Ted Kremenek7ea02e62008-04-19 18:07:44 +0000262 # just looking for duplicate files that come from a non-malicious source.
263 # We use Digest::MD5 because it is a standard Perl module that should
Ted Kremenek63c20172008-08-04 17:34:06 +0000264 # come bundled on most systems.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000265 open(FILE, $FName) or DieDiag("Cannot open $FName when computing Digest.\n");
Ted Kremeneka6e24812008-04-19 18:05:48 +0000266 binmode FILE;
267 my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest;
268 close(FILE);
269
Ted Kremenek63c20172008-08-04 17:34:06 +0000270 # Return the digest.
Ted Kremeneka6e24812008-04-19 18:05:48 +0000271 return $Result;
Ted Kremenek57cf4462008-04-18 15:09:30 +0000272}
273
274##----------------------------------------------------------------------------##
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000275# UpdatePrefix - Compute the common prefix of files.
276##----------------------------------------------------------------------------##
277
278my $Prefix;
279
280sub UpdatePrefix {
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000281 my $x = shift;
282 my $y = basename($x);
283 $x =~ s/\Q$y\E$//;
284
285 # Ignore /usr, /Library, /System, /Developer
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000286 return if ( $x =~ /^\/usr/ or $x =~ /^\/Library/
287 or $x =~ /^\/System/ or $x =~ /^\/Developer/);
288
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000289 if (!defined $Prefix) {
290 $Prefix = $x;
291 return;
292 }
293
Ted Kremenek20b2bae2008-09-11 21:15:10 +0000294 chop $Prefix while (!($x =~ /^\Q$Prefix/));
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000295}
296
297sub GetPrefix {
298 return $Prefix;
299}
300
301##----------------------------------------------------------------------------##
302# UpdateInFilePath - Update the path in the report file.
303##----------------------------------------------------------------------------##
304
305sub UpdateInFilePath {
306 my $fname = shift;
307 my $regex = shift;
308 my $newtext = shift;
Ted Kremenek63c20172008-08-04 17:34:06 +0000309
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000310 open (RIN, $fname) or die "cannot open $fname";
Ted Kremenek63c20172008-08-04 17:34:06 +0000311 open (ROUT, ">", "$fname.tmp") or die "cannot open $fname.tmp";
312
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000313 while (<RIN>) {
314 s/$regex/$newtext/;
315 print ROUT $_;
316 }
Ted Kremenek63c20172008-08-04 17:34:06 +0000317
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000318 close (ROUT);
319 close (RIN);
Ted Kremenek20161e92008-07-15 20:18:21 +0000320 system("mv", "$fname.tmp", $fname);
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000321}
322
323##----------------------------------------------------------------------------##
Ted Kremenek5744dc22008-04-02 18:03:36 +0000324# ScanFile - Scan a report file for various identifying attributes.
325##----------------------------------------------------------------------------##
326
Ted Kremenek57cf4462008-04-18 15:09:30 +0000327# Sometimes a source file is scanned more than once, and thus produces
328# multiple error reports. We use a cache to solve this problem.
329
330my %AlreadyScanned;
331
Ted Kremenek5744dc22008-04-02 18:03:36 +0000332sub ScanFile {
333
334 my $Index = shift;
335 my $Dir = shift;
336 my $FName = shift;
337
Ted Kremenek57cf4462008-04-18 15:09:30 +0000338 # Compute a digest for the report file. Determine if we have already
339 # scanned a file that looks just like it.
340
341 my $digest = ComputeDigest("$Dir/$FName");
342
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000343 if (defined $AlreadyScanned{$digest}) {
Ted Kremenek57cf4462008-04-18 15:09:30 +0000344 # Redundant file. Remove it.
Ted Kremenek20161e92008-07-15 20:18:21 +0000345 system ("rm", "-f", "$Dir/$FName");
Ted Kremenek57cf4462008-04-18 15:09:30 +0000346 return;
347 }
348
349 $AlreadyScanned{$digest} = 1;
350
Ted Kremenek809709f2008-04-18 16:58:34 +0000351 # At this point the report file is not world readable. Make it happen.
Ted Kremenek20161e92008-07-15 20:18:21 +0000352 system ("chmod", "644", "$Dir/$FName");
Ted Kremenek684bb092008-04-18 15:18:20 +0000353
354 # Scan the report file for tags.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000355 open(IN, "$Dir/$FName") or DieDiag("Cannot open '$Dir/$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000356
357 my $BugDesc = "";
Ted Kremenek22d6a632008-04-02 20:43:36 +0000358 my $BugFile = "";
Ted Kremenekebb74132008-09-21 06:58:09 +0000359 my $BugCategory;
Ted Kremenek22d6a632008-04-02 20:43:36 +0000360 my $BugPathLength = 1;
361 my $BugLine = 0;
Ted Kremenekebb74132008-09-21 06:58:09 +0000362 my $found = 0;
363
Ted Kremenek5744dc22008-04-02 18:03:36 +0000364 while (<IN>) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000365
366 last if ($found == 5);
367
Ted Kremenek5744dc22008-04-02 18:03:36 +0000368 if (/<!-- BUGDESC (.*) -->$/) {
369 $BugDesc = $1;
Ted Kremenekebb74132008-09-21 06:58:09 +0000370 ++$found;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000371 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000372 elsif (/<!-- BUGFILE (.*) -->$/) {
373 $BugFile = $1;
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000374 UpdatePrefix($BugFile);
Ted Kremenekebb74132008-09-21 06:58:09 +0000375 ++$found;
Ted Kremenek22d6a632008-04-02 20:43:36 +0000376 }
377 elsif (/<!-- BUGPATHLENGTH (.*) -->$/) {
378 $BugPathLength = $1;
Ted Kremenekebb74132008-09-21 06:58:09 +0000379 ++$found;
Ted Kremenek22d6a632008-04-02 20:43:36 +0000380 }
381 elsif (/<!-- BUGLINE (.*) -->$/) {
382 $BugLine = $1;
Ted Kremenekebb74132008-09-21 06:58:09 +0000383 ++$found;
384 }
385 elsif (/<!-- BUGCATEGORY (.*) -->$/) {
386 $BugCategory = $1;
387 ++$found;
Ted Kremenek22d6a632008-04-02 20:43:36 +0000388 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000389 }
390
391 close(IN);
Ted Kremenekebb74132008-09-21 06:58:09 +0000392
393 if (!defined $BugCategory) {
394 $BugCategory = "Other";
395 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000396
Ted Kremenek81983112008-09-28 04:13:09 +0000397 push @$Index,[ $FName, $BugCategory, $BugDesc, $BugFile, $BugLine,
398 $BugPathLength ];
Ted Kremenek22d6a632008-04-02 20:43:36 +0000399}
400
401##----------------------------------------------------------------------------##
Ted Kremenek3ce12072008-09-22 17:50:47 +0000402# CopyFiles - Copy resource files to target directory.
Ted Kremenek22d6a632008-04-02 20:43:36 +0000403##----------------------------------------------------------------------------##
404
Ted Kremenek3ce12072008-09-22 17:50:47 +0000405sub CopyFiles {
Ted Kremenek22d6a632008-04-02 20:43:36 +0000406
407 my $Dir = shift;
Ted Kremeneke15fa272008-10-13 21:46:42 +0000408
409 my $JS = Cwd::realpath("$RealBin/sorttable.js");
Ted Kremenek22d6a632008-04-02 20:43:36 +0000410
Ted Kremenek23cfca32008-06-16 22:40:14 +0000411 DieDiag("Cannot find 'sorttable.js'.\n")
Ted Kremeneke15fa272008-10-13 21:46:42 +0000412 if (! -r $JS);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000413
Ted Kremeneke15fa272008-10-13 21:46:42 +0000414 system ("cp", $JS, "$Dir");
Ted Kremenek22d6a632008-04-02 20:43:36 +0000415
Ted Kremenek23cfca32008-06-16 22:40:14 +0000416 DieDiag("Could not copy 'sorttable.js' to '$Dir'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000417 if (! -r "$Dir/sorttable.js");
Ted Kremenek3ce12072008-09-22 17:50:47 +0000418
Ted Kremeneke15fa272008-10-13 21:46:42 +0000419 my $CSS = Cwd::realpath("$RealBin/scanview.css");
420
Ted Kremenek3ce12072008-09-22 17:50:47 +0000421 DieDiag("Cannot find 'scanview.css'.\n")
Ted Kremeneke15fa272008-10-13 21:46:42 +0000422 if (! -r $CSS);
Ted Kremenek3ce12072008-09-22 17:50:47 +0000423
Ted Kremeneke15fa272008-10-13 21:46:42 +0000424 system ("cp", $CSS, "$Dir");
Ted Kremenek3ce12072008-09-22 17:50:47 +0000425
426 DieDiag("Could not copy 'scanview.css' to '$Dir'.\n")
Ted Kremeneke15fa272008-10-13 21:46:42 +0000427 if (! -r $CSS);
Ted Kremenek5744dc22008-04-02 18:03:36 +0000428}
429
430##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000431# Postprocess - Postprocess the results of an analysis scan.
432##----------------------------------------------------------------------------##
433
Sam Bishopa0e22662008-04-02 03:35:43 +0000434sub Postprocess {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000435
436 my $Dir = shift;
Ted Kremenek684bb092008-04-18 15:18:20 +0000437 my $BaseDir = shift;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000438
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000439 die "No directory specified." if (!defined $Dir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000440
441 if (! -d $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000442 Diag("No bugs found.\n");
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000443 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000444 }
445
446 opendir(DIR, $Dir);
Ted Kremenek991c54b2008-08-08 20:46:42 +0000447 my $Crashes = 0;
448 my @files = grep { if ($_ eq "crashes") { $Crashes++; }
449 /^report-.*\.html$/; } readdir(DIR);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000450 closedir(DIR);
451
Ted Kremenek991c54b2008-08-08 20:46:42 +0000452 if (scalar(@files) == 0 and $Crashes == 0) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000453 Diag("Removing directory '$Dir' because it contains no reports.\n");
Ted Kremenek20161e92008-07-15 20:18:21 +0000454 system ("rm", "-fR", $Dir);
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000455 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000456 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000457
Ted Kremenek991c54b2008-08-08 20:46:42 +0000458 # Scan each report file and build an index.
459 my @Index;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000460 foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); }
461
Ted Kremenekd52e4252008-08-25 20:45:07 +0000462 # Scan the crashes directory and use the information in the .info files
463 # to update the common prefix directory.
464 if (-d "$Dir/crashes") {
465 opendir(DIR, "$Dir/crashes");
Ted Kremenek82a12532008-09-25 00:25:16 +0000466 my @files = grep { /[.]info.txt$/; } readdir(DIR);
Ted Kremenekd52e4252008-08-25 20:45:07 +0000467 closedir(DIR);
468 foreach my $file (@files) {
469 open IN, "$Dir/crashes/$file" or DieDiag("cannot open $file\n");
470 my $Path = <IN>;
471 if (defined $Path) { UpdatePrefix($Path); }
472 close IN;
473 }
474 }
475
Ted Kremenek63c20172008-08-04 17:34:06 +0000476 # Generate an index.html file.
477 my $FName = "$Dir/index.html";
478 open(OUT, ">", $FName) or DieDiag("Cannot create file '$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000479
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000480 # Print out the header.
481
Ted Kremenek5744dc22008-04-02 18:03:36 +0000482print OUT <<ENDTEXT;
483<html>
484<head>
Ted Kremenek7cba1122008-09-22 01:35:58 +0000485<title>${HtmlTitle}</title>
Ted Kremenekf1435452008-09-23 22:34:51 +0000486<link type="text/css" rel="stylesheet" href="scanview.css"/>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000487<script src="sorttable.js"></script>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000488<script language='javascript' type="text/javascript">
489function SetDisplay(RowClass, DisplayVal)
490{
491 var Rows = document.getElementsByTagName("tr");
492 for ( var i = 0 ; i < Rows.length; ++i ) {
493 if (Rows[i].className == RowClass) {
494 Rows[i].style.display = DisplayVal;
495 }
496 }
497}
Ted Kremenekebb74132008-09-21 06:58:09 +0000498
Ted Kremenek2350a462008-10-28 19:56:52 +0000499function CopyCheckedStateToCheckButtons(SummaryCheckButton) {
500 var Inputs = document.getElementsByTagName("input");
501 for ( var i = 0 ; i < Inputs.length; ++i ) {
502 if (Inputs[i].type == "checkbox") {
503 if(Inputs[i] != SummaryCheckButton) {
504 Inputs[i].checked = SummaryCheckButton.checked;
505 Inputs[i].onclick();
506 }
507 }
508 }
509}
510
Ted Kremenek999e1202008-10-28 20:09:57 +0000511function returnObjById( id ) {
512 if (document.getElementById)
513 var returnVar = document.getElementById(id);
514 else if (document.all)
515 var returnVar = document.all[id];
516 else if (document.layers)
517 var returnVar = document.layers[id];
518 return returnVar;
519}
520
521var NumUnchecked = 0;
522
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000523function ToggleDisplay(CheckButton, ClassName) {
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000524 if (CheckButton.checked) {
525 SetDisplay(ClassName, "");
Ted Kremenek999e1202008-10-28 20:09:57 +0000526 if (--NumUnchecked == 0) {
527 returnObjById("AllBugsCheck").checked = true;
528 }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000529 }
530 else {
531 SetDisplay(ClassName, "none");
Ted Kremenek999e1202008-10-28 20:09:57 +0000532 NumUnchecked++;
533 returnObjById("AllBugsCheck").checked = false;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000534 }
535}
536</script>
Ted Kremenek1d1abb12008-09-22 17:52:58 +0000537<!-- SUMMARYENDHEAD -->
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000538</head>
539<body>
Ted Kremenek7cba1122008-09-22 01:35:58 +0000540<h1>${HtmlTitle}</h1>
541
542<table>
543<tr><th>User:</th><td>${UserName}\@${HostName}</td></tr>
544<tr><th>Working Directory:</th><td>${CurrentDir}</td></tr>
545<tr><th>Command Line:</th><td>${CmdArgs}</td></tr>
546<tr><th>Date:</th><td>${Date}</td></tr>
547ENDTEXT
548
549print OUT "<tr><th>Version:</th><td>${BuildName} (${BuildDate})</td></tr>\n"
550 if (defined($BuildName) && defined($BuildDate));
551
552print OUT <<ENDTEXT;
553</table>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000554ENDTEXT
555
Ted Kremenek991c54b2008-08-08 20:46:42 +0000556 if (scalar(@files)) {
557 # Print out the summary table.
558 my %Totals;
Ted Kremenekebb74132008-09-21 06:58:09 +0000559
Ted Kremenek991c54b2008-08-08 20:46:42 +0000560 for my $row ( @Index ) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000561 my $bug_type = ($row->[2]);
562 my $bug_category = ($row->[1]);
563 my $key = "$bug_category:$bug_type";
564
565 if (!defined $Totals{$key}) { $Totals{$key} = [1,$bug_category,$bug_type]; }
566 else { $Totals{$key}->[0]++; }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000567 }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000568
Ted Kremenek7cba1122008-09-22 01:35:58 +0000569 print OUT "<h2>Bug Summary</h2>";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000570
571 if (defined $BuildName) {
572 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 +0000573 }
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000574
Ted Kremenek2350a462008-10-28 19:56:52 +0000575
576 my $TotalBugs = scalar(@Index);
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000577print OUT <<ENDTEXT;
Ted Kremenekebb74132008-09-21 06:58:09 +0000578<table>
579<thead><tr><td>Bug Type</td><td>Quantity</td><td class="sorttable_nosort">Display?</td></tr></thead>
Ted Kremenek999e1202008-10-28 20:09:57 +0000580<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 +0000581ENDTEXT
582
Ted Kremenekebb74132008-09-21 06:58:09 +0000583 my $last_category;
584
585 for my $key (
586 sort {
587 my $x = $Totals{$a};
588 my $y = $Totals{$b};
589 my $res = $x->[1] cmp $y->[1];
590 $res = $x->[2] cmp $y->[2] if ($res == 0);
591 $res
592 } keys %Totals )
593 {
594 my $val = $Totals{$key};
595 my $category = $val->[1];
596 if (!defined $last_category or $last_category ne $category) {
597 $last_category = $category;
598 print OUT "<tr><th>$category</th><th colspan=2></th></tr>\n";
599 }
600 my $x = lc $key;
601 $x =~ s/[ ,'":\/()]+/_/g;
602 print OUT "<tr><td class=\"SUMM_DESC\">";
603 print OUT $val->[2];
Ted Kremenek2350a462008-10-28 19:56:52 +0000604 print OUT "</td><td class=\"Q\">";
Ted Kremenekebb74132008-09-21 06:58:09 +0000605 print OUT $val->[0];
606 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 +0000607 }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000608
609 # Print out the table of errors.
610
611print OUT <<ENDTEXT;
612</table>
Ted Kremenek7cba1122008-09-22 01:35:58 +0000613<h2>Reports</h2>
Ted Kremenekebb74132008-09-21 06:58:09 +0000614
615<table class="sortable" style="table-layout:automatic">
616<thead><tr>
617 <td>Bug Group</td>
618 <td class="sorttable_sorted">Bug Type<span id="sorttable_sortfwdind">&nbsp;&#x25BE;</span></td>
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000619 <td>File</td>
Ted Kremenekebb74132008-09-21 06:58:09 +0000620 <td class="Q">Line</td>
Ted Kremenek81983112008-09-28 04:13:09 +0000621 <td class="Q">Path Length</td>
Ted Kremenek2645c772008-07-07 16:58:44 +0000622 <td class="sorttable_nosort"></td>
Ted Kremenekebb74132008-09-21 06:58:09 +0000623 <!-- REPORTBUGCOL -->
624</tr></thead>
625<tbody>
Ted Kremenek5744dc22008-04-02 18:03:36 +0000626ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000627
Ted Kremenek991c54b2008-08-08 20:46:42 +0000628 my $prefix = GetPrefix();
629 my $regex;
630 my $InFileRegex;
631 my $InFilePrefix = "File:</td><td>";
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000632
Ted Kremenek991c54b2008-08-08 20:46:42 +0000633 if (defined $prefix) {
634 $regex = qr/^\Q$prefix\E/is;
635 $InFileRegex = qr/\Q$InFilePrefix$prefix\E/is;
636 }
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000637
Ted Kremenekebb74132008-09-21 06:58:09 +0000638 for my $row ( sort { $a->[2] cmp $b->[2] } @Index ) {
639 my $x = "$row->[1]:$row->[2]";
640 $x = lc $x;
641 $x =~ s/[ ,'":\/()]+/_/g;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000642
Ted Kremenek991c54b2008-08-08 20:46:42 +0000643 my $ReportFile = $row->[0];
Ted Kremenekebb74132008-09-21 06:58:09 +0000644
645 print OUT "<tr class=\"bt_$x\">";
646 print OUT "<td class=\"DESC\">";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000647 print OUT $row->[1];
Ted Kremenekebb74132008-09-21 06:58:09 +0000648 print OUT "</td>";
649 print OUT "<td class=\"DESC\">";
650 print OUT $row->[2];
651 print OUT "</td>";
652
653 # Update the file prefix.
654 my $fname = $row->[3];
Ted Kremenekebb74132008-09-21 06:58:09 +0000655
Ted Kremenek991c54b2008-08-08 20:46:42 +0000656 if (defined $regex) {
657 $fname =~ s/$regex//;
658 UpdateInFilePath("$Dir/$ReportFile", $InFileRegex, $InFilePrefix)
659 }
Ted Kremenekebb74132008-09-21 06:58:09 +0000660
Ted Kremenek91639ef2008-09-22 17:42:31 +0000661 print OUT "<td>";
Ted Kremenekebb74132008-09-21 06:58:09 +0000662 my @fname = split /\//,$fname;
663 if ($#fname > 0) {
664 while ($#fname >= 0) {
665 my $x = shift @fname;
666 print OUT $x;
667 if ($#fname >= 0) {
668 print OUT "<span class=\"W\"> </span>/";
669 }
670 }
671 }
672 else {
673 print OUT $fname;
Ted Kremenek91639ef2008-09-22 17:42:31 +0000674 }
Ted Kremenekebb74132008-09-21 06:58:09 +0000675 print OUT "</td>";
676
677 # Print out the quantities.
Ted Kremenek81983112008-09-28 04:13:09 +0000678 for my $j ( 4 .. 5 ) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000679 print OUT "<td class=\"Q\">$row->[$j]</td>";
680 }
681
Ted Kremenek991c54b2008-08-08 20:46:42 +0000682 # Print the rest of the columns.
Ted Kremenek81983112008-09-28 04:13:09 +0000683 for (my $j = 6; $j <= $#{$row}; ++$j) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000684 print OUT "<td>$row->[$j]</td>"
Ted Kremenek991c54b2008-08-08 20:46:42 +0000685 }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000686
Ted Kremenek991c54b2008-08-08 20:46:42 +0000687 # Emit the "View" link.
Ted Kremenek68005dd2008-09-22 17:39:18 +0000688 print OUT "<td><a href=\"$ReportFile#EndPath\">View Report</a></td>";
Ted Kremenek3cea9ee2008-07-30 17:58:08 +0000689
Daniel Dunbare43038e2008-09-19 23:18:44 +0000690 # Emit REPORTBUG markers.
Ted Kremenekebb74132008-09-21 06:58:09 +0000691 print OUT "\n<!-- REPORTBUG id=\"$ReportFile\" -->\n";
Daniel Dunbare43038e2008-09-19 23:18:44 +0000692
Ted Kremenek991c54b2008-08-08 20:46:42 +0000693 # End the row.
694 print OUT "</tr>\n";
695 }
696
Ted Kremenekebb74132008-09-21 06:58:09 +0000697 print OUT "</tbody>\n</table>\n\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000698 }
699
700 if ($Crashes) {
701 # Read the crash directory for files.
702 opendir(DIR, "$Dir/crashes");
Ted Kremenek82a12532008-09-25 00:25:16 +0000703 my @files = grep { /[.]info.txt$/ } readdir(DIR);
Ted Kremenek991c54b2008-08-08 20:46:42 +0000704 closedir(DIR);
705
706 if (scalar(@files)) {
707 print OUT <<ENDTEXT;
Ted Kremenek7cba1122008-09-22 01:35:58 +0000708<h2>Analyzer Failures</h2>
Ted Kremenek991c54b2008-08-08 20:46:42 +0000709
Ted Kremenek5d31f832008-08-18 18:38:29 +0000710<p>The analyzer had problems processing the following files:</p>
Ted Kremenek991c54b2008-08-08 20:46:42 +0000711
712<table>
Ted Kremenek9f9b1fd2008-09-12 22:49:36 +0000713<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 +0000714ENDTEXT
715
716 foreach my $file (sort @files) {
Ted Kremenek82a12532008-09-25 00:25:16 +0000717 $file =~ /(.+).info.txt$/;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000718 # Get the preprocessed file.
719 my $ppfile = $1;
720 # Open the info file and get the name of the source file.
721 open (INFO, "$Dir/crashes/$file") or
722 die "Cannot open $Dir/crashes/$file\n";
723 my $srcfile = <INFO>;
Ted Kremenek5d31f832008-08-18 18:38:29 +0000724 chomp $srcfile;
725 my $problem = <INFO>;
726 chomp $problem;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000727 close (INFO);
728 # Print the information in the table.
Ted Kremenekd52e4252008-08-25 20:45:07 +0000729 my $prefix = GetPrefix();
Ted Kremenek9f9b1fd2008-09-12 22:49:36 +0000730 if (defined $prefix) { $srcfile =~ s/^\Q$prefix//; }
731 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 +0000732 my $ppfile_clang = $ppfile;
733 $ppfile_clang =~ s/[.](.+)$/.clang.$1/;
Daniel Dunbar7ebe0ed2008-09-25 06:05:31 +0000734 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 +0000735 }
736
737 print OUT <<ENDTEXT;
738</table>
Daniel Dunbarce723ce2008-09-25 01:10:50 +0000739<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 +0000740ENDTEXT
741 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000742 }
743
Ted Kremenek991c54b2008-08-08 20:46:42 +0000744 print OUT "</body></html>\n";
Ted Kremenek5744dc22008-04-02 18:03:36 +0000745 close(OUT);
Ted Kremenek3ce12072008-09-22 17:50:47 +0000746 CopyFiles($Dir);
Ted Kremenek20161e92008-07-15 20:18:21 +0000747
748 # Make sure $Dir and $BaseDir are world readable/executable.
749 system("chmod", "755", $Dir);
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000750 if (defined $BaseDir) { system("chmod", "755", $BaseDir); }
Ted Kremenek20161e92008-07-15 20:18:21 +0000751
Ted Kremenek23cfca32008-06-16 22:40:14 +0000752 my $Num = scalar(@Index);
Ted Kremenek150c2122008-07-11 19:15:05 +0000753 Diag("$Num bugs found.\n");
754 if ($Num > 0 && -r "$Dir/index.html") {
Ted Kremenek5950b3f2008-09-22 06:47:01 +0000755 Diag("Run 'scan-view $Dir' to examine bug reports.\n");
Ted Kremenek150c2122008-07-11 19:15:05 +0000756 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000757
Ted Kremenek991c54b2008-08-08 20:46:42 +0000758 DiagCrashes($Dir) if ($Crashes);
759
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000760 return $Num;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000761}
762
763##----------------------------------------------------------------------------##
Ted Kremenekdab11102008-04-02 04:43:42 +0000764# RunBuildCommand - Run the build command.
765##----------------------------------------------------------------------------##
766
Ted Kremenek6b628982008-04-30 23:47:12 +0000767sub AddIfNotPresent {
768 my $Args = shift;
769 my $Arg = shift;
770 my $found = 0;
771
772 foreach my $k (@$Args) {
773 if ($k eq $Arg) {
774 $found = 1;
775 last;
776 }
777 }
778
779 if ($found == 0) {
780 push @$Args, $Arg;
781 }
782}
783
Ted Kremenekdab11102008-04-02 04:43:42 +0000784sub RunBuildCommand {
785
786 my $Args = shift;
Ted Kremenek7442ca62008-04-02 16:04:51 +0000787 my $IgnoreErrors = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000788 my $Cmd = $Args->[0];
Ted Kremenek6195c372008-06-02 21:52:47 +0000789 my $CCAnalyzer = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000790
Ted Kremenek3301cb12008-06-30 18:18:16 +0000791 # Get only the part of the command after the last '/'.
792 if ($Cmd =~ /\/([^\/]+)$/) {
793 $Cmd = $1;
794 }
795
Ted Kremenek92548fe2008-11-19 01:46:21 +0000796 if ($Cmd =~ /(.*\/?gcc[^\/]*$)/ or
797 $Cmd =~ /(.*\/?cc[^\/]*$)/ or
798 $Cmd =~ /(.*\/?llvm-gcc[^\/]*$)/ or
799 $Cmd =~ /(.*\/?ccc-analyzer[^\/]*$)/) {
800
801 if (!($Cmd =~ /ccc-analyzer/) and !defined $ENV{"CCC_CC"}) {
802 $ENV{"CCC_CC"} = $1;
803 }
804
Ted Kremenekdab11102008-04-02 04:43:42 +0000805 shift @$Args;
Ted Kremenek6195c372008-06-02 21:52:47 +0000806 unshift @$Args, $CCAnalyzer;
Ted Kremenekdab11102008-04-02 04:43:42 +0000807 }
Ted Kremenek7442ca62008-04-02 16:04:51 +0000808 elsif ($IgnoreErrors) {
809 if ($Cmd eq "make" or $Cmd eq "gmake") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000810 AddIfNotPresent($Args,"-k");
Ted Kremenek8912b542008-05-13 21:28:02 +0000811 AddIfNotPresent($Args,"-i");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000812 }
813 elsif ($Cmd eq "xcodebuild") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000814 AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000815 }
Ted Kremenek6b628982008-04-30 23:47:12 +0000816 }
817
Ted Kremenek6b628982008-04-30 23:47:12 +0000818 if ($Cmd eq "xcodebuild") {
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000819 # Disable distributed builds for xcodebuild.
Ted Kremenek6b628982008-04-30 23:47:12 +0000820 AddIfNotPresent($Args,"-nodistribute");
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000821
822 # Disable PCH files until clang supports them.
823 AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO");
Ted Kremenek915e9722008-05-27 23:18:07 +0000824
825 # When 'CC' is set, xcodebuild uses it to do all linking, even if we are
826 # linking C++ object files. Set 'LDPLUSPLUS' so that xcodebuild uses 'g++'
827 # when linking such files.
Ted Kremenek95aa1052008-09-04 17:52:41 +0000828 die if (!defined $CXX);
829 my $LDPLUSPLUS = `which $CXX`;
Ted Kremenek915e9722008-05-27 23:18:07 +0000830 $LDPLUSPLUS =~ s/\015?\012//; # strip newlines
831 $ENV{'LDPLUSPLUS'} = $LDPLUSPLUS;
Ted Kremenek6b628982008-04-30 23:47:12 +0000832 }
Ted Kremenekdab11102008-04-02 04:43:42 +0000833
Ted Kremenek5a4ddaf2008-08-25 20:10:45 +0000834 return (system(@$Args) >> 8);
Ted Kremenekdab11102008-04-02 04:43:42 +0000835}
836
Ted Kremenekdab11102008-04-02 04:43:42 +0000837##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000838# DisplayHelp - Utility function to display all help options.
839##----------------------------------------------------------------------------##
840
Sam Bishopa0e22662008-04-02 03:35:43 +0000841sub DisplayHelp {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000842
Ted Kremenek5744dc22008-04-02 18:03:36 +0000843print <<ENDTEXT;
Sam Bishopa0e22662008-04-02 03:35:43 +0000844USAGE: $Prog [options] <build command> [build options]
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000845
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000846ENDTEXT
847
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000848 if (defined $BuildName) {
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000849 print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n";
850 }
851
852print <<ENDTEXT;
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000853OPTIONS:
854
Ted Kremeneke15fa272008-10-13 21:46:42 +0000855 -analyze-headers - Also analyze functions in #included files.
856
Zhongxing Xu07c37672008-10-27 14:26:32 +0000857 -store [model] - Specify the store model used by the analyzer. By default,
858 the 'basic' store model is used. 'region' specifies a field-
859 sensitive store model. Be warned that the 'region' model
860 is still in very early testing phase and may often crash.
861
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000862 -o - Target directory for HTML report files. Subdirectories
Sam Bishopa0e22662008-04-02 03:35:43 +0000863 will be created as needed to represent separate "runs" of
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000864 the analyzer. If this option is not specified, a directory
Ted Kremenekffda0b42008-10-31 05:48:42 +0000865 is created in /tmp (TMPDIR on Mac OS X) to store the reports.
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000866
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000867 -h - Display this message.
868 --help
Ted Kremenek1262fc42008-05-14 20:10:33 +0000869
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000870 -k - Add a "keep on going" option to the specified build command.
871 --keep-going This option currently supports make and xcodebuild.
Ted Kremenekf02e8db2008-04-02 16:41:25 +0000872 This is a convenience option; one can specify this
873 behavior directly using build options.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000874
Ted Kremenek7cba1122008-09-22 01:35:58 +0000875 --html-title [title] - Specify the title used on generated HTML pages.
876 --html-title=[title] If not specified, a default title will be used.
877
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000878 -plist - By default the output of scan-build is a set of HTML files.
879 This option outputs the results as a set of .plist files.
880
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000881 --status-bugs - By default, the exit status of $Prog is the same as the
882 executed build command. Specifying this option causes the
883 exit status of $Prog to be 1 if it found potential bugs
884 and 0 otherwise.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000885
Ted Kremenek386c6932008-09-03 17:59:35 +0000886 --use-cc [compiler path] - By default, $Prog uses 'gcc' to compile and link
887 --use-cc=[compiler path] your C and Objective-C code. Use this option
888 to specify an alternate compiler.
889
890 --use-c++ [compiler path] - By default, $Prog uses 'g++' to compile and link
891 --use-c++=[compiler path] your C++ and Objective-C++ code. Use this option
892 to specify an alternate compiler.
Ted Kremenekf17ef3c2008-08-21 21:47:09 +0000893
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000894 -v - Verbose output from $Prog and the analyzer.
Ted Kremenek386c6932008-09-03 17:59:35 +0000895 A second and third '-v' increases verbosity.
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000896
897 -V - View analysis results in a web browser when the build
898 --view completes.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000899
Ted Kremenekb7770c02008-07-15 17:06:13 +0000900
Ted Kremenek386c6932008-09-03 17:59:35 +0000901AVAILABLE ANALYSES (multiple analyses may be specified):
Ted Kremenekd52e4252008-08-25 20:45:07 +0000902
903ENDTEXT
Ted Kremenekb7770c02008-07-15 17:06:13 +0000904
905 foreach my $Analysis (sort keys %AvailableAnalyses) {
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000906 if (defined $AnalysesDefaultEnabled{$Analysis}) {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000907 print " (+)";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000908 }
909 else {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000910 print " ";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000911 }
912
913 print " $Analysis $AvailableAnalyses{$Analysis}\n";
914 }
915
916print <<ENDTEXT
917
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000918 NOTE: "(+)" indicates that an analysis is enabled by default unless one
919 or more analysis options are specified
Ted Kremenekb7770c02008-07-15 17:06:13 +0000920
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000921BUILD OPTIONS
922
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000923 You can specify any build option acceptable to the build command.
Ted Kremenek39eefde2008-04-02 16:47:27 +0000924
Ted Kremenek5744dc22008-04-02 18:03:36 +0000925EXAMPLE
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000926
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000927 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000928
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000929 The above example causes analysis reports to be deposited into
930 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
931 A different subdirectory is created each time $Prog analyzes a project.
932 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000933
934ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000935}
936
937##----------------------------------------------------------------------------##
Ted Kremenek7cba1122008-09-22 01:35:58 +0000938# HtmlEscape - HTML entity encode characters that are special in HTML
939##----------------------------------------------------------------------------##
940
941sub HtmlEscape {
942 # copy argument to new variable so we don't clobber the original
943 my $arg = shift || '';
944 my $tmp = $arg;
Ted Kremenek87f8de72008-11-03 07:44:16 +0000945 $tmp =~ s/&/&amp;/g;
946 $tmp =~ s/</&lt;/g;
947 $tmp =~ s/>/&gt;/g;
Ted Kremenek7cba1122008-09-22 01:35:58 +0000948 return $tmp;
949}
950
951##----------------------------------------------------------------------------##
952# ShellEscape - backslash escape characters that are special to the shell
953##----------------------------------------------------------------------------##
954
955sub ShellEscape {
956 # copy argument to new variable so we don't clobber the original
957 my $arg = shift || '';
Ted Kremenek87f8de72008-11-03 07:44:16 +0000958 if ($arg =~ /["\s]/) { return "'" . $arg . "'"; }
959 return $arg;
Ted Kremenek7cba1122008-09-22 01:35:58 +0000960}
961
962##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000963# Process command-line arguments.
964##----------------------------------------------------------------------------##
965
Ted Kremeneke15fa272008-10-13 21:46:42 +0000966my $AnalyzeHeaders = 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000967my $HtmlDir; # Parent directory to store HTML files.
968my $IgnoreErrors = 0; # Ignore build errors.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000969my $ViewResults = 0; # View results when the build terminates.
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000970my $ExitStatusFoundBugs = 0; # Exit status reflects whether bugs were found
Ted Kremenekb7770c02008-07-15 17:06:13 +0000971my @AnalysesToRun;
Zhongxing Xu07c37672008-10-27 14:26:32 +0000972my $StoreModel;
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000973my $OutputFormat;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000974
975if (!@ARGV) {
976 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000977 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000978}
979
980while (@ARGV) {
981
982 # Scan for options we recognize.
983
984 my $arg = $ARGV[0];
985
Sam Bishop2f2418e2008-04-03 14:29:47 +0000986 if ($arg eq "-h" or $arg eq "--help") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000987 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000988 exit 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000989 }
990
Ted Kremeneke15fa272008-10-13 21:46:42 +0000991 if ($arg eq '-analyze-headers') {
992 shift @ARGV;
993 $AnalyzeHeaders = 1;
994 next;
995 }
996
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000997 if (defined $AvailableAnalyses{$arg}) {
Ted Kremenek1262fc42008-05-14 20:10:33 +0000998 shift @ARGV;
Ted Kremenekb7770c02008-07-15 17:06:13 +0000999 push @AnalysesToRun, $arg;
Ted Kremenek1262fc42008-05-14 20:10:33 +00001000 next;
1001 }
1002
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001003 if ($arg eq "-o") {
1004 shift @ARGV;
1005
1006 if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +00001007 DieDiag("'-o' option requires a target directory name.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001008 }
1009
1010 $HtmlDir = shift @ARGV;
1011 next;
1012 }
Ted Kremenek7cba1122008-09-22 01:35:58 +00001013
1014 if ($arg =~ /^--html-title(=(.+))?$/) {
1015 shift @ARGV;
1016
1017 if ($2 eq '') {
1018 if (!@ARGV) {
1019 DieDiag("'--html-title' option requires a string.\n");
1020 }
1021
1022 $HtmlTitle = shift @ARGV;
1023 } else {
1024 $HtmlTitle = $2;
1025 }
1026
1027 next;
1028 }
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001029
Ted Kremenek2b74ab62008-04-01 21:22:03 +00001030 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001031 shift @ARGV;
1032 $IgnoreErrors = 1;
1033 next;
1034 }
1035
Ted Kremenekf17ef3c2008-08-21 21:47:09 +00001036 if ($arg =~ /^--use-cc(=(.+))?$/) {
1037 shift @ARGV;
1038 my $cc;
1039
1040 if ($2 eq "") {
1041 if (!@ARGV) {
1042 DieDiag("'--use-cc' option requires a compiler executable name.\n");
1043 }
1044 $cc = shift @ARGV;
1045 }
1046 else {
1047 $cc = $2;
1048 }
1049
1050 $ENV{"CCC_CC"} = $cc;
1051 next;
1052 }
1053
Ted Kremenek7cba1122008-09-22 01:35:58 +00001054 if ($arg =~ /^--use-c\+\+(=(.+))?$/) {
Ted Kremenek386c6932008-09-03 17:59:35 +00001055 shift @ARGV;
1056
1057 if ($2 eq "") {
1058 if (!@ARGV) {
1059 DieDiag("'--use-c++' option requires a compiler executable name.\n");
1060 }
1061 $CXX = shift @ARGV;
1062 }
1063 else {
1064 $CXX = $2;
1065 }
1066 next;
1067 }
1068
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001069 if ($arg eq "-v") {
1070 shift @ARGV;
1071 $Verbose++;
1072 next;
1073 }
1074
Ted Kremenek7f8a3252008-04-02 18:42:49 +00001075 if ($arg eq "-V" or $arg eq "--view") {
1076 shift @ARGV;
1077 $ViewResults = 1;
1078 next;
1079 }
1080
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001081 if ($arg eq "--status-bugs") {
1082 shift @ARGV;
1083 $ExitStatusFoundBugs = 1;
1084 next;
1085 }
Zhongxing Xu07c37672008-10-27 14:26:32 +00001086
1087 if ($arg eq "-store") {
1088 shift @ARGV;
1089 $StoreModel = '-analyzer-store-' . shift @ARGV;
1090 next;
1091 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001092
Ted Kremenekdb4f5f22008-11-04 00:02:53 +00001093 if ($arg eq "-plist") {
1094 shift @ARGV;
1095 $OutputFormat = "plist";
1096 next;
1097 }
1098
Ted Kremenek23cfca32008-06-16 22:40:14 +00001099 DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/);
Ted Kremenek0062ad42008-04-02 16:35:01 +00001100
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001101 last;
1102}
1103
1104if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +00001105 Diag("No build command specified.\n\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001106 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +00001107 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001108}
1109
Ted Kremenek7cba1122008-09-22 01:35:58 +00001110$CmdArgs = HtmlEscape(join(' ', map(ShellEscape($_), @ARGV)));
1111$HtmlTitle = "${CurrentDirSuffix} - scan-build results"
1112 unless (defined($HtmlTitle));
Ted Kremenek386c6932008-09-03 17:59:35 +00001113
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001114# Determine the output directory for the HTML reports.
Ted Kremenek684bb092008-04-18 15:18:20 +00001115my $BaseDir = $HtmlDir;
Sam Bishopa0e22662008-04-02 03:35:43 +00001116$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001117
1118# Set the appropriate environment variables.
Sam Bishopa0e22662008-04-02 03:35:43 +00001119SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001120
Ted Kremeneke15fa272008-10-13 21:46:42 +00001121my $Cmd = Cwd::realpath("$RealBin/ccc-analyzer");
Ted Kremenek0b6c1532008-04-08 20:22:12 +00001122
Ted Kremenek23cfca32008-06-16 22:40:14 +00001123DieDiag("Executable 'ccc-analyzer' does not exist at '$Cmd'\n")
Ted Kremenek0b6c1532008-04-08 20:22:12 +00001124 if (! -x $Cmd);
Ted Kremenekf22eacb2008-04-18 22:00:56 +00001125
Ted Kremenekb7770c02008-07-15 17:06:13 +00001126if (! -x $ClangSB) {
1127 Diag("'clang' executable not found in '$RealBin'.\n");
1128 Diag("Using 'clang' from path.\n");
Ted Kremenekf22eacb2008-04-18 22:00:56 +00001129}
Ted Kremenek0b6c1532008-04-08 20:22:12 +00001130
Ted Kremenek95aa1052008-09-04 17:52:41 +00001131if (defined $CXX) {
1132 $ENV{'CXX'} = $CXX;
1133}
1134else {
1135 $CXX = 'g++'; # This variable is used by other parts of scan-build
1136 # that need to know a default C++ compiler to fall back to.
1137}
1138
Ted Kremenek4f4b17d2008-04-03 20:08:18 +00001139$ENV{'CC'} = $Cmd;
Ted Kremenekf22eacb2008-04-18 22:00:56 +00001140$ENV{'CLANG'} = $Clang;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001141
1142if ($Verbose >= 2) {
1143 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
1144}
1145
Ted Kremeneka9525c92008-05-12 22:07:14 +00001146if ($Verbose >= 3) {
1147 $ENV{'CCC_ANALYZER_LOG'} = 1;
1148}
1149
Ted Kremenek90125992008-07-15 23:41:32 +00001150if (scalar(@AnalysesToRun) == 0) {
1151 foreach my $key (keys %AnalysesDefaultEnabled) {
1152 push @AnalysesToRun,$key;
1153 }
Ted Kremenek01006782008-07-02 23:16:10 +00001154}
Ted Kremenek1262fc42008-05-14 20:10:33 +00001155
Ted Kremeneke15fa272008-10-13 21:46:42 +00001156if ($AnalyzeHeaders) {
1157 push @AnalysesToRun,"-analyzer-opt-analyze-headers";
1158}
1159
Ted Kremenek90125992008-07-15 23:41:32 +00001160$ENV{'CCC_ANALYZER_ANALYSIS'} = join ' ',@AnalysesToRun;
1161
Zhongxing Xu3cab2b12008-11-02 10:58:16 +00001162if (defined $StoreModel) {
Zhongxing Xu07c37672008-10-27 14:26:32 +00001163 $ENV{'CCC_ANALYZER_STORE_MODEL'} = $StoreModel;
1164}
1165
Ted Kremenekdb4f5f22008-11-04 00:02:53 +00001166if (defined $OutputFormat) {
1167 $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'} = $OutputFormat;
1168}
1169
1170
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001171# Run the build.
Ted Kremenek5656a982008-07-15 17:09:28 +00001172my $ExitStatus = RunBuildCommand(\@ARGV, $IgnoreErrors, $Cmd);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001173
Ted Kremenek655aba72008-11-04 00:22:12 +00001174if (defined $OutputFormat and $OutputFormat eq "plist") {
Ted Kremenek50534dc2008-09-22 17:38:23 +00001175 Diag "Analysis run complete.\n";
Ted Kremenekdb4f5f22008-11-04 00:02:53 +00001176 Diag "Analysis results (plist files) deposited in '$HtmlDir'\n";
Ted Kremenek7f8a3252008-04-02 18:42:49 +00001177}
Ted Kremenekdb4f5f22008-11-04 00:02:53 +00001178else {
1179 # Postprocess the HTML directory.
1180 my $NumBugs = Postprocess($HtmlDir, $BaseDir);
Ted Kremenek5656a982008-07-15 17:09:28 +00001181
Ted Kremenekdb4f5f22008-11-04 00:02:53 +00001182 if ($ViewResults and -r "$HtmlDir/index.html") {
1183 Diag "Analysis run complete.\n";
1184 Diag "Viewing analysis results in '$HtmlDir' using scan-view.\n";
1185 my $ScanView = Cwd::realpath("$RealBin/scan-view");
1186 if (! -x $ScanView) { $ScanView = "scan-view"; }
1187 exec $ScanView, "$HtmlDir";
1188 }
1189
1190 if ($ExitStatusFoundBugs) {
1191 exit 1 if ($NumBugs > 0);
1192 exit 0;
1193 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001194}
1195
Ted Kremenek5656a982008-07-15 17:09:28 +00001196exit $ExitStatus;
1197