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