| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl | 
 | 2 | # | 
 | 3 | #                     The LLVM Compiler Infrastructure | 
 | 4 | # | 
 | 5 | # This file is distributed under the University of Illinois Open Source | 
 | 6 | # License. See LICENSE.TXT for details. | 
 | 7 | # | 
 | 8 | ##===----------------------------------------------------------------------===## | 
 | 9 | # | 
 | 10 | # A script designed to wrap a build so that all calls to gcc are intercepted | 
 | 11 | # and piped to the static analyzer. | 
 | 12 | # | 
 | 13 | ##===----------------------------------------------------------------------===## | 
 | 14 |  | 
 | 15 | use strict; | 
 | 16 | use warnings; | 
 | 17 | use File::Temp qw/ :mktemp /; | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 18 | use FindBin qw($RealBin); | 
| Ted Kremenek | a6e2481 | 2008-04-19 18:05:48 +0000 | [diff] [blame] | 19 | use Digest::MD5; | 
| Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 20 | use File::Basename; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 21 |  | 
 | 22 | my $Verbose = 0;       # Verbose output from this script. | 
 | 23 | my $Prog = "scan-build"; | 
 | 24 |  | 
 | 25 | ##----------------------------------------------------------------------------## | 
 | 26 | # GetHTMLRunDir - Construct an HTML directory name for the current run. | 
 | 27 | ##----------------------------------------------------------------------------## | 
 | 28 |  | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 29 | sub GetHTMLRunDir {   | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 30 |  | 
 | 31 |   die "Not enough arguments." if (@_ == 0); | 
 | 32 |    | 
 | 33 |   my $Dir = shift @_; | 
 | 34 |    | 
 | 35 |   # Get current date and time. | 
 | 36 |    | 
 | 37 |   my @CurrentTime = localtime(); | 
 | 38 |    | 
 | 39 |   my $year  = $CurrentTime[5] + 1900; | 
 | 40 |   my $day   = $CurrentTime[3]; | 
 | 41 |   my $month = $CurrentTime[4] + 1; | 
 | 42 |    | 
 | 43 |   my $DateString = "$year-$month-$day"; | 
 | 44 |    | 
 | 45 |   # Determine the run number. | 
 | 46 |    | 
 | 47 |   my $RunNumber; | 
 | 48 |    | 
 | 49 |   if (-d $Dir) { | 
 | 50 |      | 
 | 51 |     if (! -r $Dir) { | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 52 |       die "error: '$Dir' exists but is not readable.\n"; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 53 |     } | 
 | 54 |      | 
 | 55 |     # Iterate over all files in the specified directory. | 
 | 56 |      | 
 | 57 |     my $max = 0; | 
 | 58 |      | 
 | 59 |     opendir(DIR, $Dir); | 
 | 60 |     my @FILES= readdir(DIR);  | 
 | 61 |     closedir(DIR); | 
 | 62 |      | 
 | 63 |     foreach my $f (@FILES) { | 
 | 64 |  | 
 | 65 |       my @x = split/-/, $f; | 
 | 66 |        | 
 | 67 |       next if (scalar(@x) != 4); | 
 | 68 |       next if ($x[0] != $year); | 
 | 69 |       next if ($x[1] != $month); | 
 | 70 |       next if ($x[2] != $day); | 
 | 71 |        | 
 | 72 |       if ($x[3] > $max) { | 
 | 73 |         $max = $x[3]; | 
 | 74 |       }       | 
 | 75 |     } | 
 | 76 |      | 
 | 77 |     $RunNumber = $max + 1; | 
 | 78 |   } | 
 | 79 |   else { | 
 | 80 |      | 
 | 81 |     if (-x $Dir) { | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 82 |       die "error: '$Dir' exists but is not a directory.\n"; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 83 |     } | 
 | 84 |      | 
 | 85 |     # $Dir does not exist.  It will be automatically created by the  | 
 | 86 |     # clang driver.  Set the run number to 1.   | 
 | 87 |      | 
 | 88 |     $RunNumber = 1; | 
 | 89 |   } | 
 | 90 |    | 
 | 91 |   die "RunNumber must be defined!" if (!defined($RunNumber)); | 
 | 92 |    | 
 | 93 |   # Append the run number. | 
 | 94 |    | 
 | 95 |   return "$Dir/$DateString-$RunNumber";   | 
 | 96 | } | 
 | 97 |  | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 98 | sub SetHtmlEnv { | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 99 |    | 
 | 100 |   die "Wrong number of arguments." if (scalar(@_) != 2); | 
 | 101 |    | 
 | 102 |   my $Args = shift; | 
 | 103 |   my $Dir = shift; | 
 | 104 |    | 
 | 105 |   die "No build command." if (scalar(@$Args) == 0); | 
 | 106 |    | 
 | 107 |   my $Cmd = $$Args[0]; | 
 | 108 |    | 
 | 109 |   if ($Cmd =~ /configure/) { | 
 | 110 |     return; | 
 | 111 |   } | 
 | 112 |    | 
 | 113 |   if ($Verbose) { | 
 | 114 |     print "$Prog: Emitting reports for this run to '$Dir'.\n";   | 
 | 115 |   } | 
 | 116 |    | 
 | 117 |   $ENV{'CCC_ANALYZER_HTML'} = $Dir; | 
 | 118 | } | 
 | 119 |  | 
 | 120 | ##----------------------------------------------------------------------------## | 
| Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 121 | # ComputeDigest - Compute a digest of the specified file. | 
 | 122 | ##----------------------------------------------------------------------------## | 
 | 123 |  | 
 | 124 | sub ComputeDigest { | 
 | 125 |   my $FName = shift; | 
 | 126 |   die "Cannot read $FName" if (! -r $FName);   | 
| Ted Kremenek | a6e2481 | 2008-04-19 18:05:48 +0000 | [diff] [blame] | 127 |    | 
 | 128 |   # 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] | 129 |   # just looking for duplicate files that come from a non-malicious source. | 
 | 130 |   # We use Digest::MD5 because it is a standard Perl module that should | 
| Ted Kremenek | a6e2481 | 2008-04-19 18:05:48 +0000 | [diff] [blame] | 131 |   # come bundled on most systems. | 
 | 132 |    | 
 | 133 |   open(FILE, $FName) or die "Cannot open $FName."; | 
 | 134 |   binmode FILE; | 
 | 135 |   my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest; | 
 | 136 |   close(FILE); | 
 | 137 |    | 
 | 138 |   # Return the digest. | 
 | 139 |    | 
 | 140 |   return $Result; | 
| Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 141 | } | 
 | 142 |  | 
 | 143 | ##----------------------------------------------------------------------------## | 
| Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 144 | #  UpdatePrefix - Compute the common prefix of files. | 
 | 145 | ##----------------------------------------------------------------------------## | 
 | 146 |  | 
 | 147 | my $Prefix; | 
 | 148 |  | 
 | 149 | sub UpdatePrefix { | 
 | 150 |    | 
 | 151 |   my $x = shift; | 
 | 152 |   my $y = basename($x); | 
 | 153 |   $x =~ s/\Q$y\E$//; | 
 | 154 |    | 
 | 155 |   # Ignore /usr, /Library, /System, /Developer | 
 | 156 |  | 
 | 157 |   return if ( $x =~ /^\/usr/ or $x =~ /^\/Library/ | 
 | 158 |               or $x =~ /^\/System/ or $x =~ /^\/Developer/); | 
 | 159 |  | 
 | 160 |    | 
 | 161 |   if (!defined $Prefix) { | 
 | 162 |     $Prefix = $x; | 
 | 163 |     return; | 
 | 164 |   } | 
 | 165 |    | 
 | 166 |   chop $Prefix while (!($x =~ /^$Prefix/)); | 
 | 167 | } | 
 | 168 |  | 
 | 169 | sub GetPrefix { | 
 | 170 |   return $Prefix; | 
 | 171 | } | 
 | 172 |  | 
 | 173 | ##----------------------------------------------------------------------------## | 
 | 174 | #  UpdateInFilePath - Update the path in the report file. | 
 | 175 | ##----------------------------------------------------------------------------## | 
 | 176 |  | 
 | 177 | sub UpdateInFilePath { | 
 | 178 |   my $fname = shift; | 
 | 179 |   my $regex = shift; | 
 | 180 |   my $newtext = shift; | 
 | 181 |    | 
 | 182 |   open (RIN, $fname) or die "cannot open $fname"; | 
 | 183 |   open (ROUT, ">$fname.tmp") or die "cannot open $fname.tmp"; | 
 | 184 |    | 
 | 185 |   while (<RIN>) { | 
 | 186 |     s/$regex/$newtext/; | 
 | 187 |     print ROUT $_; | 
 | 188 |   } | 
 | 189 |    | 
 | 190 |   close (ROUT); | 
 | 191 |   close (RIN); | 
 | 192 |   `mv $fname.tmp $fname`; | 
 | 193 | } | 
 | 194 |  | 
 | 195 | ##----------------------------------------------------------------------------## | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 196 | # ScanFile - Scan a report file for various identifying attributes. | 
 | 197 | ##----------------------------------------------------------------------------## | 
 | 198 |  | 
| Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 199 | # Sometimes a source file is scanned more than once, and thus produces | 
 | 200 | # multiple error reports.  We use a cache to solve this problem. | 
 | 201 |  | 
 | 202 | my %AlreadyScanned; | 
 | 203 |  | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 204 | sub ScanFile { | 
 | 205 |    | 
 | 206 |   my $Index = shift; | 
 | 207 |   my $Dir = shift; | 
 | 208 |   my $FName = shift; | 
 | 209 |    | 
| Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 210 |   # Compute a digest for the report file.  Determine if we have already | 
 | 211 |   # scanned a file that looks just like it. | 
 | 212 |    | 
 | 213 |   my $digest = ComputeDigest("$Dir/$FName"); | 
 | 214 |  | 
 | 215 |   if (defined($AlreadyScanned{$digest})) { | 
 | 216 |     # Redundant file.  Remove it. | 
 | 217 |     `rm -f $Dir/$FName`; | 
 | 218 |     return; | 
 | 219 |   } | 
 | 220 |    | 
 | 221 |   $AlreadyScanned{$digest} = 1; | 
 | 222 |    | 
| Ted Kremenek | 809709f | 2008-04-18 16:58:34 +0000 | [diff] [blame] | 223 |   # At this point the report file is not world readable.  Make it happen. | 
| Ted Kremenek | 684bb09 | 2008-04-18 15:18:20 +0000 | [diff] [blame] | 224 |   `chmod 644 $Dir/$FName`; | 
 | 225 |    | 
 | 226 |   # Scan the report file for tags. | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 227 |   open(IN, "$Dir/$FName") or die "$Prog: Cannot open '$Dir/$FName'\n"; | 
 | 228 |  | 
 | 229 |   my $BugDesc = ""; | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 230 |   my $BugFile = ""; | 
 | 231 |   my $BugPathLength = 1; | 
 | 232 |   my $BugLine = 0; | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 233 |    | 
 | 234 |   while (<IN>) { | 
 | 235 |      | 
 | 236 |     if (/<!-- BUGDESC (.*) -->$/) { | 
 | 237 |       $BugDesc = $1; | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 238 |     } | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 239 |     elsif (/<!-- BUGFILE (.*) -->$/) { | 
 | 240 |       $BugFile = $1; | 
| Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 241 |       UpdatePrefix($BugFile); | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 242 |     } | 
 | 243 |     elsif (/<!-- BUGPATHLENGTH (.*) -->$/) { | 
 | 244 |       $BugPathLength = $1; | 
 | 245 |     } | 
 | 246 |     elsif (/<!-- BUGLINE (.*) -->$/) { | 
 | 247 |       $BugLine = $1;     | 
 | 248 |     } | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 249 |   } | 
 | 250 |  | 
 | 251 |   close(IN); | 
 | 252 |      | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 253 |   push @$Index,[ $FName, $BugDesc, $BugFile, $BugLine, $BugPathLength ]; | 
 | 254 | } | 
 | 255 |  | 
 | 256 | ##----------------------------------------------------------------------------## | 
 | 257 | # CopyJS - Copy JavaScript code to target directory. | 
 | 258 | ##----------------------------------------------------------------------------## | 
 | 259 |  | 
 | 260 | sub CopyJS { | 
 | 261 |  | 
 | 262 |   my $Dir = shift; | 
 | 263 |    | 
 | 264 |   die "$Prog: Cannot find 'sorttable.js'.\n" | 
 | 265 |     if (! -r "$RealBin/sorttable.js");   | 
 | 266 |  | 
 | 267 |   `cp $RealBin/sorttable.js $Dir`; | 
 | 268 |  | 
 | 269 |   die "$Prog: Could not copy 'sorttable.js' to '$Dir'." | 
 | 270 |     if (! -r "$Dir/sorttable.js"); | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 271 | } | 
 | 272 |  | 
 | 273 | ##----------------------------------------------------------------------------## | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 274 | # Postprocess - Postprocess the results of an analysis scan. | 
 | 275 | ##----------------------------------------------------------------------------## | 
 | 276 |  | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 277 | sub Postprocess { | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 278 |    | 
 | 279 |   my $Dir = shift; | 
| Ted Kremenek | 684bb09 | 2008-04-18 15:18:20 +0000 | [diff] [blame] | 280 |   my $BaseDir = shift; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 281 |    | 
 | 282 |   die "No directory specified." if (!defined($Dir)); | 
| Ted Kremenek | 684bb09 | 2008-04-18 15:18:20 +0000 | [diff] [blame] | 283 |   die "No base directory specified." if (!defined($BaseDir)); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 284 |    | 
 | 285 |   if (! -d $Dir) { | 
 | 286 |     return; | 
 | 287 |   } | 
 | 288 |    | 
 | 289 |   opendir(DIR, $Dir); | 
 | 290 |   my @files = grep(/^report-.*\.html$/,readdir(DIR)); | 
 | 291 |   closedir(DIR); | 
 | 292 |  | 
 | 293 |   if (scalar(@files) == 0) { | 
| Ted Kremenek | 0249378 | 2008-04-02 07:05:07 +0000 | [diff] [blame] | 294 |     print "$Prog: Removing directory '$Dir' because it contains no reports.\n"; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 295 |     `rm -fR $Dir`; | 
 | 296 |     return; | 
 | 297 |   } | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 298 |    | 
 | 299 |   # Scan each report file and build an index. | 
 | 300 |    | 
 | 301 |   my @Index; | 
 | 302 |      | 
 | 303 |   foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); } | 
 | 304 |    | 
 | 305 |   # Generate an index.html file. | 
 | 306 |    | 
 | 307 |   my $FName = "$Dir/index.html"; | 
 | 308 |    | 
 | 309 |   open(OUT, ">$FName") or die "$Prog: Cannot create file '$FName'\n"; | 
 | 310 |    | 
| Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 311 |   # Print out the header. | 
 | 312 |    | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 313 | print OUT <<ENDTEXT; | 
 | 314 | <html> | 
 | 315 | <head> | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 316 | <style type="text/css"> | 
 | 317 |  body { color:#000000; background-color:#ffffff } | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 318 |  body { font-family: Helvetica, sans-serif; font-size:9pt } | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 319 |  h1 { font-size:12pt } | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 320 |  table.sortable thead { | 
 | 321 |    background-color:#eee; color:#666666; | 
 | 322 |    font-weight: bold; cursor: default; | 
| Ted Kremenek | bba1cf5 | 2008-04-03 05:50:51 +0000 | [diff] [blame] | 323 |    text-align:center; | 
 | 324 |    border-top: 2px solid #000000; | 
 | 325 |    border-bottom: 2px solid #000000; | 
 | 326 |    font-weight: bold; font-family: Verdana | 
 | 327 |  }  | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 328 |  table.sortable { border: 1px #000000 solid } | 
 | 329 |  table.sortable { border-collapse: collapse; border-spacing: 0px } | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 330 |  td { border-bottom: 1px #000000 dotted } | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 331 |  td { padding:5px; padding-left:8px; padding-right:8px } | 
| Ted Kremenek | d8c6d0c | 2008-04-07 23:50:07 +0000 | [diff] [blame] | 332 |  td { text-align:left; font-size:9pt } | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 333 |  td.View   { padding-left: 10px } | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 334 | </style> | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 335 | <script src="sorttable.js"></script> | 
| Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 336 | <script language='javascript' type="text/javascript"> | 
 | 337 | function SetDisplay(RowClass, DisplayVal) | 
 | 338 | { | 
 | 339 |   var Rows = document.getElementsByTagName("tr"); | 
 | 340 |   for ( var i = 0 ; i < Rows.length; ++i ) { | 
 | 341 |     if (Rows[i].className == RowClass) { | 
 | 342 |       Rows[i].style.display = DisplayVal; | 
 | 343 |     } | 
 | 344 |   } | 
 | 345 | } | 
 | 346 |    | 
 | 347 | function ToggleDisplay(CheckButton, ClassName) { | 
 | 348 |   window.console.log("writing"); | 
 | 349 |   if (CheckButton.checked) { | 
 | 350 |     SetDisplay(ClassName, ""); | 
 | 351 |   } | 
 | 352 |   else { | 
 | 353 |     SetDisplay(ClassName, "none"); | 
 | 354 |   } | 
 | 355 | } | 
 | 356 | </script> | 
 | 357 | </head> | 
 | 358 | <body> | 
 | 359 | ENDTEXT | 
 | 360 |  | 
 | 361 |   # Print out the summary table. | 
 | 362 |    | 
 | 363 |   my %Totals; | 
 | 364 |    | 
 | 365 |   for my $row ( @Index ) { | 
 | 366 |      | 
 | 367 |     my $bug_type = lc($row->[1]); | 
 | 368 |      | 
 | 369 |     if (!defined($Totals{$bug_type})) { | 
 | 370 |       $Totals{$bug_type} = 1; | 
 | 371 |     } | 
 | 372 |     else { | 
 | 373 |       $Totals{$bug_type}++; | 
 | 374 |     } | 
 | 375 |   } | 
 | 376 |  | 
 | 377 | print OUT <<ENDTEXT; | 
 | 378 | <h3>Summary</h3> | 
 | 379 | <table class="sortable"> | 
 | 380 | <tr> | 
 | 381 |   <td>Bug Type</td> | 
 | 382 |   <td>Quantity</td> | 
 | 383 |   <td "sorttable_nosort">Display?</td> | 
 | 384 | </tr> | 
 | 385 | ENDTEXT | 
 | 386 |    | 
 | 387 |   for my $key ( sort { $a cmp $b } keys %Totals ) { | 
 | 388 |     my $x = $key; | 
 | 389 |     $x =~ s/\s/_/g;     | 
 | 390 |     print OUT "<tr><td>$key</td><td>$Totals{$key}</td><td><input type=\"checkbox\" onClick=\"ToggleDisplay(this,'bt_$x');\" checked/></td></tr>\n"; | 
 | 391 |   } | 
 | 392 |  | 
 | 393 |   # Print out the table of errors. | 
 | 394 |  | 
 | 395 | print OUT <<ENDTEXT; | 
 | 396 | </table> | 
 | 397 | <h3>Reports</h3> | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 398 | <table class="sortable"> | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 399 | <tr> | 
| Ted Kremenek | bba1cf5 | 2008-04-03 05:50:51 +0000 | [diff] [blame] | 400 |   <td>Bug Type</td> | 
 | 401 |   <td>File</td> | 
 | 402 |   <td>Line</td> | 
 | 403 |   <td>Path Length</td> | 
 | 404 |   <td "sorttable_nosort"></td> | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 405 | </tr> | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 406 | ENDTEXT | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 407 |  | 
| Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 408 |   my $prefix = GetPrefix(); | 
 | 409 |   my $regex; | 
 | 410 |   my $InFileRegex; | 
 | 411 |   my $InFilePrefix = "File:</td><td>"; | 
 | 412 |    | 
 | 413 |   if (defined($prefix)) {  | 
 | 414 |     $regex = qr/^\Q$prefix\E/is;     | 
 | 415 |     $InFileRegex = qr/\Q$InFilePrefix$prefix\E/is; | 
 | 416 |   }     | 
 | 417 |  | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 418 |   for my $row ( sort { $a->[1] cmp $b->[1] } @Index ) { | 
 | 419 |      | 
| Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 420 |     my $x = lc($row->[1]); | 
 | 421 |     $x =~ s/\s/_/g;     | 
 | 422 |      | 
 | 423 |     print OUT "<tr class=\"bt_$x\">\n"; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 424 |  | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 425 |     my $ReportFile = $row->[0]; | 
 | 426 |  | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 427 |     print OUT " <td class=\"DESC\">"; | 
 | 428 |     print OUT lc($row->[1]); | 
 | 429 |     print OUT "</td>\n"; | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 430 |      | 
| Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 431 |     # Update the file prefix. | 
 | 432 |      | 
 | 433 |     my $fname = $row->[2]; | 
 | 434 |     if (defined($regex)) {       | 
 | 435 |       $fname =~ s/$regex//; | 
 | 436 |       UpdateInFilePath("$Dir/$ReportFile", $InFileRegex, $InFilePrefix) | 
 | 437 |     } | 
| Ted Kremenek | 3e56e0b | 2008-05-02 23:40:49 +0000 | [diff] [blame] | 438 |  | 
| Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 439 |     print OUT "<td>$fname</td>\n"; | 
 | 440 |  | 
 | 441 |     # Print the rest of the columns. | 
 | 442 |      | 
 | 443 |     for my $j ( 3 .. $#{$row} ) { | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 444 |       print OUT "<td>$row->[$j]</td>\n" | 
 | 445 |     } | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 446 |  | 
 | 447 |     # Emit the "View" link. | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 448 |      | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 449 |     print OUT " <td class=\"View\"><a href=\"$ReportFile#EndPath\">View</a></td>\n"; | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 450 |      | 
 | 451 |     # End the row. | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 452 |     print OUT "</tr>\n"; | 
 | 453 |   } | 
 | 454 |    | 
 | 455 |   print OUT "</table>\n</body></html>\n";   | 
 | 456 |   close(OUT); | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 457 |    | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 458 |   CopyJS($Dir); | 
| Ted Kremenek | 684bb09 | 2008-04-18 15:18:20 +0000 | [diff] [blame] | 459 |    | 
 | 460 |   # Make sure $Dir and $BaseDir is world readable/executable. | 
 | 461 |   `chmod 755 $Dir`; | 
 | 462 |   `chmod 755 $BaseDir`; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 463 | } | 
 | 464 |  | 
 | 465 | ##----------------------------------------------------------------------------## | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 466 | # RunBuildCommand - Run the build command. | 
 | 467 | ##----------------------------------------------------------------------------## | 
 | 468 |  | 
| Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 469 | sub AddIfNotPresent { | 
 | 470 |   my $Args = shift; | 
 | 471 |   my $Arg = shift;   | 
 | 472 |   my $found = 0; | 
 | 473 |    | 
 | 474 |   foreach my $k (@$Args) { | 
 | 475 |     if ($k eq $Arg) { | 
 | 476 |       $found = 1; | 
 | 477 |       last; | 
 | 478 |     } | 
 | 479 |   } | 
 | 480 |    | 
 | 481 |   if ($found == 0) { | 
 | 482 |     push @$Args, $Arg; | 
 | 483 |   } | 
 | 484 | } | 
 | 485 |  | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 486 | sub RunBuildCommand { | 
 | 487 |    | 
 | 488 |   my $Args = shift; | 
| Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame] | 489 |   my $IgnoreErrors = shift; | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 490 |   my $Cmd = $Args->[0]; | 
 | 491 |    | 
| Ted Kremenek | 6a43ba9 | 2008-04-02 15:34:12 +0000 | [diff] [blame] | 492 |   if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc") { | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 493 |     shift @$Args; | 
 | 494 |     unshift @$Args, "ccc-analyzer" | 
 | 495 |   } | 
| Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame] | 496 |   elsif ($IgnoreErrors) { | 
 | 497 |     if ($Cmd eq "make" or $Cmd eq "gmake") { | 
| Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 498 |       AddIfNotPresent($Args,"-k"); | 
| Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame] | 499 |     } | 
 | 500 |     elsif ($Cmd eq "xcodebuild") { | 
| Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 501 |       AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES"); | 
| Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame] | 502 |     } | 
| Ted Kremenek | 6b62898 | 2008-04-30 23:47:12 +0000 | [diff] [blame] | 503 |   }  | 
 | 504 |    | 
 | 505 |   # Disable distributed builds for xcodebuild. | 
 | 506 |   if ($Cmd eq "xcodebuild") { | 
 | 507 |     AddIfNotPresent($Args,"-nodistribute"); | 
 | 508 |   } | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 509 |    | 
 | 510 |   system(@$Args); | 
 | 511 | } | 
 | 512 |  | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 513 | ##----------------------------------------------------------------------------## | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 514 | # DisplayHelp - Utility function to display all help options. | 
 | 515 | ##----------------------------------------------------------------------------## | 
 | 516 |  | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 517 | sub DisplayHelp { | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 518 |    | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 519 | print <<ENDTEXT; | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 520 | USAGE: $Prog [options] <build command> [build options] | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 521 |  | 
 | 522 | OPTIONS: | 
 | 523 |  | 
 | 524 |   -o            - Target directory for HTML report files.  Subdirectories | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 525 |                   will be created as needed to represent separate "runs" of | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 526 |                   the analyzer.  If this option is not specified, a directory | 
 | 527 |                   is created in /tmp to store the reports. | 
 | 528 |                  | 
| Ted Kremenek | 10f883f | 2008-04-03 07:11:44 +0000 | [diff] [blame] | 529 |   -h            - Display this message. | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 530 |   --help | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 531 |    | 
| Ted Kremenek | af08f64 | 2008-04-02 16:31:58 +0000 | [diff] [blame] | 532 |   -k            - Add a "keep on going" option to the specified build command. | 
| Ted Kremenek | f02e8db | 2008-04-02 16:41:25 +0000 | [diff] [blame] | 533 |   --keep-going    This option currently supports make and xcodebuild. | 
 | 534 |                   This is a convenience option; one can specify this | 
 | 535 |                   behavior directly using build options. | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 536 |  | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 537 |   -v            - Verbose output from $Prog and the analyzer. | 
 | 538 |                   A second "-v" increases verbosity. | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 539 |  | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 540 |   -V            - View analysis results in a web browser when the build | 
 | 541 |   --view          completes. | 
 | 542 |  | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 543 | BUILD OPTIONS | 
 | 544 |  | 
| Ted Kremenek | 39eefde | 2008-04-02 16:47:27 +0000 | [diff] [blame] | 545 |   You can specify any build option acceptable to the build command. | 
 | 546 |  | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 547 | EXAMPLE | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 548 |  | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 549 |     $Prog -o /tmp/myhtmldir make -j4 | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 550 |       | 
| Ted Kremenek | 39eefde | 2008-04-02 16:47:27 +0000 | [diff] [blame] | 551 |   The above example causes analysis reports to be deposited into | 
 | 552 |   a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option. | 
 | 553 |   A different subdirectory is created each time $Prog analyzes a project. | 
 | 554 |   The analyzer should support most parallel builds, but not distributed builds. | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 555 |  | 
 | 556 | ENDTEXT | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 557 | } | 
 | 558 |  | 
 | 559 | ##----------------------------------------------------------------------------## | 
 | 560 | # Process command-line arguments. | 
 | 561 | ##----------------------------------------------------------------------------## | 
 | 562 |  | 
 | 563 | my $HtmlDir;           # Parent directory to store HTML files. | 
 | 564 | my $IgnoreErrors = 0;  # Ignore build errors. | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 565 | my $ViewResults  = 0;  # View results when the build terminates. | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 566 |  | 
 | 567 | if (!@ARGV) { | 
 | 568 |   DisplayHelp(); | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 569 |   exit 1; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 570 | } | 
 | 571 |  | 
 | 572 | while (@ARGV) { | 
 | 573 |    | 
 | 574 |   # Scan for options we recognize. | 
 | 575 |    | 
 | 576 |   my $arg = $ARGV[0]; | 
 | 577 |  | 
| Sam Bishop | 2f2418e | 2008-04-03 14:29:47 +0000 | [diff] [blame] | 578 |   if ($arg eq "-h" or $arg eq "--help") { | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 579 |     DisplayHelp(); | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 580 |     exit 0; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 581 |   } | 
 | 582 |    | 
 | 583 |   if ($arg eq "-o") { | 
 | 584 |     shift @ARGV; | 
 | 585 |          | 
 | 586 |     if (!@ARGV) { | 
| Ted Kremenek | 0062ad4 | 2008-04-02 16:35:01 +0000 | [diff] [blame] | 587 |       die "$Prog: '-o' option requires a target directory name.\n"; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 588 |     } | 
 | 589 |      | 
 | 590 |     $HtmlDir = shift @ARGV; | 
 | 591 |     next; | 
 | 592 |   } | 
 | 593 |    | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 594 |   if ($arg eq "-k" or $arg eq "--keep-going") { | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 595 |     shift @ARGV; | 
 | 596 |     $IgnoreErrors = 1; | 
 | 597 |     next; | 
 | 598 |   } | 
 | 599 |    | 
 | 600 |   if ($arg eq "-v") { | 
 | 601 |     shift @ARGV; | 
 | 602 |     $Verbose++; | 
 | 603 |     next; | 
 | 604 |   } | 
 | 605 |    | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 606 |   if ($arg eq "-V" or $arg eq "--view") { | 
 | 607 |     shift @ARGV; | 
 | 608 |     $ViewResults = 1;     | 
 | 609 |     next; | 
 | 610 |   } | 
 | 611 |    | 
| Ted Kremenek | 0062ad4 | 2008-04-02 16:35:01 +0000 | [diff] [blame] | 612 |   die "$Prog: unrecognized option '$arg'\n" if ($arg =~ /^-/); | 
 | 613 |    | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 614 |   last; | 
 | 615 | } | 
 | 616 |  | 
 | 617 | if (!@ARGV) { | 
 | 618 |   print STDERR "$Prog: No build command specified.\n\n"; | 
 | 619 |   DisplayHelp(); | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 620 |   exit 1; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 621 | } | 
 | 622 |  | 
 | 623 | # Determine the output directory for the HTML reports. | 
 | 624 |  | 
 | 625 | if (!defined($HtmlDir)) { | 
 | 626 |    | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 627 |   $HtmlDir = mkdtemp("/tmp/$Prog-XXXXXX"); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 628 |    | 
 | 629 |   if (!defined($HtmlDir)) { | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 630 |     die "error: Cannot create HTML directory in /tmp.\n"; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 631 |   } | 
 | 632 |    | 
 | 633 |   if (!$Verbose) { | 
 | 634 |     print "$Prog: Using '$HtmlDir' as base HTML report directory.\n"; | 
 | 635 |   } | 
 | 636 | } | 
 | 637 |  | 
| Ted Kremenek | 684bb09 | 2008-04-18 15:18:20 +0000 | [diff] [blame] | 638 | my $BaseDir = $HtmlDir; | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 639 | $HtmlDir = GetHTMLRunDir($HtmlDir); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 640 |  | 
 | 641 | # Set the appropriate environment variables. | 
 | 642 |  | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 643 | SetHtmlEnv(\@ARGV, $HtmlDir); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 644 |  | 
| Ted Kremenek | 0b6c153 | 2008-04-08 20:22:12 +0000 | [diff] [blame] | 645 | my $Cmd = "$RealBin/ccc-analyzer"; | 
 | 646 |  | 
 | 647 | die "$Prog: Executable 'ccc-analyzer' does not exist at '$Cmd'\n" | 
 | 648 |   if (! -x $Cmd); | 
| Ted Kremenek | f22eacb | 2008-04-18 22:00:56 +0000 | [diff] [blame] | 649 |    | 
 | 650 | my $Clang = "$RealBin/clang"; | 
 | 651 |  | 
 | 652 | if (! -x $Clang) { | 
 | 653 |   print "$Prog: 'clang' executable not found in '$RealBin'.  Using 'clang' from path.\n"; | 
 | 654 |   $Clang = "clang"; | 
 | 655 | } | 
| Ted Kremenek | 0b6c153 | 2008-04-08 20:22:12 +0000 | [diff] [blame] | 656 |  | 
| Ted Kremenek | 4f4b17d | 2008-04-03 20:08:18 +0000 | [diff] [blame] | 657 | $ENV{'CC'} = $Cmd; | 
| Ted Kremenek | f22eacb | 2008-04-18 22:00:56 +0000 | [diff] [blame] | 658 | $ENV{'CLANG'} = $Clang; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 659 |  | 
 | 660 | if ($Verbose >= 2) { | 
 | 661 |   $ENV{'CCC_ANALYZER_VERBOSE'} = 1; | 
 | 662 | } | 
 | 663 |  | 
 | 664 | # Run the build. | 
 | 665 |  | 
| Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame] | 666 | RunBuildCommand(\@ARGV, $IgnoreErrors); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 667 |  | 
 | 668 | # Postprocess the HTML directory. | 
 | 669 |  | 
| Ted Kremenek | 684bb09 | 2008-04-18 15:18:20 +0000 | [diff] [blame] | 670 | Postprocess($HtmlDir, $BaseDir); | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 671 |  | 
 | 672 | if ($ViewResults and -r "$HtmlDir/index.html") { | 
 | 673 |   # Only works on Mac OS X (for now). | 
 | 674 |   print "Viewing analysis results: '$HtmlDir/index.html'\n"; | 
 | 675 |   `open $HtmlDir/index.html` | 
 | 676 | } |