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