blob: b4efa7bb9c8805c5889624cd125cc55d2276d146 [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 Kremenekc9d8fde2008-04-01 20:47:38 +000019
20my $Verbose = 0; # Verbose output from this script.
21my $Prog = "scan-build";
22
23##----------------------------------------------------------------------------##
24# GetHTMLRunDir - Construct an HTML directory name for the current run.
25##----------------------------------------------------------------------------##
26
Sam Bishop479982e2008-04-02 03:35:43 +000027sub GetHTMLRunDir {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +000028
29 die "Not enough arguments." if (@_ == 0);
30
31 my $Dir = shift @_;
32
33 # Get current date and time.
34
35 my @CurrentTime = localtime();
36
37 my $year = $CurrentTime[5] + 1900;
38 my $day = $CurrentTime[3];
39 my $month = $CurrentTime[4] + 1;
40
41 my $DateString = "$year-$month-$day";
42
43 # Determine the run number.
44
45 my $RunNumber;
46
47 if (-d $Dir) {
48
49 if (! -r $Dir) {
Sam Bishop479982e2008-04-02 03:35:43 +000050 die "error: '$Dir' exists but is not readable.\n";
Ted Kremenekc9d8fde2008-04-01 20:47:38 +000051 }
52
53 # Iterate over all files in the specified directory.
54
55 my $max = 0;
56
57 opendir(DIR, $Dir);
58 my @FILES= readdir(DIR);
59 closedir(DIR);
60
61 foreach my $f (@FILES) {
62
63 my @x = split/-/, $f;
64
65 next if (scalar(@x) != 4);
66 next if ($x[0] != $year);
67 next if ($x[1] != $month);
68 next if ($x[2] != $day);
69
70 if ($x[3] > $max) {
71 $max = $x[3];
72 }
73 }
74
75 $RunNumber = $max + 1;
76 }
77 else {
78
79 if (-x $Dir) {
Sam Bishop479982e2008-04-02 03:35:43 +000080 die "error: '$Dir' exists but is not a directory.\n";
Ted Kremenekc9d8fde2008-04-01 20:47:38 +000081 }
82
83 # $Dir does not exist. It will be automatically created by the
84 # clang driver. Set the run number to 1.
85
86 $RunNumber = 1;
87 }
88
89 die "RunNumber must be defined!" if (!defined($RunNumber));
90
91 # Append the run number.
92
93 return "$Dir/$DateString-$RunNumber";
94}
95
Sam Bishop479982e2008-04-02 03:35:43 +000096sub SetHtmlEnv {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +000097
98 die "Wrong number of arguments." if (scalar(@_) != 2);
99
100 my $Args = shift;
101 my $Dir = shift;
102
103 die "No build command." if (scalar(@$Args) == 0);
104
105 my $Cmd = $$Args[0];
106
107 if ($Cmd =~ /configure/) {
108 return;
109 }
110
111 if ($Verbose) {
112 print "$Prog: Emitting reports for this run to '$Dir'.\n";
113 }
114
115 $ENV{'CCC_ANALYZER_HTML'} = $Dir;
116}
117
118##----------------------------------------------------------------------------##
Ted Kremeneked757e02008-04-02 18:03:36 +0000119# ScanFile - Scan a report file for various identifying attributes.
120##----------------------------------------------------------------------------##
121
122sub ScanFile {
123
124 my $Index = shift;
125 my $Dir = shift;
126 my $FName = shift;
127
128 open(IN, "$Dir/$FName") or die "$Prog: Cannot open '$Dir/$FName'\n";
129
130 my $BugDesc = "";
Ted Kremenekd5d43622008-04-02 20:43:36 +0000131 my $BugFile = "";
132 my $BugPathLength = 1;
133 my $BugLine = 0;
Ted Kremeneked757e02008-04-02 18:03:36 +0000134
135 while (<IN>) {
136
137 if (/<!-- BUGDESC (.*) -->$/) {
138 $BugDesc = $1;
Ted Kremeneked757e02008-04-02 18:03:36 +0000139 }
Ted Kremenekd5d43622008-04-02 20:43:36 +0000140 elsif (/<!-- BUGFILE (.*) -->$/) {
141 $BugFile = $1;
142 }
143 elsif (/<!-- BUGPATHLENGTH (.*) -->$/) {
144 $BugPathLength = $1;
145 }
146 elsif (/<!-- BUGLINE (.*) -->$/) {
147 $BugLine = $1;
148 }
Ted Kremeneked757e02008-04-02 18:03:36 +0000149 }
150
151 close(IN);
152
Ted Kremenekd5d43622008-04-02 20:43:36 +0000153 push @$Index,[ $FName, $BugDesc, $BugFile, $BugLine, $BugPathLength ];
154}
155
156##----------------------------------------------------------------------------##
157# CopyJS - Copy JavaScript code to target directory.
158##----------------------------------------------------------------------------##
159
160sub CopyJS {
161
162 my $Dir = shift;
163
164 die "$Prog: Cannot find 'sorttable.js'.\n"
165 if (! -r "$RealBin/sorttable.js");
166
167 `cp $RealBin/sorttable.js $Dir`;
168
169 die "$Prog: Could not copy 'sorttable.js' to '$Dir'."
170 if (! -r "$Dir/sorttable.js");
Ted Kremeneked757e02008-04-02 18:03:36 +0000171}
172
173##----------------------------------------------------------------------------##
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000174# Postprocess - Postprocess the results of an analysis scan.
175##----------------------------------------------------------------------------##
176
Sam Bishop479982e2008-04-02 03:35:43 +0000177sub Postprocess {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000178
179 my $Dir = shift;
180
181 die "No directory specified." if (!defined($Dir));
182
183 if (! -d $Dir) {
184 return;
185 }
186
187 opendir(DIR, $Dir);
188 my @files = grep(/^report-.*\.html$/,readdir(DIR));
189 closedir(DIR);
190
191 if (scalar(@files) == 0) {
Ted Kremenek4efca192008-04-02 07:05:07 +0000192 print "$Prog: Removing directory '$Dir' because it contains no reports.\n";
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000193 `rm -fR $Dir`;
194 return;
195 }
Ted Kremeneked757e02008-04-02 18:03:36 +0000196
197 # Scan each report file and build an index.
198
199 my @Index;
200
201 foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); }
202
203 # Generate an index.html file.
204
205 my $FName = "$Dir/index.html";
206
207 open(OUT, ">$FName") or die "$Prog: Cannot create file '$FName'\n";
208
209print OUT <<ENDTEXT;
210<html>
211<head>
Ted Kremenekb59b7542008-04-02 18:42:49 +0000212<style type="text/css">
213 body { color:#000000; background-color:#ffffff }
Ted Kremenekd5d43622008-04-02 20:43:36 +0000214 body { font-family: Helvetica, sans-serif; font-size:9pt }
Ted Kremenekb59b7542008-04-02 18:42:49 +0000215 h1 { font-size:12pt }
Ted Kremenekd5d43622008-04-02 20:43:36 +0000216 table.sortable thead {
217 background-color:#eee; color:#666666;
218 font-weight: bold; cursor: default;
Ted Kremenek38471832008-04-03 05:50:51 +0000219 text-align:center;
220 border-top: 2px solid #000000;
221 border-bottom: 2px solid #000000;
222 font-weight: bold; font-family: Verdana
223 }
Ted Kremenekd5d43622008-04-02 20:43:36 +0000224 table.sortable { border: 1px #000000 solid }
225 table.sortable { border-collapse: collapse; border-spacing: 0px }
Ted Kremenekb59b7542008-04-02 18:42:49 +0000226 td { border-bottom: 1px #000000 dotted }
Ted Kremenekd5d43622008-04-02 20:43:36 +0000227 td { padding:5px; padding-left:8px; padding-right:8px }
Ted Kremenek756515f2008-04-07 23:50:07 +0000228 td { text-align:left; font-size:9pt }
Ted Kremenekd5d43622008-04-02 20:43:36 +0000229 td.View { padding-left: 10px }
Ted Kremenekb59b7542008-04-02 18:42:49 +0000230</style>
Ted Kremenekd5d43622008-04-02 20:43:36 +0000231<script src="sorttable.js"></script>
Ted Kremeneked757e02008-04-02 18:03:36 +0000232</head>\n<body>
Ted Kremenekd5d43622008-04-02 20:43:36 +0000233<table class="sortable">
Ted Kremenekb59b7542008-04-02 18:42:49 +0000234<tr>
Ted Kremenek38471832008-04-03 05:50:51 +0000235 <td>Bug Type</td>
236 <td>File</td>
237 <td>Line</td>
238 <td>Path Length</td>
239 <td "sorttable_nosort"></td>
Ted Kremenekb59b7542008-04-02 18:42:49 +0000240</tr>
Ted Kremeneked757e02008-04-02 18:03:36 +0000241ENDTEXT
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000242
Ted Kremeneked757e02008-04-02 18:03:36 +0000243 for my $row ( sort { $a->[1] cmp $b->[1] } @Index ) {
244
245 print OUT "<tr>\n";
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000246
Ted Kremeneked757e02008-04-02 18:03:36 +0000247 my $ReportFile = $row->[0];
248
Ted Kremenekd5d43622008-04-02 20:43:36 +0000249 print OUT " <td class=\"DESC\">";
250 print OUT lc($row->[1]);
251 print OUT "</td>\n";
Ted Kremeneked757e02008-04-02 18:03:36 +0000252
253 for my $j ( 2 .. $#{$row} ) {
254 print OUT "<td>$row->[$j]</td>\n"
255 }
Ted Kremenekb59b7542008-04-02 18:42:49 +0000256
257 # Emit the "View" link.
Ted Kremeneked757e02008-04-02 18:03:36 +0000258
Ted Kremenekd5d43622008-04-02 20:43:36 +0000259 print OUT " <td class=\"View\"><a href=\"$ReportFile#EndPath\">View</a></td>\n";
Ted Kremenekb59b7542008-04-02 18:42:49 +0000260
261 # End the row.
Ted Kremeneked757e02008-04-02 18:03:36 +0000262 print OUT "</tr>\n";
263 }
264
265 print OUT "</table>\n</body></html>\n";
266 close(OUT);
Ted Kremenekd5d43622008-04-02 20:43:36 +0000267
Ted Kremenekd5d43622008-04-02 20:43:36 +0000268 CopyJS($Dir);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000269}
270
271##----------------------------------------------------------------------------##
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000272# RunBuildCommand - Run the build command.
273##----------------------------------------------------------------------------##
274
275sub RunBuildCommand {
276
277 my $Args = shift;
Ted Kremenek091f9372008-04-02 16:04:51 +0000278 my $IgnoreErrors = shift;
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000279 my $Cmd = $Args->[0];
280
Ted Kremenek404cb262008-04-02 15:34:12 +0000281 if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc") {
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000282 shift @$Args;
283 unshift @$Args, "ccc-analyzer"
284 }
Ted Kremenek091f9372008-04-02 16:04:51 +0000285 elsif ($IgnoreErrors) {
286 if ($Cmd eq "make" or $Cmd eq "gmake") {
287 push @$Args, "-k";
288 }
289 elsif ($Cmd eq "xcodebuild") {
290 push @$Args, "-PBXBuildsContinueAfterErrors=YES";
291 }
Ted Kremenekd5d43622008-04-02 20:43:36 +0000292 }
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000293
294 system(@$Args);
295}
296
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000297##----------------------------------------------------------------------------##
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000298# DisplayHelp - Utility function to display all help options.
299##----------------------------------------------------------------------------##
300
Sam Bishop479982e2008-04-02 03:35:43 +0000301sub DisplayHelp {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000302
Ted Kremeneked757e02008-04-02 18:03:36 +0000303print <<ENDTEXT;
Sam Bishop479982e2008-04-02 03:35:43 +0000304USAGE: $Prog [options] <build command> [build options]
Ted Kremenekb9177df2008-04-01 21:22:03 +0000305
306OPTIONS:
307
308 -o - Target directory for HTML report files. Subdirectories
Sam Bishop479982e2008-04-02 03:35:43 +0000309 will be created as needed to represent separate "runs" of
Ted Kremenekb9177df2008-04-01 21:22:03 +0000310 the analyzer. If this option is not specified, a directory
311 is created in /tmp to store the reports.
312
Ted Kremeneke9c91a82008-04-03 07:11:44 +0000313 -h - Display this message.
Ted Kremenekb9177df2008-04-01 21:22:03 +0000314 --help
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000315
Ted Kremenekd1843772008-04-02 16:31:58 +0000316 -k - Add a "keep on going" option to the specified build command.
Ted Kremenek707a9ad2008-04-02 16:41:25 +0000317 --keep-going This option currently supports make and xcodebuild.
318 This is a convenience option; one can specify this
319 behavior directly using build options.
Ted Kremenekb9177df2008-04-01 21:22:03 +0000320
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000321 -v - Verbose output from $Prog and the analyzer.
322 A second "-v" increases verbosity.
Ted Kremenekb9177df2008-04-01 21:22:03 +0000323
Ted Kremenekb59b7542008-04-02 18:42:49 +0000324 -V - View analysis results in a web browser when the build
325 --view completes.
326
Ted Kremenekb9177df2008-04-01 21:22:03 +0000327BUILD OPTIONS
328
Ted Kremenekf76812d2008-04-02 16:47:27 +0000329 You can specify any build option acceptable to the build command.
330
Ted Kremeneked757e02008-04-02 18:03:36 +0000331EXAMPLE
Ted Kremenekb9177df2008-04-01 21:22:03 +0000332
Ted Kremeneked757e02008-04-02 18:03:36 +0000333 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenekb9177df2008-04-01 21:22:03 +0000334
Ted Kremenekf76812d2008-04-02 16:47:27 +0000335 The above example causes analysis reports to be deposited into
336 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
337 A different subdirectory is created each time $Prog analyzes a project.
338 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenekb9177df2008-04-01 21:22:03 +0000339
340ENDTEXT
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000341}
342
343##----------------------------------------------------------------------------##
344# Process command-line arguments.
345##----------------------------------------------------------------------------##
346
347my $HtmlDir; # Parent directory to store HTML files.
348my $IgnoreErrors = 0; # Ignore build errors.
Ted Kremenekb59b7542008-04-02 18:42:49 +0000349my $ViewResults = 0; # View results when the build terminates.
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000350
351if (!@ARGV) {
352 DisplayHelp();
Sam Bishop479982e2008-04-02 03:35:43 +0000353 exit 1;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000354}
355
356while (@ARGV) {
357
358 # Scan for options we recognize.
359
360 my $arg = $ARGV[0];
361
Sam Bishopa3281512008-04-03 14:29:47 +0000362 if ($arg eq "-h" or $arg eq "--help") {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000363 DisplayHelp();
Sam Bishop479982e2008-04-02 03:35:43 +0000364 exit 0;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000365 }
366
367 if ($arg eq "-o") {
368 shift @ARGV;
369
370 if (!@ARGV) {
Ted Kremenek78965de2008-04-02 16:35:01 +0000371 die "$Prog: '-o' option requires a target directory name.\n";
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000372 }
373
374 $HtmlDir = shift @ARGV;
375 next;
376 }
377
Ted Kremenekb9177df2008-04-01 21:22:03 +0000378 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000379 shift @ARGV;
380 $IgnoreErrors = 1;
381 next;
382 }
383
384 if ($arg eq "-v") {
385 shift @ARGV;
386 $Verbose++;
387 next;
388 }
389
Ted Kremenekb59b7542008-04-02 18:42:49 +0000390 if ($arg eq "-V" or $arg eq "--view") {
391 shift @ARGV;
392 $ViewResults = 1;
393 next;
394 }
395
Ted Kremenek78965de2008-04-02 16:35:01 +0000396 die "$Prog: unrecognized option '$arg'\n" if ($arg =~ /^-/);
397
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000398 last;
399}
400
401if (!@ARGV) {
402 print STDERR "$Prog: No build command specified.\n\n";
403 DisplayHelp();
Sam Bishop479982e2008-04-02 03:35:43 +0000404 exit 1;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000405}
406
407# Determine the output directory for the HTML reports.
408
409if (!defined($HtmlDir)) {
410
Sam Bishop479982e2008-04-02 03:35:43 +0000411 $HtmlDir = mkdtemp("/tmp/$Prog-XXXXXX");
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000412
413 if (!defined($HtmlDir)) {
Sam Bishop479982e2008-04-02 03:35:43 +0000414 die "error: Cannot create HTML directory in /tmp.\n";
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000415 }
416
417 if (!$Verbose) {
418 print "$Prog: Using '$HtmlDir' as base HTML report directory.\n";
419 }
420}
421
Sam Bishop479982e2008-04-02 03:35:43 +0000422$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000423
424# Set the appropriate environment variables.
425
Sam Bishop479982e2008-04-02 03:35:43 +0000426SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000427
Ted Kremenekfc0f1c22008-04-08 20:22:12 +0000428my $Cmd = "$RealBin/ccc-analyzer";
429
430die "$Prog: Executable 'ccc-analyzer' does not exist at '$Cmd'\n"
431 if (! -x $Cmd);
432
Ted Kremenek47bf3892008-04-03 20:08:18 +0000433$ENV{'CC'} = $Cmd;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000434
435if ($Verbose >= 2) {
436 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
437}
438
439# Run the build.
440
Ted Kremenek091f9372008-04-02 16:04:51 +0000441RunBuildCommand(\@ARGV, $IgnoreErrors);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000442
443# Postprocess the HTML directory.
444
Sam Bishop479982e2008-04-02 03:35:43 +0000445Postprocess($HtmlDir);
Ted Kremenekb59b7542008-04-02 18:42:49 +0000446
447if ($ViewResults and -r "$HtmlDir/index.html") {
448 # Only works on Mac OS X (for now).
449 print "Viewing analysis results: '$HtmlDir/index.html'\n";
450 `open $HtmlDir/index.html`
451}