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 | cd25c13 | 2008-12-03 19:50:37 +0000 | [diff] [blame] | 22 | use Cwd qw/ getcwd abs_path /; |
Ted Kremenek | 7cba112 | 2008-09-22 01:35:58 +0000 | [diff] [blame] | 23 | use Sys::Hostname; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 24 | |
| 25 | my $Verbose = 0; # Verbose output from this script. |
| 26 | my $Prog = "scan-build"; |
Ted Kremenek | f4cdf41 | 2008-05-23 18:17:05 +0000 | [diff] [blame] | 27 | my $BuildName; |
| 28 | my $BuildDate; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 29 | |
Ted Kremenek | 0e68938 | 2008-09-11 18:17:51 +0000 | [diff] [blame] | 30 | my $TERM = $ENV{'TERM'}; |
| 31 | my $UseColor = (defined $TERM and $TERM eq 'xterm-color' and -t STDOUT |
| 32 | and defined $ENV{'SCAN_BUILD_COLOR'}); |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 33 | |
Ted Kremenek | 7cba112 | 2008-09-22 01:35:58 +0000 | [diff] [blame] | 34 | my $UserName = HtmlEscape(getpwuid($<) || 'unknown'); |
| 35 | my $HostName = HtmlEscape(hostname() || 'unknown'); |
| 36 | my $CurrentDir = HtmlEscape(getcwd()); |
| 37 | my $CurrentDirSuffix = basename($CurrentDir); |
| 38 | |
| 39 | my $CmdArgs; |
| 40 | |
| 41 | my $HtmlTitle; |
| 42 | |
| 43 | my $Date = localtime(); |
| 44 | |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 45 | ##----------------------------------------------------------------------------## |
| 46 | # Diagnostics |
| 47 | ##----------------------------------------------------------------------------## |
| 48 | |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 49 | sub Diag { |
| 50 | if ($UseColor) { |
| 51 | print BOLD, MAGENTA "$Prog: @_"; |
| 52 | print RESET; |
| 53 | } |
| 54 | else { |
| 55 | print "$Prog: @_"; |
| 56 | } |
| 57 | } |
| 58 | |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 59 | sub DiagCrashes { |
| 60 | my $Dir = shift; |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 61 | Diag ("The analyzer encountered problems on some source files.\n"); |
| 62 | Diag ("Preprocessed versions of these sources were deposited in '$Dir/failures'.\n"); |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 63 | Diag ("Please consider submitting a bug report using these files:\n"); |
Nico Weber | e2c8663 | 2011-08-28 11:50:56 +0000 | [diff] [blame] | 64 | Diag (" http://clang-analyzer.llvm.org/filing_bugs.html\n") |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 67 | sub DieDiag { |
| 68 | if ($UseColor) { |
| 69 | print BOLD, RED "$Prog: "; |
| 70 | print RESET, RED @_; |
| 71 | print RESET; |
| 72 | } |
| 73 | else { |
| 74 | print "$Prog: ", @_; |
| 75 | } |
| 76 | exit(0); |
| 77 | } |
| 78 | |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 79 | ##----------------------------------------------------------------------------## |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 80 | # Some initial preprocessing of Clang options. |
| 81 | ##----------------------------------------------------------------------------## |
| 82 | |
Ted Kremenek | 2a3a8b9 | 2009-12-11 22:44:53 +0000 | [diff] [blame] | 83 | # Find 'clang' |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 84 | my $ClangSB = Cwd::realpath("$RealBin/bin/clang"); |
Ted Kremenek | 8d10cdd | 2009-02-20 04:34:29 +0000 | [diff] [blame] | 85 | if (!defined $ClangSB || ! -x $ClangSB) { |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 86 | $ClangSB = Cwd::realpath("$RealBin/clang"); |
| 87 | } |
Ted Kremenek | e01ca51 | 2010-02-05 20:34:14 +0000 | [diff] [blame] | 88 | my $Clang; |
| 89 | if (!defined $ClangSB || ! -x $ClangSB) { |
| 90 | # Default to looking for 'clang' in the path. |
Ted Kremenek | 6492200 | 2010-02-12 00:12:25 +0000 | [diff] [blame] | 91 | $Clang = `which clang`; |
| 92 | chomp $Clang; |
| 93 | if ($Clang eq "") { |
| 94 | DieDiag("No 'clang' executable found in path."); |
| 95 | } |
Ted Kremenek | 2a3a8b9 | 2009-12-11 22:44:53 +0000 | [diff] [blame] | 96 | } |
Ted Kremenek | e01ca51 | 2010-02-05 20:34:14 +0000 | [diff] [blame] | 97 | else { |
| 98 | $Clang = $ClangSB; |
| 99 | } |
| 100 | my $ClangCXX = $Clang . "++"; |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 101 | |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 102 | ##----------------------------------------------------------------------------## |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 103 | # GetHTMLRunDir - Construct an HTML directory name for the current sub-run. |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 104 | ##----------------------------------------------------------------------------## |
| 105 | |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 106 | sub GetHTMLRunDir { |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 107 | die "Not enough arguments." if (@_ == 0); |
Ted Kremenek | 2a3a8b9 | 2009-12-11 22:44:53 +0000 | [diff] [blame] | 108 | my $Dir = shift @_; |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 109 | my $TmpMode = 0; |
| 110 | if (!defined $Dir) { |
Ted Kremenek | ffda0b4 | 2008-10-31 05:48:42 +0000 | [diff] [blame] | 111 | if (`uname` =~ /Darwin/) { |
| 112 | $Dir = $ENV{'TMPDIR'}; |
| 113 | if (!defined $Dir) { $Dir = "/tmp"; } |
| 114 | } |
| 115 | else { |
| 116 | $Dir = "/tmp"; |
Ted Kremenek | 2a3a8b9 | 2009-12-11 22:44:53 +0000 | [diff] [blame] | 117 | } |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 118 | $TmpMode = 1; |
| 119 | } |
Ted Kremenek | bf762c9 | 2009-02-24 02:38:02 +0000 | [diff] [blame] | 120 | |
| 121 | # Chop off any trailing '/' characters. |
| 122 | while ($Dir =~ /\/$/) { chop $Dir; } |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 123 | |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 124 | # Get current date and time. |
Ted Kremenek | 2a3a8b9 | 2009-12-11 22:44:53 +0000 | [diff] [blame] | 125 | my @CurrentTime = localtime(); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 126 | my $year = $CurrentTime[5] + 1900; |
| 127 | my $day = $CurrentTime[3]; |
| 128 | my $month = $CurrentTime[4] + 1; |
Ted Kremenek | 9d7405f | 2008-05-14 17:23:56 +0000 | [diff] [blame] | 129 | my $DateString = sprintf("%d-%02d-%02d", $year, $month, $day); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 130 | |
Ted Kremenek | 2a3a8b9 | 2009-12-11 22:44:53 +0000 | [diff] [blame] | 131 | # Determine the run number. |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 132 | my $RunNumber; |
| 133 | |
Ted Kremenek | 2a3a8b9 | 2009-12-11 22:44:53 +0000 | [diff] [blame] | 134 | if (-d $Dir) { |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 135 | if (! -r $Dir) { |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 136 | DieDiag("directory '$Dir' exists but is not readable.\n"); |
Ted Kremenek | 2a3a8b9 | 2009-12-11 22:44:53 +0000 | [diff] [blame] | 137 | } |
| 138 | # Iterate over all files in the specified directory. |
| 139 | my $max = 0; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 140 | opendir(DIR, $Dir); |
Ted Kremenek | 29da6c5 | 2008-08-07 17:57:34 +0000 | [diff] [blame] | 141 | my @FILES = grep { -d "$Dir/$_" } readdir(DIR); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 142 | closedir(DIR); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 143 | |
Ted Kremenek | 2a3a8b9 | 2009-12-11 22:44:53 +0000 | [diff] [blame] | 144 | foreach my $f (@FILES) { |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 145 | # Strip the prefix '$Prog-' if we are dumping files to /tmp. |
| 146 | if ($TmpMode) { |
| 147 | next if (!($f =~ /^$Prog-(.+)/)); |
| 148 | $f = $1; |
| 149 | } |
| 150 | |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 151 | my @x = split/-/, $f; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 152 | next if (scalar(@x) != 4); |
| 153 | next if ($x[0] != $year); |
| 154 | next if ($x[1] != $month); |
| 155 | next if ($x[2] != $day); |
| 156 | |
| 157 | if ($x[3] > $max) { |
| 158 | $max = $x[3]; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | $RunNumber = $max + 1; |
| 163 | } |
| 164 | else { |
| 165 | |
| 166 | if (-x $Dir) { |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 167 | DieDiag("'$Dir' exists but is not a directory.\n"); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 168 | } |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 169 | |
| 170 | if ($TmpMode) { |
Ted Kremenek | 445fa77 | 2008-10-10 00:17:08 +0000 | [diff] [blame] | 171 | DieDiag("The directory '/tmp' does not exist or cannot be accessed.\n"); |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 174 | # $Dir does not exist. It will be automatically created by the |
| 175 | # clang driver. Set the run number to 1. |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 176 | |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 177 | $RunNumber = 1; |
| 178 | } |
| 179 | |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 180 | die "RunNumber must be defined!" if (!defined $RunNumber); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 181 | |
| 182 | # Append the run number. |
Ted Kremenek | fc0898a | 2008-09-04 23:56:36 +0000 | [diff] [blame] | 183 | my $NewDir; |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 184 | if ($TmpMode) { |
Ted Kremenek | fc0898a | 2008-09-04 23:56:36 +0000 | [diff] [blame] | 185 | $NewDir = "$Dir/$Prog-$DateString-$RunNumber"; |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 186 | } |
| 187 | else { |
Ted Kremenek | fc0898a | 2008-09-04 23:56:36 +0000 | [diff] [blame] | 188 | $NewDir = "$Dir/$DateString-$RunNumber"; |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 189 | } |
Ted Kremenek | fc0898a | 2008-09-04 23:56:36 +0000 | [diff] [blame] | 190 | system 'mkdir','-p',$NewDir; |
| 191 | return $NewDir; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 194 | sub SetHtmlEnv { |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 195 | |
| 196 | die "Wrong number of arguments." if (scalar(@_) != 2); |
| 197 | |
| 198 | my $Args = shift; |
| 199 | my $Dir = shift; |
| 200 | |
| 201 | die "No build command." if (scalar(@$Args) == 0); |
| 202 | |
| 203 | my $Cmd = $$Args[0]; |
Ted Kremenek | 9e4a1bb | 2011-04-01 18:47:06 +0000 | [diff] [blame] | 204 | |
| 205 | if ($Cmd =~ /configure/ || $Cmd =~ /autogen/) { |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 206 | return; |
| 207 | } |
| 208 | |
| 209 | if ($Verbose) { |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 210 | Diag("Emitting reports for this run to '$Dir'.\n"); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | $ENV{'CCC_ANALYZER_HTML'} = $Dir; |
| 214 | } |
| 215 | |
| 216 | ##----------------------------------------------------------------------------## |
Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 217 | # ComputeDigest - Compute a digest of the specified file. |
| 218 | ##----------------------------------------------------------------------------## |
| 219 | |
| 220 | sub ComputeDigest { |
| 221 | my $FName = shift; |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 222 | DieDiag("Cannot read $FName to compute Digest.\n") if (! -r $FName); |
Ted Kremenek | a6e2481 | 2008-04-19 18:05:48 +0000 | [diff] [blame] | 223 | |
| 224 | # 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] | 225 | # just looking for duplicate files that come from a non-malicious source. |
| 226 | # 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] | 227 | # come bundled on most systems. |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 228 | open(FILE, $FName) or DieDiag("Cannot open $FName when computing Digest.\n"); |
Ted Kremenek | a6e2481 | 2008-04-19 18:05:48 +0000 | [diff] [blame] | 229 | binmode FILE; |
| 230 | my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest; |
| 231 | close(FILE); |
| 232 | |
Ted Kremenek | 63c2017 | 2008-08-04 17:34:06 +0000 | [diff] [blame] | 233 | # Return the digest. |
Ted Kremenek | a6e2481 | 2008-04-19 18:05:48 +0000 | [diff] [blame] | 234 | return $Result; |
Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | ##----------------------------------------------------------------------------## |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 238 | # UpdatePrefix - Compute the common prefix of files. |
| 239 | ##----------------------------------------------------------------------------## |
| 240 | |
| 241 | my $Prefix; |
| 242 | |
| 243 | sub UpdatePrefix { |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 244 | my $x = shift; |
| 245 | my $y = basename($x); |
| 246 | $x =~ s/\Q$y\E$//; |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 247 | |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 248 | if (!defined $Prefix) { |
| 249 | $Prefix = $x; |
| 250 | return; |
| 251 | } |
| 252 | |
Ted Kremenek | 20b2bae | 2008-09-11 21:15:10 +0000 | [diff] [blame] | 253 | chop $Prefix while (!($x =~ /^\Q$Prefix/)); |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | sub GetPrefix { |
| 257 | return $Prefix; |
| 258 | } |
| 259 | |
| 260 | ##----------------------------------------------------------------------------## |
| 261 | # UpdateInFilePath - Update the path in the report file. |
| 262 | ##----------------------------------------------------------------------------## |
| 263 | |
| 264 | sub UpdateInFilePath { |
| 265 | my $fname = shift; |
| 266 | my $regex = shift; |
| 267 | my $newtext = shift; |
Ted Kremenek | 63c2017 | 2008-08-04 17:34:06 +0000 | [diff] [blame] | 268 | |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 269 | open (RIN, $fname) or die "cannot open $fname"; |
Ted Kremenek | 63c2017 | 2008-08-04 17:34:06 +0000 | [diff] [blame] | 270 | open (ROUT, ">", "$fname.tmp") or die "cannot open $fname.tmp"; |
| 271 | |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 272 | while (<RIN>) { |
| 273 | s/$regex/$newtext/; |
| 274 | print ROUT $_; |
| 275 | } |
Ted Kremenek | 63c2017 | 2008-08-04 17:34:06 +0000 | [diff] [blame] | 276 | |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 277 | close (ROUT); |
| 278 | close (RIN); |
Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 279 | system("mv", "$fname.tmp", $fname); |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | ##----------------------------------------------------------------------------## |
Tom Care | 4f2b10b | 2010-09-30 01:12:05 +0000 | [diff] [blame] | 283 | # AddStatLine - Decode and insert a statistics line into the database. |
| 284 | ##----------------------------------------------------------------------------## |
| 285 | |
| 286 | sub AddStatLine { |
| 287 | my $Line = shift; |
| 288 | my $Stats = shift; |
| 289 | |
| 290 | print $Line . "\n"; |
| 291 | |
| 292 | my $Regex = qr/(.*?)\ :\ (.*?)\ ->\ Total\ CFGBlocks:\ (\d+)\ \|\ Unreachable |
Ted Kremenek | 6bdda82 | 2011-04-27 23:43:27 +0000 | [diff] [blame] | 293 | \ CFGBlocks:\ (\d+)\ \|\ Exhausted\ Block:\ (yes|no)\ \|\ Empty\ WorkList: |
Tom Care | 4f2b10b | 2010-09-30 01:12:05 +0000 | [diff] [blame] | 294 | \ (yes|no)/x; |
| 295 | |
| 296 | if ($Line !~ $Regex) { |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | # Create a hash of the interesting fields |
| 301 | my $Row = { |
| 302 | Filename => $1, |
| 303 | Function => $2, |
| 304 | Total => $3, |
| 305 | Unreachable => $4, |
| 306 | Aborted => $5, |
| 307 | Empty => $6 |
| 308 | }; |
| 309 | |
| 310 | # Add them to the stats array |
| 311 | push @$Stats, $Row; |
| 312 | } |
| 313 | |
| 314 | ##----------------------------------------------------------------------------## |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 315 | # ScanFile - Scan a report file for various identifying attributes. |
| 316 | ##----------------------------------------------------------------------------## |
| 317 | |
Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 318 | # Sometimes a source file is scanned more than once, and thus produces |
| 319 | # multiple error reports. We use a cache to solve this problem. |
| 320 | |
| 321 | my %AlreadyScanned; |
| 322 | |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 323 | sub ScanFile { |
| 324 | |
| 325 | my $Index = shift; |
| 326 | my $Dir = shift; |
| 327 | my $FName = shift; |
Tom Care | 4f2b10b | 2010-09-30 01:12:05 +0000 | [diff] [blame] | 328 | my $Stats = shift; |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 329 | |
Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 330 | # Compute a digest for the report file. Determine if we have already |
| 331 | # scanned a file that looks just like it. |
| 332 | |
| 333 | my $digest = ComputeDigest("$Dir/$FName"); |
| 334 | |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 335 | if (defined $AlreadyScanned{$digest}) { |
Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 336 | # Redundant file. Remove it. |
Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 337 | system ("rm", "-f", "$Dir/$FName"); |
Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 338 | return; |
| 339 | } |
| 340 | |
| 341 | $AlreadyScanned{$digest} = 1; |
| 342 | |
Ted Kremenek | 809709f | 2008-04-18 16:58:34 +0000 | [diff] [blame] | 343 | # 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] | 344 | system ("chmod", "644", "$Dir/$FName"); |
Ted Kremenek | 684bb09 | 2008-04-18 15:18:20 +0000 | [diff] [blame] | 345 | |
| 346 | # Scan the report file for tags. |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 347 | open(IN, "$Dir/$FName") or DieDiag("Cannot open '$Dir/$FName'\n"); |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 348 | |
Tom Care | 4f2b10b | 2010-09-30 01:12:05 +0000 | [diff] [blame] | 349 | my $BugType = ""; |
| 350 | my $BugFile = ""; |
| 351 | my $BugCategory = ""; |
| 352 | my $BugDescription = ""; |
| 353 | my $BugPathLength = 1; |
| 354 | my $BugLine = 0; |
Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 355 | |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 356 | while (<IN>) { |
Ted Kremenek | d658e67 | 2009-08-03 23:45:27 +0000 | [diff] [blame] | 357 | last if (/<!-- BUGMETAEND -->/); |
Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 358 | |
Ted Kremenek | a26ddab | 2009-01-27 01:53:39 +0000 | [diff] [blame] | 359 | if (/<!-- BUGTYPE (.*) -->$/) { |
| 360 | $BugType = $1; |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 361 | } |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 362 | elsif (/<!-- BUGFILE (.*) -->$/) { |
Ted Kremenek | 990c2f4 | 2008-12-03 19:19:23 +0000 | [diff] [blame] | 363 | $BugFile = abs_path($1); |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 364 | UpdatePrefix($BugFile); |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 365 | } |
| 366 | elsif (/<!-- BUGPATHLENGTH (.*) -->$/) { |
| 367 | $BugPathLength = $1; |
| 368 | } |
| 369 | elsif (/<!-- BUGLINE (.*) -->$/) { |
| 370 | $BugLine = $1; |
Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 371 | } |
| 372 | elsif (/<!-- BUGCATEGORY (.*) -->$/) { |
| 373 | $BugCategory = $1; |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 374 | } |
Tom Care | 4f2b10b | 2010-09-30 01:12:05 +0000 | [diff] [blame] | 375 | elsif (/<!-- BUGDESC (.*) -->$/) { |
| 376 | $BugDescription = $1; |
| 377 | } |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | close(IN); |
Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 381 | |
| 382 | if (!defined $BugCategory) { |
| 383 | $BugCategory = "Other"; |
| 384 | } |
Tom Care | 4f2b10b | 2010-09-30 01:12:05 +0000 | [diff] [blame] | 385 | |
| 386 | # Don't add internal statistics to the bug reports |
| 387 | if ($BugCategory =~ /statistics/i) { |
| 388 | AddStatLine($BugDescription, $Stats); |
| 389 | return; |
| 390 | } |
| 391 | |
Ted Kremenek | a26ddab | 2009-01-27 01:53:39 +0000 | [diff] [blame] | 392 | push @$Index,[ $FName, $BugCategory, $BugType, $BugFile, $BugLine, |
Ted Kremenek | 8198311 | 2008-09-28 04:13:09 +0000 | [diff] [blame] | 393 | $BugPathLength ]; |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | ##----------------------------------------------------------------------------## |
Ted Kremenek | 3ce1207 | 2008-09-22 17:50:47 +0000 | [diff] [blame] | 397 | # CopyFiles - Copy resource files to target directory. |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 398 | ##----------------------------------------------------------------------------## |
| 399 | |
Ted Kremenek | 3ce1207 | 2008-09-22 17:50:47 +0000 | [diff] [blame] | 400 | sub CopyFiles { |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 401 | |
| 402 | my $Dir = shift; |
Ted Kremenek | e15fa27 | 2008-10-13 21:46:42 +0000 | [diff] [blame] | 403 | |
| 404 | my $JS = Cwd::realpath("$RealBin/sorttable.js"); |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 405 | |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 406 | DieDiag("Cannot find 'sorttable.js'.\n") |
Ted Kremenek | e15fa27 | 2008-10-13 21:46:42 +0000 | [diff] [blame] | 407 | if (! -r $JS); |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 408 | |
Ted Kremenek | e15fa27 | 2008-10-13 21:46:42 +0000 | [diff] [blame] | 409 | system ("cp", $JS, "$Dir"); |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 410 | |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 411 | DieDiag("Could not copy 'sorttable.js' to '$Dir'.\n") |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 412 | if (! -r "$Dir/sorttable.js"); |
Ted Kremenek | 3ce1207 | 2008-09-22 17:50:47 +0000 | [diff] [blame] | 413 | |
Ted Kremenek | e15fa27 | 2008-10-13 21:46:42 +0000 | [diff] [blame] | 414 | my $CSS = Cwd::realpath("$RealBin/scanview.css"); |
| 415 | |
Ted Kremenek | 3ce1207 | 2008-09-22 17:50:47 +0000 | [diff] [blame] | 416 | DieDiag("Cannot find 'scanview.css'.\n") |
Ted Kremenek | e15fa27 | 2008-10-13 21:46:42 +0000 | [diff] [blame] | 417 | if (! -r $CSS); |
Ted Kremenek | 3ce1207 | 2008-09-22 17:50:47 +0000 | [diff] [blame] | 418 | |
Ted Kremenek | e15fa27 | 2008-10-13 21:46:42 +0000 | [diff] [blame] | 419 | system ("cp", $CSS, "$Dir"); |
Ted Kremenek | 3ce1207 | 2008-09-22 17:50:47 +0000 | [diff] [blame] | 420 | |
| 421 | DieDiag("Could not copy 'scanview.css' to '$Dir'.\n") |
Ted Kremenek | e15fa27 | 2008-10-13 21:46:42 +0000 | [diff] [blame] | 422 | if (! -r $CSS); |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | ##----------------------------------------------------------------------------## |
Tom Care | 4f2b10b | 2010-09-30 01:12:05 +0000 | [diff] [blame] | 426 | # CalcStats - Calculates visitation statistics and returns the string. |
| 427 | ##----------------------------------------------------------------------------## |
| 428 | |
| 429 | sub CalcStats { |
| 430 | my $Stats = shift; |
| 431 | |
| 432 | my $TotalBlocks = 0; |
| 433 | my $UnreachedBlocks = 0; |
| 434 | my $TotalFunctions = scalar(@$Stats); |
| 435 | my $BlockAborted = 0; |
| 436 | my $WorkListAborted = 0; |
| 437 | my $Aborted = 0; |
| 438 | |
| 439 | # Calculate the unique files |
| 440 | my $FilesHash = {}; |
| 441 | |
| 442 | foreach my $Row (@$Stats) { |
| 443 | $FilesHash->{$Row->{Filename}} = 1; |
| 444 | $TotalBlocks += $Row->{Total}; |
| 445 | $UnreachedBlocks += $Row->{Unreachable}; |
| 446 | $BlockAborted++ if $Row->{Aborted} eq 'yes'; |
| 447 | $WorkListAborted++ if $Row->{Empty} eq 'no'; |
| 448 | $Aborted++ if $Row->{Aborted} eq 'yes' || $Row->{Empty} eq 'no'; |
| 449 | } |
| 450 | |
| 451 | my $TotalFiles = scalar(keys(%$FilesHash)); |
| 452 | |
| 453 | # Calculations |
| 454 | my $PercentAborted = sprintf("%.2f", $Aborted / $TotalFunctions * 100); |
| 455 | my $PercentBlockAborted = sprintf("%.2f", $BlockAborted / $TotalFunctions |
| 456 | * 100); |
| 457 | my $PercentWorkListAborted = sprintf("%.2f", $WorkListAborted / |
| 458 | $TotalFunctions * 100); |
| 459 | my $PercentBlocksUnreached = sprintf("%.2f", $UnreachedBlocks / $TotalBlocks |
| 460 | * 100); |
| 461 | |
| 462 | my $StatsString = "Analyzed $TotalBlocks blocks in $TotalFunctions functions" |
| 463 | . " in $TotalFiles files\n" |
| 464 | . "$Aborted functions aborted early ($PercentAborted%)\n" |
| 465 | . "$BlockAborted had aborted blocks ($PercentBlockAborted%)\n" |
| 466 | . "$WorkListAborted had unfinished worklists ($PercentWorkListAborted%)\n" |
| 467 | . "$UnreachedBlocks blocks were never reached ($PercentBlocksUnreached%)\n"; |
| 468 | |
| 469 | return $StatsString; |
| 470 | } |
| 471 | |
| 472 | ##----------------------------------------------------------------------------## |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 473 | # Postprocess - Postprocess the results of an analysis scan. |
| 474 | ##----------------------------------------------------------------------------## |
| 475 | |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 476 | sub Postprocess { |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 477 | |
Tom Care | 4f2b10b | 2010-09-30 01:12:05 +0000 | [diff] [blame] | 478 | my $Dir = shift; |
| 479 | my $BaseDir = shift; |
| 480 | my $AnalyzerStats = shift; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 481 | |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 482 | die "No directory specified." if (!defined $Dir); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 483 | |
| 484 | if (! -d $Dir) { |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 485 | Diag("No bugs found.\n"); |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 486 | return 0; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | opendir(DIR, $Dir); |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 490 | my @files = grep { /^report-.*\.html$/ } readdir(DIR); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 491 | closedir(DIR); |
| 492 | |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 493 | if (scalar(@files) == 0 and ! -e "$Dir/failures") { |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 494 | Diag("Removing directory '$Dir' because it contains no reports.\n"); |
Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 495 | system ("rm", "-fR", $Dir); |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 496 | return 0; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 497 | } |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 498 | |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 499 | # Scan each report file and build an index. |
Tom Care | 4f2b10b | 2010-09-30 01:12:05 +0000 | [diff] [blame] | 500 | my @Index; |
| 501 | my @Stats; |
| 502 | foreach my $file (@files) { ScanFile(\@Index, $Dir, $file, \@Stats); } |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 503 | |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 504 | # Scan the failures directory and use the information in the .info files |
Ted Kremenek | d52e425 | 2008-08-25 20:45:07 +0000 | [diff] [blame] | 505 | # to update the common prefix directory. |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 506 | my @failures; |
| 507 | my @attributes_ignored; |
| 508 | if (-d "$Dir/failures") { |
| 509 | opendir(DIR, "$Dir/failures"); |
| 510 | @failures = grep { /[.]info.txt$/ && !/attribute_ignored/; } readdir(DIR); |
Ted Kremenek | d52e425 | 2008-08-25 20:45:07 +0000 | [diff] [blame] | 511 | closedir(DIR); |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 512 | opendir(DIR, "$Dir/failures"); |
| 513 | @attributes_ignored = grep { /^attribute_ignored/; } readdir(DIR); |
| 514 | closedir(DIR); |
| 515 | foreach my $file (@failures) { |
| 516 | open IN, "$Dir/failures/$file" or DieDiag("cannot open $file\n"); |
Ted Kremenek | d52e425 | 2008-08-25 20:45:07 +0000 | [diff] [blame] | 517 | my $Path = <IN>; |
| 518 | if (defined $Path) { UpdatePrefix($Path); } |
| 519 | close IN; |
| 520 | } |
| 521 | } |
| 522 | |
Ted Kremenek | 63c2017 | 2008-08-04 17:34:06 +0000 | [diff] [blame] | 523 | # Generate an index.html file. |
| 524 | my $FName = "$Dir/index.html"; |
| 525 | open(OUT, ">", $FName) or DieDiag("Cannot create file '$FName'\n"); |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 526 | |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 527 | # Print out the header. |
| 528 | |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 529 | print OUT <<ENDTEXT; |
| 530 | <html> |
| 531 | <head> |
Ted Kremenek | 7cba112 | 2008-09-22 01:35:58 +0000 | [diff] [blame] | 532 | <title>${HtmlTitle}</title> |
Ted Kremenek | f143545 | 2008-09-23 22:34:51 +0000 | [diff] [blame] | 533 | <link type="text/css" rel="stylesheet" href="scanview.css"/> |
Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 534 | <script src="sorttable.js"></script> |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 535 | <script language='javascript' type="text/javascript"> |
| 536 | function SetDisplay(RowClass, DisplayVal) |
| 537 | { |
| 538 | var Rows = document.getElementsByTagName("tr"); |
| 539 | for ( var i = 0 ; i < Rows.length; ++i ) { |
| 540 | if (Rows[i].className == RowClass) { |
| 541 | Rows[i].style.display = DisplayVal; |
| 542 | } |
| 543 | } |
| 544 | } |
Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 545 | |
Ted Kremenek | 2350a46 | 2008-10-28 19:56:52 +0000 | [diff] [blame] | 546 | function CopyCheckedStateToCheckButtons(SummaryCheckButton) { |
| 547 | var Inputs = document.getElementsByTagName("input"); |
| 548 | for ( var i = 0 ; i < Inputs.length; ++i ) { |
| 549 | if (Inputs[i].type == "checkbox") { |
| 550 | if(Inputs[i] != SummaryCheckButton) { |
| 551 | Inputs[i].checked = SummaryCheckButton.checked; |
| 552 | Inputs[i].onclick(); |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
Ted Kremenek | 999e120 | 2008-10-28 20:09:57 +0000 | [diff] [blame] | 558 | function returnObjById( id ) { |
| 559 | if (document.getElementById) |
| 560 | var returnVar = document.getElementById(id); |
| 561 | else if (document.all) |
| 562 | var returnVar = document.all[id]; |
| 563 | else if (document.layers) |
| 564 | var returnVar = document.layers[id]; |
| 565 | return returnVar; |
| 566 | } |
| 567 | |
| 568 | var NumUnchecked = 0; |
| 569 | |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 570 | function ToggleDisplay(CheckButton, ClassName) { |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 571 | if (CheckButton.checked) { |
| 572 | SetDisplay(ClassName, ""); |
Ted Kremenek | 999e120 | 2008-10-28 20:09:57 +0000 | [diff] [blame] | 573 | if (--NumUnchecked == 0) { |
| 574 | returnObjById("AllBugsCheck").checked = true; |
| 575 | } |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 576 | } |
| 577 | else { |
| 578 | SetDisplay(ClassName, "none"); |
Ted Kremenek | 999e120 | 2008-10-28 20:09:57 +0000 | [diff] [blame] | 579 | NumUnchecked++; |
| 580 | returnObjById("AllBugsCheck").checked = false; |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | </script> |
Ted Kremenek | 1d1abb1 | 2008-09-22 17:52:58 +0000 | [diff] [blame] | 584 | <!-- SUMMARYENDHEAD --> |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 585 | </head> |
| 586 | <body> |
Ted Kremenek | 7cba112 | 2008-09-22 01:35:58 +0000 | [diff] [blame] | 587 | <h1>${HtmlTitle}</h1> |
| 588 | |
| 589 | <table> |
| 590 | <tr><th>User:</th><td>${UserName}\@${HostName}</td></tr> |
| 591 | <tr><th>Working Directory:</th><td>${CurrentDir}</td></tr> |
| 592 | <tr><th>Command Line:</th><td>${CmdArgs}</td></tr> |
| 593 | <tr><th>Date:</th><td>${Date}</td></tr> |
| 594 | ENDTEXT |
| 595 | |
| 596 | print OUT "<tr><th>Version:</th><td>${BuildName} (${BuildDate})</td></tr>\n" |
| 597 | if (defined($BuildName) && defined($BuildDate)); |
| 598 | |
| 599 | print OUT <<ENDTEXT; |
| 600 | </table> |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 601 | ENDTEXT |
| 602 | |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 603 | if (scalar(@files)) { |
| 604 | # Print out the summary table. |
| 605 | my %Totals; |
Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 606 | |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 607 | for my $row ( @Index ) { |
Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 608 | my $bug_type = ($row->[2]); |
| 609 | my $bug_category = ($row->[1]); |
| 610 | my $key = "$bug_category:$bug_type"; |
| 611 | |
| 612 | if (!defined $Totals{$key}) { $Totals{$key} = [1,$bug_category,$bug_type]; } |
| 613 | else { $Totals{$key}->[0]++; } |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 614 | } |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 615 | |
Ted Kremenek | 7cba112 | 2008-09-22 01:35:58 +0000 | [diff] [blame] | 616 | print OUT "<h2>Bug Summary</h2>"; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 617 | |
| 618 | if (defined $BuildName) { |
| 619 | 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] | 620 | } |
Ted Kremenek | f4cdf41 | 2008-05-23 18:17:05 +0000 | [diff] [blame] | 621 | |
Ted Kremenek | 2350a46 | 2008-10-28 19:56:52 +0000 | [diff] [blame] | 622 | my $TotalBugs = scalar(@Index); |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 623 | print OUT <<ENDTEXT; |
Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 624 | <table> |
| 625 | <thead><tr><td>Bug Type</td><td>Quantity</td><td class="sorttable_nosort">Display?</td></tr></thead> |
Ted Kremenek | 999e120 | 2008-10-28 20:09:57 +0000 | [diff] [blame] | 626 | <tr style="font-weight:bold"><td class="SUMM_DESC">All Bugs</td><td class="Q">$TotalBugs</td><td><center><input type="checkbox" id="AllBugsCheck" onClick="CopyCheckedStateToCheckButtons(this);" checked/></center></td></tr> |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 627 | ENDTEXT |
| 628 | |
Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 629 | my $last_category; |
| 630 | |
| 631 | for my $key ( |
| 632 | sort { |
| 633 | my $x = $Totals{$a}; |
| 634 | my $y = $Totals{$b}; |
| 635 | my $res = $x->[1] cmp $y->[1]; |
| 636 | $res = $x->[2] cmp $y->[2] if ($res == 0); |
| 637 | $res |
| 638 | } keys %Totals ) |
| 639 | { |
| 640 | my $val = $Totals{$key}; |
| 641 | my $category = $val->[1]; |
| 642 | if (!defined $last_category or $last_category ne $category) { |
| 643 | $last_category = $category; |
| 644 | print OUT "<tr><th>$category</th><th colspan=2></th></tr>\n"; |
| 645 | } |
| 646 | my $x = lc $key; |
| 647 | $x =~ s/[ ,'":\/()]+/_/g; |
| 648 | print OUT "<tr><td class=\"SUMM_DESC\">"; |
| 649 | print OUT $val->[2]; |
Ted Kremenek | 2350a46 | 2008-10-28 19:56:52 +0000 | [diff] [blame] | 650 | print OUT "</td><td class=\"Q\">"; |
Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 651 | print OUT $val->[0]; |
| 652 | 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] | 653 | } |
Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 654 | |
| 655 | # Print out the table of errors. |
| 656 | |
| 657 | print OUT <<ENDTEXT; |
| 658 | </table> |
Ted Kremenek | 7cba112 | 2008-09-22 01:35:58 +0000 | [diff] [blame] | 659 | <h2>Reports</h2> |
Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 660 | |
| 661 | <table class="sortable" style="table-layout:automatic"> |
| 662 | <thead><tr> |
| 663 | <td>Bug Group</td> |
| 664 | <td class="sorttable_sorted">Bug Type<span id="sorttable_sortfwdind"> ▾</span></td> |
Ted Kremenek | bba1cf5 | 2008-04-03 05:50:51 +0000 | [diff] [blame] | 665 | <td>File</td> |
Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 666 | <td class="Q">Line</td> |
Ted Kremenek | 8198311 | 2008-09-28 04:13:09 +0000 | [diff] [blame] | 667 | <td class="Q">Path Length</td> |
Ted Kremenek | 2645c77 | 2008-07-07 16:58:44 +0000 | [diff] [blame] | 668 | <td class="sorttable_nosort"></td> |
Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 669 | <!-- REPORTBUGCOL --> |
| 670 | </tr></thead> |
| 671 | <tbody> |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 672 | ENDTEXT |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 673 | |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 674 | my $prefix = GetPrefix(); |
| 675 | my $regex; |
| 676 | my $InFileRegex; |
| 677 | my $InFilePrefix = "File:</td><td>"; |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 678 | |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 679 | if (defined $prefix) { |
| 680 | $regex = qr/^\Q$prefix\E/is; |
| 681 | $InFileRegex = qr/\Q$InFilePrefix$prefix\E/is; |
| 682 | } |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 683 | |
Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 684 | for my $row ( sort { $a->[2] cmp $b->[2] } @Index ) { |
| 685 | my $x = "$row->[1]:$row->[2]"; |
| 686 | $x = lc $x; |
| 687 | $x =~ s/[ ,'":\/()]+/_/g; |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 688 | |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 689 | my $ReportFile = $row->[0]; |
Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 690 | |
| 691 | print OUT "<tr class=\"bt_$x\">"; |
| 692 | print OUT "<td class=\"DESC\">"; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 693 | print OUT $row->[1]; |
Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 694 | print OUT "</td>"; |
| 695 | print OUT "<td class=\"DESC\">"; |
| 696 | print OUT $row->[2]; |
| 697 | print OUT "</td>"; |
| 698 | |
| 699 | # Update the file prefix. |
| 700 | my $fname = $row->[3]; |
Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 701 | |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 702 | if (defined $regex) { |
| 703 | $fname =~ s/$regex//; |
| 704 | UpdateInFilePath("$Dir/$ReportFile", $InFileRegex, $InFilePrefix) |
| 705 | } |
Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 706 | |
Ted Kremenek | 91639ef | 2008-09-22 17:42:31 +0000 | [diff] [blame] | 707 | print OUT "<td>"; |
Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 708 | my @fname = split /\//,$fname; |
| 709 | if ($#fname > 0) { |
| 710 | while ($#fname >= 0) { |
| 711 | my $x = shift @fname; |
| 712 | print OUT $x; |
| 713 | if ($#fname >= 0) { |
| 714 | print OUT "<span class=\"W\"> </span>/"; |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | else { |
| 719 | print OUT $fname; |
Ted Kremenek | 91639ef | 2008-09-22 17:42:31 +0000 | [diff] [blame] | 720 | } |
Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 721 | print OUT "</td>"; |
| 722 | |
| 723 | # Print out the quantities. |
Ted Kremenek | 8198311 | 2008-09-28 04:13:09 +0000 | [diff] [blame] | 724 | for my $j ( 4 .. 5 ) { |
Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 725 | print OUT "<td class=\"Q\">$row->[$j]</td>"; |
| 726 | } |
| 727 | |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 728 | # Print the rest of the columns. |
Ted Kremenek | 8198311 | 2008-09-28 04:13:09 +0000 | [diff] [blame] | 729 | for (my $j = 6; $j <= $#{$row}; ++$j) { |
Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 730 | print OUT "<td>$row->[$j]</td>" |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 731 | } |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 732 | |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 733 | # Emit the "View" link. |
Ted Kremenek | 68005dd | 2008-09-22 17:39:18 +0000 | [diff] [blame] | 734 | print OUT "<td><a href=\"$ReportFile#EndPath\">View Report</a></td>"; |
Ted Kremenek | 3cea9ee | 2008-07-30 17:58:08 +0000 | [diff] [blame] | 735 | |
Daniel Dunbar | e43038e | 2008-09-19 23:18:44 +0000 | [diff] [blame] | 736 | # Emit REPORTBUG markers. |
Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 737 | print OUT "\n<!-- REPORTBUG id=\"$ReportFile\" -->\n"; |
Daniel Dunbar | e43038e | 2008-09-19 23:18:44 +0000 | [diff] [blame] | 738 | |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 739 | # End the row. |
| 740 | print OUT "</tr>\n"; |
| 741 | } |
| 742 | |
Ted Kremenek | ebb7413 | 2008-09-21 06:58:09 +0000 | [diff] [blame] | 743 | print OUT "</tbody>\n</table>\n\n"; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 746 | if (scalar (@failures) || scalar(@attributes_ignored)) { |
| 747 | print OUT "<h2>Analyzer Failures</h2>\n"; |
| 748 | |
| 749 | if (scalar @attributes_ignored) { |
| 750 | print OUT "The analyzer's parser ignored the following attributes:<p>\n"; |
| 751 | print OUT "<table>\n"; |
| 752 | print OUT "<thead><tr><td>Attribute</td><td>Source File</td><td>Preprocessed File</td><td>STDERR Output</td></tr></thead>\n"; |
| 753 | foreach my $file (sort @attributes_ignored) { |
| 754 | die "cannot demangle attribute name\n" if (! ($file =~ /^attribute_ignored_(.+).txt/)); |
| 755 | my $attribute = $1; |
| 756 | # Open the attribute file to get the first file that failed. |
| 757 | next if (!open (ATTR, "$Dir/failures/$file")); |
| 758 | my $ppfile = <ATTR>; |
| 759 | chomp $ppfile; |
| 760 | close ATTR; |
| 761 | next if (! -e "$Dir/failures/$ppfile"); |
| 762 | # Open the info file and get the name of the source file. |
| 763 | open (INFO, "$Dir/failures/$ppfile.info.txt") or |
| 764 | die "Cannot open $Dir/failures/$ppfile.info.txt\n"; |
| 765 | my $srcfile = <INFO>; |
| 766 | chomp $srcfile; |
| 767 | close (INFO); |
| 768 | # Print the information in the table. |
| 769 | my $prefix = GetPrefix(); |
| 770 | if (defined $prefix) { $srcfile =~ s/^\Q$prefix//; } |
| 771 | print OUT "<tr><td>$attribute</td><td>$srcfile</td><td><a href=\"failures/$ppfile\">$ppfile</a></td><td><a href=\"failures/$ppfile.stderr.txt\">$ppfile.stderr.txt</a></td></tr>\n"; |
| 772 | my $ppfile_clang = $ppfile; |
| 773 | $ppfile_clang =~ s/[.](.+)$/.clang.$1/; |
| 774 | print OUT " <!-- REPORTPROBLEM src=\"$srcfile\" file=\"failures/$ppfile\" clangfile=\"failures/$ppfile_clang\" stderr=\"failures/$ppfile.stderr.txt\" info=\"failures/$ppfile.info.txt\" -->\n"; |
| 775 | } |
| 776 | print OUT "</table>\n"; |
| 777 | } |
| 778 | |
| 779 | if (scalar @failures) { |
| 780 | print OUT "<p>The analyzer had problems processing the following files:</p>\n"; |
| 781 | print OUT "<table>\n"; |
| 782 | print OUT "<thead><tr><td>Problem</td><td>Source File</td><td>Preprocessed File</td><td>STDERR Output</td></tr></thead>\n"; |
| 783 | foreach my $file (sort @failures) { |
Ted Kremenek | 82a1253 | 2008-09-25 00:25:16 +0000 | [diff] [blame] | 784 | $file =~ /(.+).info.txt$/; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 785 | # Get the preprocessed file. |
| 786 | my $ppfile = $1; |
| 787 | # Open the info file and get the name of the source file. |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 788 | open (INFO, "$Dir/failures/$file") or |
| 789 | die "Cannot open $Dir/failures/$file\n"; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 790 | my $srcfile = <INFO>; |
Ted Kremenek | 5d31f83 | 2008-08-18 18:38:29 +0000 | [diff] [blame] | 791 | chomp $srcfile; |
| 792 | my $problem = <INFO>; |
| 793 | chomp $problem; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 794 | close (INFO); |
| 795 | # Print the information in the table. |
Ted Kremenek | d52e425 | 2008-08-25 20:45:07 +0000 | [diff] [blame] | 796 | my $prefix = GetPrefix(); |
Ted Kremenek | 9f9b1fd | 2008-09-12 22:49:36 +0000 | [diff] [blame] | 797 | if (defined $prefix) { $srcfile =~ s/^\Q$prefix//; } |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 798 | print OUT "<tr><td>$problem</td><td>$srcfile</td><td><a href=\"failures/$ppfile\">$ppfile</a></td><td><a href=\"failures/$ppfile.stderr.txt\">$ppfile.stderr.txt</a></td></tr>\n"; |
Daniel Dunbar | ce723ce | 2008-09-25 01:10:50 +0000 | [diff] [blame] | 799 | my $ppfile_clang = $ppfile; |
| 800 | $ppfile_clang =~ s/[.](.+)$/.clang.$1/; |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 801 | print OUT " <!-- REPORTPROBLEM src=\"$srcfile\" file=\"failures/$ppfile\" clangfile=\"failures/$ppfile_clang\" stderr=\"failures/$ppfile.stderr.txt\" info=\"failures/$ppfile.info.txt\" -->\n"; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 802 | } |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 803 | print OUT "</table>\n"; |
| 804 | } |
Nico Weber | e2c8663 | 2011-08-28 11:50:56 +0000 | [diff] [blame] | 805 | print OUT "<p>Please consider submitting preprocessed files as <a href=\"http://clang-analyzer.llvm.org/filing_bugs.html\">bug reports</a>. <!-- REPORTCRASHES --> </p>\n"; |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 808 | print OUT "</body></html>\n"; |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 809 | close(OUT); |
Ted Kremenek | 3ce1207 | 2008-09-22 17:50:47 +0000 | [diff] [blame] | 810 | CopyFiles($Dir); |
Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 811 | |
| 812 | # Make sure $Dir and $BaseDir are world readable/executable. |
| 813 | system("chmod", "755", $Dir); |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 814 | if (defined $BaseDir) { system("chmod", "755", $BaseDir); } |
Ted Kremenek | 20161e9 | 2008-07-15 20:18:21 +0000 | [diff] [blame] | 815 | |
Tom Care | 4f2b10b | 2010-09-30 01:12:05 +0000 | [diff] [blame] | 816 | # Print statistics |
| 817 | print CalcStats(\@Stats) if $AnalyzerStats; |
| 818 | |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 819 | my $Num = scalar(@Index); |
Ted Kremenek | 150c212 | 2008-07-11 19:15:05 +0000 | [diff] [blame] | 820 | Diag("$Num bugs found.\n"); |
| 821 | if ($Num > 0 && -r "$Dir/index.html") { |
Ted Kremenek | 5950b3f | 2008-09-22 06:47:01 +0000 | [diff] [blame] | 822 | Diag("Run 'scan-view $Dir' to examine bug reports.\n"); |
Ted Kremenek | 150c212 | 2008-07-11 19:15:05 +0000 | [diff] [blame] | 823 | } |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 824 | |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 825 | DiagCrashes($Dir) if (scalar @failures || scalar @attributes_ignored); |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 826 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 827 | return $Num; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | ##----------------------------------------------------------------------------## |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 831 | # RunBuildCommand - Run the build command. |
| 832 | ##----------------------------------------------------------------------------## |
| 833 | |
Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 834 | sub AddIfNotPresent { |
| 835 | my $Args = shift; |
| 836 | my $Arg = shift; |
| 837 | my $found = 0; |
| 838 | |
| 839 | foreach my $k (@$Args) { |
| 840 | if ($k eq $Arg) { |
| 841 | $found = 1; |
| 842 | last; |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | if ($found == 0) { |
| 847 | push @$Args, $Arg; |
| 848 | } |
| 849 | } |
| 850 | |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 851 | sub RunBuildCommand { |
| 852 | |
| 853 | my $Args = shift; |
Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame] | 854 | my $IgnoreErrors = shift; |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 855 | my $Cmd = $Args->[0]; |
Ted Kremenek | 6195c37 | 2008-06-02 21:52:47 +0000 | [diff] [blame] | 856 | my $CCAnalyzer = shift; |
Ted Kremenek | 524c308 | 2010-03-27 00:20:01 +0000 | [diff] [blame] | 857 | my $CXXAnalyzer = shift; |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 858 | |
Ted Kremenek | 3301cb1 | 2008-06-30 18:18:16 +0000 | [diff] [blame] | 859 | # Get only the part of the command after the last '/'. |
| 860 | if ($Cmd =~ /\/([^\/]+)$/) { |
| 861 | $Cmd = $1; |
| 862 | } |
| 863 | |
Ted Kremenek | 92548fe | 2008-11-19 01:46:21 +0000 | [diff] [blame] | 864 | if ($Cmd =~ /(.*\/?gcc[^\/]*$)/ or |
| 865 | $Cmd =~ /(.*\/?cc[^\/]*$)/ or |
| 866 | $Cmd =~ /(.*\/?llvm-gcc[^\/]*$)/ or |
Ted Kremenek | 05acf8b | 2010-10-16 00:29:16 +0000 | [diff] [blame] | 867 | $Cmd =~ /(.*\/?clang$)/ or |
Ted Kremenek | 92548fe | 2008-11-19 01:46:21 +0000 | [diff] [blame] | 868 | $Cmd =~ /(.*\/?ccc-analyzer[^\/]*$)/) { |
| 869 | |
| 870 | if (!($Cmd =~ /ccc-analyzer/) and !defined $ENV{"CCC_CC"}) { |
Ted Kremenek | 51365b5 | 2009-12-15 02:35:54 +0000 | [diff] [blame] | 871 | $ENV{"CCC_CC"} = $1; |
Ted Kremenek | 92548fe | 2008-11-19 01:46:21 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 874 | shift @$Args; |
Ted Kremenek | 6195c37 | 2008-06-02 21:52:47 +0000 | [diff] [blame] | 875 | unshift @$Args, $CCAnalyzer; |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 876 | } |
Ted Kremenek | 51365b5 | 2009-12-15 02:35:54 +0000 | [diff] [blame] | 877 | elsif ($Cmd =~ /(.*\/?g\+\+[^\/]*$)/ or |
| 878 | $Cmd =~ /(.*\/?c\+\+[^\/]*$)/ or |
| 879 | $Cmd =~ /(.*\/?llvm-g\+\+[^\/]*$)/ or |
Ted Kremenek | 05acf8b | 2010-10-16 00:29:16 +0000 | [diff] [blame] | 880 | $Cmd =~ /(.*\/?clang\+\+$)/ or |
Ted Kremenek | 51365b5 | 2009-12-15 02:35:54 +0000 | [diff] [blame] | 881 | $Cmd =~ /(.*\/?c\+\+-analyzer[^\/]*$)/) { |
| 882 | if (!($Cmd =~ /c\+\+-analyzer/) and !defined $ENV{"CCC_CXX"}) { |
| 883 | $ENV{"CCC_CXX"} = $1; |
| 884 | } |
| 885 | shift @$Args; |
Ted Kremenek | 524c308 | 2010-03-27 00:20:01 +0000 | [diff] [blame] | 886 | unshift @$Args, $CXXAnalyzer; |
Ted Kremenek | 51365b5 | 2009-12-15 02:35:54 +0000 | [diff] [blame] | 887 | } |
Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame] | 888 | elsif ($IgnoreErrors) { |
| 889 | if ($Cmd eq "make" or $Cmd eq "gmake") { |
Ted Kremenek | 6fba85d | 2009-12-11 23:22:52 +0000 | [diff] [blame] | 890 | AddIfNotPresent($Args, "CC=$CCAnalyzer"); |
Ted Kremenek | 524c308 | 2010-03-27 00:20:01 +0000 | [diff] [blame] | 891 | AddIfNotPresent($Args, "CXX=$CXXAnalyzer"); |
Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 892 | AddIfNotPresent($Args,"-k"); |
Ted Kremenek | 8912b54 | 2008-05-13 21:28:02 +0000 | [diff] [blame] | 893 | AddIfNotPresent($Args,"-i"); |
Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame] | 894 | } |
| 895 | elsif ($Cmd eq "xcodebuild") { |
Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 896 | AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES"); |
Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame] | 897 | } |
Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 900 | if ($Cmd eq "xcodebuild") { |
Ted Kremenek | 87752b2 | 2009-05-15 21:14:16 +0000 | [diff] [blame] | 901 | # Check if using iPhone SDK 3.0 (simulator). If so the compiler being |
| 902 | # used should be gcc-4.2. |
| 903 | if (!defined $ENV{"CCC_CC"}) { |
| 904 | for (my $i = 0 ; $i < scalar(@$Args); ++$i) { |
| 905 | if ($Args->[$i] eq "-sdk" && $i + 1 < scalar(@$Args)) { |
| 906 | if (@$Args[$i+1] =~ /^iphonesimulator3/) { |
| 907 | $ENV{"CCC_CC"} = "gcc-4.2"; |
Ted Kremenek | 51365b5 | 2009-12-15 02:35:54 +0000 | [diff] [blame] | 908 | $ENV{"CCC_CXX"} = "g++-4.2"; |
Ted Kremenek | 87752b2 | 2009-05-15 21:14:16 +0000 | [diff] [blame] | 909 | } |
| 910 | } |
| 911 | } |
| 912 | } |
| 913 | |
Ted Kremenek | cfd4c7b | 2008-05-23 22:18:16 +0000 | [diff] [blame] | 914 | # Disable PCH files until clang supports them. |
| 915 | AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO"); |
Ted Kremenek | 915e972 | 2008-05-27 23:18:07 +0000 | [diff] [blame] | 916 | |
| 917 | # When 'CC' is set, xcodebuild uses it to do all linking, even if we are |
| 918 | # linking C++ object files. Set 'LDPLUSPLUS' so that xcodebuild uses 'g++' |
Ted Kremenek | 524c308 | 2010-03-27 00:20:01 +0000 | [diff] [blame] | 919 | # (via c++-analyzer) when linking such files. |
| 920 | $ENV{"LDPLUSPLUS"} = $CXXAnalyzer; |
Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 921 | } |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 922 | |
Ted Kremenek | 5a4ddaf | 2008-08-25 20:10:45 +0000 | [diff] [blame] | 923 | return (system(@$Args) >> 8); |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 926 | ##----------------------------------------------------------------------------## |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 927 | # DisplayHelp - Utility function to display all help options. |
| 928 | ##----------------------------------------------------------------------------## |
| 929 | |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 930 | sub DisplayHelp { |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 931 | |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 932 | print <<ENDTEXT; |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 933 | USAGE: $Prog [options] <build command> [build options] |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 934 | |
Ted Kremenek | f4cdf41 | 2008-05-23 18:17:05 +0000 | [diff] [blame] | 935 | ENDTEXT |
| 936 | |
Ted Kremenek | fc1d340 | 2008-08-04 18:15:26 +0000 | [diff] [blame] | 937 | if (defined $BuildName) { |
Ted Kremenek | f4cdf41 | 2008-05-23 18:17:05 +0000 | [diff] [blame] | 938 | print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n"; |
| 939 | } |
| 940 | |
| 941 | print <<ENDTEXT; |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 942 | OPTIONS: |
| 943 | |
Ted Kremenek | e15fa27 | 2008-10-13 21:46:42 +0000 | [diff] [blame] | 944 | -analyze-headers - Also analyze functions in #included files. |
Ted Kremenek | 8382cf5 | 2009-11-13 18:46:29 +0000 | [diff] [blame] | 945 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 946 | -o - Target directory for HTML report files. Subdirectories |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 947 | will be created as needed to represent separate "runs" of |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 948 | the analyzer. If this option is not specified, a directory |
Ted Kremenek | ffda0b4 | 2008-10-31 05:48:42 +0000 | [diff] [blame] | 949 | is created in /tmp (TMPDIR on Mac OS X) to store the reports. |
Ted Kremenek | db4f5f2 | 2008-11-04 00:02:53 +0000 | [diff] [blame] | 950 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 951 | -h - Display this message. |
| 952 | --help |
Ted Kremenek | 1262fc4 | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 953 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 954 | -k - Add a "keep on going" option to the specified build command. |
| 955 | --keep-going This option currently supports make and xcodebuild. |
Ted Kremenek | f02e8db | 2008-04-02 16:41:25 +0000 | [diff] [blame] | 956 | This is a convenience option; one can specify this |
| 957 | behavior directly using build options. |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 958 | |
Ted Kremenek | 7cba112 | 2008-09-22 01:35:58 +0000 | [diff] [blame] | 959 | --html-title [title] - Specify the title used on generated HTML pages. |
| 960 | --html-title=[title] If not specified, a default title will be used. |
| 961 | |
Ted Kremenek | db4f5f2 | 2008-11-04 00:02:53 +0000 | [diff] [blame] | 962 | -plist - By default the output of scan-build is a set of HTML files. |
| 963 | This option outputs the results as a set of .plist files. |
| 964 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 965 | --status-bugs - By default, the exit status of $Prog is the same as the |
| 966 | executed build command. Specifying this option causes the |
| 967 | exit status of $Prog to be 1 if it found potential bugs |
| 968 | and 0 otherwise. |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 969 | |
Ted Kremenek | 386c693 | 2008-09-03 17:59:35 +0000 | [diff] [blame] | 970 | --use-cc [compiler path] - By default, $Prog uses 'gcc' to compile and link |
| 971 | --use-cc=[compiler path] your C and Objective-C code. Use this option |
| 972 | to specify an alternate compiler. |
| 973 | |
| 974 | --use-c++ [compiler path] - By default, $Prog uses 'g++' to compile and link |
| 975 | --use-c++=[compiler path] your C++ and Objective-C++ code. Use this option |
| 976 | to specify an alternate compiler. |
Ted Kremenek | f17ef3c | 2008-08-21 21:47:09 +0000 | [diff] [blame] | 977 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 978 | -v - Verbose output from $Prog and the analyzer. |
Ted Kremenek | 386c693 | 2008-09-03 17:59:35 +0000 | [diff] [blame] | 979 | A second and third '-v' increases verbosity. |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 980 | |
| 981 | -V - View analysis results in a web browser when the build |
| 982 | --view completes. |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 983 | |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 984 | ADVANCED OPTIONS: |
| 985 | |
Ted Kremenek | 9f4ecb3 | 2009-02-20 21:49:22 +0000 | [diff] [blame] | 986 | -constraints [model] - Specify the contraint engine used by the analyzer. |
| 987 | By default the 'range' model is used. Specifying |
| 988 | 'basic' uses a simpler, less powerful constraint model |
Ted Kremenek | d4c7684 | 2009-02-21 04:46:41 +0000 | [diff] [blame] | 989 | used by checker-0.160 and earlier. |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 990 | |
| 991 | -store [model] - Specify the store model used by the analyzer. By default, |
Ted Kremenek | b8bb3e7 | 2009-09-25 05:55:59 +0000 | [diff] [blame] | 992 | the 'region' store model is used. 'region' specifies a field- |
| 993 | sensitive store model. Users can also specify 'basic', which |
| 994 | is far less precise but can more quickly analyze code. |
| 995 | 'basic' was the default store model for checker-0.221 and |
| 996 | earlier. |
| 997 | |
Ted Kremenek | e600bed | 2009-07-30 23:55:19 +0000 | [diff] [blame] | 998 | -no-failure-reports - Do not create a 'failures' subdirectory that includes |
| 999 | analyzer crash reports and preprocessed source files. |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 1000 | |
Tom Care | 4f2b10b | 2010-09-30 01:12:05 +0000 | [diff] [blame] | 1001 | -stats - Generates visitation statistics for the project being analyzed. |
| 1002 | |
| 1003 | -maxloop N - specifiy the number of times a block can be visited before giving |
Ted Kremenek | 09fbf29 | 2011-04-12 21:47:00 +0000 | [diff] [blame] | 1004 | up. Default is 4. Increase for more comprehensive coverage at a |
Tom Care | 4f2b10b | 2010-09-30 01:12:05 +0000 | [diff] [blame] | 1005 | cost of speed. |
Ted Kremenek | 09fbf29 | 2011-04-12 21:47:00 +0000 | [diff] [blame] | 1006 | |
| 1007 | CONTROLLING CHECKERS: |
| 1008 | |
| 1009 | A default group of checkers are always run unless explicitly disabled. |
| 1010 | Checkers may be enabled/disabled using the following options: |
| 1011 | |
| 1012 | -enable-checker [checker name] |
| 1013 | -disable-checker [checker name] |
Ted Kremenek | d52e425 | 2008-08-25 20:45:07 +0000 | [diff] [blame] | 1014 | ENDTEXT |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 1015 | |
Ted Kremenek | ba90e8a | 2011-02-25 22:00:40 +0000 | [diff] [blame] | 1016 | # Query clang for list of checkers that are enabled. |
| 1017 | my %EnabledCheckers; |
| 1018 | foreach my $lang ("c", "objective-c", "objective-c++", "c++") { |
| 1019 | pipe(FROM_CHILD, TO_PARENT); |
| 1020 | my $pid = fork(); |
| 1021 | if ($pid == 0) { |
| 1022 | close FROM_CHILD; |
| 1023 | open(STDOUT,">&", \*TO_PARENT); |
| 1024 | open(STDERR,">&", \*TO_PARENT); |
| 1025 | exec $Clang, ('--analyze', '-x', $lang, '-', '-###'); |
| 1026 | } |
| 1027 | close(TO_PARENT); |
| 1028 | while(<FROM_CHILD>) { |
| 1029 | foreach my $val (split /\s+/) { |
| 1030 | $val =~ s/\"//g; |
| 1031 | if ($val =~ /-analyzer-checker\=([^\s]+)/) { |
| 1032 | $EnabledCheckers{$1} = 1; |
| 1033 | } |
| 1034 | } |
| 1035 | } |
| 1036 | waitpid($pid,0); |
| 1037 | close(FROM_CHILD); |
| 1038 | } |
| 1039 | |
| 1040 | # Query clang for complete list of checkers. |
| 1041 | pipe(FROM_CHILD, TO_PARENT); |
| 1042 | my $pid = fork(); |
| 1043 | if ($pid == 0) { |
| 1044 | close FROM_CHILD; |
| 1045 | open(STDOUT,">&", \*TO_PARENT); |
| 1046 | open(STDERR,">&", \*TO_PARENT); |
| 1047 | exec $Clang, ('-cc1', '-analyzer-checker-help'); |
| 1048 | } |
| 1049 | close(TO_PARENT); |
| 1050 | my $foundCheckers = 0; |
| 1051 | while(<FROM_CHILD>) { |
| 1052 | if (/CHECKERS:/) { |
| 1053 | $foundCheckers = 1; |
| 1054 | last; |
| 1055 | } |
| 1056 | } |
| 1057 | if (!$foundCheckers) { |
| 1058 | print " *** Could not query Clang for the list of available checkers."; |
| 1059 | } |
| 1060 | else { |
| 1061 | print("\nAVAILABLE CHECKERS:\n\n"); |
| 1062 | my $skip = 0; |
| 1063 | while(<FROM_CHILD>) { |
Ted Kremenek | 4cd6ea9 | 2011-04-05 00:21:49 +0000 | [diff] [blame] | 1064 | if (/experimental/) { |
Ted Kremenek | ba90e8a | 2011-02-25 22:00:40 +0000 | [diff] [blame] | 1065 | $skip = 1; |
| 1066 | next; |
| 1067 | } |
| 1068 | if ($skip) { |
| 1069 | next if (!/^\s\s[^\s]/); |
| 1070 | $skip = 0; |
| 1071 | } |
| 1072 | s/^\s\s//; |
| 1073 | if (/^([^\s]+)/) { |
| 1074 | # Is the checker enabled? |
| 1075 | my $checker = $1; |
| 1076 | my $enabled = 0; |
| 1077 | my $aggregate = ""; |
| 1078 | foreach my $domain (split /\./, $checker) { |
| 1079 | $aggregate .= $domain; |
| 1080 | if ($EnabledCheckers{$aggregate}) { |
| 1081 | $enabled =1; |
| 1082 | last; |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | if ($enabled) { |
| 1087 | print " + "; |
| 1088 | } |
| 1089 | else { |
| 1090 | print " "; |
| 1091 | } |
| 1092 | } |
| 1093 | else { |
| 1094 | print " "; |
| 1095 | } |
| 1096 | print $_; |
| 1097 | } |
| 1098 | } |
| 1099 | waitpid($pid,0); |
| 1100 | close(FROM_CHILD); |
Ted Kremenek | 7fe679f | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 1101 | |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 1102 | print <<ENDTEXT |
| 1103 | |
Ted Kremenek | ba90e8a | 2011-02-25 22:00:40 +0000 | [diff] [blame] | 1104 | NOTE: "+" indicates that an analysis is enabled by default. |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 1105 | |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 1106 | BUILD OPTIONS |
| 1107 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 1108 | You can specify any build option acceptable to the build command. |
Ted Kremenek | 39eefde | 2008-04-02 16:47:27 +0000 | [diff] [blame] | 1109 | |
Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 1110 | EXAMPLE |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 1111 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 1112 | $Prog -o /tmp/myhtmldir make -j4 |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 1113 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 1114 | The above example causes analysis reports to be deposited into |
| 1115 | a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option. |
| 1116 | A different subdirectory is created each time $Prog analyzes a project. |
| 1117 | The analyzer should support most parallel builds, but not distributed builds. |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 1118 | |
| 1119 | ENDTEXT |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | ##----------------------------------------------------------------------------## |
Ted Kremenek | 7cba112 | 2008-09-22 01:35:58 +0000 | [diff] [blame] | 1123 | # HtmlEscape - HTML entity encode characters that are special in HTML |
| 1124 | ##----------------------------------------------------------------------------## |
| 1125 | |
| 1126 | sub HtmlEscape { |
| 1127 | # copy argument to new variable so we don't clobber the original |
| 1128 | my $arg = shift || ''; |
| 1129 | my $tmp = $arg; |
Ted Kremenek | 87f8de7 | 2008-11-03 07:44:16 +0000 | [diff] [blame] | 1130 | $tmp =~ s/&/&/g; |
| 1131 | $tmp =~ s/</</g; |
| 1132 | $tmp =~ s/>/>/g; |
Ted Kremenek | 7cba112 | 2008-09-22 01:35:58 +0000 | [diff] [blame] | 1133 | return $tmp; |
| 1134 | } |
| 1135 | |
| 1136 | ##----------------------------------------------------------------------------## |
| 1137 | # ShellEscape - backslash escape characters that are special to the shell |
| 1138 | ##----------------------------------------------------------------------------## |
| 1139 | |
| 1140 | sub ShellEscape { |
| 1141 | # copy argument to new variable so we don't clobber the original |
| 1142 | my $arg = shift || ''; |
Ted Kremenek | 87f8de7 | 2008-11-03 07:44:16 +0000 | [diff] [blame] | 1143 | if ($arg =~ /["\s]/) { return "'" . $arg . "'"; } |
| 1144 | return $arg; |
Ted Kremenek | 7cba112 | 2008-09-22 01:35:58 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | ##----------------------------------------------------------------------------## |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1148 | # Process command-line arguments. |
| 1149 | ##----------------------------------------------------------------------------## |
| 1150 | |
Ted Kremenek | e15fa27 | 2008-10-13 21:46:42 +0000 | [diff] [blame] | 1151 | my $AnalyzeHeaders = 0; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1152 | my $HtmlDir; # Parent directory to store HTML files. |
| 1153 | my $IgnoreErrors = 0; # Ignore build errors. |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 1154 | my $ViewResults = 0; # View results when the build terminates. |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 1155 | my $ExitStatusFoundBugs = 0; # Exit status reflects whether bugs were found |
Ted Kremenek | b7770c0 | 2008-07-15 17:06:13 +0000 | [diff] [blame] | 1156 | my @AnalysesToRun; |
Zhongxing Xu | 07c3767 | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 1157 | my $StoreModel; |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 1158 | my $ConstraintsModel; |
Ted Kremenek | 8d8bc91 | 2009-08-04 17:05:18 +0000 | [diff] [blame] | 1159 | my $OutputFormat = "html"; |
Tom Care | 4f2b10b | 2010-09-30 01:12:05 +0000 | [diff] [blame] | 1160 | my $AnalyzerStats = 0; |
| 1161 | my $MaxLoop = 0; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1162 | |
| 1163 | if (!@ARGV) { |
| 1164 | DisplayHelp(); |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 1165 | exit 1; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
Ted Kremenek | ba90e8a | 2011-02-25 22:00:40 +0000 | [diff] [blame] | 1168 | |
| 1169 | my $displayHelp = 0; |
| 1170 | |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1171 | while (@ARGV) { |
| 1172 | |
| 1173 | # Scan for options we recognize. |
| 1174 | |
| 1175 | my $arg = $ARGV[0]; |
| 1176 | |
Sam Bishop | 2f2418e | 2008-04-03 14:29:47 +0000 | [diff] [blame] | 1177 | if ($arg eq "-h" or $arg eq "--help") { |
Ted Kremenek | ba90e8a | 2011-02-25 22:00:40 +0000 | [diff] [blame] | 1178 | $displayHelp = 1; |
| 1179 | shift @ARGV; |
| 1180 | next; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
Ted Kremenek | e15fa27 | 2008-10-13 21:46:42 +0000 | [diff] [blame] | 1183 | if ($arg eq '-analyze-headers') { |
| 1184 | shift @ARGV; |
| 1185 | $AnalyzeHeaders = 1; |
| 1186 | next; |
| 1187 | } |
| 1188 | |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1189 | if ($arg eq "-o") { |
| 1190 | shift @ARGV; |
| 1191 | |
| 1192 | if (!@ARGV) { |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 1193 | DieDiag("'-o' option requires a target directory name.\n"); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Ted Kremenek | db51a55 | 2009-03-11 18:20:33 +0000 | [diff] [blame] | 1196 | # Construct an absolute path. Uses the current working directory |
| 1197 | # as a base if the original path was not absolute. |
| 1198 | $HtmlDir = abs_path(shift @ARGV); |
| 1199 | |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1200 | next; |
| 1201 | } |
Ted Kremenek | 7cba112 | 2008-09-22 01:35:58 +0000 | [diff] [blame] | 1202 | |
| 1203 | if ($arg =~ /^--html-title(=(.+))?$/) { |
| 1204 | shift @ARGV; |
| 1205 | |
Ted Kremenek | 278a551 | 2009-05-12 18:04:43 +0000 | [diff] [blame] | 1206 | if (!defined $2 || $2 eq '') { |
Ted Kremenek | 7cba112 | 2008-09-22 01:35:58 +0000 | [diff] [blame] | 1207 | if (!@ARGV) { |
| 1208 | DieDiag("'--html-title' option requires a string.\n"); |
| 1209 | } |
| 1210 | |
| 1211 | $HtmlTitle = shift @ARGV; |
| 1212 | } else { |
| 1213 | $HtmlTitle = $2; |
| 1214 | } |
| 1215 | |
| 1216 | next; |
| 1217 | } |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1218 | |
Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 1219 | if ($arg eq "-k" or $arg eq "--keep-going") { |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1220 | shift @ARGV; |
| 1221 | $IgnoreErrors = 1; |
| 1222 | next; |
| 1223 | } |
Ted Kremenek | 7fe679f | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 1224 | |
Ted Kremenek | f17ef3c | 2008-08-21 21:47:09 +0000 | [diff] [blame] | 1225 | if ($arg =~ /^--use-cc(=(.+))?$/) { |
| 1226 | shift @ARGV; |
| 1227 | my $cc; |
| 1228 | |
Ted Kremenek | 278a551 | 2009-05-12 18:04:43 +0000 | [diff] [blame] | 1229 | if (!defined $2 || $2 eq "") { |
Ted Kremenek | f17ef3c | 2008-08-21 21:47:09 +0000 | [diff] [blame] | 1230 | if (!@ARGV) { |
| 1231 | DieDiag("'--use-cc' option requires a compiler executable name.\n"); |
| 1232 | } |
| 1233 | $cc = shift @ARGV; |
| 1234 | } |
| 1235 | else { |
| 1236 | $cc = $2; |
| 1237 | } |
| 1238 | |
| 1239 | $ENV{"CCC_CC"} = $cc; |
| 1240 | next; |
| 1241 | } |
| 1242 | |
Ted Kremenek | 7cba112 | 2008-09-22 01:35:58 +0000 | [diff] [blame] | 1243 | if ($arg =~ /^--use-c\+\+(=(.+))?$/) { |
Ted Kremenek | 386c693 | 2008-09-03 17:59:35 +0000 | [diff] [blame] | 1244 | shift @ARGV; |
Ted Kremenek | 51365b5 | 2009-12-15 02:35:54 +0000 | [diff] [blame] | 1245 | my $cxx; |
Ted Kremenek | 386c693 | 2008-09-03 17:59:35 +0000 | [diff] [blame] | 1246 | |
Ted Kremenek | 278a551 | 2009-05-12 18:04:43 +0000 | [diff] [blame] | 1247 | if (!defined $2 || $2 eq "") { |
Ted Kremenek | 386c693 | 2008-09-03 17:59:35 +0000 | [diff] [blame] | 1248 | if (!@ARGV) { |
| 1249 | DieDiag("'--use-c++' option requires a compiler executable name.\n"); |
| 1250 | } |
Ted Kremenek | 51365b5 | 2009-12-15 02:35:54 +0000 | [diff] [blame] | 1251 | $cxx = shift @ARGV; |
Ted Kremenek | 386c693 | 2008-09-03 17:59:35 +0000 | [diff] [blame] | 1252 | } |
| 1253 | else { |
Ted Kremenek | 51365b5 | 2009-12-15 02:35:54 +0000 | [diff] [blame] | 1254 | $cxx = $2; |
Ted Kremenek | 386c693 | 2008-09-03 17:59:35 +0000 | [diff] [blame] | 1255 | } |
Ted Kremenek | 51365b5 | 2009-12-15 02:35:54 +0000 | [diff] [blame] | 1256 | |
| 1257 | $ENV{"CCC_CXX"} = $cxx; |
Ted Kremenek | 386c693 | 2008-09-03 17:59:35 +0000 | [diff] [blame] | 1258 | next; |
| 1259 | } |
| 1260 | |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1261 | if ($arg eq "-v") { |
| 1262 | shift @ARGV; |
| 1263 | $Verbose++; |
| 1264 | next; |
| 1265 | } |
| 1266 | |
Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 1267 | if ($arg eq "-V" or $arg eq "--view") { |
| 1268 | shift @ARGV; |
| 1269 | $ViewResults = 1; |
| 1270 | next; |
| 1271 | } |
| 1272 | |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 1273 | if ($arg eq "--status-bugs") { |
| 1274 | shift @ARGV; |
| 1275 | $ExitStatusFoundBugs = 1; |
| 1276 | next; |
| 1277 | } |
Zhongxing Xu | 07c3767 | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 1278 | |
| 1279 | if ($arg eq "-store") { |
| 1280 | shift @ARGV; |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 1281 | $StoreModel = shift @ARGV; |
| 1282 | next; |
| 1283 | } |
| 1284 | |
| 1285 | if ($arg eq "-constraints") { |
| 1286 | shift @ARGV; |
| 1287 | $ConstraintsModel = shift @ARGV; |
Zhongxing Xu | 07c3767 | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 1288 | next; |
| 1289 | } |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 1290 | |
Ted Kremenek | db4f5f2 | 2008-11-04 00:02:53 +0000 | [diff] [blame] | 1291 | if ($arg eq "-plist") { |
| 1292 | shift @ARGV; |
| 1293 | $OutputFormat = "plist"; |
| 1294 | next; |
| 1295 | } |
Ted Kremenek | 7753b35 | 2009-07-27 22:10:34 +0000 | [diff] [blame] | 1296 | if ($arg eq "-plist-html") { |
| 1297 | shift @ARGV; |
| 1298 | $OutputFormat = "plist-html"; |
| 1299 | next; |
| 1300 | } |
Ted Kremenek | e600bed | 2009-07-30 23:55:19 +0000 | [diff] [blame] | 1301 | |
| 1302 | if ($arg eq "-no-failure-reports") { |
| 1303 | $ENV{"CCC_REPORT_FAILURES"} = 0; |
| 1304 | next; |
| 1305 | } |
Tom Care | 4f2b10b | 2010-09-30 01:12:05 +0000 | [diff] [blame] | 1306 | if ($arg eq "-stats") { |
| 1307 | shift @ARGV; |
| 1308 | $AnalyzerStats = 1; |
| 1309 | next; |
| 1310 | } |
| 1311 | if ($arg eq "-maxloop") { |
| 1312 | shift @ARGV; |
| 1313 | $MaxLoop = shift @ARGV; |
| 1314 | next; |
| 1315 | } |
Ted Kremenek | 09fbf29 | 2011-04-12 21:47:00 +0000 | [diff] [blame] | 1316 | if ($arg eq "-enable-checker") { |
| 1317 | shift @ARGV; |
| 1318 | push @AnalysesToRun, "-analyzer-checker", shift @ARGV; |
| 1319 | next; |
| 1320 | } |
| 1321 | if ($arg eq "-disable-checker") { |
| 1322 | shift @ARGV; |
| 1323 | push @AnalysesToRun, "-analyzer-disable-checker", shift @ARGV; |
| 1324 | next; |
| 1325 | } |
Ted Kremenek | 7753b35 | 2009-07-27 22:10:34 +0000 | [diff] [blame] | 1326 | |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 1327 | DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/); |
Ted Kremenek | 0062ad4 | 2008-04-02 16:35:01 +0000 | [diff] [blame] | 1328 | |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1329 | last; |
| 1330 | } |
| 1331 | |
Ted Kremenek | ba90e8a | 2011-02-25 22:00:40 +0000 | [diff] [blame] | 1332 | if (!@ARGV and $displayHelp == 0) { |
Ted Kremenek | 23cfca3 | 2008-06-16 22:40:14 +0000 | [diff] [blame] | 1333 | Diag("No build command specified.\n\n"); |
Ted Kremenek | ba90e8a | 2011-02-25 22:00:40 +0000 | [diff] [blame] | 1334 | $displayHelp = 1; |
| 1335 | } |
| 1336 | |
| 1337 | if ($displayHelp) { |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1338 | DisplayHelp(); |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 1339 | exit 1; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Ted Kremenek | ba90e8a | 2011-02-25 22:00:40 +0000 | [diff] [blame] | 1342 | # Determine where results go. |
Ted Kremenek | 7cba112 | 2008-09-22 01:35:58 +0000 | [diff] [blame] | 1343 | $CmdArgs = HtmlEscape(join(' ', map(ShellEscape($_), @ARGV))); |
| 1344 | $HtmlTitle = "${CurrentDirSuffix} - scan-build results" |
| 1345 | unless (defined($HtmlTitle)); |
Ted Kremenek | 386c693 | 2008-09-03 17:59:35 +0000 | [diff] [blame] | 1346 | |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1347 | # Determine the output directory for the HTML reports. |
Ted Kremenek | 684bb09 | 2008-04-18 15:18:20 +0000 | [diff] [blame] | 1348 | my $BaseDir = $HtmlDir; |
Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 1349 | $HtmlDir = GetHTMLRunDir($HtmlDir); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1350 | |
Ted Kremenek | ba90e8a | 2011-02-25 22:00:40 +0000 | [diff] [blame] | 1351 | # Determine the location of ccc-analyzer. |
Ted Kremenek | 51365b5 | 2009-12-15 02:35:54 +0000 | [diff] [blame] | 1352 | my $AbsRealBin = Cwd::realpath($RealBin); |
| 1353 | my $Cmd = "$AbsRealBin/libexec/ccc-analyzer"; |
| 1354 | my $CmdCXX = "$AbsRealBin/libexec/c++-analyzer"; |
| 1355 | |
Ted Kremenek | ce87b92 | 2009-02-25 22:54:02 +0000 | [diff] [blame] | 1356 | if (!defined $Cmd || ! -x $Cmd) { |
Ted Kremenek | 51365b5 | 2009-12-15 02:35:54 +0000 | [diff] [blame] | 1357 | $Cmd = "$AbsRealBin/ccc-analyzer"; |
Ted Kremenek | 6b89636 | 2009-02-27 06:17:05 +0000 | [diff] [blame] | 1358 | DieDiag("Executable 'ccc-analyzer' does not exist at '$Cmd'\n") if(! -x $Cmd); |
Ted Kremenek | ce87b92 | 2009-02-25 22:54:02 +0000 | [diff] [blame] | 1359 | } |
Ted Kremenek | 51365b5 | 2009-12-15 02:35:54 +0000 | [diff] [blame] | 1360 | if (!defined $CmdCXX || ! -x $CmdCXX) { |
| 1361 | $CmdCXX = "$AbsRealBin/c++-analyzer"; |
| 1362 | DieDiag("Executable 'c++-analyzer' does not exist at '$CmdCXX'\n") if(! -x $CmdCXX); |
| 1363 | } |
Ted Kremenek | f22eacb | 2008-04-18 22:00:56 +0000 | [diff] [blame] | 1364 | |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 1365 | if (!defined $ClangSB || ! -x $ClangSB) { |
| 1366 | Diag("'clang' executable not found in '$RealBin/bin'.\n"); |
Ted Kremenek | 6492200 | 2010-02-12 00:12:25 +0000 | [diff] [blame] | 1367 | Diag("Using 'clang' from path: $Clang\n"); |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 1368 | } |
Ted Kremenek | 0b6c153 | 2008-04-08 20:22:12 +0000 | [diff] [blame] | 1369 | |
Ted Kremenek | ba90e8a | 2011-02-25 22:00:40 +0000 | [diff] [blame] | 1370 | # Set the appropriate environment variables. |
| 1371 | SetHtmlEnv(\@ARGV, $HtmlDir); |
Ted Kremenek | 4f4b17d | 2008-04-03 20:08:18 +0000 | [diff] [blame] | 1372 | $ENV{'CC'} = $Cmd; |
Ted Kremenek | 51365b5 | 2009-12-15 02:35:54 +0000 | [diff] [blame] | 1373 | $ENV{'CXX'} = $CmdCXX; |
Ted Kremenek | f22eacb | 2008-04-18 22:00:56 +0000 | [diff] [blame] | 1374 | $ENV{'CLANG'} = $Clang; |
Ted Kremenek | 51365b5 | 2009-12-15 02:35:54 +0000 | [diff] [blame] | 1375 | $ENV{'CLANG_CXX'} = $ClangCXX; |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1376 | if ($Verbose >= 2) { |
| 1377 | $ENV{'CCC_ANALYZER_VERBOSE'} = 1; |
| 1378 | } |
Ted Kremenek | a9525c9 | 2008-05-12 22:07:14 +0000 | [diff] [blame] | 1379 | if ($Verbose >= 3) { |
| 1380 | $ENV{'CCC_ANALYZER_LOG'} = 1; |
| 1381 | } |
Ted Kremenek | e15fa27 | 2008-10-13 21:46:42 +0000 | [diff] [blame] | 1382 | if ($AnalyzeHeaders) { |
| 1383 | push @AnalysesToRun,"-analyzer-opt-analyze-headers"; |
| 1384 | } |
Tom Care | 4f2b10b | 2010-09-30 01:12:05 +0000 | [diff] [blame] | 1385 | if ($AnalyzerStats) { |
Ted Kremenek | 251c27b | 2011-04-27 18:53:08 +0000 | [diff] [blame] | 1386 | push @AnalysesToRun, '-analyzer-checker', 'debug.Stats'; |
Tom Care | 4f2b10b | 2010-09-30 01:12:05 +0000 | [diff] [blame] | 1387 | } |
Tom Care | 4f2b10b | 2010-09-30 01:12:05 +0000 | [diff] [blame] | 1388 | if ($MaxLoop > 0) { |
| 1389 | push @AnalysesToRun, '-analyzer-max-loop ' . $MaxLoop; |
| 1390 | } |
| 1391 | |
Ted Kremenek | 9012599 | 2008-07-15 23:41:32 +0000 | [diff] [blame] | 1392 | $ENV{'CCC_ANALYZER_ANALYSIS'} = join ' ',@AnalysesToRun; |
| 1393 | |
Zhongxing Xu | 3cab2b1 | 2008-11-02 10:58:16 +0000 | [diff] [blame] | 1394 | if (defined $StoreModel) { |
Zhongxing Xu | 07c3767 | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 1395 | $ENV{'CCC_ANALYZER_STORE_MODEL'} = $StoreModel; |
| 1396 | } |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 1397 | if (defined $ConstraintsModel) { |
| 1398 | $ENV{'CCC_ANALYZER_CONSTRAINTS_MODEL'} = $ConstraintsModel; |
| 1399 | } |
Ted Kremenek | db4f5f2 | 2008-11-04 00:02:53 +0000 | [diff] [blame] | 1400 | if (defined $OutputFormat) { |
| 1401 | $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'} = $OutputFormat; |
| 1402 | } |
| 1403 | |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1404 | # Run the build. |
Ted Kremenek | 524c308 | 2010-03-27 00:20:01 +0000 | [diff] [blame] | 1405 | my $ExitStatus = RunBuildCommand(\@ARGV, $IgnoreErrors, $Cmd, $CmdCXX); |
Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1406 | |
Ted Kremenek | 7753b35 | 2009-07-27 22:10:34 +0000 | [diff] [blame] | 1407 | if (defined $OutputFormat) { |
Daniel Dunbar | 1182502 | 2009-07-29 16:21:23 +0000 | [diff] [blame] | 1408 | if ($OutputFormat =~ /plist/) { |
| 1409 | Diag "Analysis run complete.\n"; |
| 1410 | Diag "Analysis results (plist files) deposited in '$HtmlDir'\n"; |
| 1411 | } |
| 1412 | elsif ($OutputFormat =~ /html/) { |
Ted Kremenek | 7753b35 | 2009-07-27 22:10:34 +0000 | [diff] [blame] | 1413 | # Postprocess the HTML directory. |
Tom Care | 4f2b10b | 2010-09-30 01:12:05 +0000 | [diff] [blame] | 1414 | my $NumBugs = Postprocess($HtmlDir, $BaseDir, $AnalyzerStats); |
Ted Kremenek | 5656a98 | 2008-07-15 17:09:28 +0000 | [diff] [blame] | 1415 | |
Ted Kremenek | 7753b35 | 2009-07-27 22:10:34 +0000 | [diff] [blame] | 1416 | if ($ViewResults and -r "$HtmlDir/index.html") { |
| 1417 | Diag "Analysis run complete.\n"; |
| 1418 | Diag "Viewing analysis results in '$HtmlDir' using scan-view.\n"; |
| 1419 | my $ScanView = Cwd::realpath("$RealBin/scan-view"); |
| 1420 | if (! -x $ScanView) { $ScanView = "scan-view"; } |
| 1421 | exec $ScanView, "$HtmlDir"; |
| 1422 | } |
| 1423 | |
| 1424 | if ($ExitStatusFoundBugs) { |
| 1425 | exit 1 if ($NumBugs > 0); |
| 1426 | exit 0; |
| 1427 | } |
Ted Kremenek | db4f5f2 | 2008-11-04 00:02:53 +0000 | [diff] [blame] | 1428 | } |
Ted Kremenek | 363dc3f | 2008-07-15 22:03:09 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Ted Kremenek | 5656a98 | 2008-07-15 17:09:28 +0000 | [diff] [blame] | 1431 | exit $ExitStatus; |
| 1432 | |