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 interpose between the build system and gcc. It invokes 11 # both gcc and the static analyzer. 12 # 13 ##===----------------------------------------------------------------------===## 14 15 use strict; 16 use warnings; 17 use FindBin; 18 use Cwd qw/ getcwd abs_path /; 19 use File::Temp qw/ tempfile /; 20 use File::Path qw / mkpath /; 21 use File::Basename; 22 use Text::ParseWords; 23 24 ##===----------------------------------------------------------------------===## 25 # Compiler command setup. 26 ##===----------------------------------------------------------------------===## 27 28 my $Compiler; 29 my $Clang; 30 31 if ($FindBin::Script =~ /c\+\+-analyzer/) { 32 $Compiler = $ENV{'CCC_CXX'}; 33 if (!defined $Compiler) { $Compiler = "g++"; } 34 35 $Clang = $ENV{'CLANG_CXX'}; 36 if (!defined $Clang) { $Clang = 'clang++'; } 37 } 38 else { 39 $Compiler = $ENV{'CCC_CC'}; 40 if (!defined $Compiler) { $Compiler = "gcc"; } 41 42 $Clang = $ENV{'CLANG'}; 43 if (!defined $Clang) { $Clang = 'clang'; } 44 } 45 46 ##===----------------------------------------------------------------------===## 47 # Cleanup. 48 ##===----------------------------------------------------------------------===## 49 50 my $ReportFailures = $ENV{'CCC_REPORT_FAILURES'}; 51 if (!defined $ReportFailures) { $ReportFailures = 1; } 52 53 my $CleanupFile; 54 my $ResultFile; 55 56 # Remove any stale files at exit. 57 END { 58 if (defined $ResultFile && -z $ResultFile) { 59 `rm -f $ResultFile`; 60 } 61 if (defined $CleanupFile) { 62 `rm -f $CleanupFile`; 63 } 64 } 65 66 ##----------------------------------------------------------------------------## 67 # Process Clang Crashes. 68 ##----------------------------------------------------------------------------## 69 70 sub GetPPExt { 71 my $Lang = shift; 72 if ($Lang =~ /objective-c\+\+/) { return ".mii" }; 73 if ($Lang =~ /objective-c/) { return ".mi"; } 74 if ($Lang =~ /c\+\+/) { return ".ii"; } 75 return ".i"; 76 } 77 78 # Set this to 1 if we want to include 'parser rejects' files. 79 my $IncludeParserRejects = 0; 80 my $ParserRejects = "Parser Rejects"; 81 82 my $AttributeIgnored = "Attribute Ignored"; 83 84 sub ProcessClangFailure { 85 my ($Clang, $Lang, $file, $Args, $HtmlDir, $ErrorType, $ofile) = @_; 86 my $Dir = "$HtmlDir/failures"; 87 mkpath $Dir; 88 89 my $prefix = "clang_crash"; 90 if ($ErrorType eq $ParserRejects) { 91 $prefix = "clang_parser_rejects"; 92 } 93 elsif ($ErrorType eq $AttributeIgnored) { 94 $prefix = "clang_attribute_ignored"; 95 } 96 97 # Generate the preprocessed file with Clang. 98 my ($PPH, $PPFile) = tempfile( $prefix . "_XXXXXX", 99 SUFFIX => GetPPExt($Lang), 100 DIR => $Dir); 101 system $Clang, @$Args, "-E", "-o", $PPFile; 102 close ($PPH); 103 104 # Create the info file. 105 open (OUT, ">", "$PPFile.info.txt") or die "Cannot open $PPFile.info.txt\n"; 106 print OUT abs_path($file), "\n"; 107 print OUT "$ErrorType\n"; 108 print OUT "@$Args\n"; 109 close OUT; 110 `uname -a >> $PPFile.info.txt 2>&1`; 111 `$Compiler -v >> $PPFile.info.txt 2>&1`; 112 system 'mv',$ofile,"$PPFile.stderr.txt"; 113 return (basename $PPFile); 114 } 115 116 ##----------------------------------------------------------------------------## 117 # Running the analyzer. 118 ##----------------------------------------------------------------------------## 119 120 sub GetCCArgs { 121 my $mode = shift; 122 my $Args = shift; 123 124 pipe (FROM_CHILD, TO_PARENT); 125 my $pid = fork(); 126 if ($pid == 0) { 127 close FROM_CHILD; 128 open(STDOUT,">&", \*TO_PARENT); 129 open(STDERR,">&", \*TO_PARENT); 130 exec $Clang, "-###", $mode, @$Args; 131 } 132 close(TO_PARENT); 133 my $line; 134 while (<FROM_CHILD>) { 135 next if (!/-cc1/); 136 $line = $_; 137 } 138 139 waitpid($pid,0); 140 close(FROM_CHILD); 141 142 die "could not find clang line\n" if (!defined $line); 143 # Strip the newline and initial whitspace 144 chomp $line; 145 $line =~ s/^\s+//; 146 my @items = quotewords('\s+', 0, $line); 147 my $cmd = shift @items; 148 die "cannot find 'clang' in 'clang' command\n" if (!($cmd =~ /clang/)); 149 return \@items; 150 } 151 152 sub Analyze { 153 my ($Clang, $OriginalArgs, $AnalyzeArgs, $Lang, $Output, $Verbose, $HtmlDir, 154 $file) = @_; 155 156 my @Args = @$OriginalArgs; 157 my $Cmd; 158 my @CmdArgs; 159 my @CmdArgsSansAnalyses; 160 161 if ($Lang =~ /header/) { 162 exit 0 if (!defined ($Output)); 163 $Cmd = 'cp'; 164 push @CmdArgs, $file; 165 # Remove the PCH extension. 166 $Output =~ s/[.]gch$//; 167 push @CmdArgs, $Output; 168 @CmdArgsSansAnalyses = @CmdArgs; 169 } 170 else { 171 $Cmd = $Clang; 172 if ($Lang eq "objective-c" || $Lang eq "objective-c++") { 173 push @Args,'-DIBOutlet=__attribute__((iboutlet))'; 174 push @Args,'-DIBOutletCollection(ClassName)=__attribute__((iboutletcollection)))'; 175 push @Args,'-DIBAction=void)__attribute__((ibaction)'; 176 } 177 178 # Create arguments for doing regular parsing. 179 my $SyntaxArgs = GetCCArgs("-fsyntax-only", \@Args); 180 @CmdArgsSansAnalyses = @$SyntaxArgs; 181 182 # Create arguments for doing static analysis. 183 if (defined $ResultFile) { 184 push @Args, '-o', $ResultFile; 185 } 186 elsif (defined $HtmlDir) { 187 push @Args, '-o', $HtmlDir; 188 } 189 if ($Verbose) { 190 push @Args, "-Xclang", "-analyzer-display-progress"; 191 } 192 193 foreach my $arg (@$AnalyzeArgs) { 194 push @Args, "-Xclang", $arg; 195 } 196 197 # Display Ubiviz graph? 198 if (defined $ENV{'CCC_UBI'}) { 199 push @Args, "-Xclang", "-analyzer-viz-egraph-ubigraph"; 200 } 201 202 my $AnalysisArgs = GetCCArgs("--analyze", \@Args); 203 @CmdArgs = @$AnalysisArgs; 204 } 205 206 my @PrintArgs; 207 my $dir; 208 209 if ($Verbose) { 210 $dir = getcwd(); 211 print STDERR "\n[LOCATION]: $dir\n"; 212 push @PrintArgs,"'$Cmd'"; 213 foreach my $arg (@CmdArgs) { 214 push @PrintArgs,"\'$arg\'"; 215 } 216 } 217 if ($Verbose == 1) { 218 # We MUST print to stderr. Some clients use the stdout output of 219 # gcc for various purposes. 220 print STDERR join(' ', @PrintArgs); 221 print STDERR "\n"; 222 } 223 elsif ($Verbose == 2) { 224 print STDERR "#SHELL (cd '$dir' && @PrintArgs)\n"; 225 } 226 227 # Capture the STDERR of clang and send it to a temporary file. 228 # Capture the STDOUT of clang and reroute it to ccc-analyzer's STDERR. 229 # We save the output file in the 'crashes' directory if clang encounters 230 # any problems with the file. 231 pipe (FROM_CHILD, TO_PARENT); 232 my $pid = fork(); 233 if ($pid == 0) { 234 close FROM_CHILD; 235 open(STDOUT,">&", \*TO_PARENT); 236 open(STDERR,">&", \*TO_PARENT); 237 exec $Cmd, @CmdArgs; 238 } 239 240 close TO_PARENT; 241 my ($ofh, $ofile) = tempfile("clang_output_XXXXXX", DIR => $HtmlDir); 242 243 while (<FROM_CHILD>) { 244 print $ofh $_; 245 print STDERR $_; 246 } 247 248 waitpid($pid,0); 249 close(FROM_CHILD); 250 my $Result = $?; 251 252 # Did the command die because of a signal? 253 if ($ReportFailures) { 254 if ($Result & 127 and $Cmd eq $Clang and defined $HtmlDir) { 255 ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses, 256 $HtmlDir, "Crash", $ofile); 257 } 258 elsif ($Result) { 259 if ($IncludeParserRejects && !($file =~/conftest/)) { 260 ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses, 261 $HtmlDir, $ParserRejects, $ofile); 262 } 263 } 264 else { 265 # Check if there were any unhandled attributes. 266 if (open(CHILD, $ofile)) { 267 my %attributes_not_handled; 268 269 # Don't flag warnings about the following attributes that we 270 # know are currently not supported by Clang. 271 $attributes_not_handled{"cdecl"} = 1; 272 273 my $ppfile; 274 while (<CHILD>) { 275 next if (! /warning: '([^\']+)' attribute ignored/); 276 277 # Have we already spotted this unhandled attribute? 278 next if (defined $attributes_not_handled{$1}); 279 $attributes_not_handled{$1} = 1; 280 281 # Get the name of the attribute file. 282 my $dir = "$HtmlDir/failures"; 283 my $afile = "$dir/attribute_ignored_$1.txt"; 284 285 # Only create another preprocessed file if the attribute file 286 # doesn't exist yet. 287 next if (-e $afile); 288 289 # Add this file to the list of files that contained this attribute. 290 # Generate a preprocessed file if we haven't already. 291 if (!(defined $ppfile)) { 292 $ppfile = ProcessClangFailure($Clang, $Lang, $file, 293 \@CmdArgsSansAnalyses, 294 $HtmlDir, $AttributeIgnored, $ofile); 295 } 296 297 mkpath $dir; 298 open(AFILE, ">$afile"); 299 print AFILE "$ppfile\n"; 300 close(AFILE); 301 } 302 close CHILD; 303 } 304 } 305 } 306 307 unlink($ofile); 308 } 309 310 ##----------------------------------------------------------------------------## 311 # Lookup tables. 312 ##----------------------------------------------------------------------------## 313 314 my %CompileOptionMap = ( 315 '-nostdinc' => 0, 316 '-fblocks' => 0, 317 '-fno-builtin' => 0, 318 '-fobjc-gc-only' => 0, 319 '-fobjc-gc' => 0, 320 '-ffreestanding' => 0, 321 '-include' => 1, 322 '-idirafter' => 1, 323 '-imacros' => 1, 324 '-iprefix' => 1, 325 '-iquote' => 1, 326 '-isystem' => 1, 327 '-iwithprefix' => 1, 328 '-iwithprefixbefore' => 1 329 ); 330 331 my %LinkerOptionMap = ( 332 '-framework' => 1 333 ); 334 335 my %CompilerLinkerOptionMap = ( 336 '-isysroot' => 1, 337 '-arch' => 1, 338 '-m32' => 0, 339 '-m64' => 0, 340 '-v' => 0, 341 '-fpascal-strings' => 0, 342 '-mmacosx-version-min' => 0, # This is really a 1 argument, but always has '=' 343 '-miphoneos-version-min' => 0 # This is really a 1 argument, but always has '=' 344 ); 345 346 my %IgnoredOptionMap = ( 347 '-MT' => 1, # Ignore these preprocessor options. 348 '-MF' => 1, 349 350 '-fsyntax-only' => 0, 351 '-save-temps' => 0, 352 '-install_name' => 1, 353 '-exported_symbols_list' => 1, 354 '-current_version' => 1, 355 '-compatibility_version' => 1, 356 '-init' => 1, 357 '-e' => 1, 358 '-seg1addr' => 1, 359 '-bundle_loader' => 1, 360 '-multiply_defined' => 1, 361 '-sectorder' => 3, 362 '--param' => 1, 363 '-u' => 1 364 ); 365 366 my %LangMap = ( 367 'c' => 'c', 368 'cp' => 'c++', 369 'cpp' => 'c++', 370 'cc' => 'c++', 371 'ii' => 'c++', 372 'i' => 'c-cpp-output', 373 'm' => 'objective-c', 374 'mi' => 'objective-c-cpp-output', 375 'mm' => 'objective-c++' 376 ); 377 378 my %UniqueOptions = ( 379 '-isysroot' => 0 380 ); 381 382 ##----------------------------------------------------------------------------## 383 # Languages accepted. 384 ##----------------------------------------------------------------------------## 385 386 my %LangsAccepted = ( 387 "objective-c" => 1, 388 "c" => 1, 389 "c++" => 1, 390 "objective-c++" => 1 391 ); 392 393 ##----------------------------------------------------------------------------## 394 # Main Logic. 395 ##----------------------------------------------------------------------------## 396 397 my $Action = 'link'; 398 my @CompileOpts; 399 my @LinkOpts; 400 my @Files; 401 my $Lang; 402 my $Output; 403 my %Uniqued; 404 405 # Forward arguments to gcc. 406 my $Status = system($Compiler,@ARGV); 407 if (defined $ENV{'CCC_ANALYZER_LOG'}) { 408 print "$Compiler @ARGV\n"; 409 } 410 if ($Status) { exit($Status >> 8); } 411 412 # Get the analysis options. 413 my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'}; 414 415 # Get the store model. 416 my $StoreModel = $ENV{'CCC_ANALYZER_STORE_MODEL'}; 417 418 # Get the constraints engine. 419 my $ConstraintsModel = $ENV{'CCC_ANALYZER_CONSTRAINTS_MODEL'}; 420 421 # Get the output format. 422 my $OutputFormat = $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'}; 423 if (!defined $OutputFormat) { $OutputFormat = "html"; } 424 425 # Determine the level of verbosity. 426 my $Verbose = 0; 427 if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; } 428 if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; } 429 430 # Get the HTML output directory. 431 my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'}; 432 433 my %DisabledArchs = ('ppc' => 1, 'ppc64' => 1); 434 my %ArchsSeen; 435 my $HadArch = 0; 436 437 # Process the arguments. 438 foreach (my $i = 0; $i < scalar(@ARGV); ++$i) { 439 my $Arg = $ARGV[$i]; 440 my ($ArgKey) = split /=/,$Arg,2; 441 442 # Modes ccc-analyzer supports 443 if ($Arg =~ /^-(E|MM?)$/) { $Action = 'preprocess'; } 444 elsif ($Arg eq '-c') { $Action = 'compile'; } 445 elsif ($Arg =~ /^-print-prog-name/) { exit 0; } 446 447 # Specially handle duplicate cases of -arch 448 if ($Arg eq "-arch") { 449 my $arch = $ARGV[$i+1]; 450 # We don't want to process 'ppc' because of Clang's lack of support 451 # for Altivec (also some #defines won't likely be defined correctly, etc.) 452 if (!(defined $DisabledArchs{$arch})) { $ArchsSeen{$arch} = 1; } 453 $HadArch = 1; 454 ++$i; 455 next; 456 } 457 458 # Options with possible arguments that should pass through to compiler. 459 if (defined $CompileOptionMap{$ArgKey}) { 460 my $Cnt = $CompileOptionMap{$ArgKey}; 461 push @CompileOpts,$Arg; 462 while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; } 463 next; 464 } 465 466 # Options with possible arguments that should pass through to linker. 467 if (defined $LinkerOptionMap{$ArgKey}) { 468 my $Cnt = $LinkerOptionMap{$ArgKey}; 469 push @LinkOpts,$Arg; 470 while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; } 471 next; 472 } 473 474 # Options with possible arguments that should pass through to both compiler 475 # and the linker. 476 if (defined $CompilerLinkerOptionMap{$ArgKey}) { 477 my $Cnt = $CompilerLinkerOptionMap{$ArgKey}; 478 479 # Check if this is an option that should have a unique value, and if so 480 # determine if the value was checked before. 481 if ($UniqueOptions{$Arg}) { 482 if (defined $Uniqued{$Arg}) { 483 $i += $Cnt; 484 next; 485 } 486 $Uniqued{$Arg} = 1; 487 } 488 489 push @CompileOpts,$Arg; 490 push @LinkOpts,$Arg; 491 492 while ($Cnt > 0) { 493 ++$i; --$Cnt; 494 push @CompileOpts, $ARGV[$i]; 495 push @LinkOpts, $ARGV[$i]; 496 } 497 next; 498 } 499 500 # Ignored options. 501 if (defined $IgnoredOptionMap{$ArgKey}) { 502 my $Cnt = $IgnoredOptionMap{$ArgKey}; 503 while ($Cnt > 0) { 504 ++$i; --$Cnt; 505 } 506 next; 507 } 508 509 # Compile mode flags. 510 if ($Arg =~ /^-[D,I,U](.*)$/) { 511 my $Tmp = $Arg; 512 if ($1 eq '') { 513 # FIXME: Check if we are going off the end. 514 ++$i; 515 $Tmp = $Arg . $ARGV[$i]; 516 } 517 push @CompileOpts,$Tmp; 518 next; 519 } 520 521 # Language. 522 if ($Arg eq '-x') { 523 $Lang = $ARGV[$i+1]; 524 ++$i; next; 525 } 526 527 # Output file. 528 if ($Arg eq '-o') { 529 ++$i; 530 $Output = $ARGV[$i]; 531 next; 532 } 533 534 # Get the link mode. 535 if ($Arg =~ /^-[l,L,O]/) { 536 if ($Arg eq '-O') { push @LinkOpts,'-O1'; } 537 elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; } 538 else { push @LinkOpts,$Arg; } 539 next; 540 } 541 542 if ($Arg =~ /^-std=/) { 543 push @CompileOpts,$Arg; 544 next; 545 } 546 547 # if ($Arg =~ /^-f/) { 548 # # FIXME: Not sure if the remaining -fxxxx options have no arguments. 549 # push @CompileOpts,$Arg; 550 # push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts. 551 # } 552 553 # Get the compiler/link mode. 554 if ($Arg =~ /^-F(.+)$/) { 555 my $Tmp = $Arg; 556 if ($1 eq '') { 557 # FIXME: Check if we are going off the end. 558 ++$i; 559 $Tmp = $Arg . $ARGV[$i]; 560 } 561 push @CompileOpts,$Tmp; 562 push @LinkOpts,$Tmp; 563 next; 564 } 565 566 # Input files. 567 if ($Arg eq '-filelist') { 568 # FIXME: Make sure we aren't walking off the end. 569 open(IN, $ARGV[$i+1]); 570 while (<IN>) { s/\015?\012//; push @Files,$_; } 571 close(IN); 572 ++$i; 573 next; 574 } 575 576 # Handle -Wno-. We don't care about extra warnings, but 577 # we should suppress ones that we don't want to see. 578 if ($Arg =~ /^-Wno-/) { 579 push @CompileOpts, $Arg; 580 next; 581 } 582 583 if (!($Arg =~ /^-/)) { 584 push @Files, $Arg; 585 next; 586 } 587 } 588 589 if ($Action eq 'compile' or $Action eq 'link') { 590 my @Archs = keys %ArchsSeen; 591 # Skip the file if we don't support the architectures specified. 592 exit 0 if ($HadArch && scalar(@Archs) == 0); 593 594 foreach my $file (@Files) { 595 # Determine the language for the file. 596 my $FileLang = $Lang; 597 598 if (!defined($FileLang)) { 599 # Infer the language from the extension. 600 if ($file =~ /[.]([^.]+)$/) { 601 $FileLang = $LangMap{$1}; 602 } 603 } 604 605 # FileLang still not defined? Skip the file. 606 next if (!defined $FileLang); 607 608 # Language not accepted? 609 next if (!defined $LangsAccepted{$FileLang}); 610 611 my @CmdArgs; 612 my @AnalyzeArgs; 613 614 if ($FileLang ne 'unknown') { 615 push @CmdArgs, '-x', $FileLang; 616 } 617 618 if (defined $StoreModel) { 619 push @AnalyzeArgs, "-analyzer-store=$StoreModel"; 620 } 621 622 if (defined $ConstraintsModel) { 623 push @AnalyzeArgs, "-analyzer-constraints=$ConstraintsModel"; 624 } 625 626 if (defined $Analyses) { 627 push @AnalyzeArgs, split '\s+', $Analyses; 628 } 629 630 if (defined $OutputFormat) { 631 push @AnalyzeArgs, "-analyzer-output=" . $OutputFormat; 632 if ($OutputFormat =~ /plist/) { 633 # Change "Output" to be a file. 634 my ($h, $f) = tempfile("report-XXXXXX", SUFFIX => ".plist", 635 DIR => $HtmlDir); 636 $ResultFile = $f; 637 # If the HtmlDir is not set, we sould clean up the plist files. 638 if (!defined $HtmlDir || -z $HtmlDir) { 639 $CleanupFile = $f; 640 } 641 } 642 } 643 644 push @CmdArgs, @CompileOpts; 645 push @CmdArgs, $file; 646 647 if (scalar @Archs) { 648 foreach my $arch (@Archs) { 649 my @NewArgs; 650 push @NewArgs, '-arch', $arch; 651 push @NewArgs, @CmdArgs; 652 Analyze($Clang, \@NewArgs, \@AnalyzeArgs, $FileLang, $Output, 653 $Verbose, $HtmlDir, $file); 654 } 655 } 656 else { 657 Analyze($Clang, \@CmdArgs, \@AnalyzeArgs, $FileLang, $Output, 658 $Verbose, $HtmlDir, $file); 659 } 660 } 661 } 662 663 exit($Status >> 8); 664 665