blob: a7ce79796288d9aa53199e4d794d316df94baa29 [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 Kremenek9cc8c2c2008-04-01 20:47:38 +000021
22my $Verbose = 0; # Verbose output from this script.
23my $Prog = "scan-build";
Ted Kremenekf4cdf412008-05-23 18:17:05 +000024my $BuildName;
25my $BuildDate;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000026
27##----------------------------------------------------------------------------##
28# GetHTMLRunDir - Construct an HTML directory name for the current run.
29##----------------------------------------------------------------------------##
30
Sam Bishopa0e22662008-04-02 03:35:43 +000031sub GetHTMLRunDir {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000032
33 die "Not enough arguments." if (@_ == 0);
34
35 my $Dir = shift @_;
36
37 # Get current date and time.
38
39 my @CurrentTime = localtime();
40
41 my $year = $CurrentTime[5] + 1900;
42 my $day = $CurrentTime[3];
43 my $month = $CurrentTime[4] + 1;
44
Ted Kremenek9d7405f2008-05-14 17:23:56 +000045 my $DateString = sprintf("%d-%02d-%02d", $year, $month, $day);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000046
47 # Determine the run number.
48
49 my $RunNumber;
50
51 if (-d $Dir) {
52
53 if (! -r $Dir) {
Sam Bishopa0e22662008-04-02 03:35:43 +000054 die "error: '$Dir' exists but is not readable.\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000055 }
56
57 # Iterate over all files in the specified directory.
58
59 my $max = 0;
60
61 opendir(DIR, $Dir);
62 my @FILES= readdir(DIR);
63 closedir(DIR);
64
65 foreach my $f (@FILES) {
66
67 my @x = split/-/, $f;
68
69 next if (scalar(@x) != 4);
70 next if ($x[0] != $year);
71 next if ($x[1] != $month);
72 next if ($x[2] != $day);
73
74 if ($x[3] > $max) {
75 $max = $x[3];
76 }
77 }
78
79 $RunNumber = $max + 1;
80 }
81 else {
82
83 if (-x $Dir) {
Sam Bishopa0e22662008-04-02 03:35:43 +000084 die "error: '$Dir' exists but is not a directory.\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000085 }
86
87 # $Dir does not exist. It will be automatically created by the
88 # clang driver. Set the run number to 1.
89
90 $RunNumber = 1;
91 }
92
93 die "RunNumber must be defined!" if (!defined($RunNumber));
94
95 # Append the run number.
96
97 return "$Dir/$DateString-$RunNumber";
98}
99
Sam Bishopa0e22662008-04-02 03:35:43 +0000100sub SetHtmlEnv {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000101
102 die "Wrong number of arguments." if (scalar(@_) != 2);
103
104 my $Args = shift;
105 my $Dir = shift;
106
107 die "No build command." if (scalar(@$Args) == 0);
108
109 my $Cmd = $$Args[0];
110
111 if ($Cmd =~ /configure/) {
112 return;
113 }
114
115 if ($Verbose) {
116 print "$Prog: Emitting reports for this run to '$Dir'.\n";
117 }
118
119 $ENV{'CCC_ANALYZER_HTML'} = $Dir;
120}
121
122##----------------------------------------------------------------------------##
Ted Kremenek57cf4462008-04-18 15:09:30 +0000123# ComputeDigest - Compute a digest of the specified file.
124##----------------------------------------------------------------------------##
125
126sub ComputeDigest {
127 my $FName = shift;
128 die "Cannot read $FName" if (! -r $FName);
Ted Kremeneka6e24812008-04-19 18:05:48 +0000129
130 # Use Digest::MD5. We don't have to be cryptographically secure. We're
Ted Kremenek7ea02e62008-04-19 18:07:44 +0000131 # just looking for duplicate files that come from a non-malicious source.
132 # We use Digest::MD5 because it is a standard Perl module that should
Ted Kremeneka6e24812008-04-19 18:05:48 +0000133 # come bundled on most systems.
134
135 open(FILE, $FName) or die "Cannot open $FName.";
136 binmode FILE;
137 my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest;
138 close(FILE);
139
140 # Return the digest.
141
142 return $Result;
Ted Kremenek57cf4462008-04-18 15:09:30 +0000143}
144
145##----------------------------------------------------------------------------##
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000146# UpdatePrefix - Compute the common prefix of files.
147##----------------------------------------------------------------------------##
148
149my $Prefix;
150
151sub UpdatePrefix {
152
153 my $x = shift;
154 my $y = basename($x);
155 $x =~ s/\Q$y\E$//;
156
157 # Ignore /usr, /Library, /System, /Developer
158
159 return if ( $x =~ /^\/usr/ or $x =~ /^\/Library/
160 or $x =~ /^\/System/ or $x =~ /^\/Developer/);
161
162
163 if (!defined $Prefix) {
164 $Prefix = $x;
165 return;
166 }
167
168 chop $Prefix while (!($x =~ /^$Prefix/));
169}
170
171sub GetPrefix {
172 return $Prefix;
173}
174
175##----------------------------------------------------------------------------##
176# UpdateInFilePath - Update the path in the report file.
177##----------------------------------------------------------------------------##
178
179sub UpdateInFilePath {
180 my $fname = shift;
181 my $regex = shift;
182 my $newtext = shift;
183
184 open (RIN, $fname) or die "cannot open $fname";
185 open (ROUT, ">$fname.tmp") or die "cannot open $fname.tmp";
186
187 while (<RIN>) {
188 s/$regex/$newtext/;
189 print ROUT $_;
190 }
191
192 close (ROUT);
193 close (RIN);
194 `mv $fname.tmp $fname`;
195}
196
197##----------------------------------------------------------------------------##
Ted Kremenek5744dc22008-04-02 18:03:36 +0000198# ScanFile - Scan a report file for various identifying attributes.
199##----------------------------------------------------------------------------##
200
Ted Kremenek57cf4462008-04-18 15:09:30 +0000201# Sometimes a source file is scanned more than once, and thus produces
202# multiple error reports. We use a cache to solve this problem.
203
204my %AlreadyScanned;
205
Ted Kremenek5744dc22008-04-02 18:03:36 +0000206sub ScanFile {
207
208 my $Index = shift;
209 my $Dir = shift;
210 my $FName = shift;
211
Ted Kremenek57cf4462008-04-18 15:09:30 +0000212 # Compute a digest for the report file. Determine if we have already
213 # scanned a file that looks just like it.
214
215 my $digest = ComputeDigest("$Dir/$FName");
216
217 if (defined($AlreadyScanned{$digest})) {
218 # Redundant file. Remove it.
219 `rm -f $Dir/$FName`;
220 return;
221 }
222
223 $AlreadyScanned{$digest} = 1;
224
Ted Kremenek809709f2008-04-18 16:58:34 +0000225 # At this point the report file is not world readable. Make it happen.
Ted Kremenek684bb092008-04-18 15:18:20 +0000226 `chmod 644 $Dir/$FName`;
227
228 # Scan the report file for tags.
Ted Kremenek5744dc22008-04-02 18:03:36 +0000229 open(IN, "$Dir/$FName") or die "$Prog: Cannot open '$Dir/$FName'\n";
230
231 my $BugDesc = "";
Ted Kremenek22d6a632008-04-02 20:43:36 +0000232 my $BugFile = "";
233 my $BugPathLength = 1;
234 my $BugLine = 0;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000235
236 while (<IN>) {
237
238 if (/<!-- BUGDESC (.*) -->$/) {
239 $BugDesc = $1;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000240 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000241 elsif (/<!-- BUGFILE (.*) -->$/) {
242 $BugFile = $1;
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000243 UpdatePrefix($BugFile);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000244 }
245 elsif (/<!-- BUGPATHLENGTH (.*) -->$/) {
246 $BugPathLength = $1;
247 }
248 elsif (/<!-- BUGLINE (.*) -->$/) {
249 $BugLine = $1;
250 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000251 }
252
253 close(IN);
254
Ted Kremenek22d6a632008-04-02 20:43:36 +0000255 push @$Index,[ $FName, $BugDesc, $BugFile, $BugLine, $BugPathLength ];
256}
257
258##----------------------------------------------------------------------------##
259# CopyJS - Copy JavaScript code to target directory.
260##----------------------------------------------------------------------------##
261
262sub CopyJS {
263
264 my $Dir = shift;
265
266 die "$Prog: Cannot find 'sorttable.js'.\n"
267 if (! -r "$RealBin/sorttable.js");
268
269 `cp $RealBin/sorttable.js $Dir`;
270
271 die "$Prog: Could not copy 'sorttable.js' to '$Dir'."
272 if (! -r "$Dir/sorttable.js");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000273}
274
275##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000276# Postprocess - Postprocess the results of an analysis scan.
277##----------------------------------------------------------------------------##
278
Sam Bishopa0e22662008-04-02 03:35:43 +0000279sub Postprocess {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000280
281 my $Dir = shift;
Ted Kremenek684bb092008-04-18 15:18:20 +0000282 my $BaseDir = shift;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000283
284 die "No directory specified." if (!defined($Dir));
Ted Kremenek684bb092008-04-18 15:18:20 +0000285 die "No base directory specified." if (!defined($BaseDir));
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000286
287 if (! -d $Dir) {
288 return;
289 }
290
291 opendir(DIR, $Dir);
292 my @files = grep(/^report-.*\.html$/,readdir(DIR));
293 closedir(DIR);
294
295 if (scalar(@files) == 0) {
Ted Kremenek02493782008-04-02 07:05:07 +0000296 print "$Prog: Removing directory '$Dir' because it contains no reports.\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000297 `rm -fR $Dir`;
298 return;
299 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000300
301 # Scan each report file and build an index.
302
303 my @Index;
304
305 foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); }
306
307 # Generate an index.html file.
308
309 my $FName = "$Dir/index.html";
310
311 open(OUT, ">$FName") or die "$Prog: Cannot create file '$FName'\n";
312
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000313 # Print out the header.
314
Ted Kremenek5744dc22008-04-02 18:03:36 +0000315print OUT <<ENDTEXT;
316<html>
317<head>
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000318<style type="text/css">
319 body { color:#000000; background-color:#ffffff }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000320 body { font-family: Helvetica, sans-serif; font-size:9pt }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000321 h1 { font-size:12pt }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000322 table.sortable thead {
323 background-color:#eee; color:#666666;
324 font-weight: bold; cursor: default;
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000325 text-align:center;
326 border-top: 2px solid #000000;
327 border-bottom: 2px solid #000000;
328 font-weight: bold; font-family: Verdana
329 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000330 table.sortable { border: 1px #000000 solid }
331 table.sortable { border-collapse: collapse; border-spacing: 0px }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000332 td { border-bottom: 1px #000000 dotted }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000333 td { padding:5px; padding-left:8px; padding-right:8px }
Ted Kremenekd8c6d0c2008-04-07 23:50:07 +0000334 td { text-align:left; font-size:9pt }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000335 td.View { padding-left: 10px }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000336</style>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000337<script src="sorttable.js"></script>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000338<script language='javascript' type="text/javascript">
339function SetDisplay(RowClass, DisplayVal)
340{
341 var Rows = document.getElementsByTagName("tr");
342 for ( var i = 0 ; i < Rows.length; ++i ) {
343 if (Rows[i].className == RowClass) {
344 Rows[i].style.display = DisplayVal;
345 }
346 }
347}
348
349function ToggleDisplay(CheckButton, ClassName) {
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000350 if (CheckButton.checked) {
351 SetDisplay(ClassName, "");
352 }
353 else {
354 SetDisplay(ClassName, "none");
355 }
356}
357</script>
358</head>
359<body>
360ENDTEXT
361
362 # Print out the summary table.
363
364 my %Totals;
365
366 for my $row ( @Index ) {
367
Ted Kremenek432af592008-05-06 18:11:36 +0000368 #my $bug_type = lc($row->[1]);
369 my $bug_type = ($row->[1]);
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000370
371 if (!defined($Totals{$bug_type})) {
372 $Totals{$bug_type} = 1;
373 }
374 else {
375 $Totals{$bug_type}++;
376 }
377 }
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000378
379 print OUT "<h3>Summary</h3>";
380
381 if (defined($BuildName)) {
382 print OUT "\n<p>Results in this analysis run are based on analyzer build <b>$BuildName</b>.</p>\n"
383 }
384
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000385print OUT <<ENDTEXT;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000386<table class="sortable">
387<tr>
388 <td>Bug Type</td>
389 <td>Quantity</td>
390 <td "sorttable_nosort">Display?</td>
391</tr>
392ENDTEXT
393
394 for my $key ( sort { $a cmp $b } keys %Totals ) {
Ted Kremenekbdf66c72008-05-06 23:51:45 +0000395 my $x = lc($key);
396 $x =~ s/\s[,]/_/g;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000397 print OUT "<tr><td>$key</td><td>$Totals{$key}</td><td><input type=\"checkbox\" onClick=\"ToggleDisplay(this,'bt_$x');\" checked/></td></tr>\n";
398 }
399
400 # Print out the table of errors.
401
402print OUT <<ENDTEXT;
403</table>
404<h3>Reports</h3>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000405<table class="sortable">
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000406<tr>
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000407 <td>Bug Type</td>
408 <td>File</td>
409 <td>Line</td>
410 <td>Path Length</td>
411 <td "sorttable_nosort"></td>
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000412</tr>
Ted Kremenek5744dc22008-04-02 18:03:36 +0000413ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000414
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000415 my $prefix = GetPrefix();
416 my $regex;
417 my $InFileRegex;
418 my $InFilePrefix = "File:</td><td>";
419
420 if (defined($prefix)) {
421 $regex = qr/^\Q$prefix\E/is;
422 $InFileRegex = qr/\Q$InFilePrefix$prefix\E/is;
423 }
424
Ted Kremenek5744dc22008-04-02 18:03:36 +0000425 for my $row ( sort { $a->[1] cmp $b->[1] } @Index ) {
426
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000427 my $x = lc($row->[1]);
Ted Kremenekbdf66c72008-05-06 23:51:45 +0000428 $x =~ s/\s[,]/_/g;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000429
430 print OUT "<tr class=\"bt_$x\">\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000431
Ted Kremenek5744dc22008-04-02 18:03:36 +0000432 my $ReportFile = $row->[0];
433
Ted Kremenek22d6a632008-04-02 20:43:36 +0000434 print OUT " <td class=\"DESC\">";
Ted Kremenek432af592008-05-06 18:11:36 +0000435 #print OUT lc($row->[1]);
436 print OUT $row->[1];
Ted Kremenek22d6a632008-04-02 20:43:36 +0000437 print OUT "</td>\n";
Ted Kremenek5744dc22008-04-02 18:03:36 +0000438
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000439 # Update the file prefix.
440
441 my $fname = $row->[2];
442 if (defined($regex)) {
443 $fname =~ s/$regex//;
444 UpdateInFilePath("$Dir/$ReportFile", $InFileRegex, $InFilePrefix)
445 }
Ted Kremenek3e56e0b2008-05-02 23:40:49 +0000446
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000447 print OUT "<td>$fname</td>\n";
448
449 # Print the rest of the columns.
450
451 for my $j ( 3 .. $#{$row} ) {
Ted Kremenek5744dc22008-04-02 18:03:36 +0000452 print OUT "<td>$row->[$j]</td>\n"
453 }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000454
455 # Emit the "View" link.
Ted Kremenek5744dc22008-04-02 18:03:36 +0000456
Ted Kremenek22d6a632008-04-02 20:43:36 +0000457 print OUT " <td class=\"View\"><a href=\"$ReportFile#EndPath\">View</a></td>\n";
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000458
459 # End the row.
Ted Kremenek5744dc22008-04-02 18:03:36 +0000460 print OUT "</tr>\n";
461 }
462
463 print OUT "</table>\n</body></html>\n";
464 close(OUT);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000465
Ted Kremenek22d6a632008-04-02 20:43:36 +0000466 CopyJS($Dir);
Ted Kremenek684bb092008-04-18 15:18:20 +0000467
468 # Make sure $Dir and $BaseDir is world readable/executable.
469 `chmod 755 $Dir`;
470 `chmod 755 $BaseDir`;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000471}
472
473##----------------------------------------------------------------------------##
Ted Kremenekdab11102008-04-02 04:43:42 +0000474# RunBuildCommand - Run the build command.
475##----------------------------------------------------------------------------##
476
Ted Kremenek6b628982008-04-30 23:47:12 +0000477sub AddIfNotPresent {
478 my $Args = shift;
479 my $Arg = shift;
480 my $found = 0;
481
482 foreach my $k (@$Args) {
483 if ($k eq $Arg) {
484 $found = 1;
485 last;
486 }
487 }
488
489 if ($found == 0) {
490 push @$Args, $Arg;
491 }
492}
493
Ted Kremenekdab11102008-04-02 04:43:42 +0000494sub RunBuildCommand {
495
496 my $Args = shift;
Ted Kremenek7442ca62008-04-02 16:04:51 +0000497 my $IgnoreErrors = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000498 my $Cmd = $Args->[0];
499
Ted Kremenek6a43ba92008-04-02 15:34:12 +0000500 if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc") {
Ted Kremenekdab11102008-04-02 04:43:42 +0000501 shift @$Args;
502 unshift @$Args, "ccc-analyzer"
503 }
Ted Kremenek7442ca62008-04-02 16:04:51 +0000504 elsif ($IgnoreErrors) {
505 if ($Cmd eq "make" or $Cmd eq "gmake") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000506 AddIfNotPresent($Args,"-k");
Ted Kremenek8912b542008-05-13 21:28:02 +0000507 AddIfNotPresent($Args,"-i");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000508 }
509 elsif ($Cmd eq "xcodebuild") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000510 AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000511 }
Ted Kremenek6b628982008-04-30 23:47:12 +0000512 }
513
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000514
Ted Kremenek6b628982008-04-30 23:47:12 +0000515 if ($Cmd eq "xcodebuild") {
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000516 # Disable distributed builds for xcodebuild.
Ted Kremenek6b628982008-04-30 23:47:12 +0000517 AddIfNotPresent($Args,"-nodistribute");
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000518
519 # Disable PCH files until clang supports them.
520 AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO");
Ted Kremenek6b628982008-04-30 23:47:12 +0000521 }
Ted Kremenekdab11102008-04-02 04:43:42 +0000522
523 system(@$Args);
524}
525
Ted Kremenekdab11102008-04-02 04:43:42 +0000526##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000527# DisplayHelp - Utility function to display all help options.
528##----------------------------------------------------------------------------##
529
Sam Bishopa0e22662008-04-02 03:35:43 +0000530sub DisplayHelp {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000531
Ted Kremenek5744dc22008-04-02 18:03:36 +0000532print <<ENDTEXT;
Sam Bishopa0e22662008-04-02 03:35:43 +0000533USAGE: $Prog [options] <build command> [build options]
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000534
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000535ENDTEXT
536
537 if (defined($BuildName)) {
538 print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n";
539 }
540
541print <<ENDTEXT;
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000542OPTIONS:
543
Ted Kremenek1262fc42008-05-14 20:10:33 +0000544 -a - The analysis to run. The default is 'checker-cfref'.
545 Valid options are: 'checker-cfref', 'fsyntax-only'
546
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000547 -o - Target directory for HTML report files. Subdirectories
Sam Bishopa0e22662008-04-02 03:35:43 +0000548 will be created as needed to represent separate "runs" of
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000549 the analyzer. If this option is not specified, a directory
550 is created in /tmp to store the reports.
Ted Kremenek1262fc42008-05-14 20:10:33 +0000551
Ted Kremenek10f883f2008-04-03 07:11:44 +0000552 -h - Display this message.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000553 --help
Ted Kremenek1262fc42008-05-14 20:10:33 +0000554
Ted Kremenekaf08f642008-04-02 16:31:58 +0000555 -k - Add a "keep on going" option to the specified build command.
Ted Kremenekf02e8db2008-04-02 16:41:25 +0000556 --keep-going This option currently supports make and xcodebuild.
557 This is a convenience option; one can specify this
558 behavior directly using build options.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000559
Ted Kremenekdab11102008-04-02 04:43:42 +0000560 -v - Verbose output from $Prog and the analyzer.
561 A second "-v" increases verbosity.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000562
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000563 -V - View analysis results in a web browser when the build
564 --view completes.
565
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000566BUILD OPTIONS
567
Ted Kremenek39eefde2008-04-02 16:47:27 +0000568 You can specify any build option acceptable to the build command.
569
Ted Kremenek5744dc22008-04-02 18:03:36 +0000570EXAMPLE
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000571
Ted Kremenek5744dc22008-04-02 18:03:36 +0000572 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000573
Ted Kremenek39eefde2008-04-02 16:47:27 +0000574 The above example causes analysis reports to be deposited into
575 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
576 A different subdirectory is created each time $Prog analyzes a project.
577 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000578
579ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000580}
581
582##----------------------------------------------------------------------------##
583# Process command-line arguments.
584##----------------------------------------------------------------------------##
585
586my $HtmlDir; # Parent directory to store HTML files.
587my $IgnoreErrors = 0; # Ignore build errors.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000588my $ViewResults = 0; # View results when the build terminates.
Ted Kremenek1262fc42008-05-14 20:10:33 +0000589my $Analysis = "checker-cfref";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000590
591if (!@ARGV) {
592 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000593 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000594}
595
596while (@ARGV) {
597
598 # Scan for options we recognize.
599
600 my $arg = $ARGV[0];
601
Sam Bishop2f2418e2008-04-03 14:29:47 +0000602 if ($arg eq "-h" or $arg eq "--help") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000603 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000604 exit 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000605 }
606
Ted Kremenek1262fc42008-05-14 20:10:33 +0000607 if ($arg eq "-a") {
608 shift @ARGV;
609
610 if (!@ARGV) {
611 die "$Prog: '-a' option requires an analysis type.\n";
612 }
613
614 $Analysis = shift @ARGV;
615
616 if (!($Analysis eq "checker-cfref" or $Analysis eq "fsyntax-only")) {
617 die "$Prog: Invalid argument '$Analysis' to -a.\n";
618 }
619
620 next;
621 }
622
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000623 if ($arg eq "-o") {
624 shift @ARGV;
625
626 if (!@ARGV) {
Ted Kremenek0062ad42008-04-02 16:35:01 +0000627 die "$Prog: '-o' option requires a target directory name.\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000628 }
629
630 $HtmlDir = shift @ARGV;
631 next;
632 }
633
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000634 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000635 shift @ARGV;
636 $IgnoreErrors = 1;
637 next;
638 }
639
640 if ($arg eq "-v") {
641 shift @ARGV;
642 $Verbose++;
643 next;
644 }
645
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000646 if ($arg eq "-V" or $arg eq "--view") {
647 shift @ARGV;
648 $ViewResults = 1;
649 next;
650 }
651
Ted Kremenek0062ad42008-04-02 16:35:01 +0000652 die "$Prog: unrecognized option '$arg'\n" if ($arg =~ /^-/);
653
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000654 last;
655}
656
657if (!@ARGV) {
658 print STDERR "$Prog: No build command specified.\n\n";
659 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000660 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000661}
662
663# Determine the output directory for the HTML reports.
664
665if (!defined($HtmlDir)) {
666
Sam Bishopa0e22662008-04-02 03:35:43 +0000667 $HtmlDir = mkdtemp("/tmp/$Prog-XXXXXX");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000668
669 if (!defined($HtmlDir)) {
Sam Bishopa0e22662008-04-02 03:35:43 +0000670 die "error: Cannot create HTML directory in /tmp.\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000671 }
672
673 if (!$Verbose) {
674 print "$Prog: Using '$HtmlDir' as base HTML report directory.\n";
675 }
676}
677
Ted Kremenek684bb092008-04-18 15:18:20 +0000678my $BaseDir = $HtmlDir;
Sam Bishopa0e22662008-04-02 03:35:43 +0000679$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000680
681# Set the appropriate environment variables.
682
Sam Bishopa0e22662008-04-02 03:35:43 +0000683SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000684
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000685my $Cmd = "$RealBin/ccc-analyzer";
686
687die "$Prog: Executable 'ccc-analyzer' does not exist at '$Cmd'\n"
688 if (! -x $Cmd);
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000689
690my $Clang = "$RealBin/clang";
691
692if (! -x $Clang) {
693 print "$Prog: 'clang' executable not found in '$RealBin'. Using 'clang' from path.\n";
694 $Clang = "clang";
695}
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000696
Ted Kremenek4f4b17d2008-04-03 20:08:18 +0000697$ENV{'CC'} = $Cmd;
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000698$ENV{'CLANG'} = $Clang;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000699
700if ($Verbose >= 2) {
701 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
702}
703
Ted Kremeneka9525c92008-05-12 22:07:14 +0000704if ($Verbose >= 3) {
705 $ENV{'CCC_ANALYZER_LOG'} = 1;
706}
707
Ted Kremenek1262fc42008-05-14 20:10:33 +0000708$ENV{'CCC_ANALYZER_ANALYSIS'} = $Analysis;
709
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000710# Run the build.
711
Ted Kremenek7442ca62008-04-02 16:04:51 +0000712RunBuildCommand(\@ARGV, $IgnoreErrors);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000713
714# Postprocess the HTML directory.
715
Ted Kremenek684bb092008-04-18 15:18:20 +0000716Postprocess($HtmlDir, $BaseDir);
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000717
718if ($ViewResults and -r "$HtmlDir/index.html") {
719 # Only works on Mac OS X (for now).
720 print "Viewing analysis results: '$HtmlDir/index.html'\n";
721 `open $HtmlDir/index.html`
722}