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 | 63c2017 | 2008-08-04 17:34:06 +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'"); |
Ted Kremenek | 63c2017 | 2008-08-04 17:34:06 +0000 | [diff] [blame] | 74 | |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 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 | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 108 | # GetHTMLRunDir - Construct an HTML directory name for the current sub-run. |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 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 | |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 113 | die "Not enough arguments." if (@_ == 0); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 114 | my $Dir = shift @_; |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 115 | |
| 116 | my $TmpMode = 0; |
| 117 | if (!defined $Dir) { |
| 118 | $Dir = "/tmp"; |
| 119 | $TmpMode = 1; |
| 120 | } |
| 121 | |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 122 | # Get current date and time. |
| 123 | |
| 124 | my @CurrentTime = localtime(); |
| 125 | |
| 126 | my $year = $CurrentTime[5] + 1900; |
| 127 | my $day = $CurrentTime[3]; |
| 128 | my $month = $CurrentTime[4] + 1; |
| 129 | |
Ted Kremenek | 9d7405f | 2008-05-14 17:23:56 +0000 | [diff] [blame] | 130 | my $DateString = sprintf("%d-%02d-%02d", $year, $month, $day); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 131 | |
| 132 | # Determine the run number. |
| 133 | |
| 134 | my $RunNumber; |
| 135 | |
| 136 | if (-d $Dir) { |
| 137 | |
| 138 | if (! -r $Dir) { |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 139 | DieDiag("directory '$Dir' exists but is not readable.\n"); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | # Iterate over all files in the specified directory. |
| 143 | |
| 144 | my $max = 0; |
| 145 | |
| 146 | opendir(DIR, $Dir); |
Ted Kremenek | 29da6c5 | 2008-08-07 17:57:34 +0000 | [diff] [blame^] | 147 | my @FILES = grep { -d "$Dir/$_" } readdir(DIR); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 148 | closedir(DIR); |
| 149 | |
| 150 | foreach my $f (@FILES) { |
| 151 | |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 152 | # Strip the prefix '$Prog-' if we are dumping files to /tmp. |
| 153 | if ($TmpMode) { |
| 154 | next if (!($f =~ /^$Prog-(.+)/)); |
| 155 | $f = $1; |
| 156 | } |
| 157 | |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 158 | my @x = split/-/, $f; |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 159 | |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 160 | next if (scalar(@x) != 4); |
| 161 | next if ($x[0] != $year); |
| 162 | next if ($x[1] != $month); |
| 163 | next if ($x[2] != $day); |
| 164 | |
| 165 | if ($x[3] > $max) { |
| 166 | $max = $x[3]; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | $RunNumber = $max + 1; |
| 171 | } |
| 172 | else { |
| 173 | |
| 174 | if (-x $Dir) { |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 175 | DieDiag("'$Dir' exists but is not a directory.\n"); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 176 | } |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 177 | |
| 178 | if ($TmpMode) { |
| 179 | DieDiag("The directory '/tmp' does not exist or cannot be accessed."); |
| 180 | } |
| 181 | |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 182 | # $Dir does not exist. It will be automatically created by the |
| 183 | # clang driver. Set the run number to 1. |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 184 | |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 185 | $RunNumber = 1; |
| 186 | } |
| 187 | |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 188 | die "RunNumber must be defined!" if (!defined $RunNumber); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 189 | |
| 190 | # Append the run number. |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 191 | if ($TmpMode) { |
| 192 | my $NewDir = "$Dir/$Prog-$DateString-$RunNumber"; |
| 193 | mkdir $NewDir; |
| 194 | return $NewDir; |
| 195 | } |
| 196 | else { |
| 197 | return "$Dir/$DateString-$RunNumber"; |
| 198 | } |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 201 | sub SetHtmlEnv { |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 202 | |
| 203 | die "Wrong number of arguments." if (scalar(@_) != 2); |
| 204 | |
| 205 | my $Args = shift; |
| 206 | my $Dir = shift; |
| 207 | |
| 208 | die "No build command." if (scalar(@$Args) == 0); |
| 209 | |
| 210 | my $Cmd = $$Args[0]; |
| 211 | |
| 212 | if ($Cmd =~ /configure/) { |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | if ($Verbose) { |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 217 | Diag("Emitting reports for this run to '$Dir'.\n"); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | $ENV{'CCC_ANALYZER_HTML'} = $Dir; |
| 221 | } |
| 222 | |
| 223 | ##----------------------------------------------------------------------------## |
Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 224 | # ComputeDigest - Compute a digest of the specified file. |
| 225 | ##----------------------------------------------------------------------------## |
| 226 | |
| 227 | sub ComputeDigest { |
| 228 | my $FName = shift; |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 229 | DieDiag("Cannot read $FName to compute Digest.\n") if (! -r $FName); |
Ted Kremenek | a6e2481 | 2008-04-19 18:05:48 +0000 | [diff] [blame] | 230 | |
| 231 | # 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] | 232 | # just looking for duplicate files that come from a non-malicious source. |
| 233 | # We use Digest::MD5 because it is a standard Perl module that should |
Ted Kremenek | 63c2017 | 2008-08-04 17:34:06 +0000 | [diff] [blame] | 234 | # come bundled on most systems. |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 235 | open(FILE, $FName) or DieDiag("Cannot open $FName when computing Digest.\n"); |
Ted Kremenek | a6e2481 | 2008-04-19 18:05:48 +0000 | [diff] [blame] | 236 | binmode FILE; |
| 237 | my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest; |
| 238 | close(FILE); |
| 239 | |
Ted Kremenek | 63c2017 | 2008-08-04 17:34:06 +0000 | [diff] [blame] | 240 | # Return the digest. |
Ted Kremenek | a6e2481 | 2008-04-19 18:05:48 +0000 | [diff] [blame] | 241 | return $Result; |
Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | ##----------------------------------------------------------------------------## |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 245 | # UpdatePrefix - Compute the common prefix of files. |
| 246 | ##----------------------------------------------------------------------------## |
| 247 | |
| 248 | my $Prefix; |
| 249 | |
| 250 | sub UpdatePrefix { |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 251 | my $x = shift; |
| 252 | my $y = basename($x); |
| 253 | $x =~ s/\Q$y\E$//; |
| 254 | |
| 255 | # Ignore /usr, /Library, /System, /Developer |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 256 | return if ( $x =~ /^\/usr/ or $x =~ /^\/Library/ |
| 257 | or $x =~ /^\/System/ or $x =~ /^\/Developer/); |
| 258 | |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 259 | if (!defined $Prefix) { |
| 260 | $Prefix = $x; |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | chop $Prefix while (!($x =~ /^$Prefix/)); |
| 265 | } |
| 266 | |
| 267 | sub GetPrefix { |
| 268 | return $Prefix; |
| 269 | } |
| 270 | |
| 271 | ##----------------------------------------------------------------------------## |
| 272 | # UpdateInFilePath - Update the path in the report file. |
| 273 | ##----------------------------------------------------------------------------## |
| 274 | |
| 275 | sub UpdateInFilePath { |
| 276 | my $fname = shift; |
| 277 | my $regex = shift; |
| 278 | my $newtext = shift; |
Ted Kremenek | 63c2017 | 2008-08-04 17:34:06 +0000 | [diff] [blame] | 279 | |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 280 | open (RIN, $fname) or die "cannot open $fname"; |
Ted Kremenek | 63c2017 | 2008-08-04 17:34:06 +0000 | [diff] [blame] | 281 | open (ROUT, ">", "$fname.tmp") or die "cannot open $fname.tmp"; |
| 282 | |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 283 | while (<RIN>) { |
| 284 | s/$regex/$newtext/; |
| 285 | print ROUT $_; |
| 286 | } |
Ted Kremenek | 63c2017 | 2008-08-04 17:34:06 +0000 | [diff] [blame] | 287 | |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 288 | close (ROUT); |
| 289 | close (RIN); |
Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 290 | system("mv", "$fname.tmp", $fname); |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | ##----------------------------------------------------------------------------## |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 294 | # ScanFile - Scan a report file for various identifying attributes. |
| 295 | ##----------------------------------------------------------------------------## |
| 296 | |
Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 297 | # Sometimes a source file is scanned more than once, and thus produces |
| 298 | # multiple error reports. We use a cache to solve this problem. |
| 299 | |
| 300 | my %AlreadyScanned; |
| 301 | |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 302 | sub ScanFile { |
| 303 | |
| 304 | my $Index = shift; |
| 305 | my $Dir = shift; |
| 306 | my $FName = shift; |
| 307 | |
Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 308 | # Compute a digest for the report file. Determine if we have already |
| 309 | # scanned a file that looks just like it. |
| 310 | |
| 311 | my $digest = ComputeDigest("$Dir/$FName"); |
| 312 | |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 313 | if (defined $AlreadyScanned{$digest}) { |
Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 314 | # Redundant file. Remove it. |
Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 315 | system ("rm", "-f", "$Dir/$FName"); |
Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 316 | return; |
| 317 | } |
| 318 | |
| 319 | $AlreadyScanned{$digest} = 1; |
| 320 | |
Ted Kremenek | 809709f | 2008-04-18 16:58:34 +0000 | [diff] [blame] | 321 | # 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] | 322 | system ("chmod", "644", "$Dir/$FName"); |
Ted Kremenek | 684bb09 | 2008-04-18 15:18:20 +0000 | [diff] [blame] | 323 | |
| 324 | # Scan the report file for tags. |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 325 | open(IN, "$Dir/$FName") or DieDiag("Cannot open '$Dir/$FName'\n"); |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 326 | |
| 327 | my $BugDesc = ""; |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 328 | my $BugFile = ""; |
| 329 | my $BugPathLength = 1; |
| 330 | my $BugLine = 0; |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 331 | |
| 332 | while (<IN>) { |
| 333 | |
| 334 | if (/<!-- BUGDESC (.*) -->$/) { |
| 335 | $BugDesc = $1; |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 336 | } |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 337 | elsif (/<!-- BUGFILE (.*) -->$/) { |
| 338 | $BugFile = $1; |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 339 | UpdatePrefix($BugFile); |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 340 | } |
| 341 | elsif (/<!-- BUGPATHLENGTH (.*) -->$/) { |
| 342 | $BugPathLength = $1; |
| 343 | } |
| 344 | elsif (/<!-- BUGLINE (.*) -->$/) { |
| 345 | $BugLine = $1; |
| 346 | } |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | close(IN); |
| 350 | |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 351 | push @$Index,[ $FName, $BugDesc, $BugFile, $BugLine, $BugPathLength ]; |
| 352 | } |
| 353 | |
| 354 | ##----------------------------------------------------------------------------## |
| 355 | # CopyJS - Copy JavaScript code to target directory. |
| 356 | ##----------------------------------------------------------------------------## |
| 357 | |
| 358 | sub CopyJS { |
| 359 | |
| 360 | my $Dir = shift; |
| 361 | |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 362 | DieDiag("Cannot find 'sorttable.js'.\n") |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 363 | if (! -r "$RealBin/sorttable.js"); |
| 364 | |
Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 365 | system ("cp", "$RealBin/sorttable.js", "$Dir"); |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 366 | |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 367 | DieDiag("Could not copy 'sorttable.js' to '$Dir'.\n") |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 368 | if (! -r "$Dir/sorttable.js"); |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | ##----------------------------------------------------------------------------## |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 372 | # Postprocess - Postprocess the results of an analysis scan. |
| 373 | ##----------------------------------------------------------------------------## |
| 374 | |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 375 | sub Postprocess { |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 376 | |
| 377 | my $Dir = shift; |
Ted Kremenek | 684bb09 | 2008-04-18 15:18:20 +0000 | [diff] [blame] | 378 | my $BaseDir = shift; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 379 | |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 380 | die "No directory specified." if (!defined $Dir); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 381 | |
| 382 | if (! -d $Dir) { |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 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 | } |
| 386 | |
| 387 | opendir(DIR, $Dir); |
| 388 | my @files = grep(/^report-.*\.html$/,readdir(DIR)); |
| 389 | closedir(DIR); |
| 390 | |
| 391 | if (scalar(@files) == 0) { |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 392 | Diag("Removing directory '$Dir' because it contains no reports.\n"); |
Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 393 | system ("rm", "-fR", $Dir); |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 394 | |
| 395 | # Remove the base directory if it contains no files (don't use '-R'). |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 396 | if (defined $BaseDir) { system ("rm", "-f", $BaseDir); } |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 397 | |
| 398 | Diag("No bugs found.\n"); |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 399 | return 0; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 400 | } |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 401 | |
| 402 | # Scan each report file and build an index. |
| 403 | |
| 404 | my @Index; |
| 405 | |
| 406 | foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); } |
| 407 | |
Ted Kremenek | 63c2017 | 2008-08-04 17:34:06 +0000 | [diff] [blame] | 408 | # Generate an index.html file. |
| 409 | my $FName = "$Dir/index.html"; |
| 410 | open(OUT, ">", $FName) or DieDiag("Cannot create file '$FName'\n"); |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 411 | |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 412 | # Print out the header. |
| 413 | |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 414 | print OUT <<ENDTEXT; |
| 415 | <html> |
| 416 | <head> |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 417 | <style type="text/css"> |
| 418 | body { color:#000000; background-color:#ffffff } |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 419 | body { font-family: Helvetica, sans-serif; font-size:9pt } |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 420 | h1 { font-size:12pt } |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 421 | table.sortable thead { |
| 422 | background-color:#eee; color:#666666; |
| 423 | font-weight: bold; cursor: default; |
Ted Kremenek | bba1cf5 | 2008-04-03 05:50:51 +0000 | [diff] [blame] | 424 | text-align:center; |
| 425 | border-top: 2px solid #000000; |
| 426 | border-bottom: 2px solid #000000; |
| 427 | font-weight: bold; font-family: Verdana |
| 428 | } |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 429 | table.sortable { border: 1px #000000 solid } |
| 430 | table.sortable { border-collapse: collapse; border-spacing: 0px } |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 431 | td { border-bottom: 1px #000000 dotted } |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 432 | td { padding:5px; padding-left:8px; padding-right:8px } |
Ted Kremenek | d8c6d0c | 2008-04-07 23:50:07 +0000 | [diff] [blame] | 433 | td { text-align:left; font-size:9pt } |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 434 | td.View { padding-left: 10px } |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 435 | </style> |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 436 | <script src="sorttable.js"></script> |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 437 | <script language='javascript' type="text/javascript"> |
| 438 | function SetDisplay(RowClass, DisplayVal) |
| 439 | { |
| 440 | var Rows = document.getElementsByTagName("tr"); |
| 441 | for ( var i = 0 ; i < Rows.length; ++i ) { |
| 442 | if (Rows[i].className == RowClass) { |
| 443 | Rows[i].style.display = DisplayVal; |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | function ToggleDisplay(CheckButton, ClassName) { |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 449 | if (CheckButton.checked) { |
| 450 | SetDisplay(ClassName, ""); |
| 451 | } |
| 452 | else { |
| 453 | SetDisplay(ClassName, "none"); |
| 454 | } |
| 455 | } |
| 456 | </script> |
| 457 | </head> |
| 458 | <body> |
| 459 | ENDTEXT |
| 460 | |
| 461 | # Print out the summary table. |
| 462 | |
| 463 | my %Totals; |
| 464 | |
| 465 | for my $row ( @Index ) { |
| 466 | |
Ted Kremenek | 432af59 | 2008-05-06 18:11:36 +0000 | [diff] [blame] | 467 | #my $bug_type = lc($row->[1]); |
| 468 | my $bug_type = ($row->[1]); |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 469 | |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 470 | if (!defined $Totals{$bug_type}) { |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 471 | $Totals{$bug_type} = 1; |
| 472 | } |
| 473 | else { |
| 474 | $Totals{$bug_type}++; |
| 475 | } |
| 476 | } |
Ted Kremenek | f4cdf41 | 2008-05-23 18:17:05 +0000 | [diff] [blame] | 477 | |
| 478 | print OUT "<h3>Summary</h3>"; |
| 479 | |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 480 | if (defined $BuildName) { |
Ted Kremenek | f4cdf41 | 2008-05-23 18:17:05 +0000 | [diff] [blame] | 481 | print OUT "\n<p>Results in this analysis run are based on analyzer build <b>$BuildName</b>.</p>\n" |
| 482 | } |
| 483 | |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 484 | print OUT <<ENDTEXT; |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 485 | <table class="sortable"> |
| 486 | <tr> |
| 487 | <td>Bug Type</td> |
| 488 | <td>Quantity</td> |
Ted Kremenek | 2645c77 | 2008-07-07 16:58:44 +0000 | [diff] [blame] | 489 | <td class="sorttable_nosort">Display?</td> |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 490 | </tr> |
| 491 | ENDTEXT |
| 492 | |
| 493 | for my $key ( sort { $a cmp $b } keys %Totals ) { |
Ted Kremenek | bdf66c7 | 2008-05-06 23:51:45 +0000 | [diff] [blame] | 494 | my $x = lc($key); |
Ted Kremenek | 3cea9ee | 2008-07-30 17:58:08 +0000 | [diff] [blame] | 495 | $x =~ s/[ ,'"]+/_/g; |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 496 | print OUT "<tr><td>$key</td><td>$Totals{$key}</td><td><input type=\"checkbox\" onClick=\"ToggleDisplay(this,'bt_$x');\" checked/></td></tr>\n"; |
| 497 | } |
| 498 | |
| 499 | # Print out the table of errors. |
| 500 | |
| 501 | print OUT <<ENDTEXT; |
| 502 | </table> |
| 503 | <h3>Reports</h3> |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 504 | <table class="sortable"> |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 505 | <tr> |
Ted Kremenek | 88a96d6 | 2008-07-07 17:23:32 +0000 | [diff] [blame] | 506 | <td class="sorttable_sorted">Bug Type<span id="sorttable_sortfwdind"> ▾</span> |
Ted Kremenek | bba1cf5 | 2008-04-03 05:50:51 +0000 | [diff] [blame] | 507 | <td>File</td> |
| 508 | <td>Line</td> |
| 509 | <td>Path Length</td> |
Ted Kremenek | 2645c77 | 2008-07-07 16:58:44 +0000 | [diff] [blame] | 510 | <td class="sorttable_nosort"></td> |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 511 | </tr> |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 512 | ENDTEXT |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 513 | |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 514 | my $prefix = GetPrefix(); |
| 515 | my $regex; |
| 516 | my $InFileRegex; |
| 517 | my $InFilePrefix = "File:</td><td>"; |
| 518 | |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 519 | if (defined $prefix) { |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 520 | $regex = qr/^\Q$prefix\E/is; |
| 521 | $InFileRegex = qr/\Q$InFilePrefix$prefix\E/is; |
| 522 | } |
| 523 | |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 524 | for my $row ( sort { $a->[1] cmp $b->[1] } @Index ) { |
| 525 | |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 526 | my $x = lc($row->[1]); |
Ted Kremenek | 3cea9ee | 2008-07-30 17:58:08 +0000 | [diff] [blame] | 527 | $x =~ s/[ ,'"]+/_/g; |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 528 | |
| 529 | print OUT "<tr class=\"bt_$x\">\n"; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 530 | |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 531 | my $ReportFile = $row->[0]; |
| 532 | |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 533 | print OUT " <td class=\"DESC\">"; |
Ted Kremenek | 432af59 | 2008-05-06 18:11:36 +0000 | [diff] [blame] | 534 | #print OUT lc($row->[1]); |
| 535 | print OUT $row->[1]; |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 536 | print OUT "</td>\n"; |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 537 | |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 538 | # Update the file prefix. |
| 539 | |
| 540 | my $fname = $row->[2]; |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 541 | if (defined $regex) { |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 542 | $fname =~ s/$regex//; |
| 543 | UpdateInFilePath("$Dir/$ReportFile", $InFileRegex, $InFilePrefix) |
| 544 | } |
Ted Kremenek | 3e56e0b | 2008-05-02 23:40:49 +0000 | [diff] [blame] | 545 | |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 546 | print OUT "<td>$fname</td>\n"; |
| 547 | |
| 548 | # Print the rest of the columns. |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 549 | for my $j ( 3 .. $#{$row} ) { |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 550 | print OUT "<td>$row->[$j]</td>\n" |
| 551 | } |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 552 | |
| 553 | # Emit the "View" link. |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 554 | print OUT " <td class=\"View\"><a href=\"$ReportFile#EndPath\">View</a></td>\n"; |
Ted Kremenek | 3cea9ee | 2008-07-30 17:58:08 +0000 | [diff] [blame] | 555 | |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 556 | # End the row. |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 557 | print OUT "</tr>\n"; |
| 558 | } |
| 559 | |
| 560 | print OUT "</table>\n</body></html>\n"; |
| 561 | close(OUT); |
Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 562 | |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 563 | CopyJS($Dir); |
Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 564 | |
| 565 | # Make sure $Dir and $BaseDir are world readable/executable. |
| 566 | system("chmod", "755", $Dir); |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 567 | if (defined $BaseDir) { system("chmod", "755", $BaseDir); } |
Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 568 | |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 569 | my $Num = scalar(@Index); |
Ted Kremenek | 150c212 | 2008-07-11 19:15:05 +0000 | [diff] [blame] | 570 | Diag("$Num bugs found.\n"); |
| 571 | if ($Num > 0 && -r "$Dir/index.html") { |
| 572 | Diag("Open '$Dir/index.html' to examine bug reports.\n"); |
| 573 | } |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 574 | |
| 575 | return $Num; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | ##----------------------------------------------------------------------------## |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 579 | # RunBuildCommand - Run the build command. |
| 580 | ##----------------------------------------------------------------------------## |
| 581 | |
Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 582 | sub AddIfNotPresent { |
| 583 | my $Args = shift; |
| 584 | my $Arg = shift; |
| 585 | my $found = 0; |
| 586 | |
| 587 | foreach my $k (@$Args) { |
| 588 | if ($k eq $Arg) { |
| 589 | $found = 1; |
| 590 | last; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | if ($found == 0) { |
| 595 | push @$Args, $Arg; |
| 596 | } |
| 597 | } |
| 598 | |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 599 | sub RunBuildCommand { |
| 600 | |
| 601 | my $Args = shift; |
Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame] | 602 | my $IgnoreErrors = shift; |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 603 | my $Cmd = $Args->[0]; |
Ted Kremenek | 6195c37 | 2008-06-02 21:52:47 +0000 | [diff] [blame] | 604 | my $CCAnalyzer = shift; |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 605 | |
Ted Kremenek | 3301cb1 | 2008-06-30 18:18:16 +0000 | [diff] [blame] | 606 | # Get only the part of the command after the last '/'. |
| 607 | if ($Cmd =~ /\/([^\/]+)$/) { |
| 608 | $Cmd = $1; |
| 609 | } |
| 610 | |
Ted Kremenek | 63c2017 | 2008-08-04 17:34:06 +0000 | [diff] [blame] | 611 | if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc" |
| 612 | or $Cmd eq "ccc-analyzer") { |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 613 | shift @$Args; |
Ted Kremenek | 6195c37 | 2008-06-02 21:52:47 +0000 | [diff] [blame] | 614 | unshift @$Args, $CCAnalyzer; |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 615 | } |
Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame] | 616 | elsif ($IgnoreErrors) { |
| 617 | if ($Cmd eq "make" or $Cmd eq "gmake") { |
Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 618 | AddIfNotPresent($Args,"-k"); |
Ted Kremenek | 8912b54 | 2008-05-13 21:28:02 +0000 | [diff] [blame] | 619 | AddIfNotPresent($Args,"-i"); |
Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame] | 620 | } |
| 621 | elsif ($Cmd eq "xcodebuild") { |
Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 622 | AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES"); |
Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame] | 623 | } |
Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 626 | if ($Cmd eq "xcodebuild") { |
Ted Kremenek | cfd4c7b | 2008-05-23 22:18:16 +0000 | [diff] [blame] | 627 | # Disable distributed builds for xcodebuild. |
Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 628 | AddIfNotPresent($Args,"-nodistribute"); |
Ted Kremenek | cfd4c7b | 2008-05-23 22:18:16 +0000 | [diff] [blame] | 629 | |
| 630 | # Disable PCH files until clang supports them. |
| 631 | AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO"); |
Ted Kremenek | 915e972 | 2008-05-27 23:18:07 +0000 | [diff] [blame] | 632 | |
| 633 | # When 'CC' is set, xcodebuild uses it to do all linking, even if we are |
| 634 | # linking C++ object files. Set 'LDPLUSPLUS' so that xcodebuild uses 'g++' |
| 635 | # when linking such files. |
| 636 | my $LDPLUSPLUS = `which g++`; |
| 637 | $LDPLUSPLUS =~ s/\015?\012//; # strip newlines |
| 638 | $ENV{'LDPLUSPLUS'} = $LDPLUSPLUS; |
Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 639 | } |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 640 | |
Ted Kremenek | 5656a98 | 2008-07-15 17:09:28 +0000 | [diff] [blame] | 641 | return system(@$Args); |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 644 | ##----------------------------------------------------------------------------## |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 645 | # DisplayHelp - Utility function to display all help options. |
| 646 | ##----------------------------------------------------------------------------## |
| 647 | |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 648 | sub DisplayHelp { |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 649 | |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 650 | print <<ENDTEXT; |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 651 | USAGE: $Prog [options] <build command> [build options] |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 652 | |
Ted Kremenek | f4cdf41 | 2008-05-23 18:17:05 +0000 | [diff] [blame] | 653 | ENDTEXT |
| 654 | |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 655 | if (defined $BuildName) { |
Ted Kremenek | f4cdf41 | 2008-05-23 18:17:05 +0000 | [diff] [blame] | 656 | print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n"; |
| 657 | } |
| 658 | |
| 659 | print <<ENDTEXT; |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 660 | OPTIONS: |
| 661 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 662 | -o - Target directory for HTML report files. Subdirectories |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 663 | will be created as needed to represent separate "runs" of |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 664 | the analyzer. If this option is not specified, a directory |
| 665 | is created in /tmp to store the reports. |
Ted Kremenek | 1262fc4 | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 666 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 667 | -h - Display this message. |
| 668 | --help |
Ted Kremenek | 1262fc4 | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 669 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 670 | -k - Add a "keep on going" option to the specified build command. |
| 671 | --keep-going This option currently supports make and xcodebuild. |
Ted Kremenek | f02e8db | 2008-04-02 16:41:25 +0000 | [diff] [blame] | 672 | This is a convenience option; one can specify this |
| 673 | behavior directly using build options. |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 674 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 675 | --status-bugs - By default, the exit status of $Prog is the same as the |
| 676 | executed build command. Specifying this option causes the |
| 677 | exit status of $Prog to be 1 if it found potential bugs |
| 678 | and 0 otherwise. |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 679 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 680 | -v - Verbose output from $Prog and the analyzer. |
| 681 | A second and third "-v" increases verbosity. |
| 682 | |
| 683 | -V - View analysis results in a web browser when the build |
| 684 | --view completes. |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 685 | |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 686 | ENDTEXT |
| 687 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 688 | print " Available Source Code Analyses (multiple analyses may be specified):\n\n"; |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 689 | |
| 690 | foreach my $Analysis (sort keys %AvailableAnalyses) { |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 691 | if (defined $AnalysesDefaultEnabled{$Analysis}) { |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 692 | print " (+)"; |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 693 | } |
| 694 | else { |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 695 | print " "; |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | print " $Analysis $AvailableAnalyses{$Analysis}\n"; |
| 699 | } |
| 700 | |
| 701 | print <<ENDTEXT |
| 702 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 703 | NOTE: "(+)" indicates that an analysis is enabled by default unless one |
| 704 | or more analysis options are specified |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 705 | |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 706 | BUILD OPTIONS |
| 707 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 708 | You can specify any build option acceptable to the build command. |
Ted Kremenek | 39eefde | 2008-04-02 16:47:27 +0000 | [diff] [blame] | 709 | |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 710 | EXAMPLE |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 711 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 712 | $Prog -o /tmp/myhtmldir make -j4 |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 713 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 714 | The above example causes analysis reports to be deposited into |
| 715 | a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option. |
| 716 | A different subdirectory is created each time $Prog analyzes a project. |
| 717 | The analyzer should support most parallel builds, but not distributed builds. |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 718 | |
| 719 | ENDTEXT |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | ##----------------------------------------------------------------------------## |
| 723 | # Process command-line arguments. |
| 724 | ##----------------------------------------------------------------------------## |
| 725 | |
| 726 | my $HtmlDir; # Parent directory to store HTML files. |
| 727 | my $IgnoreErrors = 0; # Ignore build errors. |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 728 | my $ViewResults = 0; # View results when the build terminates. |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 729 | my $ExitStatusFoundBugs = 0; # Exit status reflects whether bugs were found |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 730 | my @AnalysesToRun; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 731 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 732 | |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 733 | if (!@ARGV) { |
| 734 | DisplayHelp(); |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 735 | exit 1; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | while (@ARGV) { |
| 739 | |
| 740 | # Scan for options we recognize. |
| 741 | |
| 742 | my $arg = $ARGV[0]; |
| 743 | |
Sam Bishop | 2f2418e | 2008-04-03 14:29:47 +0000 | [diff] [blame] | 744 | if ($arg eq "-h" or $arg eq "--help") { |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 745 | DisplayHelp(); |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 746 | exit 0; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 749 | if (defined $AvailableAnalyses{$arg}) { |
Ted Kremenek | 1262fc4 | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 750 | shift @ARGV; |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 751 | push @AnalysesToRun, $arg; |
Ted Kremenek | 1262fc4 | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 752 | next; |
| 753 | } |
| 754 | |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 755 | if ($arg eq "-o") { |
| 756 | shift @ARGV; |
| 757 | |
| 758 | if (!@ARGV) { |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 759 | DieDiag("'-o' option requires a target directory name.\n"); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | $HtmlDir = shift @ARGV; |
| 763 | next; |
| 764 | } |
| 765 | |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 766 | if ($arg eq "-k" or $arg eq "--keep-going") { |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 767 | shift @ARGV; |
| 768 | $IgnoreErrors = 1; |
| 769 | next; |
| 770 | } |
| 771 | |
| 772 | if ($arg eq "-v") { |
| 773 | shift @ARGV; |
| 774 | $Verbose++; |
| 775 | next; |
| 776 | } |
| 777 | |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 778 | if ($arg eq "-V" or $arg eq "--view") { |
| 779 | shift @ARGV; |
| 780 | $ViewResults = 1; |
| 781 | next; |
| 782 | } |
| 783 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 784 | if ($arg eq "--status-bugs") { |
| 785 | shift @ARGV; |
| 786 | $ExitStatusFoundBugs = 1; |
| 787 | next; |
| 788 | } |
| 789 | |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 790 | DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/); |
Ted Kremenek | 0062ad4 | 2008-04-02 16:35:01 +0000 | [diff] [blame] | 791 | |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 792 | last; |
| 793 | } |
| 794 | |
| 795 | if (!@ARGV) { |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 796 | Diag("No build command specified.\n\n"); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 797 | DisplayHelp(); |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 798 | exit 1; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | # Determine the output directory for the HTML reports. |
Ted Kremenek | 684bb09 | 2008-04-18 15:18:20 +0000 | [diff] [blame] | 802 | my $BaseDir = $HtmlDir; |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 803 | $HtmlDir = GetHTMLRunDir($HtmlDir); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 804 | |
| 805 | # Set the appropriate environment variables. |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 806 | SetHtmlEnv(\@ARGV, $HtmlDir); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 807 | |
Ted Kremenek | 0b6c153 | 2008-04-08 20:22:12 +0000 | [diff] [blame] | 808 | my $Cmd = "$RealBin/ccc-analyzer"; |
| 809 | |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 810 | DieDiag("Executable 'ccc-analyzer' does not exist at '$Cmd'\n") |
Ted Kremenek | 0b6c153 | 2008-04-08 20:22:12 +0000 | [diff] [blame] | 811 | if (! -x $Cmd); |
Ted Kremenek | f22eacb | 2008-04-18 22:00:56 +0000 | [diff] [blame] | 812 | |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 813 | if (! -x $ClangSB) { |
| 814 | Diag("'clang' executable not found in '$RealBin'.\n"); |
| 815 | Diag("Using 'clang' from path.\n"); |
Ted Kremenek | f22eacb | 2008-04-18 22:00:56 +0000 | [diff] [blame] | 816 | } |
Ted Kremenek | 0b6c153 | 2008-04-08 20:22:12 +0000 | [diff] [blame] | 817 | |
Ted Kremenek | 4f4b17d | 2008-04-03 20:08:18 +0000 | [diff] [blame] | 818 | $ENV{'CC'} = $Cmd; |
Ted Kremenek | f22eacb | 2008-04-18 22:00:56 +0000 | [diff] [blame] | 819 | $ENV{'CLANG'} = $Clang; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 820 | |
| 821 | if ($Verbose >= 2) { |
| 822 | $ENV{'CCC_ANALYZER_VERBOSE'} = 1; |
| 823 | } |
| 824 | |
Ted Kremenek | a9525c9 | 2008-05-12 22:07:14 +0000 | [diff] [blame] | 825 | if ($Verbose >= 3) { |
| 826 | $ENV{'CCC_ANALYZER_LOG'} = 1; |
| 827 | } |
| 828 | |
Ted Kremenek | 9012599 | 2008-07-15 23:41:32 +0000 | [diff] [blame] | 829 | if (scalar(@AnalysesToRun) == 0) { |
| 830 | foreach my $key (keys %AnalysesDefaultEnabled) { |
| 831 | push @AnalysesToRun,$key; |
| 832 | } |
Ted Kremenek | 0100678 | 2008-07-02 23:16:10 +0000 | [diff] [blame] | 833 | } |
Ted Kremenek | 1262fc4 | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 834 | |
Ted Kremenek | 9012599 | 2008-07-15 23:41:32 +0000 | [diff] [blame] | 835 | $ENV{'CCC_ANALYZER_ANALYSIS'} = join ' ',@AnalysesToRun; |
| 836 | |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 837 | # Run the build. |
Ted Kremenek | 5656a98 | 2008-07-15 17:09:28 +0000 | [diff] [blame] | 838 | my $ExitStatus = RunBuildCommand(\@ARGV, $IgnoreErrors, $Cmd); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 839 | |
| 840 | # Postprocess the HTML directory. |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 841 | my $NumBugs = Postprocess($HtmlDir, $BaseDir); |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 842 | |
| 843 | if ($ViewResults and -r "$HtmlDir/index.html") { |
| 844 | # Only works on Mac OS X (for now). |
| 845 | print "Viewing analysis results: '$HtmlDir/index.html'\n"; |
Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 846 | system("open", "$HtmlDir/index.html"); |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 847 | } |
Ted Kremenek | 5656a98 | 2008-07-15 17:09:28 +0000 | [diff] [blame] | 848 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 849 | if ($ExitStatusFoundBugs) { |
| 850 | exit 1 if ($NumBugs > 0); |
| 851 | exit 0; |
| 852 | } |
| 853 | |
Ted Kremenek | 5656a98 | 2008-07-15 17:09:28 +0000 | [diff] [blame] | 854 | exit $ExitStatus; |
| 855 | |