Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1 | #!/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 | |
| 15 | use strict; |
| 16 | use warnings; |
| 17 | use File::Temp qw/ :mktemp /; |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 18 | use FindBin qw($RealBin); |
Ted Kremenek | a6e2481 | 2008-04-19 18:05:48 +0000 | [diff] [blame] | 19 | use Digest::MD5; |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 20 | use File::Basename; |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 21 | use Term::ANSIColor; |
| 22 | use Term::ANSIColor qw(:constants); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 23 | |
| 24 | my $Verbose = 0; # Verbose output from this script. |
| 25 | my $Prog = "scan-build"; |
Ted Kremenek | f4cdf41 | 2008-05-23 18:17:05 +0000 | [diff] [blame] | 26 | my $BuildName; |
| 27 | my $BuildDate; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 28 | |
Ted Kremenek | f2f8d6c | 2008-06-17 03:06:59 +0000 | [diff] [blame] | 29 | my $UseColor = ((($ENV{'TERM'} eq 'xterm-color') and -t STDOUT) |
| 30 | and defined($ENV{'SCAN_BUILD_COLOR'})); |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 31 | |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 32 | ##----------------------------------------------------------------------------## |
| 33 | # Diagnostics |
| 34 | ##----------------------------------------------------------------------------## |
| 35 | |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 36 | sub Diag { |
| 37 | if ($UseColor) { |
| 38 | print BOLD, MAGENTA "$Prog: @_"; |
| 39 | print RESET; |
| 40 | } |
| 41 | else { |
| 42 | print "$Prog: @_"; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | sub 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 Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 58 | ##----------------------------------------------------------------------------## |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 59 | # Some initial preprocessing of Clang options. |
| 60 | ##----------------------------------------------------------------------------## |
| 61 | |
| 62 | my $ClangSB = "$RealBin/clang"; |
| 63 | my $Clang = $ClangSB; |
| 64 | |
| 65 | if (! -x $ClangSB) { |
| 66 | $Clang = "clang"; |
| 67 | } |
| 68 | |
| 69 | my %AvailableAnalyses; |
| 70 | |
| 71 | # Query clang for analysis options. |
Ted Kremenek | 2be1853 | 2008-07-18 23:13:03 +0000 | [diff] [blame] | 72 | open(PIPE, "'$Clang' --help |") or |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 73 | DieDiag("Cannot execute '$Clang'"); |
| 74 | |
| 75 | my $FoundAnalysis = 0; |
| 76 | |
| 77 | while(<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 | |
| 97 | close (PIPE); |
| 98 | |
| 99 | my %AnalysesDefaultEnabled = ( |
| 100 | '-warn-dead-stores' => 1, |
| 101 | '-checker-cfref' => 1, |
Ted Kremenek | 9012599 | 2008-07-15 23:41:32 +0000 | [diff] [blame] | 102 | '-warn-objc-methodsigs' => 1, |
Ted Kremenek | bde3a05 | 2008-07-25 20:35:01 +0000 | [diff] [blame^] | 103 | '-warn-objc-missing-dealloc' => 1, |
| 104 | '-warn-objc-unused-ivars' => 1 |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 105 | ); |
| 106 | |
| 107 | ##----------------------------------------------------------------------------## |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 108 | # GetHTMLRunDir - Construct an HTML directory name for the current run. |
| 109 | ##----------------------------------------------------------------------------## |
| 110 | |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 111 | sub GetHTMLRunDir { |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 112 | |
| 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 Kremenek | 9d7405f | 2008-05-14 17:23:56 +0000 | [diff] [blame] | 125 | my $DateString = sprintf("%d-%02d-%02d", $year, $month, $day); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 126 | |
| 127 | # Determine the run number. |
| 128 | |
| 129 | my $RunNumber; |
| 130 | |
| 131 | if (-d $Dir) { |
| 132 | |
| 133 | if (! -r $Dir) { |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 134 | DieDiag("directory '$Dir' exists but is not readable.\n"); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 135 | } |
| 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 Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 164 | DieDiag("'$Dir' exists but is not a directory.\n"); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 165 | } |
| 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 Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 180 | sub SetHtmlEnv { |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 181 | |
| 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 Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 196 | Diag("Emitting reports for this run to '$Dir'.\n"); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | $ENV{'CCC_ANALYZER_HTML'} = $Dir; |
| 200 | } |
| 201 | |
| 202 | ##----------------------------------------------------------------------------## |
Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 203 | # ComputeDigest - Compute a digest of the specified file. |
| 204 | ##----------------------------------------------------------------------------## |
| 205 | |
| 206 | sub ComputeDigest { |
| 207 | my $FName = shift; |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 208 | DieDiag("Cannot read $FName to compute Digest.\n") if (! -r $FName); |
Ted Kremenek | a6e2481 | 2008-04-19 18:05:48 +0000 | [diff] [blame] | 209 | |
| 210 | # Use Digest::MD5. We don't have to be cryptographically secure. We're |
Ted Kremenek | 7ea02e6 | 2008-04-19 18:07:44 +0000 | [diff] [blame] | 211 | # 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 Kremenek | a6e2481 | 2008-04-19 18:05:48 +0000 | [diff] [blame] | 213 | # come bundled on most systems. |
| 214 | |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 215 | open(FILE, $FName) or DieDiag("Cannot open $FName when computing Digest.\n"); |
Ted Kremenek | a6e2481 | 2008-04-19 18:05:48 +0000 | [diff] [blame] | 216 | binmode FILE; |
| 217 | my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest; |
| 218 | close(FILE); |
| 219 | |
| 220 | # Return the digest. |
| 221 | |
| 222 | return $Result; |
Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | ##----------------------------------------------------------------------------## |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 226 | # UpdatePrefix - Compute the common prefix of files. |
| 227 | ##----------------------------------------------------------------------------## |
| 228 | |
| 229 | my $Prefix; |
| 230 | |
| 231 | sub 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 | |
| 251 | sub GetPrefix { |
| 252 | return $Prefix; |
| 253 | } |
| 254 | |
| 255 | ##----------------------------------------------------------------------------## |
| 256 | # UpdateInFilePath - Update the path in the report file. |
| 257 | ##----------------------------------------------------------------------------## |
| 258 | |
| 259 | sub 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 Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 274 | system("mv", "$fname.tmp", $fname); |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | ##----------------------------------------------------------------------------## |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 278 | # ScanFile - Scan a report file for various identifying attributes. |
| 279 | ##----------------------------------------------------------------------------## |
| 280 | |
Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 281 | # 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 | |
| 284 | my %AlreadyScanned; |
| 285 | |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 286 | sub ScanFile { |
| 287 | |
| 288 | my $Index = shift; |
| 289 | my $Dir = shift; |
| 290 | my $FName = shift; |
| 291 | |
Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 292 | # 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 Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 299 | system ("rm", "-f", "$Dir/$FName"); |
Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 300 | return; |
| 301 | } |
| 302 | |
| 303 | $AlreadyScanned{$digest} = 1; |
| 304 | |
Ted Kremenek | 809709f | 2008-04-18 16:58:34 +0000 | [diff] [blame] | 305 | # At this point the report file is not world readable. Make it happen. |
Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 306 | system ("chmod", "644", "$Dir/$FName"); |
Ted Kremenek | 684bb09 | 2008-04-18 15:18:20 +0000 | [diff] [blame] | 307 | |
| 308 | # Scan the report file for tags. |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 309 | open(IN, "$Dir/$FName") or DieDiag("Cannot open '$Dir/$FName'\n"); |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 310 | |
| 311 | my $BugDesc = ""; |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 312 | my $BugFile = ""; |
| 313 | my $BugPathLength = 1; |
| 314 | my $BugLine = 0; |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 315 | |
| 316 | while (<IN>) { |
| 317 | |
| 318 | if (/<!-- BUGDESC (.*) -->$/) { |
| 319 | $BugDesc = $1; |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 320 | } |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 321 | elsif (/<!-- BUGFILE (.*) -->$/) { |
| 322 | $BugFile = $1; |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 323 | UpdatePrefix($BugFile); |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 324 | } |
| 325 | elsif (/<!-- BUGPATHLENGTH (.*) -->$/) { |
| 326 | $BugPathLength = $1; |
| 327 | } |
| 328 | elsif (/<!-- BUGLINE (.*) -->$/) { |
| 329 | $BugLine = $1; |
| 330 | } |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | close(IN); |
| 334 | |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 335 | push @$Index,[ $FName, $BugDesc, $BugFile, $BugLine, $BugPathLength ]; |
| 336 | } |
| 337 | |
| 338 | ##----------------------------------------------------------------------------## |
| 339 | # CopyJS - Copy JavaScript code to target directory. |
| 340 | ##----------------------------------------------------------------------------## |
| 341 | |
| 342 | sub CopyJS { |
| 343 | |
| 344 | my $Dir = shift; |
| 345 | |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 346 | DieDiag("Cannot find 'sorttable.js'.\n") |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 347 | if (! -r "$RealBin/sorttable.js"); |
| 348 | |
Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 349 | system ("cp", "$RealBin/sorttable.js", "$Dir"); |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 350 | |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 351 | DieDiag("Could not copy 'sorttable.js' to '$Dir'.\n") |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 352 | if (! -r "$Dir/sorttable.js"); |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | ##----------------------------------------------------------------------------## |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 356 | # Postprocess - Postprocess the results of an analysis scan. |
| 357 | ##----------------------------------------------------------------------------## |
| 358 | |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 359 | sub Postprocess { |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 360 | |
| 361 | my $Dir = shift; |
Ted Kremenek | 684bb09 | 2008-04-18 15:18:20 +0000 | [diff] [blame] | 362 | my $BaseDir = shift; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 363 | |
| 364 | die "No directory specified." if (!defined($Dir)); |
Ted Kremenek | 684bb09 | 2008-04-18 15:18:20 +0000 | [diff] [blame] | 365 | die "No base directory specified." if (!defined($BaseDir)); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 366 | |
| 367 | if (! -d $Dir) { |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 368 | Diag("No bugs found.\n"); |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 369 | return 0; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | opendir(DIR, $Dir); |
| 373 | my @files = grep(/^report-.*\.html$/,readdir(DIR)); |
| 374 | closedir(DIR); |
| 375 | |
| 376 | if (scalar(@files) == 0) { |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 377 | Diag("Removing directory '$Dir' because it contains no reports.\n"); |
Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 378 | system ("rm", "-fR", $Dir); |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 379 | |
| 380 | # Remove the base directory if it contains no files (don't use '-R'). |
Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 381 | system ("rm", "-f", $BaseDir); |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 382 | |
| 383 | Diag("No bugs found.\n"); |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 384 | return 0; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 385 | } |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 386 | |
| 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 Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 397 | open(OUT, ">$FName") or DieDiag("Cannot create file '$FName'\n"); |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 398 | |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 399 | # Print out the header. |
| 400 | |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 401 | print OUT <<ENDTEXT; |
| 402 | <html> |
| 403 | <head> |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 404 | <style type="text/css"> |
| 405 | body { color:#000000; background-color:#ffffff } |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 406 | body { font-family: Helvetica, sans-serif; font-size:9pt } |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 407 | h1 { font-size:12pt } |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 408 | table.sortable thead { |
| 409 | background-color:#eee; color:#666666; |
| 410 | font-weight: bold; cursor: default; |
Ted Kremenek | bba1cf5 | 2008-04-03 05:50:51 +0000 | [diff] [blame] | 411 | text-align:center; |
| 412 | border-top: 2px solid #000000; |
| 413 | border-bottom: 2px solid #000000; |
| 414 | font-weight: bold; font-family: Verdana |
| 415 | } |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 416 | table.sortable { border: 1px #000000 solid } |
| 417 | table.sortable { border-collapse: collapse; border-spacing: 0px } |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 418 | td { border-bottom: 1px #000000 dotted } |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 419 | td { padding:5px; padding-left:8px; padding-right:8px } |
Ted Kremenek | d8c6d0c | 2008-04-07 23:50:07 +0000 | [diff] [blame] | 420 | td { text-align:left; font-size:9pt } |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 421 | td.View { padding-left: 10px } |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 422 | </style> |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 423 | <script src="sorttable.js"></script> |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 424 | <script language='javascript' type="text/javascript"> |
| 425 | function 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 | |
| 435 | function ToggleDisplay(CheckButton, ClassName) { |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 436 | if (CheckButton.checked) { |
| 437 | SetDisplay(ClassName, ""); |
| 438 | } |
| 439 | else { |
| 440 | SetDisplay(ClassName, "none"); |
| 441 | } |
| 442 | } |
| 443 | </script> |
| 444 | </head> |
| 445 | <body> |
| 446 | ENDTEXT |
| 447 | |
| 448 | # Print out the summary table. |
| 449 | |
| 450 | my %Totals; |
| 451 | |
| 452 | for my $row ( @Index ) { |
| 453 | |
Ted Kremenek | 432af59 | 2008-05-06 18:11:36 +0000 | [diff] [blame] | 454 | #my $bug_type = lc($row->[1]); |
| 455 | my $bug_type = ($row->[1]); |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 456 | |
| 457 | if (!defined($Totals{$bug_type})) { |
| 458 | $Totals{$bug_type} = 1; |
| 459 | } |
| 460 | else { |
| 461 | $Totals{$bug_type}++; |
| 462 | } |
| 463 | } |
Ted Kremenek | f4cdf41 | 2008-05-23 18:17:05 +0000 | [diff] [blame] | 464 | |
| 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 Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 471 | print OUT <<ENDTEXT; |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 472 | <table class="sortable"> |
| 473 | <tr> |
| 474 | <td>Bug Type</td> |
| 475 | <td>Quantity</td> |
Ted Kremenek | 2645c77 | 2008-07-07 16:58:44 +0000 | [diff] [blame] | 476 | <td class="sorttable_nosort">Display?</td> |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 477 | </tr> |
| 478 | ENDTEXT |
| 479 | |
| 480 | for my $key ( sort { $a cmp $b } keys %Totals ) { |
Ted Kremenek | bdf66c7 | 2008-05-06 23:51:45 +0000 | [diff] [blame] | 481 | my $x = lc($key); |
| 482 | $x =~ s/\s[,]/_/g; |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 483 | 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 | |
| 488 | print OUT <<ENDTEXT; |
| 489 | </table> |
| 490 | <h3>Reports</h3> |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 491 | <table class="sortable"> |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 492 | <tr> |
Ted Kremenek | 88a96d6 | 2008-07-07 17:23:32 +0000 | [diff] [blame] | 493 | <td class="sorttable_sorted">Bug Type<span id="sorttable_sortfwdind"> ▾</span> |
Ted Kremenek | bba1cf5 | 2008-04-03 05:50:51 +0000 | [diff] [blame] | 494 | <td>File</td> |
| 495 | <td>Line</td> |
| 496 | <td>Path Length</td> |
Ted Kremenek | 2645c77 | 2008-07-07 16:58:44 +0000 | [diff] [blame] | 497 | <td class="sorttable_nosort"></td> |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 498 | </tr> |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 499 | ENDTEXT |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 500 | |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 501 | 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 Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 511 | for my $row ( sort { $a->[1] cmp $b->[1] } @Index ) { |
| 512 | |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 513 | my $x = lc($row->[1]); |
Ted Kremenek | bdf66c7 | 2008-05-06 23:51:45 +0000 | [diff] [blame] | 514 | $x =~ s/\s[,]/_/g; |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 515 | |
| 516 | print OUT "<tr class=\"bt_$x\">\n"; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 517 | |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 518 | my $ReportFile = $row->[0]; |
| 519 | |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 520 | print OUT " <td class=\"DESC\">"; |
Ted Kremenek | 432af59 | 2008-05-06 18:11:36 +0000 | [diff] [blame] | 521 | #print OUT lc($row->[1]); |
| 522 | print OUT $row->[1]; |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 523 | print OUT "</td>\n"; |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 524 | |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 525 | # 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 Kremenek | 3e56e0b | 2008-05-02 23:40:49 +0000 | [diff] [blame] | 532 | |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 533 | print OUT "<td>$fname</td>\n"; |
| 534 | |
| 535 | # Print the rest of the columns. |
| 536 | |
| 537 | for my $j ( 3 .. $#{$row} ) { |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 538 | print OUT "<td>$row->[$j]</td>\n" |
| 539 | } |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 540 | |
| 541 | # Emit the "View" link. |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 542 | |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 543 | print OUT " <td class=\"View\"><a href=\"$ReportFile#EndPath\">View</a></td>\n"; |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 544 | |
| 545 | # End the row. |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 546 | print OUT "</tr>\n"; |
| 547 | } |
| 548 | |
| 549 | print OUT "</table>\n</body></html>\n"; |
| 550 | close(OUT); |
Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 551 | |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 552 | CopyJS($Dir); |
Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 553 | |
| 554 | # Make sure $Dir and $BaseDir are world readable/executable. |
| 555 | system("chmod", "755", $Dir); |
| 556 | system("chmod", "755", $BaseDir); |
| 557 | |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 558 | my $Num = scalar(@Index); |
Ted Kremenek | 150c212 | 2008-07-11 19:15:05 +0000 | [diff] [blame] | 559 | Diag("$Num bugs found.\n"); |
| 560 | if ($Num > 0 && -r "$Dir/index.html") { |
| 561 | Diag("Open '$Dir/index.html' to examine bug reports.\n"); |
| 562 | } |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 563 | |
| 564 | return $Num; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | ##----------------------------------------------------------------------------## |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 568 | # RunBuildCommand - Run the build command. |
| 569 | ##----------------------------------------------------------------------------## |
| 570 | |
Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 571 | sub AddIfNotPresent { |
| 572 | my $Args = shift; |
| 573 | my $Arg = shift; |
| 574 | my $found = 0; |
| 575 | |
| 576 | foreach my $k (@$Args) { |
| 577 | if ($k eq $Arg) { |
| 578 | $found = 1; |
| 579 | last; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | if ($found == 0) { |
| 584 | push @$Args, $Arg; |
| 585 | } |
| 586 | } |
| 587 | |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 588 | sub RunBuildCommand { |
| 589 | |
| 590 | my $Args = shift; |
Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame] | 591 | my $IgnoreErrors = shift; |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 592 | my $Cmd = $Args->[0]; |
Ted Kremenek | 6195c37 | 2008-06-02 21:52:47 +0000 | [diff] [blame] | 593 | my $CCAnalyzer = shift; |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 594 | |
Ted Kremenek | 3301cb1 | 2008-06-30 18:18:16 +0000 | [diff] [blame] | 595 | # Get only the part of the command after the last '/'. |
| 596 | if ($Cmd =~ /\/([^\/]+)$/) { |
| 597 | $Cmd = $1; |
| 598 | } |
| 599 | |
Ted Kremenek | 6a43ba9 | 2008-04-02 15:34:12 +0000 | [diff] [blame] | 600 | if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc") { |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 601 | shift @$Args; |
Ted Kremenek | 6195c37 | 2008-06-02 21:52:47 +0000 | [diff] [blame] | 602 | unshift @$Args, $CCAnalyzer; |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 603 | } |
Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame] | 604 | elsif ($IgnoreErrors) { |
| 605 | if ($Cmd eq "make" or $Cmd eq "gmake") { |
Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 606 | AddIfNotPresent($Args,"-k"); |
Ted Kremenek | 8912b54 | 2008-05-13 21:28:02 +0000 | [diff] [blame] | 607 | AddIfNotPresent($Args,"-i"); |
Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame] | 608 | } |
| 609 | elsif ($Cmd eq "xcodebuild") { |
Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 610 | AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES"); |
Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame] | 611 | } |
Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 614 | if ($Cmd eq "xcodebuild") { |
Ted Kremenek | cfd4c7b | 2008-05-23 22:18:16 +0000 | [diff] [blame] | 615 | # Disable distributed builds for xcodebuild. |
Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 616 | AddIfNotPresent($Args,"-nodistribute"); |
Ted Kremenek | cfd4c7b | 2008-05-23 22:18:16 +0000 | [diff] [blame] | 617 | |
| 618 | # Disable PCH files until clang supports them. |
| 619 | AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO"); |
Ted Kremenek | 915e972 | 2008-05-27 23:18:07 +0000 | [diff] [blame] | 620 | |
| 621 | # When 'CC' is set, xcodebuild uses it to do all linking, even if we are |
| 622 | # linking C++ object files. Set 'LDPLUSPLUS' so that xcodebuild uses 'g++' |
| 623 | # when linking such files. |
| 624 | my $LDPLUSPLUS = `which g++`; |
| 625 | $LDPLUSPLUS =~ s/\015?\012//; # strip newlines |
| 626 | $ENV{'LDPLUSPLUS'} = $LDPLUSPLUS; |
Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 627 | } |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 628 | |
Ted Kremenek | 5656a98 | 2008-07-15 17:09:28 +0000 | [diff] [blame] | 629 | return system(@$Args); |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 632 | ##----------------------------------------------------------------------------## |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 633 | # DisplayHelp - Utility function to display all help options. |
| 634 | ##----------------------------------------------------------------------------## |
| 635 | |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 636 | sub DisplayHelp { |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 637 | |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 638 | print <<ENDTEXT; |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 639 | USAGE: $Prog [options] <build command> [build options] |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 640 | |
Ted Kremenek | f4cdf41 | 2008-05-23 18:17:05 +0000 | [diff] [blame] | 641 | ENDTEXT |
| 642 | |
| 643 | if (defined($BuildName)) { |
| 644 | print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n"; |
| 645 | } |
| 646 | |
| 647 | print <<ENDTEXT; |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 648 | OPTIONS: |
| 649 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 650 | -o - Target directory for HTML report files. Subdirectories |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 651 | will be created as needed to represent separate "runs" of |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 652 | the analyzer. If this option is not specified, a directory |
| 653 | is created in /tmp to store the reports. |
Ted Kremenek | 1262fc4 | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 654 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 655 | -h - Display this message. |
| 656 | --help |
Ted Kremenek | 1262fc4 | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 657 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 658 | -k - Add a "keep on going" option to the specified build command. |
| 659 | --keep-going This option currently supports make and xcodebuild. |
Ted Kremenek | f02e8db | 2008-04-02 16:41:25 +0000 | [diff] [blame] | 660 | This is a convenience option; one can specify this |
| 661 | behavior directly using build options. |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 662 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 663 | --status-bugs - By default, the exit status of $Prog is the same as the |
| 664 | executed build command. Specifying this option causes the |
| 665 | exit status of $Prog to be 1 if it found potential bugs |
| 666 | and 0 otherwise. |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 667 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 668 | -v - Verbose output from $Prog and the analyzer. |
| 669 | A second and third "-v" increases verbosity. |
| 670 | |
| 671 | -V - View analysis results in a web browser when the build |
| 672 | --view completes. |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 673 | |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 674 | ENDTEXT |
| 675 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 676 | print " Available Source Code Analyses (multiple analyses may be specified):\n\n"; |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 677 | |
| 678 | foreach my $Analysis (sort keys %AvailableAnalyses) { |
| 679 | if (defined($AnalysesDefaultEnabled{$Analysis})) { |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 680 | print " (+)"; |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 681 | } |
| 682 | else { |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 683 | print " "; |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | print " $Analysis $AvailableAnalyses{$Analysis}\n"; |
| 687 | } |
| 688 | |
| 689 | print <<ENDTEXT |
| 690 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 691 | NOTE: "(+)" indicates that an analysis is enabled by default unless one |
| 692 | or more analysis options are specified |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 693 | |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 694 | BUILD OPTIONS |
| 695 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 696 | You can specify any build option acceptable to the build command. |
Ted Kremenek | 39eefde | 2008-04-02 16:47:27 +0000 | [diff] [blame] | 697 | |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 698 | EXAMPLE |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 699 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 700 | $Prog -o /tmp/myhtmldir make -j4 |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 701 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 702 | The above example causes analysis reports to be deposited into |
| 703 | a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option. |
| 704 | A different subdirectory is created each time $Prog analyzes a project. |
| 705 | The analyzer should support most parallel builds, but not distributed builds. |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 706 | |
| 707 | ENDTEXT |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | ##----------------------------------------------------------------------------## |
| 711 | # Process command-line arguments. |
| 712 | ##----------------------------------------------------------------------------## |
| 713 | |
| 714 | my $HtmlDir; # Parent directory to store HTML files. |
| 715 | my $IgnoreErrors = 0; # Ignore build errors. |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 716 | my $ViewResults = 0; # View results when the build terminates. |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 717 | my $ExitStatusFoundBugs = 0; # Exit status reflects whether bugs were found |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 718 | my @AnalysesToRun; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 719 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 720 | |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 721 | if (!@ARGV) { |
| 722 | DisplayHelp(); |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 723 | exit 1; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | while (@ARGV) { |
| 727 | |
| 728 | # Scan for options we recognize. |
| 729 | |
| 730 | my $arg = $ARGV[0]; |
| 731 | |
Sam Bishop | 2f2418e | 2008-04-03 14:29:47 +0000 | [diff] [blame] | 732 | if ($arg eq "-h" or $arg eq "--help") { |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 733 | DisplayHelp(); |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 734 | exit 0; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 737 | if (defined($AvailableAnalyses{$arg})) { |
Ted Kremenek | 1262fc4 | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 738 | shift @ARGV; |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 739 | push @AnalysesToRun, $arg; |
Ted Kremenek | 1262fc4 | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 740 | next; |
| 741 | } |
| 742 | |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 743 | if ($arg eq "-o") { |
| 744 | shift @ARGV; |
| 745 | |
| 746 | if (!@ARGV) { |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 747 | DieDiag("'-o' option requires a target directory name.\n"); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | $HtmlDir = shift @ARGV; |
| 751 | next; |
| 752 | } |
| 753 | |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 754 | if ($arg eq "-k" or $arg eq "--keep-going") { |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 755 | shift @ARGV; |
| 756 | $IgnoreErrors = 1; |
| 757 | next; |
| 758 | } |
| 759 | |
| 760 | if ($arg eq "-v") { |
| 761 | shift @ARGV; |
| 762 | $Verbose++; |
| 763 | next; |
| 764 | } |
| 765 | |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 766 | if ($arg eq "-V" or $arg eq "--view") { |
| 767 | shift @ARGV; |
| 768 | $ViewResults = 1; |
| 769 | next; |
| 770 | } |
| 771 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 772 | if ($arg eq "--status-bugs") { |
| 773 | shift @ARGV; |
| 774 | $ExitStatusFoundBugs = 1; |
| 775 | next; |
| 776 | } |
| 777 | |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 778 | DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/); |
Ted Kremenek | 0062ad4 | 2008-04-02 16:35:01 +0000 | [diff] [blame] | 779 | |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 780 | last; |
| 781 | } |
| 782 | |
| 783 | if (!@ARGV) { |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 784 | Diag("No build command specified.\n\n"); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 785 | DisplayHelp(); |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 786 | exit 1; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | # Determine the output directory for the HTML reports. |
| 790 | |
| 791 | if (!defined($HtmlDir)) { |
| 792 | |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 793 | $HtmlDir = mkdtemp("/tmp/$Prog-XXXXXX"); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 794 | |
| 795 | if (!defined($HtmlDir)) { |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 796 | DieDiag("Cannot create HTML directory in /tmp.\n"); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | if (!$Verbose) { |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 800 | Diag("Using '$HtmlDir' as base HTML report directory.\n"); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 801 | } |
| 802 | } |
| 803 | |
Ted Kremenek | 684bb09 | 2008-04-18 15:18:20 +0000 | [diff] [blame] | 804 | my $BaseDir = $HtmlDir; |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 805 | $HtmlDir = GetHTMLRunDir($HtmlDir); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 806 | |
| 807 | # Set the appropriate environment variables. |
| 808 | |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 809 | SetHtmlEnv(\@ARGV, $HtmlDir); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 810 | |
Ted Kremenek | 0b6c153 | 2008-04-08 20:22:12 +0000 | [diff] [blame] | 811 | my $Cmd = "$RealBin/ccc-analyzer"; |
| 812 | |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 813 | DieDiag("Executable 'ccc-analyzer' does not exist at '$Cmd'\n") |
Ted Kremenek | 0b6c153 | 2008-04-08 20:22:12 +0000 | [diff] [blame] | 814 | if (! -x $Cmd); |
Ted Kremenek | f22eacb | 2008-04-18 22:00:56 +0000 | [diff] [blame] | 815 | |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 816 | if (! -x $ClangSB) { |
| 817 | Diag("'clang' executable not found in '$RealBin'.\n"); |
| 818 | Diag("Using 'clang' from path.\n"); |
Ted Kremenek | f22eacb | 2008-04-18 22:00:56 +0000 | [diff] [blame] | 819 | } |
Ted Kremenek | 0b6c153 | 2008-04-08 20:22:12 +0000 | [diff] [blame] | 820 | |
Ted Kremenek | 4f4b17d | 2008-04-03 20:08:18 +0000 | [diff] [blame] | 821 | $ENV{'CC'} = $Cmd; |
Ted Kremenek | f22eacb | 2008-04-18 22:00:56 +0000 | [diff] [blame] | 822 | $ENV{'CLANG'} = $Clang; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 823 | |
| 824 | if ($Verbose >= 2) { |
| 825 | $ENV{'CCC_ANALYZER_VERBOSE'} = 1; |
| 826 | } |
| 827 | |
Ted Kremenek | a9525c9 | 2008-05-12 22:07:14 +0000 | [diff] [blame] | 828 | if ($Verbose >= 3) { |
| 829 | $ENV{'CCC_ANALYZER_LOG'} = 1; |
| 830 | } |
| 831 | |
Ted Kremenek | 9012599 | 2008-07-15 23:41:32 +0000 | [diff] [blame] | 832 | if (scalar(@AnalysesToRun) == 0) { |
| 833 | foreach my $key (keys %AnalysesDefaultEnabled) { |
| 834 | push @AnalysesToRun,$key; |
| 835 | } |
Ted Kremenek | 0100678 | 2008-07-02 23:16:10 +0000 | [diff] [blame] | 836 | } |
Ted Kremenek | 1262fc4 | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 837 | |
Ted Kremenek | 9012599 | 2008-07-15 23:41:32 +0000 | [diff] [blame] | 838 | $ENV{'CCC_ANALYZER_ANALYSIS'} = join ' ',@AnalysesToRun; |
| 839 | |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 840 | # Run the build. |
| 841 | |
Ted Kremenek | 5656a98 | 2008-07-15 17:09:28 +0000 | [diff] [blame] | 842 | my $ExitStatus = RunBuildCommand(\@ARGV, $IgnoreErrors, $Cmd); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 843 | |
| 844 | # Postprocess the HTML directory. |
| 845 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 846 | my $NumBugs = Postprocess($HtmlDir, $BaseDir); |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 847 | |
| 848 | if ($ViewResults and -r "$HtmlDir/index.html") { |
| 849 | # Only works on Mac OS X (for now). |
| 850 | print "Viewing analysis results: '$HtmlDir/index.html'\n"; |
Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 851 | system("open", "$HtmlDir/index.html"); |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 852 | } |
Ted Kremenek | 5656a98 | 2008-07-15 17:09:28 +0000 | [diff] [blame] | 853 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 854 | if ($ExitStatusFoundBugs) { |
| 855 | exit 1 if ($NumBugs > 0); |
| 856 | exit 0; |
| 857 | } |
| 858 | |
Ted Kremenek | 5656a98 | 2008-07-15 17:09:28 +0000 | [diff] [blame] | 859 | exit $ExitStatus; |
| 860 | |