blob: f881cc8ea67f742178352a86020b32c66bbd4d33 [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;
17use File::Temp qw/ :mktemp /;
Ted Kremenek22d6a632008-04-02 20:43:36 +000018use FindBin qw($RealBin);
Ted Kremeneka6e24812008-04-19 18:05:48 +000019use Digest::MD5;
Ted Kremenek7a4648d2008-05-02 22:04:53 +000020use File::Basename;
Ted Kremenek23cfca32008-06-16 22:40:14 +000021use Term::ANSIColor;
22use Term::ANSIColor qw(:constants);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000023
24my $Verbose = 0; # Verbose output from this script.
25my $Prog = "scan-build";
Ted Kremenekf4cdf412008-05-23 18:17:05 +000026my $BuildName;
27my $BuildDate;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000028
Ted Kremenekf2f8d6c2008-06-17 03:06:59 +000029my $UseColor = ((($ENV{'TERM'} eq 'xterm-color') and -t STDOUT)
30 and defined($ENV{'SCAN_BUILD_COLOR'}));
Ted Kremenek23cfca32008-06-16 22:40:14 +000031
32sub Diag {
33 if ($UseColor) {
34 print BOLD, MAGENTA "$Prog: @_";
35 print RESET;
36 }
37 else {
38 print "$Prog: @_";
39 }
40}
41
42sub DieDiag {
43 if ($UseColor) {
44 print BOLD, RED "$Prog: ";
45 print RESET, RED @_;
46 print RESET;
47 }
48 else {
49 print "$Prog: ", @_;
50 }
51 exit(0);
52}
53
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000054##----------------------------------------------------------------------------##
55# GetHTMLRunDir - Construct an HTML directory name for the current run.
56##----------------------------------------------------------------------------##
57
Sam Bishopa0e22662008-04-02 03:35:43 +000058sub GetHTMLRunDir {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000059
60 die "Not enough arguments." if (@_ == 0);
61
62 my $Dir = shift @_;
63
64 # Get current date and time.
65
66 my @CurrentTime = localtime();
67
68 my $year = $CurrentTime[5] + 1900;
69 my $day = $CurrentTime[3];
70 my $month = $CurrentTime[4] + 1;
71
Ted Kremenek9d7405f2008-05-14 17:23:56 +000072 my $DateString = sprintf("%d-%02d-%02d", $year, $month, $day);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000073
74 # Determine the run number.
75
76 my $RunNumber;
77
78 if (-d $Dir) {
79
80 if (! -r $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +000081 DieDiag("directory '$Dir' exists but is not readable.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000082 }
83
84 # Iterate over all files in the specified directory.
85
86 my $max = 0;
87
88 opendir(DIR, $Dir);
89 my @FILES= readdir(DIR);
90 closedir(DIR);
91
92 foreach my $f (@FILES) {
93
94 my @x = split/-/, $f;
95
96 next if (scalar(@x) != 4);
97 next if ($x[0] != $year);
98 next if ($x[1] != $month);
99 next if ($x[2] != $day);
100
101 if ($x[3] > $max) {
102 $max = $x[3];
103 }
104 }
105
106 $RunNumber = $max + 1;
107 }
108 else {
109
110 if (-x $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000111 DieDiag("'$Dir' exists but is not a directory.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000112 }
113
114 # $Dir does not exist. It will be automatically created by the
115 # clang driver. Set the run number to 1.
116
117 $RunNumber = 1;
118 }
119
120 die "RunNumber must be defined!" if (!defined($RunNumber));
121
122 # Append the run number.
123
124 return "$Dir/$DateString-$RunNumber";
125}
126
Sam Bishopa0e22662008-04-02 03:35:43 +0000127sub SetHtmlEnv {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000128
129 die "Wrong number of arguments." if (scalar(@_) != 2);
130
131 my $Args = shift;
132 my $Dir = shift;
133
134 die "No build command." if (scalar(@$Args) == 0);
135
136 my $Cmd = $$Args[0];
137
138 if ($Cmd =~ /configure/) {
139 return;
140 }
141
142 if ($Verbose) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000143 Diag("Emitting reports for this run to '$Dir'.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000144 }
145
146 $ENV{'CCC_ANALYZER_HTML'} = $Dir;
147}
148
149##----------------------------------------------------------------------------##
Ted Kremenek57cf4462008-04-18 15:09:30 +0000150# ComputeDigest - Compute a digest of the specified file.
151##----------------------------------------------------------------------------##
152
153sub ComputeDigest {
154 my $FName = shift;
Ted Kremenek23cfca32008-06-16 22:40:14 +0000155 DieDiag("Cannot read $FName to compute Digest.\n") if (! -r $FName);
Ted Kremeneka6e24812008-04-19 18:05:48 +0000156
157 # Use Digest::MD5. We don't have to be cryptographically secure. We're
Ted Kremenek7ea02e62008-04-19 18:07:44 +0000158 # just looking for duplicate files that come from a non-malicious source.
159 # We use Digest::MD5 because it is a standard Perl module that should
Ted Kremeneka6e24812008-04-19 18:05:48 +0000160 # come bundled on most systems.
161
Ted Kremenek23cfca32008-06-16 22:40:14 +0000162 open(FILE, $FName) or DieDiag("Cannot open $FName when computing Digest.\n");
Ted Kremeneka6e24812008-04-19 18:05:48 +0000163 binmode FILE;
164 my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest;
165 close(FILE);
166
167 # Return the digest.
168
169 return $Result;
Ted Kremenek57cf4462008-04-18 15:09:30 +0000170}
171
172##----------------------------------------------------------------------------##
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000173# UpdatePrefix - Compute the common prefix of files.
174##----------------------------------------------------------------------------##
175
176my $Prefix;
177
178sub UpdatePrefix {
179
180 my $x = shift;
181 my $y = basename($x);
182 $x =~ s/\Q$y\E$//;
183
184 # Ignore /usr, /Library, /System, /Developer
185
186 return if ( $x =~ /^\/usr/ or $x =~ /^\/Library/
187 or $x =~ /^\/System/ or $x =~ /^\/Developer/);
188
189
190 if (!defined $Prefix) {
191 $Prefix = $x;
192 return;
193 }
194
195 chop $Prefix while (!($x =~ /^$Prefix/));
196}
197
198sub GetPrefix {
199 return $Prefix;
200}
201
202##----------------------------------------------------------------------------##
203# UpdateInFilePath - Update the path in the report file.
204##----------------------------------------------------------------------------##
205
206sub UpdateInFilePath {
207 my $fname = shift;
208 my $regex = shift;
209 my $newtext = shift;
210
211 open (RIN, $fname) or die "cannot open $fname";
212 open (ROUT, ">$fname.tmp") or die "cannot open $fname.tmp";
213
214 while (<RIN>) {
215 s/$regex/$newtext/;
216 print ROUT $_;
217 }
218
219 close (ROUT);
220 close (RIN);
221 `mv $fname.tmp $fname`;
222}
223
224##----------------------------------------------------------------------------##
Ted Kremenek5744dc22008-04-02 18:03:36 +0000225# ScanFile - Scan a report file for various identifying attributes.
226##----------------------------------------------------------------------------##
227
Ted Kremenek57cf4462008-04-18 15:09:30 +0000228# Sometimes a source file is scanned more than once, and thus produces
229# multiple error reports. We use a cache to solve this problem.
230
231my %AlreadyScanned;
232
Ted Kremenek5744dc22008-04-02 18:03:36 +0000233sub ScanFile {
234
235 my $Index = shift;
236 my $Dir = shift;
237 my $FName = shift;
238
Ted Kremenek57cf4462008-04-18 15:09:30 +0000239 # Compute a digest for the report file. Determine if we have already
240 # scanned a file that looks just like it.
241
242 my $digest = ComputeDigest("$Dir/$FName");
243
244 if (defined($AlreadyScanned{$digest})) {
245 # Redundant file. Remove it.
246 `rm -f $Dir/$FName`;
247 return;
248 }
249
250 $AlreadyScanned{$digest} = 1;
251
Ted Kremenek809709f2008-04-18 16:58:34 +0000252 # At this point the report file is not world readable. Make it happen.
Ted Kremenek684bb092008-04-18 15:18:20 +0000253 `chmod 644 $Dir/$FName`;
254
255 # Scan the report file for tags.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000256 open(IN, "$Dir/$FName") or DieDiag("Cannot open '$Dir/$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000257
258 my $BugDesc = "";
Ted Kremenek22d6a632008-04-02 20:43:36 +0000259 my $BugFile = "";
260 my $BugPathLength = 1;
261 my $BugLine = 0;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000262
263 while (<IN>) {
264
265 if (/<!-- BUGDESC (.*) -->$/) {
266 $BugDesc = $1;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000267 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000268 elsif (/<!-- BUGFILE (.*) -->$/) {
269 $BugFile = $1;
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000270 UpdatePrefix($BugFile);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000271 }
272 elsif (/<!-- BUGPATHLENGTH (.*) -->$/) {
273 $BugPathLength = $1;
274 }
275 elsif (/<!-- BUGLINE (.*) -->$/) {
276 $BugLine = $1;
277 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000278 }
279
280 close(IN);
281
Ted Kremenek22d6a632008-04-02 20:43:36 +0000282 push @$Index,[ $FName, $BugDesc, $BugFile, $BugLine, $BugPathLength ];
283}
284
285##----------------------------------------------------------------------------##
286# CopyJS - Copy JavaScript code to target directory.
287##----------------------------------------------------------------------------##
288
289sub CopyJS {
290
291 my $Dir = shift;
292
Ted Kremenek23cfca32008-06-16 22:40:14 +0000293 DieDiag("Cannot find 'sorttable.js'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000294 if (! -r "$RealBin/sorttable.js");
295
296 `cp $RealBin/sorttable.js $Dir`;
297
Ted Kremenek23cfca32008-06-16 22:40:14 +0000298 DieDiag("Could not copy 'sorttable.js' to '$Dir'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000299 if (! -r "$Dir/sorttable.js");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000300}
301
302##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000303# Postprocess - Postprocess the results of an analysis scan.
304##----------------------------------------------------------------------------##
305
Sam Bishopa0e22662008-04-02 03:35:43 +0000306sub Postprocess {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000307
308 my $Dir = shift;
Ted Kremenek684bb092008-04-18 15:18:20 +0000309 my $BaseDir = shift;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000310
311 die "No directory specified." if (!defined($Dir));
Ted Kremenek684bb092008-04-18 15:18:20 +0000312 die "No base directory specified." if (!defined($BaseDir));
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000313
314 if (! -d $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000315 Diag("No bugs found.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000316 return;
317 }
318
319 opendir(DIR, $Dir);
320 my @files = grep(/^report-.*\.html$/,readdir(DIR));
321 closedir(DIR);
322
323 if (scalar(@files) == 0) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000324 Diag("Removing directory '$Dir' because it contains no reports.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000325 `rm -fR $Dir`;
Ted Kremenek23cfca32008-06-16 22:40:14 +0000326
327 # Remove the base directory if it contains no files (don't use '-R').
328 `rm -f $BaseDir`;
329
330 Diag("No bugs found.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000331 return;
332 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000333
334 # Scan each report file and build an index.
335
336 my @Index;
337
338 foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); }
339
340 # Generate an index.html file.
341
342 my $FName = "$Dir/index.html";
343
Ted Kremenek23cfca32008-06-16 22:40:14 +0000344 open(OUT, ">$FName") or DieDiag("Cannot create file '$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000345
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000346 # Print out the header.
347
Ted Kremenek5744dc22008-04-02 18:03:36 +0000348print OUT <<ENDTEXT;
349<html>
350<head>
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000351<style type="text/css">
352 body { color:#000000; background-color:#ffffff }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000353 body { font-family: Helvetica, sans-serif; font-size:9pt }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000354 h1 { font-size:12pt }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000355 table.sortable thead {
356 background-color:#eee; color:#666666;
357 font-weight: bold; cursor: default;
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000358 text-align:center;
359 border-top: 2px solid #000000;
360 border-bottom: 2px solid #000000;
361 font-weight: bold; font-family: Verdana
362 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000363 table.sortable { border: 1px #000000 solid }
364 table.sortable { border-collapse: collapse; border-spacing: 0px }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000365 td { border-bottom: 1px #000000 dotted }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000366 td { padding:5px; padding-left:8px; padding-right:8px }
Ted Kremenekd8c6d0c2008-04-07 23:50:07 +0000367 td { text-align:left; font-size:9pt }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000368 td.View { padding-left: 10px }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000369</style>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000370<script src="sorttable.js"></script>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000371<script language='javascript' type="text/javascript">
372function SetDisplay(RowClass, DisplayVal)
373{
374 var Rows = document.getElementsByTagName("tr");
375 for ( var i = 0 ; i < Rows.length; ++i ) {
376 if (Rows[i].className == RowClass) {
377 Rows[i].style.display = DisplayVal;
378 }
379 }
380}
381
382function ToggleDisplay(CheckButton, ClassName) {
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000383 if (CheckButton.checked) {
384 SetDisplay(ClassName, "");
385 }
386 else {
387 SetDisplay(ClassName, "none");
388 }
389}
390</script>
391</head>
392<body>
393ENDTEXT
394
395 # Print out the summary table.
396
397 my %Totals;
398
399 for my $row ( @Index ) {
400
Ted Kremenek432af592008-05-06 18:11:36 +0000401 #my $bug_type = lc($row->[1]);
402 my $bug_type = ($row->[1]);
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000403
404 if (!defined($Totals{$bug_type})) {
405 $Totals{$bug_type} = 1;
406 }
407 else {
408 $Totals{$bug_type}++;
409 }
410 }
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000411
412 print OUT "<h3>Summary</h3>";
413
414 if (defined($BuildName)) {
415 print OUT "\n<p>Results in this analysis run are based on analyzer build <b>$BuildName</b>.</p>\n"
416 }
417
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000418print OUT <<ENDTEXT;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000419<table class="sortable">
420<tr>
421 <td>Bug Type</td>
422 <td>Quantity</td>
423 <td "sorttable_nosort">Display?</td>
424</tr>
425ENDTEXT
426
427 for my $key ( sort { $a cmp $b } keys %Totals ) {
Ted Kremenekbdf66c72008-05-06 23:51:45 +0000428 my $x = lc($key);
429 $x =~ s/\s[,]/_/g;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000430 print OUT "<tr><td>$key</td><td>$Totals{$key}</td><td><input type=\"checkbox\" onClick=\"ToggleDisplay(this,'bt_$x');\" checked/></td></tr>\n";
431 }
432
433 # Print out the table of errors.
434
435print OUT <<ENDTEXT;
436</table>
437<h3>Reports</h3>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000438<table class="sortable">
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000439<tr>
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000440 <td>Bug Type</td>
441 <td>File</td>
442 <td>Line</td>
443 <td>Path Length</td>
444 <td "sorttable_nosort"></td>
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000445</tr>
Ted Kremenek5744dc22008-04-02 18:03:36 +0000446ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000447
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000448 my $prefix = GetPrefix();
449 my $regex;
450 my $InFileRegex;
451 my $InFilePrefix = "File:</td><td>";
452
453 if (defined($prefix)) {
454 $regex = qr/^\Q$prefix\E/is;
455 $InFileRegex = qr/\Q$InFilePrefix$prefix\E/is;
456 }
457
Ted Kremenek5744dc22008-04-02 18:03:36 +0000458 for my $row ( sort { $a->[1] cmp $b->[1] } @Index ) {
459
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000460 my $x = lc($row->[1]);
Ted Kremenekbdf66c72008-05-06 23:51:45 +0000461 $x =~ s/\s[,]/_/g;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000462
463 print OUT "<tr class=\"bt_$x\">\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000464
Ted Kremenek5744dc22008-04-02 18:03:36 +0000465 my $ReportFile = $row->[0];
466
Ted Kremenek22d6a632008-04-02 20:43:36 +0000467 print OUT " <td class=\"DESC\">";
Ted Kremenek432af592008-05-06 18:11:36 +0000468 #print OUT lc($row->[1]);
469 print OUT $row->[1];
Ted Kremenek22d6a632008-04-02 20:43:36 +0000470 print OUT "</td>\n";
Ted Kremenek5744dc22008-04-02 18:03:36 +0000471
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000472 # Update the file prefix.
473
474 my $fname = $row->[2];
475 if (defined($regex)) {
476 $fname =~ s/$regex//;
477 UpdateInFilePath("$Dir/$ReportFile", $InFileRegex, $InFilePrefix)
478 }
Ted Kremenek3e56e0b2008-05-02 23:40:49 +0000479
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000480 print OUT "<td>$fname</td>\n";
481
482 # Print the rest of the columns.
483
484 for my $j ( 3 .. $#{$row} ) {
Ted Kremenek5744dc22008-04-02 18:03:36 +0000485 print OUT "<td>$row->[$j]</td>\n"
486 }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000487
488 # Emit the "View" link.
Ted Kremenek5744dc22008-04-02 18:03:36 +0000489
Ted Kremenek22d6a632008-04-02 20:43:36 +0000490 print OUT " <td class=\"View\"><a href=\"$ReportFile#EndPath\">View</a></td>\n";
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000491
492 # End the row.
Ted Kremenek5744dc22008-04-02 18:03:36 +0000493 print OUT "</tr>\n";
494 }
495
496 print OUT "</table>\n</body></html>\n";
497 close(OUT);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000498
Ted Kremenek22d6a632008-04-02 20:43:36 +0000499 CopyJS($Dir);
Ted Kremenek684bb092008-04-18 15:18:20 +0000500
501 # Make sure $Dir and $BaseDir is world readable/executable.
502 `chmod 755 $Dir`;
503 `chmod 755 $BaseDir`;
Ted Kremenek23cfca32008-06-16 22:40:14 +0000504
505 my $Num = scalar(@Index);
506 Diag("$Num bugs found.\n")
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000507}
508
509##----------------------------------------------------------------------------##
Ted Kremenekdab11102008-04-02 04:43:42 +0000510# RunBuildCommand - Run the build command.
511##----------------------------------------------------------------------------##
512
Ted Kremenek6b628982008-04-30 23:47:12 +0000513sub AddIfNotPresent {
514 my $Args = shift;
515 my $Arg = shift;
516 my $found = 0;
517
518 foreach my $k (@$Args) {
519 if ($k eq $Arg) {
520 $found = 1;
521 last;
522 }
523 }
524
525 if ($found == 0) {
526 push @$Args, $Arg;
527 }
528}
529
Ted Kremenekdab11102008-04-02 04:43:42 +0000530sub RunBuildCommand {
531
532 my $Args = shift;
Ted Kremenek7442ca62008-04-02 16:04:51 +0000533 my $IgnoreErrors = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000534 my $Cmd = $Args->[0];
Ted Kremenek6195c372008-06-02 21:52:47 +0000535 my $CCAnalyzer = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000536
Ted Kremenek3301cb12008-06-30 18:18:16 +0000537 # Get only the part of the command after the last '/'.
538 if ($Cmd =~ /\/([^\/]+)$/) {
539 $Cmd = $1;
540 }
541
Ted Kremenek6a43ba92008-04-02 15:34:12 +0000542 if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc") {
Ted Kremenekdab11102008-04-02 04:43:42 +0000543 shift @$Args;
Ted Kremenek6195c372008-06-02 21:52:47 +0000544 unshift @$Args, $CCAnalyzer;
Ted Kremenekdab11102008-04-02 04:43:42 +0000545 }
Ted Kremenek7442ca62008-04-02 16:04:51 +0000546 elsif ($IgnoreErrors) {
547 if ($Cmd eq "make" or $Cmd eq "gmake") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000548 AddIfNotPresent($Args,"-k");
Ted Kremenek8912b542008-05-13 21:28:02 +0000549 AddIfNotPresent($Args,"-i");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000550 }
551 elsif ($Cmd eq "xcodebuild") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000552 AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000553 }
Ted Kremenek6b628982008-04-30 23:47:12 +0000554 }
555
Ted Kremenek6b628982008-04-30 23:47:12 +0000556 if ($Cmd eq "xcodebuild") {
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000557 # Disable distributed builds for xcodebuild.
Ted Kremenek6b628982008-04-30 23:47:12 +0000558 AddIfNotPresent($Args,"-nodistribute");
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000559
560 # Disable PCH files until clang supports them.
561 AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO");
Ted Kremenek915e9722008-05-27 23:18:07 +0000562
563 # When 'CC' is set, xcodebuild uses it to do all linking, even if we are
564 # linking C++ object files. Set 'LDPLUSPLUS' so that xcodebuild uses 'g++'
565 # when linking such files.
566 my $LDPLUSPLUS = `which g++`;
567 $LDPLUSPLUS =~ s/\015?\012//; # strip newlines
568 $ENV{'LDPLUSPLUS'} = $LDPLUSPLUS;
Ted Kremenek6b628982008-04-30 23:47:12 +0000569 }
Ted Kremenekdab11102008-04-02 04:43:42 +0000570
571 system(@$Args);
572}
573
Ted Kremenekdab11102008-04-02 04:43:42 +0000574##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000575# DisplayHelp - Utility function to display all help options.
576##----------------------------------------------------------------------------##
577
Sam Bishopa0e22662008-04-02 03:35:43 +0000578sub DisplayHelp {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000579
Ted Kremenek5744dc22008-04-02 18:03:36 +0000580print <<ENDTEXT;
Sam Bishopa0e22662008-04-02 03:35:43 +0000581USAGE: $Prog [options] <build command> [build options]
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000582
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000583ENDTEXT
584
585 if (defined($BuildName)) {
586 print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n";
587 }
588
589print <<ENDTEXT;
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000590OPTIONS:
591
Ted Kremenek1262fc42008-05-14 20:10:33 +0000592 -a - The analysis to run. The default is 'checker-cfref'.
593 Valid options are: 'checker-cfref', 'fsyntax-only'
594
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000595 -o - Target directory for HTML report files. Subdirectories
Sam Bishopa0e22662008-04-02 03:35:43 +0000596 will be created as needed to represent separate "runs" of
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000597 the analyzer. If this option is not specified, a directory
598 is created in /tmp to store the reports.
Ted Kremenek1262fc42008-05-14 20:10:33 +0000599
Ted Kremenek10f883f2008-04-03 07:11:44 +0000600 -h - Display this message.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000601 --help
Ted Kremenek1262fc42008-05-14 20:10:33 +0000602
Ted Kremenekaf08f642008-04-02 16:31:58 +0000603 -k - Add a "keep on going" option to the specified build command.
Ted Kremenekf02e8db2008-04-02 16:41:25 +0000604 --keep-going This option currently supports make and xcodebuild.
605 This is a convenience option; one can specify this
606 behavior directly using build options.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000607
Ted Kremenekdab11102008-04-02 04:43:42 +0000608 -v - Verbose output from $Prog and the analyzer.
609 A second "-v" increases verbosity.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000610
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000611 -V - View analysis results in a web browser when the build
612 --view completes.
613
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000614BUILD OPTIONS
615
Ted Kremenek39eefde2008-04-02 16:47:27 +0000616 You can specify any build option acceptable to the build command.
617
Ted Kremenek5744dc22008-04-02 18:03:36 +0000618EXAMPLE
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000619
Ted Kremenek5744dc22008-04-02 18:03:36 +0000620 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000621
Ted Kremenek39eefde2008-04-02 16:47:27 +0000622 The above example causes analysis reports to be deposited into
623 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
624 A different subdirectory is created each time $Prog analyzes a project.
625 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000626
627ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000628}
629
630##----------------------------------------------------------------------------##
631# Process command-line arguments.
632##----------------------------------------------------------------------------##
633
634my $HtmlDir; # Parent directory to store HTML files.
635my $IgnoreErrors = 0; # Ignore build errors.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000636my $ViewResults = 0; # View results when the build terminates.
Ted Kremenek1262fc42008-05-14 20:10:33 +0000637my $Analysis = "checker-cfref";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000638
639if (!@ARGV) {
640 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000641 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000642}
643
644while (@ARGV) {
645
646 # Scan for options we recognize.
647
648 my $arg = $ARGV[0];
649
Sam Bishop2f2418e2008-04-03 14:29:47 +0000650 if ($arg eq "-h" or $arg eq "--help") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000651 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000652 exit 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000653 }
654
Ted Kremenek1262fc42008-05-14 20:10:33 +0000655 if ($arg eq "-a") {
656 shift @ARGV;
657
658 if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000659 DieDiag("'-a' option requires an analysis type.\n");
Ted Kremenek1262fc42008-05-14 20:10:33 +0000660 }
661
662 $Analysis = shift @ARGV;
663
664 if (!($Analysis eq "checker-cfref" or $Analysis eq "fsyntax-only")) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000665 DieDiag("Invalid argument '$Analysis' to -a.\n");
Ted Kremenek1262fc42008-05-14 20:10:33 +0000666 }
667
668 next;
669 }
670
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000671 if ($arg eq "-o") {
672 shift @ARGV;
673
674 if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000675 DieDiag("'-o' option requires a target directory name.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000676 }
677
678 $HtmlDir = shift @ARGV;
679 next;
680 }
681
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000682 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000683 shift @ARGV;
684 $IgnoreErrors = 1;
685 next;
686 }
687
688 if ($arg eq "-v") {
689 shift @ARGV;
690 $Verbose++;
691 next;
692 }
693
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000694 if ($arg eq "-V" or $arg eq "--view") {
695 shift @ARGV;
696 $ViewResults = 1;
697 next;
698 }
699
Ted Kremenek23cfca32008-06-16 22:40:14 +0000700 DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/);
Ted Kremenek0062ad42008-04-02 16:35:01 +0000701
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000702 last;
703}
704
705if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000706 Diag("No build command specified.\n\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000707 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000708 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000709}
710
711# Determine the output directory for the HTML reports.
712
713if (!defined($HtmlDir)) {
714
Sam Bishopa0e22662008-04-02 03:35:43 +0000715 $HtmlDir = mkdtemp("/tmp/$Prog-XXXXXX");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000716
717 if (!defined($HtmlDir)) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000718 DieDiag("Cannot create HTML directory in /tmp.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000719 }
720
721 if (!$Verbose) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000722 Diag("Using '$HtmlDir' as base HTML report directory.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000723 }
724}
725
Ted Kremenek684bb092008-04-18 15:18:20 +0000726my $BaseDir = $HtmlDir;
Sam Bishopa0e22662008-04-02 03:35:43 +0000727$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000728
729# Set the appropriate environment variables.
730
Sam Bishopa0e22662008-04-02 03:35:43 +0000731SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000732
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000733my $Cmd = "$RealBin/ccc-analyzer";
734
Ted Kremenek23cfca32008-06-16 22:40:14 +0000735DieDiag("Executable 'ccc-analyzer' does not exist at '$Cmd'\n")
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000736 if (! -x $Cmd);
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000737
738my $Clang = "$RealBin/clang";
739
740if (! -x $Clang) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000741 Diag("'clang' executable not found in '$RealBin'. Using 'clang' from path.\n");
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000742 $Clang = "clang";
743}
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000744
Ted Kremenek4f4b17d2008-04-03 20:08:18 +0000745$ENV{'CC'} = $Cmd;
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000746$ENV{'CLANG'} = $Clang;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000747
748if ($Verbose >= 2) {
749 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
750}
751
Ted Kremeneka9525c92008-05-12 22:07:14 +0000752if ($Verbose >= 3) {
753 $ENV{'CCC_ANALYZER_LOG'} = 1;
754}
755
Ted Kremenek1262fc42008-05-14 20:10:33 +0000756$ENV{'CCC_ANALYZER_ANALYSIS'} = $Analysis;
757
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000758# Run the build.
759
Ted Kremenek6195c372008-06-02 21:52:47 +0000760RunBuildCommand(\@ARGV, $IgnoreErrors, $Cmd);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000761
762# Postprocess the HTML directory.
763
Ted Kremenek684bb092008-04-18 15:18:20 +0000764Postprocess($HtmlDir, $BaseDir);
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000765
766if ($ViewResults and -r "$HtmlDir/index.html") {
767 # Only works on Mac OS X (for now).
768 print "Viewing analysis results: '$HtmlDir/index.html'\n";
769 `open $HtmlDir/index.html`
770}