blob: dae86f4b5e9c86715ae98f131382806237edbaca [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 Kremenekcd25c132008-12-03 19:50:37 +000022use Cwd qw/ getcwd abs_path /;
Ted Kremenek7cba1122008-09-22 01:35:58 +000023use 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 Kremenek9cc8c2c2008-04-01 20:47:38 +000029
Ted Kremenek0e689382008-09-11 18:17:51 +000030my $TERM = $ENV{'TERM'};
31my $UseColor = (defined $TERM and $TERM eq 'xterm-color' and -t STDOUT
32 and defined $ENV{'SCAN_BUILD_COLOR'});
Ted Kremenek23cfca32008-06-16 22:40:14 +000033
Ted Kremenek7cba1122008-09-22 01:35:58 +000034my $UserName = HtmlEscape(getpwuid($<) || 'unknown');
35my $HostName = HtmlEscape(hostname() || 'unknown');
36my $CurrentDir = HtmlEscape(getcwd());
37my $CurrentDirSuffix = basename($CurrentDir);
38
39my $CmdArgs;
40
41my $HtmlTitle;
42
43my $Date = localtime();
44
Ted Kremenekb7770c02008-07-15 17:06:13 +000045##----------------------------------------------------------------------------##
46# Diagnostics
47##----------------------------------------------------------------------------##
48
Ted Kremenek23cfca32008-06-16 22:40:14 +000049sub Diag {
50 if ($UseColor) {
51 print BOLD, MAGENTA "$Prog: @_";
52 print RESET;
53 }
54 else {
55 print "$Prog: @_";
56 }
57}
58
Ted Kremenek991c54b2008-08-08 20:46:42 +000059sub DiagCrashes {
60 my $Dir = shift;
Ted Kremenek938eef12009-02-17 23:31:05 +000061 Diag ("The analyzer encountered problems on some source files.\n");
62 Diag ("Preprocessed versions of these sources were deposited in '$Dir/failures'.\n");
Ted Kremenek991c54b2008-08-08 20:46:42 +000063 Diag ("Please consider submitting a bug report using these files:\n");
Nico Webere2c86632011-08-28 11:50:56 +000064 Diag (" http://clang-analyzer.llvm.org/filing_bugs.html\n")
Ted Kremenek991c54b2008-08-08 20:46:42 +000065}
66
Ted Kremenek23cfca32008-06-16 22:40:14 +000067sub DieDiag {
68 if ($UseColor) {
69 print BOLD, RED "$Prog: ";
70 print RESET, RED @_;
71 print RESET;
72 }
73 else {
74 print "$Prog: ", @_;
75 }
76 exit(0);
77}
78
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000079##----------------------------------------------------------------------------##
Ted Kremenekb7770c02008-07-15 17:06:13 +000080# Some initial preprocessing of Clang options.
81##----------------------------------------------------------------------------##
82
Ted Kremenek2a3a8b92009-12-11 22:44:53 +000083# Find 'clang'
Ted Kremenekfd9df0e2009-05-09 19:19:28 +000084my $ClangSB = Cwd::realpath("$RealBin/bin/clang");
Ted Kremenek8d10cdd2009-02-20 04:34:29 +000085if (!defined $ClangSB || ! -x $ClangSB) {
Ted Kremenekfd9df0e2009-05-09 19:19:28 +000086 $ClangSB = Cwd::realpath("$RealBin/clang");
87}
Ted Kremeneke01ca512010-02-05 20:34:14 +000088my $Clang;
89if (!defined $ClangSB || ! -x $ClangSB) {
90 # Default to looking for 'clang' in the path.
Ted Kremenek64922002010-02-12 00:12:25 +000091 $Clang = `which clang`;
92 chomp $Clang;
93 if ($Clang eq "") {
94 DieDiag("No 'clang' executable found in path.");
95 }
Ted Kremenek2a3a8b92009-12-11 22:44:53 +000096}
Ted Kremeneke01ca512010-02-05 20:34:14 +000097else {
98 $Clang = $ClangSB;
99}
100my $ClangCXX = $Clang . "++";
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000101
Ted Kremenekb7770c02008-07-15 17:06:13 +0000102##----------------------------------------------------------------------------##
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000103# GetHTMLRunDir - Construct an HTML directory name for the current sub-run.
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000104##----------------------------------------------------------------------------##
105
Sam Bishopa0e22662008-04-02 03:35:43 +0000106sub GetHTMLRunDir {
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000107 die "Not enough arguments." if (@_ == 0);
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000108 my $Dir = shift @_;
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000109 my $TmpMode = 0;
110 if (!defined $Dir) {
Ted Kremenekffda0b42008-10-31 05:48:42 +0000111 if (`uname` =~ /Darwin/) {
112 $Dir = $ENV{'TMPDIR'};
113 if (!defined $Dir) { $Dir = "/tmp"; }
114 }
115 else {
116 $Dir = "/tmp";
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000117 }
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000118 $TmpMode = 1;
119 }
Ted Kremenekbf762c92009-02-24 02:38:02 +0000120
121 # Chop off any trailing '/' characters.
122 while ($Dir =~ /\/$/) { chop $Dir; }
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000123
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000124 # Get current date and time.
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000125 my @CurrentTime = localtime();
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000126 my $year = $CurrentTime[5] + 1900;
127 my $day = $CurrentTime[3];
128 my $month = $CurrentTime[4] + 1;
Ted Kremenek9d7405f2008-05-14 17:23:56 +0000129 my $DateString = sprintf("%d-%02d-%02d", $year, $month, $day);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000130
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000131 # Determine the run number.
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000132 my $RunNumber;
133
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000134 if (-d $Dir) {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000135 if (! -r $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000136 DieDiag("directory '$Dir' exists but is not readable.\n");
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000137 }
138 # Iterate over all files in the specified directory.
139 my $max = 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000140 opendir(DIR, $Dir);
Ted Kremenek29da6c52008-08-07 17:57:34 +0000141 my @FILES = grep { -d "$Dir/$_" } readdir(DIR);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000142 closedir(DIR);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000143
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000144 foreach my $f (@FILES) {
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000145 # Strip the prefix '$Prog-' if we are dumping files to /tmp.
146 if ($TmpMode) {
147 next if (!($f =~ /^$Prog-(.+)/));
148 $f = $1;
149 }
150
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000151 my @x = split/-/, $f;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000152 next if (scalar(@x) != 4);
153 next if ($x[0] != $year);
154 next if ($x[1] != $month);
155 next if ($x[2] != $day);
156
157 if ($x[3] > $max) {
158 $max = $x[3];
159 }
160 }
161
162 $RunNumber = $max + 1;
163 }
164 else {
165
166 if (-x $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000167 DieDiag("'$Dir' exists but is not a directory.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000168 }
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000169
170 if ($TmpMode) {
Ted Kremenek445fa772008-10-10 00:17:08 +0000171 DieDiag("The directory '/tmp' does not exist or cannot be accessed.\n");
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000172 }
173
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000174 # $Dir does not exist. It will be automatically created by the
175 # clang driver. Set the run number to 1.
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000176
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000177 $RunNumber = 1;
178 }
179
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000180 die "RunNumber must be defined!" if (!defined $RunNumber);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000181
182 # Append the run number.
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000183 my $NewDir;
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000184 if ($TmpMode) {
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000185 $NewDir = "$Dir/$Prog-$DateString-$RunNumber";
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000186 }
187 else {
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000188 $NewDir = "$Dir/$DateString-$RunNumber";
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000189 }
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000190 system 'mkdir','-p',$NewDir;
191 return $NewDir;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000192}
193
Sam Bishopa0e22662008-04-02 03:35:43 +0000194sub SetHtmlEnv {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000195
196 die "Wrong number of arguments." if (scalar(@_) != 2);
197
198 my $Args = shift;
199 my $Dir = shift;
200
201 die "No build command." if (scalar(@$Args) == 0);
202
203 my $Cmd = $$Args[0];
Ted Kremenek9e4a1bb2011-04-01 18:47:06 +0000204
205 if ($Cmd =~ /configure/ || $Cmd =~ /autogen/) {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000206 return;
207 }
208
209 if ($Verbose) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000210 Diag("Emitting reports for this run to '$Dir'.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000211 }
212
213 $ENV{'CCC_ANALYZER_HTML'} = $Dir;
214}
215
216##----------------------------------------------------------------------------##
Ted Kremenek57cf4462008-04-18 15:09:30 +0000217# ComputeDigest - Compute a digest of the specified file.
218##----------------------------------------------------------------------------##
219
220sub ComputeDigest {
221 my $FName = shift;
Ted Kremenek23cfca32008-06-16 22:40:14 +0000222 DieDiag("Cannot read $FName to compute Digest.\n") if (! -r $FName);
Ted Kremeneka6e24812008-04-19 18:05:48 +0000223
224 # Use Digest::MD5. We don't have to be cryptographically secure. We're
Ted Kremenek7ea02e62008-04-19 18:07:44 +0000225 # just looking for duplicate files that come from a non-malicious source.
226 # We use Digest::MD5 because it is a standard Perl module that should
Ted Kremenek63c20172008-08-04 17:34:06 +0000227 # come bundled on most systems.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000228 open(FILE, $FName) or DieDiag("Cannot open $FName when computing Digest.\n");
Ted Kremeneka6e24812008-04-19 18:05:48 +0000229 binmode FILE;
230 my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest;
231 close(FILE);
232
Ted Kremenek63c20172008-08-04 17:34:06 +0000233 # Return the digest.
Ted Kremeneka6e24812008-04-19 18:05:48 +0000234 return $Result;
Ted Kremenek57cf4462008-04-18 15:09:30 +0000235}
236
237##----------------------------------------------------------------------------##
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000238# UpdatePrefix - Compute the common prefix of files.
239##----------------------------------------------------------------------------##
240
241my $Prefix;
242
243sub UpdatePrefix {
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000244 my $x = shift;
245 my $y = basename($x);
246 $x =~ s/\Q$y\E$//;
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000247
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000248 if (!defined $Prefix) {
249 $Prefix = $x;
250 return;
251 }
252
Ted Kremenek20b2bae2008-09-11 21:15:10 +0000253 chop $Prefix while (!($x =~ /^\Q$Prefix/));
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000254}
255
256sub GetPrefix {
257 return $Prefix;
258}
259
260##----------------------------------------------------------------------------##
261# UpdateInFilePath - Update the path in the report file.
262##----------------------------------------------------------------------------##
263
264sub UpdateInFilePath {
265 my $fname = shift;
266 my $regex = shift;
267 my $newtext = shift;
Ted Kremenek63c20172008-08-04 17:34:06 +0000268
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000269 open (RIN, $fname) or die "cannot open $fname";
Ted Kremenek63c20172008-08-04 17:34:06 +0000270 open (ROUT, ">", "$fname.tmp") or die "cannot open $fname.tmp";
271
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000272 while (<RIN>) {
273 s/$regex/$newtext/;
274 print ROUT $_;
275 }
Ted Kremenek63c20172008-08-04 17:34:06 +0000276
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000277 close (ROUT);
278 close (RIN);
Ted Kremenek20161e92008-07-15 20:18:21 +0000279 system("mv", "$fname.tmp", $fname);
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000280}
281
282##----------------------------------------------------------------------------##
Tom Care4f2b10b2010-09-30 01:12:05 +0000283# AddStatLine - Decode and insert a statistics line into the database.
284##----------------------------------------------------------------------------##
285
286sub AddStatLine {
287 my $Line = shift;
288 my $Stats = shift;
289
290 print $Line . "\n";
291
292 my $Regex = qr/(.*?)\ :\ (.*?)\ ->\ Total\ CFGBlocks:\ (\d+)\ \|\ Unreachable
Ted Kremenek6bdda822011-04-27 23:43:27 +0000293 \ CFGBlocks:\ (\d+)\ \|\ Exhausted\ Block:\ (yes|no)\ \|\ Empty\ WorkList:
Tom Care4f2b10b2010-09-30 01:12:05 +0000294 \ (yes|no)/x;
295
296 if ($Line !~ $Regex) {
297 return;
298 }
299
300 # Create a hash of the interesting fields
301 my $Row = {
302 Filename => $1,
303 Function => $2,
304 Total => $3,
305 Unreachable => $4,
306 Aborted => $5,
307 Empty => $6
308 };
309
310 # Add them to the stats array
311 push @$Stats, $Row;
312}
313
314##----------------------------------------------------------------------------##
Ted Kremenek5744dc22008-04-02 18:03:36 +0000315# ScanFile - Scan a report file for various identifying attributes.
316##----------------------------------------------------------------------------##
317
Ted Kremenek57cf4462008-04-18 15:09:30 +0000318# Sometimes a source file is scanned more than once, and thus produces
319# multiple error reports. We use a cache to solve this problem.
320
321my %AlreadyScanned;
322
Ted Kremenek5744dc22008-04-02 18:03:36 +0000323sub ScanFile {
324
325 my $Index = shift;
326 my $Dir = shift;
327 my $FName = shift;
Tom Care4f2b10b2010-09-30 01:12:05 +0000328 my $Stats = shift;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000329
Ted Kremenek57cf4462008-04-18 15:09:30 +0000330 # Compute a digest for the report file. Determine if we have already
331 # scanned a file that looks just like it.
332
333 my $digest = ComputeDigest("$Dir/$FName");
334
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000335 if (defined $AlreadyScanned{$digest}) {
Ted Kremenek57cf4462008-04-18 15:09:30 +0000336 # Redundant file. Remove it.
Ted Kremenek20161e92008-07-15 20:18:21 +0000337 system ("rm", "-f", "$Dir/$FName");
Ted Kremenek57cf4462008-04-18 15:09:30 +0000338 return;
339 }
340
341 $AlreadyScanned{$digest} = 1;
342
Ted Kremenek809709f2008-04-18 16:58:34 +0000343 # At this point the report file is not world readable. Make it happen.
Ted Kremenek20161e92008-07-15 20:18:21 +0000344 system ("chmod", "644", "$Dir/$FName");
Ted Kremenek684bb092008-04-18 15:18:20 +0000345
346 # Scan the report file for tags.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000347 open(IN, "$Dir/$FName") or DieDiag("Cannot open '$Dir/$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000348
Tom Care4f2b10b2010-09-30 01:12:05 +0000349 my $BugType = "";
350 my $BugFile = "";
351 my $BugCategory = "";
352 my $BugDescription = "";
353 my $BugPathLength = 1;
354 my $BugLine = 0;
Ted Kremenekebb74132008-09-21 06:58:09 +0000355
Ted Kremenek5744dc22008-04-02 18:03:36 +0000356 while (<IN>) {
Ted Kremenekd658e672009-08-03 23:45:27 +0000357 last if (/<!-- BUGMETAEND -->/);
Ted Kremenekebb74132008-09-21 06:58:09 +0000358
Ted Kremeneka26ddab2009-01-27 01:53:39 +0000359 if (/<!-- BUGTYPE (.*) -->$/) {
360 $BugType = $1;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000361 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000362 elsif (/<!-- BUGFILE (.*) -->$/) {
Ted Kremenek990c2f42008-12-03 19:19:23 +0000363 $BugFile = abs_path($1);
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000364 UpdatePrefix($BugFile);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000365 }
366 elsif (/<!-- BUGPATHLENGTH (.*) -->$/) {
367 $BugPathLength = $1;
368 }
369 elsif (/<!-- BUGLINE (.*) -->$/) {
370 $BugLine = $1;
Ted Kremenekebb74132008-09-21 06:58:09 +0000371 }
372 elsif (/<!-- BUGCATEGORY (.*) -->$/) {
373 $BugCategory = $1;
Ted Kremenek22d6a632008-04-02 20:43:36 +0000374 }
Tom Care4f2b10b2010-09-30 01:12:05 +0000375 elsif (/<!-- BUGDESC (.*) -->$/) {
376 $BugDescription = $1;
377 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000378 }
379
380 close(IN);
Ted Kremenekebb74132008-09-21 06:58:09 +0000381
382 if (!defined $BugCategory) {
383 $BugCategory = "Other";
384 }
Tom Care4f2b10b2010-09-30 01:12:05 +0000385
386 # Don't add internal statistics to the bug reports
387 if ($BugCategory =~ /statistics/i) {
388 AddStatLine($BugDescription, $Stats);
389 return;
390 }
391
Ted Kremeneka26ddab2009-01-27 01:53:39 +0000392 push @$Index,[ $FName, $BugCategory, $BugType, $BugFile, $BugLine,
Ted Kremenek81983112008-09-28 04:13:09 +0000393 $BugPathLength ];
Ted Kremenek22d6a632008-04-02 20:43:36 +0000394}
395
396##----------------------------------------------------------------------------##
Ted Kremenek3ce12072008-09-22 17:50:47 +0000397# CopyFiles - Copy resource files to target directory.
Ted Kremenek22d6a632008-04-02 20:43:36 +0000398##----------------------------------------------------------------------------##
399
Ted Kremenek3ce12072008-09-22 17:50:47 +0000400sub CopyFiles {
Ted Kremenek22d6a632008-04-02 20:43:36 +0000401
402 my $Dir = shift;
Ted Kremeneke15fa272008-10-13 21:46:42 +0000403
404 my $JS = Cwd::realpath("$RealBin/sorttable.js");
Ted Kremenek22d6a632008-04-02 20:43:36 +0000405
Ted Kremenek23cfca32008-06-16 22:40:14 +0000406 DieDiag("Cannot find 'sorttable.js'.\n")
Ted Kremeneke15fa272008-10-13 21:46:42 +0000407 if (! -r $JS);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000408
Ted Kremeneke15fa272008-10-13 21:46:42 +0000409 system ("cp", $JS, "$Dir");
Ted Kremenek22d6a632008-04-02 20:43:36 +0000410
Ted Kremenek23cfca32008-06-16 22:40:14 +0000411 DieDiag("Could not copy 'sorttable.js' to '$Dir'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000412 if (! -r "$Dir/sorttable.js");
Ted Kremenek3ce12072008-09-22 17:50:47 +0000413
Ted Kremeneke15fa272008-10-13 21:46:42 +0000414 my $CSS = Cwd::realpath("$RealBin/scanview.css");
415
Ted Kremenek3ce12072008-09-22 17:50:47 +0000416 DieDiag("Cannot find 'scanview.css'.\n")
Ted Kremeneke15fa272008-10-13 21:46:42 +0000417 if (! -r $CSS);
Ted Kremenek3ce12072008-09-22 17:50:47 +0000418
Ted Kremeneke15fa272008-10-13 21:46:42 +0000419 system ("cp", $CSS, "$Dir");
Ted Kremenek3ce12072008-09-22 17:50:47 +0000420
421 DieDiag("Could not copy 'scanview.css' to '$Dir'.\n")
Ted Kremeneke15fa272008-10-13 21:46:42 +0000422 if (! -r $CSS);
Ted Kremenek5744dc22008-04-02 18:03:36 +0000423}
424
425##----------------------------------------------------------------------------##
Tom Care4f2b10b2010-09-30 01:12:05 +0000426# CalcStats - Calculates visitation statistics and returns the string.
427##----------------------------------------------------------------------------##
428
429sub CalcStats {
430 my $Stats = shift;
431
432 my $TotalBlocks = 0;
433 my $UnreachedBlocks = 0;
434 my $TotalFunctions = scalar(@$Stats);
435 my $BlockAborted = 0;
436 my $WorkListAborted = 0;
437 my $Aborted = 0;
438
439 # Calculate the unique files
440 my $FilesHash = {};
441
442 foreach my $Row (@$Stats) {
443 $FilesHash->{$Row->{Filename}} = 1;
444 $TotalBlocks += $Row->{Total};
445 $UnreachedBlocks += $Row->{Unreachable};
446 $BlockAborted++ if $Row->{Aborted} eq 'yes';
447 $WorkListAborted++ if $Row->{Empty} eq 'no';
448 $Aborted++ if $Row->{Aborted} eq 'yes' || $Row->{Empty} eq 'no';
449 }
450
451 my $TotalFiles = scalar(keys(%$FilesHash));
452
453 # Calculations
454 my $PercentAborted = sprintf("%.2f", $Aborted / $TotalFunctions * 100);
455 my $PercentBlockAborted = sprintf("%.2f", $BlockAborted / $TotalFunctions
456 * 100);
457 my $PercentWorkListAborted = sprintf("%.2f", $WorkListAborted /
458 $TotalFunctions * 100);
459 my $PercentBlocksUnreached = sprintf("%.2f", $UnreachedBlocks / $TotalBlocks
460 * 100);
461
462 my $StatsString = "Analyzed $TotalBlocks blocks in $TotalFunctions functions"
463 . " in $TotalFiles files\n"
464 . "$Aborted functions aborted early ($PercentAborted%)\n"
465 . "$BlockAborted had aborted blocks ($PercentBlockAborted%)\n"
466 . "$WorkListAborted had unfinished worklists ($PercentWorkListAborted%)\n"
467 . "$UnreachedBlocks blocks were never reached ($PercentBlocksUnreached%)\n";
468
469 return $StatsString;
470}
471
472##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000473# Postprocess - Postprocess the results of an analysis scan.
474##----------------------------------------------------------------------------##
475
Sam Bishopa0e22662008-04-02 03:35:43 +0000476sub Postprocess {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000477
Tom Care4f2b10b2010-09-30 01:12:05 +0000478 my $Dir = shift;
479 my $BaseDir = shift;
480 my $AnalyzerStats = shift;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000481
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000482 die "No directory specified." if (!defined $Dir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000483
484 if (! -d $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000485 Diag("No bugs found.\n");
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000486 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000487 }
488
489 opendir(DIR, $Dir);
Ted Kremenek938eef12009-02-17 23:31:05 +0000490 my @files = grep { /^report-.*\.html$/ } readdir(DIR);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000491 closedir(DIR);
492
Ted Kremenek938eef12009-02-17 23:31:05 +0000493 if (scalar(@files) == 0 and ! -e "$Dir/failures") {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000494 Diag("Removing directory '$Dir' because it contains no reports.\n");
Ted Kremenek20161e92008-07-15 20:18:21 +0000495 system ("rm", "-fR", $Dir);
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000496 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000497 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000498
Ted Kremenek991c54b2008-08-08 20:46:42 +0000499 # Scan each report file and build an index.
Tom Care4f2b10b2010-09-30 01:12:05 +0000500 my @Index;
501 my @Stats;
502 foreach my $file (@files) { ScanFile(\@Index, $Dir, $file, \@Stats); }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000503
Ted Kremenek938eef12009-02-17 23:31:05 +0000504 # Scan the failures directory and use the information in the .info files
Ted Kremenekd52e4252008-08-25 20:45:07 +0000505 # to update the common prefix directory.
Ted Kremenek938eef12009-02-17 23:31:05 +0000506 my @failures;
507 my @attributes_ignored;
508 if (-d "$Dir/failures") {
509 opendir(DIR, "$Dir/failures");
510 @failures = grep { /[.]info.txt$/ && !/attribute_ignored/; } readdir(DIR);
Ted Kremenekd52e4252008-08-25 20:45:07 +0000511 closedir(DIR);
Ted Kremenek938eef12009-02-17 23:31:05 +0000512 opendir(DIR, "$Dir/failures");
513 @attributes_ignored = grep { /^attribute_ignored/; } readdir(DIR);
514 closedir(DIR);
515 foreach my $file (@failures) {
516 open IN, "$Dir/failures/$file" or DieDiag("cannot open $file\n");
Ted Kremenekd52e4252008-08-25 20:45:07 +0000517 my $Path = <IN>;
518 if (defined $Path) { UpdatePrefix($Path); }
519 close IN;
520 }
521 }
522
Ted Kremenek63c20172008-08-04 17:34:06 +0000523 # Generate an index.html file.
524 my $FName = "$Dir/index.html";
525 open(OUT, ">", $FName) or DieDiag("Cannot create file '$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000526
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000527 # Print out the header.
528
Ted Kremenek5744dc22008-04-02 18:03:36 +0000529print OUT <<ENDTEXT;
530<html>
531<head>
Ted Kremenek7cba1122008-09-22 01:35:58 +0000532<title>${HtmlTitle}</title>
Ted Kremenekf1435452008-09-23 22:34:51 +0000533<link type="text/css" rel="stylesheet" href="scanview.css"/>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000534<script src="sorttable.js"></script>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000535<script language='javascript' type="text/javascript">
536function SetDisplay(RowClass, DisplayVal)
537{
538 var Rows = document.getElementsByTagName("tr");
539 for ( var i = 0 ; i < Rows.length; ++i ) {
540 if (Rows[i].className == RowClass) {
541 Rows[i].style.display = DisplayVal;
542 }
543 }
544}
Ted Kremenekebb74132008-09-21 06:58:09 +0000545
Ted Kremenek2350a462008-10-28 19:56:52 +0000546function CopyCheckedStateToCheckButtons(SummaryCheckButton) {
547 var Inputs = document.getElementsByTagName("input");
548 for ( var i = 0 ; i < Inputs.length; ++i ) {
549 if (Inputs[i].type == "checkbox") {
550 if(Inputs[i] != SummaryCheckButton) {
551 Inputs[i].checked = SummaryCheckButton.checked;
552 Inputs[i].onclick();
553 }
554 }
555 }
556}
557
Ted Kremenek999e1202008-10-28 20:09:57 +0000558function returnObjById( id ) {
559 if (document.getElementById)
560 var returnVar = document.getElementById(id);
561 else if (document.all)
562 var returnVar = document.all[id];
563 else if (document.layers)
564 var returnVar = document.layers[id];
565 return returnVar;
566}
567
568var NumUnchecked = 0;
569
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000570function ToggleDisplay(CheckButton, ClassName) {
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000571 if (CheckButton.checked) {
572 SetDisplay(ClassName, "");
Ted Kremenek999e1202008-10-28 20:09:57 +0000573 if (--NumUnchecked == 0) {
574 returnObjById("AllBugsCheck").checked = true;
575 }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000576 }
577 else {
578 SetDisplay(ClassName, "none");
Ted Kremenek999e1202008-10-28 20:09:57 +0000579 NumUnchecked++;
580 returnObjById("AllBugsCheck").checked = false;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000581 }
582}
583</script>
Ted Kremenek1d1abb12008-09-22 17:52:58 +0000584<!-- SUMMARYENDHEAD -->
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000585</head>
586<body>
Ted Kremenek7cba1122008-09-22 01:35:58 +0000587<h1>${HtmlTitle}</h1>
588
589<table>
590<tr><th>User:</th><td>${UserName}\@${HostName}</td></tr>
591<tr><th>Working Directory:</th><td>${CurrentDir}</td></tr>
592<tr><th>Command Line:</th><td>${CmdArgs}</td></tr>
593<tr><th>Date:</th><td>${Date}</td></tr>
594ENDTEXT
595
596print OUT "<tr><th>Version:</th><td>${BuildName} (${BuildDate})</td></tr>\n"
597 if (defined($BuildName) && defined($BuildDate));
598
599print OUT <<ENDTEXT;
600</table>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000601ENDTEXT
602
Ted Kremenek991c54b2008-08-08 20:46:42 +0000603 if (scalar(@files)) {
604 # Print out the summary table.
605 my %Totals;
Ted Kremenekebb74132008-09-21 06:58:09 +0000606
Ted Kremenek991c54b2008-08-08 20:46:42 +0000607 for my $row ( @Index ) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000608 my $bug_type = ($row->[2]);
609 my $bug_category = ($row->[1]);
610 my $key = "$bug_category:$bug_type";
611
612 if (!defined $Totals{$key}) { $Totals{$key} = [1,$bug_category,$bug_type]; }
613 else { $Totals{$key}->[0]++; }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000614 }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000615
Ted Kremenek7cba1122008-09-22 01:35:58 +0000616 print OUT "<h2>Bug Summary</h2>";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000617
618 if (defined $BuildName) {
619 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 +0000620 }
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000621
Ted Kremenek2350a462008-10-28 19:56:52 +0000622 my $TotalBugs = scalar(@Index);
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000623print OUT <<ENDTEXT;
Ted Kremenekebb74132008-09-21 06:58:09 +0000624<table>
625<thead><tr><td>Bug Type</td><td>Quantity</td><td class="sorttable_nosort">Display?</td></tr></thead>
Ted Kremenek999e1202008-10-28 20:09:57 +0000626<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 +0000627ENDTEXT
628
Ted Kremenekebb74132008-09-21 06:58:09 +0000629 my $last_category;
630
631 for my $key (
632 sort {
633 my $x = $Totals{$a};
634 my $y = $Totals{$b};
635 my $res = $x->[1] cmp $y->[1];
636 $res = $x->[2] cmp $y->[2] if ($res == 0);
637 $res
638 } keys %Totals )
639 {
640 my $val = $Totals{$key};
641 my $category = $val->[1];
642 if (!defined $last_category or $last_category ne $category) {
643 $last_category = $category;
644 print OUT "<tr><th>$category</th><th colspan=2></th></tr>\n";
645 }
646 my $x = lc $key;
647 $x =~ s/[ ,'":\/()]+/_/g;
648 print OUT "<tr><td class=\"SUMM_DESC\">";
649 print OUT $val->[2];
Ted Kremenek2350a462008-10-28 19:56:52 +0000650 print OUT "</td><td class=\"Q\">";
Ted Kremenekebb74132008-09-21 06:58:09 +0000651 print OUT $val->[0];
652 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 +0000653 }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000654
655 # Print out the table of errors.
656
657print OUT <<ENDTEXT;
658</table>
Ted Kremenek7cba1122008-09-22 01:35:58 +0000659<h2>Reports</h2>
Ted Kremenekebb74132008-09-21 06:58:09 +0000660
661<table class="sortable" style="table-layout:automatic">
662<thead><tr>
663 <td>Bug Group</td>
664 <td class="sorttable_sorted">Bug Type<span id="sorttable_sortfwdind">&nbsp;&#x25BE;</span></td>
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000665 <td>File</td>
Ted Kremenekebb74132008-09-21 06:58:09 +0000666 <td class="Q">Line</td>
Ted Kremenek81983112008-09-28 04:13:09 +0000667 <td class="Q">Path Length</td>
Ted Kremenek2645c772008-07-07 16:58:44 +0000668 <td class="sorttable_nosort"></td>
Ted Kremenekebb74132008-09-21 06:58:09 +0000669 <!-- REPORTBUGCOL -->
670</tr></thead>
671<tbody>
Ted Kremenek5744dc22008-04-02 18:03:36 +0000672ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000673
Ted Kremenek991c54b2008-08-08 20:46:42 +0000674 my $prefix = GetPrefix();
675 my $regex;
676 my $InFileRegex;
677 my $InFilePrefix = "File:</td><td>";
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000678
Ted Kremenek991c54b2008-08-08 20:46:42 +0000679 if (defined $prefix) {
680 $regex = qr/^\Q$prefix\E/is;
681 $InFileRegex = qr/\Q$InFilePrefix$prefix\E/is;
682 }
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000683
Ted Kremenekebb74132008-09-21 06:58:09 +0000684 for my $row ( sort { $a->[2] cmp $b->[2] } @Index ) {
685 my $x = "$row->[1]:$row->[2]";
686 $x = lc $x;
687 $x =~ s/[ ,'":\/()]+/_/g;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000688
Ted Kremenek991c54b2008-08-08 20:46:42 +0000689 my $ReportFile = $row->[0];
Ted Kremenekebb74132008-09-21 06:58:09 +0000690
691 print OUT "<tr class=\"bt_$x\">";
692 print OUT "<td class=\"DESC\">";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000693 print OUT $row->[1];
Ted Kremenekebb74132008-09-21 06:58:09 +0000694 print OUT "</td>";
695 print OUT "<td class=\"DESC\">";
696 print OUT $row->[2];
697 print OUT "</td>";
698
699 # Update the file prefix.
700 my $fname = $row->[3];
Ted Kremenekebb74132008-09-21 06:58:09 +0000701
Ted Kremenek991c54b2008-08-08 20:46:42 +0000702 if (defined $regex) {
703 $fname =~ s/$regex//;
704 UpdateInFilePath("$Dir/$ReportFile", $InFileRegex, $InFilePrefix)
705 }
Ted Kremenekebb74132008-09-21 06:58:09 +0000706
Ted Kremenek91639ef2008-09-22 17:42:31 +0000707 print OUT "<td>";
Ted Kremenekebb74132008-09-21 06:58:09 +0000708 my @fname = split /\//,$fname;
709 if ($#fname > 0) {
710 while ($#fname >= 0) {
711 my $x = shift @fname;
712 print OUT $x;
713 if ($#fname >= 0) {
714 print OUT "<span class=\"W\"> </span>/";
715 }
716 }
717 }
718 else {
719 print OUT $fname;
Ted Kremenek91639ef2008-09-22 17:42:31 +0000720 }
Ted Kremenekebb74132008-09-21 06:58:09 +0000721 print OUT "</td>";
722
723 # Print out the quantities.
Ted Kremenek81983112008-09-28 04:13:09 +0000724 for my $j ( 4 .. 5 ) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000725 print OUT "<td class=\"Q\">$row->[$j]</td>";
726 }
727
Ted Kremenek991c54b2008-08-08 20:46:42 +0000728 # Print the rest of the columns.
Ted Kremenek81983112008-09-28 04:13:09 +0000729 for (my $j = 6; $j <= $#{$row}; ++$j) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000730 print OUT "<td>$row->[$j]</td>"
Ted Kremenek991c54b2008-08-08 20:46:42 +0000731 }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000732
Ted Kremenek991c54b2008-08-08 20:46:42 +0000733 # Emit the "View" link.
Ted Kremenek68005dd2008-09-22 17:39:18 +0000734 print OUT "<td><a href=\"$ReportFile#EndPath\">View Report</a></td>";
Ted Kremenek3cea9ee2008-07-30 17:58:08 +0000735
Daniel Dunbare43038e2008-09-19 23:18:44 +0000736 # Emit REPORTBUG markers.
Ted Kremenekebb74132008-09-21 06:58:09 +0000737 print OUT "\n<!-- REPORTBUG id=\"$ReportFile\" -->\n";
Daniel Dunbare43038e2008-09-19 23:18:44 +0000738
Ted Kremenek991c54b2008-08-08 20:46:42 +0000739 # End the row.
740 print OUT "</tr>\n";
741 }
742
Ted Kremenekebb74132008-09-21 06:58:09 +0000743 print OUT "</tbody>\n</table>\n\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000744 }
745
Ted Kremenek938eef12009-02-17 23:31:05 +0000746 if (scalar (@failures) || scalar(@attributes_ignored)) {
747 print OUT "<h2>Analyzer Failures</h2>\n";
748
749 if (scalar @attributes_ignored) {
750 print OUT "The analyzer's parser ignored the following attributes:<p>\n";
751 print OUT "<table>\n";
752 print OUT "<thead><tr><td>Attribute</td><td>Source File</td><td>Preprocessed File</td><td>STDERR Output</td></tr></thead>\n";
753 foreach my $file (sort @attributes_ignored) {
754 die "cannot demangle attribute name\n" if (! ($file =~ /^attribute_ignored_(.+).txt/));
755 my $attribute = $1;
756 # Open the attribute file to get the first file that failed.
757 next if (!open (ATTR, "$Dir/failures/$file"));
758 my $ppfile = <ATTR>;
759 chomp $ppfile;
760 close ATTR;
761 next if (! -e "$Dir/failures/$ppfile");
762 # Open the info file and get the name of the source file.
763 open (INFO, "$Dir/failures/$ppfile.info.txt") or
764 die "Cannot open $Dir/failures/$ppfile.info.txt\n";
765 my $srcfile = <INFO>;
766 chomp $srcfile;
767 close (INFO);
768 # Print the information in the table.
769 my $prefix = GetPrefix();
770 if (defined $prefix) { $srcfile =~ s/^\Q$prefix//; }
771 print OUT "<tr><td>$attribute</td><td>$srcfile</td><td><a href=\"failures/$ppfile\">$ppfile</a></td><td><a href=\"failures/$ppfile.stderr.txt\">$ppfile.stderr.txt</a></td></tr>\n";
772 my $ppfile_clang = $ppfile;
773 $ppfile_clang =~ s/[.](.+)$/.clang.$1/;
774 print OUT " <!-- REPORTPROBLEM src=\"$srcfile\" file=\"failures/$ppfile\" clangfile=\"failures/$ppfile_clang\" stderr=\"failures/$ppfile.stderr.txt\" info=\"failures/$ppfile.info.txt\" -->\n";
775 }
776 print OUT "</table>\n";
777 }
778
779 if (scalar @failures) {
780 print OUT "<p>The analyzer had problems processing the following files:</p>\n";
781 print OUT "<table>\n";
782 print OUT "<thead><tr><td>Problem</td><td>Source File</td><td>Preprocessed File</td><td>STDERR Output</td></tr></thead>\n";
783 foreach my $file (sort @failures) {
Ted Kremenek82a12532008-09-25 00:25:16 +0000784 $file =~ /(.+).info.txt$/;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000785 # Get the preprocessed file.
786 my $ppfile = $1;
787 # Open the info file and get the name of the source file.
Ted Kremenek938eef12009-02-17 23:31:05 +0000788 open (INFO, "$Dir/failures/$file") or
789 die "Cannot open $Dir/failures/$file\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000790 my $srcfile = <INFO>;
Ted Kremenek5d31f832008-08-18 18:38:29 +0000791 chomp $srcfile;
792 my $problem = <INFO>;
793 chomp $problem;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000794 close (INFO);
795 # Print the information in the table.
Ted Kremenekd52e4252008-08-25 20:45:07 +0000796 my $prefix = GetPrefix();
Ted Kremenek9f9b1fd2008-09-12 22:49:36 +0000797 if (defined $prefix) { $srcfile =~ s/^\Q$prefix//; }
Ted Kremenek938eef12009-02-17 23:31:05 +0000798 print OUT "<tr><td>$problem</td><td>$srcfile</td><td><a href=\"failures/$ppfile\">$ppfile</a></td><td><a href=\"failures/$ppfile.stderr.txt\">$ppfile.stderr.txt</a></td></tr>\n";
Daniel Dunbarce723ce2008-09-25 01:10:50 +0000799 my $ppfile_clang = $ppfile;
800 $ppfile_clang =~ s/[.](.+)$/.clang.$1/;
Ted Kremenek938eef12009-02-17 23:31:05 +0000801 print OUT " <!-- REPORTPROBLEM src=\"$srcfile\" file=\"failures/$ppfile\" clangfile=\"failures/$ppfile_clang\" stderr=\"failures/$ppfile.stderr.txt\" info=\"failures/$ppfile.info.txt\" -->\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000802 }
Ted Kremenek938eef12009-02-17 23:31:05 +0000803 print OUT "</table>\n";
804 }
Nico Webere2c86632011-08-28 11:50:56 +0000805 print OUT "<p>Please consider submitting preprocessed files as <a href=\"http://clang-analyzer.llvm.org/filing_bugs.html\">bug reports</a>. <!-- REPORTCRASHES --> </p>\n";
Ted Kremenek5744dc22008-04-02 18:03:36 +0000806 }
807
Ted Kremenek991c54b2008-08-08 20:46:42 +0000808 print OUT "</body></html>\n";
Ted Kremenek5744dc22008-04-02 18:03:36 +0000809 close(OUT);
Ted Kremenek3ce12072008-09-22 17:50:47 +0000810 CopyFiles($Dir);
Ted Kremenek20161e92008-07-15 20:18:21 +0000811
812 # Make sure $Dir and $BaseDir are world readable/executable.
813 system("chmod", "755", $Dir);
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000814 if (defined $BaseDir) { system("chmod", "755", $BaseDir); }
Ted Kremenek20161e92008-07-15 20:18:21 +0000815
Tom Care4f2b10b2010-09-30 01:12:05 +0000816 # Print statistics
817 print CalcStats(\@Stats) if $AnalyzerStats;
818
Ted Kremenek23cfca32008-06-16 22:40:14 +0000819 my $Num = scalar(@Index);
Ted Kremenek150c2122008-07-11 19:15:05 +0000820 Diag("$Num bugs found.\n");
821 if ($Num > 0 && -r "$Dir/index.html") {
Ted Kremenek5950b3f2008-09-22 06:47:01 +0000822 Diag("Run 'scan-view $Dir' to examine bug reports.\n");
Ted Kremenek150c2122008-07-11 19:15:05 +0000823 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000824
Ted Kremenek938eef12009-02-17 23:31:05 +0000825 DiagCrashes($Dir) if (scalar @failures || scalar @attributes_ignored);
Ted Kremenek991c54b2008-08-08 20:46:42 +0000826
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000827 return $Num;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000828}
829
830##----------------------------------------------------------------------------##
Ted Kremenekdab11102008-04-02 04:43:42 +0000831# RunBuildCommand - Run the build command.
832##----------------------------------------------------------------------------##
833
Ted Kremenek6b628982008-04-30 23:47:12 +0000834sub AddIfNotPresent {
835 my $Args = shift;
836 my $Arg = shift;
837 my $found = 0;
838
839 foreach my $k (@$Args) {
840 if ($k eq $Arg) {
841 $found = 1;
842 last;
843 }
844 }
845
846 if ($found == 0) {
847 push @$Args, $Arg;
848 }
849}
850
Ted Kremenekdab11102008-04-02 04:43:42 +0000851sub RunBuildCommand {
852
853 my $Args = shift;
Ted Kremenek7442ca62008-04-02 16:04:51 +0000854 my $IgnoreErrors = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000855 my $Cmd = $Args->[0];
Ted Kremenek6195c372008-06-02 21:52:47 +0000856 my $CCAnalyzer = shift;
Ted Kremenek524c3082010-03-27 00:20:01 +0000857 my $CXXAnalyzer = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000858
Ted Kremenek3301cb12008-06-30 18:18:16 +0000859 # Get only the part of the command after the last '/'.
860 if ($Cmd =~ /\/([^\/]+)$/) {
861 $Cmd = $1;
862 }
863
Ted Kremenek92548fe2008-11-19 01:46:21 +0000864 if ($Cmd =~ /(.*\/?gcc[^\/]*$)/ or
865 $Cmd =~ /(.*\/?cc[^\/]*$)/ or
866 $Cmd =~ /(.*\/?llvm-gcc[^\/]*$)/ or
Ted Kremenek05acf8b2010-10-16 00:29:16 +0000867 $Cmd =~ /(.*\/?clang$)/ or
Ted Kremenek92548fe2008-11-19 01:46:21 +0000868 $Cmd =~ /(.*\/?ccc-analyzer[^\/]*$)/) {
869
870 if (!($Cmd =~ /ccc-analyzer/) and !defined $ENV{"CCC_CC"}) {
Ted Kremenek51365b52009-12-15 02:35:54 +0000871 $ENV{"CCC_CC"} = $1;
Ted Kremenek92548fe2008-11-19 01:46:21 +0000872 }
873
Ted Kremenekdab11102008-04-02 04:43:42 +0000874 shift @$Args;
Ted Kremenek6195c372008-06-02 21:52:47 +0000875 unshift @$Args, $CCAnalyzer;
Ted Kremenekdab11102008-04-02 04:43:42 +0000876 }
Ted Kremenek51365b52009-12-15 02:35:54 +0000877 elsif ($Cmd =~ /(.*\/?g\+\+[^\/]*$)/ or
878 $Cmd =~ /(.*\/?c\+\+[^\/]*$)/ or
879 $Cmd =~ /(.*\/?llvm-g\+\+[^\/]*$)/ or
Ted Kremenek05acf8b2010-10-16 00:29:16 +0000880 $Cmd =~ /(.*\/?clang\+\+$)/ or
Ted Kremenek51365b52009-12-15 02:35:54 +0000881 $Cmd =~ /(.*\/?c\+\+-analyzer[^\/]*$)/) {
882 if (!($Cmd =~ /c\+\+-analyzer/) and !defined $ENV{"CCC_CXX"}) {
883 $ENV{"CCC_CXX"} = $1;
884 }
885 shift @$Args;
Ted Kremenek524c3082010-03-27 00:20:01 +0000886 unshift @$Args, $CXXAnalyzer;
Ted Kremenek51365b52009-12-15 02:35:54 +0000887 }
Ted Kremenek7442ca62008-04-02 16:04:51 +0000888 elsif ($IgnoreErrors) {
889 if ($Cmd eq "make" or $Cmd eq "gmake") {
Ted Kremenek6fba85d2009-12-11 23:22:52 +0000890 AddIfNotPresent($Args, "CC=$CCAnalyzer");
Ted Kremenek524c3082010-03-27 00:20:01 +0000891 AddIfNotPresent($Args, "CXX=$CXXAnalyzer");
Ted Kremenek6b628982008-04-30 23:47:12 +0000892 AddIfNotPresent($Args,"-k");
Ted Kremenek8912b542008-05-13 21:28:02 +0000893 AddIfNotPresent($Args,"-i");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000894 }
895 elsif ($Cmd eq "xcodebuild") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000896 AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000897 }
Ted Kremenek6b628982008-04-30 23:47:12 +0000898 }
899
Ted Kremenek6b628982008-04-30 23:47:12 +0000900 if ($Cmd eq "xcodebuild") {
Ted Kremenek87752b22009-05-15 21:14:16 +0000901 # Check if using iPhone SDK 3.0 (simulator). If so the compiler being
902 # used should be gcc-4.2.
903 if (!defined $ENV{"CCC_CC"}) {
904 for (my $i = 0 ; $i < scalar(@$Args); ++$i) {
905 if ($Args->[$i] eq "-sdk" && $i + 1 < scalar(@$Args)) {
906 if (@$Args[$i+1] =~ /^iphonesimulator3/) {
907 $ENV{"CCC_CC"} = "gcc-4.2";
Ted Kremenek51365b52009-12-15 02:35:54 +0000908 $ENV{"CCC_CXX"} = "g++-4.2";
Ted Kremenek87752b22009-05-15 21:14:16 +0000909 }
910 }
911 }
912 }
913
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000914 # Disable PCH files until clang supports them.
915 AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO");
Ted Kremenek915e9722008-05-27 23:18:07 +0000916
917 # When 'CC' is set, xcodebuild uses it to do all linking, even if we are
918 # linking C++ object files. Set 'LDPLUSPLUS' so that xcodebuild uses 'g++'
Ted Kremenek524c3082010-03-27 00:20:01 +0000919 # (via c++-analyzer) when linking such files.
920 $ENV{"LDPLUSPLUS"} = $CXXAnalyzer;
Ted Kremenek6b628982008-04-30 23:47:12 +0000921 }
Ted Kremenekdab11102008-04-02 04:43:42 +0000922
Ted Kremenek5a4ddaf2008-08-25 20:10:45 +0000923 return (system(@$Args) >> 8);
Ted Kremenekdab11102008-04-02 04:43:42 +0000924}
925
Ted Kremenekdab11102008-04-02 04:43:42 +0000926##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000927# DisplayHelp - Utility function to display all help options.
928##----------------------------------------------------------------------------##
929
Sam Bishopa0e22662008-04-02 03:35:43 +0000930sub DisplayHelp {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000931
Ted Kremenek5744dc22008-04-02 18:03:36 +0000932print <<ENDTEXT;
Sam Bishopa0e22662008-04-02 03:35:43 +0000933USAGE: $Prog [options] <build command> [build options]
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000934
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000935ENDTEXT
936
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000937 if (defined $BuildName) {
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000938 print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n";
939 }
940
941print <<ENDTEXT;
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000942OPTIONS:
943
Ted Kremeneke15fa272008-10-13 21:46:42 +0000944 -analyze-headers - Also analyze functions in #included files.
Ted Kremenek8382cf52009-11-13 18:46:29 +0000945
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000946 -o - Target directory for HTML report files. Subdirectories
Sam Bishopa0e22662008-04-02 03:35:43 +0000947 will be created as needed to represent separate "runs" of
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000948 the analyzer. If this option is not specified, a directory
Ted Kremenekffda0b42008-10-31 05:48:42 +0000949 is created in /tmp (TMPDIR on Mac OS X) to store the reports.
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000950
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000951 -h - Display this message.
952 --help
Ted Kremenek1262fc42008-05-14 20:10:33 +0000953
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000954 -k - Add a "keep on going" option to the specified build command.
955 --keep-going This option currently supports make and xcodebuild.
Ted Kremenekf02e8db2008-04-02 16:41:25 +0000956 This is a convenience option; one can specify this
957 behavior directly using build options.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000958
Ted Kremenek7cba1122008-09-22 01:35:58 +0000959 --html-title [title] - Specify the title used on generated HTML pages.
960 --html-title=[title] If not specified, a default title will be used.
961
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000962 -plist - By default the output of scan-build is a set of HTML files.
963 This option outputs the results as a set of .plist files.
964
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000965 --status-bugs - By default, the exit status of $Prog is the same as the
966 executed build command. Specifying this option causes the
967 exit status of $Prog to be 1 if it found potential bugs
968 and 0 otherwise.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000969
Ted Kremenek386c6932008-09-03 17:59:35 +0000970 --use-cc [compiler path] - By default, $Prog uses 'gcc' to compile and link
971 --use-cc=[compiler path] your C and Objective-C code. Use this option
972 to specify an alternate compiler.
973
974 --use-c++ [compiler path] - By default, $Prog uses 'g++' to compile and link
975 --use-c++=[compiler path] your C++ and Objective-C++ code. Use this option
976 to specify an alternate compiler.
Ted Kremenekf17ef3c2008-08-21 21:47:09 +0000977
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000978 -v - Verbose output from $Prog and the analyzer.
Ted Kremenek386c6932008-09-03 17:59:35 +0000979 A second and third '-v' increases verbosity.
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000980
981 -V - View analysis results in a web browser when the build
982 --view completes.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000983
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000984ADVANCED OPTIONS:
985
Ted Kremenek9f4ecb32009-02-20 21:49:22 +0000986 -constraints [model] - Specify the contraint engine used by the analyzer.
987 By default the 'range' model is used. Specifying
988 'basic' uses a simpler, less powerful constraint model
Ted Kremenekd4c76842009-02-21 04:46:41 +0000989 used by checker-0.160 and earlier.
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000990
991 -store [model] - Specify the store model used by the analyzer. By default,
Ted Kremenekb8bb3e72009-09-25 05:55:59 +0000992 the 'region' store model is used. 'region' specifies a field-
993 sensitive store model. Users can also specify 'basic', which
994 is far less precise but can more quickly analyze code.
995 'basic' was the default store model for checker-0.221 and
996 earlier.
997
Ted Kremeneke600bed2009-07-30 23:55:19 +0000998 -no-failure-reports - Do not create a 'failures' subdirectory that includes
999 analyzer crash reports and preprocessed source files.
Ted Kremenekb7770c02008-07-15 17:06:13 +00001000
Tom Care4f2b10b2010-09-30 01:12:05 +00001001 -stats - Generates visitation statistics for the project being analyzed.
1002
1003 -maxloop N - specifiy the number of times a block can be visited before giving
Ted Kremenek09fbf292011-04-12 21:47:00 +00001004 up. Default is 4. Increase for more comprehensive coverage at a
Tom Care4f2b10b2010-09-30 01:12:05 +00001005 cost of speed.
Ted Kremenek09fbf292011-04-12 21:47:00 +00001006
1007CONTROLLING CHECKERS:
1008
1009 A default group of checkers are always run unless explicitly disabled.
1010 Checkers may be enabled/disabled using the following options:
1011
1012 -enable-checker [checker name]
1013 -disable-checker [checker name]
Ted Kremenekd52e4252008-08-25 20:45:07 +00001014ENDTEXT
Ted Kremenekb7770c02008-07-15 17:06:13 +00001015
Ted Kremenekba90e8a2011-02-25 22:00:40 +00001016# Query clang for list of checkers that are enabled.
1017my %EnabledCheckers;
1018foreach my $lang ("c", "objective-c", "objective-c++", "c++") {
1019 pipe(FROM_CHILD, TO_PARENT);
1020 my $pid = fork();
1021 if ($pid == 0) {
1022 close FROM_CHILD;
1023 open(STDOUT,">&", \*TO_PARENT);
1024 open(STDERR,">&", \*TO_PARENT);
1025 exec $Clang, ('--analyze', '-x', $lang, '-', '-###');
1026 }
1027 close(TO_PARENT);
1028 while(<FROM_CHILD>) {
1029 foreach my $val (split /\s+/) {
1030 $val =~ s/\"//g;
1031 if ($val =~ /-analyzer-checker\=([^\s]+)/) {
1032 $EnabledCheckers{$1} = 1;
1033 }
1034 }
1035 }
1036 waitpid($pid,0);
1037 close(FROM_CHILD);
1038}
1039
1040# Query clang for complete list of checkers.
1041pipe(FROM_CHILD, TO_PARENT);
1042my $pid = fork();
1043if ($pid == 0) {
1044 close FROM_CHILD;
1045 open(STDOUT,">&", \*TO_PARENT);
1046 open(STDERR,">&", \*TO_PARENT);
1047 exec $Clang, ('-cc1', '-analyzer-checker-help');
1048}
1049close(TO_PARENT);
1050my $foundCheckers = 0;
1051while(<FROM_CHILD>) {
1052 if (/CHECKERS:/) {
1053 $foundCheckers = 1;
1054 last;
1055 }
1056}
1057if (!$foundCheckers) {
1058 print " *** Could not query Clang for the list of available checkers.";
1059}
1060else {
1061 print("\nAVAILABLE CHECKERS:\n\n");
1062 my $skip = 0;
1063 while(<FROM_CHILD>) {
Ted Kremenek4cd6ea92011-04-05 00:21:49 +00001064 if (/experimental/) {
Ted Kremenekba90e8a2011-02-25 22:00:40 +00001065 $skip = 1;
1066 next;
1067 }
1068 if ($skip) {
1069 next if (!/^\s\s[^\s]/);
1070 $skip = 0;
1071 }
1072 s/^\s\s//;
1073 if (/^([^\s]+)/) {
1074 # Is the checker enabled?
1075 my $checker = $1;
1076 my $enabled = 0;
1077 my $aggregate = "";
1078 foreach my $domain (split /\./, $checker) {
1079 $aggregate .= $domain;
1080 if ($EnabledCheckers{$aggregate}) {
1081 $enabled =1;
1082 last;
1083 }
1084 }
1085
1086 if ($enabled) {
1087 print " + ";
1088 }
1089 else {
1090 print " ";
1091 }
1092 }
1093 else {
1094 print " ";
1095 }
1096 print $_;
1097 }
1098}
1099waitpid($pid,0);
1100close(FROM_CHILD);
Ted Kremenek7fe679f2011-02-17 02:28:30 +00001101
Ted Kremenekb7770c02008-07-15 17:06:13 +00001102print <<ENDTEXT
1103
Ted Kremenekba90e8a2011-02-25 22:00:40 +00001104 NOTE: "+" indicates that an analysis is enabled by default.
Ted Kremenekb7770c02008-07-15 17:06:13 +00001105
Ted Kremenek2b74ab62008-04-01 21:22:03 +00001106BUILD OPTIONS
1107
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001108 You can specify any build option acceptable to the build command.
Ted Kremenek39eefde2008-04-02 16:47:27 +00001109
Ted Kremenek5744dc22008-04-02 18:03:36 +00001110EXAMPLE
Ted Kremenek2b74ab62008-04-01 21:22:03 +00001111
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001112 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenek2b74ab62008-04-01 21:22:03 +00001113
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001114 The above example causes analysis reports to be deposited into
1115 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
1116 A different subdirectory is created each time $Prog analyzes a project.
1117 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenek2b74ab62008-04-01 21:22:03 +00001118
1119ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001120}
1121
1122##----------------------------------------------------------------------------##
Ted Kremenek7cba1122008-09-22 01:35:58 +00001123# HtmlEscape - HTML entity encode characters that are special in HTML
1124##----------------------------------------------------------------------------##
1125
1126sub HtmlEscape {
1127 # copy argument to new variable so we don't clobber the original
1128 my $arg = shift || '';
1129 my $tmp = $arg;
Ted Kremenek87f8de72008-11-03 07:44:16 +00001130 $tmp =~ s/&/&amp;/g;
1131 $tmp =~ s/</&lt;/g;
1132 $tmp =~ s/>/&gt;/g;
Ted Kremenek7cba1122008-09-22 01:35:58 +00001133 return $tmp;
1134}
1135
1136##----------------------------------------------------------------------------##
1137# ShellEscape - backslash escape characters that are special to the shell
1138##----------------------------------------------------------------------------##
1139
1140sub ShellEscape {
1141 # copy argument to new variable so we don't clobber the original
1142 my $arg = shift || '';
Ted Kremenek87f8de72008-11-03 07:44:16 +00001143 if ($arg =~ /["\s]/) { return "'" . $arg . "'"; }
1144 return $arg;
Ted Kremenek7cba1122008-09-22 01:35:58 +00001145}
1146
1147##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001148# Process command-line arguments.
1149##----------------------------------------------------------------------------##
1150
Ted Kremeneke15fa272008-10-13 21:46:42 +00001151my $AnalyzeHeaders = 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001152my $HtmlDir; # Parent directory to store HTML files.
1153my $IgnoreErrors = 0; # Ignore build errors.
Ted Kremenek7f8a3252008-04-02 18:42:49 +00001154my $ViewResults = 0; # View results when the build terminates.
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001155my $ExitStatusFoundBugs = 0; # Exit status reflects whether bugs were found
Ted Kremenekb7770c02008-07-15 17:06:13 +00001156my @AnalysesToRun;
Zhongxing Xu07c37672008-10-27 14:26:32 +00001157my $StoreModel;
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +00001158my $ConstraintsModel;
Ted Kremenek8d8bc912009-08-04 17:05:18 +00001159my $OutputFormat = "html";
Tom Care4f2b10b2010-09-30 01:12:05 +00001160my $AnalyzerStats = 0;
1161my $MaxLoop = 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001162
1163if (!@ARGV) {
1164 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +00001165 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001166}
1167
Ted Kremenekba90e8a2011-02-25 22:00:40 +00001168
1169my $displayHelp = 0;
1170
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001171while (@ARGV) {
1172
1173 # Scan for options we recognize.
1174
1175 my $arg = $ARGV[0];
1176
Sam Bishop2f2418e2008-04-03 14:29:47 +00001177 if ($arg eq "-h" or $arg eq "--help") {
Ted Kremenekba90e8a2011-02-25 22:00:40 +00001178 $displayHelp = 1;
1179 shift @ARGV;
1180 next;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001181 }
1182
Ted Kremeneke15fa272008-10-13 21:46:42 +00001183 if ($arg eq '-analyze-headers') {
1184 shift @ARGV;
1185 $AnalyzeHeaders = 1;
1186 next;
1187 }
1188
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001189 if ($arg eq "-o") {
1190 shift @ARGV;
1191
1192 if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +00001193 DieDiag("'-o' option requires a target directory name.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001194 }
1195
Ted Kremenekdb51a552009-03-11 18:20:33 +00001196 # Construct an absolute path. Uses the current working directory
1197 # as a base if the original path was not absolute.
1198 $HtmlDir = abs_path(shift @ARGV);
1199
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001200 next;
1201 }
Ted Kremenek7cba1122008-09-22 01:35:58 +00001202
1203 if ($arg =~ /^--html-title(=(.+))?$/) {
1204 shift @ARGV;
1205
Ted Kremenek278a5512009-05-12 18:04:43 +00001206 if (!defined $2 || $2 eq '') {
Ted Kremenek7cba1122008-09-22 01:35:58 +00001207 if (!@ARGV) {
1208 DieDiag("'--html-title' option requires a string.\n");
1209 }
1210
1211 $HtmlTitle = shift @ARGV;
1212 } else {
1213 $HtmlTitle = $2;
1214 }
1215
1216 next;
1217 }
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001218
Ted Kremenek2b74ab62008-04-01 21:22:03 +00001219 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001220 shift @ARGV;
1221 $IgnoreErrors = 1;
1222 next;
1223 }
Ted Kremenek7fe679f2011-02-17 02:28:30 +00001224
Ted Kremenekf17ef3c2008-08-21 21:47:09 +00001225 if ($arg =~ /^--use-cc(=(.+))?$/) {
1226 shift @ARGV;
1227 my $cc;
1228
Ted Kremenek278a5512009-05-12 18:04:43 +00001229 if (!defined $2 || $2 eq "") {
Ted Kremenekf17ef3c2008-08-21 21:47:09 +00001230 if (!@ARGV) {
1231 DieDiag("'--use-cc' option requires a compiler executable name.\n");
1232 }
1233 $cc = shift @ARGV;
1234 }
1235 else {
1236 $cc = $2;
1237 }
1238
1239 $ENV{"CCC_CC"} = $cc;
1240 next;
1241 }
1242
Ted Kremenek7cba1122008-09-22 01:35:58 +00001243 if ($arg =~ /^--use-c\+\+(=(.+))?$/) {
Ted Kremenek386c6932008-09-03 17:59:35 +00001244 shift @ARGV;
Ted Kremenek51365b52009-12-15 02:35:54 +00001245 my $cxx;
Ted Kremenek386c6932008-09-03 17:59:35 +00001246
Ted Kremenek278a5512009-05-12 18:04:43 +00001247 if (!defined $2 || $2 eq "") {
Ted Kremenek386c6932008-09-03 17:59:35 +00001248 if (!@ARGV) {
1249 DieDiag("'--use-c++' option requires a compiler executable name.\n");
1250 }
Ted Kremenek51365b52009-12-15 02:35:54 +00001251 $cxx = shift @ARGV;
Ted Kremenek386c6932008-09-03 17:59:35 +00001252 }
1253 else {
Ted Kremenek51365b52009-12-15 02:35:54 +00001254 $cxx = $2;
Ted Kremenek386c6932008-09-03 17:59:35 +00001255 }
Ted Kremenek51365b52009-12-15 02:35:54 +00001256
1257 $ENV{"CCC_CXX"} = $cxx;
Ted Kremenek386c6932008-09-03 17:59:35 +00001258 next;
1259 }
1260
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001261 if ($arg eq "-v") {
1262 shift @ARGV;
1263 $Verbose++;
1264 next;
1265 }
1266
Ted Kremenek7f8a3252008-04-02 18:42:49 +00001267 if ($arg eq "-V" or $arg eq "--view") {
1268 shift @ARGV;
1269 $ViewResults = 1;
1270 next;
1271 }
1272
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001273 if ($arg eq "--status-bugs") {
1274 shift @ARGV;
1275 $ExitStatusFoundBugs = 1;
1276 next;
1277 }
Zhongxing Xu07c37672008-10-27 14:26:32 +00001278
1279 if ($arg eq "-store") {
1280 shift @ARGV;
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +00001281 $StoreModel = shift @ARGV;
1282 next;
1283 }
1284
1285 if ($arg eq "-constraints") {
1286 shift @ARGV;
1287 $ConstraintsModel = shift @ARGV;
Zhongxing Xu07c37672008-10-27 14:26:32 +00001288 next;
1289 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001290
Ted Kremenekdb4f5f22008-11-04 00:02:53 +00001291 if ($arg eq "-plist") {
1292 shift @ARGV;
1293 $OutputFormat = "plist";
1294 next;
1295 }
Ted Kremenek7753b352009-07-27 22:10:34 +00001296 if ($arg eq "-plist-html") {
1297 shift @ARGV;
1298 $OutputFormat = "plist-html";
1299 next;
1300 }
Ted Kremeneke600bed2009-07-30 23:55:19 +00001301
1302 if ($arg eq "-no-failure-reports") {
1303 $ENV{"CCC_REPORT_FAILURES"} = 0;
1304 next;
1305 }
Tom Care4f2b10b2010-09-30 01:12:05 +00001306 if ($arg eq "-stats") {
1307 shift @ARGV;
1308 $AnalyzerStats = 1;
1309 next;
1310 }
1311 if ($arg eq "-maxloop") {
1312 shift @ARGV;
1313 $MaxLoop = shift @ARGV;
1314 next;
1315 }
Ted Kremenek09fbf292011-04-12 21:47:00 +00001316 if ($arg eq "-enable-checker") {
1317 shift @ARGV;
1318 push @AnalysesToRun, "-analyzer-checker", shift @ARGV;
1319 next;
1320 }
1321 if ($arg eq "-disable-checker") {
1322 shift @ARGV;
1323 push @AnalysesToRun, "-analyzer-disable-checker", shift @ARGV;
1324 next;
1325 }
Ted Kremenek7753b352009-07-27 22:10:34 +00001326
Ted Kremenek23cfca32008-06-16 22:40:14 +00001327 DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/);
Ted Kremenek0062ad42008-04-02 16:35:01 +00001328
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001329 last;
1330}
1331
Ted Kremenekba90e8a2011-02-25 22:00:40 +00001332if (!@ARGV and $displayHelp == 0) {
Ted Kremenek23cfca32008-06-16 22:40:14 +00001333 Diag("No build command specified.\n\n");
Ted Kremenekba90e8a2011-02-25 22:00:40 +00001334 $displayHelp = 1;
1335}
1336
1337if ($displayHelp) {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001338 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +00001339 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001340}
1341
Ted Kremenekba90e8a2011-02-25 22:00:40 +00001342# Determine where results go.
Ted Kremenek7cba1122008-09-22 01:35:58 +00001343$CmdArgs = HtmlEscape(join(' ', map(ShellEscape($_), @ARGV)));
1344$HtmlTitle = "${CurrentDirSuffix} - scan-build results"
1345 unless (defined($HtmlTitle));
Ted Kremenek386c6932008-09-03 17:59:35 +00001346
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001347# Determine the output directory for the HTML reports.
Ted Kremenek684bb092008-04-18 15:18:20 +00001348my $BaseDir = $HtmlDir;
Sam Bishopa0e22662008-04-02 03:35:43 +00001349$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001350
Ted Kremenekba90e8a2011-02-25 22:00:40 +00001351# Determine the location of ccc-analyzer.
Ted Kremenek51365b52009-12-15 02:35:54 +00001352my $AbsRealBin = Cwd::realpath($RealBin);
1353my $Cmd = "$AbsRealBin/libexec/ccc-analyzer";
1354my $CmdCXX = "$AbsRealBin/libexec/c++-analyzer";
1355
Ted Kremenekce87b922009-02-25 22:54:02 +00001356if (!defined $Cmd || ! -x $Cmd) {
Ted Kremenek51365b52009-12-15 02:35:54 +00001357 $Cmd = "$AbsRealBin/ccc-analyzer";
Ted Kremenek6b896362009-02-27 06:17:05 +00001358 DieDiag("Executable 'ccc-analyzer' does not exist at '$Cmd'\n") if(! -x $Cmd);
Ted Kremenekce87b922009-02-25 22:54:02 +00001359}
Ted Kremenek51365b52009-12-15 02:35:54 +00001360if (!defined $CmdCXX || ! -x $CmdCXX) {
1361 $CmdCXX = "$AbsRealBin/c++-analyzer";
1362 DieDiag("Executable 'c++-analyzer' does not exist at '$CmdCXX'\n") if(! -x $CmdCXX);
1363}
Ted Kremenekf22eacb2008-04-18 22:00:56 +00001364
Ted Kremenekfd9df0e2009-05-09 19:19:28 +00001365if (!defined $ClangSB || ! -x $ClangSB) {
1366 Diag("'clang' executable not found in '$RealBin/bin'.\n");
Ted Kremenek64922002010-02-12 00:12:25 +00001367 Diag("Using 'clang' from path: $Clang\n");
Ted Kremenekfd9df0e2009-05-09 19:19:28 +00001368}
Ted Kremenek0b6c1532008-04-08 20:22:12 +00001369
Ted Kremenekba90e8a2011-02-25 22:00:40 +00001370# Set the appropriate environment variables.
1371SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenek4f4b17d2008-04-03 20:08:18 +00001372$ENV{'CC'} = $Cmd;
Ted Kremenek51365b52009-12-15 02:35:54 +00001373$ENV{'CXX'} = $CmdCXX;
Ted Kremenekf22eacb2008-04-18 22:00:56 +00001374$ENV{'CLANG'} = $Clang;
Ted Kremenek51365b52009-12-15 02:35:54 +00001375$ENV{'CLANG_CXX'} = $ClangCXX;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001376if ($Verbose >= 2) {
1377 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
1378}
Ted Kremeneka9525c92008-05-12 22:07:14 +00001379if ($Verbose >= 3) {
1380 $ENV{'CCC_ANALYZER_LOG'} = 1;
1381}
Ted Kremeneke15fa272008-10-13 21:46:42 +00001382if ($AnalyzeHeaders) {
1383 push @AnalysesToRun,"-analyzer-opt-analyze-headers";
1384}
Tom Care4f2b10b2010-09-30 01:12:05 +00001385if ($AnalyzerStats) {
Ted Kremenek251c27b2011-04-27 18:53:08 +00001386 push @AnalysesToRun, '-analyzer-checker', 'debug.Stats';
Tom Care4f2b10b2010-09-30 01:12:05 +00001387}
Tom Care4f2b10b2010-09-30 01:12:05 +00001388if ($MaxLoop > 0) {
1389 push @AnalysesToRun, '-analyzer-max-loop ' . $MaxLoop;
1390}
1391
Ted Kremenek90125992008-07-15 23:41:32 +00001392$ENV{'CCC_ANALYZER_ANALYSIS'} = join ' ',@AnalysesToRun;
1393
Zhongxing Xu3cab2b12008-11-02 10:58:16 +00001394if (defined $StoreModel) {
Zhongxing Xu07c37672008-10-27 14:26:32 +00001395 $ENV{'CCC_ANALYZER_STORE_MODEL'} = $StoreModel;
1396}
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +00001397if (defined $ConstraintsModel) {
1398 $ENV{'CCC_ANALYZER_CONSTRAINTS_MODEL'} = $ConstraintsModel;
1399}
Ted Kremenekdb4f5f22008-11-04 00:02:53 +00001400if (defined $OutputFormat) {
1401 $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'} = $OutputFormat;
1402}
1403
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001404# Run the build.
Ted Kremenek524c3082010-03-27 00:20:01 +00001405my $ExitStatus = RunBuildCommand(\@ARGV, $IgnoreErrors, $Cmd, $CmdCXX);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001406
Ted Kremenek7753b352009-07-27 22:10:34 +00001407if (defined $OutputFormat) {
Daniel Dunbar11825022009-07-29 16:21:23 +00001408 if ($OutputFormat =~ /plist/) {
1409 Diag "Analysis run complete.\n";
1410 Diag "Analysis results (plist files) deposited in '$HtmlDir'\n";
1411 }
1412 elsif ($OutputFormat =~ /html/) {
Ted Kremenek7753b352009-07-27 22:10:34 +00001413 # Postprocess the HTML directory.
Tom Care4f2b10b2010-09-30 01:12:05 +00001414 my $NumBugs = Postprocess($HtmlDir, $BaseDir, $AnalyzerStats);
Ted Kremenek5656a982008-07-15 17:09:28 +00001415
Ted Kremenek7753b352009-07-27 22:10:34 +00001416 if ($ViewResults and -r "$HtmlDir/index.html") {
1417 Diag "Analysis run complete.\n";
1418 Diag "Viewing analysis results in '$HtmlDir' using scan-view.\n";
1419 my $ScanView = Cwd::realpath("$RealBin/scan-view");
1420 if (! -x $ScanView) { $ScanView = "scan-view"; }
1421 exec $ScanView, "$HtmlDir";
1422 }
1423
1424 if ($ExitStatusFoundBugs) {
1425 exit 1 if ($NumBugs > 0);
1426 exit 0;
1427 }
Ted Kremenekdb4f5f22008-11-04 00:02:53 +00001428 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001429}
1430
Ted Kremenek5656a982008-07-15 17:09:28 +00001431exit $ExitStatus;
1432