blob: afd2e833e9e7a933f32a025b422c41e3559688d7 [file] [log] [blame]
Ted Kremenekc9d8fde2008-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;
17use File::Temp qw/ :mktemp /;
Ted Kremenekd5d43622008-04-02 20:43:36 +000018use FindBin qw($RealBin);
Ted Kremenek422b0ac2008-04-19 18:05:48 +000019use Digest::MD5;
Ted Kremenek32280862008-05-02 22:04:53 +000020use File::Basename;
Ted Kremenekc2007a92008-06-16 22:40:14 +000021use Term::ANSIColor;
22use Term::ANSIColor qw(:constants);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +000023
24my $Verbose = 0; # Verbose output from this script.
25my $Prog = "scan-build";
Ted Kremenekf54e8882008-05-23 18:17:05 +000026my $BuildName;
27my $BuildDate;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +000028
Ted Kremenekf2e86cc2008-06-17 03:06:59 +000029my $UseColor = ((($ENV{'TERM'} eq 'xterm-color') and -t STDOUT)
30 and defined($ENV{'SCAN_BUILD_COLOR'}));
Ted Kremenekc2007a92008-06-16 22:40:14 +000031
Ted Kremenekadcf72b2008-07-15 17:06:13 +000032##----------------------------------------------------------------------------##
33# Diagnostics
34##----------------------------------------------------------------------------##
35
Ted Kremenekc2007a92008-06-16 22:40:14 +000036sub Diag {
37 if ($UseColor) {
38 print BOLD, MAGENTA "$Prog: @_";
39 print RESET;
40 }
41 else {
42 print "$Prog: @_";
43 }
44}
45
46sub DieDiag {
47 if ($UseColor) {
48 print BOLD, RED "$Prog: ";
49 print RESET, RED @_;
50 print RESET;
51 }
52 else {
53 print "$Prog: ", @_;
54 }
55 exit(0);
56}
57
Ted Kremenekc9d8fde2008-04-01 20:47:38 +000058##----------------------------------------------------------------------------##
Ted Kremenekadcf72b2008-07-15 17:06:13 +000059# Some initial preprocessing of Clang options.
60##----------------------------------------------------------------------------##
61
62my $ClangSB = "$RealBin/clang";
63my $Clang = $ClangSB;
64
65if (! -x $ClangSB) {
66 $Clang = "clang";
67}
68
69my %AvailableAnalyses;
70
71# Query clang for analysis options.
Ted Kremenek77124782008-07-18 23:13:03 +000072open(PIPE, "'$Clang' --help |") or
Ted Kremenekadcf72b2008-07-15 17:06:13 +000073 DieDiag("Cannot execute '$Clang'");
74
75my $FoundAnalysis = 0;
76
77while(<PIPE>) {
78 if ($FoundAnalysis == 0) {
79 if (/Available Source Code Analyses/) {
80 $FoundAnalysis = 1;
81 }
82
83 next;
84 }
85
86 if (/^\s\s\s\s([^\s]+)\s(.+)$/) {
87 next if ($1 =~ /-dump/ or $1 =~ /-view/
88 or $1 =~ /-checker-simple/ or $1 =~ /-warn-uninit/);
89
90 $AvailableAnalyses{$1} = $2;
91 next;
92 }
93
94 last;
95}
96
97close (PIPE);
98
99my %AnalysesDefaultEnabled = (
100 '-warn-dead-stores' => 1,
101 '-checker-cfref' => 1,
Ted Kremenek47219912008-07-15 23:41:32 +0000102 '-warn-objc-methodsigs' => 1,
Ted Kremenek6a985062008-07-25 20:35:01 +0000103 '-warn-objc-missing-dealloc' => 1,
104 '-warn-objc-unused-ivars' => 1
Ted Kremenekadcf72b2008-07-15 17:06:13 +0000105);
106
107##----------------------------------------------------------------------------##
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000108# GetHTMLRunDir - Construct an HTML directory name for the current run.
109##----------------------------------------------------------------------------##
110
Sam Bishop479982e2008-04-02 03:35:43 +0000111sub GetHTMLRunDir {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000112
113 die "Not enough arguments." if (@_ == 0);
114
115 my $Dir = shift @_;
116
117 # Get current date and time.
118
119 my @CurrentTime = localtime();
120
121 my $year = $CurrentTime[5] + 1900;
122 my $day = $CurrentTime[3];
123 my $month = $CurrentTime[4] + 1;
124
Ted Kremenek573d80b2008-05-14 17:23:56 +0000125 my $DateString = sprintf("%d-%02d-%02d", $year, $month, $day);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000126
127 # Determine the run number.
128
129 my $RunNumber;
130
131 if (-d $Dir) {
132
133 if (! -r $Dir) {
Ted Kremenekc2007a92008-06-16 22:40:14 +0000134 DieDiag("directory '$Dir' exists but is not readable.\n");
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000135 }
136
137 # Iterate over all files in the specified directory.
138
139 my $max = 0;
140
141 opendir(DIR, $Dir);
142 my @FILES= readdir(DIR);
143 closedir(DIR);
144
145 foreach my $f (@FILES) {
146
147 my @x = split/-/, $f;
148
149 next if (scalar(@x) != 4);
150 next if ($x[0] != $year);
151 next if ($x[1] != $month);
152 next if ($x[2] != $day);
153
154 if ($x[3] > $max) {
155 $max = $x[3];
156 }
157 }
158
159 $RunNumber = $max + 1;
160 }
161 else {
162
163 if (-x $Dir) {
Ted Kremenekc2007a92008-06-16 22:40:14 +0000164 DieDiag("'$Dir' exists but is not a directory.\n");
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000165 }
166
167 # $Dir does not exist. It will be automatically created by the
168 # clang driver. Set the run number to 1.
169
170 $RunNumber = 1;
171 }
172
173 die "RunNumber must be defined!" if (!defined($RunNumber));
174
175 # Append the run number.
176
177 return "$Dir/$DateString-$RunNumber";
178}
179
Sam Bishop479982e2008-04-02 03:35:43 +0000180sub SetHtmlEnv {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000181
182 die "Wrong number of arguments." if (scalar(@_) != 2);
183
184 my $Args = shift;
185 my $Dir = shift;
186
187 die "No build command." if (scalar(@$Args) == 0);
188
189 my $Cmd = $$Args[0];
190
191 if ($Cmd =~ /configure/) {
192 return;
193 }
194
195 if ($Verbose) {
Ted Kremenekc2007a92008-06-16 22:40:14 +0000196 Diag("Emitting reports for this run to '$Dir'.\n");
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000197 }
198
199 $ENV{'CCC_ANALYZER_HTML'} = $Dir;
200}
201
202##----------------------------------------------------------------------------##
Ted Kremenekf0a08a52008-04-18 15:09:30 +0000203# ComputeDigest - Compute a digest of the specified file.
204##----------------------------------------------------------------------------##
205
206sub ComputeDigest {
207 my $FName = shift;
Ted Kremenekc2007a92008-06-16 22:40:14 +0000208 DieDiag("Cannot read $FName to compute Digest.\n") if (! -r $FName);
Ted Kremenek422b0ac2008-04-19 18:05:48 +0000209
210 # Use Digest::MD5. We don't have to be cryptographically secure. We're
Ted Kremenek7c400a02008-04-19 18:07:44 +0000211 # just looking for duplicate files that come from a non-malicious source.
212 # We use Digest::MD5 because it is a standard Perl module that should
Ted Kremenek422b0ac2008-04-19 18:05:48 +0000213 # come bundled on most systems.
214
Ted Kremenekc2007a92008-06-16 22:40:14 +0000215 open(FILE, $FName) or DieDiag("Cannot open $FName when computing Digest.\n");
Ted Kremenek422b0ac2008-04-19 18:05:48 +0000216 binmode FILE;
217 my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest;
218 close(FILE);
219
220 # Return the digest.
221
222 return $Result;
Ted Kremenekf0a08a52008-04-18 15:09:30 +0000223}
224
225##----------------------------------------------------------------------------##
Ted Kremenek32280862008-05-02 22:04:53 +0000226# UpdatePrefix - Compute the common prefix of files.
227##----------------------------------------------------------------------------##
228
229my $Prefix;
230
231sub UpdatePrefix {
232
233 my $x = shift;
234 my $y = basename($x);
235 $x =~ s/\Q$y\E$//;
236
237 # Ignore /usr, /Library, /System, /Developer
238
239 return if ( $x =~ /^\/usr/ or $x =~ /^\/Library/
240 or $x =~ /^\/System/ or $x =~ /^\/Developer/);
241
242
243 if (!defined $Prefix) {
244 $Prefix = $x;
245 return;
246 }
247
248 chop $Prefix while (!($x =~ /^$Prefix/));
249}
250
251sub GetPrefix {
252 return $Prefix;
253}
254
255##----------------------------------------------------------------------------##
256# UpdateInFilePath - Update the path in the report file.
257##----------------------------------------------------------------------------##
258
259sub UpdateInFilePath {
260 my $fname = shift;
261 my $regex = shift;
262 my $newtext = shift;
263
264 open (RIN, $fname) or die "cannot open $fname";
265 open (ROUT, ">$fname.tmp") or die "cannot open $fname.tmp";
266
267 while (<RIN>) {
268 s/$regex/$newtext/;
269 print ROUT $_;
270 }
271
272 close (ROUT);
273 close (RIN);
Ted Kremenekbd5e3282008-07-15 20:18:21 +0000274 system("mv", "$fname.tmp", $fname);
Ted Kremenek32280862008-05-02 22:04:53 +0000275}
276
277##----------------------------------------------------------------------------##
Ted Kremeneked757e02008-04-02 18:03:36 +0000278# ScanFile - Scan a report file for various identifying attributes.
279##----------------------------------------------------------------------------##
280
Ted Kremenekf0a08a52008-04-18 15:09:30 +0000281# Sometimes a source file is scanned more than once, and thus produces
282# multiple error reports. We use a cache to solve this problem.
283
284my %AlreadyScanned;
285
Ted Kremeneked757e02008-04-02 18:03:36 +0000286sub ScanFile {
287
288 my $Index = shift;
289 my $Dir = shift;
290 my $FName = shift;
291
Ted Kremenekf0a08a52008-04-18 15:09:30 +0000292 # Compute a digest for the report file. Determine if we have already
293 # scanned a file that looks just like it.
294
295 my $digest = ComputeDigest("$Dir/$FName");
296
297 if (defined($AlreadyScanned{$digest})) {
298 # Redundant file. Remove it.
Ted Kremenekbd5e3282008-07-15 20:18:21 +0000299 system ("rm", "-f", "$Dir/$FName");
Ted Kremenekf0a08a52008-04-18 15:09:30 +0000300 return;
301 }
302
303 $AlreadyScanned{$digest} = 1;
304
Ted Kremenekebd0bb72008-04-18 16:58:34 +0000305 # At this point the report file is not world readable. Make it happen.
Ted Kremenekbd5e3282008-07-15 20:18:21 +0000306 system ("chmod", "644", "$Dir/$FName");
Ted Kremenekc5b38202008-04-18 15:18:20 +0000307
308 # Scan the report file for tags.
Ted Kremenekc2007a92008-06-16 22:40:14 +0000309 open(IN, "$Dir/$FName") or DieDiag("Cannot open '$Dir/$FName'\n");
Ted Kremeneked757e02008-04-02 18:03:36 +0000310
311 my $BugDesc = "";
Ted Kremenekd5d43622008-04-02 20:43:36 +0000312 my $BugFile = "";
313 my $BugPathLength = 1;
314 my $BugLine = 0;
Ted Kremeneked757e02008-04-02 18:03:36 +0000315
316 while (<IN>) {
317
318 if (/<!-- BUGDESC (.*) -->$/) {
319 $BugDesc = $1;
Ted Kremeneked757e02008-04-02 18:03:36 +0000320 }
Ted Kremenekd5d43622008-04-02 20:43:36 +0000321 elsif (/<!-- BUGFILE (.*) -->$/) {
322 $BugFile = $1;
Ted Kremenek32280862008-05-02 22:04:53 +0000323 UpdatePrefix($BugFile);
Ted Kremenekd5d43622008-04-02 20:43:36 +0000324 }
325 elsif (/<!-- BUGPATHLENGTH (.*) -->$/) {
326 $BugPathLength = $1;
327 }
328 elsif (/<!-- BUGLINE (.*) -->$/) {
329 $BugLine = $1;
330 }
Ted Kremeneked757e02008-04-02 18:03:36 +0000331 }
332
333 close(IN);
334
Ted Kremenekd5d43622008-04-02 20:43:36 +0000335 push @$Index,[ $FName, $BugDesc, $BugFile, $BugLine, $BugPathLength ];
336}
337
338##----------------------------------------------------------------------------##
339# CopyJS - Copy JavaScript code to target directory.
340##----------------------------------------------------------------------------##
341
342sub CopyJS {
343
344 my $Dir = shift;
345
Ted Kremenekc2007a92008-06-16 22:40:14 +0000346 DieDiag("Cannot find 'sorttable.js'.\n")
Ted Kremenekd5d43622008-04-02 20:43:36 +0000347 if (! -r "$RealBin/sorttable.js");
348
Ted Kremenekbd5e3282008-07-15 20:18:21 +0000349 system ("cp", "$RealBin/sorttable.js", "$Dir");
Ted Kremenekd5d43622008-04-02 20:43:36 +0000350
Ted Kremenekc2007a92008-06-16 22:40:14 +0000351 DieDiag("Could not copy 'sorttable.js' to '$Dir'.\n")
Ted Kremenekd5d43622008-04-02 20:43:36 +0000352 if (! -r "$Dir/sorttable.js");
Ted Kremeneked757e02008-04-02 18:03:36 +0000353}
354
355##----------------------------------------------------------------------------##
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000356# Postprocess - Postprocess the results of an analysis scan.
357##----------------------------------------------------------------------------##
358
Sam Bishop479982e2008-04-02 03:35:43 +0000359sub Postprocess {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000360
361 my $Dir = shift;
Ted Kremenekc5b38202008-04-18 15:18:20 +0000362 my $BaseDir = shift;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000363
364 die "No directory specified." if (!defined($Dir));
Ted Kremenekc5b38202008-04-18 15:18:20 +0000365 die "No base directory specified." if (!defined($BaseDir));
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000366
367 if (! -d $Dir) {
Ted Kremenekc2007a92008-06-16 22:40:14 +0000368 Diag("No bugs found.\n");
Ted Kremenekfea39522008-07-15 22:03:09 +0000369 return 0;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000370 }
371
372 opendir(DIR, $Dir);
373 my @files = grep(/^report-.*\.html$/,readdir(DIR));
374 closedir(DIR);
375
376 if (scalar(@files) == 0) {
Ted Kremenekc2007a92008-06-16 22:40:14 +0000377 Diag("Removing directory '$Dir' because it contains no reports.\n");
Ted Kremenekbd5e3282008-07-15 20:18:21 +0000378 system ("rm", "-fR", $Dir);
Ted Kremenekc2007a92008-06-16 22:40:14 +0000379
380 # Remove the base directory if it contains no files (don't use '-R').
Ted Kremenekbd5e3282008-07-15 20:18:21 +0000381 system ("rm", "-f", $BaseDir);
Ted Kremenekc2007a92008-06-16 22:40:14 +0000382
383 Diag("No bugs found.\n");
Ted Kremenekfea39522008-07-15 22:03:09 +0000384 return 0;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000385 }
Ted Kremeneked757e02008-04-02 18:03:36 +0000386
387 # Scan each report file and build an index.
388
389 my @Index;
390
391 foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); }
392
393 # Generate an index.html file.
394
395 my $FName = "$Dir/index.html";
396
Ted Kremenekc2007a92008-06-16 22:40:14 +0000397 open(OUT, ">$FName") or DieDiag("Cannot create file '$FName'\n");
Ted Kremeneked757e02008-04-02 18:03:36 +0000398
Ted Kremenek4de0c562008-04-15 20:47:02 +0000399 # Print out the header.
400
Ted Kremeneked757e02008-04-02 18:03:36 +0000401print OUT <<ENDTEXT;
402<html>
403<head>
Ted Kremenekb59b7542008-04-02 18:42:49 +0000404<style type="text/css">
405 body { color:#000000; background-color:#ffffff }
Ted Kremenekd5d43622008-04-02 20:43:36 +0000406 body { font-family: Helvetica, sans-serif; font-size:9pt }
Ted Kremenekb59b7542008-04-02 18:42:49 +0000407 h1 { font-size:12pt }
Ted Kremenekd5d43622008-04-02 20:43:36 +0000408 table.sortable thead {
409 background-color:#eee; color:#666666;
410 font-weight: bold; cursor: default;
Ted Kremenek38471832008-04-03 05:50:51 +0000411 text-align:center;
412 border-top: 2px solid #000000;
413 border-bottom: 2px solid #000000;
414 font-weight: bold; font-family: Verdana
415 }
Ted Kremenekd5d43622008-04-02 20:43:36 +0000416 table.sortable { border: 1px #000000 solid }
417 table.sortable { border-collapse: collapse; border-spacing: 0px }
Ted Kremenekb59b7542008-04-02 18:42:49 +0000418 td { border-bottom: 1px #000000 dotted }
Ted Kremenekd5d43622008-04-02 20:43:36 +0000419 td { padding:5px; padding-left:8px; padding-right:8px }
Ted Kremenek756515f2008-04-07 23:50:07 +0000420 td { text-align:left; font-size:9pt }
Ted Kremenekd5d43622008-04-02 20:43:36 +0000421 td.View { padding-left: 10px }
Ted Kremenekb59b7542008-04-02 18:42:49 +0000422</style>
Ted Kremenekd5d43622008-04-02 20:43:36 +0000423<script src="sorttable.js"></script>
Ted Kremenek4de0c562008-04-15 20:47:02 +0000424<script language='javascript' type="text/javascript">
425function SetDisplay(RowClass, DisplayVal)
426{
427 var Rows = document.getElementsByTagName("tr");
428 for ( var i = 0 ; i < Rows.length; ++i ) {
429 if (Rows[i].className == RowClass) {
430 Rows[i].style.display = DisplayVal;
431 }
432 }
433}
434
435function ToggleDisplay(CheckButton, ClassName) {
Ted Kremenek4de0c562008-04-15 20:47:02 +0000436 if (CheckButton.checked) {
437 SetDisplay(ClassName, "");
438 }
439 else {
440 SetDisplay(ClassName, "none");
441 }
442}
443</script>
444</head>
445<body>
446ENDTEXT
447
448 # Print out the summary table.
449
450 my %Totals;
451
452 for my $row ( @Index ) {
453
Ted Kremenekb3a44e72008-05-06 18:11:36 +0000454 #my $bug_type = lc($row->[1]);
455 my $bug_type = ($row->[1]);
Ted Kremenek4de0c562008-04-15 20:47:02 +0000456
457 if (!defined($Totals{$bug_type})) {
458 $Totals{$bug_type} = 1;
459 }
460 else {
461 $Totals{$bug_type}++;
462 }
463 }
Ted Kremenekf54e8882008-05-23 18:17:05 +0000464
465 print OUT "<h3>Summary</h3>";
466
467 if (defined($BuildName)) {
468 print OUT "\n<p>Results in this analysis run are based on analyzer build <b>$BuildName</b>.</p>\n"
469 }
470
Ted Kremenek4de0c562008-04-15 20:47:02 +0000471print OUT <<ENDTEXT;
Ted Kremenek4de0c562008-04-15 20:47:02 +0000472<table class="sortable">
473<tr>
474 <td>Bug Type</td>
475 <td>Quantity</td>
Ted Kremenek3e2abdc2008-07-07 16:58:44 +0000476 <td class="sorttable_nosort">Display?</td>
Ted Kremenek4de0c562008-04-15 20:47:02 +0000477</tr>
478ENDTEXT
479
480 for my $key ( sort { $a cmp $b } keys %Totals ) {
Ted Kremenek3dba2162008-05-06 23:51:45 +0000481 my $x = lc($key);
Ted Kremeneka5a7da82008-07-30 17:58:08 +0000482 $x =~ s/[ ,'"]+/_/g;
Ted Kremenek4de0c562008-04-15 20:47:02 +0000483 print OUT "<tr><td>$key</td><td>$Totals{$key}</td><td><input type=\"checkbox\" onClick=\"ToggleDisplay(this,'bt_$x');\" checked/></td></tr>\n";
484 }
485
486 # Print out the table of errors.
487
488print OUT <<ENDTEXT;
489</table>
490<h3>Reports</h3>
Ted Kremenekd5d43622008-04-02 20:43:36 +0000491<table class="sortable">
Ted Kremenekb59b7542008-04-02 18:42:49 +0000492<tr>
Ted Kremeneke51290d2008-07-07 17:23:32 +0000493 <td class="sorttable_sorted">Bug Type<span id="sorttable_sortfwdind">&nbsp;&#x25BE;</span>
Ted Kremenek38471832008-04-03 05:50:51 +0000494 <td>File</td>
495 <td>Line</td>
496 <td>Path Length</td>
Ted Kremenek3e2abdc2008-07-07 16:58:44 +0000497 <td class="sorttable_nosort"></td>
Ted Kremenekb59b7542008-04-02 18:42:49 +0000498</tr>
Ted Kremeneked757e02008-04-02 18:03:36 +0000499ENDTEXT
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000500
Ted Kremenek32280862008-05-02 22:04:53 +0000501 my $prefix = GetPrefix();
502 my $regex;
503 my $InFileRegex;
504 my $InFilePrefix = "File:</td><td>";
505
506 if (defined($prefix)) {
507 $regex = qr/^\Q$prefix\E/is;
508 $InFileRegex = qr/\Q$InFilePrefix$prefix\E/is;
509 }
510
Ted Kremeneked757e02008-04-02 18:03:36 +0000511 for my $row ( sort { $a->[1] cmp $b->[1] } @Index ) {
512
Ted Kremenek4de0c562008-04-15 20:47:02 +0000513 my $x = lc($row->[1]);
Ted Kremeneka5a7da82008-07-30 17:58:08 +0000514 $x =~ s/[ ,'"]+/_/g;
Ted Kremenek4de0c562008-04-15 20:47:02 +0000515
516 print OUT "<tr class=\"bt_$x\">\n";
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000517
Ted Kremeneked757e02008-04-02 18:03:36 +0000518 my $ReportFile = $row->[0];
519
Ted Kremenekd5d43622008-04-02 20:43:36 +0000520 print OUT " <td class=\"DESC\">";
Ted Kremenekb3a44e72008-05-06 18:11:36 +0000521 #print OUT lc($row->[1]);
522 print OUT $row->[1];
Ted Kremenekd5d43622008-04-02 20:43:36 +0000523 print OUT "</td>\n";
Ted Kremeneked757e02008-04-02 18:03:36 +0000524
Ted Kremenek32280862008-05-02 22:04:53 +0000525 # Update the file prefix.
526
527 my $fname = $row->[2];
528 if (defined($regex)) {
529 $fname =~ s/$regex//;
530 UpdateInFilePath("$Dir/$ReportFile", $InFileRegex, $InFilePrefix)
531 }
Ted Kremenek80897002008-05-02 23:40:49 +0000532
Ted Kremenek32280862008-05-02 22:04:53 +0000533 print OUT "<td>$fname</td>\n";
534
535 # Print the rest of the columns.
Ted Kremenek32280862008-05-02 22:04:53 +0000536 for my $j ( 3 .. $#{$row} ) {
Ted Kremeneked757e02008-04-02 18:03:36 +0000537 print OUT "<td>$row->[$j]</td>\n"
538 }
Ted Kremenekb59b7542008-04-02 18:42:49 +0000539
540 # Emit the "View" link.
Ted Kremenekd5d43622008-04-02 20:43:36 +0000541 print OUT " <td class=\"View\"><a href=\"$ReportFile#EndPath\">View</a></td>\n";
Ted Kremeneka5a7da82008-07-30 17:58:08 +0000542
Ted Kremenekb59b7542008-04-02 18:42:49 +0000543 # End the row.
Ted Kremeneked757e02008-04-02 18:03:36 +0000544 print OUT "</tr>\n";
545 }
546
547 print OUT "</table>\n</body></html>\n";
548 close(OUT);
Ted Kremenekbd5e3282008-07-15 20:18:21 +0000549
Ted Kremenekd5d43622008-04-02 20:43:36 +0000550 CopyJS($Dir);
Ted Kremenekbd5e3282008-07-15 20:18:21 +0000551
552 # Make sure $Dir and $BaseDir are world readable/executable.
553 system("chmod", "755", $Dir);
554 system("chmod", "755", $BaseDir);
555
Ted Kremenekc2007a92008-06-16 22:40:14 +0000556 my $Num = scalar(@Index);
Ted Kremenek49226ac2008-07-11 19:15:05 +0000557 Diag("$Num bugs found.\n");
558 if ($Num > 0 && -r "$Dir/index.html") {
559 Diag("Open '$Dir/index.html' to examine bug reports.\n");
560 }
Ted Kremenekfea39522008-07-15 22:03:09 +0000561
562 return $Num;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000563}
564
565##----------------------------------------------------------------------------##
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000566# RunBuildCommand - Run the build command.
567##----------------------------------------------------------------------------##
568
Ted Kremenekbcdd3312008-04-30 23:47:12 +0000569sub AddIfNotPresent {
570 my $Args = shift;
571 my $Arg = shift;
572 my $found = 0;
573
574 foreach my $k (@$Args) {
575 if ($k eq $Arg) {
576 $found = 1;
577 last;
578 }
579 }
580
581 if ($found == 0) {
582 push @$Args, $Arg;
583 }
584}
585
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000586sub RunBuildCommand {
587
588 my $Args = shift;
Ted Kremenek091f9372008-04-02 16:04:51 +0000589 my $IgnoreErrors = shift;
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000590 my $Cmd = $Args->[0];
Ted Kremenek0caa57d2008-06-02 21:52:47 +0000591 my $CCAnalyzer = shift;
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000592
Ted Kremenek900b0522008-06-30 18:18:16 +0000593 # Get only the part of the command after the last '/'.
594 if ($Cmd =~ /\/([^\/]+)$/) {
595 $Cmd = $1;
596 }
597
Ted Kremenek404cb262008-04-02 15:34:12 +0000598 if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc") {
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000599 shift @$Args;
Ted Kremenek0caa57d2008-06-02 21:52:47 +0000600 unshift @$Args, $CCAnalyzer;
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000601 }
Ted Kremenek091f9372008-04-02 16:04:51 +0000602 elsif ($IgnoreErrors) {
603 if ($Cmd eq "make" or $Cmd eq "gmake") {
Ted Kremenekbcdd3312008-04-30 23:47:12 +0000604 AddIfNotPresent($Args,"-k");
Ted Kremenek135ef8d2008-05-13 21:28:02 +0000605 AddIfNotPresent($Args,"-i");
Ted Kremenek091f9372008-04-02 16:04:51 +0000606 }
607 elsif ($Cmd eq "xcodebuild") {
Ted Kremenekbcdd3312008-04-30 23:47:12 +0000608 AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES");
Ted Kremenek091f9372008-04-02 16:04:51 +0000609 }
Ted Kremenekbcdd3312008-04-30 23:47:12 +0000610 }
611
Ted Kremenekbcdd3312008-04-30 23:47:12 +0000612 if ($Cmd eq "xcodebuild") {
Ted Kremenek23df1d22008-05-23 22:18:16 +0000613 # Disable distributed builds for xcodebuild.
Ted Kremenekbcdd3312008-04-30 23:47:12 +0000614 AddIfNotPresent($Args,"-nodistribute");
Ted Kremenek23df1d22008-05-23 22:18:16 +0000615
616 # Disable PCH files until clang supports them.
617 AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO");
Ted Kremenekaa0d3722008-05-27 23:18:07 +0000618
619 # When 'CC' is set, xcodebuild uses it to do all linking, even if we are
620 # linking C++ object files. Set 'LDPLUSPLUS' so that xcodebuild uses 'g++'
621 # when linking such files.
622 my $LDPLUSPLUS = `which g++`;
623 $LDPLUSPLUS =~ s/\015?\012//; # strip newlines
624 $ENV{'LDPLUSPLUS'} = $LDPLUSPLUS;
Ted Kremenekbcdd3312008-04-30 23:47:12 +0000625 }
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000626
Ted Kremenek46035572008-07-15 17:09:28 +0000627 return system(@$Args);
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000628}
629
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000630##----------------------------------------------------------------------------##
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000631# DisplayHelp - Utility function to display all help options.
632##----------------------------------------------------------------------------##
633
Sam Bishop479982e2008-04-02 03:35:43 +0000634sub DisplayHelp {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000635
Ted Kremeneked757e02008-04-02 18:03:36 +0000636print <<ENDTEXT;
Sam Bishop479982e2008-04-02 03:35:43 +0000637USAGE: $Prog [options] <build command> [build options]
Ted Kremenekb9177df2008-04-01 21:22:03 +0000638
Ted Kremenekf54e8882008-05-23 18:17:05 +0000639ENDTEXT
640
641 if (defined($BuildName)) {
642 print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n";
643 }
644
645print <<ENDTEXT;
Ted Kremenekb9177df2008-04-01 21:22:03 +0000646OPTIONS:
647
Ted Kremenekfea39522008-07-15 22:03:09 +0000648 -o - Target directory for HTML report files. Subdirectories
Sam Bishop479982e2008-04-02 03:35:43 +0000649 will be created as needed to represent separate "runs" of
Ted Kremenekb9177df2008-04-01 21:22:03 +0000650 the analyzer. If this option is not specified, a directory
651 is created in /tmp to store the reports.
Ted Kremenek5979b082008-05-14 20:10:33 +0000652
Ted Kremenekfea39522008-07-15 22:03:09 +0000653 -h - Display this message.
654 --help
Ted Kremenek5979b082008-05-14 20:10:33 +0000655
Ted Kremenekfea39522008-07-15 22:03:09 +0000656 -k - Add a "keep on going" option to the specified build command.
657 --keep-going This option currently supports make and xcodebuild.
Ted Kremenek707a9ad2008-04-02 16:41:25 +0000658 This is a convenience option; one can specify this
659 behavior directly using build options.
Ted Kremenekb9177df2008-04-01 21:22:03 +0000660
Ted Kremenekfea39522008-07-15 22:03:09 +0000661 --status-bugs - By default, the exit status of $Prog is the same as the
662 executed build command. Specifying this option causes the
663 exit status of $Prog to be 1 if it found potential bugs
664 and 0 otherwise.
Ted Kremenekb9177df2008-04-01 21:22:03 +0000665
Ted Kremenekfea39522008-07-15 22:03:09 +0000666 -v - Verbose output from $Prog and the analyzer.
667 A second and third "-v" increases verbosity.
668
669 -V - View analysis results in a web browser when the build
670 --view completes.
Ted Kremenekb59b7542008-04-02 18:42:49 +0000671
Ted Kremenekadcf72b2008-07-15 17:06:13 +0000672ENDTEXT
673
Ted Kremenekfea39522008-07-15 22:03:09 +0000674 print " Available Source Code Analyses (multiple analyses may be specified):\n\n";
Ted Kremenekadcf72b2008-07-15 17:06:13 +0000675
676 foreach my $Analysis (sort keys %AvailableAnalyses) {
677 if (defined($AnalysesDefaultEnabled{$Analysis})) {
Ted Kremenekfea39522008-07-15 22:03:09 +0000678 print " (+)";
Ted Kremenekadcf72b2008-07-15 17:06:13 +0000679 }
680 else {
Ted Kremenekfea39522008-07-15 22:03:09 +0000681 print " ";
Ted Kremenekadcf72b2008-07-15 17:06:13 +0000682 }
683
684 print " $Analysis $AvailableAnalyses{$Analysis}\n";
685 }
686
687print <<ENDTEXT
688
Ted Kremenekfea39522008-07-15 22:03:09 +0000689 NOTE: "(+)" indicates that an analysis is enabled by default unless one
690 or more analysis options are specified
Ted Kremenekadcf72b2008-07-15 17:06:13 +0000691
Ted Kremenekb9177df2008-04-01 21:22:03 +0000692BUILD OPTIONS
693
Ted Kremenekfea39522008-07-15 22:03:09 +0000694 You can specify any build option acceptable to the build command.
Ted Kremenekf76812d2008-04-02 16:47:27 +0000695
Ted Kremeneked757e02008-04-02 18:03:36 +0000696EXAMPLE
Ted Kremenekb9177df2008-04-01 21:22:03 +0000697
Ted Kremenekfea39522008-07-15 22:03:09 +0000698 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenekb9177df2008-04-01 21:22:03 +0000699
Ted Kremenekfea39522008-07-15 22:03:09 +0000700 The above example causes analysis reports to be deposited into
701 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
702 A different subdirectory is created each time $Prog analyzes a project.
703 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenekb9177df2008-04-01 21:22:03 +0000704
705ENDTEXT
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000706}
707
708##----------------------------------------------------------------------------##
709# Process command-line arguments.
710##----------------------------------------------------------------------------##
711
712my $HtmlDir; # Parent directory to store HTML files.
713my $IgnoreErrors = 0; # Ignore build errors.
Ted Kremenekb59b7542008-04-02 18:42:49 +0000714my $ViewResults = 0; # View results when the build terminates.
Ted Kremenekfea39522008-07-15 22:03:09 +0000715my $ExitStatusFoundBugs = 0; # Exit status reflects whether bugs were found
Ted Kremenekadcf72b2008-07-15 17:06:13 +0000716my @AnalysesToRun;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000717
Ted Kremenekfea39522008-07-15 22:03:09 +0000718
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000719if (!@ARGV) {
720 DisplayHelp();
Sam Bishop479982e2008-04-02 03:35:43 +0000721 exit 1;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000722}
723
724while (@ARGV) {
725
726 # Scan for options we recognize.
727
728 my $arg = $ARGV[0];
729
Sam Bishopa3281512008-04-03 14:29:47 +0000730 if ($arg eq "-h" or $arg eq "--help") {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000731 DisplayHelp();
Sam Bishop479982e2008-04-02 03:35:43 +0000732 exit 0;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000733 }
734
Ted Kremenekadcf72b2008-07-15 17:06:13 +0000735 if (defined($AvailableAnalyses{$arg})) {
Ted Kremenek5979b082008-05-14 20:10:33 +0000736 shift @ARGV;
Ted Kremenekadcf72b2008-07-15 17:06:13 +0000737 push @AnalysesToRun, $arg;
Ted Kremenek5979b082008-05-14 20:10:33 +0000738 next;
739 }
740
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000741 if ($arg eq "-o") {
742 shift @ARGV;
743
744 if (!@ARGV) {
Ted Kremenekc2007a92008-06-16 22:40:14 +0000745 DieDiag("'-o' option requires a target directory name.\n");
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000746 }
747
748 $HtmlDir = shift @ARGV;
749 next;
750 }
751
Ted Kremenekb9177df2008-04-01 21:22:03 +0000752 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000753 shift @ARGV;
754 $IgnoreErrors = 1;
755 next;
756 }
757
758 if ($arg eq "-v") {
759 shift @ARGV;
760 $Verbose++;
761 next;
762 }
763
Ted Kremenekb59b7542008-04-02 18:42:49 +0000764 if ($arg eq "-V" or $arg eq "--view") {
765 shift @ARGV;
766 $ViewResults = 1;
767 next;
768 }
769
Ted Kremenekfea39522008-07-15 22:03:09 +0000770 if ($arg eq "--status-bugs") {
771 shift @ARGV;
772 $ExitStatusFoundBugs = 1;
773 next;
774 }
775
Ted Kremenekc2007a92008-06-16 22:40:14 +0000776 DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/);
Ted Kremenek78965de2008-04-02 16:35:01 +0000777
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000778 last;
779}
780
781if (!@ARGV) {
Ted Kremenekc2007a92008-06-16 22:40:14 +0000782 Diag("No build command specified.\n\n");
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000783 DisplayHelp();
Sam Bishop479982e2008-04-02 03:35:43 +0000784 exit 1;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000785}
786
787# Determine the output directory for the HTML reports.
788
789if (!defined($HtmlDir)) {
790
Sam Bishop479982e2008-04-02 03:35:43 +0000791 $HtmlDir = mkdtemp("/tmp/$Prog-XXXXXX");
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000792
793 if (!defined($HtmlDir)) {
Ted Kremenekc2007a92008-06-16 22:40:14 +0000794 DieDiag("Cannot create HTML directory in /tmp.\n");
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000795 }
796
797 if (!$Verbose) {
Ted Kremenekc2007a92008-06-16 22:40:14 +0000798 Diag("Using '$HtmlDir' as base HTML report directory.\n");
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000799 }
800}
801
Ted Kremenekc5b38202008-04-18 15:18:20 +0000802my $BaseDir = $HtmlDir;
Sam Bishop479982e2008-04-02 03:35:43 +0000803$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000804
805# Set the appropriate environment variables.
806
Sam Bishop479982e2008-04-02 03:35:43 +0000807SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000808
Ted Kremenekfc0f1c22008-04-08 20:22:12 +0000809my $Cmd = "$RealBin/ccc-analyzer";
810
Ted Kremenekc2007a92008-06-16 22:40:14 +0000811DieDiag("Executable 'ccc-analyzer' does not exist at '$Cmd'\n")
Ted Kremenekfc0f1c22008-04-08 20:22:12 +0000812 if (! -x $Cmd);
Ted Kremenek056171b2008-04-18 22:00:56 +0000813
Ted Kremenekadcf72b2008-07-15 17:06:13 +0000814if (! -x $ClangSB) {
815 Diag("'clang' executable not found in '$RealBin'.\n");
816 Diag("Using 'clang' from path.\n");
Ted Kremenek056171b2008-04-18 22:00:56 +0000817}
Ted Kremenekfc0f1c22008-04-08 20:22:12 +0000818
Ted Kremenek47bf3892008-04-03 20:08:18 +0000819$ENV{'CC'} = $Cmd;
Ted Kremenek056171b2008-04-18 22:00:56 +0000820$ENV{'CLANG'} = $Clang;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000821
822if ($Verbose >= 2) {
823 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
824}
825
Ted Kremenek62e737d2008-05-12 22:07:14 +0000826if ($Verbose >= 3) {
827 $ENV{'CCC_ANALYZER_LOG'} = 1;
828}
829
Ted Kremenek47219912008-07-15 23:41:32 +0000830if (scalar(@AnalysesToRun) == 0) {
831 foreach my $key (keys %AnalysesDefaultEnabled) {
832 push @AnalysesToRun,$key;
833 }
Ted Kremenek70633652008-07-02 23:16:10 +0000834}
Ted Kremenek5979b082008-05-14 20:10:33 +0000835
Ted Kremenek47219912008-07-15 23:41:32 +0000836$ENV{'CCC_ANALYZER_ANALYSIS'} = join ' ',@AnalysesToRun;
837
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000838# Run the build.
839
Ted Kremenek46035572008-07-15 17:09:28 +0000840my $ExitStatus = RunBuildCommand(\@ARGV, $IgnoreErrors, $Cmd);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000841
842# Postprocess the HTML directory.
843
Ted Kremenekfea39522008-07-15 22:03:09 +0000844my $NumBugs = Postprocess($HtmlDir, $BaseDir);
Ted Kremenekb59b7542008-04-02 18:42:49 +0000845
846if ($ViewResults and -r "$HtmlDir/index.html") {
847 # Only works on Mac OS X (for now).
848 print "Viewing analysis results: '$HtmlDir/index.html'\n";
Ted Kremenekbd5e3282008-07-15 20:18:21 +0000849 system("open", "$HtmlDir/index.html");
Ted Kremenekb59b7542008-04-02 18:42:49 +0000850}
Ted Kremenek46035572008-07-15 17:09:28 +0000851
Ted Kremenekfea39522008-07-15 22:03:09 +0000852if ($ExitStatusFoundBugs) {
853 exit 1 if ($NumBugs > 0);
854 exit 0;
855}
856
Ted Kremenek46035572008-07-15 17:09:28 +0000857exit $ExitStatus;
858