| 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; | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 17 | use FindBin qw($RealBin); | 
| Ted Kremenek | a6e2481 | 2008-04-19 18:05:48 +0000 | [diff] [blame] | 18 | use Digest::MD5; | 
| Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 19 | use File::Basename; | 
| Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 20 | use Term::ANSIColor; | 
 | 21 | use Term::ANSIColor qw(:constants); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 22 |  | 
 | 23 | my $Verbose = 0;       # Verbose output from this script. | 
 | 24 | my $Prog = "scan-build"; | 
| Ted Kremenek | f4cdf41 | 2008-05-23 18:17:05 +0000 | [diff] [blame] | 25 | my $BuildName; | 
 | 26 | my $BuildDate; | 
| Ted Kremenek | 95aa105 | 2008-09-04 17:52:41 +0000 | [diff] [blame] | 27 | my $CXX;  # Leave undefined initially. | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 28 |  | 
| Ted Kremenek | 0e68938 | 2008-09-11 18:17:51 +0000 | [diff] [blame] | 29 | my $TERM = $ENV{'TERM'}; | 
 | 30 | my $UseColor = (defined $TERM and $TERM eq 'xterm-color' and -t STDOUT | 
 | 31 |                 and defined $ENV{'SCAN_BUILD_COLOR'}); | 
| Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 32 |  | 
| Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 33 | ##----------------------------------------------------------------------------## | 
 | 34 | # Diagnostics | 
 | 35 | ##----------------------------------------------------------------------------## | 
 | 36 |  | 
| Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 37 | sub Diag { | 
 | 38 |   if ($UseColor) { | 
 | 39 |     print BOLD, MAGENTA "$Prog: @_"; | 
 | 40 |     print RESET; | 
 | 41 |   } | 
 | 42 |   else { | 
 | 43 |     print "$Prog: @_"; | 
 | 44 |   }   | 
 | 45 | } | 
 | 46 |  | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 47 | sub DiagCrashes { | 
 | 48 |   my $Dir = shift; | 
 | 49 |   Diag ("The analyzer crashed on some source files.\n"); | 
| Ted Kremenek | 386c693 | 2008-09-03 17:59:35 +0000 | [diff] [blame] | 50 |   Diag ("Preprocessed versions of crashed files were deposited in '$Dir/crashes'.\n"); | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 51 |   Diag ("Please consider submitting a bug report using these files:\n"); | 
 | 52 |   Diag ("  http://clang.llvm.org/StaticAnalysisUsage.html#filingbugs\n") | 
 | 53 | } | 
 | 54 |  | 
| Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 55 | sub DieDiag { | 
 | 56 |   if ($UseColor) { | 
 | 57 |     print BOLD, RED "$Prog: "; | 
 | 58 |     print RESET, RED @_; | 
 | 59 |     print RESET; | 
 | 60 |   } | 
 | 61 |   else { | 
 | 62 |     print "$Prog: ", @_; | 
 | 63 |   } | 
 | 64 |   exit(0); | 
 | 65 | } | 
 | 66 |  | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 67 | ##----------------------------------------------------------------------------## | 
| Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 68 | # Some initial preprocessing of Clang options. | 
 | 69 | ##----------------------------------------------------------------------------## | 
 | 70 |  | 
 | 71 | my $ClangSB = "$RealBin/clang"; | 
 | 72 | my $Clang = $ClangSB; | 
 | 73 |  | 
 | 74 | if (! -x $ClangSB) { | 
 | 75 |   $Clang = "clang"; | 
 | 76 | } | 
 | 77 |  | 
 | 78 | my %AvailableAnalyses; | 
 | 79 |  | 
 | 80 | # Query clang for analysis options. | 
| Ted Kremenek | 63c2017 | 2008-08-04 17:34:06 +0000 | [diff] [blame] | 81 | open(PIPE, "-|", $Clang, "--help") or | 
| Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 82 |   DieDiag("Cannot execute '$Clang'"); | 
| Ted Kremenek | 63c2017 | 2008-08-04 17:34:06 +0000 | [diff] [blame] | 83 |  | 
| Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 84 | my $FoundAnalysis = 0; | 
 | 85 |  | 
 | 86 | while(<PIPE>) { | 
 | 87 |   if ($FoundAnalysis == 0) { | 
 | 88 |     if (/Available Source Code Analyses/) { | 
 | 89 |       $FoundAnalysis = 1; | 
 | 90 |     } | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 91 |  | 
| Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 92 |     next; | 
 | 93 |   } | 
 | 94 |      | 
 | 95 |   if (/^\s\s\s\s([^\s]+)\s(.+)$/) { | 
 | 96 |     next if ($1 =~ /-dump/ or $1 =~ /-view/  | 
 | 97 |              or $1 =~ /-checker-simple/ or $1 =~ /-warn-uninit/); | 
 | 98 |               | 
 | 99 |     $AvailableAnalyses{$1} = $2; | 
 | 100 |     next; | 
 | 101 |   } | 
 | 102 |    | 
 | 103 |   last; | 
 | 104 | } | 
 | 105 |  | 
 | 106 | close (PIPE); | 
 | 107 |  | 
 | 108 | my %AnalysesDefaultEnabled = ( | 
 | 109 |   '-warn-dead-stores' => 1, | 
 | 110 |   '-checker-cfref' => 1, | 
| Ted Kremenek | 9012599 | 2008-07-15 23:41:32 +0000 | [diff] [blame] | 111 |   '-warn-objc-methodsigs' => 1, | 
| Ted Kremenek | bde3a05 | 2008-07-25 20:35:01 +0000 | [diff] [blame] | 112 |   '-warn-objc-missing-dealloc' => 1, | 
| Ted Kremenek | 5d44349 | 2008-09-18 06:34:16 +0000 | [diff] [blame] | 113 |   '-warn-objc-unused-ivars' => 1, | 
| Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 114 | ); | 
 | 115 |  | 
 | 116 | ##----------------------------------------------------------------------------## | 
| Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 117 | # GetHTMLRunDir - Construct an HTML directory name for the current sub-run. | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 118 | ##----------------------------------------------------------------------------## | 
 | 119 |  | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 120 | sub GetHTMLRunDir {   | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 121 |  | 
| Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 122 |   die "Not enough arguments." if (@_ == 0);   | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 123 |   my $Dir = shift @_; | 
| Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 124 |      | 
 | 125 |   my $TmpMode = 0; | 
 | 126 |   if (!defined $Dir) { | 
 | 127 |     $Dir = "/tmp"; | 
 | 128 |     $TmpMode = 1; | 
 | 129 |   } | 
 | 130 |  | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 131 |   # Get current date and time. | 
 | 132 |    | 
 | 133 |   my @CurrentTime = localtime(); | 
 | 134 |    | 
 | 135 |   my $year  = $CurrentTime[5] + 1900; | 
 | 136 |   my $day   = $CurrentTime[3]; | 
 | 137 |   my $month = $CurrentTime[4] + 1; | 
 | 138 |    | 
| Ted Kremenek | 9d7405f | 2008-05-14 17:23:56 +0000 | [diff] [blame] | 139 |   my $DateString = sprintf("%d-%02d-%02d", $year, $month, $day); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 140 |    | 
 | 141 |   # Determine the run number. | 
 | 142 |    | 
 | 143 |   my $RunNumber; | 
 | 144 |    | 
 | 145 |   if (-d $Dir) { | 
 | 146 |      | 
 | 147 |     if (! -r $Dir) { | 
| Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 148 |       DieDiag("directory '$Dir' exists but is not readable.\n"); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 149 |     } | 
 | 150 |      | 
 | 151 |     # Iterate over all files in the specified directory. | 
 | 152 |      | 
 | 153 |     my $max = 0; | 
 | 154 |      | 
 | 155 |     opendir(DIR, $Dir); | 
| Ted Kremenek | 29da6c5 | 2008-08-07 17:57:34 +0000 | [diff] [blame] | 156 |     my @FILES = grep { -d "$Dir/$_" } readdir(DIR); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 157 |     closedir(DIR); | 
 | 158 |      | 
 | 159 |     foreach my $f (@FILES) { | 
 | 160 |  | 
| Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 161 |       # Strip the prefix '$Prog-' if we are dumping files to /tmp. | 
 | 162 |       if ($TmpMode) { | 
 | 163 |         next if (!($f =~ /^$Prog-(.+)/)); | 
 | 164 |         $f = $1; | 
 | 165 |       } | 
 | 166 |  | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 167 |        | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 168 |       my @x = split/-/, $f; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 169 |       next if (scalar(@x) != 4); | 
 | 170 |       next if ($x[0] != $year); | 
 | 171 |       next if ($x[1] != $month); | 
 | 172 |       next if ($x[2] != $day); | 
 | 173 |        | 
 | 174 |       if ($x[3] > $max) { | 
 | 175 |         $max = $x[3]; | 
 | 176 |       }       | 
 | 177 |     } | 
 | 178 |      | 
 | 179 |     $RunNumber = $max + 1; | 
 | 180 |   } | 
 | 181 |   else { | 
 | 182 |      | 
 | 183 |     if (-x $Dir) { | 
| Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 184 |       DieDiag("'$Dir' exists but is not a directory.\n"); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 185 |     } | 
| Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 186 |  | 
 | 187 |     if ($TmpMode) { | 
 | 188 |       DieDiag("The directory '/tmp' does not exist or cannot be accessed."); | 
 | 189 |     } | 
 | 190 |  | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 191 |     # $Dir does not exist.  It will be automatically created by the  | 
 | 192 |     # clang driver.  Set the run number to 1.   | 
| Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 193 |  | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 194 |     $RunNumber = 1; | 
 | 195 |   } | 
 | 196 |    | 
| Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 197 |   die "RunNumber must be defined!" if (!defined $RunNumber); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 198 |    | 
 | 199 |   # Append the run number. | 
| Ted Kremenek | fc0898a | 2008-09-04 23:56:36 +0000 | [diff] [blame] | 200 |   my $NewDir; | 
| Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 201 |   if ($TmpMode) { | 
| Ted Kremenek | fc0898a | 2008-09-04 23:56:36 +0000 | [diff] [blame] | 202 |     $NewDir = "$Dir/$Prog-$DateString-$RunNumber"; | 
| Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 203 |   } | 
 | 204 |   else { | 
| Ted Kremenek | fc0898a | 2008-09-04 23:56:36 +0000 | [diff] [blame] | 205 |     $NewDir = "$Dir/$DateString-$RunNumber"; | 
| Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 206 |   } | 
| Ted Kremenek | fc0898a | 2008-09-04 23:56:36 +0000 | [diff] [blame] | 207 |   system 'mkdir','-p',$NewDir; | 
 | 208 |   return $NewDir; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 209 | } | 
 | 210 |  | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 211 | sub SetHtmlEnv { | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 212 |    | 
 | 213 |   die "Wrong number of arguments." if (scalar(@_) != 2); | 
 | 214 |    | 
 | 215 |   my $Args = shift; | 
 | 216 |   my $Dir = shift; | 
 | 217 |    | 
 | 218 |   die "No build command." if (scalar(@$Args) == 0); | 
 | 219 |    | 
 | 220 |   my $Cmd = $$Args[0]; | 
 | 221 |    | 
 | 222 |   if ($Cmd =~ /configure/) { | 
 | 223 |     return; | 
 | 224 |   } | 
 | 225 |    | 
 | 226 |   if ($Verbose) { | 
| Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 227 |     Diag("Emitting reports for this run to '$Dir'.\n"); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 228 |   } | 
 | 229 |    | 
 | 230 |   $ENV{'CCC_ANALYZER_HTML'} = $Dir; | 
 | 231 | } | 
 | 232 |  | 
 | 233 | ##----------------------------------------------------------------------------## | 
| Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 234 | # ComputeDigest - Compute a digest of the specified file. | 
 | 235 | ##----------------------------------------------------------------------------## | 
 | 236 |  | 
 | 237 | sub ComputeDigest { | 
 | 238 |   my $FName = shift; | 
| Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 239 |   DieDiag("Cannot read $FName to compute Digest.\n") if (! -r $FName);   | 
| Ted Kremenek | a6e2481 | 2008-04-19 18:05:48 +0000 | [diff] [blame] | 240 |    | 
 | 241 |   # 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] | 242 |   # just looking for duplicate files that come from a non-malicious source. | 
 | 243 |   # 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] | 244 |   # come bundled on most systems.   | 
| Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 245 |   open(FILE, $FName) or DieDiag("Cannot open $FName when computing Digest.\n"); | 
| Ted Kremenek | a6e2481 | 2008-04-19 18:05:48 +0000 | [diff] [blame] | 246 |   binmode FILE; | 
 | 247 |   my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest; | 
 | 248 |   close(FILE); | 
 | 249 |    | 
| Ted Kremenek | 63c2017 | 2008-08-04 17:34:06 +0000 | [diff] [blame] | 250 |   # Return the digest.   | 
| Ted Kremenek | a6e2481 | 2008-04-19 18:05:48 +0000 | [diff] [blame] | 251 |   return $Result; | 
| Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 252 | } | 
 | 253 |  | 
 | 254 | ##----------------------------------------------------------------------------## | 
| Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 255 | #  UpdatePrefix - Compute the common prefix of files. | 
 | 256 | ##----------------------------------------------------------------------------## | 
 | 257 |  | 
 | 258 | my $Prefix; | 
 | 259 |  | 
 | 260 | sub UpdatePrefix { | 
| Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 261 |   my $x = shift; | 
 | 262 |   my $y = basename($x); | 
 | 263 |   $x =~ s/\Q$y\E$//; | 
 | 264 |    | 
 | 265 |   # Ignore /usr, /Library, /System, /Developer | 
| Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 266 |   return if ( $x =~ /^\/usr/ or $x =~ /^\/Library/ | 
 | 267 |               or $x =~ /^\/System/ or $x =~ /^\/Developer/); | 
 | 268 |  | 
| Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 269 |   if (!defined $Prefix) { | 
 | 270 |     $Prefix = $x; | 
 | 271 |     return; | 
 | 272 |   } | 
 | 273 |    | 
| Ted Kremenek | 20b2bae | 2008-09-11 21:15:10 +0000 | [diff] [blame] | 274 |   chop $Prefix while (!($x =~ /^\Q$Prefix/)); | 
| Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 275 | } | 
 | 276 |  | 
 | 277 | sub GetPrefix { | 
 | 278 |   return $Prefix; | 
 | 279 | } | 
 | 280 |  | 
 | 281 | ##----------------------------------------------------------------------------## | 
 | 282 | #  UpdateInFilePath - Update the path in the report file. | 
 | 283 | ##----------------------------------------------------------------------------## | 
 | 284 |  | 
 | 285 | sub UpdateInFilePath { | 
 | 286 |   my $fname = shift; | 
 | 287 |   my $regex = shift; | 
 | 288 |   my $newtext = shift; | 
| Ted Kremenek | 63c2017 | 2008-08-04 17:34:06 +0000 | [diff] [blame] | 289 |  | 
| Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 290 |   open (RIN, $fname) or die "cannot open $fname"; | 
| Ted Kremenek | 63c2017 | 2008-08-04 17:34:06 +0000 | [diff] [blame] | 291 |   open (ROUT, ">", "$fname.tmp") or die "cannot open $fname.tmp"; | 
 | 292 |  | 
| Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 293 |   while (<RIN>) { | 
 | 294 |     s/$regex/$newtext/; | 
 | 295 |     print ROUT $_; | 
 | 296 |   } | 
| Ted Kremenek | 63c2017 | 2008-08-04 17:34:06 +0000 | [diff] [blame] | 297 |  | 
| Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 298 |   close (ROUT); | 
 | 299 |   close (RIN); | 
| Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 300 |   system("mv", "$fname.tmp", $fname); | 
| Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 301 | } | 
 | 302 |  | 
 | 303 | ##----------------------------------------------------------------------------## | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 304 | # ScanFile - Scan a report file for various identifying attributes. | 
 | 305 | ##----------------------------------------------------------------------------## | 
 | 306 |  | 
| Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 307 | # Sometimes a source file is scanned more than once, and thus produces | 
 | 308 | # multiple error reports.  We use a cache to solve this problem. | 
 | 309 |  | 
 | 310 | my %AlreadyScanned; | 
 | 311 |  | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 312 | sub ScanFile { | 
 | 313 |    | 
 | 314 |   my $Index = shift; | 
 | 315 |   my $Dir = shift; | 
 | 316 |   my $FName = shift; | 
 | 317 |    | 
| Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 318 |   # Compute a digest for the report file.  Determine if we have already | 
 | 319 |   # scanned a file that looks just like it. | 
 | 320 |    | 
 | 321 |   my $digest = ComputeDigest("$Dir/$FName"); | 
 | 322 |  | 
| Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 323 |   if (defined $AlreadyScanned{$digest}) { | 
| Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 324 |     # Redundant file.  Remove it. | 
| Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 325 |     system ("rm", "-f", "$Dir/$FName"); | 
| Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 326 |     return; | 
 | 327 |   } | 
 | 328 |    | 
 | 329 |   $AlreadyScanned{$digest} = 1; | 
 | 330 |    | 
| Ted Kremenek | 809709f | 2008-04-18 16:58:34 +0000 | [diff] [blame] | 331 |   # 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] | 332 |   system ("chmod", "644", "$Dir/$FName"); | 
| Ted Kremenek | 684bb09 | 2008-04-18 15:18:20 +0000 | [diff] [blame] | 333 |    | 
 | 334 |   # Scan the report file for tags. | 
| Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 335 |   open(IN, "$Dir/$FName") or DieDiag("Cannot open '$Dir/$FName'\n"); | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 336 |  | 
 | 337 |   my $BugDesc = ""; | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 338 |   my $BugFile = ""; | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 339 |   my $BugCategory; | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 340 |   my $BugPathLength = 1; | 
 | 341 |   my $BugLine = 0; | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 342 |   my $found = 0; | 
 | 343 |  | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 344 |   while (<IN>) { | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 345 |  | 
 | 346 |     last if ($found == 5); | 
 | 347 |  | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 348 |     if (/<!-- BUGDESC (.*) -->$/) { | 
 | 349 |       $BugDesc = $1; | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 350 |       ++$found; | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 351 |     } | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 352 |     elsif (/<!-- BUGFILE (.*) -->$/) { | 
 | 353 |       $BugFile = $1; | 
| Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 354 |       UpdatePrefix($BugFile); | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 355 |       ++$found; | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 356 |     } | 
 | 357 |     elsif (/<!-- BUGPATHLENGTH (.*) -->$/) { | 
 | 358 |       $BugPathLength = $1; | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 359 |       ++$found; | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 360 |     } | 
 | 361 |     elsif (/<!-- BUGLINE (.*) -->$/) { | 
 | 362 |       $BugLine = $1;     | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 363 |       ++$found; | 
 | 364 |     } | 
 | 365 |     elsif (/<!-- BUGCATEGORY (.*) -->$/) { | 
 | 366 |       $BugCategory = $1; | 
 | 367 |       ++$found; | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 368 |     } | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 369 |   } | 
 | 370 |  | 
 | 371 |   close(IN); | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 372 |    | 
 | 373 |   if (!defined $BugCategory) { | 
 | 374 |     $BugCategory = "Other"; | 
 | 375 |   } | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 376 |      | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 377 |   push @$Index,[ $FName, $BugCategory, $BugDesc, $BugFile, $BugLine, | 
 | 378 |                  $BugPathLength ]; | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 379 | } | 
 | 380 |  | 
 | 381 | ##----------------------------------------------------------------------------## | 
 | 382 | # CopyJS - Copy JavaScript code to target directory. | 
 | 383 | ##----------------------------------------------------------------------------## | 
 | 384 |  | 
 | 385 | sub CopyJS { | 
 | 386 |  | 
 | 387 |   my $Dir = shift; | 
 | 388 |    | 
| Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 389 |   DieDiag("Cannot find 'sorttable.js'.\n") | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 390 |     if (! -r "$RealBin/sorttable.js");   | 
 | 391 |  | 
| Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 392 |   system ("cp", "$RealBin/sorttable.js", "$Dir"); | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 393 |  | 
| Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 394 |   DieDiag("Could not copy 'sorttable.js' to '$Dir'.\n") | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 395 |     if (! -r "$Dir/sorttable.js"); | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 396 | } | 
 | 397 |  | 
 | 398 | ##----------------------------------------------------------------------------## | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 399 | # Postprocess - Postprocess the results of an analysis scan. | 
 | 400 | ##----------------------------------------------------------------------------## | 
 | 401 |  | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 402 | sub Postprocess { | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 403 |    | 
 | 404 |   my $Dir = shift; | 
| Ted Kremenek | 684bb09 | 2008-04-18 15:18:20 +0000 | [diff] [blame] | 405 |   my $BaseDir = shift; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 406 |    | 
| Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 407 |   die "No directory specified." if (!defined $Dir); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 408 |    | 
 | 409 |   if (! -d $Dir) { | 
| Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 410 |     Diag("No bugs found.\n"); | 
| Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 411 |     return 0; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 412 |   } | 
 | 413 |    | 
 | 414 |   opendir(DIR, $Dir); | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 415 |   my $Crashes = 0; | 
 | 416 |   my @files = grep { if ($_ eq "crashes") { $Crashes++; } | 
 | 417 |                      /^report-.*\.html$/; } readdir(DIR); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 418 |   closedir(DIR); | 
 | 419 |  | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 420 |   if (scalar(@files) == 0 and $Crashes == 0) { | 
| Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 421 |     Diag("Removing directory '$Dir' because it contains no reports.\n"); | 
| Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 422 |     system ("rm", "-fR", $Dir); | 
| Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 423 |     return 0; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 424 |   } | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 425 |    | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 426 |   # Scan each report file and build an index.   | 
 | 427 |   my @Index;     | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 428 |   foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); } | 
 | 429 |    | 
| Ted Kremenek | d52e425 | 2008-08-25 20:45:07 +0000 | [diff] [blame] | 430 |   # Scan the crashes directory and use the information in the .info files | 
 | 431 |   # to update the common prefix directory. | 
 | 432 |   if (-d "$Dir/crashes") { | 
 | 433 |     opendir(DIR, "$Dir/crashes"); | 
 | 434 |     my @files = grep { /[.]info$/; } readdir(DIR); | 
 | 435 |     closedir(DIR); | 
 | 436 |     foreach my $file (@files) { | 
 | 437 |       open IN, "$Dir/crashes/$file" or DieDiag("cannot open $file\n"); | 
 | 438 |       my $Path = <IN>; | 
 | 439 |       if (defined $Path) { UpdatePrefix($Path); } | 
 | 440 |       close IN; | 
 | 441 |     }     | 
 | 442 |   } | 
 | 443 |    | 
| Ted Kremenek | 63c2017 | 2008-08-04 17:34:06 +0000 | [diff] [blame] | 444 |   # Generate an index.html file.   | 
 | 445 |   my $FName = "$Dir/index.html";   | 
 | 446 |   open(OUT, ">", $FName) or DieDiag("Cannot create file '$FName'\n"); | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 447 |    | 
| Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 448 |   # Print out the header. | 
 | 449 |    | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 450 | print OUT <<ENDTEXT; | 
 | 451 | <html> | 
 | 452 | <head> | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 453 | <style type="text/css"> | 
 | 454 |  body { color:#000000; background-color:#ffffff } | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 455 |  body { font-family: Helvetica, sans-serif; font-size:9pt } | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 456 |  h3 { font-size:12pt } | 
 | 457 |  table { font-size:9pt } | 
 | 458 |  table { border-spacing: 0px; border: 1px solid black } | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 459 |  table thead { | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 460 |    background-color:#eee; color:#666666; | 
 | 461 |    font-weight: bold; cursor: default; | 
| Ted Kremenek | bba1cf5 | 2008-04-03 05:50:51 +0000 | [diff] [blame] | 462 |    text-align:center; | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 463 |    font-weight: bold; font-family: Verdana; | 
 | 464 |    white-space:nowrap; | 
| Ted Kremenek | bba1cf5 | 2008-04-03 05:50:51 +0000 | [diff] [blame] | 465 |  }  | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 466 |  .W { font-size:0px } | 
 | 467 |  th, td { padding:5px; padding-left:8px; text-align:left } | 
 | 468 |  td.SUMM_DESC { padding-left:12px } | 
 | 469 |  td.DESC { white-space:pre } | 
 | 470 |  td.Q { text-align:right } | 
 | 471 |  td { text-align:left } | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 472 |  tbody.scrollContent { overflow:auto } | 
 | 473 | } | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 474 | </style> | 
| Ted Kremenek | 12a467f | 2008-09-21 20:10:46 +0000 | [diff] [blame^] | 475 | <script language='javascript' type="text/javascript"> | 
 | 476 | if (document.styleSheets && RegExp(" AppleWebKit/").test(navigator.userAgent)) | 
 | 477 | { | 
 | 478 |   var sheet = document.styleSheets[0]; | 
 | 479 |   if (sheet) { | 
 | 480 |     var rules = sheet.cssRules; | 
 | 481 |     if (rules) { | 
 | 482 |       sheet.insertRule("td.View a { white-space: nowrap; -webkit-appearance:square-button; padding-left:1em; padding-right:1em; padding-top:0.5ex; padding-bottom:0.5ex; text-decoration:none; color:black }", rules.length); | 
 | 483 |     } | 
 | 484 |   } | 
 | 485 | } | 
 | 486 | </script> | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 487 | <script src="sorttable.js"></script> | 
| Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 488 | <script language='javascript' type="text/javascript"> | 
 | 489 | function SetDisplay(RowClass, DisplayVal) | 
 | 490 | { | 
 | 491 |   var Rows = document.getElementsByTagName("tr"); | 
 | 492 |   for ( var i = 0 ; i < Rows.length; ++i ) { | 
 | 493 |     if (Rows[i].className == RowClass) { | 
 | 494 |       Rows[i].style.display = DisplayVal; | 
 | 495 |     } | 
 | 496 |   } | 
 | 497 | } | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 498 |  | 
| Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 499 | function ToggleDisplay(CheckButton, ClassName) { | 
| Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 500 |   if (CheckButton.checked) { | 
 | 501 |     SetDisplay(ClassName, ""); | 
 | 502 |   } | 
 | 503 |   else { | 
 | 504 |     SetDisplay(ClassName, "none"); | 
 | 505 |   } | 
 | 506 | } | 
 | 507 | </script> | 
 | 508 | </head> | 
 | 509 | <body> | 
 | 510 | ENDTEXT | 
 | 511 |  | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 512 |   if (scalar(@files)) { | 
 | 513 |     # Print out the summary table. | 
 | 514 |     my %Totals; | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 515 |  | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 516 |     for my $row ( @Index ) { | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 517 |       my $bug_type = ($row->[2]); | 
 | 518 |       my $bug_category = ($row->[1]); | 
 | 519 |       my $key = "$bug_category:$bug_type"; | 
 | 520 |  | 
 | 521 |       if (!defined $Totals{$key}) { $Totals{$key} = [1,$bug_category,$bug_type]; } | 
 | 522 |       else { $Totals{$key}->[0]++; } | 
| Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 523 |     } | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 524 |  | 
 | 525 |     print OUT "<h3>Bug Summary</h3>"; | 
 | 526 |  | 
 | 527 |     if (defined $BuildName) { | 
 | 528 |       print OUT "\n<p>Results in this analysis run are based on analyzer build <b>$BuildName</b>.</p>\n" | 
| Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 529 |     } | 
| Ted Kremenek | f4cdf41 | 2008-05-23 18:17:05 +0000 | [diff] [blame] | 530 |    | 
| Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 531 | print OUT <<ENDTEXT; | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 532 | <table> | 
 | 533 | <thead><tr><td>Bug Type</td><td>Quantity</td><td class="sorttable_nosort">Display?</td></tr></thead> | 
| Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 534 | ENDTEXT | 
 | 535 |    | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 536 |     my $last_category; | 
 | 537 |  | 
 | 538 |     for my $key ( | 
 | 539 |       sort { | 
 | 540 |         my $x = $Totals{$a}; | 
 | 541 |         my $y = $Totals{$b}; | 
 | 542 |         my $res = $x->[1] cmp $y->[1]; | 
 | 543 |         $res = $x->[2] cmp $y->[2] if ($res == 0); | 
 | 544 |         $res | 
 | 545 |       } keys %Totals )  | 
 | 546 |     { | 
 | 547 |       my $val = $Totals{$key}; | 
 | 548 |       my $category = $val->[1]; | 
 | 549 |       if (!defined $last_category or $last_category ne $category) { | 
 | 550 |         $last_category = $category; | 
 | 551 |         print OUT "<tr><th>$category</th><th colspan=2></th></tr>\n"; | 
 | 552 |       }       | 
 | 553 |       my $x = lc $key; | 
 | 554 |       $x =~ s/[ ,'":\/()]+/_/g; | 
 | 555 |       print OUT "<tr><td class=\"SUMM_DESC\">"; | 
 | 556 |       print OUT $val->[2]; | 
 | 557 |       print OUT "</td><td>"; | 
 | 558 |       print OUT $val->[0]; | 
 | 559 |       print OUT "</td><td><center><input type=\"checkbox\" onClick=\"ToggleDisplay(this,'bt_$x');\" checked/></center></td></tr>\n"; | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 560 |     } | 
| Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 561 |  | 
 | 562 |   # Print out the table of errors. | 
 | 563 |  | 
 | 564 | print OUT <<ENDTEXT; | 
 | 565 | </table> | 
 | 566 | <h3>Reports</h3> | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 567 |  | 
 | 568 | <table class="sortable" style="table-layout:automatic"> | 
 | 569 | <thead><tr> | 
 | 570 |   <td>Bug Group</td> | 
 | 571 |   <td class="sorttable_sorted">Bug Type<span id="sorttable_sortfwdind"> ▾</span></td> | 
| Ted Kremenek | bba1cf5 | 2008-04-03 05:50:51 +0000 | [diff] [blame] | 572 |   <td>File</td> | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 573 |   <td class="Q">Line</td> | 
 | 574 |   <td class="Q">Path Length</td> | 
| Ted Kremenek | 2645c77 | 2008-07-07 16:58:44 +0000 | [diff] [blame] | 575 |   <td class="sorttable_nosort"></td> | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 576 |   <!-- REPORTBUGCOL --> | 
 | 577 | </tr></thead> | 
 | 578 | <tbody> | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 579 | ENDTEXT | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 580 |  | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 581 |     my $prefix = GetPrefix(); | 
 | 582 |     my $regex; | 
 | 583 |     my $InFileRegex; | 
 | 584 |     my $InFilePrefix = "File:</td><td>"; | 
| Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 585 |    | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 586 |     if (defined $prefix) {  | 
 | 587 |       $regex = qr/^\Q$prefix\E/is;     | 
 | 588 |       $InFileRegex = qr/\Q$InFilePrefix$prefix\E/is; | 
 | 589 |     }     | 
| Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 590 |  | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 591 |     for my $row ( sort { $a->[2] cmp $b->[2] } @Index ) { | 
 | 592 |       my $x = "$row->[1]:$row->[2]"; | 
 | 593 |       $x = lc $x; | 
 | 594 |       $x =~ s/[ ,'":\/()]+/_/g; | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 595 |      | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 596 |       my $ReportFile = $row->[0]; | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 597 |            | 
 | 598 |       print OUT "<tr class=\"bt_$x\">"; | 
 | 599 |       print OUT "<td class=\"DESC\">"; | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 600 |       print OUT $row->[1]; | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 601 |       print OUT "</td>"; | 
 | 602 |       print OUT "<td class=\"DESC\">"; | 
 | 603 |       print OUT $row->[2]; | 
 | 604 |       print OUT "</td>"; | 
 | 605 |        | 
 | 606 |       # Update the file prefix.       | 
 | 607 |       my $fname = $row->[3]; | 
 | 608 |       my $full_fname = $fname; | 
 | 609 |  | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 610 |       if (defined $regex) { | 
 | 611 |         $fname =~ s/$regex//; | 
 | 612 |         UpdateInFilePath("$Dir/$ReportFile", $InFileRegex, $InFilePrefix) | 
 | 613 |       } | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 614 |        | 
 | 615 |       print OUT "<td>"; | 
 | 616 |       my $has_fname = 0; | 
 | 617 |       if (-r $full_fname) { | 
 | 618 |         $has_fname = 1; | 
 | 619 |         print OUT "<a href=\"$full_fname\">" | 
 | 620 |       } | 
 | 621 |        | 
 | 622 |       my @fname = split /\//,$fname; | 
 | 623 |       if ($#fname > 0) { | 
 | 624 |         while ($#fname >= 0) { | 
 | 625 |           my $x = shift @fname; | 
 | 626 |           print OUT $x; | 
 | 627 |           if ($#fname >= 0) { | 
 | 628 |             print OUT "<span class=\"W\"> </span>/"; | 
 | 629 |           } | 
 | 630 |         } | 
 | 631 |       } | 
 | 632 |       else { | 
 | 633 |         print OUT $fname; | 
 | 634 |       } | 
 | 635 |        | 
 | 636 |       if ($has_fname) { | 
 | 637 |         print OUT "</a>" | 
 | 638 |       } | 
 | 639 |       print OUT "</td>"; | 
 | 640 |        | 
 | 641 |       # Print out the quantities. | 
 | 642 |       for my $j ( 4 .. 5 ) { | 
 | 643 |         print OUT "<td class=\"Q\">$row->[$j]</td>";         | 
 | 644 |       } | 
 | 645 |        | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 646 |       # Print the rest of the columns. | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 647 |       for (my $j = 6; $j <= $#{$row}; ++$j) { | 
 | 648 |         print OUT "<td>$row->[$j]</td>" | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 649 |       } | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 650 |  | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 651 |       # Emit the "View" link. | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 652 |       print OUT "<td class=\"View\"><a href=\"$ReportFile#EndPath\">View Report</a></td>"; | 
| Ted Kremenek | 3cea9ee | 2008-07-30 17:58:08 +0000 | [diff] [blame] | 653 |          | 
| Daniel Dunbar | e43038e | 2008-09-19 23:18:44 +0000 | [diff] [blame] | 654 |       # Emit REPORTBUG markers. | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 655 |       print OUT "\n<!-- REPORTBUG id=\"$ReportFile\" -->\n"; | 
| Daniel Dunbar | e43038e | 2008-09-19 23:18:44 +0000 | [diff] [blame] | 656 |          | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 657 |       # End the row. | 
 | 658 |       print OUT "</tr>\n"; | 
 | 659 |     } | 
 | 660 |    | 
| Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 661 |     print OUT "</tbody>\n</table>\n\n"; | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 662 |   } | 
 | 663 |  | 
 | 664 |   if ($Crashes) { | 
 | 665 |     # Read the crash directory for files. | 
 | 666 |     opendir(DIR, "$Dir/crashes"); | 
 | 667 |     my @files = grep { /[.]info$/ } readdir(DIR); | 
 | 668 |     closedir(DIR); | 
 | 669 |  | 
 | 670 |     if (scalar(@files)) { | 
 | 671 |       print OUT <<ENDTEXT; | 
| Ted Kremenek | 5d31f83 | 2008-08-18 18:38:29 +0000 | [diff] [blame] | 672 | <h3>Analyzer Failures</h3> | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 673 |  | 
| Ted Kremenek | 5d31f83 | 2008-08-18 18:38:29 +0000 | [diff] [blame] | 674 | <p>The analyzer had problems processing the following files:</p> | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 675 |  | 
 | 676 | <table> | 
| Ted Kremenek | 9f9b1fd | 2008-09-12 22:49:36 +0000 | [diff] [blame] | 677 | <thead><tr><td>Problem</td><td>Source File</td><td>Preprocessed File</td><td>STDERR Output</td></tr></thead> | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 678 | ENDTEXT | 
 | 679 |    | 
 | 680 |       foreach my $file (sort @files) { | 
 | 681 |         $file =~ /(.+).info$/; | 
 | 682 |         # Get the preprocessed file. | 
 | 683 |         my $ppfile = $1; | 
 | 684 |         # Open the info file and get the name of the source file. | 
 | 685 |         open (INFO, "$Dir/crashes/$file") or | 
 | 686 |           die "Cannot open $Dir/crashes/$file\n"; | 
 | 687 |         my $srcfile = <INFO>; | 
| Ted Kremenek | 5d31f83 | 2008-08-18 18:38:29 +0000 | [diff] [blame] | 688 |         chomp $srcfile; | 
 | 689 |         my $problem = <INFO>; | 
 | 690 |         chomp $problem; | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 691 |         close (INFO); | 
 | 692 |         # Print the information in the table. | 
| Ted Kremenek | d52e425 | 2008-08-25 20:45:07 +0000 | [diff] [blame] | 693 |         my $prefix = GetPrefix(); | 
| Ted Kremenek | 9f9b1fd | 2008-09-12 22:49:36 +0000 | [diff] [blame] | 694 |         if (defined $prefix) { $srcfile =~ s/^\Q$prefix//; } | 
 | 695 |         print OUT "<tr><td>$problem</td><td>$srcfile</td><td><a href=\"crashes/$ppfile\">$ppfile</a></td><td><a href=\"crashes/$ppfile.stderr.txt\">$ppfile.stderr.txt</a></td></tr>\n"; | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 696 |       } | 
 | 697 |  | 
 | 698 |       print OUT <<ENDTEXT; | 
 | 699 | </table> | 
 | 700 | <p>Please consider submitting preprocessed files as <a href="http://clang.llvm.org/StaticAnalysisUsage.html#filingbugs">bug reports</a>.</p> | 
 | 701 | ENDTEXT | 
 | 702 |     } | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 703 |   } | 
 | 704 |    | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 705 |   print OUT "</body></html>\n";   | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 706 |   close(OUT); | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 707 |   CopyJS($Dir); | 
| Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 708 |  | 
 | 709 |   # Make sure $Dir and $BaseDir are world readable/executable. | 
 | 710 |   system("chmod", "755", $Dir); | 
| Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 711 |   if (defined $BaseDir) { system("chmod", "755", $BaseDir); } | 
| Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 712 |  | 
| Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 713 |   my $Num = scalar(@Index); | 
| Ted Kremenek | 150c212 | 2008-07-11 19:15:05 +0000 | [diff] [blame] | 714 |   Diag("$Num bugs found.\n"); | 
 | 715 |   if ($Num > 0 && -r "$Dir/index.html") { | 
 | 716 |     Diag("Open '$Dir/index.html' to examine bug reports.\n"); | 
 | 717 |   } | 
| Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 718 |    | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 719 |   DiagCrashes($Dir) if ($Crashes); | 
 | 720 |    | 
| Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 721 |   return $Num; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 722 | } | 
 | 723 |  | 
 | 724 | ##----------------------------------------------------------------------------## | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 725 | # RunBuildCommand - Run the build command. | 
 | 726 | ##----------------------------------------------------------------------------## | 
 | 727 |  | 
| Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 728 | sub AddIfNotPresent { | 
 | 729 |   my $Args = shift; | 
 | 730 |   my $Arg = shift;   | 
 | 731 |   my $found = 0; | 
 | 732 |    | 
 | 733 |   foreach my $k (@$Args) { | 
 | 734 |     if ($k eq $Arg) { | 
 | 735 |       $found = 1; | 
 | 736 |       last; | 
 | 737 |     } | 
 | 738 |   } | 
 | 739 |    | 
 | 740 |   if ($found == 0) { | 
 | 741 |     push @$Args, $Arg; | 
 | 742 |   } | 
 | 743 | } | 
 | 744 |  | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 745 | sub RunBuildCommand { | 
 | 746 |    | 
 | 747 |   my $Args = shift; | 
| Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame] | 748 |   my $IgnoreErrors = shift; | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 749 |   my $Cmd = $Args->[0]; | 
| Ted Kremenek | 6195c37 | 2008-06-02 21:52:47 +0000 | [diff] [blame] | 750 |   my $CCAnalyzer = shift; | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 751 |    | 
| Ted Kremenek | 3301cb1 | 2008-06-30 18:18:16 +0000 | [diff] [blame] | 752 |   # Get only the part of the command after the last '/'. | 
 | 753 |   if ($Cmd =~ /\/([^\/]+)$/) { | 
 | 754 |     $Cmd = $1; | 
 | 755 |   } | 
 | 756 |    | 
| Ted Kremenek | 63c2017 | 2008-08-04 17:34:06 +0000 | [diff] [blame] | 757 |   if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc"  | 
 | 758 |    or $Cmd eq "ccc-analyzer") { | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 759 |     shift @$Args; | 
| Ted Kremenek | 6195c37 | 2008-06-02 21:52:47 +0000 | [diff] [blame] | 760 |     unshift @$Args, $CCAnalyzer; | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 761 |   } | 
| Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame] | 762 |   elsif ($IgnoreErrors) { | 
 | 763 |     if ($Cmd eq "make" or $Cmd eq "gmake") { | 
| Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 764 |       AddIfNotPresent($Args,"-k"); | 
| Ted Kremenek | 8912b54 | 2008-05-13 21:28:02 +0000 | [diff] [blame] | 765 |       AddIfNotPresent($Args,"-i"); | 
| Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame] | 766 |     } | 
 | 767 |     elsif ($Cmd eq "xcodebuild") { | 
| Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 768 |       AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES"); | 
| Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame] | 769 |     } | 
| Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 770 |   }  | 
 | 771 |    | 
| Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 772 |   if ($Cmd eq "xcodebuild") { | 
| Ted Kremenek | cfd4c7b | 2008-05-23 22:18:16 +0000 | [diff] [blame] | 773 |     # Disable distributed builds for xcodebuild. | 
| Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 774 |     AddIfNotPresent($Args,"-nodistribute"); | 
| Ted Kremenek | cfd4c7b | 2008-05-23 22:18:16 +0000 | [diff] [blame] | 775 |  | 
 | 776 |     # Disable PCH files until clang supports them. | 
 | 777 |     AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO"); | 
| Ted Kremenek | 915e972 | 2008-05-27 23:18:07 +0000 | [diff] [blame] | 778 |      | 
 | 779 |     # When 'CC' is set, xcodebuild uses it to do all linking, even if we are | 
 | 780 |     # linking C++ object files.  Set 'LDPLUSPLUS' so that xcodebuild uses 'g++' | 
 | 781 |     # when linking such files. | 
| Ted Kremenek | 95aa105 | 2008-09-04 17:52:41 +0000 | [diff] [blame] | 782 |     die if (!defined $CXX); | 
 | 783 |     my $LDPLUSPLUS = `which $CXX`; | 
| Ted Kremenek | 915e972 | 2008-05-27 23:18:07 +0000 | [diff] [blame] | 784 |     $LDPLUSPLUS =~ s/\015?\012//;  # strip newlines | 
 | 785 |     $ENV{'LDPLUSPLUS'} = $LDPLUSPLUS;     | 
| Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 786 |   } | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 787 |    | 
| Ted Kremenek | 5a4ddaf | 2008-08-25 20:10:45 +0000 | [diff] [blame] | 788 |   return (system(@$Args) >> 8); | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 789 | } | 
 | 790 |  | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 791 | ##----------------------------------------------------------------------------## | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 792 | # DisplayHelp - Utility function to display all help options. | 
 | 793 | ##----------------------------------------------------------------------------## | 
 | 794 |  | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 795 | sub DisplayHelp { | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 796 |    | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 797 | print <<ENDTEXT; | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 798 | USAGE: $Prog [options] <build command> [build options] | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 799 |  | 
| Ted Kremenek | f4cdf41 | 2008-05-23 18:17:05 +0000 | [diff] [blame] | 800 | ENDTEXT | 
 | 801 |  | 
| Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 802 |   if (defined $BuildName) { | 
| Ted Kremenek | f4cdf41 | 2008-05-23 18:17:05 +0000 | [diff] [blame] | 803 |     print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n"; | 
 | 804 |   } | 
 | 805 |  | 
 | 806 | print <<ENDTEXT; | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 807 | OPTIONS: | 
 | 808 |  | 
| Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 809 |  -o             - Target directory for HTML report files.  Subdirectories | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 810 |                   will be created as needed to represent separate "runs" of | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 811 |                   the analyzer.  If this option is not specified, a directory | 
 | 812 |                   is created in /tmp to store the reports. | 
| Ted Kremenek | 1262fc4 | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 813 |  | 
| Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 814 |  -h             - Display this message. | 
 | 815 |  --help | 
| Ted Kremenek | 1262fc4 | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 816 |  | 
| Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 817 |  -k             - Add a "keep on going" option to the specified build command. | 
 | 818 |  --keep-going     This option currently supports make and xcodebuild. | 
| Ted Kremenek | f02e8db | 2008-04-02 16:41:25 +0000 | [diff] [blame] | 819 |                   This is a convenience option; one can specify this | 
 | 820 |                   behavior directly using build options. | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 821 |  | 
| Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 822 |  --status-bugs  - By default, the exit status of $Prog is the same as the | 
 | 823 |                   executed build command.  Specifying this option causes the | 
 | 824 |                   exit status of $Prog to be 1 if it found potential bugs | 
 | 825 |                   and 0 otherwise. | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 826 |  | 
| Ted Kremenek | 386c693 | 2008-09-03 17:59:35 +0000 | [diff] [blame] | 827 |  --use-cc [compiler path]   - By default, $Prog uses 'gcc' to compile and link | 
 | 828 |  --use-cc=[compiler path]     your C and Objective-C code. Use this option | 
 | 829 |                               to specify an alternate compiler. | 
 | 830 |  | 
 | 831 |  --use-c++ [compiler path]  - By default, $Prog uses 'g++' to compile and link | 
 | 832 |  --use-c++=[compiler path]    your C++ and Objective-C++ code. Use this option | 
 | 833 |                               to specify an alternate compiler. | 
| Ted Kremenek | f17ef3c | 2008-08-21 21:47:09 +0000 | [diff] [blame] | 834 |  | 
| Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 835 |  -v             - Verbose output from $Prog and the analyzer. | 
| Ted Kremenek | 386c693 | 2008-09-03 17:59:35 +0000 | [diff] [blame] | 836 |                   A second and third '-v' increases verbosity. | 
| Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 837 |  | 
 | 838 |  -V             - View analysis results in a web browser when the build | 
 | 839 |  --view           completes. | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 840 |  | 
| Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 841 |  | 
| Ted Kremenek | 386c693 | 2008-09-03 17:59:35 +0000 | [diff] [blame] | 842 | AVAILABLE ANALYSES (multiple analyses may be specified): | 
| Ted Kremenek | d52e425 | 2008-08-25 20:45:07 +0000 | [diff] [blame] | 843 |  | 
 | 844 | ENDTEXT | 
| Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 845 |  | 
 | 846 |   foreach my $Analysis (sort keys %AvailableAnalyses) { | 
| Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 847 |     if (defined $AnalysesDefaultEnabled{$Analysis}) { | 
| Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 848 |       print " (+)"; | 
| Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 849 |     } | 
 | 850 |     else { | 
| Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 851 |       print "    "; | 
| Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 852 |     } | 
 | 853 |      | 
 | 854 |     print " $Analysis  $AvailableAnalyses{$Analysis}\n"; | 
 | 855 |   } | 
 | 856 |    | 
 | 857 | print <<ENDTEXT | 
 | 858 |  | 
| Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 859 |  NOTE: "(+)" indicates that an analysis is enabled by default unless one | 
 | 860 |        or more analysis options are specified | 
| Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 861 |  | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 862 | BUILD OPTIONS | 
 | 863 |  | 
| Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 864 |  You can specify any build option acceptable to the build command. | 
| Ted Kremenek | 39eefde | 2008-04-02 16:47:27 +0000 | [diff] [blame] | 865 |  | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 866 | EXAMPLE | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 867 |  | 
| Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 868 |  $Prog -o /tmp/myhtmldir make -j4 | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 869 |       | 
| Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 870 |  The above example causes analysis reports to be deposited into | 
 | 871 |  a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option. | 
 | 872 |  A different subdirectory is created each time $Prog analyzes a project. | 
 | 873 |  The analyzer should support most parallel builds, but not distributed builds. | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 874 |  | 
 | 875 | ENDTEXT | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 876 | } | 
 | 877 |  | 
 | 878 | ##----------------------------------------------------------------------------## | 
 | 879 | # Process command-line arguments. | 
 | 880 | ##----------------------------------------------------------------------------## | 
 | 881 |  | 
 | 882 | my $HtmlDir;           # Parent directory to store HTML files. | 
 | 883 | my $IgnoreErrors = 0;  # Ignore build errors. | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 884 | my $ViewResults  = 0;  # View results when the build terminates. | 
| Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 885 | my $ExitStatusFoundBugs = 0; # Exit status reflects whether bugs were found | 
| Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 886 | my @AnalysesToRun; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 887 |  | 
 | 888 | if (!@ARGV) { | 
 | 889 |   DisplayHelp(); | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 890 |   exit 1; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 891 | } | 
 | 892 |  | 
 | 893 | while (@ARGV) { | 
 | 894 |    | 
 | 895 |   # Scan for options we recognize. | 
 | 896 |    | 
 | 897 |   my $arg = $ARGV[0]; | 
 | 898 |  | 
| Sam Bishop | 2f2418e | 2008-04-03 14:29:47 +0000 | [diff] [blame] | 899 |   if ($arg eq "-h" or $arg eq "--help") { | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 900 |     DisplayHelp(); | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 901 |     exit 0; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 902 |   } | 
 | 903 |    | 
| Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 904 |   if (defined $AvailableAnalyses{$arg}) { | 
| Ted Kremenek | 1262fc4 | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 905 |     shift @ARGV; | 
| Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 906 |     push @AnalysesToRun, $arg; | 
| Ted Kremenek | 1262fc4 | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 907 |     next; | 
 | 908 |   } | 
 | 909 |    | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 910 |   if ($arg eq "-o") { | 
 | 911 |     shift @ARGV; | 
 | 912 |          | 
 | 913 |     if (!@ARGV) { | 
| Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 914 |       DieDiag("'-o' option requires a target directory name.\n"); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 915 |     } | 
 | 916 |      | 
 | 917 |     $HtmlDir = shift @ARGV; | 
 | 918 |     next; | 
 | 919 |   } | 
 | 920 |    | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 921 |   if ($arg eq "-k" or $arg eq "--keep-going") { | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 922 |     shift @ARGV; | 
 | 923 |     $IgnoreErrors = 1; | 
 | 924 |     next; | 
 | 925 |   } | 
 | 926 |    | 
| Ted Kremenek | f17ef3c | 2008-08-21 21:47:09 +0000 | [diff] [blame] | 927 |   if ($arg =~ /^--use-cc(=(.+))?$/) { | 
 | 928 |     shift @ARGV; | 
 | 929 |     my $cc; | 
 | 930 |      | 
 | 931 |     if ($2 eq "") { | 
 | 932 |       if (!@ARGV) { | 
 | 933 |         DieDiag("'--use-cc' option requires a compiler executable name.\n"); | 
 | 934 |       } | 
 | 935 |       $cc = shift @ARGV; | 
 | 936 |     } | 
 | 937 |     else { | 
 | 938 |       $cc = $2; | 
 | 939 |     } | 
 | 940 |      | 
 | 941 |     $ENV{"CCC_CC"} = $cc; | 
 | 942 |     next; | 
 | 943 |   } | 
 | 944 |    | 
| Ted Kremenek | 386c693 | 2008-09-03 17:59:35 +0000 | [diff] [blame] | 945 |   if ($arg =~ /^--use-c[+][+](=(.+))?$/) { | 
 | 946 |     shift @ARGV; | 
 | 947 |      | 
 | 948 |     if ($2 eq "") { | 
 | 949 |       if (!@ARGV) { | 
 | 950 |         DieDiag("'--use-c++' option requires a compiler executable name.\n"); | 
 | 951 |       } | 
 | 952 |       $CXX = shift @ARGV; | 
 | 953 |     } | 
 | 954 |     else { | 
 | 955 |       $CXX = $2; | 
 | 956 |     } | 
 | 957 |     next; | 
 | 958 |   } | 
 | 959 |    | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 960 |   if ($arg eq "-v") { | 
 | 961 |     shift @ARGV; | 
 | 962 |     $Verbose++; | 
 | 963 |     next; | 
 | 964 |   } | 
 | 965 |    | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 966 |   if ($arg eq "-V" or $arg eq "--view") { | 
 | 967 |     shift @ARGV; | 
 | 968 |     $ViewResults = 1;     | 
 | 969 |     next; | 
 | 970 |   } | 
 | 971 |    | 
| Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 972 |   if ($arg eq "--status-bugs") { | 
 | 973 |     shift @ARGV; | 
 | 974 |     $ExitStatusFoundBugs = 1; | 
 | 975 |     next; | 
 | 976 |   } | 
 | 977 |    | 
| Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 978 |   DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/); | 
| Ted Kremenek | 0062ad4 | 2008-04-02 16:35:01 +0000 | [diff] [blame] | 979 |    | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 980 |   last; | 
 | 981 | } | 
 | 982 |  | 
 | 983 | if (!@ARGV) { | 
| Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 984 |   Diag("No build command specified.\n\n"); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 985 |   DisplayHelp(); | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 986 |   exit 1; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 987 | } | 
 | 988 |  | 
| Ted Kremenek | 386c693 | 2008-09-03 17:59:35 +0000 | [diff] [blame] | 989 |  | 
 | 990 |  | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 991 | # Determine the output directory for the HTML reports. | 
| Ted Kremenek | 684bb09 | 2008-04-18 15:18:20 +0000 | [diff] [blame] | 992 | my $BaseDir = $HtmlDir; | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 993 | $HtmlDir = GetHTMLRunDir($HtmlDir); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 994 |  | 
 | 995 | # Set the appropriate environment variables. | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 996 | SetHtmlEnv(\@ARGV, $HtmlDir); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 997 |  | 
| Ted Kremenek | 0b6c153 | 2008-04-08 20:22:12 +0000 | [diff] [blame] | 998 | my $Cmd = "$RealBin/ccc-analyzer"; | 
 | 999 |  | 
| Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 1000 | DieDiag("Executable 'ccc-analyzer' does not exist at '$Cmd'\n") | 
| Ted Kremenek | 0b6c153 | 2008-04-08 20:22:12 +0000 | [diff] [blame] | 1001 |   if (! -x $Cmd); | 
| Ted Kremenek | f22eacb | 2008-04-18 22:00:56 +0000 | [diff] [blame] | 1002 |  | 
| Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 1003 | if (! -x $ClangSB) { | 
 | 1004 |   Diag("'clang' executable not found in '$RealBin'.\n"); | 
 | 1005 |   Diag("Using 'clang' from path.\n"); | 
| Ted Kremenek | f22eacb | 2008-04-18 22:00:56 +0000 | [diff] [blame] | 1006 | } | 
| Ted Kremenek | 0b6c153 | 2008-04-08 20:22:12 +0000 | [diff] [blame] | 1007 |  | 
| Ted Kremenek | 95aa105 | 2008-09-04 17:52:41 +0000 | [diff] [blame] | 1008 | if (defined $CXX) { | 
 | 1009 |   $ENV{'CXX'} = $CXX; | 
 | 1010 | } | 
 | 1011 | else { | 
 | 1012 |   $CXX = 'g++';  # This variable is used by other parts of scan-build | 
 | 1013 |                  # that need to know a default C++ compiler to fall back to. | 
 | 1014 | } | 
 | 1015 |    | 
| Ted Kremenek | 4f4b17d | 2008-04-03 20:08:18 +0000 | [diff] [blame] | 1016 | $ENV{'CC'} = $Cmd; | 
| Ted Kremenek | f22eacb | 2008-04-18 22:00:56 +0000 | [diff] [blame] | 1017 | $ENV{'CLANG'} = $Clang; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1018 |  | 
 | 1019 | if ($Verbose >= 2) { | 
 | 1020 |   $ENV{'CCC_ANALYZER_VERBOSE'} = 1; | 
 | 1021 | } | 
 | 1022 |  | 
| Ted Kremenek | a9525c9 | 2008-05-12 22:07:14 +0000 | [diff] [blame] | 1023 | if ($Verbose >= 3) { | 
 | 1024 |   $ENV{'CCC_ANALYZER_LOG'} = 1; | 
 | 1025 | } | 
 | 1026 |  | 
| Ted Kremenek | 9012599 | 2008-07-15 23:41:32 +0000 | [diff] [blame] | 1027 | if (scalar(@AnalysesToRun) == 0) { | 
 | 1028 |   foreach my $key (keys %AnalysesDefaultEnabled) { | 
 | 1029 |     push @AnalysesToRun,$key; | 
 | 1030 |   } | 
| Ted Kremenek | 0100678 | 2008-07-02 23:16:10 +0000 | [diff] [blame] | 1031 | } | 
| Ted Kremenek | 1262fc4 | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 1032 |  | 
| Ted Kremenek | 9012599 | 2008-07-15 23:41:32 +0000 | [diff] [blame] | 1033 | $ENV{'CCC_ANALYZER_ANALYSIS'} = join ' ',@AnalysesToRun; | 
 | 1034 |  | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1035 | # Run the build. | 
| Ted Kremenek | 5656a98 | 2008-07-15 17:09:28 +0000 | [diff] [blame] | 1036 | my $ExitStatus = RunBuildCommand(\@ARGV, $IgnoreErrors, $Cmd); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1037 |  | 
 | 1038 | # Postprocess the HTML directory. | 
| Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 1039 | my $NumBugs = Postprocess($HtmlDir, $BaseDir); | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 1040 |  | 
 | 1041 | if ($ViewResults and -r "$HtmlDir/index.html") { | 
 | 1042 |   # Only works on Mac OS X (for now). | 
 | 1043 |   print "Viewing analysis results: '$HtmlDir/index.html'\n"; | 
| Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 1044 |   system("open", "$HtmlDir/index.html"); | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 1045 | } | 
| Ted Kremenek | 5656a98 | 2008-07-15 17:09:28 +0000 | [diff] [blame] | 1046 |  | 
| Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 1047 | if ($ExitStatusFoundBugs) { | 
 | 1048 |   exit 1 if ($NumBugs > 0); | 
 | 1049 |   exit 0; | 
 | 1050 | } | 
 | 1051 |  | 
| Ted Kremenek | 5656a98 | 2008-07-15 17:09:28 +0000 | [diff] [blame] | 1052 | exit $ExitStatus; | 
 | 1053 |  |