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