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