blob: 4b097a72ecbaa80a604cc2eb8e7943323b08cf12 [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 Kremenek95aa1052008-09-04 17:52:41 +000029my $CXX; # Leave undefined initially.
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000030
Ted Kremenek0e689382008-09-11 18:17:51 +000031my $TERM = $ENV{'TERM'};
32my $UseColor = (defined $TERM and $TERM eq 'xterm-color' and -t STDOUT
33 and defined $ENV{'SCAN_BUILD_COLOR'});
Ted Kremenek23cfca32008-06-16 22:40:14 +000034
Ted Kremenek7cba1122008-09-22 01:35:58 +000035my $UserName = HtmlEscape(getpwuid($<) || 'unknown');
36my $HostName = HtmlEscape(hostname() || 'unknown');
37my $CurrentDir = HtmlEscape(getcwd());
38my $CurrentDirSuffix = basename($CurrentDir);
39
40my $CmdArgs;
41
42my $HtmlTitle;
43
44my $Date = localtime();
45
Ted Kremenekb7770c02008-07-15 17:06:13 +000046##----------------------------------------------------------------------------##
47# Diagnostics
48##----------------------------------------------------------------------------##
49
Ted Kremenek23cfca32008-06-16 22:40:14 +000050sub Diag {
51 if ($UseColor) {
52 print BOLD, MAGENTA "$Prog: @_";
53 print RESET;
54 }
55 else {
56 print "$Prog: @_";
57 }
58}
59
Ted Kremenek991c54b2008-08-08 20:46:42 +000060sub DiagCrashes {
61 my $Dir = shift;
Ted Kremenek938eef12009-02-17 23:31:05 +000062 Diag ("The analyzer encountered problems on some source files.\n");
63 Diag ("Preprocessed versions of these sources were deposited in '$Dir/failures'.\n");
Ted Kremenek991c54b2008-08-08 20:46:42 +000064 Diag ("Please consider submitting a bug report using these files:\n");
65 Diag (" http://clang.llvm.org/StaticAnalysisUsage.html#filingbugs\n")
66}
67
Ted Kremenek23cfca32008-06-16 22:40:14 +000068sub DieDiag {
69 if ($UseColor) {
70 print BOLD, RED "$Prog: ";
71 print RESET, RED @_;
72 print RESET;
73 }
74 else {
75 print "$Prog: ", @_;
76 }
77 exit(0);
78}
79
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000080##----------------------------------------------------------------------------##
Ted Kremenekb7770c02008-07-15 17:06:13 +000081# Some initial preprocessing of Clang options.
82##----------------------------------------------------------------------------##
83
Ted Kremenek833da022009-03-29 00:31:32 +000084# First, look for 'clang-cc' in libexec.
Ted Kremenekfd9df0e2009-05-09 19:19:28 +000085my $ClangCCSB = Cwd::realpath("$RealBin/libexec/clang-cc");
Ted Kremenek833da022009-03-29 00:31:32 +000086# Second, look for 'clang-cc' in the same directory as scan-build.
Ted Kremenekfd9df0e2009-05-09 19:19:28 +000087if (!defined $ClangCCSB || ! -x $ClangCCSB) {
88 $ClangCCSB = Cwd::realpath("$RealBin/clang-cc");
Ted Kremenek43b7bd32009-03-09 23:14:38 +000089}
Ted Kremenek833da022009-03-29 00:31:32 +000090# Third, look for 'clang-cc' in ../libexec
Ted Kremenekfd9df0e2009-05-09 19:19:28 +000091if (!defined $ClangCCSB || ! -x $ClangCCSB) {
92 $ClangCCSB = Cwd::realpath("$RealBin/../libexec/clang-cc");
93}
94# Finally, default to looking for 'clang-cc' in the path.
95if (!defined $ClangCCSB || ! -x $ClangCCSB) {
96 $ClangCCSB = "clang-cc";
97}
98my $ClangCC = $ClangCCSB;
99
100# Now find 'clang'
101my $ClangSB = Cwd::realpath("$RealBin/bin/clang");
Ted Kremenek8d10cdd2009-02-20 04:34:29 +0000102if (!defined $ClangSB || ! -x $ClangSB) {
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000103 $ClangSB = Cwd::realpath("$RealBin/clang");
104}
105# Third, look for 'clang' in ../bin
106if (!defined $ClangSB || ! -x $ClangSB) {
107 $ClangSB = Cwd::realpath("$RealBin/../bin/clang");
Ted Kremenekb7770c02008-07-15 17:06:13 +0000108}
Ted Kremenek833da022009-03-29 00:31:32 +0000109# Finally, default to looking for 'clang-cc' in the path.
110if (!defined $ClangSB || ! -x $ClangSB) {
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000111 $ClangSB = "clang";
Ted Kremenek833da022009-03-29 00:31:32 +0000112}
113my $Clang = $ClangSB;
Ted Kremenekb7770c02008-07-15 17:06:13 +0000114
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000115
Ted Kremenekb7770c02008-07-15 17:06:13 +0000116my %AvailableAnalyses;
117
118# Query clang for analysis options.
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000119open(PIPE, "-|", $ClangCC, "--help") or
120 DieDiag("Cannot execute '$ClangCC'\n");
Ted Kremenek63c20172008-08-04 17:34:06 +0000121
Ted Kremenekb7770c02008-07-15 17:06:13 +0000122my $FoundAnalysis = 0;
123
124while(<PIPE>) {
125 if ($FoundAnalysis == 0) {
Ted Kremenek938eef12009-02-17 23:31:05 +0000126 if (/Checks and Analyses/) {
Ted Kremenekb7770c02008-07-15 17:06:13 +0000127 $FoundAnalysis = 1;
128 }
Ted Kremenekb7770c02008-07-15 17:06:13 +0000129 next;
130 }
131
132 if (/^\s\s\s\s([^\s]+)\s(.+)$/) {
133 next if ($1 =~ /-dump/ or $1 =~ /-view/
134 or $1 =~ /-checker-simple/ or $1 =~ /-warn-uninit/);
135
136 $AvailableAnalyses{$1} = $2;
137 next;
Ted Kremenek938eef12009-02-17 23:31:05 +0000138 }
Ted Kremenekb7770c02008-07-15 17:06:13 +0000139 last;
140}
141
142close (PIPE);
143
144my %AnalysesDefaultEnabled = (
145 '-warn-dead-stores' => 1,
146 '-checker-cfref' => 1,
Ted Kremenek90125992008-07-15 23:41:32 +0000147 '-warn-objc-methodsigs' => 1,
Ted Kremenekd76c6a32009-02-25 21:08:30 +0000148 # Do not enable the missing -dealloc check by default.
149 # '-warn-objc-missing-dealloc' => 1,
Ted Kremenek5d443492008-09-18 06:34:16 +0000150 '-warn-objc-unused-ivars' => 1,
Ted Kremenek3a92d6d2009-07-24 02:52:07 +0000151 '-warn-security-syntactic' => 1
Ted Kremenekb7770c02008-07-15 17:06:13 +0000152);
153
154##----------------------------------------------------------------------------##
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000155# GetHTMLRunDir - Construct an HTML directory name for the current sub-run.
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000156##----------------------------------------------------------------------------##
157
Sam Bishopa0e22662008-04-02 03:35:43 +0000158sub GetHTMLRunDir {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000159
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000160 die "Not enough arguments." if (@_ == 0);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000161 my $Dir = shift @_;
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000162
163 my $TmpMode = 0;
164 if (!defined $Dir) {
Ted Kremenekffda0b42008-10-31 05:48:42 +0000165 if (`uname` =~ /Darwin/) {
166 $Dir = $ENV{'TMPDIR'};
167 if (!defined $Dir) { $Dir = "/tmp"; }
168 }
169 else {
170 $Dir = "/tmp";
171 }
172
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000173 $TmpMode = 1;
174 }
Ted Kremenekbf762c92009-02-24 02:38:02 +0000175
176 # Chop off any trailing '/' characters.
177 while ($Dir =~ /\/$/) { chop $Dir; }
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000178
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000179 # Get current date and time.
180
181 my @CurrentTime = localtime();
182
183 my $year = $CurrentTime[5] + 1900;
184 my $day = $CurrentTime[3];
185 my $month = $CurrentTime[4] + 1;
186
Ted Kremenek9d7405f2008-05-14 17:23:56 +0000187 my $DateString = sprintf("%d-%02d-%02d", $year, $month, $day);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000188
189 # Determine the run number.
190
191 my $RunNumber;
192
193 if (-d $Dir) {
194
195 if (! -r $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000196 DieDiag("directory '$Dir' exists but is not readable.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000197 }
198
199 # Iterate over all files in the specified directory.
200
201 my $max = 0;
202
203 opendir(DIR, $Dir);
Ted Kremenek29da6c52008-08-07 17:57:34 +0000204 my @FILES = grep { -d "$Dir/$_" } readdir(DIR);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000205 closedir(DIR);
206
207 foreach my $f (@FILES) {
208
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000209 # Strip the prefix '$Prog-' if we are dumping files to /tmp.
210 if ($TmpMode) {
211 next if (!($f =~ /^$Prog-(.+)/));
212 $f = $1;
213 }
214
Ted Kremenekebb74132008-09-21 06:58:09 +0000215
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000216 my @x = split/-/, $f;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000217 next if (scalar(@x) != 4);
218 next if ($x[0] != $year);
219 next if ($x[1] != $month);
220 next if ($x[2] != $day);
221
222 if ($x[3] > $max) {
223 $max = $x[3];
224 }
225 }
226
227 $RunNumber = $max + 1;
228 }
229 else {
230
231 if (-x $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000232 DieDiag("'$Dir' exists but is not a directory.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000233 }
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000234
235 if ($TmpMode) {
Ted Kremenek445fa772008-10-10 00:17:08 +0000236 DieDiag("The directory '/tmp' does not exist or cannot be accessed.\n");
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000237 }
238
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000239 # $Dir does not exist. It will be automatically created by the
240 # clang driver. Set the run number to 1.
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000241
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000242 $RunNumber = 1;
243 }
244
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000245 die "RunNumber must be defined!" if (!defined $RunNumber);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000246
247 # Append the run number.
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000248 my $NewDir;
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000249 if ($TmpMode) {
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000250 $NewDir = "$Dir/$Prog-$DateString-$RunNumber";
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000251 }
252 else {
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000253 $NewDir = "$Dir/$DateString-$RunNumber";
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000254 }
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000255 system 'mkdir','-p',$NewDir;
256 return $NewDir;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000257}
258
Sam Bishopa0e22662008-04-02 03:35:43 +0000259sub SetHtmlEnv {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000260
261 die "Wrong number of arguments." if (scalar(@_) != 2);
262
263 my $Args = shift;
264 my $Dir = shift;
265
266 die "No build command." if (scalar(@$Args) == 0);
267
268 my $Cmd = $$Args[0];
269
270 if ($Cmd =~ /configure/) {
271 return;
272 }
273
274 if ($Verbose) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000275 Diag("Emitting reports for this run to '$Dir'.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000276 }
277
278 $ENV{'CCC_ANALYZER_HTML'} = $Dir;
279}
280
281##----------------------------------------------------------------------------##
Ted Kremenek57cf4462008-04-18 15:09:30 +0000282# ComputeDigest - Compute a digest of the specified file.
283##----------------------------------------------------------------------------##
284
285sub ComputeDigest {
286 my $FName = shift;
Ted Kremenek23cfca32008-06-16 22:40:14 +0000287 DieDiag("Cannot read $FName to compute Digest.\n") if (! -r $FName);
Ted Kremeneka6e24812008-04-19 18:05:48 +0000288
289 # Use Digest::MD5. We don't have to be cryptographically secure. We're
Ted Kremenek7ea02e62008-04-19 18:07:44 +0000290 # just looking for duplicate files that come from a non-malicious source.
291 # We use Digest::MD5 because it is a standard Perl module that should
Ted Kremenek63c20172008-08-04 17:34:06 +0000292 # come bundled on most systems.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000293 open(FILE, $FName) or DieDiag("Cannot open $FName when computing Digest.\n");
Ted Kremeneka6e24812008-04-19 18:05:48 +0000294 binmode FILE;
295 my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest;
296 close(FILE);
297
Ted Kremenek63c20172008-08-04 17:34:06 +0000298 # Return the digest.
Ted Kremeneka6e24812008-04-19 18:05:48 +0000299 return $Result;
Ted Kremenek57cf4462008-04-18 15:09:30 +0000300}
301
302##----------------------------------------------------------------------------##
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000303# UpdatePrefix - Compute the common prefix of files.
304##----------------------------------------------------------------------------##
305
306my $Prefix;
307
308sub UpdatePrefix {
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000309 my $x = shift;
310 my $y = basename($x);
311 $x =~ s/\Q$y\E$//;
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000312
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000313 if (!defined $Prefix) {
314 $Prefix = $x;
315 return;
316 }
317
Ted Kremenek20b2bae2008-09-11 21:15:10 +0000318 chop $Prefix while (!($x =~ /^\Q$Prefix/));
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000319}
320
321sub GetPrefix {
322 return $Prefix;
323}
324
325##----------------------------------------------------------------------------##
326# UpdateInFilePath - Update the path in the report file.
327##----------------------------------------------------------------------------##
328
329sub UpdateInFilePath {
330 my $fname = shift;
331 my $regex = shift;
332 my $newtext = shift;
Ted Kremenek63c20172008-08-04 17:34:06 +0000333
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000334 open (RIN, $fname) or die "cannot open $fname";
Ted Kremenek63c20172008-08-04 17:34:06 +0000335 open (ROUT, ">", "$fname.tmp") or die "cannot open $fname.tmp";
336
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000337 while (<RIN>) {
338 s/$regex/$newtext/;
339 print ROUT $_;
340 }
Ted Kremenek63c20172008-08-04 17:34:06 +0000341
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000342 close (ROUT);
343 close (RIN);
Ted Kremenek20161e92008-07-15 20:18:21 +0000344 system("mv", "$fname.tmp", $fname);
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000345}
346
347##----------------------------------------------------------------------------##
Ted Kremenek5744dc22008-04-02 18:03:36 +0000348# ScanFile - Scan a report file for various identifying attributes.
349##----------------------------------------------------------------------------##
350
Ted Kremenek57cf4462008-04-18 15:09:30 +0000351# Sometimes a source file is scanned more than once, and thus produces
352# multiple error reports. We use a cache to solve this problem.
353
354my %AlreadyScanned;
355
Ted Kremenek5744dc22008-04-02 18:03:36 +0000356sub ScanFile {
357
358 my $Index = shift;
359 my $Dir = shift;
360 my $FName = shift;
361
Ted Kremenek57cf4462008-04-18 15:09:30 +0000362 # Compute a digest for the report file. Determine if we have already
363 # scanned a file that looks just like it.
364
365 my $digest = ComputeDigest("$Dir/$FName");
366
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000367 if (defined $AlreadyScanned{$digest}) {
Ted Kremenek57cf4462008-04-18 15:09:30 +0000368 # Redundant file. Remove it.
Ted Kremenek20161e92008-07-15 20:18:21 +0000369 system ("rm", "-f", "$Dir/$FName");
Ted Kremenek57cf4462008-04-18 15:09:30 +0000370 return;
371 }
372
373 $AlreadyScanned{$digest} = 1;
374
Ted Kremenek809709f2008-04-18 16:58:34 +0000375 # At this point the report file is not world readable. Make it happen.
Ted Kremenek20161e92008-07-15 20:18:21 +0000376 system ("chmod", "644", "$Dir/$FName");
Ted Kremenek684bb092008-04-18 15:18:20 +0000377
378 # Scan the report file for tags.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000379 open(IN, "$Dir/$FName") or DieDiag("Cannot open '$Dir/$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000380
Ted Kremeneka26ddab2009-01-27 01:53:39 +0000381 my $BugType = "";
Ted Kremenek22d6a632008-04-02 20:43:36 +0000382 my $BugFile = "";
Ted Kremenekebb74132008-09-21 06:58:09 +0000383 my $BugCategory;
Ted Kremenek22d6a632008-04-02 20:43:36 +0000384 my $BugPathLength = 1;
385 my $BugLine = 0;
Ted Kremenekebb74132008-09-21 06:58:09 +0000386
Ted Kremenek5744dc22008-04-02 18:03:36 +0000387 while (<IN>) {
Ted Kremenekd658e672009-08-03 23:45:27 +0000388 last if (/<!-- BUGMETAEND -->/);
Ted Kremenekebb74132008-09-21 06:58:09 +0000389
Ted Kremeneka26ddab2009-01-27 01:53:39 +0000390 if (/<!-- BUGTYPE (.*) -->$/) {
391 $BugType = $1;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000392 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000393 elsif (/<!-- BUGFILE (.*) -->$/) {
Ted Kremenek990c2f42008-12-03 19:19:23 +0000394 $BugFile = abs_path($1);
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000395 UpdatePrefix($BugFile);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000396 }
397 elsif (/<!-- BUGPATHLENGTH (.*) -->$/) {
398 $BugPathLength = $1;
399 }
400 elsif (/<!-- BUGLINE (.*) -->$/) {
401 $BugLine = $1;
Ted Kremenekebb74132008-09-21 06:58:09 +0000402 }
403 elsif (/<!-- BUGCATEGORY (.*) -->$/) {
404 $BugCategory = $1;
Ted Kremenek22d6a632008-04-02 20:43:36 +0000405 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000406 }
407
408 close(IN);
Ted Kremenekebb74132008-09-21 06:58:09 +0000409
410 if (!defined $BugCategory) {
411 $BugCategory = "Other";
412 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000413
Ted Kremeneka26ddab2009-01-27 01:53:39 +0000414 push @$Index,[ $FName, $BugCategory, $BugType, $BugFile, $BugLine,
Ted Kremenek81983112008-09-28 04:13:09 +0000415 $BugPathLength ];
Ted Kremenek22d6a632008-04-02 20:43:36 +0000416}
417
418##----------------------------------------------------------------------------##
Ted Kremenek3ce12072008-09-22 17:50:47 +0000419# CopyFiles - Copy resource files to target directory.
Ted Kremenek22d6a632008-04-02 20:43:36 +0000420##----------------------------------------------------------------------------##
421
Ted Kremenek3ce12072008-09-22 17:50:47 +0000422sub CopyFiles {
Ted Kremenek22d6a632008-04-02 20:43:36 +0000423
424 my $Dir = shift;
Ted Kremeneke15fa272008-10-13 21:46:42 +0000425
426 my $JS = Cwd::realpath("$RealBin/sorttable.js");
Ted Kremenek22d6a632008-04-02 20:43:36 +0000427
Ted Kremenek23cfca32008-06-16 22:40:14 +0000428 DieDiag("Cannot find 'sorttable.js'.\n")
Ted Kremeneke15fa272008-10-13 21:46:42 +0000429 if (! -r $JS);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000430
Ted Kremeneke15fa272008-10-13 21:46:42 +0000431 system ("cp", $JS, "$Dir");
Ted Kremenek22d6a632008-04-02 20:43:36 +0000432
Ted Kremenek23cfca32008-06-16 22:40:14 +0000433 DieDiag("Could not copy 'sorttable.js' to '$Dir'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000434 if (! -r "$Dir/sorttable.js");
Ted Kremenek3ce12072008-09-22 17:50:47 +0000435
Ted Kremeneke15fa272008-10-13 21:46:42 +0000436 my $CSS = Cwd::realpath("$RealBin/scanview.css");
437
Ted Kremenek3ce12072008-09-22 17:50:47 +0000438 DieDiag("Cannot find 'scanview.css'.\n")
Ted Kremeneke15fa272008-10-13 21:46:42 +0000439 if (! -r $CSS);
Ted Kremenek3ce12072008-09-22 17:50:47 +0000440
Ted Kremeneke15fa272008-10-13 21:46:42 +0000441 system ("cp", $CSS, "$Dir");
Ted Kremenek3ce12072008-09-22 17:50:47 +0000442
443 DieDiag("Could not copy 'scanview.css' to '$Dir'.\n")
Ted Kremeneke15fa272008-10-13 21:46:42 +0000444 if (! -r $CSS);
Ted Kremenek5744dc22008-04-02 18:03:36 +0000445}
446
447##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000448# Postprocess - Postprocess the results of an analysis scan.
449##----------------------------------------------------------------------------##
450
Sam Bishopa0e22662008-04-02 03:35:43 +0000451sub Postprocess {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000452
453 my $Dir = shift;
Ted Kremenek684bb092008-04-18 15:18:20 +0000454 my $BaseDir = shift;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000455
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000456 die "No directory specified." if (!defined $Dir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000457
458 if (! -d $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000459 Diag("No bugs found.\n");
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000460 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000461 }
462
463 opendir(DIR, $Dir);
Ted Kremenek938eef12009-02-17 23:31:05 +0000464 my @files = grep { /^report-.*\.html$/ } readdir(DIR);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000465 closedir(DIR);
466
Ted Kremenek938eef12009-02-17 23:31:05 +0000467 if (scalar(@files) == 0 and ! -e "$Dir/failures") {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000468 Diag("Removing directory '$Dir' because it contains no reports.\n");
Ted Kremenek20161e92008-07-15 20:18:21 +0000469 system ("rm", "-fR", $Dir);
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000470 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000471 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000472
Ted Kremenek991c54b2008-08-08 20:46:42 +0000473 # Scan each report file and build an index.
474 my @Index;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000475 foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); }
476
Ted Kremenek938eef12009-02-17 23:31:05 +0000477 # Scan the failures directory and use the information in the .info files
Ted Kremenekd52e4252008-08-25 20:45:07 +0000478 # to update the common prefix directory.
Ted Kremenek938eef12009-02-17 23:31:05 +0000479 my @failures;
480 my @attributes_ignored;
481 if (-d "$Dir/failures") {
482 opendir(DIR, "$Dir/failures");
483 @failures = grep { /[.]info.txt$/ && !/attribute_ignored/; } readdir(DIR);
Ted Kremenekd52e4252008-08-25 20:45:07 +0000484 closedir(DIR);
Ted Kremenek938eef12009-02-17 23:31:05 +0000485 opendir(DIR, "$Dir/failures");
486 @attributes_ignored = grep { /^attribute_ignored/; } readdir(DIR);
487 closedir(DIR);
488 foreach my $file (@failures) {
489 open IN, "$Dir/failures/$file" or DieDiag("cannot open $file\n");
Ted Kremenekd52e4252008-08-25 20:45:07 +0000490 my $Path = <IN>;
491 if (defined $Path) { UpdatePrefix($Path); }
492 close IN;
493 }
494 }
495
Ted Kremenek63c20172008-08-04 17:34:06 +0000496 # Generate an index.html file.
497 my $FName = "$Dir/index.html";
498 open(OUT, ">", $FName) or DieDiag("Cannot create file '$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000499
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000500 # Print out the header.
501
Ted Kremenek5744dc22008-04-02 18:03:36 +0000502print OUT <<ENDTEXT;
503<html>
504<head>
Ted Kremenek7cba1122008-09-22 01:35:58 +0000505<title>${HtmlTitle}</title>
Ted Kremenekf1435452008-09-23 22:34:51 +0000506<link type="text/css" rel="stylesheet" href="scanview.css"/>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000507<script src="sorttable.js"></script>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000508<script language='javascript' type="text/javascript">
509function SetDisplay(RowClass, DisplayVal)
510{
511 var Rows = document.getElementsByTagName("tr");
512 for ( var i = 0 ; i < Rows.length; ++i ) {
513 if (Rows[i].className == RowClass) {
514 Rows[i].style.display = DisplayVal;
515 }
516 }
517}
Ted Kremenekebb74132008-09-21 06:58:09 +0000518
Ted Kremenek2350a462008-10-28 19:56:52 +0000519function CopyCheckedStateToCheckButtons(SummaryCheckButton) {
520 var Inputs = document.getElementsByTagName("input");
521 for ( var i = 0 ; i < Inputs.length; ++i ) {
522 if (Inputs[i].type == "checkbox") {
523 if(Inputs[i] != SummaryCheckButton) {
524 Inputs[i].checked = SummaryCheckButton.checked;
525 Inputs[i].onclick();
526 }
527 }
528 }
529}
530
Ted Kremenek999e1202008-10-28 20:09:57 +0000531function returnObjById( id ) {
532 if (document.getElementById)
533 var returnVar = document.getElementById(id);
534 else if (document.all)
535 var returnVar = document.all[id];
536 else if (document.layers)
537 var returnVar = document.layers[id];
538 return returnVar;
539}
540
541var NumUnchecked = 0;
542
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000543function ToggleDisplay(CheckButton, ClassName) {
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000544 if (CheckButton.checked) {
545 SetDisplay(ClassName, "");
Ted Kremenek999e1202008-10-28 20:09:57 +0000546 if (--NumUnchecked == 0) {
547 returnObjById("AllBugsCheck").checked = true;
548 }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000549 }
550 else {
551 SetDisplay(ClassName, "none");
Ted Kremenek999e1202008-10-28 20:09:57 +0000552 NumUnchecked++;
553 returnObjById("AllBugsCheck").checked = false;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000554 }
555}
556</script>
Ted Kremenek1d1abb12008-09-22 17:52:58 +0000557<!-- SUMMARYENDHEAD -->
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000558</head>
559<body>
Ted Kremenek7cba1122008-09-22 01:35:58 +0000560<h1>${HtmlTitle}</h1>
561
562<table>
563<tr><th>User:</th><td>${UserName}\@${HostName}</td></tr>
564<tr><th>Working Directory:</th><td>${CurrentDir}</td></tr>
565<tr><th>Command Line:</th><td>${CmdArgs}</td></tr>
566<tr><th>Date:</th><td>${Date}</td></tr>
567ENDTEXT
568
569print OUT "<tr><th>Version:</th><td>${BuildName} (${BuildDate})</td></tr>\n"
570 if (defined($BuildName) && defined($BuildDate));
571
572print OUT <<ENDTEXT;
573</table>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000574ENDTEXT
575
Ted Kremenek991c54b2008-08-08 20:46:42 +0000576 if (scalar(@files)) {
577 # Print out the summary table.
578 my %Totals;
Ted Kremenekebb74132008-09-21 06:58:09 +0000579
Ted Kremenek991c54b2008-08-08 20:46:42 +0000580 for my $row ( @Index ) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000581 my $bug_type = ($row->[2]);
582 my $bug_category = ($row->[1]);
583 my $key = "$bug_category:$bug_type";
584
585 if (!defined $Totals{$key}) { $Totals{$key} = [1,$bug_category,$bug_type]; }
586 else { $Totals{$key}->[0]++; }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000587 }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000588
Ted Kremenek7cba1122008-09-22 01:35:58 +0000589 print OUT "<h2>Bug Summary</h2>";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000590
591 if (defined $BuildName) {
592 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 +0000593 }
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000594
Ted Kremenek2350a462008-10-28 19:56:52 +0000595 my $TotalBugs = scalar(@Index);
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000596print OUT <<ENDTEXT;
Ted Kremenekebb74132008-09-21 06:58:09 +0000597<table>
598<thead><tr><td>Bug Type</td><td>Quantity</td><td class="sorttable_nosort">Display?</td></tr></thead>
Ted Kremenek999e1202008-10-28 20:09:57 +0000599<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 +0000600ENDTEXT
601
Ted Kremenekebb74132008-09-21 06:58:09 +0000602 my $last_category;
603
604 for my $key (
605 sort {
606 my $x = $Totals{$a};
607 my $y = $Totals{$b};
608 my $res = $x->[1] cmp $y->[1];
609 $res = $x->[2] cmp $y->[2] if ($res == 0);
610 $res
611 } keys %Totals )
612 {
613 my $val = $Totals{$key};
614 my $category = $val->[1];
615 if (!defined $last_category or $last_category ne $category) {
616 $last_category = $category;
617 print OUT "<tr><th>$category</th><th colspan=2></th></tr>\n";
618 }
619 my $x = lc $key;
620 $x =~ s/[ ,'":\/()]+/_/g;
621 print OUT "<tr><td class=\"SUMM_DESC\">";
622 print OUT $val->[2];
Ted Kremenek2350a462008-10-28 19:56:52 +0000623 print OUT "</td><td class=\"Q\">";
Ted Kremenekebb74132008-09-21 06:58:09 +0000624 print OUT $val->[0];
625 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 +0000626 }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000627
628 # Print out the table of errors.
629
630print OUT <<ENDTEXT;
631</table>
Ted Kremenek7cba1122008-09-22 01:35:58 +0000632<h2>Reports</h2>
Ted Kremenekebb74132008-09-21 06:58:09 +0000633
634<table class="sortable" style="table-layout:automatic">
635<thead><tr>
636 <td>Bug Group</td>
637 <td class="sorttable_sorted">Bug Type<span id="sorttable_sortfwdind">&nbsp;&#x25BE;</span></td>
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000638 <td>File</td>
Ted Kremenekebb74132008-09-21 06:58:09 +0000639 <td class="Q">Line</td>
Ted Kremenek81983112008-09-28 04:13:09 +0000640 <td class="Q">Path Length</td>
Ted Kremenek2645c772008-07-07 16:58:44 +0000641 <td class="sorttable_nosort"></td>
Ted Kremenekebb74132008-09-21 06:58:09 +0000642 <!-- REPORTBUGCOL -->
643</tr></thead>
644<tbody>
Ted Kremenek5744dc22008-04-02 18:03:36 +0000645ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000646
Ted Kremenek991c54b2008-08-08 20:46:42 +0000647 my $prefix = GetPrefix();
648 my $regex;
649 my $InFileRegex;
650 my $InFilePrefix = "File:</td><td>";
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000651
Ted Kremenek991c54b2008-08-08 20:46:42 +0000652 if (defined $prefix) {
653 $regex = qr/^\Q$prefix\E/is;
654 $InFileRegex = qr/\Q$InFilePrefix$prefix\E/is;
655 }
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000656
Ted Kremenekebb74132008-09-21 06:58:09 +0000657 for my $row ( sort { $a->[2] cmp $b->[2] } @Index ) {
658 my $x = "$row->[1]:$row->[2]";
659 $x = lc $x;
660 $x =~ s/[ ,'":\/()]+/_/g;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000661
Ted Kremenek991c54b2008-08-08 20:46:42 +0000662 my $ReportFile = $row->[0];
Ted Kremenekebb74132008-09-21 06:58:09 +0000663
664 print OUT "<tr class=\"bt_$x\">";
665 print OUT "<td class=\"DESC\">";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000666 print OUT $row->[1];
Ted Kremenekebb74132008-09-21 06:58:09 +0000667 print OUT "</td>";
668 print OUT "<td class=\"DESC\">";
669 print OUT $row->[2];
670 print OUT "</td>";
671
672 # Update the file prefix.
673 my $fname = $row->[3];
Ted Kremenekebb74132008-09-21 06:58:09 +0000674
Ted Kremenek991c54b2008-08-08 20:46:42 +0000675 if (defined $regex) {
676 $fname =~ s/$regex//;
677 UpdateInFilePath("$Dir/$ReportFile", $InFileRegex, $InFilePrefix)
678 }
Ted Kremenekebb74132008-09-21 06:58:09 +0000679
Ted Kremenek91639ef2008-09-22 17:42:31 +0000680 print OUT "<td>";
Ted Kremenekebb74132008-09-21 06:58:09 +0000681 my @fname = split /\//,$fname;
682 if ($#fname > 0) {
683 while ($#fname >= 0) {
684 my $x = shift @fname;
685 print OUT $x;
686 if ($#fname >= 0) {
687 print OUT "<span class=\"W\"> </span>/";
688 }
689 }
690 }
691 else {
692 print OUT $fname;
Ted Kremenek91639ef2008-09-22 17:42:31 +0000693 }
Ted Kremenekebb74132008-09-21 06:58:09 +0000694 print OUT "</td>";
695
696 # Print out the quantities.
Ted Kremenek81983112008-09-28 04:13:09 +0000697 for my $j ( 4 .. 5 ) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000698 print OUT "<td class=\"Q\">$row->[$j]</td>";
699 }
700
Ted Kremenek991c54b2008-08-08 20:46:42 +0000701 # Print the rest of the columns.
Ted Kremenek81983112008-09-28 04:13:09 +0000702 for (my $j = 6; $j <= $#{$row}; ++$j) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000703 print OUT "<td>$row->[$j]</td>"
Ted Kremenek991c54b2008-08-08 20:46:42 +0000704 }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000705
Ted Kremenek991c54b2008-08-08 20:46:42 +0000706 # Emit the "View" link.
Ted Kremenek68005dd2008-09-22 17:39:18 +0000707 print OUT "<td><a href=\"$ReportFile#EndPath\">View Report</a></td>";
Ted Kremenek3cea9ee2008-07-30 17:58:08 +0000708
Daniel Dunbare43038e2008-09-19 23:18:44 +0000709 # Emit REPORTBUG markers.
Ted Kremenekebb74132008-09-21 06:58:09 +0000710 print OUT "\n<!-- REPORTBUG id=\"$ReportFile\" -->\n";
Daniel Dunbare43038e2008-09-19 23:18:44 +0000711
Ted Kremenek991c54b2008-08-08 20:46:42 +0000712 # End the row.
713 print OUT "</tr>\n";
714 }
715
Ted Kremenekebb74132008-09-21 06:58:09 +0000716 print OUT "</tbody>\n</table>\n\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000717 }
718
Ted Kremenek938eef12009-02-17 23:31:05 +0000719 if (scalar (@failures) || scalar(@attributes_ignored)) {
720 print OUT "<h2>Analyzer Failures</h2>\n";
721
722 if (scalar @attributes_ignored) {
723 print OUT "The analyzer's parser ignored the following attributes:<p>\n";
724 print OUT "<table>\n";
725 print OUT "<thead><tr><td>Attribute</td><td>Source File</td><td>Preprocessed File</td><td>STDERR Output</td></tr></thead>\n";
726 foreach my $file (sort @attributes_ignored) {
727 die "cannot demangle attribute name\n" if (! ($file =~ /^attribute_ignored_(.+).txt/));
728 my $attribute = $1;
729 # Open the attribute file to get the first file that failed.
730 next if (!open (ATTR, "$Dir/failures/$file"));
731 my $ppfile = <ATTR>;
732 chomp $ppfile;
733 close ATTR;
734 next if (! -e "$Dir/failures/$ppfile");
735 # Open the info file and get the name of the source file.
736 open (INFO, "$Dir/failures/$ppfile.info.txt") or
737 die "Cannot open $Dir/failures/$ppfile.info.txt\n";
738 my $srcfile = <INFO>;
739 chomp $srcfile;
740 close (INFO);
741 # Print the information in the table.
742 my $prefix = GetPrefix();
743 if (defined $prefix) { $srcfile =~ s/^\Q$prefix//; }
744 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";
745 my $ppfile_clang = $ppfile;
746 $ppfile_clang =~ s/[.](.+)$/.clang.$1/;
747 print OUT " <!-- REPORTPROBLEM src=\"$srcfile\" file=\"failures/$ppfile\" clangfile=\"failures/$ppfile_clang\" stderr=\"failures/$ppfile.stderr.txt\" info=\"failures/$ppfile.info.txt\" -->\n";
748 }
749 print OUT "</table>\n";
750 }
751
752 if (scalar @failures) {
753 print OUT "<p>The analyzer had problems processing the following files:</p>\n";
754 print OUT "<table>\n";
755 print OUT "<thead><tr><td>Problem</td><td>Source File</td><td>Preprocessed File</td><td>STDERR Output</td></tr></thead>\n";
756 foreach my $file (sort @failures) {
Ted Kremenek82a12532008-09-25 00:25:16 +0000757 $file =~ /(.+).info.txt$/;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000758 # Get the preprocessed file.
759 my $ppfile = $1;
760 # Open the info file and get the name of the source file.
Ted Kremenek938eef12009-02-17 23:31:05 +0000761 open (INFO, "$Dir/failures/$file") or
762 die "Cannot open $Dir/failures/$file\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000763 my $srcfile = <INFO>;
Ted Kremenek5d31f832008-08-18 18:38:29 +0000764 chomp $srcfile;
765 my $problem = <INFO>;
766 chomp $problem;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000767 close (INFO);
768 # Print the information in the table.
Ted Kremenekd52e4252008-08-25 20:45:07 +0000769 my $prefix = GetPrefix();
Ted Kremenek9f9b1fd2008-09-12 22:49:36 +0000770 if (defined $prefix) { $srcfile =~ s/^\Q$prefix//; }
Ted Kremenek938eef12009-02-17 23:31:05 +0000771 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 +0000772 my $ppfile_clang = $ppfile;
773 $ppfile_clang =~ s/[.](.+)$/.clang.$1/;
Ted Kremenek938eef12009-02-17 23:31:05 +0000774 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 +0000775 }
Ted Kremenek938eef12009-02-17 23:31:05 +0000776 print OUT "</table>\n";
777 }
778 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 +0000779 }
780
Ted Kremenek991c54b2008-08-08 20:46:42 +0000781 print OUT "</body></html>\n";
Ted Kremenek5744dc22008-04-02 18:03:36 +0000782 close(OUT);
Ted Kremenek3ce12072008-09-22 17:50:47 +0000783 CopyFiles($Dir);
Ted Kremenek20161e92008-07-15 20:18:21 +0000784
785 # Make sure $Dir and $BaseDir are world readable/executable.
786 system("chmod", "755", $Dir);
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000787 if (defined $BaseDir) { system("chmod", "755", $BaseDir); }
Ted Kremenek20161e92008-07-15 20:18:21 +0000788
Ted Kremenek23cfca32008-06-16 22:40:14 +0000789 my $Num = scalar(@Index);
Ted Kremenek150c2122008-07-11 19:15:05 +0000790 Diag("$Num bugs found.\n");
791 if ($Num > 0 && -r "$Dir/index.html") {
Ted Kremenek5950b3f2008-09-22 06:47:01 +0000792 Diag("Run 'scan-view $Dir' to examine bug reports.\n");
Ted Kremenek150c2122008-07-11 19:15:05 +0000793 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000794
Ted Kremenek938eef12009-02-17 23:31:05 +0000795 DiagCrashes($Dir) if (scalar @failures || scalar @attributes_ignored);
Ted Kremenek991c54b2008-08-08 20:46:42 +0000796
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000797 return $Num;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000798}
799
800##----------------------------------------------------------------------------##
Ted Kremenekdab11102008-04-02 04:43:42 +0000801# RunBuildCommand - Run the build command.
802##----------------------------------------------------------------------------##
803
Ted Kremenek6b628982008-04-30 23:47:12 +0000804sub AddIfNotPresent {
805 my $Args = shift;
806 my $Arg = shift;
807 my $found = 0;
808
809 foreach my $k (@$Args) {
810 if ($k eq $Arg) {
811 $found = 1;
812 last;
813 }
814 }
815
816 if ($found == 0) {
817 push @$Args, $Arg;
818 }
819}
820
Ted Kremenekdab11102008-04-02 04:43:42 +0000821sub RunBuildCommand {
822
823 my $Args = shift;
Ted Kremenek7442ca62008-04-02 16:04:51 +0000824 my $IgnoreErrors = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000825 my $Cmd = $Args->[0];
Ted Kremenek6195c372008-06-02 21:52:47 +0000826 my $CCAnalyzer = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000827
Ted Kremenek3301cb12008-06-30 18:18:16 +0000828 # Get only the part of the command after the last '/'.
829 if ($Cmd =~ /\/([^\/]+)$/) {
830 $Cmd = $1;
831 }
832
Ted Kremenek92548fe2008-11-19 01:46:21 +0000833 if ($Cmd =~ /(.*\/?gcc[^\/]*$)/ or
834 $Cmd =~ /(.*\/?cc[^\/]*$)/ or
835 $Cmd =~ /(.*\/?llvm-gcc[^\/]*$)/ or
836 $Cmd =~ /(.*\/?ccc-analyzer[^\/]*$)/) {
837
838 if (!($Cmd =~ /ccc-analyzer/) and !defined $ENV{"CCC_CC"}) {
839 $ENV{"CCC_CC"} = $1;
840 }
841
Ted Kremenekdab11102008-04-02 04:43:42 +0000842 shift @$Args;
Ted Kremenek6195c372008-06-02 21:52:47 +0000843 unshift @$Args, $CCAnalyzer;
Ted Kremenekdab11102008-04-02 04:43:42 +0000844 }
Ted Kremenek7442ca62008-04-02 16:04:51 +0000845 elsif ($IgnoreErrors) {
846 if ($Cmd eq "make" or $Cmd eq "gmake") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000847 AddIfNotPresent($Args,"-k");
Ted Kremenek8912b542008-05-13 21:28:02 +0000848 AddIfNotPresent($Args,"-i");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000849 }
850 elsif ($Cmd eq "xcodebuild") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000851 AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000852 }
Ted Kremenek6b628982008-04-30 23:47:12 +0000853 }
854
Ted Kremenek6b628982008-04-30 23:47:12 +0000855 if ($Cmd eq "xcodebuild") {
Ted Kremenek87752b22009-05-15 21:14:16 +0000856 # Check if using iPhone SDK 3.0 (simulator). If so the compiler being
857 # used should be gcc-4.2.
858 if (!defined $ENV{"CCC_CC"}) {
859 for (my $i = 0 ; $i < scalar(@$Args); ++$i) {
860 if ($Args->[$i] eq "-sdk" && $i + 1 < scalar(@$Args)) {
861 if (@$Args[$i+1] =~ /^iphonesimulator3/) {
862 $ENV{"CCC_CC"} = "gcc-4.2";
863 }
864 }
865 }
866 }
867
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000868 # Disable distributed builds for xcodebuild.
Ted Kremenek6b628982008-04-30 23:47:12 +0000869 AddIfNotPresent($Args,"-nodistribute");
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000870
871 # Disable PCH files until clang supports them.
872 AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO");
Ted Kremenek915e9722008-05-27 23:18:07 +0000873
874 # When 'CC' is set, xcodebuild uses it to do all linking, even if we are
875 # linking C++ object files. Set 'LDPLUSPLUS' so that xcodebuild uses 'g++'
876 # when linking such files.
Ted Kremenek95aa1052008-09-04 17:52:41 +0000877 die if (!defined $CXX);
878 my $LDPLUSPLUS = `which $CXX`;
Ted Kremenek915e9722008-05-27 23:18:07 +0000879 $LDPLUSPLUS =~ s/\015?\012//; # strip newlines
880 $ENV{'LDPLUSPLUS'} = $LDPLUSPLUS;
Ted Kremenek6b628982008-04-30 23:47:12 +0000881 }
Ted Kremenekdab11102008-04-02 04:43:42 +0000882
Ted Kremenek5a4ddaf2008-08-25 20:10:45 +0000883 return (system(@$Args) >> 8);
Ted Kremenekdab11102008-04-02 04:43:42 +0000884}
885
Ted Kremenekdab11102008-04-02 04:43:42 +0000886##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000887# DisplayHelp - Utility function to display all help options.
888##----------------------------------------------------------------------------##
889
Sam Bishopa0e22662008-04-02 03:35:43 +0000890sub DisplayHelp {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000891
Ted Kremenek5744dc22008-04-02 18:03:36 +0000892print <<ENDTEXT;
Sam Bishopa0e22662008-04-02 03:35:43 +0000893USAGE: $Prog [options] <build command> [build options]
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000894
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000895ENDTEXT
896
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000897 if (defined $BuildName) {
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000898 print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n";
899 }
900
901print <<ENDTEXT;
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000902OPTIONS:
903
Ted Kremeneke15fa272008-10-13 21:46:42 +0000904 -analyze-headers - Also analyze functions in #included files.
905
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000906 -o - Target directory for HTML report files. Subdirectories
Sam Bishopa0e22662008-04-02 03:35:43 +0000907 will be created as needed to represent separate "runs" of
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000908 the analyzer. If this option is not specified, a directory
Ted Kremenekffda0b42008-10-31 05:48:42 +0000909 is created in /tmp (TMPDIR on Mac OS X) to store the reports.
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000910
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000911 -h - Display this message.
912 --help
Ted Kremenek1262fc42008-05-14 20:10:33 +0000913
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000914 -k - Add a "keep on going" option to the specified build command.
915 --keep-going This option currently supports make and xcodebuild.
Ted Kremenekf02e8db2008-04-02 16:41:25 +0000916 This is a convenience option; one can specify this
917 behavior directly using build options.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000918
Ted Kremenek7cba1122008-09-22 01:35:58 +0000919 --html-title [title] - Specify the title used on generated HTML pages.
920 --html-title=[title] If not specified, a default title will be used.
921
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000922 -plist - By default the output of scan-build is a set of HTML files.
923 This option outputs the results as a set of .plist files.
924
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000925 --status-bugs - By default, the exit status of $Prog is the same as the
926 executed build command. Specifying this option causes the
927 exit status of $Prog to be 1 if it found potential bugs
928 and 0 otherwise.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000929
Ted Kremenek386c6932008-09-03 17:59:35 +0000930 --use-cc [compiler path] - By default, $Prog uses 'gcc' to compile and link
931 --use-cc=[compiler path] your C and Objective-C code. Use this option
932 to specify an alternate compiler.
933
934 --use-c++ [compiler path] - By default, $Prog uses 'g++' to compile and link
935 --use-c++=[compiler path] your C++ and Objective-C++ code. Use this option
936 to specify an alternate compiler.
Ted Kremenekf17ef3c2008-08-21 21:47:09 +0000937
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000938 -v - Verbose output from $Prog and the analyzer.
Ted Kremenek386c6932008-09-03 17:59:35 +0000939 A second and third '-v' increases verbosity.
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000940
941 -V - View analysis results in a web browser when the build
942 --view completes.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000943
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000944ADVANCED OPTIONS:
945
Ted Kremenek9f4ecb32009-02-20 21:49:22 +0000946 -constraints [model] - Specify the contraint engine used by the analyzer.
947 By default the 'range' model is used. Specifying
948 'basic' uses a simpler, less powerful constraint model
Ted Kremenekd4c76842009-02-21 04:46:41 +0000949 used by checker-0.160 and earlier.
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000950
951 -store [model] - Specify the store model used by the analyzer. By default,
952 the 'basic' store model is used. 'region' specifies a field-
953 sensitive store model. Be warned that the 'region' model
954 is still in very early testing phase and may often crash.
Ted Kremeneke600bed2009-07-30 23:55:19 +0000955
956 -no-failure-reports - Do not create a 'failures' subdirectory that includes
957 analyzer crash reports and preprocessed source files.
Ted Kremenekb7770c02008-07-15 17:06:13 +0000958
Ted Kremenek386c6932008-09-03 17:59:35 +0000959AVAILABLE ANALYSES (multiple analyses may be specified):
Ted Kremenekd52e4252008-08-25 20:45:07 +0000960
961ENDTEXT
Ted Kremenekb7770c02008-07-15 17:06:13 +0000962
963 foreach my $Analysis (sort keys %AvailableAnalyses) {
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000964 if (defined $AnalysesDefaultEnabled{$Analysis}) {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000965 print " (+)";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000966 }
967 else {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000968 print " ";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000969 }
970
971 print " $Analysis $AvailableAnalyses{$Analysis}\n";
972 }
973
974print <<ENDTEXT
975
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000976 NOTE: "(+)" indicates that an analysis is enabled by default unless one
977 or more analysis options are specified
Ted Kremenekb7770c02008-07-15 17:06:13 +0000978
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000979BUILD OPTIONS
980
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000981 You can specify any build option acceptable to the build command.
Ted Kremenek39eefde2008-04-02 16:47:27 +0000982
Ted Kremenek5744dc22008-04-02 18:03:36 +0000983EXAMPLE
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000984
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000985 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000986
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000987 The above example causes analysis reports to be deposited into
988 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
989 A different subdirectory is created each time $Prog analyzes a project.
990 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000991
992ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000993}
994
995##----------------------------------------------------------------------------##
Ted Kremenek7cba1122008-09-22 01:35:58 +0000996# HtmlEscape - HTML entity encode characters that are special in HTML
997##----------------------------------------------------------------------------##
998
999sub HtmlEscape {
1000 # copy argument to new variable so we don't clobber the original
1001 my $arg = shift || '';
1002 my $tmp = $arg;
Ted Kremenek87f8de72008-11-03 07:44:16 +00001003 $tmp =~ s/&/&amp;/g;
1004 $tmp =~ s/</&lt;/g;
1005 $tmp =~ s/>/&gt;/g;
Ted Kremenek7cba1122008-09-22 01:35:58 +00001006 return $tmp;
1007}
1008
1009##----------------------------------------------------------------------------##
1010# ShellEscape - backslash escape characters that are special to the shell
1011##----------------------------------------------------------------------------##
1012
1013sub ShellEscape {
1014 # copy argument to new variable so we don't clobber the original
1015 my $arg = shift || '';
Ted Kremenek87f8de72008-11-03 07:44:16 +00001016 if ($arg =~ /["\s]/) { return "'" . $arg . "'"; }
1017 return $arg;
Ted Kremenek7cba1122008-09-22 01:35:58 +00001018}
1019
1020##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001021# Process command-line arguments.
1022##----------------------------------------------------------------------------##
1023
Ted Kremeneke15fa272008-10-13 21:46:42 +00001024my $AnalyzeHeaders = 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001025my $HtmlDir; # Parent directory to store HTML files.
1026my $IgnoreErrors = 0; # Ignore build errors.
Ted Kremenek7f8a3252008-04-02 18:42:49 +00001027my $ViewResults = 0; # View results when the build terminates.
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001028my $ExitStatusFoundBugs = 0; # Exit status reflects whether bugs were found
Ted Kremenekb7770c02008-07-15 17:06:13 +00001029my @AnalysesToRun;
Zhongxing Xu07c37672008-10-27 14:26:32 +00001030my $StoreModel;
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +00001031my $ConstraintsModel;
Ted Kremenek8d8bc912009-08-04 17:05:18 +00001032my $OutputFormat = "html";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001033
1034if (!@ARGV) {
1035 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +00001036 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001037}
1038
1039while (@ARGV) {
1040
1041 # Scan for options we recognize.
1042
1043 my $arg = $ARGV[0];
1044
Sam Bishop2f2418e2008-04-03 14:29:47 +00001045 if ($arg eq "-h" or $arg eq "--help") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001046 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +00001047 exit 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001048 }
1049
Ted Kremeneke15fa272008-10-13 21:46:42 +00001050 if ($arg eq '-analyze-headers') {
1051 shift @ARGV;
1052 $AnalyzeHeaders = 1;
1053 next;
1054 }
1055
Ted Kremenekfc1d3402008-08-04 18:15:26 +00001056 if (defined $AvailableAnalyses{$arg}) {
Ted Kremenek1262fc42008-05-14 20:10:33 +00001057 shift @ARGV;
Ted Kremenekb7770c02008-07-15 17:06:13 +00001058 push @AnalysesToRun, $arg;
Ted Kremenek1262fc42008-05-14 20:10:33 +00001059 next;
1060 }
1061
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001062 if ($arg eq "-o") {
1063 shift @ARGV;
1064
1065 if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +00001066 DieDiag("'-o' option requires a target directory name.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001067 }
1068
Ted Kremenekdb51a552009-03-11 18:20:33 +00001069 # Construct an absolute path. Uses the current working directory
1070 # as a base if the original path was not absolute.
1071 $HtmlDir = abs_path(shift @ARGV);
1072
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001073 next;
1074 }
Ted Kremenek7cba1122008-09-22 01:35:58 +00001075
1076 if ($arg =~ /^--html-title(=(.+))?$/) {
1077 shift @ARGV;
1078
Ted Kremenek278a5512009-05-12 18:04:43 +00001079 if (!defined $2 || $2 eq '') {
Ted Kremenek7cba1122008-09-22 01:35:58 +00001080 if (!@ARGV) {
1081 DieDiag("'--html-title' option requires a string.\n");
1082 }
1083
1084 $HtmlTitle = shift @ARGV;
1085 } else {
1086 $HtmlTitle = $2;
1087 }
1088
1089 next;
1090 }
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001091
Ted Kremenek2b74ab62008-04-01 21:22:03 +00001092 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001093 shift @ARGV;
1094 $IgnoreErrors = 1;
1095 next;
1096 }
1097
Ted Kremenekf17ef3c2008-08-21 21:47:09 +00001098 if ($arg =~ /^--use-cc(=(.+))?$/) {
1099 shift @ARGV;
1100 my $cc;
1101
Ted Kremenek278a5512009-05-12 18:04:43 +00001102 if (!defined $2 || $2 eq "") {
Ted Kremenekf17ef3c2008-08-21 21:47:09 +00001103 if (!@ARGV) {
1104 DieDiag("'--use-cc' option requires a compiler executable name.\n");
1105 }
1106 $cc = shift @ARGV;
1107 }
1108 else {
1109 $cc = $2;
1110 }
1111
1112 $ENV{"CCC_CC"} = $cc;
1113 next;
1114 }
1115
Ted Kremenek7cba1122008-09-22 01:35:58 +00001116 if ($arg =~ /^--use-c\+\+(=(.+))?$/) {
Ted Kremenek386c6932008-09-03 17:59:35 +00001117 shift @ARGV;
1118
Ted Kremenek278a5512009-05-12 18:04:43 +00001119 if (!defined $2 || $2 eq "") {
Ted Kremenek386c6932008-09-03 17:59:35 +00001120 if (!@ARGV) {
1121 DieDiag("'--use-c++' option requires a compiler executable name.\n");
1122 }
1123 $CXX = shift @ARGV;
1124 }
1125 else {
1126 $CXX = $2;
1127 }
1128 next;
1129 }
1130
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001131 if ($arg eq "-v") {
1132 shift @ARGV;
1133 $Verbose++;
1134 next;
1135 }
1136
Ted Kremenek7f8a3252008-04-02 18:42:49 +00001137 if ($arg eq "-V" or $arg eq "--view") {
1138 shift @ARGV;
1139 $ViewResults = 1;
1140 next;
1141 }
1142
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001143 if ($arg eq "--status-bugs") {
1144 shift @ARGV;
1145 $ExitStatusFoundBugs = 1;
1146 next;
1147 }
Zhongxing Xu07c37672008-10-27 14:26:32 +00001148
1149 if ($arg eq "-store") {
1150 shift @ARGV;
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +00001151 $StoreModel = shift @ARGV;
1152 next;
1153 }
1154
1155 if ($arg eq "-constraints") {
1156 shift @ARGV;
1157 $ConstraintsModel = shift @ARGV;
Zhongxing Xu07c37672008-10-27 14:26:32 +00001158 next;
1159 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001160
Ted Kremenekdb4f5f22008-11-04 00:02:53 +00001161 if ($arg eq "-plist") {
1162 shift @ARGV;
1163 $OutputFormat = "plist";
1164 next;
1165 }
Ted Kremenek7753b352009-07-27 22:10:34 +00001166 if ($arg eq "-plist-html") {
1167 shift @ARGV;
1168 $OutputFormat = "plist-html";
1169 next;
1170 }
Ted Kremeneke600bed2009-07-30 23:55:19 +00001171
1172 if ($arg eq "-no-failure-reports") {
1173 $ENV{"CCC_REPORT_FAILURES"} = 0;
1174 next;
1175 }
Ted Kremenek7753b352009-07-27 22:10:34 +00001176
Ted Kremenek23cfca32008-06-16 22:40:14 +00001177 DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/);
Ted Kremenek0062ad42008-04-02 16:35:01 +00001178
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001179 last;
1180}
1181
1182if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +00001183 Diag("No build command specified.\n\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001184 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +00001185 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001186}
1187
Ted Kremenek7cba1122008-09-22 01:35:58 +00001188$CmdArgs = HtmlEscape(join(' ', map(ShellEscape($_), @ARGV)));
1189$HtmlTitle = "${CurrentDirSuffix} - scan-build results"
1190 unless (defined($HtmlTitle));
Ted Kremenek386c6932008-09-03 17:59:35 +00001191
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001192# Determine the output directory for the HTML reports.
Ted Kremenek684bb092008-04-18 15:18:20 +00001193my $BaseDir = $HtmlDir;
Sam Bishopa0e22662008-04-02 03:35:43 +00001194$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001195
1196# Set the appropriate environment variables.
Sam Bishopa0e22662008-04-02 03:35:43 +00001197SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001198
Ted Kremenek91ea79d2009-03-24 05:30:14 +00001199my $Cmd = Cwd::realpath("$RealBin/libexec/ccc-analyzer");
Ted Kremenekce87b922009-02-25 22:54:02 +00001200if (!defined $Cmd || ! -x $Cmd) {
1201 $Cmd = Cwd::realpath("$RealBin/ccc-analyzer");
Ted Kremenek6b896362009-02-27 06:17:05 +00001202 DieDiag("Executable 'ccc-analyzer' does not exist at '$Cmd'\n") if(! -x $Cmd);
Ted Kremenekce87b922009-02-25 22:54:02 +00001203}
Ted Kremenekf22eacb2008-04-18 22:00:56 +00001204
Ted Kremenekfd9df0e2009-05-09 19:19:28 +00001205if (!defined $ClangCCSB || ! -x $ClangCCSB) {
Ted Kremenek91ea79d2009-03-24 05:30:14 +00001206 Diag("'clang-cc' executable not found in '$RealBin/libexec'.\n");
Ted Kremenek318e6a62009-03-24 04:29:13 +00001207 Diag("Using 'clang-cc' from path.\n");
Ted Kremenekf22eacb2008-04-18 22:00:56 +00001208}
Ted Kremenekfd9df0e2009-05-09 19:19:28 +00001209if (!defined $ClangSB || ! -x $ClangSB) {
1210 Diag("'clang' executable not found in '$RealBin/bin'.\n");
1211 Diag("Using 'clang' from path.\n");
1212}
Ted Kremenek0b6c1532008-04-08 20:22:12 +00001213
Ted Kremenek95aa1052008-09-04 17:52:41 +00001214if (defined $CXX) {
1215 $ENV{'CXX'} = $CXX;
1216}
1217else {
1218 $CXX = 'g++'; # This variable is used by other parts of scan-build
1219 # that need to know a default C++ compiler to fall back to.
1220}
1221
Ted Kremenek4f4b17d2008-04-03 20:08:18 +00001222$ENV{'CC'} = $Cmd;
Ted Kremenekfd9df0e2009-05-09 19:19:28 +00001223$ENV{'CLANG_CC'} = $ClangCC;
Ted Kremenekf22eacb2008-04-18 22:00:56 +00001224$ENV{'CLANG'} = $Clang;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001225
1226if ($Verbose >= 2) {
1227 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
1228}
1229
Ted Kremeneka9525c92008-05-12 22:07:14 +00001230if ($Verbose >= 3) {
1231 $ENV{'CCC_ANALYZER_LOG'} = 1;
1232}
1233
Ted Kremenek90125992008-07-15 23:41:32 +00001234if (scalar(@AnalysesToRun) == 0) {
1235 foreach my $key (keys %AnalysesDefaultEnabled) {
1236 push @AnalysesToRun,$key;
1237 }
Ted Kremenek01006782008-07-02 23:16:10 +00001238}
Ted Kremenek1262fc42008-05-14 20:10:33 +00001239
Ted Kremeneke15fa272008-10-13 21:46:42 +00001240if ($AnalyzeHeaders) {
1241 push @AnalysesToRun,"-analyzer-opt-analyze-headers";
1242}
1243
Ted Kremenek90125992008-07-15 23:41:32 +00001244$ENV{'CCC_ANALYZER_ANALYSIS'} = join ' ',@AnalysesToRun;
1245
Zhongxing Xu3cab2b12008-11-02 10:58:16 +00001246if (defined $StoreModel) {
Zhongxing Xu07c37672008-10-27 14:26:32 +00001247 $ENV{'CCC_ANALYZER_STORE_MODEL'} = $StoreModel;
1248}
1249
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +00001250if (defined $ConstraintsModel) {
1251 $ENV{'CCC_ANALYZER_CONSTRAINTS_MODEL'} = $ConstraintsModel;
1252}
1253
Ted Kremenekdb4f5f22008-11-04 00:02:53 +00001254if (defined $OutputFormat) {
1255 $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'} = $OutputFormat;
1256}
1257
1258
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001259# Run the build.
Ted Kremenek5656a982008-07-15 17:09:28 +00001260my $ExitStatus = RunBuildCommand(\@ARGV, $IgnoreErrors, $Cmd);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001261
Ted Kremenek7753b352009-07-27 22:10:34 +00001262if (defined $OutputFormat) {
Daniel Dunbar11825022009-07-29 16:21:23 +00001263 if ($OutputFormat =~ /plist/) {
1264 Diag "Analysis run complete.\n";
1265 Diag "Analysis results (plist files) deposited in '$HtmlDir'\n";
1266 }
1267 elsif ($OutputFormat =~ /html/) {
Ted Kremenek7753b352009-07-27 22:10:34 +00001268 # Postprocess the HTML directory.
1269 my $NumBugs = Postprocess($HtmlDir, $BaseDir);
Ted Kremenek5656a982008-07-15 17:09:28 +00001270
Ted Kremenek7753b352009-07-27 22:10:34 +00001271 if ($ViewResults and -r "$HtmlDir/index.html") {
1272 Diag "Analysis run complete.\n";
1273 Diag "Viewing analysis results in '$HtmlDir' using scan-view.\n";
1274 my $ScanView = Cwd::realpath("$RealBin/scan-view");
1275 if (! -x $ScanView) { $ScanView = "scan-view"; }
1276 exec $ScanView, "$HtmlDir";
1277 }
1278
1279 if ($ExitStatusFoundBugs) {
1280 exit 1 if ($NumBugs > 0);
1281 exit 0;
1282 }
Ted Kremenekdb4f5f22008-11-04 00:02:53 +00001283 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001284}
1285
Ted Kremenek5656a982008-07-15 17:09:28 +00001286exit $ExitStatus;
1287