Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 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 | # |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 10 | # A script designed to interpose between the build system and gcc. It invokes |
| 11 | # both gcc and the static analyzer. |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 12 | # |
| 13 | ##===----------------------------------------------------------------------===## |
| 14 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 15 | use strict; |
| 16 | use warnings; |
| 17 | use Cwd; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 18 | use File::Temp qw/ tempfile /; |
| 19 | use File::Path qw / mkpath /; |
Ted Kremenek | 2ec5cd5 | 2008-08-25 20:44:31 +0000 | [diff] [blame] | 20 | |
| 21 | my $CC = $ENV{'CCC_CC'}; |
| 22 | if (!defined $CC) { $CC = "gcc"; } |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 23 | |
| 24 | ##----------------------------------------------------------------------------## |
| 25 | # Process Clang Crashes. |
| 26 | ##----------------------------------------------------------------------------## |
| 27 | |
| 28 | sub GetPPExt { |
| 29 | my $Lang = shift; |
| 30 | if ($Lang =~ /objective-c/) { return ".mi"; } |
| 31 | return ".i"; |
| 32 | } |
| 33 | |
Ted Kremenek | 5d31f83 | 2008-08-18 18:38:29 +0000 | [diff] [blame] | 34 | sub ProcessClangFailure { |
Ted Kremenek | 9a3c7da | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 35 | my ($Lang, $file, $Args, $HtmlDir, $ErrorType, $ofile) = @_; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 36 | my $Dir = "$HtmlDir/crashes"; |
| 37 | mkpath $Dir; |
| 38 | my ($PPH, $PPFile) = tempfile("clang_crash_XXXXXX", |
| 39 | SUFFIX => GetPPExt($Lang), |
| 40 | DIR => $Dir); |
| 41 | |
Ted Kremenek | 2ec5cd5 | 2008-08-25 20:44:31 +0000 | [diff] [blame] | 42 | system $CC, @$Args, "-E", "-o", $PPFile; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 43 | close ($PPH); |
| 44 | open (OUT, ">", "$PPFile.info") or die "Cannot open $PPFile.info\n"; |
Ted Kremenek | 5d31f83 | 2008-08-18 18:38:29 +0000 | [diff] [blame] | 45 | print OUT "$file\n"; |
| 46 | print OUT "$ErrorType\n"; |
Ted Kremenek | 2dd7ad1 | 2008-08-18 20:55:25 +0000 | [diff] [blame] | 47 | print OUT "@$Args\n"; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 48 | close OUT; |
Ted Kremenek | 01479d0 | 2008-09-04 00:41:45 +0000 | [diff] [blame] | 49 | `uname -a >> $PPFile.info 2>&1`; |
| 50 | `$CC -v >> $PPFile.info 2>&1`; |
Ted Kremenek | 9a3c7da | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 51 | system 'mv',$ofile,"$PPFile.output"; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 52 | } |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 53 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 54 | ##----------------------------------------------------------------------------## |
| 55 | # Running the analyzer. |
| 56 | ##----------------------------------------------------------------------------## |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 57 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 58 | sub Analyze { |
| 59 | my ($Clang, $Args, $Lang, $Output, $Verbose, $HtmlDir, $file, $Analyses) = @_; |
Seo Sanghyeon | d389465 | 2008-04-04 11:02:21 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 61 | # Skip anything related to C++. |
| 62 | return if ($Lang =~ /c[+][+]/); |
Ted Kremenek | 5d31f83 | 2008-08-18 18:38:29 +0000 | [diff] [blame] | 63 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 64 | my $RunAnalyzer = 0; |
| 65 | my $Cmd; |
| 66 | my @CmdArgs; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 67 | my @CmdArgsSansAnalyses; |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 69 | if ($Lang =~ /header/) { |
| 70 | exit 0 if (!defined ($Output)); |
| 71 | $Cmd = 'cp'; |
| 72 | push @CmdArgs,$file; |
| 73 | # Remove the PCH extension. |
| 74 | $Output =~ s/[.]gch$//; |
| 75 | push @CmdArgs,$Output; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 76 | @CmdArgsSansAnalyses = @CmdArgs; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 77 | } |
| 78 | else { |
| 79 | $Cmd = $Clang; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 80 | push @CmdArgs,'-DIBOutlet=__attribute__((iboutlet))'; |
| 81 | push @CmdArgs,@$Args; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 82 | @CmdArgsSansAnalyses = @CmdArgs; |
| 83 | push @CmdArgs,(split /\s/,$Analyses); |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 84 | $RunAnalyzer = 1; |
| 85 | } |
| 86 | |
| 87 | my @PrintArgs; |
| 88 | my $dir; |
| 89 | |
| 90 | if ($Verbose) { |
| 91 | $dir = getcwd(); |
| 92 | print STDERR "\n[LOCATION]: $dir\n"; |
| 93 | push @PrintArgs,"'$Cmd'"; |
| 94 | foreach my $arg (@CmdArgs) { push @PrintArgs,"\'$arg\'"; } |
| 95 | } |
| 96 | |
| 97 | if ($Verbose == 1) { |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 98 | # We MUST print to stderr. Some clients use the stdout output of |
| 99 | # gcc for various purposes. |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 100 | print STDERR join(' ',@PrintArgs); |
| 101 | print STDERR "\n"; |
| 102 | } |
| 103 | elsif ($Verbose == 2) { |
| 104 | print STDERR "#SHELL (cd '$dir' && @PrintArgs)\n"; |
| 105 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 106 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 107 | if ($RunAnalyzer and defined($HtmlDir)) { |
| 108 | push @CmdArgs,'-o'; |
| 109 | push @CmdArgs,$HtmlDir; |
| 110 | } |
Ted Kremenek | 948e06b | 2008-08-27 22:30:34 +0000 | [diff] [blame] | 111 | |
| 112 | if (defined $ENV{'CCC_UBI'}) { |
| 113 | push @CmdArgs,"--analyzer-viz-egraph-ubigraph"; |
| 114 | } |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 115 | |
Ted Kremenek | 9a3c7da | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 116 | # Capture the STDERR of clang and send it to a temporary file. |
| 117 | # Capture the STDOUT of clang and reroute it to ccc-analyzer's STDERR. |
| 118 | # We save the output file in the 'crashes' directory if clang encounters |
| 119 | # any problems with the file. |
| 120 | my ($ofh, $ofile) = tempfile("clang_output_XXXXXX", DIR => $HtmlDir); |
| 121 | my $pid = fork(); |
| 122 | if ($pid == 0) { |
| 123 | open(STDOUT,">&", \*STDERR); |
| 124 | open(STDERR,">&", $ofh); |
| 125 | exec $Cmd, @CmdArgs; |
| 126 | } |
| 127 | close ($ofh); |
| 128 | wait; |
| 129 | my $Result = $?; |
| 130 | |
| 131 | # Did the command die because of a signal? |
| 132 | if ($Result & 127 and $Cmd eq $Clang and defined $HtmlDir) { |
| 133 | ProcessClangFailure($Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir, |
| 134 | "Crash", $ofile); |
| 135 | } |
| 136 | elsif ($Result) { |
| 137 | ProcessClangFailure($Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir, |
| 138 | "Parser Rejects", $ofile); |
| 139 | } |
| 140 | |
| 141 | `rm -f $ofile`; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 142 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 143 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 144 | ##----------------------------------------------------------------------------## |
| 145 | # Lookup tables. |
| 146 | ##----------------------------------------------------------------------------## |
| 147 | |
| 148 | my %CompileOptionMap = ( |
| 149 | '-nostdinc' => 0, |
| 150 | '-fobjc-gc-only' => 0, |
| 151 | '-fobjc-gc' => 0, |
| 152 | '-include' => 1, |
| 153 | '-idirafter' => 1, |
| 154 | '-iprefix' => 1, |
| 155 | '-iquote' => 1, |
| 156 | '-isystem' => 1, |
| 157 | '-iwithprefix' => 1, |
| 158 | '-iwithprefixbefore' => 1 |
| 159 | ); |
| 160 | |
| 161 | my %LinkerOptionMap = ( |
| 162 | '-framework' => 1 |
| 163 | ); |
| 164 | |
| 165 | my %CompilerLinkerOptionMap = ( |
| 166 | '-isysroot' => 1, |
| 167 | '-arch' => 1, |
| 168 | '-v' => 0 |
| 169 | ); |
| 170 | |
| 171 | my %IgnoredOptionMap = ( |
Ted Kremenek | 9402609 | 2008-07-24 03:52:21 +0000 | [diff] [blame] | 172 | '-MT' => 1, # Ignore these preprocessor options. |
| 173 | '-MF' => 1, |
| 174 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 175 | '-fsyntax-only' => 0, |
| 176 | '-save-temps' => 0, |
| 177 | '-install_name' => 1, |
| 178 | '-exported_symbols_list' => 1, |
| 179 | '-current_version' => 1, |
| 180 | '-compatibility_version' => 1, |
| 181 | '-init' => 1, |
| 182 | '-e' => 1, |
| 183 | '-seg1addr' => 1, |
| 184 | '-bundle_loader' => 1, |
| 185 | '-multiply_defined' => 1, |
| 186 | '-sectorder' => 3, |
| 187 | '--param' => 1, |
| 188 | '-u' => 1 |
| 189 | ); |
| 190 | |
| 191 | my %LangMap = ( |
| 192 | 'c' => 'c', |
| 193 | 'cpp' => 'c++', |
| 194 | 'cc' => 'c++', |
| 195 | 'i' => 'c-cpp-output', |
| 196 | 'm' => 'objective-c', |
| 197 | 'mi' => 'objective-c-cpp-output' |
| 198 | ); |
| 199 | |
| 200 | ##----------------------------------------------------------------------------## |
| 201 | # Main Logic. |
| 202 | ##----------------------------------------------------------------------------## |
| 203 | |
| 204 | my $Action = 'link'; |
| 205 | my @CompileOpts; |
| 206 | my @LinkOpts; |
| 207 | my @Files; |
| 208 | my $Lang; |
| 209 | my $Output; |
| 210 | |
| 211 | # Forward arguments to gcc. |
Ted Kremenek | f17ef3c | 2008-08-21 21:47:09 +0000 | [diff] [blame] | 212 | my $Status = system($CC,@ARGV); |
Ted Kremenek | cb344d0 | 2008-08-28 01:18:44 +0000 | [diff] [blame] | 213 | if ($Status) { exit($Status >> 8); } |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 214 | |
| 215 | # Get the analysis options. |
| 216 | my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'}; |
| 217 | if (!defined($Analyses)) { $Analyses = '-checker-cfref'; } |
| 218 | |
| 219 | # Determine the level of verbosity. |
| 220 | my $Verbose = 0; |
| 221 | if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; } |
| 222 | if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; } |
| 223 | |
| 224 | # Determine what clang executable to use. |
| 225 | my $Clang = $ENV{'CLANG'}; |
| 226 | if (!defined $Clang) { $Clang = 'clang'; } |
| 227 | |
| 228 | # Get the HTML output directory. |
| 229 | my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'}; |
| 230 | |
| 231 | |
| 232 | # Process the arguments. |
| 233 | foreach (my $i = 0; $i < scalar(@ARGV); ++$i) { |
| 234 | my $Arg = $ARGV[$i]; |
| 235 | |
| 236 | # Modes ccc-analyzer supports |
| 237 | if ($Arg eq '-E') { $Action = 'preprocess'; } |
| 238 | elsif ($Arg eq '-c') { $Action = 'compile'; } |
| 239 | elsif ($Arg =~ /^-print-prog-name/) { exit 0; } |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 240 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 241 | # Options with possible arguments that should pass through to compiler. |
| 242 | if (defined $CompileOptionMap{$Arg}) { |
| 243 | my $Cnt = $CompileOptionMap{$Arg}; |
| 244 | push @CompileOpts,$Arg; |
| 245 | while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; } |
| 246 | next; |
| 247 | } |
| 248 | |
| 249 | # Options with possible arguments that should pass through to linker. |
| 250 | if (defined $LinkerOptionMap{$Arg}) { |
| 251 | my $Cnt = $LinkerOptionMap{$Arg}; |
| 252 | push @LinkOpts,$Arg; |
| 253 | while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; } |
| 254 | next; |
| 255 | } |
| 256 | |
| 257 | # Options with possible arguments that should pass through to both compiler |
| 258 | # and the linker. |
| 259 | if (defined $CompilerLinkerOptionMap{$Arg}) { |
| 260 | my $Cnt = $CompilerLinkerOptionMap{$Arg}; |
| 261 | push @CompileOpts,$Arg; |
| 262 | push @LinkOpts,$Arg; |
| 263 | while ($Cnt > 0) { |
| 264 | ++$i; --$Cnt; |
| 265 | push @CompileOpts, $ARGV[$i]; |
| 266 | push @LinkOpts, $ARGV[$i]; |
| 267 | } |
| 268 | next; |
| 269 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 270 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 271 | # Ignored options. |
| 272 | if (defined $IgnoredOptionMap{$Arg}) { |
| 273 | my $Cnt = $IgnoredOptionMap{$Arg}; |
| 274 | while ($Cnt > 0) { |
| 275 | ++$i; --$Cnt; |
| 276 | } |
| 277 | next; |
| 278 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 279 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 280 | # Compile mode flags. |
| 281 | if ($Arg =~ /^-[D,I,U](.*)$/) { |
| 282 | my $Tmp = $Arg; |
| 283 | if ($1 eq '') { |
| 284 | # FIXME: Check if we are going off the end. |
| 285 | ++$i; |
| 286 | $Tmp = $Arg . $ARGV[$i]; |
| 287 | } |
| 288 | push @CompileOpts,$Tmp; |
| 289 | next; |
| 290 | } |
| 291 | |
| 292 | # Language. |
| 293 | if ($Arg eq '-x') { |
| 294 | $Lang = $ARGV[$i+1]; |
| 295 | ++$i; next; |
| 296 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 297 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 298 | # Output file. |
| 299 | if ($Arg eq '-o') { |
| 300 | ++$i; |
| 301 | $Output = $ARGV[$i]; |
| 302 | next; |
| 303 | } |
| 304 | |
| 305 | # Get the link mode. |
| 306 | if ($Arg =~ /^-[l,L,O]/) { |
| 307 | if ($Arg eq '-O') { push @LinkOpts,'-O1'; } |
| 308 | elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; } |
| 309 | else { push @LinkOpts,$Arg; } |
| 310 | next; |
| 311 | } |
| 312 | |
| 313 | if ($Arg =~ /^-std=/) { |
| 314 | push @CompileOpts,$Arg; |
| 315 | next; |
| 316 | } |
| 317 | |
| 318 | # if ($Arg =~ /^-f/) { |
| 319 | # # FIXME: Not sure if the remaining -fxxxx options have no arguments. |
| 320 | # push @CompileOpts,$Arg; |
| 321 | # push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts. |
| 322 | # } |
| 323 | |
| 324 | # Get the compiler/link mode. |
| 325 | if ($Arg =~ /^-F(.+)$/) { |
| 326 | my $Tmp = $Arg; |
| 327 | if ($1 eq '') { |
| 328 | # FIXME: Check if we are going off the end. |
| 329 | ++$i; |
| 330 | $Tmp = $Arg . $ARGV[$i]; |
| 331 | } |
| 332 | push @CompileOpts,$Tmp; |
| 333 | push @LinkOpts,$Tmp; |
| 334 | next; |
| 335 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 336 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 337 | # Input files. |
| 338 | if ($Arg eq '-filelist') { |
| 339 | # FIXME: Make sure we aren't walking off the end. |
| 340 | open(IN, $ARGV[$i+1]); |
| 341 | while (<IN>) { s/\015?\012//; push @Files,$_; } |
| 342 | close(IN); |
| 343 | ++$i; next; |
| 344 | } |
| 345 | |
| 346 | if (!($Arg =~ /^-/)) { |
| 347 | push @Files,$Arg; next; |
| 348 | } |
| 349 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 350 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 351 | if ($Action eq 'compile' or $Action eq 'link') { |
| 352 | foreach my $file (@Files) { |
| 353 | # Determine the language for the file. |
| 354 | my $FileLang = $Lang; |
| 355 | |
| 356 | if (!defined($FileLang)) { |
| 357 | # Infer the language from the extension. |
| 358 | if ($file =~ /[.]([^.]+)$/) { |
| 359 | $FileLang = $LangMap{$1}; |
| 360 | } |
| 361 | } |
Ted Kremenek | 1262fc4 | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 362 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 363 | next if (!defined $FileLang); |
| 364 | |
| 365 | my @AnalyzeArgs; |
| 366 | |
| 367 | if ($FileLang ne 'unknown') { |
| 368 | push @AnalyzeArgs,'-x'; |
| 369 | push @AnalyzeArgs,$FileLang; |
| 370 | } |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 371 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 372 | push @AnalyzeArgs,@CompileOpts; |
| 373 | push @AnalyzeArgs,$file; |
| 374 | |
| 375 | Analyze($Clang, \@AnalyzeArgs, $FileLang, $Output, |
| 376 | $Verbose, $HtmlDir, $file, $Analyses); |
| 377 | } |
| 378 | } |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 379 | |
Ted Kremenek | 948e06b | 2008-08-27 22:30:34 +0000 | [diff] [blame] | 380 | exit($Status >> 8); |
| 381 | |
| 382 | |