blob: 0e377b8d035ec2d3e66e2a7fff8e132e98ec36c4 [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 Kremeneke15fa272008-10-13 21:46:42 +000084my $ClangSB = Cwd::realpath("$RealBin/clang");
Ted Kremenekb7770c02008-07-15 17:06:13 +000085my $Clang = $ClangSB;
86
87if (! -x $ClangSB) {
88 $Clang = "clang";
89}
90
91my %AvailableAnalyses;
92
93# Query clang for analysis options.
Ted Kremenek63c20172008-08-04 17:34:06 +000094open(PIPE, "-|", $Clang, "--help") or
Ted Kremenek445fa772008-10-10 00:17:08 +000095 DieDiag("Cannot execute '$Clang'\n");
Ted Kremenek63c20172008-08-04 17:34:06 +000096
Ted Kremenekb7770c02008-07-15 17:06:13 +000097my $FoundAnalysis = 0;
98
99while(<PIPE>) {
100 if ($FoundAnalysis == 0) {
Ted Kremenek938eef12009-02-17 23:31:05 +0000101 if (/Checks and Analyses/) {
Ted Kremenekb7770c02008-07-15 17:06:13 +0000102 $FoundAnalysis = 1;
103 }
Ted Kremenekb7770c02008-07-15 17:06:13 +0000104 next;
105 }
106
107 if (/^\s\s\s\s([^\s]+)\s(.+)$/) {
108 next if ($1 =~ /-dump/ or $1 =~ /-view/
109 or $1 =~ /-checker-simple/ or $1 =~ /-warn-uninit/);
110
111 $AvailableAnalyses{$1} = $2;
112 next;
Ted Kremenek938eef12009-02-17 23:31:05 +0000113 }
Ted Kremenekb7770c02008-07-15 17:06:13 +0000114 last;
115}
116
117close (PIPE);
118
119my %AnalysesDefaultEnabled = (
120 '-warn-dead-stores' => 1,
121 '-checker-cfref' => 1,
Ted Kremenek90125992008-07-15 23:41:32 +0000122 '-warn-objc-methodsigs' => 1,
Ted Kremenekbde3a052008-07-25 20:35:01 +0000123 '-warn-objc-missing-dealloc' => 1,
Ted Kremenek5d443492008-09-18 06:34:16 +0000124 '-warn-objc-unused-ivars' => 1,
Ted Kremenekb7770c02008-07-15 17:06:13 +0000125);
126
127##----------------------------------------------------------------------------##
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000128# GetHTMLRunDir - Construct an HTML directory name for the current sub-run.
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000129##----------------------------------------------------------------------------##
130
Sam Bishopa0e22662008-04-02 03:35:43 +0000131sub GetHTMLRunDir {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000132
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000133 die "Not enough arguments." if (@_ == 0);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000134 my $Dir = shift @_;
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000135
136 my $TmpMode = 0;
137 if (!defined $Dir) {
Ted Kremenekffda0b42008-10-31 05:48:42 +0000138 if (`uname` =~ /Darwin/) {
139 $Dir = $ENV{'TMPDIR'};
140 if (!defined $Dir) { $Dir = "/tmp"; }
141 }
142 else {
143 $Dir = "/tmp";
144 }
145
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000146 $TmpMode = 1;
147 }
148
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000149 # Get current date and time.
150
151 my @CurrentTime = localtime();
152
153 my $year = $CurrentTime[5] + 1900;
154 my $day = $CurrentTime[3];
155 my $month = $CurrentTime[4] + 1;
156
Ted Kremenek9d7405f2008-05-14 17:23:56 +0000157 my $DateString = sprintf("%d-%02d-%02d", $year, $month, $day);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000158
159 # Determine the run number.
160
161 my $RunNumber;
162
163 if (-d $Dir) {
164
165 if (! -r $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000166 DieDiag("directory '$Dir' exists but is not readable.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000167 }
168
169 # Iterate over all files in the specified directory.
170
171 my $max = 0;
172
173 opendir(DIR, $Dir);
Ted Kremenek29da6c52008-08-07 17:57:34 +0000174 my @FILES = grep { -d "$Dir/$_" } readdir(DIR);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000175 closedir(DIR);
176
177 foreach my $f (@FILES) {
178
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000179 # Strip the prefix '$Prog-' if we are dumping files to /tmp.
180 if ($TmpMode) {
181 next if (!($f =~ /^$Prog-(.+)/));
182 $f = $1;
183 }
184
Ted Kremenekebb74132008-09-21 06:58:09 +0000185
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000186 my @x = split/-/, $f;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000187 next if (scalar(@x) != 4);
188 next if ($x[0] != $year);
189 next if ($x[1] != $month);
190 next if ($x[2] != $day);
191
192 if ($x[3] > $max) {
193 $max = $x[3];
194 }
195 }
196
197 $RunNumber = $max + 1;
198 }
199 else {
200
201 if (-x $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000202 DieDiag("'$Dir' exists but is not a directory.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000203 }
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000204
205 if ($TmpMode) {
Ted Kremenek445fa772008-10-10 00:17:08 +0000206 DieDiag("The directory '/tmp' does not exist or cannot be accessed.\n");
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000207 }
208
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000209 # $Dir does not exist. It will be automatically created by the
210 # clang driver. Set the run number to 1.
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000211
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000212 $RunNumber = 1;
213 }
214
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000215 die "RunNumber must be defined!" if (!defined $RunNumber);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000216
217 # Append the run number.
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000218 my $NewDir;
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000219 if ($TmpMode) {
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000220 $NewDir = "$Dir/$Prog-$DateString-$RunNumber";
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000221 }
222 else {
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000223 $NewDir = "$Dir/$DateString-$RunNumber";
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000224 }
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000225 system 'mkdir','-p',$NewDir;
226 return $NewDir;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000227}
228
Sam Bishopa0e22662008-04-02 03:35:43 +0000229sub SetHtmlEnv {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000230
231 die "Wrong number of arguments." if (scalar(@_) != 2);
232
233 my $Args = shift;
234 my $Dir = shift;
235
236 die "No build command." if (scalar(@$Args) == 0);
237
238 my $Cmd = $$Args[0];
239
240 if ($Cmd =~ /configure/) {
241 return;
242 }
243
244 if ($Verbose) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000245 Diag("Emitting reports for this run to '$Dir'.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000246 }
247
248 $ENV{'CCC_ANALYZER_HTML'} = $Dir;
249}
250
251##----------------------------------------------------------------------------##
Ted Kremenek57cf4462008-04-18 15:09:30 +0000252# ComputeDigest - Compute a digest of the specified file.
253##----------------------------------------------------------------------------##
254
255sub ComputeDigest {
256 my $FName = shift;
Ted Kremenek23cfca32008-06-16 22:40:14 +0000257 DieDiag("Cannot read $FName to compute Digest.\n") if (! -r $FName);
Ted Kremeneka6e24812008-04-19 18:05:48 +0000258
259 # Use Digest::MD5. We don't have to be cryptographically secure. We're
Ted Kremenek7ea02e62008-04-19 18:07:44 +0000260 # just looking for duplicate files that come from a non-malicious source.
261 # We use Digest::MD5 because it is a standard Perl module that should
Ted Kremenek63c20172008-08-04 17:34:06 +0000262 # come bundled on most systems.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000263 open(FILE, $FName) or DieDiag("Cannot open $FName when computing Digest.\n");
Ted Kremeneka6e24812008-04-19 18:05:48 +0000264 binmode FILE;
265 my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest;
266 close(FILE);
267
Ted Kremenek63c20172008-08-04 17:34:06 +0000268 # Return the digest.
Ted Kremeneka6e24812008-04-19 18:05:48 +0000269 return $Result;
Ted Kremenek57cf4462008-04-18 15:09:30 +0000270}
271
272##----------------------------------------------------------------------------##
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000273# UpdatePrefix - Compute the common prefix of files.
274##----------------------------------------------------------------------------##
275
276my $Prefix;
277
278sub UpdatePrefix {
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000279 my $x = shift;
280 my $y = basename($x);
281 $x =~ s/\Q$y\E$//;
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000282
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000283 if (!defined $Prefix) {
284 $Prefix = $x;
285 return;
286 }
287
Ted Kremenek20b2bae2008-09-11 21:15:10 +0000288 chop $Prefix while (!($x =~ /^\Q$Prefix/));
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000289}
290
291sub GetPrefix {
292 return $Prefix;
293}
294
295##----------------------------------------------------------------------------##
296# UpdateInFilePath - Update the path in the report file.
297##----------------------------------------------------------------------------##
298
299sub UpdateInFilePath {
300 my $fname = shift;
301 my $regex = shift;
302 my $newtext = shift;
Ted Kremenek63c20172008-08-04 17:34:06 +0000303
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000304 open (RIN, $fname) or die "cannot open $fname";
Ted Kremenek63c20172008-08-04 17:34:06 +0000305 open (ROUT, ">", "$fname.tmp") or die "cannot open $fname.tmp";
306
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000307 while (<RIN>) {
308 s/$regex/$newtext/;
309 print ROUT $_;
310 }
Ted Kremenek63c20172008-08-04 17:34:06 +0000311
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000312 close (ROUT);
313 close (RIN);
Ted Kremenek20161e92008-07-15 20:18:21 +0000314 system("mv", "$fname.tmp", $fname);
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000315}
316
317##----------------------------------------------------------------------------##
Ted Kremenek5744dc22008-04-02 18:03:36 +0000318# ScanFile - Scan a report file for various identifying attributes.
319##----------------------------------------------------------------------------##
320
Ted Kremenek57cf4462008-04-18 15:09:30 +0000321# Sometimes a source file is scanned more than once, and thus produces
322# multiple error reports. We use a cache to solve this problem.
323
324my %AlreadyScanned;
325
Ted Kremenek5744dc22008-04-02 18:03:36 +0000326sub ScanFile {
327
328 my $Index = shift;
329 my $Dir = shift;
330 my $FName = shift;
331
Ted Kremenek57cf4462008-04-18 15:09:30 +0000332 # Compute a digest for the report file. Determine if we have already
333 # scanned a file that looks just like it.
334
335 my $digest = ComputeDigest("$Dir/$FName");
336
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000337 if (defined $AlreadyScanned{$digest}) {
Ted Kremenek57cf4462008-04-18 15:09:30 +0000338 # Redundant file. Remove it.
Ted Kremenek20161e92008-07-15 20:18:21 +0000339 system ("rm", "-f", "$Dir/$FName");
Ted Kremenek57cf4462008-04-18 15:09:30 +0000340 return;
341 }
342
343 $AlreadyScanned{$digest} = 1;
344
Ted Kremenek809709f2008-04-18 16:58:34 +0000345 # At this point the report file is not world readable. Make it happen.
Ted Kremenek20161e92008-07-15 20:18:21 +0000346 system ("chmod", "644", "$Dir/$FName");
Ted Kremenek684bb092008-04-18 15:18:20 +0000347
348 # Scan the report file for tags.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000349 open(IN, "$Dir/$FName") or DieDiag("Cannot open '$Dir/$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000350
Ted Kremeneka26ddab2009-01-27 01:53:39 +0000351 my $BugType = "";
Ted Kremenek22d6a632008-04-02 20:43:36 +0000352 my $BugFile = "";
Ted Kremenekebb74132008-09-21 06:58:09 +0000353 my $BugCategory;
Ted Kremenek22d6a632008-04-02 20:43:36 +0000354 my $BugPathLength = 1;
355 my $BugLine = 0;
Ted Kremenekebb74132008-09-21 06:58:09 +0000356 my $found = 0;
357
Ted Kremenek5744dc22008-04-02 18:03:36 +0000358 while (<IN>) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000359
360 last if ($found == 5);
361
Ted Kremeneka26ddab2009-01-27 01:53:39 +0000362 if (/<!-- BUGTYPE (.*) -->$/) {
363 $BugType = $1;
Ted Kremenekebb74132008-09-21 06:58:09 +0000364 ++$found;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000365 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000366 elsif (/<!-- BUGFILE (.*) -->$/) {
Ted Kremenek990c2f42008-12-03 19:19:23 +0000367 $BugFile = abs_path($1);
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000368 UpdatePrefix($BugFile);
Ted Kremenekebb74132008-09-21 06:58:09 +0000369 ++$found;
Ted Kremenek22d6a632008-04-02 20:43:36 +0000370 }
371 elsif (/<!-- BUGPATHLENGTH (.*) -->$/) {
372 $BugPathLength = $1;
Ted Kremenekebb74132008-09-21 06:58:09 +0000373 ++$found;
Ted Kremenek22d6a632008-04-02 20:43:36 +0000374 }
375 elsif (/<!-- BUGLINE (.*) -->$/) {
376 $BugLine = $1;
Ted Kremenekebb74132008-09-21 06:58:09 +0000377 ++$found;
378 }
379 elsif (/<!-- BUGCATEGORY (.*) -->$/) {
380 $BugCategory = $1;
381 ++$found;
Ted Kremenek22d6a632008-04-02 20:43:36 +0000382 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000383 }
384
385 close(IN);
Ted Kremenekebb74132008-09-21 06:58:09 +0000386
387 if (!defined $BugCategory) {
388 $BugCategory = "Other";
389 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000390
Ted Kremeneka26ddab2009-01-27 01:53:39 +0000391 push @$Index,[ $FName, $BugCategory, $BugType, $BugFile, $BugLine,
Ted Kremenek81983112008-09-28 04:13:09 +0000392 $BugPathLength ];
Ted Kremenek22d6a632008-04-02 20:43:36 +0000393}
394
395##----------------------------------------------------------------------------##
Ted Kremenek3ce12072008-09-22 17:50:47 +0000396# CopyFiles - Copy resource files to target directory.
Ted Kremenek22d6a632008-04-02 20:43:36 +0000397##----------------------------------------------------------------------------##
398
Ted Kremenek3ce12072008-09-22 17:50:47 +0000399sub CopyFiles {
Ted Kremenek22d6a632008-04-02 20:43:36 +0000400
401 my $Dir = shift;
Ted Kremeneke15fa272008-10-13 21:46:42 +0000402
403 my $JS = Cwd::realpath("$RealBin/sorttable.js");
Ted Kremenek22d6a632008-04-02 20:43:36 +0000404
Ted Kremenek23cfca32008-06-16 22:40:14 +0000405 DieDiag("Cannot find 'sorttable.js'.\n")
Ted Kremeneke15fa272008-10-13 21:46:42 +0000406 if (! -r $JS);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000407
Ted Kremeneke15fa272008-10-13 21:46:42 +0000408 system ("cp", $JS, "$Dir");
Ted Kremenek22d6a632008-04-02 20:43:36 +0000409
Ted Kremenek23cfca32008-06-16 22:40:14 +0000410 DieDiag("Could not copy 'sorttable.js' to '$Dir'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000411 if (! -r "$Dir/sorttable.js");
Ted Kremenek3ce12072008-09-22 17:50:47 +0000412
Ted Kremeneke15fa272008-10-13 21:46:42 +0000413 my $CSS = Cwd::realpath("$RealBin/scanview.css");
414
Ted Kremenek3ce12072008-09-22 17:50:47 +0000415 DieDiag("Cannot find 'scanview.css'.\n")
Ted Kremeneke15fa272008-10-13 21:46:42 +0000416 if (! -r $CSS);
Ted Kremenek3ce12072008-09-22 17:50:47 +0000417
Ted Kremeneke15fa272008-10-13 21:46:42 +0000418 system ("cp", $CSS, "$Dir");
Ted Kremenek3ce12072008-09-22 17:50:47 +0000419
420 DieDiag("Could not copy 'scanview.css' to '$Dir'.\n")
Ted Kremeneke15fa272008-10-13 21:46:42 +0000421 if (! -r $CSS);
Ted Kremenek5744dc22008-04-02 18:03:36 +0000422}
423
424##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000425# Postprocess - Postprocess the results of an analysis scan.
426##----------------------------------------------------------------------------##
427
Sam Bishopa0e22662008-04-02 03:35:43 +0000428sub Postprocess {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000429
430 my $Dir = shift;
Ted Kremenek684bb092008-04-18 15:18:20 +0000431 my $BaseDir = shift;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000432
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000433 die "No directory specified." if (!defined $Dir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000434
435 if (! -d $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000436 Diag("No bugs found.\n");
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000437 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000438 }
439
440 opendir(DIR, $Dir);
Ted Kremenek938eef12009-02-17 23:31:05 +0000441 my @files = grep { /^report-.*\.html$/ } readdir(DIR);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000442 closedir(DIR);
443
Ted Kremenek938eef12009-02-17 23:31:05 +0000444 if (scalar(@files) == 0 and ! -e "$Dir/failures") {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000445 Diag("Removing directory '$Dir' because it contains no reports.\n");
Ted Kremenek20161e92008-07-15 20:18:21 +0000446 system ("rm", "-fR", $Dir);
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000447 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000448 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000449
Ted Kremenek991c54b2008-08-08 20:46:42 +0000450 # Scan each report file and build an index.
451 my @Index;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000452 foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); }
453
Ted Kremenek938eef12009-02-17 23:31:05 +0000454 # Scan the failures directory and use the information in the .info files
Ted Kremenekd52e4252008-08-25 20:45:07 +0000455 # to update the common prefix directory.
Ted Kremenek938eef12009-02-17 23:31:05 +0000456 my @failures;
457 my @attributes_ignored;
458 if (-d "$Dir/failures") {
459 opendir(DIR, "$Dir/failures");
460 @failures = grep { /[.]info.txt$/ && !/attribute_ignored/; } readdir(DIR);
Ted Kremenekd52e4252008-08-25 20:45:07 +0000461 closedir(DIR);
Ted Kremenek938eef12009-02-17 23:31:05 +0000462 opendir(DIR, "$Dir/failures");
463 @attributes_ignored = grep { /^attribute_ignored/; } readdir(DIR);
464 closedir(DIR);
465 foreach my $file (@failures) {
466 open IN, "$Dir/failures/$file" or DieDiag("cannot open $file\n");
Ted Kremenekd52e4252008-08-25 20:45:07 +0000467 my $Path = <IN>;
468 if (defined $Path) { UpdatePrefix($Path); }
469 close IN;
470 }
471 }
472
Ted Kremenek63c20172008-08-04 17:34:06 +0000473 # Generate an index.html file.
474 my $FName = "$Dir/index.html";
475 open(OUT, ">", $FName) or DieDiag("Cannot create file '$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000476
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000477 # Print out the header.
478
Ted Kremenek5744dc22008-04-02 18:03:36 +0000479print OUT <<ENDTEXT;
480<html>
481<head>
Ted Kremenek7cba1122008-09-22 01:35:58 +0000482<title>${HtmlTitle}</title>
Ted Kremenekf1435452008-09-23 22:34:51 +0000483<link type="text/css" rel="stylesheet" href="scanview.css"/>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000484<script src="sorttable.js"></script>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000485<script language='javascript' type="text/javascript">
486function SetDisplay(RowClass, DisplayVal)
487{
488 var Rows = document.getElementsByTagName("tr");
489 for ( var i = 0 ; i < Rows.length; ++i ) {
490 if (Rows[i].className == RowClass) {
491 Rows[i].style.display = DisplayVal;
492 }
493 }
494}
Ted Kremenekebb74132008-09-21 06:58:09 +0000495
Ted Kremenek2350a462008-10-28 19:56:52 +0000496function CopyCheckedStateToCheckButtons(SummaryCheckButton) {
497 var Inputs = document.getElementsByTagName("input");
498 for ( var i = 0 ; i < Inputs.length; ++i ) {
499 if (Inputs[i].type == "checkbox") {
500 if(Inputs[i] != SummaryCheckButton) {
501 Inputs[i].checked = SummaryCheckButton.checked;
502 Inputs[i].onclick();
503 }
504 }
505 }
506}
507
Ted Kremenek999e1202008-10-28 20:09:57 +0000508function returnObjById( id ) {
509 if (document.getElementById)
510 var returnVar = document.getElementById(id);
511 else if (document.all)
512 var returnVar = document.all[id];
513 else if (document.layers)
514 var returnVar = document.layers[id];
515 return returnVar;
516}
517
518var NumUnchecked = 0;
519
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000520function ToggleDisplay(CheckButton, ClassName) {
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000521 if (CheckButton.checked) {
522 SetDisplay(ClassName, "");
Ted Kremenek999e1202008-10-28 20:09:57 +0000523 if (--NumUnchecked == 0) {
524 returnObjById("AllBugsCheck").checked = true;
525 }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000526 }
527 else {
528 SetDisplay(ClassName, "none");
Ted Kremenek999e1202008-10-28 20:09:57 +0000529 NumUnchecked++;
530 returnObjById("AllBugsCheck").checked = false;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000531 }
532}
533</script>
Ted Kremenek1d1abb12008-09-22 17:52:58 +0000534<!-- SUMMARYENDHEAD -->
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000535</head>
536<body>
Ted Kremenek7cba1122008-09-22 01:35:58 +0000537<h1>${HtmlTitle}</h1>
538
539<table>
540<tr><th>User:</th><td>${UserName}\@${HostName}</td></tr>
541<tr><th>Working Directory:</th><td>${CurrentDir}</td></tr>
542<tr><th>Command Line:</th><td>${CmdArgs}</td></tr>
543<tr><th>Date:</th><td>${Date}</td></tr>
544ENDTEXT
545
546print OUT "<tr><th>Version:</th><td>${BuildName} (${BuildDate})</td></tr>\n"
547 if (defined($BuildName) && defined($BuildDate));
548
549print OUT <<ENDTEXT;
550</table>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000551ENDTEXT
552
Ted Kremenek991c54b2008-08-08 20:46:42 +0000553 if (scalar(@files)) {
554 # Print out the summary table.
555 my %Totals;
Ted Kremenekebb74132008-09-21 06:58:09 +0000556
Ted Kremenek991c54b2008-08-08 20:46:42 +0000557 for my $row ( @Index ) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000558 my $bug_type = ($row->[2]);
559 my $bug_category = ($row->[1]);
560 my $key = "$bug_category:$bug_type";
561
562 if (!defined $Totals{$key}) { $Totals{$key} = [1,$bug_category,$bug_type]; }
563 else { $Totals{$key}->[0]++; }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000564 }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000565
Ted Kremenek7cba1122008-09-22 01:35:58 +0000566 print OUT "<h2>Bug Summary</h2>";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000567
568 if (defined $BuildName) {
569 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 +0000570 }
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000571
Ted Kremenek2350a462008-10-28 19:56:52 +0000572 my $TotalBugs = scalar(@Index);
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000573print OUT <<ENDTEXT;
Ted Kremenekebb74132008-09-21 06:58:09 +0000574<table>
575<thead><tr><td>Bug Type</td><td>Quantity</td><td class="sorttable_nosort">Display?</td></tr></thead>
Ted Kremenek999e1202008-10-28 20:09:57 +0000576<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 +0000577ENDTEXT
578
Ted Kremenekebb74132008-09-21 06:58:09 +0000579 my $last_category;
580
581 for my $key (
582 sort {
583 my $x = $Totals{$a};
584 my $y = $Totals{$b};
585 my $res = $x->[1] cmp $y->[1];
586 $res = $x->[2] cmp $y->[2] if ($res == 0);
587 $res
588 } keys %Totals )
589 {
590 my $val = $Totals{$key};
591 my $category = $val->[1];
592 if (!defined $last_category or $last_category ne $category) {
593 $last_category = $category;
594 print OUT "<tr><th>$category</th><th colspan=2></th></tr>\n";
595 }
596 my $x = lc $key;
597 $x =~ s/[ ,'":\/()]+/_/g;
598 print OUT "<tr><td class=\"SUMM_DESC\">";
599 print OUT $val->[2];
Ted Kremenek2350a462008-10-28 19:56:52 +0000600 print OUT "</td><td class=\"Q\">";
Ted Kremenekebb74132008-09-21 06:58:09 +0000601 print OUT $val->[0];
602 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 +0000603 }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000604
605 # Print out the table of errors.
606
607print OUT <<ENDTEXT;
608</table>
Ted Kremenek7cba1122008-09-22 01:35:58 +0000609<h2>Reports</h2>
Ted Kremenekebb74132008-09-21 06:58:09 +0000610
611<table class="sortable" style="table-layout:automatic">
612<thead><tr>
613 <td>Bug Group</td>
614 <td class="sorttable_sorted">Bug Type<span id="sorttable_sortfwdind">&nbsp;&#x25BE;</span></td>
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000615 <td>File</td>
Ted Kremenekebb74132008-09-21 06:58:09 +0000616 <td class="Q">Line</td>
Ted Kremenek81983112008-09-28 04:13:09 +0000617 <td class="Q">Path Length</td>
Ted Kremenek2645c772008-07-07 16:58:44 +0000618 <td class="sorttable_nosort"></td>
Ted Kremenekebb74132008-09-21 06:58:09 +0000619 <!-- REPORTBUGCOL -->
620</tr></thead>
621<tbody>
Ted Kremenek5744dc22008-04-02 18:03:36 +0000622ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000623
Ted Kremenek991c54b2008-08-08 20:46:42 +0000624 my $prefix = GetPrefix();
625 my $regex;
626 my $InFileRegex;
627 my $InFilePrefix = "File:</td><td>";
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000628
Ted Kremenek991c54b2008-08-08 20:46:42 +0000629 if (defined $prefix) {
630 $regex = qr/^\Q$prefix\E/is;
631 $InFileRegex = qr/\Q$InFilePrefix$prefix\E/is;
632 }
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000633
Ted Kremenekebb74132008-09-21 06:58:09 +0000634 for my $row ( sort { $a->[2] cmp $b->[2] } @Index ) {
635 my $x = "$row->[1]:$row->[2]";
636 $x = lc $x;
637 $x =~ s/[ ,'":\/()]+/_/g;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000638
Ted Kremenek991c54b2008-08-08 20:46:42 +0000639 my $ReportFile = $row->[0];
Ted Kremenekebb74132008-09-21 06:58:09 +0000640
641 print OUT "<tr class=\"bt_$x\">";
642 print OUT "<td class=\"DESC\">";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000643 print OUT $row->[1];
Ted Kremenekebb74132008-09-21 06:58:09 +0000644 print OUT "</td>";
645 print OUT "<td class=\"DESC\">";
646 print OUT $row->[2];
647 print OUT "</td>";
648
649 # Update the file prefix.
650 my $fname = $row->[3];
Ted Kremenekebb74132008-09-21 06:58:09 +0000651
Ted Kremenek991c54b2008-08-08 20:46:42 +0000652 if (defined $regex) {
653 $fname =~ s/$regex//;
654 UpdateInFilePath("$Dir/$ReportFile", $InFileRegex, $InFilePrefix)
655 }
Ted Kremenekebb74132008-09-21 06:58:09 +0000656
Ted Kremenek91639ef2008-09-22 17:42:31 +0000657 print OUT "<td>";
Ted Kremenekebb74132008-09-21 06:58:09 +0000658 my @fname = split /\//,$fname;
659 if ($#fname > 0) {
660 while ($#fname >= 0) {
661 my $x = shift @fname;
662 print OUT $x;
663 if ($#fname >= 0) {
664 print OUT "<span class=\"W\"> </span>/";
665 }
666 }
667 }
668 else {
669 print OUT $fname;
Ted Kremenek91639ef2008-09-22 17:42:31 +0000670 }
Ted Kremenekebb74132008-09-21 06:58:09 +0000671 print OUT "</td>";
672
673 # Print out the quantities.
Ted Kremenek81983112008-09-28 04:13:09 +0000674 for my $j ( 4 .. 5 ) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000675 print OUT "<td class=\"Q\">$row->[$j]</td>";
676 }
677
Ted Kremenek991c54b2008-08-08 20:46:42 +0000678 # Print the rest of the columns.
Ted Kremenek81983112008-09-28 04:13:09 +0000679 for (my $j = 6; $j <= $#{$row}; ++$j) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000680 print OUT "<td>$row->[$j]</td>"
Ted Kremenek991c54b2008-08-08 20:46:42 +0000681 }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000682
Ted Kremenek991c54b2008-08-08 20:46:42 +0000683 # Emit the "View" link.
Ted Kremenek68005dd2008-09-22 17:39:18 +0000684 print OUT "<td><a href=\"$ReportFile#EndPath\">View Report</a></td>";
Ted Kremenek3cea9ee2008-07-30 17:58:08 +0000685
Daniel Dunbare43038e2008-09-19 23:18:44 +0000686 # Emit REPORTBUG markers.
Ted Kremenekebb74132008-09-21 06:58:09 +0000687 print OUT "\n<!-- REPORTBUG id=\"$ReportFile\" -->\n";
Daniel Dunbare43038e2008-09-19 23:18:44 +0000688
Ted Kremenek991c54b2008-08-08 20:46:42 +0000689 # End the row.
690 print OUT "</tr>\n";
691 }
692
Ted Kremenekebb74132008-09-21 06:58:09 +0000693 print OUT "</tbody>\n</table>\n\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000694 }
695
Ted Kremenek938eef12009-02-17 23:31:05 +0000696 if (scalar (@failures) || scalar(@attributes_ignored)) {
697 print OUT "<h2>Analyzer Failures</h2>\n";
698
699 if (scalar @attributes_ignored) {
700 print OUT "The analyzer's parser ignored the following attributes:<p>\n";
701 print OUT "<table>\n";
702 print OUT "<thead><tr><td>Attribute</td><td>Source File</td><td>Preprocessed File</td><td>STDERR Output</td></tr></thead>\n";
703 foreach my $file (sort @attributes_ignored) {
704 die "cannot demangle attribute name\n" if (! ($file =~ /^attribute_ignored_(.+).txt/));
705 my $attribute = $1;
706 # Open the attribute file to get the first file that failed.
707 next if (!open (ATTR, "$Dir/failures/$file"));
708 my $ppfile = <ATTR>;
709 chomp $ppfile;
710 close ATTR;
711 next if (! -e "$Dir/failures/$ppfile");
712 # Open the info file and get the name of the source file.
713 open (INFO, "$Dir/failures/$ppfile.info.txt") or
714 die "Cannot open $Dir/failures/$ppfile.info.txt\n";
715 my $srcfile = <INFO>;
716 chomp $srcfile;
717 close (INFO);
718 # Print the information in the table.
719 my $prefix = GetPrefix();
720 if (defined $prefix) { $srcfile =~ s/^\Q$prefix//; }
721 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";
722 my $ppfile_clang = $ppfile;
723 $ppfile_clang =~ s/[.](.+)$/.clang.$1/;
724 print OUT " <!-- REPORTPROBLEM src=\"$srcfile\" file=\"failures/$ppfile\" clangfile=\"failures/$ppfile_clang\" stderr=\"failures/$ppfile.stderr.txt\" info=\"failures/$ppfile.info.txt\" -->\n";
725 }
726 print OUT "</table>\n";
727 }
728
729 if (scalar @failures) {
730 print OUT "<p>The analyzer had problems processing the following files:</p>\n";
731 print OUT "<table>\n";
732 print OUT "<thead><tr><td>Problem</td><td>Source File</td><td>Preprocessed File</td><td>STDERR Output</td></tr></thead>\n";
733 foreach my $file (sort @failures) {
Ted Kremenek82a12532008-09-25 00:25:16 +0000734 $file =~ /(.+).info.txt$/;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000735 # Get the preprocessed file.
736 my $ppfile = $1;
737 # Open the info file and get the name of the source file.
Ted Kremenek938eef12009-02-17 23:31:05 +0000738 open (INFO, "$Dir/failures/$file") or
739 die "Cannot open $Dir/failures/$file\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000740 my $srcfile = <INFO>;
Ted Kremenek5d31f832008-08-18 18:38:29 +0000741 chomp $srcfile;
742 my $problem = <INFO>;
743 chomp $problem;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000744 close (INFO);
745 # Print the information in the table.
Ted Kremenekd52e4252008-08-25 20:45:07 +0000746 my $prefix = GetPrefix();
Ted Kremenek9f9b1fd2008-09-12 22:49:36 +0000747 if (defined $prefix) { $srcfile =~ s/^\Q$prefix//; }
Ted Kremenek938eef12009-02-17 23:31:05 +0000748 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 +0000749 my $ppfile_clang = $ppfile;
750 $ppfile_clang =~ s/[.](.+)$/.clang.$1/;
Ted Kremenek938eef12009-02-17 23:31:05 +0000751 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 +0000752 }
Ted Kremenek938eef12009-02-17 23:31:05 +0000753 print OUT "</table>\n";
754 }
755 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 +0000756 }
757
Ted Kremenek991c54b2008-08-08 20:46:42 +0000758 print OUT "</body></html>\n";
Ted Kremenek5744dc22008-04-02 18:03:36 +0000759 close(OUT);
Ted Kremenek3ce12072008-09-22 17:50:47 +0000760 CopyFiles($Dir);
Ted Kremenek20161e92008-07-15 20:18:21 +0000761
762 # Make sure $Dir and $BaseDir are world readable/executable.
763 system("chmod", "755", $Dir);
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000764 if (defined $BaseDir) { system("chmod", "755", $BaseDir); }
Ted Kremenek20161e92008-07-15 20:18:21 +0000765
Ted Kremenek23cfca32008-06-16 22:40:14 +0000766 my $Num = scalar(@Index);
Ted Kremenek150c2122008-07-11 19:15:05 +0000767 Diag("$Num bugs found.\n");
768 if ($Num > 0 && -r "$Dir/index.html") {
Ted Kremenek5950b3f2008-09-22 06:47:01 +0000769 Diag("Run 'scan-view $Dir' to examine bug reports.\n");
Ted Kremenek150c2122008-07-11 19:15:05 +0000770 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000771
Ted Kremenek938eef12009-02-17 23:31:05 +0000772 DiagCrashes($Dir) if (scalar @failures || scalar @attributes_ignored);
Ted Kremenek991c54b2008-08-08 20:46:42 +0000773
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000774 return $Num;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000775}
776
777##----------------------------------------------------------------------------##
Ted Kremenekdab11102008-04-02 04:43:42 +0000778# RunBuildCommand - Run the build command.
779##----------------------------------------------------------------------------##
780
Ted Kremenek6b628982008-04-30 23:47:12 +0000781sub AddIfNotPresent {
782 my $Args = shift;
783 my $Arg = shift;
784 my $found = 0;
785
786 foreach my $k (@$Args) {
787 if ($k eq $Arg) {
788 $found = 1;
789 last;
790 }
791 }
792
793 if ($found == 0) {
794 push @$Args, $Arg;
795 }
796}
797
Ted Kremenekdab11102008-04-02 04:43:42 +0000798sub RunBuildCommand {
799
800 my $Args = shift;
Ted Kremenek7442ca62008-04-02 16:04:51 +0000801 my $IgnoreErrors = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000802 my $Cmd = $Args->[0];
Ted Kremenek6195c372008-06-02 21:52:47 +0000803 my $CCAnalyzer = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000804
Ted Kremenek3301cb12008-06-30 18:18:16 +0000805 # Get only the part of the command after the last '/'.
806 if ($Cmd =~ /\/([^\/]+)$/) {
807 $Cmd = $1;
808 }
809
Ted Kremenek92548fe2008-11-19 01:46:21 +0000810 if ($Cmd =~ /(.*\/?gcc[^\/]*$)/ or
811 $Cmd =~ /(.*\/?cc[^\/]*$)/ or
812 $Cmd =~ /(.*\/?llvm-gcc[^\/]*$)/ or
813 $Cmd =~ /(.*\/?ccc-analyzer[^\/]*$)/) {
814
815 if (!($Cmd =~ /ccc-analyzer/) and !defined $ENV{"CCC_CC"}) {
816 $ENV{"CCC_CC"} = $1;
817 }
818
Ted Kremenekdab11102008-04-02 04:43:42 +0000819 shift @$Args;
Ted Kremenek6195c372008-06-02 21:52:47 +0000820 unshift @$Args, $CCAnalyzer;
Ted Kremenekdab11102008-04-02 04:43:42 +0000821 }
Ted Kremenek7442ca62008-04-02 16:04:51 +0000822 elsif ($IgnoreErrors) {
823 if ($Cmd eq "make" or $Cmd eq "gmake") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000824 AddIfNotPresent($Args,"-k");
Ted Kremenek8912b542008-05-13 21:28:02 +0000825 AddIfNotPresent($Args,"-i");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000826 }
827 elsif ($Cmd eq "xcodebuild") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000828 AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000829 }
Ted Kremenek6b628982008-04-30 23:47:12 +0000830 }
831
Ted Kremenek6b628982008-04-30 23:47:12 +0000832 if ($Cmd eq "xcodebuild") {
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000833 # Disable distributed builds for xcodebuild.
Ted Kremenek6b628982008-04-30 23:47:12 +0000834 AddIfNotPresent($Args,"-nodistribute");
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000835
836 # Disable PCH files until clang supports them.
837 AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO");
Ted Kremenek915e9722008-05-27 23:18:07 +0000838
839 # When 'CC' is set, xcodebuild uses it to do all linking, even if we are
840 # linking C++ object files. Set 'LDPLUSPLUS' so that xcodebuild uses 'g++'
841 # when linking such files.
Ted Kremenek95aa1052008-09-04 17:52:41 +0000842 die if (!defined $CXX);
843 my $LDPLUSPLUS = `which $CXX`;
Ted Kremenek915e9722008-05-27 23:18:07 +0000844 $LDPLUSPLUS =~ s/\015?\012//; # strip newlines
845 $ENV{'LDPLUSPLUS'} = $LDPLUSPLUS;
Ted Kremenek6b628982008-04-30 23:47:12 +0000846 }
Ted Kremenekdab11102008-04-02 04:43:42 +0000847
Ted Kremenek5a4ddaf2008-08-25 20:10:45 +0000848 return (system(@$Args) >> 8);
Ted Kremenekdab11102008-04-02 04:43:42 +0000849}
850
Ted Kremenekdab11102008-04-02 04:43:42 +0000851##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000852# DisplayHelp - Utility function to display all help options.
853##----------------------------------------------------------------------------##
854
Sam Bishopa0e22662008-04-02 03:35:43 +0000855sub DisplayHelp {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000856
Ted Kremenek5744dc22008-04-02 18:03:36 +0000857print <<ENDTEXT;
Sam Bishopa0e22662008-04-02 03:35:43 +0000858USAGE: $Prog [options] <build command> [build options]
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000859
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000860ENDTEXT
861
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000862 if (defined $BuildName) {
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000863 print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n";
864 }
865
866print <<ENDTEXT;
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000867OPTIONS:
868
Ted Kremeneke15fa272008-10-13 21:46:42 +0000869 -analyze-headers - Also analyze functions in #included files.
870
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000871 -o - Target directory for HTML report files. Subdirectories
Sam Bishopa0e22662008-04-02 03:35:43 +0000872 will be created as needed to represent separate "runs" of
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000873 the analyzer. If this option is not specified, a directory
Ted Kremenekffda0b42008-10-31 05:48:42 +0000874 is created in /tmp (TMPDIR on Mac OS X) to store the reports.
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000875
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000876 -h - Display this message.
877 --help
Ted Kremenek1262fc42008-05-14 20:10:33 +0000878
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000879 -k - Add a "keep on going" option to the specified build command.
880 --keep-going This option currently supports make and xcodebuild.
Ted Kremenekf02e8db2008-04-02 16:41:25 +0000881 This is a convenience option; one can specify this
882 behavior directly using build options.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000883
Ted Kremenek7cba1122008-09-22 01:35:58 +0000884 --html-title [title] - Specify the title used on generated HTML pages.
885 --html-title=[title] If not specified, a default title will be used.
886
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000887 -plist - By default the output of scan-build is a set of HTML files.
888 This option outputs the results as a set of .plist files.
889
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000890 --status-bugs - By default, the exit status of $Prog is the same as the
891 executed build command. Specifying this option causes the
892 exit status of $Prog to be 1 if it found potential bugs
893 and 0 otherwise.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000894
Ted Kremenek386c6932008-09-03 17:59:35 +0000895 --use-cc [compiler path] - By default, $Prog uses 'gcc' to compile and link
896 --use-cc=[compiler path] your C and Objective-C code. Use this option
897 to specify an alternate compiler.
898
899 --use-c++ [compiler path] - By default, $Prog uses 'g++' to compile and link
900 --use-c++=[compiler path] your C++ and Objective-C++ code. Use this option
901 to specify an alternate compiler.
Ted Kremenekf17ef3c2008-08-21 21:47:09 +0000902
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000903 -v - Verbose output from $Prog and the analyzer.
Ted Kremenek386c6932008-09-03 17:59:35 +0000904 A second and third '-v' increases verbosity.
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000905
906 -V - View analysis results in a web browser when the build
907 --view completes.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000908
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000909ADVANCED OPTIONS:
910
911 -constraints [model] - Specify the contraint model used by the analyzer.
912 By default the 'basic' model is used. 'range' adds
913 experimental range tracking for program values.
914
915 -store [model] - Specify the store model used by the analyzer. By default,
916 the 'basic' store model is used. 'region' specifies a field-
917 sensitive store model. Be warned that the 'region' model
918 is still in very early testing phase and may often crash.
Ted Kremenekb7770c02008-07-15 17:06:13 +0000919
Ted Kremenek386c6932008-09-03 17:59:35 +0000920AVAILABLE ANALYSES (multiple analyses may be specified):
Ted Kremenekd52e4252008-08-25 20:45:07 +0000921
922ENDTEXT
Ted Kremenekb7770c02008-07-15 17:06:13 +0000923
924 foreach my $Analysis (sort keys %AvailableAnalyses) {
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000925 if (defined $AnalysesDefaultEnabled{$Analysis}) {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000926 print " (+)";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000927 }
928 else {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000929 print " ";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000930 }
931
932 print " $Analysis $AvailableAnalyses{$Analysis}\n";
933 }
934
935print <<ENDTEXT
936
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000937 NOTE: "(+)" indicates that an analysis is enabled by default unless one
938 or more analysis options are specified
Ted Kremenekb7770c02008-07-15 17:06:13 +0000939
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000940BUILD OPTIONS
941
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000942 You can specify any build option acceptable to the build command.
Ted Kremenek39eefde2008-04-02 16:47:27 +0000943
Ted Kremenek5744dc22008-04-02 18:03:36 +0000944EXAMPLE
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000945
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000946 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000947
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000948 The above example causes analysis reports to be deposited into
949 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
950 A different subdirectory is created each time $Prog analyzes a project.
951 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000952
953ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000954}
955
956##----------------------------------------------------------------------------##
Ted Kremenek7cba1122008-09-22 01:35:58 +0000957# HtmlEscape - HTML entity encode characters that are special in HTML
958##----------------------------------------------------------------------------##
959
960sub HtmlEscape {
961 # copy argument to new variable so we don't clobber the original
962 my $arg = shift || '';
963 my $tmp = $arg;
Ted Kremenek87f8de72008-11-03 07:44:16 +0000964 $tmp =~ s/&/&amp;/g;
965 $tmp =~ s/</&lt;/g;
966 $tmp =~ s/>/&gt;/g;
Ted Kremenek7cba1122008-09-22 01:35:58 +0000967 return $tmp;
968}
969
970##----------------------------------------------------------------------------##
971# ShellEscape - backslash escape characters that are special to the shell
972##----------------------------------------------------------------------------##
973
974sub ShellEscape {
975 # copy argument to new variable so we don't clobber the original
976 my $arg = shift || '';
Ted Kremenek87f8de72008-11-03 07:44:16 +0000977 if ($arg =~ /["\s]/) { return "'" . $arg . "'"; }
978 return $arg;
Ted Kremenek7cba1122008-09-22 01:35:58 +0000979}
980
981##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000982# Process command-line arguments.
983##----------------------------------------------------------------------------##
984
Ted Kremeneke15fa272008-10-13 21:46:42 +0000985my $AnalyzeHeaders = 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000986my $HtmlDir; # Parent directory to store HTML files.
987my $IgnoreErrors = 0; # Ignore build errors.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000988my $ViewResults = 0; # View results when the build terminates.
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000989my $ExitStatusFoundBugs = 0; # Exit status reflects whether bugs were found
Ted Kremenekb7770c02008-07-15 17:06:13 +0000990my @AnalysesToRun;
Zhongxing Xu07c37672008-10-27 14:26:32 +0000991my $StoreModel;
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000992my $ConstraintsModel;
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000993my $OutputFormat;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000994
995if (!@ARGV) {
996 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000997 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000998}
999
1000while (@ARGV) {
1001
1002 # Scan for options we recognize.
1003
1004 my $arg = $ARGV[0];
1005
Sam Bishop2f2418e2008-04-03 14:29:47 +00001006 if ($arg eq "-h" or $arg eq "--help") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001007 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +00001008 exit 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001009 }
1010
Ted Kremeneke15fa272008-10-13 21:46:42 +00001011 if ($arg eq '-analyze-headers') {
1012 shift @ARGV;
1013 $AnalyzeHeaders = 1;
1014 next;
1015 }
1016
Ted Kremenekfc1d3402008-08-04 18:15:26 +00001017 if (defined $AvailableAnalyses{$arg}) {
Ted Kremenek1262fc42008-05-14 20:10:33 +00001018 shift @ARGV;
Ted Kremenekb7770c02008-07-15 17:06:13 +00001019 push @AnalysesToRun, $arg;
Ted Kremenek1262fc42008-05-14 20:10:33 +00001020 next;
1021 }
1022
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001023 if ($arg eq "-o") {
1024 shift @ARGV;
1025
1026 if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +00001027 DieDiag("'-o' option requires a target directory name.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001028 }
1029
1030 $HtmlDir = shift @ARGV;
1031 next;
1032 }
Ted Kremenek7cba1122008-09-22 01:35:58 +00001033
1034 if ($arg =~ /^--html-title(=(.+))?$/) {
1035 shift @ARGV;
1036
1037 if ($2 eq '') {
1038 if (!@ARGV) {
1039 DieDiag("'--html-title' option requires a string.\n");
1040 }
1041
1042 $HtmlTitle = shift @ARGV;
1043 } else {
1044 $HtmlTitle = $2;
1045 }
1046
1047 next;
1048 }
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001049
Ted Kremenek2b74ab62008-04-01 21:22:03 +00001050 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001051 shift @ARGV;
1052 $IgnoreErrors = 1;
1053 next;
1054 }
1055
Ted Kremenekf17ef3c2008-08-21 21:47:09 +00001056 if ($arg =~ /^--use-cc(=(.+))?$/) {
1057 shift @ARGV;
1058 my $cc;
1059
1060 if ($2 eq "") {
1061 if (!@ARGV) {
1062 DieDiag("'--use-cc' option requires a compiler executable name.\n");
1063 }
1064 $cc = shift @ARGV;
1065 }
1066 else {
1067 $cc = $2;
1068 }
1069
1070 $ENV{"CCC_CC"} = $cc;
1071 next;
1072 }
1073
Ted Kremenek7cba1122008-09-22 01:35:58 +00001074 if ($arg =~ /^--use-c\+\+(=(.+))?$/) {
Ted Kremenek386c6932008-09-03 17:59:35 +00001075 shift @ARGV;
1076
1077 if ($2 eq "") {
1078 if (!@ARGV) {
1079 DieDiag("'--use-c++' option requires a compiler executable name.\n");
1080 }
1081 $CXX = shift @ARGV;
1082 }
1083 else {
1084 $CXX = $2;
1085 }
1086 next;
1087 }
1088
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001089 if ($arg eq "-v") {
1090 shift @ARGV;
1091 $Verbose++;
1092 next;
1093 }
1094
Ted Kremenek7f8a3252008-04-02 18:42:49 +00001095 if ($arg eq "-V" or $arg eq "--view") {
1096 shift @ARGV;
1097 $ViewResults = 1;
1098 next;
1099 }
1100
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001101 if ($arg eq "--status-bugs") {
1102 shift @ARGV;
1103 $ExitStatusFoundBugs = 1;
1104 next;
1105 }
Zhongxing Xu07c37672008-10-27 14:26:32 +00001106
1107 if ($arg eq "-store") {
1108 shift @ARGV;
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +00001109 $StoreModel = shift @ARGV;
1110 next;
1111 }
1112
1113 if ($arg eq "-constraints") {
1114 shift @ARGV;
1115 $ConstraintsModel = shift @ARGV;
Zhongxing Xu07c37672008-10-27 14:26:32 +00001116 next;
1117 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001118
Ted Kremenekdb4f5f22008-11-04 00:02:53 +00001119 if ($arg eq "-plist") {
1120 shift @ARGV;
1121 $OutputFormat = "plist";
1122 next;
1123 }
1124
Ted Kremenek23cfca32008-06-16 22:40:14 +00001125 DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/);
Ted Kremenek0062ad42008-04-02 16:35:01 +00001126
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001127 last;
1128}
1129
1130if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +00001131 Diag("No build command specified.\n\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001132 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +00001133 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001134}
1135
Ted Kremenek7cba1122008-09-22 01:35:58 +00001136$CmdArgs = HtmlEscape(join(' ', map(ShellEscape($_), @ARGV)));
1137$HtmlTitle = "${CurrentDirSuffix} - scan-build results"
1138 unless (defined($HtmlTitle));
Ted Kremenek386c6932008-09-03 17:59:35 +00001139
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001140# Determine the output directory for the HTML reports.
Ted Kremenek684bb092008-04-18 15:18:20 +00001141my $BaseDir = $HtmlDir;
Sam Bishopa0e22662008-04-02 03:35:43 +00001142$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001143
1144# Set the appropriate environment variables.
Sam Bishopa0e22662008-04-02 03:35:43 +00001145SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001146
Ted Kremeneke15fa272008-10-13 21:46:42 +00001147my $Cmd = Cwd::realpath("$RealBin/ccc-analyzer");
Ted Kremenek0b6c1532008-04-08 20:22:12 +00001148
Ted Kremenek23cfca32008-06-16 22:40:14 +00001149DieDiag("Executable 'ccc-analyzer' does not exist at '$Cmd'\n")
Ted Kremenek0b6c1532008-04-08 20:22:12 +00001150 if (! -x $Cmd);
Ted Kremenekf22eacb2008-04-18 22:00:56 +00001151
Ted Kremenekb7770c02008-07-15 17:06:13 +00001152if (! -x $ClangSB) {
1153 Diag("'clang' executable not found in '$RealBin'.\n");
1154 Diag("Using 'clang' from path.\n");
Ted Kremenekf22eacb2008-04-18 22:00:56 +00001155}
Ted Kremenek0b6c1532008-04-08 20:22:12 +00001156
Ted Kremenek95aa1052008-09-04 17:52:41 +00001157if (defined $CXX) {
1158 $ENV{'CXX'} = $CXX;
1159}
1160else {
1161 $CXX = 'g++'; # This variable is used by other parts of scan-build
1162 # that need to know a default C++ compiler to fall back to.
1163}
1164
Ted Kremenek4f4b17d2008-04-03 20:08:18 +00001165$ENV{'CC'} = $Cmd;
Ted Kremenekf22eacb2008-04-18 22:00:56 +00001166$ENV{'CLANG'} = $Clang;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001167
1168if ($Verbose >= 2) {
1169 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
1170}
1171
Ted Kremeneka9525c92008-05-12 22:07:14 +00001172if ($Verbose >= 3) {
1173 $ENV{'CCC_ANALYZER_LOG'} = 1;
1174}
1175
Ted Kremenek90125992008-07-15 23:41:32 +00001176if (scalar(@AnalysesToRun) == 0) {
1177 foreach my $key (keys %AnalysesDefaultEnabled) {
1178 push @AnalysesToRun,$key;
1179 }
Ted Kremenek01006782008-07-02 23:16:10 +00001180}
Ted Kremenek1262fc42008-05-14 20:10:33 +00001181
Ted Kremeneke15fa272008-10-13 21:46:42 +00001182if ($AnalyzeHeaders) {
1183 push @AnalysesToRun,"-analyzer-opt-analyze-headers";
1184}
1185
Ted Kremenek90125992008-07-15 23:41:32 +00001186$ENV{'CCC_ANALYZER_ANALYSIS'} = join ' ',@AnalysesToRun;
1187
Zhongxing Xu3cab2b12008-11-02 10:58:16 +00001188if (defined $StoreModel) {
Zhongxing Xu07c37672008-10-27 14:26:32 +00001189 $ENV{'CCC_ANALYZER_STORE_MODEL'} = $StoreModel;
1190}
1191
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +00001192if (defined $ConstraintsModel) {
1193 $ENV{'CCC_ANALYZER_CONSTRAINTS_MODEL'} = $ConstraintsModel;
1194}
1195
Ted Kremenekdb4f5f22008-11-04 00:02:53 +00001196if (defined $OutputFormat) {
1197 $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'} = $OutputFormat;
1198}
1199
1200
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001201# Run the build.
Ted Kremenek5656a982008-07-15 17:09:28 +00001202my $ExitStatus = RunBuildCommand(\@ARGV, $IgnoreErrors, $Cmd);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001203
Ted Kremenek655aba72008-11-04 00:22:12 +00001204if (defined $OutputFormat and $OutputFormat eq "plist") {
Ted Kremenek50534dc2008-09-22 17:38:23 +00001205 Diag "Analysis run complete.\n";
Ted Kremenekdb4f5f22008-11-04 00:02:53 +00001206 Diag "Analysis results (plist files) deposited in '$HtmlDir'\n";
Ted Kremenek7f8a3252008-04-02 18:42:49 +00001207}
Ted Kremenekdb4f5f22008-11-04 00:02:53 +00001208else {
1209 # Postprocess the HTML directory.
1210 my $NumBugs = Postprocess($HtmlDir, $BaseDir);
Ted Kremenek5656a982008-07-15 17:09:28 +00001211
Ted Kremenekdb4f5f22008-11-04 00:02:53 +00001212 if ($ViewResults and -r "$HtmlDir/index.html") {
1213 Diag "Analysis run complete.\n";
1214 Diag "Viewing analysis results in '$HtmlDir' using scan-view.\n";
1215 my $ScanView = Cwd::realpath("$RealBin/scan-view");
1216 if (! -x $ScanView) { $ScanView = "scan-view"; }
1217 exec $ScanView, "$HtmlDir";
1218 }
1219
1220 if ($ExitStatusFoundBugs) {
1221 exit 1 if ($NumBugs > 0);
1222 exit 0;
1223 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001224}
1225
Ted Kremenek5656a982008-07-15 17:09:28 +00001226exit $ExitStatus;
1227