3 # The LLVM Compiler Infrastructure
5 # This file is distributed under the University of Illinois Open Source
6 # License. See LICENSE.TXT for details.
8 ##===----------------------------------------------------------------------===##
10 # A script designed to interpose between the build system and gcc. It invokes
11 # both gcc and the static analyzer.
13 ##===----------------------------------------------------------------------===##
18 use Cwd qw
/ getcwd abs_path /;
19 use File
::Temp qw
/ tempfile /;
20 use File
::Path qw
/ mkpath /;
24 ##===----------------------------------------------------------------------===##
25 # Compiler command setup.
26 ##===----------------------------------------------------------------------===##
31 if ($FindBin::Script
=~ /c\+\+-analyzer/) {
32 $Compiler = $ENV{'CCC_CXX'};
33 if (!defined $Compiler) { $Compiler = "g++"; }
35 $Clang = $ENV{'CLANG_CXX'};
36 if (!defined $Clang) { $Clang = 'clang++'; }
39 $Compiler = $ENV{'CCC_CC'};
40 if (!defined $Compiler) { $Compiler = "gcc"; }
42 $Clang = $ENV{'CLANG'};
43 if (!defined $Clang) { $Clang = 'clang'; }
46 ##===----------------------------------------------------------------------===##
48 ##===----------------------------------------------------------------------===##
50 my $ReportFailures = $ENV{'CCC_REPORT_FAILURES'};
51 if (!defined $ReportFailures) { $ReportFailures = 1; }
56 # Remove any stale files at exit.
58 if (defined $CleanupFile && -z
$CleanupFile) {
63 ##----------------------------------------------------------------------------##
64 # Process Clang Crashes.
65 ##----------------------------------------------------------------------------##
69 if ($Lang =~ /objective-c\+\+/) { return ".mii" };
70 if ($Lang =~ /objective-c/) { return ".mi"; }
71 if ($Lang =~ /c\+\+/) { return ".ii"; }
75 # Set this to 1 if we want to include 'parser rejects' files.
76 my $IncludeParserRejects = 0;
77 my $ParserRejects = "Parser Rejects";
79 my $AttributeIgnored = "Attribute Ignored";
81 sub ProcessClangFailure
{
82 my ($Clang, $Lang, $file, $Args, $HtmlDir, $ErrorType, $ofile) = @_;
83 my $Dir = "$HtmlDir/failures";
86 my $prefix = "clang_crash";
87 if ($ErrorType eq $ParserRejects) {
88 $prefix = "clang_parser_rejects";
90 elsif ($ErrorType eq $AttributeIgnored) {
91 $prefix = "clang_attribute_ignored";
94 # Generate the preprocessed file with Clang.
95 my ($PPH, $PPFile) = tempfile
( $prefix . "_XXXXXX",
96 SUFFIX
=> GetPPExt
($Lang),
98 system $Clang, @
$Args, "-E", "-o", $PPFile;
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";
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);
113 ##----------------------------------------------------------------------------##
114 # Running the analyzer.
115 ##----------------------------------------------------------------------------##
120 pipe (FROM_CHILD
, TO_PARENT
);
124 open(STDOUT
,">&", \
*TO_PARENT
);
125 open(STDERR
,">&", \
*TO_PARENT
);
126 exec $Clang, "-###", "-fsyntax-only", @
$Args;
130 while (<FROM_CHILD
>) {
138 die "could not find clang line\n" if (!defined $line);
139 # Strip the newline and initial whitspace
142 my @items = quotewords
('\s+', 0, $line);
143 my $cmd = shift @items;
144 die "cannot find 'clang' in 'clang' command\n" if (!($cmd =~ /clang/));
149 my ($Clang, $Args, $AnalyzeArgs, $Lang, $Output, $Verbose, $HtmlDir,
150 $file, $Analyses) = @_;
152 $Args = GetCCArgs
($Args);
157 my @CmdArgsSansAnalyses;
159 if ($Lang =~ /header/) {
160 exit 0 if (!defined ($Output));
163 # Remove the PCH extension.
164 $Output =~ s/[.]gch$//;
165 push @CmdArgs,$Output;
166 @CmdArgsSansAnalyses = @CmdArgs;
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);
180 if (defined $ENV{"CCC_EXPERIMENTAL_CHECKS"}) {
181 push @CmdArgs,"-analyzer-experimental-internal-checks";
182 push @CmdArgs,"-analyzer-experimental-checks";
188 # Add the analysis arguments passed down from scan-build.
189 foreach my $Arg (@
$AnalyzeArgs) {
197 if (defined $ResultFile) {
199 push @CmdArgs, $ResultFile;
201 elsif (defined $HtmlDir) {
203 push @CmdArgs, $HtmlDir;
209 print STDERR
"\n[LOCATION]: $dir\n";
210 push @PrintArgs,"'$Cmd'";
211 foreach my $arg (@CmdArgs) { push @PrintArgs,"\'$arg\'"; }
215 # We MUST print to stderr. Some clients use the stdout output of
216 # gcc for various purposes.
217 print STDERR
join(' ',@PrintArgs);
220 elsif ($Verbose == 2) {
221 print STDERR
"#SHELL (cd '$dir' && @PrintArgs)\n";
224 if (defined $ENV{'CCC_UBI'}) {
225 push @CmdArgs,"--analyzer-viz-egraph-ubigraph";
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
);
236 open(STDOUT
,">&", \
*TO_PARENT
);
237 open(STDERR
,">&", \
*TO_PARENT
);
242 my ($ofh, $ofile) = tempfile
("clang_output_XXXXXX", DIR
=> $HtmlDir);
244 while (<FROM_CHILD
>) {
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);
260 if ($IncludeParserRejects && !($file =~/conftest/)) {
261 ProcessClangFailure
($Clang, $Lang, $file, \
@CmdArgsSansAnalyses,
262 $HtmlDir, $ParserRejects, $ofile);
266 # Check if there were any unhandled attributes.
267 if (open(CHILD
, $ofile)) {
268 my %attributes_not_handled;
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;
276 next if (! /warning: '([^\']+)' attribute ignored/);
278 # Have we already spotted this unhandled attribute?
279 next if (defined $attributes_not_handled{$1});
280 $attributes_not_handled{$1} = 1;
282 # Get the name of the attribute file.
283 my $dir = "$HtmlDir/failures";
284 my $afile = "$dir/attribute_ignored_$1.txt";
286 # Only create another preprocessed file if the attribute file
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);
299 open(AFILE
, ">$afile");
300 print AFILE
"$ppfile\n";
311 ##----------------------------------------------------------------------------##
313 ##----------------------------------------------------------------------------##
315 my %CompileOptionMap = (
319 '-fobjc-gc-only' => 0,
321 '-ffreestanding' => 0,
329 '-iwithprefixbefore' => 1
332 my %LinkerOptionMap = (
336 my %CompilerLinkerOptionMap = (
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 '='
347 my %IgnoredOptionMap = (
348 '-MT' => 1, # Ignore these preprocessor options.
351 '-fsyntax-only' => 0,
353 '-install_name' => 1,
354 '-exported_symbols_list' => 1,
355 '-current_version' => 1,
356 '-compatibility_version' => 1,
360 '-bundle_loader' => 1,
361 '-multiply_defined' => 1,
372 'i' => 'c-cpp-output',
373 'm' => 'objective-c',
374 'mi' => 'objective-c-cpp-output'
377 my %UniqueOptions = (
381 ##----------------------------------------------------------------------------##
382 # Languages accepted.
383 ##----------------------------------------------------------------------------##
385 my %LangsAccepted = (
390 if (defined $ENV{'CCC_ANALYZER_CPLUSPLUS'}) {
391 $LangsAccepted{"c++"} = 1;
392 $LangsAccepted{"objective-c++"} = 1;
395 ##----------------------------------------------------------------------------##
397 ##----------------------------------------------------------------------------##
407 # Forward arguments to gcc.
408 my $Status = system($Compiler,@ARGV);
409 if (defined $ENV{'CCC_ANALYZER_LOG'}) {
410 print "$Compiler @ARGV\n";
412 if ($Status) { exit($Status >> 8); }
414 # Get the analysis options.
415 my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
416 if (!defined($Analyses)) { $Analyses = '-analyzer-check-objc-mem'; }
418 # Get the store model.
419 my $StoreModel = $ENV{'CCC_ANALYZER_STORE_MODEL'};
420 if (!defined $StoreModel) { $StoreModel = "region"; }
422 # Get the constraints engine.
423 my $ConstraintsModel = $ENV{'CCC_ANALYZER_CONSTRAINTS_MODEL'};
424 if (!defined $ConstraintsModel) { $ConstraintsModel = "range"; }
426 # Get the output format.
427 my $OutputFormat = $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'};
428 if (!defined $OutputFormat) { $OutputFormat = "html"; }
430 # Determine the level of verbosity.
432 if (defined $ENV{CCC_ANALYZER_VERBOSE
}) { $Verbose = 1; }
433 if (defined $ENV{CCC_ANALYZER_LOG
}) { $Verbose = 2; }
435 # Get the HTML output directory.
436 my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'};
438 my %DisabledArchs = ('ppc' => 1, 'ppc64' => 1);
442 # Process the arguments.
443 foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
445 my ($ArgKey) = split /=/,$Arg,2;
447 # Modes ccc-analyzer supports
448 if ($Arg =~ /^-(E|MM?)$/) { $Action = 'preprocess'; }
449 elsif ($Arg eq '-c') { $Action = 'compile'; }
450 elsif ($Arg =~ /^-print-prog-name/) { exit 0; }
452 # Specially handle duplicate cases of -arch
453 if ($Arg eq "-arch") {
454 my $arch = $ARGV[$i+1];
455 # We don't want to process 'ppc' because of Clang's lack of support
456 # for Altivec (also some #defines won't likely be defined correctly, etc.)
457 if (!(defined $DisabledArchs{$arch})) { $ArchsSeen{$arch} = 1; }
463 # Options with possible arguments that should pass through to compiler.
464 if (defined $CompileOptionMap{$ArgKey}) {
465 my $Cnt = $CompileOptionMap{$ArgKey};
466 push @CompileOpts,$Arg;
467 while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; }
471 # Options with possible arguments that should pass through to linker.
472 if (defined $LinkerOptionMap{$ArgKey}) {
473 my $Cnt = $LinkerOptionMap{$ArgKey};
475 while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
479 # Options with possible arguments that should pass through to both compiler
481 if (defined $CompilerLinkerOptionMap{$ArgKey}) {
482 my $Cnt = $CompilerLinkerOptionMap{$ArgKey};
484 # Check if this is an option that should have a unique value, and if so
485 # determine if the value was checked before.
486 if ($UniqueOptions{$Arg}) {
487 if (defined $Uniqued{$Arg}) {
494 push @CompileOpts,$Arg;
499 push @CompileOpts, $ARGV[$i];
500 push @LinkOpts, $ARGV[$i];
506 if (defined $IgnoredOptionMap{$ArgKey}) {
507 my $Cnt = $IgnoredOptionMap{$ArgKey};
514 # Compile mode flags.
515 if ($Arg =~ /^-[D,I,U](.*)$/) {
518 # FIXME: Check if we are going off the end.
520 $Tmp = $Arg . $ARGV[$i];
522 push @CompileOpts,$Tmp;
540 if ($Arg =~ /^-[l,L,O]/) {
541 if ($Arg eq '-O') { push @LinkOpts,'-O1'; }
542 elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; }
543 else { push @LinkOpts,$Arg; }
547 if ($Arg =~ /^-std=/) {
548 push @CompileOpts,$Arg;
552 # if ($Arg =~ /^-f/) {
553 # # FIXME: Not sure if the remaining -fxxxx options have no arguments.
554 # push @CompileOpts,$Arg;
555 # push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts.
558 # Get the compiler/link mode.
559 if ($Arg =~ /^-F(.+)$/) {
562 # FIXME: Check if we are going off the end.
564 $Tmp = $Arg . $ARGV[$i];
566 push @CompileOpts,$Tmp;
572 if ($Arg eq '-filelist') {
573 # FIXME: Make sure we aren't walking off the end.
574 open(IN
, $ARGV[$i+1]);
575 while (<IN
>) { s/\015?\012//; push @Files,$_; }
581 # Handle -Wno-. We don't care about extra warnings, but
582 # we should suppress ones that we don't want to see.
583 if ($Arg =~ /^-Wno-/) {
584 push @CompileOpts, $Arg;
588 if (!($Arg =~ /^-/)) {
594 if ($Action eq 'compile' or $Action eq 'link') {
595 my @Archs = keys %ArchsSeen;
596 # Skip the file if we don't support the architectures specified.
597 exit 0 if ($HadArch && scalar(@Archs) == 0);
599 foreach my $file (@Files) {
600 # Determine the language for the file.
601 my $FileLang = $Lang;
603 if (!defined($FileLang)) {
604 # Infer the language from the extension.
605 if ($file =~ /[.]([^.]+)$/) {
606 $FileLang = $LangMap{$1};
610 # FileLang still not defined? Skip the file.
611 next if (!defined $FileLang);
613 # Language not accepted?
614 next if (!defined $LangsAccepted{$FileLang});
619 if ($FileLang ne 'unknown') {
621 push @CmdArgs,$FileLang;
624 if (defined $StoreModel) {
625 push @AnalyzeArgs, "-analyzer-store=$StoreModel";
628 if (defined $ConstraintsModel) {
629 push @AnalyzeArgs, "-analyzer-constraints=$ConstraintsModel";
632 if (defined $OutputFormat) {
633 push @AnalyzeArgs, "-analyzer-output=" . $OutputFormat;
634 if ($OutputFormat =~ /plist/) {
635 # Change "Output" to be a file.
636 my ($h, $f) = tempfile
("report-XXXXXX", SUFFIX
=> ".plist",
643 push @CmdArgs,@CompileOpts;
647 foreach my $arch (@Archs) {
649 push @NewArgs, '-arch';
650 push @NewArgs, $arch;
651 push @NewArgs, @CmdArgs;
652 Analyze
($Clang, \
@NewArgs, \
@AnalyzeArgs, $FileLang, $Output,
653 $Verbose, $HtmlDir, $file, $Analyses);
657 Analyze
($Clang, \
@CmdArgs, \
@AnalyzeArgs, $FileLang, $Output,
658 $Verbose, $HtmlDir, $file, $Analyses);