blob: f978a888be8386180938ed7bfbcc4f74443ec773 [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");
64 Diag (" http://clang.llvm.org/StaticAnalysisUsage.html#filingbugs\n")
65}
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 Kremenek51365b52009-12-15 02:35:54 +000085my $ClangCXXSB;
Ted Kremenek8d10cdd2009-02-20 04:34:29 +000086if (!defined $ClangSB || ! -x $ClangSB) {
Ted Kremenekfd9df0e2009-05-09 19:19:28 +000087 $ClangSB = Cwd::realpath("$RealBin/clang");
Ted Kremenek51365b52009-12-15 02:35:54 +000088 if (defined $ClangSB) { $ClangCXXSB = $ClangSB . "++"; }
Ted Kremenekfd9df0e2009-05-09 19:19:28 +000089}
Ted Kremenek833da022009-03-29 00:31:32 +000090my $Clang = $ClangSB;
Ted Kremenek51365b52009-12-15 02:35:54 +000091my $ClangCXX = $ClangCXXSB;
Ted Kremenek2a3a8b92009-12-11 22:44:53 +000092# Default to looking for 'clang' in the path.
93if (!defined $Clang || ! -x $Clang) {
94 $Clang = "clang";
Ted Kremenek51365b52009-12-15 02:35:54 +000095 $ClangCXX = "clang++";
Ted Kremenek2a3a8b92009-12-11 22:44:53 +000096}
Ted Kremenekfd9df0e2009-05-09 19:19:28 +000097
Ted Kremenekb7770c02008-07-15 17:06:13 +000098my %AvailableAnalyses;
99
100# Query clang for analysis options.
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000101open(PIPE, "-|", $Clang, "-cc1", "--help") or
102 DieDiag("Cannot execute '$Clang'\n");
Ted Kremenek63c20172008-08-04 17:34:06 +0000103
Ted Kremenekb7770c02008-07-15 17:06:13 +0000104my $FoundAnalysis = 0;
105
106while(<PIPE>) {
107 if ($FoundAnalysis == 0) {
Ted Kremenek938eef12009-02-17 23:31:05 +0000108 if (/Checks and Analyses/) {
Ted Kremenekb7770c02008-07-15 17:06:13 +0000109 $FoundAnalysis = 1;
110 }
Ted Kremenekb7770c02008-07-15 17:06:13 +0000111 next;
112 }
Ted Kremenekb7770c02008-07-15 17:06:13 +0000113 if (/^\s\s\s\s([^\s]+)\s(.+)$/) {
114 next if ($1 =~ /-dump/ or $1 =~ /-view/
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000115 or $1 =~ /-warn-uninit/);
Ted Kremenekb7770c02008-07-15 17:06:13 +0000116 $AvailableAnalyses{$1} = $2;
117 next;
Ted Kremenek938eef12009-02-17 23:31:05 +0000118 }
Ted Kremenekb7770c02008-07-15 17:06:13 +0000119 last;
120}
Ted Kremenekb7770c02008-07-15 17:06:13 +0000121close (PIPE);
122
123my %AnalysesDefaultEnabled = (
124 '-warn-dead-stores' => 1,
125 '-checker-cfref' => 1,
Ted Kremenek90125992008-07-15 23:41:32 +0000126 '-warn-objc-methodsigs' => 1,
Ted Kremenekd76c6a32009-02-25 21:08:30 +0000127 # Do not enable the missing -dealloc check by default.
128 # '-warn-objc-missing-dealloc' => 1,
Ted Kremenek5d443492008-09-18 06:34:16 +0000129 '-warn-objc-unused-ivars' => 1,
Ted Kremenek3a92d6d2009-07-24 02:52:07 +0000130 '-warn-security-syntactic' => 1
Ted Kremenekb7770c02008-07-15 17:06:13 +0000131);
132
133##----------------------------------------------------------------------------##
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000134# GetHTMLRunDir - Construct an HTML directory name for the current sub-run.
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000135##----------------------------------------------------------------------------##
136
Sam Bishopa0e22662008-04-02 03:35:43 +0000137sub GetHTMLRunDir {
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000138 die "Not enough arguments." if (@_ == 0);
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000139 my $Dir = shift @_;
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000140 my $TmpMode = 0;
141 if (!defined $Dir) {
Ted Kremenekffda0b42008-10-31 05:48:42 +0000142 if (`uname` =~ /Darwin/) {
143 $Dir = $ENV{'TMPDIR'};
144 if (!defined $Dir) { $Dir = "/tmp"; }
145 }
146 else {
147 $Dir = "/tmp";
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000148 }
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000149 $TmpMode = 1;
150 }
Ted Kremenekbf762c92009-02-24 02:38:02 +0000151
152 # Chop off any trailing '/' characters.
153 while ($Dir =~ /\/$/) { chop $Dir; }
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000154
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000155 # Get current date and time.
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000156 my @CurrentTime = localtime();
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000157 my $year = $CurrentTime[5] + 1900;
158 my $day = $CurrentTime[3];
159 my $month = $CurrentTime[4] + 1;
Ted Kremenek9d7405f2008-05-14 17:23:56 +0000160 my $DateString = sprintf("%d-%02d-%02d", $year, $month, $day);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000161
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000162 # Determine the run number.
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000163 my $RunNumber;
164
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000165 if (-d $Dir) {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000166 if (! -r $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000167 DieDiag("directory '$Dir' exists but is not readable.\n");
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000168 }
169 # Iterate over all files in the specified directory.
170 my $max = 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000171 opendir(DIR, $Dir);
Ted Kremenek29da6c52008-08-07 17:57:34 +0000172 my @FILES = grep { -d "$Dir/$_" } readdir(DIR);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000173 closedir(DIR);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000174
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000175 foreach my $f (@FILES) {
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000176 # Strip the prefix '$Prog-' if we are dumping files to /tmp.
177 if ($TmpMode) {
178 next if (!($f =~ /^$Prog-(.+)/));
179 $f = $1;
180 }
181
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000182 my @x = split/-/, $f;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000183 next if (scalar(@x) != 4);
184 next if ($x[0] != $year);
185 next if ($x[1] != $month);
186 next if ($x[2] != $day);
187
188 if ($x[3] > $max) {
189 $max = $x[3];
190 }
191 }
192
193 $RunNumber = $max + 1;
194 }
195 else {
196
197 if (-x $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000198 DieDiag("'$Dir' exists but is not a directory.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000199 }
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000200
201 if ($TmpMode) {
Ted Kremenek445fa772008-10-10 00:17:08 +0000202 DieDiag("The directory '/tmp' does not exist or cannot be accessed.\n");
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000203 }
204
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000205 # $Dir does not exist. It will be automatically created by the
206 # clang driver. Set the run number to 1.
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000207
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000208 $RunNumber = 1;
209 }
210
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000211 die "RunNumber must be defined!" if (!defined $RunNumber);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000212
213 # Append the run number.
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000214 my $NewDir;
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000215 if ($TmpMode) {
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000216 $NewDir = "$Dir/$Prog-$DateString-$RunNumber";
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000217 }
218 else {
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000219 $NewDir = "$Dir/$DateString-$RunNumber";
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000220 }
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000221 system 'mkdir','-p',$NewDir;
222 return $NewDir;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000223}
224
Sam Bishopa0e22662008-04-02 03:35:43 +0000225sub SetHtmlEnv {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000226
227 die "Wrong number of arguments." if (scalar(@_) != 2);
228
229 my $Args = shift;
230 my $Dir = shift;
231
232 die "No build command." if (scalar(@$Args) == 0);
233
234 my $Cmd = $$Args[0];
235
236 if ($Cmd =~ /configure/) {
237 return;
238 }
239
240 if ($Verbose) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000241 Diag("Emitting reports for this run to '$Dir'.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000242 }
243
244 $ENV{'CCC_ANALYZER_HTML'} = $Dir;
245}
246
247##----------------------------------------------------------------------------##
Ted Kremenek57cf4462008-04-18 15:09:30 +0000248# ComputeDigest - Compute a digest of the specified file.
249##----------------------------------------------------------------------------##
250
251sub ComputeDigest {
252 my $FName = shift;
Ted Kremenek23cfca32008-06-16 22:40:14 +0000253 DieDiag("Cannot read $FName to compute Digest.\n") if (! -r $FName);
Ted Kremeneka6e24812008-04-19 18:05:48 +0000254
255 # Use Digest::MD5. We don't have to be cryptographically secure. We're
Ted Kremenek7ea02e62008-04-19 18:07:44 +0000256 # just looking for duplicate files that come from a non-malicious source.
257 # We use Digest::MD5 because it is a standard Perl module that should
Ted Kremenek63c20172008-08-04 17:34:06 +0000258 # come bundled on most systems.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000259 open(FILE, $FName) or DieDiag("Cannot open $FName when computing Digest.\n");
Ted Kremeneka6e24812008-04-19 18:05:48 +0000260 binmode FILE;
261 my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest;
262 close(FILE);
263
Ted Kremenek63c20172008-08-04 17:34:06 +0000264 # Return the digest.
Ted Kremeneka6e24812008-04-19 18:05:48 +0000265 return $Result;
Ted Kremenek57cf4462008-04-18 15:09:30 +0000266}
267
268##----------------------------------------------------------------------------##
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000269# UpdatePrefix - Compute the common prefix of files.
270##----------------------------------------------------------------------------##
271
272my $Prefix;
273
274sub UpdatePrefix {
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000275 my $x = shift;
276 my $y = basename($x);
277 $x =~ s/\Q$y\E$//;
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000278
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000279 if (!defined $Prefix) {
280 $Prefix = $x;
281 return;
282 }
283
Ted Kremenek20b2bae2008-09-11 21:15:10 +0000284 chop $Prefix while (!($x =~ /^\Q$Prefix/));
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000285}
286
287sub GetPrefix {
288 return $Prefix;
289}
290
291##----------------------------------------------------------------------------##
292# UpdateInFilePath - Update the path in the report file.
293##----------------------------------------------------------------------------##
294
295sub UpdateInFilePath {
296 my $fname = shift;
297 my $regex = shift;
298 my $newtext = shift;
Ted Kremenek63c20172008-08-04 17:34:06 +0000299
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000300 open (RIN, $fname) or die "cannot open $fname";
Ted Kremenek63c20172008-08-04 17:34:06 +0000301 open (ROUT, ">", "$fname.tmp") or die "cannot open $fname.tmp";
302
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000303 while (<RIN>) {
304 s/$regex/$newtext/;
305 print ROUT $_;
306 }
Ted Kremenek63c20172008-08-04 17:34:06 +0000307
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000308 close (ROUT);
309 close (RIN);
Ted Kremenek20161e92008-07-15 20:18:21 +0000310 system("mv", "$fname.tmp", $fname);
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000311}
312
313##----------------------------------------------------------------------------##
Ted Kremenek5744dc22008-04-02 18:03:36 +0000314# ScanFile - Scan a report file for various identifying attributes.
315##----------------------------------------------------------------------------##
316
Ted Kremenek57cf4462008-04-18 15:09:30 +0000317# Sometimes a source file is scanned more than once, and thus produces
318# multiple error reports. We use a cache to solve this problem.
319
320my %AlreadyScanned;
321
Ted Kremenek5744dc22008-04-02 18:03:36 +0000322sub ScanFile {
323
324 my $Index = shift;
325 my $Dir = shift;
326 my $FName = shift;
327
Ted Kremenek57cf4462008-04-18 15:09:30 +0000328 # Compute a digest for the report file. Determine if we have already
329 # scanned a file that looks just like it.
330
331 my $digest = ComputeDigest("$Dir/$FName");
332
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000333 if (defined $AlreadyScanned{$digest}) {
Ted Kremenek57cf4462008-04-18 15:09:30 +0000334 # Redundant file. Remove it.
Ted Kremenek20161e92008-07-15 20:18:21 +0000335 system ("rm", "-f", "$Dir/$FName");
Ted Kremenek57cf4462008-04-18 15:09:30 +0000336 return;
337 }
338
339 $AlreadyScanned{$digest} = 1;
340
Ted Kremenek809709f2008-04-18 16:58:34 +0000341 # At this point the report file is not world readable. Make it happen.
Ted Kremenek20161e92008-07-15 20:18:21 +0000342 system ("chmod", "644", "$Dir/$FName");
Ted Kremenek684bb092008-04-18 15:18:20 +0000343
344 # Scan the report file for tags.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000345 open(IN, "$Dir/$FName") or DieDiag("Cannot open '$Dir/$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000346
Ted Kremeneka26ddab2009-01-27 01:53:39 +0000347 my $BugType = "";
Ted Kremenek22d6a632008-04-02 20:43:36 +0000348 my $BugFile = "";
Ted Kremenekebb74132008-09-21 06:58:09 +0000349 my $BugCategory;
Ted Kremenek22d6a632008-04-02 20:43:36 +0000350 my $BugPathLength = 1;
351 my $BugLine = 0;
Ted Kremenekebb74132008-09-21 06:58:09 +0000352
Ted Kremenek5744dc22008-04-02 18:03:36 +0000353 while (<IN>) {
Ted Kremenekd658e672009-08-03 23:45:27 +0000354 last if (/<!-- BUGMETAEND -->/);
Ted Kremenekebb74132008-09-21 06:58:09 +0000355
Ted Kremeneka26ddab2009-01-27 01:53:39 +0000356 if (/<!-- BUGTYPE (.*) -->$/) {
357 $BugType = $1;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000358 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000359 elsif (/<!-- BUGFILE (.*) -->$/) {
Ted Kremenek990c2f42008-12-03 19:19:23 +0000360 $BugFile = abs_path($1);
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000361 UpdatePrefix($BugFile);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000362 }
363 elsif (/<!-- BUGPATHLENGTH (.*) -->$/) {
364 $BugPathLength = $1;
365 }
366 elsif (/<!-- BUGLINE (.*) -->$/) {
367 $BugLine = $1;
Ted Kremenekebb74132008-09-21 06:58:09 +0000368 }
369 elsif (/<!-- BUGCATEGORY (.*) -->$/) {
370 $BugCategory = $1;
Ted Kremenek22d6a632008-04-02 20:43:36 +0000371 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000372 }
373
374 close(IN);
Ted Kremenekebb74132008-09-21 06:58:09 +0000375
376 if (!defined $BugCategory) {
377 $BugCategory = "Other";
378 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000379
Ted Kremeneka26ddab2009-01-27 01:53:39 +0000380 push @$Index,[ $FName, $BugCategory, $BugType, $BugFile, $BugLine,
Ted Kremenek81983112008-09-28 04:13:09 +0000381 $BugPathLength ];
Ted Kremenek22d6a632008-04-02 20:43:36 +0000382}
383
384##----------------------------------------------------------------------------##
Ted Kremenek3ce12072008-09-22 17:50:47 +0000385# CopyFiles - Copy resource files to target directory.
Ted Kremenek22d6a632008-04-02 20:43:36 +0000386##----------------------------------------------------------------------------##
387
Ted Kremenek3ce12072008-09-22 17:50:47 +0000388sub CopyFiles {
Ted Kremenek22d6a632008-04-02 20:43:36 +0000389
390 my $Dir = shift;
Ted Kremeneke15fa272008-10-13 21:46:42 +0000391
392 my $JS = Cwd::realpath("$RealBin/sorttable.js");
Ted Kremenek22d6a632008-04-02 20:43:36 +0000393
Ted Kremenek23cfca32008-06-16 22:40:14 +0000394 DieDiag("Cannot find 'sorttable.js'.\n")
Ted Kremeneke15fa272008-10-13 21:46:42 +0000395 if (! -r $JS);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000396
Ted Kremeneke15fa272008-10-13 21:46:42 +0000397 system ("cp", $JS, "$Dir");
Ted Kremenek22d6a632008-04-02 20:43:36 +0000398
Ted Kremenek23cfca32008-06-16 22:40:14 +0000399 DieDiag("Could not copy 'sorttable.js' to '$Dir'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000400 if (! -r "$Dir/sorttable.js");
Ted Kremenek3ce12072008-09-22 17:50:47 +0000401
Ted Kremeneke15fa272008-10-13 21:46:42 +0000402 my $CSS = Cwd::realpath("$RealBin/scanview.css");
403
Ted Kremenek3ce12072008-09-22 17:50:47 +0000404 DieDiag("Cannot find 'scanview.css'.\n")
Ted Kremeneke15fa272008-10-13 21:46:42 +0000405 if (! -r $CSS);
Ted Kremenek3ce12072008-09-22 17:50:47 +0000406
Ted Kremeneke15fa272008-10-13 21:46:42 +0000407 system ("cp", $CSS, "$Dir");
Ted Kremenek3ce12072008-09-22 17:50:47 +0000408
409 DieDiag("Could not copy 'scanview.css' to '$Dir'.\n")
Ted Kremeneke15fa272008-10-13 21:46:42 +0000410 if (! -r $CSS);
Ted Kremenek5744dc22008-04-02 18:03:36 +0000411}
412
413##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000414# Postprocess - Postprocess the results of an analysis scan.
415##----------------------------------------------------------------------------##
416
Sam Bishopa0e22662008-04-02 03:35:43 +0000417sub Postprocess {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000418
419 my $Dir = shift;
Ted Kremenek684bb092008-04-18 15:18:20 +0000420 my $BaseDir = shift;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000421
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000422 die "No directory specified." if (!defined $Dir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000423
424 if (! -d $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000425 Diag("No bugs found.\n");
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000426 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000427 }
428
429 opendir(DIR, $Dir);
Ted Kremenek938eef12009-02-17 23:31:05 +0000430 my @files = grep { /^report-.*\.html$/ } readdir(DIR);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000431 closedir(DIR);
432
Ted Kremenek938eef12009-02-17 23:31:05 +0000433 if (scalar(@files) == 0 and ! -e "$Dir/failures") {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000434 Diag("Removing directory '$Dir' because it contains no reports.\n");
Ted Kremenek20161e92008-07-15 20:18:21 +0000435 system ("rm", "-fR", $Dir);
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000436 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000437 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000438
Ted Kremenek991c54b2008-08-08 20:46:42 +0000439 # Scan each report file and build an index.
440 my @Index;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000441 foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); }
442
Ted Kremenek938eef12009-02-17 23:31:05 +0000443 # Scan the failures directory and use the information in the .info files
Ted Kremenekd52e4252008-08-25 20:45:07 +0000444 # to update the common prefix directory.
Ted Kremenek938eef12009-02-17 23:31:05 +0000445 my @failures;
446 my @attributes_ignored;
447 if (-d "$Dir/failures") {
448 opendir(DIR, "$Dir/failures");
449 @failures = grep { /[.]info.txt$/ && !/attribute_ignored/; } readdir(DIR);
Ted Kremenekd52e4252008-08-25 20:45:07 +0000450 closedir(DIR);
Ted Kremenek938eef12009-02-17 23:31:05 +0000451 opendir(DIR, "$Dir/failures");
452 @attributes_ignored = grep { /^attribute_ignored/; } readdir(DIR);
453 closedir(DIR);
454 foreach my $file (@failures) {
455 open IN, "$Dir/failures/$file" or DieDiag("cannot open $file\n");
Ted Kremenekd52e4252008-08-25 20:45:07 +0000456 my $Path = <IN>;
457 if (defined $Path) { UpdatePrefix($Path); }
458 close IN;
459 }
460 }
461
Ted Kremenek63c20172008-08-04 17:34:06 +0000462 # Generate an index.html file.
463 my $FName = "$Dir/index.html";
464 open(OUT, ">", $FName) or DieDiag("Cannot create file '$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000465
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000466 # Print out the header.
467
Ted Kremenek5744dc22008-04-02 18:03:36 +0000468print OUT <<ENDTEXT;
469<html>
470<head>
Ted Kremenek7cba1122008-09-22 01:35:58 +0000471<title>${HtmlTitle}</title>
Ted Kremenekf1435452008-09-23 22:34:51 +0000472<link type="text/css" rel="stylesheet" href="scanview.css"/>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000473<script src="sorttable.js"></script>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000474<script language='javascript' type="text/javascript">
475function SetDisplay(RowClass, DisplayVal)
476{
477 var Rows = document.getElementsByTagName("tr");
478 for ( var i = 0 ; i < Rows.length; ++i ) {
479 if (Rows[i].className == RowClass) {
480 Rows[i].style.display = DisplayVal;
481 }
482 }
483}
Ted Kremenekebb74132008-09-21 06:58:09 +0000484
Ted Kremenek2350a462008-10-28 19:56:52 +0000485function CopyCheckedStateToCheckButtons(SummaryCheckButton) {
486 var Inputs = document.getElementsByTagName("input");
487 for ( var i = 0 ; i < Inputs.length; ++i ) {
488 if (Inputs[i].type == "checkbox") {
489 if(Inputs[i] != SummaryCheckButton) {
490 Inputs[i].checked = SummaryCheckButton.checked;
491 Inputs[i].onclick();
492 }
493 }
494 }
495}
496
Ted Kremenek999e1202008-10-28 20:09:57 +0000497function returnObjById( id ) {
498 if (document.getElementById)
499 var returnVar = document.getElementById(id);
500 else if (document.all)
501 var returnVar = document.all[id];
502 else if (document.layers)
503 var returnVar = document.layers[id];
504 return returnVar;
505}
506
507var NumUnchecked = 0;
508
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000509function ToggleDisplay(CheckButton, ClassName) {
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000510 if (CheckButton.checked) {
511 SetDisplay(ClassName, "");
Ted Kremenek999e1202008-10-28 20:09:57 +0000512 if (--NumUnchecked == 0) {
513 returnObjById("AllBugsCheck").checked = true;
514 }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000515 }
516 else {
517 SetDisplay(ClassName, "none");
Ted Kremenek999e1202008-10-28 20:09:57 +0000518 NumUnchecked++;
519 returnObjById("AllBugsCheck").checked = false;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000520 }
521}
522</script>
Ted Kremenek1d1abb12008-09-22 17:52:58 +0000523<!-- SUMMARYENDHEAD -->
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000524</head>
525<body>
Ted Kremenek7cba1122008-09-22 01:35:58 +0000526<h1>${HtmlTitle}</h1>
527
528<table>
529<tr><th>User:</th><td>${UserName}\@${HostName}</td></tr>
530<tr><th>Working Directory:</th><td>${CurrentDir}</td></tr>
531<tr><th>Command Line:</th><td>${CmdArgs}</td></tr>
532<tr><th>Date:</th><td>${Date}</td></tr>
533ENDTEXT
534
535print OUT "<tr><th>Version:</th><td>${BuildName} (${BuildDate})</td></tr>\n"
536 if (defined($BuildName) && defined($BuildDate));
537
538print OUT <<ENDTEXT;
539</table>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000540ENDTEXT
541
Ted Kremenek991c54b2008-08-08 20:46:42 +0000542 if (scalar(@files)) {
543 # Print out the summary table.
544 my %Totals;
Ted Kremenekebb74132008-09-21 06:58:09 +0000545
Ted Kremenek991c54b2008-08-08 20:46:42 +0000546 for my $row ( @Index ) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000547 my $bug_type = ($row->[2]);
548 my $bug_category = ($row->[1]);
549 my $key = "$bug_category:$bug_type";
550
551 if (!defined $Totals{$key}) { $Totals{$key} = [1,$bug_category,$bug_type]; }
552 else { $Totals{$key}->[0]++; }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000553 }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000554
Ted Kremenek7cba1122008-09-22 01:35:58 +0000555 print OUT "<h2>Bug Summary</h2>";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000556
557 if (defined $BuildName) {
558 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 +0000559 }
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000560
Ted Kremenek2350a462008-10-28 19:56:52 +0000561 my $TotalBugs = scalar(@Index);
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000562print OUT <<ENDTEXT;
Ted Kremenekebb74132008-09-21 06:58:09 +0000563<table>
564<thead><tr><td>Bug Type</td><td>Quantity</td><td class="sorttable_nosort">Display?</td></tr></thead>
Ted Kremenek999e1202008-10-28 20:09:57 +0000565<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 +0000566ENDTEXT
567
Ted Kremenekebb74132008-09-21 06:58:09 +0000568 my $last_category;
569
570 for my $key (
571 sort {
572 my $x = $Totals{$a};
573 my $y = $Totals{$b};
574 my $res = $x->[1] cmp $y->[1];
575 $res = $x->[2] cmp $y->[2] if ($res == 0);
576 $res
577 } keys %Totals )
578 {
579 my $val = $Totals{$key};
580 my $category = $val->[1];
581 if (!defined $last_category or $last_category ne $category) {
582 $last_category = $category;
583 print OUT "<tr><th>$category</th><th colspan=2></th></tr>\n";
584 }
585 my $x = lc $key;
586 $x =~ s/[ ,'":\/()]+/_/g;
587 print OUT "<tr><td class=\"SUMM_DESC\">";
588 print OUT $val->[2];
Ted Kremenek2350a462008-10-28 19:56:52 +0000589 print OUT "</td><td class=\"Q\">";
Ted Kremenekebb74132008-09-21 06:58:09 +0000590 print OUT $val->[0];
591 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 +0000592 }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000593
594 # Print out the table of errors.
595
596print OUT <<ENDTEXT;
597</table>
Ted Kremenek7cba1122008-09-22 01:35:58 +0000598<h2>Reports</h2>
Ted Kremenekebb74132008-09-21 06:58:09 +0000599
600<table class="sortable" style="table-layout:automatic">
601<thead><tr>
602 <td>Bug Group</td>
603 <td class="sorttable_sorted">Bug Type<span id="sorttable_sortfwdind">&nbsp;&#x25BE;</span></td>
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000604 <td>File</td>
Ted Kremenekebb74132008-09-21 06:58:09 +0000605 <td class="Q">Line</td>
Ted Kremenek81983112008-09-28 04:13:09 +0000606 <td class="Q">Path Length</td>
Ted Kremenek2645c772008-07-07 16:58:44 +0000607 <td class="sorttable_nosort"></td>
Ted Kremenekebb74132008-09-21 06:58:09 +0000608 <!-- REPORTBUGCOL -->
609</tr></thead>
610<tbody>
Ted Kremenek5744dc22008-04-02 18:03:36 +0000611ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000612
Ted Kremenek991c54b2008-08-08 20:46:42 +0000613 my $prefix = GetPrefix();
614 my $regex;
615 my $InFileRegex;
616 my $InFilePrefix = "File:</td><td>";
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000617
Ted Kremenek991c54b2008-08-08 20:46:42 +0000618 if (defined $prefix) {
619 $regex = qr/^\Q$prefix\E/is;
620 $InFileRegex = qr/\Q$InFilePrefix$prefix\E/is;
621 }
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000622
Ted Kremenekebb74132008-09-21 06:58:09 +0000623 for my $row ( sort { $a->[2] cmp $b->[2] } @Index ) {
624 my $x = "$row->[1]:$row->[2]";
625 $x = lc $x;
626 $x =~ s/[ ,'":\/()]+/_/g;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000627
Ted Kremenek991c54b2008-08-08 20:46:42 +0000628 my $ReportFile = $row->[0];
Ted Kremenekebb74132008-09-21 06:58:09 +0000629
630 print OUT "<tr class=\"bt_$x\">";
631 print OUT "<td class=\"DESC\">";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000632 print OUT $row->[1];
Ted Kremenekebb74132008-09-21 06:58:09 +0000633 print OUT "</td>";
634 print OUT "<td class=\"DESC\">";
635 print OUT $row->[2];
636 print OUT "</td>";
637
638 # Update the file prefix.
639 my $fname = $row->[3];
Ted Kremenekebb74132008-09-21 06:58:09 +0000640
Ted Kremenek991c54b2008-08-08 20:46:42 +0000641 if (defined $regex) {
642 $fname =~ s/$regex//;
643 UpdateInFilePath("$Dir/$ReportFile", $InFileRegex, $InFilePrefix)
644 }
Ted Kremenekebb74132008-09-21 06:58:09 +0000645
Ted Kremenek91639ef2008-09-22 17:42:31 +0000646 print OUT "<td>";
Ted Kremenekebb74132008-09-21 06:58:09 +0000647 my @fname = split /\//,$fname;
648 if ($#fname > 0) {
649 while ($#fname >= 0) {
650 my $x = shift @fname;
651 print OUT $x;
652 if ($#fname >= 0) {
653 print OUT "<span class=\"W\"> </span>/";
654 }
655 }
656 }
657 else {
658 print OUT $fname;
Ted Kremenek91639ef2008-09-22 17:42:31 +0000659 }
Ted Kremenekebb74132008-09-21 06:58:09 +0000660 print OUT "</td>";
661
662 # Print out the quantities.
Ted Kremenek81983112008-09-28 04:13:09 +0000663 for my $j ( 4 .. 5 ) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000664 print OUT "<td class=\"Q\">$row->[$j]</td>";
665 }
666
Ted Kremenek991c54b2008-08-08 20:46:42 +0000667 # Print the rest of the columns.
Ted Kremenek81983112008-09-28 04:13:09 +0000668 for (my $j = 6; $j <= $#{$row}; ++$j) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000669 print OUT "<td>$row->[$j]</td>"
Ted Kremenek991c54b2008-08-08 20:46:42 +0000670 }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000671
Ted Kremenek991c54b2008-08-08 20:46:42 +0000672 # Emit the "View" link.
Ted Kremenek68005dd2008-09-22 17:39:18 +0000673 print OUT "<td><a href=\"$ReportFile#EndPath\">View Report</a></td>";
Ted Kremenek3cea9ee2008-07-30 17:58:08 +0000674
Daniel Dunbare43038e2008-09-19 23:18:44 +0000675 # Emit REPORTBUG markers.
Ted Kremenekebb74132008-09-21 06:58:09 +0000676 print OUT "\n<!-- REPORTBUG id=\"$ReportFile\" -->\n";
Daniel Dunbare43038e2008-09-19 23:18:44 +0000677
Ted Kremenek991c54b2008-08-08 20:46:42 +0000678 # End the row.
679 print OUT "</tr>\n";
680 }
681
Ted Kremenekebb74132008-09-21 06:58:09 +0000682 print OUT "</tbody>\n</table>\n\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000683 }
684
Ted Kremenek938eef12009-02-17 23:31:05 +0000685 if (scalar (@failures) || scalar(@attributes_ignored)) {
686 print OUT "<h2>Analyzer Failures</h2>\n";
687
688 if (scalar @attributes_ignored) {
689 print OUT "The analyzer's parser ignored the following attributes:<p>\n";
690 print OUT "<table>\n";
691 print OUT "<thead><tr><td>Attribute</td><td>Source File</td><td>Preprocessed File</td><td>STDERR Output</td></tr></thead>\n";
692 foreach my $file (sort @attributes_ignored) {
693 die "cannot demangle attribute name\n" if (! ($file =~ /^attribute_ignored_(.+).txt/));
694 my $attribute = $1;
695 # Open the attribute file to get the first file that failed.
696 next if (!open (ATTR, "$Dir/failures/$file"));
697 my $ppfile = <ATTR>;
698 chomp $ppfile;
699 close ATTR;
700 next if (! -e "$Dir/failures/$ppfile");
701 # Open the info file and get the name of the source file.
702 open (INFO, "$Dir/failures/$ppfile.info.txt") or
703 die "Cannot open $Dir/failures/$ppfile.info.txt\n";
704 my $srcfile = <INFO>;
705 chomp $srcfile;
706 close (INFO);
707 # Print the information in the table.
708 my $prefix = GetPrefix();
709 if (defined $prefix) { $srcfile =~ s/^\Q$prefix//; }
710 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";
711 my $ppfile_clang = $ppfile;
712 $ppfile_clang =~ s/[.](.+)$/.clang.$1/;
713 print OUT " <!-- REPORTPROBLEM src=\"$srcfile\" file=\"failures/$ppfile\" clangfile=\"failures/$ppfile_clang\" stderr=\"failures/$ppfile.stderr.txt\" info=\"failures/$ppfile.info.txt\" -->\n";
714 }
715 print OUT "</table>\n";
716 }
717
718 if (scalar @failures) {
719 print OUT "<p>The analyzer had problems processing the following files:</p>\n";
720 print OUT "<table>\n";
721 print OUT "<thead><tr><td>Problem</td><td>Source File</td><td>Preprocessed File</td><td>STDERR Output</td></tr></thead>\n";
722 foreach my $file (sort @failures) {
Ted Kremenek82a12532008-09-25 00:25:16 +0000723 $file =~ /(.+).info.txt$/;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000724 # Get the preprocessed file.
725 my $ppfile = $1;
726 # Open the info file and get the name of the source file.
Ted Kremenek938eef12009-02-17 23:31:05 +0000727 open (INFO, "$Dir/failures/$file") or
728 die "Cannot open $Dir/failures/$file\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000729 my $srcfile = <INFO>;
Ted Kremenek5d31f832008-08-18 18:38:29 +0000730 chomp $srcfile;
731 my $problem = <INFO>;
732 chomp $problem;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000733 close (INFO);
734 # Print the information in the table.
Ted Kremenekd52e4252008-08-25 20:45:07 +0000735 my $prefix = GetPrefix();
Ted Kremenek9f9b1fd2008-09-12 22:49:36 +0000736 if (defined $prefix) { $srcfile =~ s/^\Q$prefix//; }
Ted Kremenek938eef12009-02-17 23:31:05 +0000737 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 +0000738 my $ppfile_clang = $ppfile;
739 $ppfile_clang =~ s/[.](.+)$/.clang.$1/;
Ted Kremenek938eef12009-02-17 23:31:05 +0000740 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 +0000741 }
Ted Kremenek938eef12009-02-17 23:31:05 +0000742 print OUT "</table>\n";
743 }
744 print OUT "<p>Please consider submitting preprocessed files as <a href=\"http://clang.llvm.org/StaticAnalysisUsage.html#filingbugs\">bug reports</a>. <!-- REPORTCRASHES --> </p>\n";
Ted Kremenek5744dc22008-04-02 18:03:36 +0000745 }
746
Ted Kremenek991c54b2008-08-08 20:46:42 +0000747 print OUT "</body></html>\n";
Ted Kremenek5744dc22008-04-02 18:03:36 +0000748 close(OUT);
Ted Kremenek3ce12072008-09-22 17:50:47 +0000749 CopyFiles($Dir);
Ted Kremenek20161e92008-07-15 20:18:21 +0000750
751 # Make sure $Dir and $BaseDir are world readable/executable.
752 system("chmod", "755", $Dir);
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000753 if (defined $BaseDir) { system("chmod", "755", $BaseDir); }
Ted Kremenek20161e92008-07-15 20:18:21 +0000754
Ted Kremenek23cfca32008-06-16 22:40:14 +0000755 my $Num = scalar(@Index);
Ted Kremenek150c2122008-07-11 19:15:05 +0000756 Diag("$Num bugs found.\n");
757 if ($Num > 0 && -r "$Dir/index.html") {
Ted Kremenek5950b3f2008-09-22 06:47:01 +0000758 Diag("Run 'scan-view $Dir' to examine bug reports.\n");
Ted Kremenek150c2122008-07-11 19:15:05 +0000759 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000760
Ted Kremenek938eef12009-02-17 23:31:05 +0000761 DiagCrashes($Dir) if (scalar @failures || scalar @attributes_ignored);
Ted Kremenek991c54b2008-08-08 20:46:42 +0000762
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000763 return $Num;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000764}
765
766##----------------------------------------------------------------------------##
Ted Kremenekdab11102008-04-02 04:43:42 +0000767# RunBuildCommand - Run the build command.
768##----------------------------------------------------------------------------##
769
Ted Kremenek6b628982008-04-30 23:47:12 +0000770sub AddIfNotPresent {
771 my $Args = shift;
772 my $Arg = shift;
773 my $found = 0;
774
775 foreach my $k (@$Args) {
776 if ($k eq $Arg) {
777 $found = 1;
778 last;
779 }
780 }
781
782 if ($found == 0) {
783 push @$Args, $Arg;
784 }
785}
786
Ted Kremenekdab11102008-04-02 04:43:42 +0000787sub RunBuildCommand {
788
789 my $Args = shift;
Ted Kremenek7442ca62008-04-02 16:04:51 +0000790 my $IgnoreErrors = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000791 my $Cmd = $Args->[0];
Ted Kremenek6195c372008-06-02 21:52:47 +0000792 my $CCAnalyzer = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000793
Ted Kremenek3301cb12008-06-30 18:18:16 +0000794 # Get only the part of the command after the last '/'.
795 if ($Cmd =~ /\/([^\/]+)$/) {
796 $Cmd = $1;
797 }
798
Ted Kremenek92548fe2008-11-19 01:46:21 +0000799 if ($Cmd =~ /(.*\/?gcc[^\/]*$)/ or
800 $Cmd =~ /(.*\/?cc[^\/]*$)/ or
801 $Cmd =~ /(.*\/?llvm-gcc[^\/]*$)/ or
802 $Cmd =~ /(.*\/?ccc-analyzer[^\/]*$)/) {
803
804 if (!($Cmd =~ /ccc-analyzer/) and !defined $ENV{"CCC_CC"}) {
Ted Kremenek51365b52009-12-15 02:35:54 +0000805 $ENV{"CCC_CC"} = $1;
Ted Kremenek92548fe2008-11-19 01:46:21 +0000806 }
807
Ted Kremenekdab11102008-04-02 04:43:42 +0000808 shift @$Args;
Ted Kremenek6195c372008-06-02 21:52:47 +0000809 unshift @$Args, $CCAnalyzer;
Ted Kremenekdab11102008-04-02 04:43:42 +0000810 }
Ted Kremenek51365b52009-12-15 02:35:54 +0000811 elsif ($Cmd =~ /(.*\/?g\+\+[^\/]*$)/ or
812 $Cmd =~ /(.*\/?c\+\+[^\/]*$)/ or
813 $Cmd =~ /(.*\/?llvm-g\+\+[^\/]*$)/ or
814 $Cmd =~ /(.*\/?c\+\+-analyzer[^\/]*$)/) {
815 if (!($Cmd =~ /c\+\+-analyzer/) and !defined $ENV{"CCC_CXX"}) {
816 $ENV{"CCC_CXX"} = $1;
817 }
818 shift @$Args;
819 unshift @$Args, $CCAnalyzer;
820 }
Ted Kremenek7442ca62008-04-02 16:04:51 +0000821 elsif ($IgnoreErrors) {
822 if ($Cmd eq "make" or $Cmd eq "gmake") {
Ted Kremenek6fba85d2009-12-11 23:22:52 +0000823 AddIfNotPresent($Args, "CC=$CCAnalyzer");
824 AddIfNotPresent($Args, "CXX=$CCAnalyzer");
Ted Kremenek6b628982008-04-30 23:47:12 +0000825 AddIfNotPresent($Args,"-k");
Ted Kremenek8912b542008-05-13 21:28:02 +0000826 AddIfNotPresent($Args,"-i");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000827 }
828 elsif ($Cmd eq "xcodebuild") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000829 AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000830 }
Ted Kremenek6b628982008-04-30 23:47:12 +0000831 }
832
Ted Kremenek6b628982008-04-30 23:47:12 +0000833 if ($Cmd eq "xcodebuild") {
Ted Kremenek87752b22009-05-15 21:14:16 +0000834 # Check if using iPhone SDK 3.0 (simulator). If so the compiler being
835 # used should be gcc-4.2.
836 if (!defined $ENV{"CCC_CC"}) {
837 for (my $i = 0 ; $i < scalar(@$Args); ++$i) {
838 if ($Args->[$i] eq "-sdk" && $i + 1 < scalar(@$Args)) {
839 if (@$Args[$i+1] =~ /^iphonesimulator3/) {
840 $ENV{"CCC_CC"} = "gcc-4.2";
Ted Kremenek51365b52009-12-15 02:35:54 +0000841 $ENV{"CCC_CXX"} = "g++-4.2";
Ted Kremenek87752b22009-05-15 21:14:16 +0000842 }
843 }
844 }
845 }
846
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000847 # Disable distributed builds for xcodebuild.
Ted Kremenek6b628982008-04-30 23:47:12 +0000848 AddIfNotPresent($Args,"-nodistribute");
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000849
850 # Disable PCH files until clang supports them.
851 AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO");
Ted Kremenek915e9722008-05-27 23:18:07 +0000852
853 # When 'CC' is set, xcodebuild uses it to do all linking, even if we are
854 # linking C++ object files. Set 'LDPLUSPLUS' so that xcodebuild uses 'g++'
855 # when linking such files.
Ted Kremenek51365b52009-12-15 02:35:54 +0000856 if (!defined $ENV{'CCC_CXX'}) {
857 $ENV{'CCC_CXX'} = 'g++';
858 }
859 $ENV{'LDPLUSPLUS'} = $ENV{'CCC_CXX'};
Ted Kremenek6b628982008-04-30 23:47:12 +0000860 }
Ted Kremenekdab11102008-04-02 04:43:42 +0000861
Ted Kremenek5a4ddaf2008-08-25 20:10:45 +0000862 return (system(@$Args) >> 8);
Ted Kremenekdab11102008-04-02 04:43:42 +0000863}
864
Ted Kremenekdab11102008-04-02 04:43:42 +0000865##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000866# DisplayHelp - Utility function to display all help options.
867##----------------------------------------------------------------------------##
868
Sam Bishopa0e22662008-04-02 03:35:43 +0000869sub DisplayHelp {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000870
Ted Kremenek5744dc22008-04-02 18:03:36 +0000871print <<ENDTEXT;
Sam Bishopa0e22662008-04-02 03:35:43 +0000872USAGE: $Prog [options] <build command> [build options]
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000873
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000874ENDTEXT
875
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000876 if (defined $BuildName) {
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000877 print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n";
878 }
879
880print <<ENDTEXT;
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000881OPTIONS:
882
Ted Kremeneke15fa272008-10-13 21:46:42 +0000883 -analyze-headers - Also analyze functions in #included files.
Ted Kremenek8382cf52009-11-13 18:46:29 +0000884
885 --experimental-checks - Enable experimental checks that are currently in heavy testing
Ted Kremeneke15fa272008-10-13 21:46:42 +0000886
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000887 -o - Target directory for HTML report files. Subdirectories
Sam Bishopa0e22662008-04-02 03:35:43 +0000888 will be created as needed to represent separate "runs" of
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000889 the analyzer. If this option is not specified, a directory
Ted Kremenekffda0b42008-10-31 05:48:42 +0000890 is created in /tmp (TMPDIR on Mac OS X) to store the reports.
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000891
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000892 -h - Display this message.
893 --help
Ted Kremenek1262fc42008-05-14 20:10:33 +0000894
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000895 -k - Add a "keep on going" option to the specified build command.
896 --keep-going This option currently supports make and xcodebuild.
Ted Kremenekf02e8db2008-04-02 16:41:25 +0000897 This is a convenience option; one can specify this
898 behavior directly using build options.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000899
Ted Kremenek7cba1122008-09-22 01:35:58 +0000900 --html-title [title] - Specify the title used on generated HTML pages.
901 --html-title=[title] If not specified, a default title will be used.
902
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000903 -plist - By default the output of scan-build is a set of HTML files.
904 This option outputs the results as a set of .plist files.
905
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000906 --status-bugs - By default, the exit status of $Prog is the same as the
907 executed build command. Specifying this option causes the
908 exit status of $Prog to be 1 if it found potential bugs
909 and 0 otherwise.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000910
Ted Kremenek386c6932008-09-03 17:59:35 +0000911 --use-cc [compiler path] - By default, $Prog uses 'gcc' to compile and link
912 --use-cc=[compiler path] your C and Objective-C code. Use this option
913 to specify an alternate compiler.
914
915 --use-c++ [compiler path] - By default, $Prog uses 'g++' to compile and link
916 --use-c++=[compiler path] your C++ and Objective-C++ code. Use this option
917 to specify an alternate compiler.
Ted Kremenekf17ef3c2008-08-21 21:47:09 +0000918
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000919 -v - Verbose output from $Prog and the analyzer.
Ted Kremenek386c6932008-09-03 17:59:35 +0000920 A second and third '-v' increases verbosity.
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000921
922 -V - View analysis results in a web browser when the build
923 --view completes.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000924
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000925ADVANCED OPTIONS:
926
Ted Kremenek9f4ecb32009-02-20 21:49:22 +0000927 -constraints [model] - Specify the contraint engine used by the analyzer.
928 By default the 'range' model is used. Specifying
929 'basic' uses a simpler, less powerful constraint model
Ted Kremenekd4c76842009-02-21 04:46:41 +0000930 used by checker-0.160 and earlier.
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000931
932 -store [model] - Specify the store model used by the analyzer. By default,
Ted Kremenekb8bb3e72009-09-25 05:55:59 +0000933 the 'region' store model is used. 'region' specifies a field-
934 sensitive store model. Users can also specify 'basic', which
935 is far less precise but can more quickly analyze code.
936 'basic' was the default store model for checker-0.221 and
937 earlier.
938
Ted Kremeneke600bed2009-07-30 23:55:19 +0000939 -no-failure-reports - Do not create a 'failures' subdirectory that includes
940 analyzer crash reports and preprocessed source files.
Ted Kremenekb7770c02008-07-15 17:06:13 +0000941
Ted Kremenek386c6932008-09-03 17:59:35 +0000942AVAILABLE ANALYSES (multiple analyses may be specified):
Ted Kremenekd52e4252008-08-25 20:45:07 +0000943
944ENDTEXT
Ted Kremenekb7770c02008-07-15 17:06:13 +0000945
946 foreach my $Analysis (sort keys %AvailableAnalyses) {
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000947 if (defined $AnalysesDefaultEnabled{$Analysis}) {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000948 print " (+)";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000949 }
950 else {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000951 print " ";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000952 }
953
954 print " $Analysis $AvailableAnalyses{$Analysis}\n";
955 }
956
957print <<ENDTEXT
958
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000959 NOTE: "(+)" indicates that an analysis is enabled by default unless one
960 or more analysis options are specified
Ted Kremenekb7770c02008-07-15 17:06:13 +0000961
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000962BUILD OPTIONS
963
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000964 You can specify any build option acceptable to the build command.
Ted Kremenek39eefde2008-04-02 16:47:27 +0000965
Ted Kremenek5744dc22008-04-02 18:03:36 +0000966EXAMPLE
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000967
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000968 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000969
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000970 The above example causes analysis reports to be deposited into
971 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
972 A different subdirectory is created each time $Prog analyzes a project.
973 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000974
975ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000976}
977
978##----------------------------------------------------------------------------##
Ted Kremenek7cba1122008-09-22 01:35:58 +0000979# HtmlEscape - HTML entity encode characters that are special in HTML
980##----------------------------------------------------------------------------##
981
982sub HtmlEscape {
983 # copy argument to new variable so we don't clobber the original
984 my $arg = shift || '';
985 my $tmp = $arg;
Ted Kremenek87f8de72008-11-03 07:44:16 +0000986 $tmp =~ s/&/&amp;/g;
987 $tmp =~ s/</&lt;/g;
988 $tmp =~ s/>/&gt;/g;
Ted Kremenek7cba1122008-09-22 01:35:58 +0000989 return $tmp;
990}
991
992##----------------------------------------------------------------------------##
993# ShellEscape - backslash escape characters that are special to the shell
994##----------------------------------------------------------------------------##
995
996sub ShellEscape {
997 # copy argument to new variable so we don't clobber the original
998 my $arg = shift || '';
Ted Kremenek87f8de72008-11-03 07:44:16 +0000999 if ($arg =~ /["\s]/) { return "'" . $arg . "'"; }
1000 return $arg;
Ted Kremenek7cba1122008-09-22 01:35:58 +00001001}
1002
1003##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001004# Process command-line arguments.
1005##----------------------------------------------------------------------------##
1006
Ted Kremeneke15fa272008-10-13 21:46:42 +00001007my $AnalyzeHeaders = 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001008my $HtmlDir; # Parent directory to store HTML files.
1009my $IgnoreErrors = 0; # Ignore build errors.
Ted Kremenek7f8a3252008-04-02 18:42:49 +00001010my $ViewResults = 0; # View results when the build terminates.
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001011my $ExitStatusFoundBugs = 0; # Exit status reflects whether bugs were found
Ted Kremenekb7770c02008-07-15 17:06:13 +00001012my @AnalysesToRun;
Zhongxing Xu07c37672008-10-27 14:26:32 +00001013my $StoreModel;
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +00001014my $ConstraintsModel;
Ted Kremenek8d8bc912009-08-04 17:05:18 +00001015my $OutputFormat = "html";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001016
1017if (!@ARGV) {
1018 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +00001019 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001020}
1021
1022while (@ARGV) {
1023
1024 # Scan for options we recognize.
1025
1026 my $arg = $ARGV[0];
1027
Sam Bishop2f2418e2008-04-03 14:29:47 +00001028 if ($arg eq "-h" or $arg eq "--help") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001029 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +00001030 exit 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001031 }
1032
Ted Kremeneke15fa272008-10-13 21:46:42 +00001033 if ($arg eq '-analyze-headers') {
1034 shift @ARGV;
1035 $AnalyzeHeaders = 1;
1036 next;
1037 }
1038
Ted Kremenekfc1d3402008-08-04 18:15:26 +00001039 if (defined $AvailableAnalyses{$arg}) {
Ted Kremenek1262fc42008-05-14 20:10:33 +00001040 shift @ARGV;
Ted Kremenekb7770c02008-07-15 17:06:13 +00001041 push @AnalysesToRun, $arg;
Ted Kremenek1262fc42008-05-14 20:10:33 +00001042 next;
1043 }
1044
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001045 if ($arg eq "-o") {
1046 shift @ARGV;
1047
1048 if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +00001049 DieDiag("'-o' option requires a target directory name.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001050 }
1051
Ted Kremenekdb51a552009-03-11 18:20:33 +00001052 # Construct an absolute path. Uses the current working directory
1053 # as a base if the original path was not absolute.
1054 $HtmlDir = abs_path(shift @ARGV);
1055
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001056 next;
1057 }
Ted Kremenek7cba1122008-09-22 01:35:58 +00001058
1059 if ($arg =~ /^--html-title(=(.+))?$/) {
1060 shift @ARGV;
1061
Ted Kremenek278a5512009-05-12 18:04:43 +00001062 if (!defined $2 || $2 eq '') {
Ted Kremenek7cba1122008-09-22 01:35:58 +00001063 if (!@ARGV) {
1064 DieDiag("'--html-title' option requires a string.\n");
1065 }
1066
1067 $HtmlTitle = shift @ARGV;
1068 } else {
1069 $HtmlTitle = $2;
1070 }
1071
1072 next;
1073 }
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001074
Ted Kremenek2b74ab62008-04-01 21:22:03 +00001075 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001076 shift @ARGV;
1077 $IgnoreErrors = 1;
1078 next;
1079 }
1080
Ted Kremenek2a1814a2009-11-13 18:49:48 +00001081 if ($arg eq "--experimental-checks") {
Ted Kremenek8382cf52009-11-13 18:46:29 +00001082 shift @ARGV;
1083 $ENV{"CCC_EXPERIMENTAL_CHECKS"} = 1;
1084 next;
1085 }
1086
Ted Kremenekf17ef3c2008-08-21 21:47:09 +00001087 if ($arg =~ /^--use-cc(=(.+))?$/) {
1088 shift @ARGV;
1089 my $cc;
1090
Ted Kremenek278a5512009-05-12 18:04:43 +00001091 if (!defined $2 || $2 eq "") {
Ted Kremenekf17ef3c2008-08-21 21:47:09 +00001092 if (!@ARGV) {
1093 DieDiag("'--use-cc' option requires a compiler executable name.\n");
1094 }
1095 $cc = shift @ARGV;
1096 }
1097 else {
1098 $cc = $2;
1099 }
1100
1101 $ENV{"CCC_CC"} = $cc;
1102 next;
1103 }
1104
Ted Kremenek7cba1122008-09-22 01:35:58 +00001105 if ($arg =~ /^--use-c\+\+(=(.+))?$/) {
Ted Kremenek386c6932008-09-03 17:59:35 +00001106 shift @ARGV;
Ted Kremenek51365b52009-12-15 02:35:54 +00001107 my $cxx;
Ted Kremenek386c6932008-09-03 17:59:35 +00001108
Ted Kremenek278a5512009-05-12 18:04:43 +00001109 if (!defined $2 || $2 eq "") {
Ted Kremenek386c6932008-09-03 17:59:35 +00001110 if (!@ARGV) {
1111 DieDiag("'--use-c++' option requires a compiler executable name.\n");
1112 }
Ted Kremenek51365b52009-12-15 02:35:54 +00001113 $cxx = shift @ARGV;
Ted Kremenek386c6932008-09-03 17:59:35 +00001114 }
1115 else {
Ted Kremenek51365b52009-12-15 02:35:54 +00001116 $cxx = $2;
Ted Kremenek386c6932008-09-03 17:59:35 +00001117 }
Ted Kremenek51365b52009-12-15 02:35:54 +00001118
1119 $ENV{"CCC_CXX"} = $cxx;
Ted Kremenek386c6932008-09-03 17:59:35 +00001120 next;
1121 }
1122
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001123 if ($arg eq "-v") {
1124 shift @ARGV;
1125 $Verbose++;
1126 next;
1127 }
1128
Ted Kremenek7f8a3252008-04-02 18:42:49 +00001129 if ($arg eq "-V" or $arg eq "--view") {
1130 shift @ARGV;
1131 $ViewResults = 1;
1132 next;
1133 }
1134
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001135 if ($arg eq "--status-bugs") {
1136 shift @ARGV;
1137 $ExitStatusFoundBugs = 1;
1138 next;
1139 }
Zhongxing Xu07c37672008-10-27 14:26:32 +00001140
1141 if ($arg eq "-store") {
1142 shift @ARGV;
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +00001143 $StoreModel = shift @ARGV;
1144 next;
1145 }
1146
1147 if ($arg eq "-constraints") {
1148 shift @ARGV;
1149 $ConstraintsModel = shift @ARGV;
Zhongxing Xu07c37672008-10-27 14:26:32 +00001150 next;
1151 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001152
Ted Kremenekdb4f5f22008-11-04 00:02:53 +00001153 if ($arg eq "-plist") {
1154 shift @ARGV;
1155 $OutputFormat = "plist";
1156 next;
1157 }
Ted Kremenek7753b352009-07-27 22:10:34 +00001158 if ($arg eq "-plist-html") {
1159 shift @ARGV;
1160 $OutputFormat = "plist-html";
1161 next;
1162 }
Ted Kremeneke600bed2009-07-30 23:55:19 +00001163
1164 if ($arg eq "-no-failure-reports") {
1165 $ENV{"CCC_REPORT_FAILURES"} = 0;
1166 next;
1167 }
Ted Kremenek7753b352009-07-27 22:10:34 +00001168
Ted Kremenek23cfca32008-06-16 22:40:14 +00001169 DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/);
Ted Kremenek0062ad42008-04-02 16:35:01 +00001170
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001171 last;
1172}
1173
1174if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +00001175 Diag("No build command specified.\n\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001176 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +00001177 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001178}
1179
Ted Kremenek7cba1122008-09-22 01:35:58 +00001180$CmdArgs = HtmlEscape(join(' ', map(ShellEscape($_), @ARGV)));
1181$HtmlTitle = "${CurrentDirSuffix} - scan-build results"
1182 unless (defined($HtmlTitle));
Ted Kremenek386c6932008-09-03 17:59:35 +00001183
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001184# Determine the output directory for the HTML reports.
Ted Kremenek684bb092008-04-18 15:18:20 +00001185my $BaseDir = $HtmlDir;
Sam Bishopa0e22662008-04-02 03:35:43 +00001186$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001187
1188# Set the appropriate environment variables.
Sam Bishopa0e22662008-04-02 03:35:43 +00001189SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001190
Ted Kremenek51365b52009-12-15 02:35:54 +00001191my $AbsRealBin = Cwd::realpath($RealBin);
1192my $Cmd = "$AbsRealBin/libexec/ccc-analyzer";
1193my $CmdCXX = "$AbsRealBin/libexec/c++-analyzer";
1194
Ted Kremenekce87b922009-02-25 22:54:02 +00001195if (!defined $Cmd || ! -x $Cmd) {
Ted Kremenek51365b52009-12-15 02:35:54 +00001196 $Cmd = "$AbsRealBin/ccc-analyzer";
Ted Kremenek6b896362009-02-27 06:17:05 +00001197 DieDiag("Executable 'ccc-analyzer' does not exist at '$Cmd'\n") if(! -x $Cmd);
Ted Kremenekce87b922009-02-25 22:54:02 +00001198}
Ted Kremenek51365b52009-12-15 02:35:54 +00001199if (!defined $CmdCXX || ! -x $CmdCXX) {
1200 $CmdCXX = "$AbsRealBin/c++-analyzer";
1201 DieDiag("Executable 'c++-analyzer' does not exist at '$CmdCXX'\n") if(! -x $CmdCXX);
1202}
Ted Kremenekf22eacb2008-04-18 22:00:56 +00001203
Ted Kremenekfd9df0e2009-05-09 19:19:28 +00001204if (!defined $ClangSB || ! -x $ClangSB) {
1205 Diag("'clang' executable not found in '$RealBin/bin'.\n");
1206 Diag("Using 'clang' from path.\n");
1207}
Ted Kremenek0b6c1532008-04-08 20:22:12 +00001208
Ted Kremenek4f4b17d2008-04-03 20:08:18 +00001209$ENV{'CC'} = $Cmd;
Ted Kremenek51365b52009-12-15 02:35:54 +00001210$ENV{'CXX'} = $CmdCXX;
Ted Kremenekf22eacb2008-04-18 22:00:56 +00001211$ENV{'CLANG'} = $Clang;
Ted Kremenek51365b52009-12-15 02:35:54 +00001212$ENV{'CLANG_CXX'} = $ClangCXX;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001213
1214if ($Verbose >= 2) {
1215 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
1216}
1217
Ted Kremeneka9525c92008-05-12 22:07:14 +00001218if ($Verbose >= 3) {
1219 $ENV{'CCC_ANALYZER_LOG'} = 1;
1220}
1221
Ted Kremenek90125992008-07-15 23:41:32 +00001222if (scalar(@AnalysesToRun) == 0) {
1223 foreach my $key (keys %AnalysesDefaultEnabled) {
1224 push @AnalysesToRun,$key;
1225 }
Ted Kremenek01006782008-07-02 23:16:10 +00001226}
Ted Kremenek1262fc42008-05-14 20:10:33 +00001227
Ted Kremeneke15fa272008-10-13 21:46:42 +00001228if ($AnalyzeHeaders) {
1229 push @AnalysesToRun,"-analyzer-opt-analyze-headers";
1230}
1231
Ted Kremenek90125992008-07-15 23:41:32 +00001232$ENV{'CCC_ANALYZER_ANALYSIS'} = join ' ',@AnalysesToRun;
1233
Zhongxing Xu3cab2b12008-11-02 10:58:16 +00001234if (defined $StoreModel) {
Zhongxing Xu07c37672008-10-27 14:26:32 +00001235 $ENV{'CCC_ANALYZER_STORE_MODEL'} = $StoreModel;
1236}
1237
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +00001238if (defined $ConstraintsModel) {
1239 $ENV{'CCC_ANALYZER_CONSTRAINTS_MODEL'} = $ConstraintsModel;
1240}
1241
Ted Kremenekdb4f5f22008-11-04 00:02:53 +00001242if (defined $OutputFormat) {
1243 $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'} = $OutputFormat;
1244}
1245
1246
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001247# Run the build.
Ted Kremenek5656a982008-07-15 17:09:28 +00001248my $ExitStatus = RunBuildCommand(\@ARGV, $IgnoreErrors, $Cmd);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001249
Ted Kremenek7753b352009-07-27 22:10:34 +00001250if (defined $OutputFormat) {
Daniel Dunbar11825022009-07-29 16:21:23 +00001251 if ($OutputFormat =~ /plist/) {
1252 Diag "Analysis run complete.\n";
1253 Diag "Analysis results (plist files) deposited in '$HtmlDir'\n";
1254 }
1255 elsif ($OutputFormat =~ /html/) {
Ted Kremenek7753b352009-07-27 22:10:34 +00001256 # Postprocess the HTML directory.
1257 my $NumBugs = Postprocess($HtmlDir, $BaseDir);
Ted Kremenek5656a982008-07-15 17:09:28 +00001258
Ted Kremenek7753b352009-07-27 22:10:34 +00001259 if ($ViewResults and -r "$HtmlDir/index.html") {
1260 Diag "Analysis run complete.\n";
1261 Diag "Viewing analysis results in '$HtmlDir' using scan-view.\n";
1262 my $ScanView = Cwd::realpath("$RealBin/scan-view");
1263 if (! -x $ScanView) { $ScanView = "scan-view"; }
1264 exec $ScanView, "$HtmlDir";
1265 }
1266
1267 if ($ExitStatusFoundBugs) {
1268 exit 1 if ($NumBugs > 0);
1269 exit 0;
1270 }
Ted Kremenekdb4f5f22008-11-04 00:02:53 +00001271 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001272}
1273
Ted Kremenek5656a982008-07-15 17:09:28 +00001274exit $ExitStatus;
1275