blob: c070ed33d14d609344687288561201a0d31766e6 [file] [log] [blame]
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001#!/usr/bin/env perl
2#
3# The LLVM Compiler Infrastructure
4#
5# This file is distributed under the University of Illinois Open Source
6# License. See LICENSE.TXT for details.
7#
8##===----------------------------------------------------------------------===##
9#
10# A script designed to wrap a build so that all calls to gcc are intercepted
11# and piped to the static analyzer.
12#
13##===----------------------------------------------------------------------===##
14
15use strict;
16use warnings;
Ted Kremenek22d6a632008-04-02 20:43:36 +000017use FindBin qw($RealBin);
Ted Kremeneka6e24812008-04-19 18:05:48 +000018use Digest::MD5;
Ted Kremenek7a4648d2008-05-02 22:04:53 +000019use File::Basename;
Ted Kremenek23cfca32008-06-16 22:40:14 +000020use Term::ANSIColor;
21use Term::ANSIColor qw(:constants);
Ted Kremenek7cba1122008-09-22 01:35:58 +000022use Cwd;
23use Sys::Hostname;
24use File::Basename;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000025
26my $Verbose = 0; # Verbose output from this script.
27my $Prog = "scan-build";
Ted Kremenekf4cdf412008-05-23 18:17:05 +000028my $BuildName;
29my $BuildDate;
Ted Kremenek95aa1052008-09-04 17:52:41 +000030my $CXX; # Leave undefined initially.
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000031
Ted Kremenek0e689382008-09-11 18:17:51 +000032my $TERM = $ENV{'TERM'};
33my $UseColor = (defined $TERM and $TERM eq 'xterm-color' and -t STDOUT
34 and defined $ENV{'SCAN_BUILD_COLOR'});
Ted Kremenek23cfca32008-06-16 22:40:14 +000035
Ted Kremenek7cba1122008-09-22 01:35:58 +000036my $UserName = HtmlEscape(getpwuid($<) || 'unknown');
37my $HostName = HtmlEscape(hostname() || 'unknown');
38my $CurrentDir = HtmlEscape(getcwd());
39my $CurrentDirSuffix = basename($CurrentDir);
40
41my $CmdArgs;
42
43my $HtmlTitle;
44
45my $Date = localtime();
46
Ted Kremenekb7770c02008-07-15 17:06:13 +000047##----------------------------------------------------------------------------##
48# Diagnostics
49##----------------------------------------------------------------------------##
50
Ted Kremenek23cfca32008-06-16 22:40:14 +000051sub Diag {
52 if ($UseColor) {
53 print BOLD, MAGENTA "$Prog: @_";
54 print RESET;
55 }
56 else {
57 print "$Prog: @_";
58 }
59}
60
Ted Kremenek991c54b2008-08-08 20:46:42 +000061sub DiagCrashes {
62 my $Dir = shift;
63 Diag ("The analyzer crashed on some source files.\n");
Ted Kremenek386c6932008-09-03 17:59:35 +000064 Diag ("Preprocessed versions of crashed files were deposited in '$Dir/crashes'.\n");
Ted Kremenek991c54b2008-08-08 20:46:42 +000065 Diag ("Please consider submitting a bug report using these files:\n");
66 Diag (" http://clang.llvm.org/StaticAnalysisUsage.html#filingbugs\n")
67}
68
Ted Kremenek23cfca32008-06-16 22:40:14 +000069sub DieDiag {
70 if ($UseColor) {
71 print BOLD, RED "$Prog: ";
72 print RESET, RED @_;
73 print RESET;
74 }
75 else {
76 print "$Prog: ", @_;
77 }
78 exit(0);
79}
80
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000081##----------------------------------------------------------------------------##
Ted Kremenekb7770c02008-07-15 17:06:13 +000082# Some initial preprocessing of Clang options.
83##----------------------------------------------------------------------------##
84
85my $ClangSB = "$RealBin/clang";
86my $Clang = $ClangSB;
87
88if (! -x $ClangSB) {
89 $Clang = "clang";
90}
91
92my %AvailableAnalyses;
93
94# Query clang for analysis options.
Ted Kremenek63c20172008-08-04 17:34:06 +000095open(PIPE, "-|", $Clang, "--help") or
Ted Kremenekb7770c02008-07-15 17:06:13 +000096 DieDiag("Cannot execute '$Clang'");
Ted Kremenek63c20172008-08-04 17:34:06 +000097
Ted Kremenekb7770c02008-07-15 17:06:13 +000098my $FoundAnalysis = 0;
99
100while(<PIPE>) {
101 if ($FoundAnalysis == 0) {
102 if (/Available Source Code Analyses/) {
103 $FoundAnalysis = 1;
104 }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000105
Ted Kremenekb7770c02008-07-15 17:06:13 +0000106 next;
107 }
108
109 if (/^\s\s\s\s([^\s]+)\s(.+)$/) {
110 next if ($1 =~ /-dump/ or $1 =~ /-view/
111 or $1 =~ /-checker-simple/ or $1 =~ /-warn-uninit/);
112
113 $AvailableAnalyses{$1} = $2;
114 next;
115 }
116
117 last;
118}
119
120close (PIPE);
121
122my %AnalysesDefaultEnabled = (
123 '-warn-dead-stores' => 1,
124 '-checker-cfref' => 1,
Ted Kremenek90125992008-07-15 23:41:32 +0000125 '-warn-objc-methodsigs' => 1,
Ted Kremenekbde3a052008-07-25 20:35:01 +0000126 '-warn-objc-missing-dealloc' => 1,
Ted Kremenek5d443492008-09-18 06:34:16 +0000127 '-warn-objc-unused-ivars' => 1,
Ted Kremenekb7770c02008-07-15 17:06:13 +0000128);
129
130##----------------------------------------------------------------------------##
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000131# GetHTMLRunDir - Construct an HTML directory name for the current sub-run.
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000132##----------------------------------------------------------------------------##
133
Sam Bishopa0e22662008-04-02 03:35:43 +0000134sub GetHTMLRunDir {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000135
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000136 die "Not enough arguments." if (@_ == 0);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000137 my $Dir = shift @_;
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000138
139 my $TmpMode = 0;
140 if (!defined $Dir) {
141 $Dir = "/tmp";
142 $TmpMode = 1;
143 }
144
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000145 # Get current date and time.
146
147 my @CurrentTime = localtime();
148
149 my $year = $CurrentTime[5] + 1900;
150 my $day = $CurrentTime[3];
151 my $month = $CurrentTime[4] + 1;
152
Ted Kremenek9d7405f2008-05-14 17:23:56 +0000153 my $DateString = sprintf("%d-%02d-%02d", $year, $month, $day);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000154
155 # Determine the run number.
156
157 my $RunNumber;
158
159 if (-d $Dir) {
160
161 if (! -r $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000162 DieDiag("directory '$Dir' exists but is not readable.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000163 }
164
165 # Iterate over all files in the specified directory.
166
167 my $max = 0;
168
169 opendir(DIR, $Dir);
Ted Kremenek29da6c52008-08-07 17:57:34 +0000170 my @FILES = grep { -d "$Dir/$_" } readdir(DIR);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000171 closedir(DIR);
172
173 foreach my $f (@FILES) {
174
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000175 # Strip the prefix '$Prog-' if we are dumping files to /tmp.
176 if ($TmpMode) {
177 next if (!($f =~ /^$Prog-(.+)/));
178 $f = $1;
179 }
180
Ted Kremenekebb74132008-09-21 06:58:09 +0000181
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000182 my @x = split/-/, $f;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000183 next if (scalar(@x) != 4);
184 next if ($x[0] != $year);
185 next if ($x[1] != $month);
186 next if ($x[2] != $day);
187
188 if ($x[3] > $max) {
189 $max = $x[3];
190 }
191 }
192
193 $RunNumber = $max + 1;
194 }
195 else {
196
197 if (-x $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000198 DieDiag("'$Dir' exists but is not a directory.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000199 }
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000200
201 if ($TmpMode) {
202 DieDiag("The directory '/tmp' does not exist or cannot be accessed.");
203 }
204
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000205 # $Dir does not exist. It will be automatically created by the
206 # clang driver. Set the run number to 1.
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000207
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000208 $RunNumber = 1;
209 }
210
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000211 die "RunNumber must be defined!" if (!defined $RunNumber);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000212
213 # Append the run number.
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000214 my $NewDir;
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000215 if ($TmpMode) {
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000216 $NewDir = "$Dir/$Prog-$DateString-$RunNumber";
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000217 }
218 else {
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000219 $NewDir = "$Dir/$DateString-$RunNumber";
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000220 }
Ted Kremenekfc0898a2008-09-04 23:56:36 +0000221 system 'mkdir','-p',$NewDir;
222 return $NewDir;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000223}
224
Sam Bishopa0e22662008-04-02 03:35:43 +0000225sub SetHtmlEnv {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000226
227 die "Wrong number of arguments." if (scalar(@_) != 2);
228
229 my $Args = shift;
230 my $Dir = shift;
231
232 die "No build command." if (scalar(@$Args) == 0);
233
234 my $Cmd = $$Args[0];
235
236 if ($Cmd =~ /configure/) {
237 return;
238 }
239
240 if ($Verbose) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000241 Diag("Emitting reports for this run to '$Dir'.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000242 }
243
244 $ENV{'CCC_ANALYZER_HTML'} = $Dir;
245}
246
247##----------------------------------------------------------------------------##
Ted Kremenek57cf4462008-04-18 15:09:30 +0000248# ComputeDigest - Compute a digest of the specified file.
249##----------------------------------------------------------------------------##
250
251sub ComputeDigest {
252 my $FName = shift;
Ted Kremenek23cfca32008-06-16 22:40:14 +0000253 DieDiag("Cannot read $FName to compute Digest.\n") if (! -r $FName);
Ted Kremeneka6e24812008-04-19 18:05:48 +0000254
255 # Use Digest::MD5. We don't have to be cryptographically secure. We're
Ted Kremenek7ea02e62008-04-19 18:07:44 +0000256 # just looking for duplicate files that come from a non-malicious source.
257 # We use Digest::MD5 because it is a standard Perl module that should
Ted Kremenek63c20172008-08-04 17:34:06 +0000258 # come bundled on most systems.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000259 open(FILE, $FName) or DieDiag("Cannot open $FName when computing Digest.\n");
Ted Kremeneka6e24812008-04-19 18:05:48 +0000260 binmode FILE;
261 my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest;
262 close(FILE);
263
Ted Kremenek63c20172008-08-04 17:34:06 +0000264 # Return the digest.
Ted Kremeneka6e24812008-04-19 18:05:48 +0000265 return $Result;
Ted Kremenek57cf4462008-04-18 15:09:30 +0000266}
267
268##----------------------------------------------------------------------------##
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000269# UpdatePrefix - Compute the common prefix of files.
270##----------------------------------------------------------------------------##
271
272my $Prefix;
273
274sub UpdatePrefix {
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000275 my $x = shift;
276 my $y = basename($x);
277 $x =~ s/\Q$y\E$//;
278
279 # Ignore /usr, /Library, /System, /Developer
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000280 return if ( $x =~ /^\/usr/ or $x =~ /^\/Library/
281 or $x =~ /^\/System/ or $x =~ /^\/Developer/);
282
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
351 my $BugDesc = "";
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 Kremenek5744dc22008-04-02 18:03:36 +0000362 if (/<!-- BUGDESC (.*) -->$/) {
363 $BugDesc = $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 (.*) -->$/) {
367 $BugFile = $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 Kremenek50534dc2008-09-22 17:38:23 +0000391 push @$Index,[ $FName, $BugCategory, $BugDesc, $BugFile, $BugLine ];
Ted Kremenek22d6a632008-04-02 20:43:36 +0000392}
393
394##----------------------------------------------------------------------------##
Ted Kremenek3ce12072008-09-22 17:50:47 +0000395# CopyFiles - Copy resource files to target directory.
Ted Kremenek22d6a632008-04-02 20:43:36 +0000396##----------------------------------------------------------------------------##
397
Ted Kremenek3ce12072008-09-22 17:50:47 +0000398sub CopyFiles {
Ted Kremenek22d6a632008-04-02 20:43:36 +0000399
400 my $Dir = shift;
401
Ted Kremenek23cfca32008-06-16 22:40:14 +0000402 DieDiag("Cannot find 'sorttable.js'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000403 if (! -r "$RealBin/sorttable.js");
404
Ted Kremenek20161e92008-07-15 20:18:21 +0000405 system ("cp", "$RealBin/sorttable.js", "$Dir");
Ted Kremenek22d6a632008-04-02 20:43:36 +0000406
Ted Kremenek23cfca32008-06-16 22:40:14 +0000407 DieDiag("Could not copy 'sorttable.js' to '$Dir'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000408 if (! -r "$Dir/sorttable.js");
Ted Kremenek3ce12072008-09-22 17:50:47 +0000409
410 DieDiag("Cannot find 'scanview.css'.\n")
411 if (! -r "$RealBin/scanview.css");
412
413 system ("cp", "$RealBin/scanview.css", "$Dir");
414
415 DieDiag("Could not copy 'scanview.css' to '$Dir'.\n")
416 if (! -r "$Dir/scanview.css");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000417}
418
419##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000420# Postprocess - Postprocess the results of an analysis scan.
421##----------------------------------------------------------------------------##
422
Sam Bishopa0e22662008-04-02 03:35:43 +0000423sub Postprocess {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000424
425 my $Dir = shift;
Ted Kremenek684bb092008-04-18 15:18:20 +0000426 my $BaseDir = shift;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000427
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000428 die "No directory specified." if (!defined $Dir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000429
430 if (! -d $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000431 Diag("No bugs found.\n");
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000432 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000433 }
434
435 opendir(DIR, $Dir);
Ted Kremenek991c54b2008-08-08 20:46:42 +0000436 my $Crashes = 0;
437 my @files = grep { if ($_ eq "crashes") { $Crashes++; }
438 /^report-.*\.html$/; } readdir(DIR);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000439 closedir(DIR);
440
Ted Kremenek991c54b2008-08-08 20:46:42 +0000441 if (scalar(@files) == 0 and $Crashes == 0) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000442 Diag("Removing directory '$Dir' because it contains no reports.\n");
Ted Kremenek20161e92008-07-15 20:18:21 +0000443 system ("rm", "-fR", $Dir);
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000444 return 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000445 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000446
Ted Kremenek991c54b2008-08-08 20:46:42 +0000447 # Scan each report file and build an index.
448 my @Index;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000449 foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); }
450
Ted Kremenekd52e4252008-08-25 20:45:07 +0000451 # Scan the crashes directory and use the information in the .info files
452 # to update the common prefix directory.
453 if (-d "$Dir/crashes") {
454 opendir(DIR, "$Dir/crashes");
455 my @files = grep { /[.]info$/; } readdir(DIR);
456 closedir(DIR);
457 foreach my $file (@files) {
458 open IN, "$Dir/crashes/$file" or DieDiag("cannot open $file\n");
459 my $Path = <IN>;
460 if (defined $Path) { UpdatePrefix($Path); }
461 close IN;
462 }
463 }
464
Ted Kremenek63c20172008-08-04 17:34:06 +0000465 # Generate an index.html file.
466 my $FName = "$Dir/index.html";
467 open(OUT, ">", $FName) or DieDiag("Cannot create file '$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000468
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000469 # Print out the header.
470
Ted Kremenek5744dc22008-04-02 18:03:36 +0000471print OUT <<ENDTEXT;
472<html>
473<head>
Ted Kremenek7cba1122008-09-22 01:35:58 +0000474<title>${HtmlTitle}</title>
Ted Kremenek3ce12072008-09-22 17:50:47 +0000475<link type="text/css" rel="stylesheet" href="/scanview.css"/>
Ted Kremenek12a467f2008-09-21 20:10:46 +0000476<script language='javascript' type="text/javascript">
477if (document.styleSheets && RegExp(" AppleWebKit/").test(navigator.userAgent))
478{
479 var sheet = document.styleSheets[0];
480 if (sheet) {
481 var rules = sheet.cssRules;
482 if (rules) {
Ted Kremenek3ce12072008-09-22 17:50:47 +0000483 sheet.insertRule("td.Button a { white-space: nowrap; -webkit-appearance:square-button; padding-left:1em; padding-right:1em; padding-top:0.5ex; padding-bottom:0.5ex; text-decoration:none; color:black }", rules.length);
Ted Kremenek12a467f2008-09-21 20:10:46 +0000484 }
485 }
486}
487</script>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000488<script src="sorttable.js"></script>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000489<script language='javascript' type="text/javascript">
490function SetDisplay(RowClass, DisplayVal)
491{
492 var Rows = document.getElementsByTagName("tr");
493 for ( var i = 0 ; i < Rows.length; ++i ) {
494 if (Rows[i].className == RowClass) {
495 Rows[i].style.display = DisplayVal;
496 }
497 }
498}
Ted Kremenekebb74132008-09-21 06:58:09 +0000499
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000500function ToggleDisplay(CheckButton, ClassName) {
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000501 if (CheckButton.checked) {
502 SetDisplay(ClassName, "");
503 }
504 else {
505 SetDisplay(ClassName, "none");
506 }
507}
508</script>
509</head>
510<body>
Ted Kremenek7cba1122008-09-22 01:35:58 +0000511<h1>${HtmlTitle}</h1>
512
513<table>
514<tr><th>User:</th><td>${UserName}\@${HostName}</td></tr>
515<tr><th>Working Directory:</th><td>${CurrentDir}</td></tr>
516<tr><th>Command Line:</th><td>${CmdArgs}</td></tr>
517<tr><th>Date:</th><td>${Date}</td></tr>
518ENDTEXT
519
520print OUT "<tr><th>Version:</th><td>${BuildName} (${BuildDate})</td></tr>\n"
521 if (defined($BuildName) && defined($BuildDate));
522
523print OUT <<ENDTEXT;
524</table>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000525ENDTEXT
526
Ted Kremenek991c54b2008-08-08 20:46:42 +0000527 if (scalar(@files)) {
528 # Print out the summary table.
529 my %Totals;
Ted Kremenekebb74132008-09-21 06:58:09 +0000530
Ted Kremenek991c54b2008-08-08 20:46:42 +0000531 for my $row ( @Index ) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000532 my $bug_type = ($row->[2]);
533 my $bug_category = ($row->[1]);
534 my $key = "$bug_category:$bug_type";
535
536 if (!defined $Totals{$key}) { $Totals{$key} = [1,$bug_category,$bug_type]; }
537 else { $Totals{$key}->[0]++; }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000538 }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000539
Ted Kremenek7cba1122008-09-22 01:35:58 +0000540 print OUT "<h2>Bug Summary</h2>";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000541
542 if (defined $BuildName) {
543 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 +0000544 }
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000545
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000546print OUT <<ENDTEXT;
Ted Kremenekebb74132008-09-21 06:58:09 +0000547<table>
548<thead><tr><td>Bug Type</td><td>Quantity</td><td class="sorttable_nosort">Display?</td></tr></thead>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000549ENDTEXT
550
Ted Kremenekebb74132008-09-21 06:58:09 +0000551 my $last_category;
552
553 for my $key (
554 sort {
555 my $x = $Totals{$a};
556 my $y = $Totals{$b};
557 my $res = $x->[1] cmp $y->[1];
558 $res = $x->[2] cmp $y->[2] if ($res == 0);
559 $res
560 } keys %Totals )
561 {
562 my $val = $Totals{$key};
563 my $category = $val->[1];
564 if (!defined $last_category or $last_category ne $category) {
565 $last_category = $category;
566 print OUT "<tr><th>$category</th><th colspan=2></th></tr>\n";
567 }
568 my $x = lc $key;
569 $x =~ s/[ ,'":\/()]+/_/g;
570 print OUT "<tr><td class=\"SUMM_DESC\">";
571 print OUT $val->[2];
572 print OUT "</td><td>";
573 print OUT $val->[0];
574 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 +0000575 }
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000576
577 # Print out the table of errors.
578
579print OUT <<ENDTEXT;
580</table>
Ted Kremenek7cba1122008-09-22 01:35:58 +0000581<h2>Reports</h2>
Ted Kremenekebb74132008-09-21 06:58:09 +0000582
583<table class="sortable" style="table-layout:automatic">
584<thead><tr>
585 <td>Bug Group</td>
586 <td class="sorttable_sorted">Bug Type<span id="sorttable_sortfwdind">&nbsp;&#x25BE;</span></td>
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000587 <td>File</td>
Ted Kremenekebb74132008-09-21 06:58:09 +0000588 <td class="Q">Line</td>
Ted Kremenek2645c772008-07-07 16:58:44 +0000589 <td class="sorttable_nosort"></td>
Ted Kremenekebb74132008-09-21 06:58:09 +0000590 <!-- REPORTBUGCOL -->
591</tr></thead>
592<tbody>
Ted Kremenek5744dc22008-04-02 18:03:36 +0000593ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000594
Ted Kremenek991c54b2008-08-08 20:46:42 +0000595 my $prefix = GetPrefix();
596 my $regex;
597 my $InFileRegex;
598 my $InFilePrefix = "File:</td><td>";
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000599
Ted Kremenek991c54b2008-08-08 20:46:42 +0000600 if (defined $prefix) {
601 $regex = qr/^\Q$prefix\E/is;
602 $InFileRegex = qr/\Q$InFilePrefix$prefix\E/is;
603 }
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000604
Ted Kremenekebb74132008-09-21 06:58:09 +0000605 for my $row ( sort { $a->[2] cmp $b->[2] } @Index ) {
606 my $x = "$row->[1]:$row->[2]";
607 $x = lc $x;
608 $x =~ s/[ ,'":\/()]+/_/g;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000609
Ted Kremenek991c54b2008-08-08 20:46:42 +0000610 my $ReportFile = $row->[0];
Ted Kremenekebb74132008-09-21 06:58:09 +0000611
612 print OUT "<tr class=\"bt_$x\">";
613 print OUT "<td class=\"DESC\">";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000614 print OUT $row->[1];
Ted Kremenekebb74132008-09-21 06:58:09 +0000615 print OUT "</td>";
616 print OUT "<td class=\"DESC\">";
617 print OUT $row->[2];
618 print OUT "</td>";
619
620 # Update the file prefix.
621 my $fname = $row->[3];
Ted Kremenekebb74132008-09-21 06:58:09 +0000622
Ted Kremenek991c54b2008-08-08 20:46:42 +0000623 if (defined $regex) {
624 $fname =~ s/$regex//;
625 UpdateInFilePath("$Dir/$ReportFile", $InFileRegex, $InFilePrefix)
626 }
Ted Kremenekebb74132008-09-21 06:58:09 +0000627
Ted Kremenek91639ef2008-09-22 17:42:31 +0000628 print OUT "<td>";
Ted Kremenekebb74132008-09-21 06:58:09 +0000629 my @fname = split /\//,$fname;
630 if ($#fname > 0) {
631 while ($#fname >= 0) {
632 my $x = shift @fname;
633 print OUT $x;
634 if ($#fname >= 0) {
635 print OUT "<span class=\"W\"> </span>/";
636 }
637 }
638 }
639 else {
640 print OUT $fname;
Ted Kremenek91639ef2008-09-22 17:42:31 +0000641 }
Ted Kremenekebb74132008-09-21 06:58:09 +0000642 print OUT "</td>";
643
644 # Print out the quantities.
Ted Kremenek50534dc2008-09-22 17:38:23 +0000645 for my $j ( 4 .. 4 ) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000646 print OUT "<td class=\"Q\">$row->[$j]</td>";
647 }
648
Ted Kremenek991c54b2008-08-08 20:46:42 +0000649 # Print the rest of the columns.
Ted Kremenek50534dc2008-09-22 17:38:23 +0000650 for (my $j = 5; $j <= $#{$row}; ++$j) {
Ted Kremenekebb74132008-09-21 06:58:09 +0000651 print OUT "<td>$row->[$j]</td>"
Ted Kremenek991c54b2008-08-08 20:46:42 +0000652 }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000653
Ted Kremenek991c54b2008-08-08 20:46:42 +0000654 # Emit the "View" link.
Ted Kremenek68005dd2008-09-22 17:39:18 +0000655 print OUT "<td><a href=\"$ReportFile#EndPath\">View Report</a></td>";
Ted Kremenek3cea9ee2008-07-30 17:58:08 +0000656
Daniel Dunbare43038e2008-09-19 23:18:44 +0000657 # Emit REPORTBUG markers.
Ted Kremenekebb74132008-09-21 06:58:09 +0000658 print OUT "\n<!-- REPORTBUG id=\"$ReportFile\" -->\n";
Daniel Dunbare43038e2008-09-19 23:18:44 +0000659
Ted Kremenek991c54b2008-08-08 20:46:42 +0000660 # End the row.
661 print OUT "</tr>\n";
662 }
663
Ted Kremenekebb74132008-09-21 06:58:09 +0000664 print OUT "</tbody>\n</table>\n\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000665 }
666
667 if ($Crashes) {
668 # Read the crash directory for files.
669 opendir(DIR, "$Dir/crashes");
670 my @files = grep { /[.]info$/ } readdir(DIR);
671 closedir(DIR);
672
673 if (scalar(@files)) {
674 print OUT <<ENDTEXT;
Ted Kremenek7cba1122008-09-22 01:35:58 +0000675<h2>Analyzer Failures</h2>
Ted Kremenek991c54b2008-08-08 20:46:42 +0000676
Ted Kremenek5d31f832008-08-18 18:38:29 +0000677<p>The analyzer had problems processing the following files:</p>
Ted Kremenek991c54b2008-08-08 20:46:42 +0000678
679<table>
Ted Kremenek9f9b1fd2008-09-12 22:49:36 +0000680<thead><tr><td>Problem</td><td>Source File</td><td>Preprocessed File</td><td>STDERR Output</td></tr></thead>
Ted Kremenek991c54b2008-08-08 20:46:42 +0000681ENDTEXT
682
683 foreach my $file (sort @files) {
684 $file =~ /(.+).info$/;
685 # Get the preprocessed file.
686 my $ppfile = $1;
687 # Open the info file and get the name of the source file.
688 open (INFO, "$Dir/crashes/$file") or
689 die "Cannot open $Dir/crashes/$file\n";
690 my $srcfile = <INFO>;
Ted Kremenek5d31f832008-08-18 18:38:29 +0000691 chomp $srcfile;
692 my $problem = <INFO>;
693 chomp $problem;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000694 close (INFO);
695 # Print the information in the table.
Ted Kremenekd52e4252008-08-25 20:45:07 +0000696 my $prefix = GetPrefix();
Ted Kremenek9f9b1fd2008-09-12 22:49:36 +0000697 if (defined $prefix) { $srcfile =~ s/^\Q$prefix//; }
698 print OUT "<tr><td>$problem</td><td>$srcfile</td><td><a href=\"crashes/$ppfile\">$ppfile</a></td><td><a href=\"crashes/$ppfile.stderr.txt\">$ppfile.stderr.txt</a></td></tr>\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000699 }
700
701 print OUT <<ENDTEXT;
702</table>
703<p>Please consider submitting preprocessed files as <a href="http://clang.llvm.org/StaticAnalysisUsage.html#filingbugs">bug reports</a>.</p>
704ENDTEXT
705 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000706 }
707
Ted Kremenek991c54b2008-08-08 20:46:42 +0000708 print OUT "</body></html>\n";
Ted Kremenek5744dc22008-04-02 18:03:36 +0000709 close(OUT);
Ted Kremenek3ce12072008-09-22 17:50:47 +0000710 CopyFiles($Dir);
Ted Kremenek20161e92008-07-15 20:18:21 +0000711
712 # Make sure $Dir and $BaseDir are world readable/executable.
713 system("chmod", "755", $Dir);
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000714 if (defined $BaseDir) { system("chmod", "755", $BaseDir); }
Ted Kremenek20161e92008-07-15 20:18:21 +0000715
Ted Kremenek23cfca32008-06-16 22:40:14 +0000716 my $Num = scalar(@Index);
Ted Kremenek150c2122008-07-11 19:15:05 +0000717 Diag("$Num bugs found.\n");
718 if ($Num > 0 && -r "$Dir/index.html") {
Ted Kremenek5950b3f2008-09-22 06:47:01 +0000719 Diag("Run 'scan-view $Dir' to examine bug reports.\n");
Ted Kremenek150c2122008-07-11 19:15:05 +0000720 }
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000721
Ted Kremenek991c54b2008-08-08 20:46:42 +0000722 DiagCrashes($Dir) if ($Crashes);
723
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000724 return $Num;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000725}
726
727##----------------------------------------------------------------------------##
Ted Kremenekdab11102008-04-02 04:43:42 +0000728# RunBuildCommand - Run the build command.
729##----------------------------------------------------------------------------##
730
Ted Kremenek6b628982008-04-30 23:47:12 +0000731sub AddIfNotPresent {
732 my $Args = shift;
733 my $Arg = shift;
734 my $found = 0;
735
736 foreach my $k (@$Args) {
737 if ($k eq $Arg) {
738 $found = 1;
739 last;
740 }
741 }
742
743 if ($found == 0) {
744 push @$Args, $Arg;
745 }
746}
747
Ted Kremenekdab11102008-04-02 04:43:42 +0000748sub RunBuildCommand {
749
750 my $Args = shift;
Ted Kremenek7442ca62008-04-02 16:04:51 +0000751 my $IgnoreErrors = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000752 my $Cmd = $Args->[0];
Ted Kremenek6195c372008-06-02 21:52:47 +0000753 my $CCAnalyzer = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000754
Ted Kremenek3301cb12008-06-30 18:18:16 +0000755 # Get only the part of the command after the last '/'.
756 if ($Cmd =~ /\/([^\/]+)$/) {
757 $Cmd = $1;
758 }
759
Ted Kremenek63c20172008-08-04 17:34:06 +0000760 if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc"
761 or $Cmd eq "ccc-analyzer") {
Ted Kremenekdab11102008-04-02 04:43:42 +0000762 shift @$Args;
Ted Kremenek6195c372008-06-02 21:52:47 +0000763 unshift @$Args, $CCAnalyzer;
Ted Kremenekdab11102008-04-02 04:43:42 +0000764 }
Ted Kremenek7442ca62008-04-02 16:04:51 +0000765 elsif ($IgnoreErrors) {
766 if ($Cmd eq "make" or $Cmd eq "gmake") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000767 AddIfNotPresent($Args,"-k");
Ted Kremenek8912b542008-05-13 21:28:02 +0000768 AddIfNotPresent($Args,"-i");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000769 }
770 elsif ($Cmd eq "xcodebuild") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000771 AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000772 }
Ted Kremenek6b628982008-04-30 23:47:12 +0000773 }
774
Ted Kremenek6b628982008-04-30 23:47:12 +0000775 if ($Cmd eq "xcodebuild") {
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000776 # Disable distributed builds for xcodebuild.
Ted Kremenek6b628982008-04-30 23:47:12 +0000777 AddIfNotPresent($Args,"-nodistribute");
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000778
779 # Disable PCH files until clang supports them.
780 AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO");
Ted Kremenek915e9722008-05-27 23:18:07 +0000781
782 # When 'CC' is set, xcodebuild uses it to do all linking, even if we are
783 # linking C++ object files. Set 'LDPLUSPLUS' so that xcodebuild uses 'g++'
784 # when linking such files.
Ted Kremenek95aa1052008-09-04 17:52:41 +0000785 die if (!defined $CXX);
786 my $LDPLUSPLUS = `which $CXX`;
Ted Kremenek915e9722008-05-27 23:18:07 +0000787 $LDPLUSPLUS =~ s/\015?\012//; # strip newlines
788 $ENV{'LDPLUSPLUS'} = $LDPLUSPLUS;
Ted Kremenek6b628982008-04-30 23:47:12 +0000789 }
Ted Kremenekdab11102008-04-02 04:43:42 +0000790
Ted Kremenek5a4ddaf2008-08-25 20:10:45 +0000791 return (system(@$Args) >> 8);
Ted Kremenekdab11102008-04-02 04:43:42 +0000792}
793
Ted Kremenekdab11102008-04-02 04:43:42 +0000794##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000795# DisplayHelp - Utility function to display all help options.
796##----------------------------------------------------------------------------##
797
Sam Bishopa0e22662008-04-02 03:35:43 +0000798sub DisplayHelp {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000799
Ted Kremenek5744dc22008-04-02 18:03:36 +0000800print <<ENDTEXT;
Sam Bishopa0e22662008-04-02 03:35:43 +0000801USAGE: $Prog [options] <build command> [build options]
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000802
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000803ENDTEXT
804
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000805 if (defined $BuildName) {
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000806 print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n";
807 }
808
809print <<ENDTEXT;
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000810OPTIONS:
811
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000812 -o - Target directory for HTML report files. Subdirectories
Sam Bishopa0e22662008-04-02 03:35:43 +0000813 will be created as needed to represent separate "runs" of
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000814 the analyzer. If this option is not specified, a directory
815 is created in /tmp to store the reports.
Ted Kremenek1262fc42008-05-14 20:10:33 +0000816
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000817 -h - Display this message.
818 --help
Ted Kremenek1262fc42008-05-14 20:10:33 +0000819
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000820 -k - Add a "keep on going" option to the specified build command.
821 --keep-going This option currently supports make and xcodebuild.
Ted Kremenekf02e8db2008-04-02 16:41:25 +0000822 This is a convenience option; one can specify this
823 behavior directly using build options.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000824
Ted Kremenek7cba1122008-09-22 01:35:58 +0000825 --html-title [title] - Specify the title used on generated HTML pages.
826 --html-title=[title] If not specified, a default title will be used.
827
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000828 --status-bugs - By default, the exit status of $Prog is the same as the
829 executed build command. Specifying this option causes the
830 exit status of $Prog to be 1 if it found potential bugs
831 and 0 otherwise.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000832
Ted Kremenek386c6932008-09-03 17:59:35 +0000833 --use-cc [compiler path] - By default, $Prog uses 'gcc' to compile and link
834 --use-cc=[compiler path] your C and Objective-C code. Use this option
835 to specify an alternate compiler.
836
837 --use-c++ [compiler path] - By default, $Prog uses 'g++' to compile and link
838 --use-c++=[compiler path] your C++ and Objective-C++ code. Use this option
839 to specify an alternate compiler.
Ted Kremenekf17ef3c2008-08-21 21:47:09 +0000840
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000841 -v - Verbose output from $Prog and the analyzer.
Ted Kremenek386c6932008-09-03 17:59:35 +0000842 A second and third '-v' increases verbosity.
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000843
844 -V - View analysis results in a web browser when the build
845 --view completes.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000846
Ted Kremenekb7770c02008-07-15 17:06:13 +0000847
Ted Kremenek386c6932008-09-03 17:59:35 +0000848AVAILABLE ANALYSES (multiple analyses may be specified):
Ted Kremenekd52e4252008-08-25 20:45:07 +0000849
850ENDTEXT
Ted Kremenekb7770c02008-07-15 17:06:13 +0000851
852 foreach my $Analysis (sort keys %AvailableAnalyses) {
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000853 if (defined $AnalysesDefaultEnabled{$Analysis}) {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000854 print " (+)";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000855 }
856 else {
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000857 print " ";
Ted Kremenekb7770c02008-07-15 17:06:13 +0000858 }
859
860 print " $Analysis $AvailableAnalyses{$Analysis}\n";
861 }
862
863print <<ENDTEXT
864
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000865 NOTE: "(+)" indicates that an analysis is enabled by default unless one
866 or more analysis options are specified
Ted Kremenekb7770c02008-07-15 17:06:13 +0000867
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000868BUILD OPTIONS
869
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000870 You can specify any build option acceptable to the build command.
Ted Kremenek39eefde2008-04-02 16:47:27 +0000871
Ted Kremenek5744dc22008-04-02 18:03:36 +0000872EXAMPLE
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000873
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000874 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000875
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000876 The above example causes analysis reports to be deposited into
877 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
878 A different subdirectory is created each time $Prog analyzes a project.
879 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000880
881ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000882}
883
884##----------------------------------------------------------------------------##
Ted Kremenek7cba1122008-09-22 01:35:58 +0000885# HtmlEscape - HTML entity encode characters that are special in HTML
886##----------------------------------------------------------------------------##
887
888sub HtmlEscape {
889 # copy argument to new variable so we don't clobber the original
890 my $arg = shift || '';
891 my $tmp = $arg;
892
893 $tmp =~ s/([\<\>\'\"])/sprintf("&#%02x;", chr($1))/ge;
894
895 return $tmp;
896}
897
898##----------------------------------------------------------------------------##
899# ShellEscape - backslash escape characters that are special to the shell
900##----------------------------------------------------------------------------##
901
902sub ShellEscape {
903 # copy argument to new variable so we don't clobber the original
904 my $arg = shift || '';
905 my $tmp = $arg;
906
907 $tmp =~ s/([\!\;\\\'\"\`\<\>\|\s\(\)\[\]\?\#\$\^\&\*\=])/\\$1/g;
908
909 return $tmp;
910}
911
912##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000913# Process command-line arguments.
914##----------------------------------------------------------------------------##
915
916my $HtmlDir; # Parent directory to store HTML files.
917my $IgnoreErrors = 0; # Ignore build errors.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000918my $ViewResults = 0; # View results when the build terminates.
Ted Kremenek363dc3f2008-07-15 22:03:09 +0000919my $ExitStatusFoundBugs = 0; # Exit status reflects whether bugs were found
Ted Kremenekb7770c02008-07-15 17:06:13 +0000920my @AnalysesToRun;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000921
922if (!@ARGV) {
923 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000924 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000925}
926
927while (@ARGV) {
928
929 # Scan for options we recognize.
930
931 my $arg = $ARGV[0];
932
Sam Bishop2f2418e2008-04-03 14:29:47 +0000933 if ($arg eq "-h" or $arg eq "--help") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000934 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000935 exit 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000936 }
937
Ted Kremenekfc1d3402008-08-04 18:15:26 +0000938 if (defined $AvailableAnalyses{$arg}) {
Ted Kremenek1262fc42008-05-14 20:10:33 +0000939 shift @ARGV;
Ted Kremenekb7770c02008-07-15 17:06:13 +0000940 push @AnalysesToRun, $arg;
Ted Kremenek1262fc42008-05-14 20:10:33 +0000941 next;
942 }
943
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000944 if ($arg eq "-o") {
945 shift @ARGV;
946
947 if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000948 DieDiag("'-o' option requires a target directory name.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000949 }
950
951 $HtmlDir = shift @ARGV;
952 next;
953 }
Ted Kremenek7cba1122008-09-22 01:35:58 +0000954
955 if ($arg =~ /^--html-title(=(.+))?$/) {
956 shift @ARGV;
957
958 if ($2 eq '') {
959 if (!@ARGV) {
960 DieDiag("'--html-title' option requires a string.\n");
961 }
962
963 $HtmlTitle = shift @ARGV;
964 } else {
965 $HtmlTitle = $2;
966 }
967
968 next;
969 }
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000970
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000971 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000972 shift @ARGV;
973 $IgnoreErrors = 1;
974 next;
975 }
976
Ted Kremenekf17ef3c2008-08-21 21:47:09 +0000977 if ($arg =~ /^--use-cc(=(.+))?$/) {
978 shift @ARGV;
979 my $cc;
980
981 if ($2 eq "") {
982 if (!@ARGV) {
983 DieDiag("'--use-cc' option requires a compiler executable name.\n");
984 }
985 $cc = shift @ARGV;
986 }
987 else {
988 $cc = $2;
989 }
990
991 $ENV{"CCC_CC"} = $cc;
992 next;
993 }
994
Ted Kremenek7cba1122008-09-22 01:35:58 +0000995 if ($arg =~ /^--use-c\+\+(=(.+))?$/) {
Ted Kremenek386c6932008-09-03 17:59:35 +0000996 shift @ARGV;
997
998 if ($2 eq "") {
999 if (!@ARGV) {
1000 DieDiag("'--use-c++' option requires a compiler executable name.\n");
1001 }
1002 $CXX = shift @ARGV;
1003 }
1004 else {
1005 $CXX = $2;
1006 }
1007 next;
1008 }
1009
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001010 if ($arg eq "-v") {
1011 shift @ARGV;
1012 $Verbose++;
1013 next;
1014 }
1015
Ted Kremenek7f8a3252008-04-02 18:42:49 +00001016 if ($arg eq "-V" or $arg eq "--view") {
1017 shift @ARGV;
1018 $ViewResults = 1;
1019 next;
1020 }
1021
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001022 if ($arg eq "--status-bugs") {
1023 shift @ARGV;
1024 $ExitStatusFoundBugs = 1;
1025 next;
1026 }
1027
Ted Kremenek23cfca32008-06-16 22:40:14 +00001028 DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/);
Ted Kremenek0062ad42008-04-02 16:35:01 +00001029
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001030 last;
1031}
1032
1033if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +00001034 Diag("No build command specified.\n\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001035 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +00001036 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001037}
1038
Ted Kremenek7cba1122008-09-22 01:35:58 +00001039$CmdArgs = HtmlEscape(join(' ', map(ShellEscape($_), @ARGV)));
1040$HtmlTitle = "${CurrentDirSuffix} - scan-build results"
1041 unless (defined($HtmlTitle));
Ted Kremenek386c6932008-09-03 17:59:35 +00001042
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001043# Determine the output directory for the HTML reports.
Ted Kremenek684bb092008-04-18 15:18:20 +00001044my $BaseDir = $HtmlDir;
Sam Bishopa0e22662008-04-02 03:35:43 +00001045$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001046
1047# Set the appropriate environment variables.
Sam Bishopa0e22662008-04-02 03:35:43 +00001048SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001049
Ted Kremenek0b6c1532008-04-08 20:22:12 +00001050my $Cmd = "$RealBin/ccc-analyzer";
1051
Ted Kremenek23cfca32008-06-16 22:40:14 +00001052DieDiag("Executable 'ccc-analyzer' does not exist at '$Cmd'\n")
Ted Kremenek0b6c1532008-04-08 20:22:12 +00001053 if (! -x $Cmd);
Ted Kremenekf22eacb2008-04-18 22:00:56 +00001054
Ted Kremenekb7770c02008-07-15 17:06:13 +00001055if (! -x $ClangSB) {
1056 Diag("'clang' executable not found in '$RealBin'.\n");
1057 Diag("Using 'clang' from path.\n");
Ted Kremenekf22eacb2008-04-18 22:00:56 +00001058}
Ted Kremenek0b6c1532008-04-08 20:22:12 +00001059
Ted Kremenek95aa1052008-09-04 17:52:41 +00001060if (defined $CXX) {
1061 $ENV{'CXX'} = $CXX;
1062}
1063else {
1064 $CXX = 'g++'; # This variable is used by other parts of scan-build
1065 # that need to know a default C++ compiler to fall back to.
1066}
1067
Ted Kremenek4f4b17d2008-04-03 20:08:18 +00001068$ENV{'CC'} = $Cmd;
Ted Kremenekf22eacb2008-04-18 22:00:56 +00001069$ENV{'CLANG'} = $Clang;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001070
1071if ($Verbose >= 2) {
1072 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
1073}
1074
Ted Kremeneka9525c92008-05-12 22:07:14 +00001075if ($Verbose >= 3) {
1076 $ENV{'CCC_ANALYZER_LOG'} = 1;
1077}
1078
Ted Kremenek90125992008-07-15 23:41:32 +00001079if (scalar(@AnalysesToRun) == 0) {
1080 foreach my $key (keys %AnalysesDefaultEnabled) {
1081 push @AnalysesToRun,$key;
1082 }
Ted Kremenek01006782008-07-02 23:16:10 +00001083}
Ted Kremenek1262fc42008-05-14 20:10:33 +00001084
Ted Kremenek90125992008-07-15 23:41:32 +00001085$ENV{'CCC_ANALYZER_ANALYSIS'} = join ' ',@AnalysesToRun;
1086
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001087# Run the build.
Ted Kremenek5656a982008-07-15 17:09:28 +00001088my $ExitStatus = RunBuildCommand(\@ARGV, $IgnoreErrors, $Cmd);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001089
1090# Postprocess the HTML directory.
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001091my $NumBugs = Postprocess($HtmlDir, $BaseDir);
Ted Kremenek7f8a3252008-04-02 18:42:49 +00001092
1093if ($ViewResults and -r "$HtmlDir/index.html") {
Ted Kremenek50534dc2008-09-22 17:38:23 +00001094 Diag "Analysis run complete.\n";
Ted Kremenek5950b3f2008-09-22 06:47:01 +00001095 Diag "Viewing analysis results in '$HtmlDir' using scan-view.\n";
1096 my $ScanView = "$RealBin/scan-view";
1097 if (! -x $ScanView) { $ScanView = "scan-view"; }
1098 exec $ScanView, "$HtmlDir";
Ted Kremenek7f8a3252008-04-02 18:42:49 +00001099}
Ted Kremenek5656a982008-07-15 17:09:28 +00001100
Ted Kremenek363dc3f2008-07-15 22:03:09 +00001101if ($ExitStatusFoundBugs) {
1102 exit 1 if ($NumBugs > 0);
1103 exit 0;
1104}
1105
Ted Kremenek5656a982008-07-15 17:09:28 +00001106exit $ExitStatus;
1107