blob: 12a7dbca2e5eb32cae47f1c3a205262cdcad2f01 [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 Kremenek9cc8c2c2008-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 Bishopa0e22662008-04-02 03:35:43 +000027sub GetHTMLRunDir {
Ted Kremenek9cc8c2c2008-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 Bishopa0e22662008-04-02 03:35:43 +000050 die "error: '$Dir' exists but is not readable.\n";
Ted Kremenek9cc8c2c2008-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 Bishopa0e22662008-04-02 03:35:43 +000080 die "error: '$Dir' exists but is not a directory.\n";
Ted Kremenek9cc8c2c2008-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 Bishopa0e22662008-04-02 03:35:43 +000096sub SetHtmlEnv {
Ted Kremenek9cc8c2c2008-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 Kremenek5744dc22008-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 Kremenek22d6a632008-04-02 20:43:36 +0000131 my $BugFile = "";
132 my $BugPathLength = 1;
133 my $BugLine = 0;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000134
135 while (<IN>) {
136
137 if (/<!-- BUGDESC (.*) -->$/) {
138 $BugDesc = $1;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000139 }
Ted Kremenek22d6a632008-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 Kremenek5744dc22008-04-02 18:03:36 +0000149 }
150
151 close(IN);
152
Ted Kremenek22d6a632008-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 Kremenek5744dc22008-04-02 18:03:36 +0000171}
172
173##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000174# Postprocess - Postprocess the results of an analysis scan.
175##----------------------------------------------------------------------------##
176
Sam Bishopa0e22662008-04-02 03:35:43 +0000177sub Postprocess {
Ted Kremenek9cc8c2c2008-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 Kremenek02493782008-04-02 07:05:07 +0000192 print "$Prog: Removing directory '$Dir' because it contains no reports.\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000193 `rm -fR $Dir`;
194 return;
195 }
Ted Kremenek5744dc22008-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 Kremenek7f8a3252008-04-02 18:42:49 +0000212<style type="text/css">
213 body { color:#000000; background-color:#ffffff }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000214 body { font-family: Helvetica, sans-serif; font-size:9pt }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000215 h1 { font-size:12pt }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000216 table.sortable thead {
217 background-color:#eee; color:#666666;
218 font-weight: bold; cursor: default;
Ted Kremenekbba1cf52008-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 Kremenek22d6a632008-04-02 20:43:36 +0000224 table.sortable { border: 1px #000000 solid }
225 table.sortable { border-collapse: collapse; border-spacing: 0px }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000226 td { border-bottom: 1px #000000 dotted }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000227 td { padding:5px; padding-left:8px; padding-right:8px }
228 td { text-align:right; font-size:9pt }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000229 td.View { padding-left: 10px }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000230</style>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000231<script src="sorttable.js"></script>
Ted Kremenek5744dc22008-04-02 18:03:36 +0000232</head>\n<body>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000233<table class="sortable">
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000234<tr>
Ted Kremenekbba1cf52008-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 Kremenek7f8a3252008-04-02 18:42:49 +0000240</tr>
Ted Kremenek5744dc22008-04-02 18:03:36 +0000241ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000242
Ted Kremenek5744dc22008-04-02 18:03:36 +0000243 for my $row ( sort { $a->[1] cmp $b->[1] } @Index ) {
244
245 print OUT "<tr>\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000246
Ted Kremenek5744dc22008-04-02 18:03:36 +0000247 my $ReportFile = $row->[0];
248
Ted Kremenek22d6a632008-04-02 20:43:36 +0000249 print OUT " <td class=\"DESC\">";
250 print OUT lc($row->[1]);
251 print OUT "</td>\n";
Ted Kremenek5744dc22008-04-02 18:03:36 +0000252
253 for my $j ( 2 .. $#{$row} ) {
254 print OUT "<td>$row->[$j]</td>\n"
255 }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000256
257 # Emit the "View" link.
Ted Kremenek5744dc22008-04-02 18:03:36 +0000258
Ted Kremenek22d6a632008-04-02 20:43:36 +0000259 print OUT " <td class=\"View\"><a href=\"$ReportFile#EndPath\">View</a></td>\n";
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000260
261 # End the row.
Ted Kremenek5744dc22008-04-02 18:03:36 +0000262 print OUT "</tr>\n";
263 }
264
265 print OUT "</table>\n</body></html>\n";
266 close(OUT);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000267
268 print "$RealBin\n";
269
270 CopyJS($Dir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000271}
272
273##----------------------------------------------------------------------------##
Ted Kremenekdab11102008-04-02 04:43:42 +0000274# RunBuildCommand - Run the build command.
275##----------------------------------------------------------------------------##
276
277sub RunBuildCommand {
278
279 my $Args = shift;
Ted Kremenek7442ca62008-04-02 16:04:51 +0000280 my $IgnoreErrors = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000281 my $Cmd = $Args->[0];
282
Ted Kremenek6a43ba92008-04-02 15:34:12 +0000283 if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc") {
Ted Kremenekdab11102008-04-02 04:43:42 +0000284 shift @$Args;
285 unshift @$Args, "ccc-analyzer"
286 }
Ted Kremenek7442ca62008-04-02 16:04:51 +0000287 elsif ($IgnoreErrors) {
288 if ($Cmd eq "make" or $Cmd eq "gmake") {
289 push @$Args, "-k";
290 }
291 elsif ($Cmd eq "xcodebuild") {
292 push @$Args, "-PBXBuildsContinueAfterErrors=YES";
293 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000294 }
Ted Kremenekdab11102008-04-02 04:43:42 +0000295
296 system(@$Args);
297}
298
Ted Kremenekdab11102008-04-02 04:43:42 +0000299##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000300# DisplayHelp - Utility function to display all help options.
301##----------------------------------------------------------------------------##
302
Sam Bishopa0e22662008-04-02 03:35:43 +0000303sub DisplayHelp {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000304
Ted Kremenek5744dc22008-04-02 18:03:36 +0000305print <<ENDTEXT;
Sam Bishopa0e22662008-04-02 03:35:43 +0000306USAGE: $Prog [options] <build command> [build options]
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000307
308OPTIONS:
309
310 -o - Target directory for HTML report files. Subdirectories
Sam Bishopa0e22662008-04-02 03:35:43 +0000311 will be created as needed to represent separate "runs" of
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000312 the analyzer. If this option is not specified, a directory
313 is created in /tmp to store the reports.
314
Sam Bishopa0e22662008-04-02 03:35:43 +0000315 -?, -h - Display this message.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000316 --help
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000317
Ted Kremenekaf08f642008-04-02 16:31:58 +0000318 -k - Add a "keep on going" option to the specified build command.
Ted Kremenekf02e8db2008-04-02 16:41:25 +0000319 --keep-going This option currently supports make and xcodebuild.
320 This is a convenience option; one can specify this
321 behavior directly using build options.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000322
Ted Kremenekdab11102008-04-02 04:43:42 +0000323 -v - Verbose output from $Prog and the analyzer.
324 A second "-v" increases verbosity.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000325
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000326 -V - View analysis results in a web browser when the build
327 --view completes.
328
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000329BUILD OPTIONS
330
Ted Kremenek39eefde2008-04-02 16:47:27 +0000331 You can specify any build option acceptable to the build command.
332
Ted Kremenek5744dc22008-04-02 18:03:36 +0000333EXAMPLE
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000334
Ted Kremenek5744dc22008-04-02 18:03:36 +0000335 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000336
Ted Kremenek39eefde2008-04-02 16:47:27 +0000337 The above example causes analysis reports to be deposited into
338 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
339 A different subdirectory is created each time $Prog analyzes a project.
340 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000341
342ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000343}
344
345##----------------------------------------------------------------------------##
346# Process command-line arguments.
347##----------------------------------------------------------------------------##
348
349my $HtmlDir; # Parent directory to store HTML files.
350my $IgnoreErrors = 0; # Ignore build errors.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000351my $ViewResults = 0; # View results when the build terminates.
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000352
353if (!@ARGV) {
354 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000355 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000356}
357
358while (@ARGV) {
359
360 # Scan for options we recognize.
361
362 my $arg = $ARGV[0];
363
Sam Bishopa0e22662008-04-02 03:35:43 +0000364 if ($arg eq "-?" or $arg eq "-h" or $arg eq "--help") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000365 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000366 exit 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000367 }
368
369 if ($arg eq "-o") {
370 shift @ARGV;
371
372 if (!@ARGV) {
Ted Kremenek0062ad42008-04-02 16:35:01 +0000373 die "$Prog: '-o' option requires a target directory name.\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000374 }
375
376 $HtmlDir = shift @ARGV;
377 next;
378 }
379
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000380 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000381 shift @ARGV;
382 $IgnoreErrors = 1;
383 next;
384 }
385
386 if ($arg eq "-v") {
387 shift @ARGV;
388 $Verbose++;
389 next;
390 }
391
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000392 if ($arg eq "-V" or $arg eq "--view") {
393 shift @ARGV;
394 $ViewResults = 1;
395 next;
396 }
397
Ted Kremenek0062ad42008-04-02 16:35:01 +0000398 die "$Prog: unrecognized option '$arg'\n" if ($arg =~ /^-/);
399
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000400 last;
401}
402
403if (!@ARGV) {
404 print STDERR "$Prog: No build command specified.\n\n";
405 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000406 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000407}
408
409# Determine the output directory for the HTML reports.
410
411if (!defined($HtmlDir)) {
412
Sam Bishopa0e22662008-04-02 03:35:43 +0000413 $HtmlDir = mkdtemp("/tmp/$Prog-XXXXXX");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000414
415 if (!defined($HtmlDir)) {
Sam Bishopa0e22662008-04-02 03:35:43 +0000416 die "error: Cannot create HTML directory in /tmp.\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000417 }
418
419 if (!$Verbose) {
420 print "$Prog: Using '$HtmlDir' as base HTML report directory.\n";
421 }
422}
423
Sam Bishopa0e22662008-04-02 03:35:43 +0000424$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000425
426# Set the appropriate environment variables.
427
Sam Bishopa0e22662008-04-02 03:35:43 +0000428SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000429
430$ENV{'CC'} = "ccc-analyzer";
431
432if ($Verbose >= 2) {
433 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
434}
435
436# Run the build.
437
Ted Kremenek7442ca62008-04-02 16:04:51 +0000438RunBuildCommand(\@ARGV, $IgnoreErrors);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000439
440# Postprocess the HTML directory.
441
Sam Bishopa0e22662008-04-02 03:35:43 +0000442Postprocess($HtmlDir);
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000443
444if ($ViewResults and -r "$HtmlDir/index.html") {
445 # Only works on Mac OS X (for now).
446 print "Viewing analysis results: '$HtmlDir/index.html'\n";
447 `open $HtmlDir/index.html`
448}