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