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