Ted Kremenek | c9d8fde | 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 /; |
| 18 | |
| 19 | my $Verbose = 0; # Verbose output from this script. |
| 20 | my $Prog = "scan-build"; |
| 21 | |
| 22 | ##----------------------------------------------------------------------------## |
| 23 | # GetHTMLRunDir - Construct an HTML directory name for the current run. |
| 24 | ##----------------------------------------------------------------------------## |
| 25 | |
| 26 | sub GetHTMLRunDir() { |
| 27 | |
| 28 | die "Not enough arguments." if (@_ == 0); |
| 29 | |
| 30 | my $Dir = shift @_; |
| 31 | |
| 32 | # Get current date and time. |
| 33 | |
| 34 | my @CurrentTime = localtime(); |
| 35 | |
| 36 | my $year = $CurrentTime[5] + 1900; |
| 37 | my $day = $CurrentTime[3]; |
| 38 | my $month = $CurrentTime[4] + 1; |
| 39 | |
| 40 | my $DateString = "$year-$month-$day"; |
| 41 | |
| 42 | # Determine the run number. |
| 43 | |
| 44 | my $RunNumber; |
| 45 | |
| 46 | if (-d $Dir) { |
| 47 | |
| 48 | if (! -r $Dir) { |
| 49 | print STDERR "error: '$Dir' exists but is not readable.\n"; |
| 50 | exit 0; |
| 51 | } |
| 52 | |
| 53 | # Iterate over all files in the specified directory. |
| 54 | |
| 55 | my $max = 0; |
| 56 | |
| 57 | opendir(DIR, $Dir); |
| 58 | my @FILES= readdir(DIR); |
| 59 | closedir(DIR); |
| 60 | |
| 61 | foreach my $f (@FILES) { |
| 62 | |
| 63 | my @x = split/-/, $f; |
| 64 | |
| 65 | next if (scalar(@x) != 4); |
| 66 | next if ($x[0] != $year); |
| 67 | next if ($x[1] != $month); |
| 68 | next if ($x[2] != $day); |
| 69 | |
| 70 | if ($x[3] > $max) { |
| 71 | $max = $x[3]; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | $RunNumber = $max + 1; |
| 76 | } |
| 77 | else { |
| 78 | |
| 79 | if (-x $Dir) { |
| 80 | print STDERR "error: '$Dir' exists but is not a directory.\n"; |
| 81 | exit 0; |
| 82 | } |
| 83 | |
| 84 | # $Dir does not exist. It will be automatically created by the |
| 85 | # clang driver. Set the run number to 1. |
| 86 | |
| 87 | $RunNumber = 1; |
| 88 | } |
| 89 | |
| 90 | die "RunNumber must be defined!" if (!defined($RunNumber)); |
| 91 | |
| 92 | # Append the run number. |
| 93 | |
| 94 | return "$Dir/$DateString-$RunNumber"; |
| 95 | } |
| 96 | |
| 97 | sub SetHtmlEnv() { |
| 98 | |
| 99 | die "Wrong number of arguments." if (scalar(@_) != 2); |
| 100 | |
| 101 | my $Args = shift; |
| 102 | my $Dir = shift; |
| 103 | |
| 104 | die "No build command." if (scalar(@$Args) == 0); |
| 105 | |
| 106 | my $Cmd = $$Args[0]; |
| 107 | |
| 108 | if ($Cmd =~ /configure/) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | if ($Verbose) { |
| 113 | print "$Prog: Emitting reports for this run to '$Dir'.\n"; |
| 114 | } |
| 115 | |
| 116 | $ENV{'CCC_ANALYZER_HTML'} = $Dir; |
| 117 | } |
| 118 | |
| 119 | ##----------------------------------------------------------------------------## |
| 120 | # Postprocess - Postprocess the results of an analysis scan. |
| 121 | ##----------------------------------------------------------------------------## |
| 122 | |
| 123 | sub Postprocess() { |
| 124 | |
| 125 | my $Dir = shift; |
| 126 | |
| 127 | die "No directory specified." if (!defined($Dir)); |
| 128 | |
| 129 | if (! -d $Dir) { |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | opendir(DIR, $Dir); |
| 134 | my @files = grep(/^report-.*\.html$/,readdir(DIR)); |
| 135 | closedir(DIR); |
| 136 | |
| 137 | if (scalar(@files) == 0) { |
| 138 | print "$Prog: Remove directory '$Dir' because it contains no reports.\n"; |
| 139 | `rm -fR $Dir`; |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | } |
| 145 | |
| 146 | ##----------------------------------------------------------------------------## |
| 147 | # DisplayHelp - Utility function to display all help options. |
| 148 | ##----------------------------------------------------------------------------## |
| 149 | |
| 150 | sub DisplayHelp() { |
| 151 | |
| 152 | |
| 153 | |
| 154 | } |
| 155 | |
| 156 | ##----------------------------------------------------------------------------## |
| 157 | # Process command-line arguments. |
| 158 | ##----------------------------------------------------------------------------## |
| 159 | |
| 160 | my $HtmlDir; # Parent directory to store HTML files. |
| 161 | my $IgnoreErrors = 0; # Ignore build errors. |
| 162 | |
| 163 | if (!@ARGV) { |
| 164 | DisplayHelp(); |
| 165 | exit 1 |
| 166 | } |
| 167 | |
| 168 | while (@ARGV) { |
| 169 | |
| 170 | # Scan for options we recognize. |
| 171 | |
| 172 | my $arg = $ARGV[0]; |
| 173 | |
| 174 | if ($arg eq "-?") { |
| 175 | DisplayHelp(); |
| 176 | exit 1; |
| 177 | } |
| 178 | |
| 179 | if ($arg eq "-o") { |
| 180 | shift @ARGV; |
| 181 | |
| 182 | if (!@ARGV) { |
| 183 | print STDERR "'-o' option requires a target directory name."; |
| 184 | exit 0; |
| 185 | } |
| 186 | |
| 187 | $HtmlDir = shift @ARGV; |
| 188 | next; |
| 189 | } |
| 190 | |
| 191 | if ($arg eq "-k") { |
| 192 | shift @ARGV; |
| 193 | $IgnoreErrors = 1; |
| 194 | next; |
| 195 | } |
| 196 | |
| 197 | if ($arg eq "-v") { |
| 198 | shift @ARGV; |
| 199 | $Verbose++; |
| 200 | next; |
| 201 | } |
| 202 | |
| 203 | last; |
| 204 | } |
| 205 | |
| 206 | if (!@ARGV) { |
| 207 | print STDERR "$Prog: No build command specified.\n\n"; |
| 208 | DisplayHelp(); |
| 209 | exit 0; |
| 210 | } |
| 211 | |
| 212 | # Determine the output directory for the HTML reports. |
| 213 | |
| 214 | if (!defined($HtmlDir)) { |
| 215 | |
| 216 | $HtmlDir = mkdtemp("/tmp/scan-build-XXXXXX"); |
| 217 | |
| 218 | if (!defined($HtmlDir)) { |
| 219 | print STDERR "error: Cannot create HTML directory in /tmp.\n"; |
| 220 | exit 0; |
| 221 | } |
| 222 | |
| 223 | if (!$Verbose) { |
| 224 | print "$Prog: Using '$HtmlDir' as base HTML report directory.\n"; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | $HtmlDir = &GetHTMLRunDir($HtmlDir); |
| 229 | |
| 230 | # Set the appropriate environment variables. |
| 231 | |
| 232 | &SetHtmlEnv(\@ARGV, $HtmlDir); |
| 233 | |
| 234 | $ENV{'CC'} = "ccc-analyzer"; |
| 235 | |
| 236 | if ($Verbose >= 2) { |
| 237 | $ENV{'CCC_ANALYZER_VERBOSE'} = 1; |
| 238 | } |
| 239 | |
| 240 | # Run the build. |
| 241 | |
| 242 | system(@ARGV); |
| 243 | |
| 244 | # Postprocess the HTML directory. |
| 245 | |
| 246 | &Postprocess($HtmlDir); |