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 = (
318 '-fobjc-gc-only' => 0,
320 '-ffreestanding' => 0,
327 '-iwithprefixbefore' => 1
330 my %LinkerOptionMap = (
334 my %CompilerLinkerOptionMap = (
340 '-fpascal-strings' => 0,
341 '-mmacosx-version-min' => 0, # This is really a 1 argument, but always has '='
342 '-miphoneos-version-min' => 0 # This is really a 1 argument, but always has '='
345 my %IgnoredOptionMap = (
346 '-MT' => 1, # Ignore these preprocessor options.
349 '-fsyntax-only' => 0,
351 '-install_name' => 1,
352 '-exported_symbols_list' => 1,
353 '-current_version' => 1,
354 '-compatibility_version' => 1,
358 '-bundle_loader' => 1,
359 '-multiply_defined' => 1,
369 'i' => 'c-cpp-output',
370 'm' => 'objective-c',
371 'mi' => 'objective-c-cpp-output'
374 my %UniqueOptions = (
378 ##----------------------------------------------------------------------------##
379 # Languages accepted.
380 ##----------------------------------------------------------------------------##
382 my %LangsAccepted = (
387 if (defined $ENV{'CCC_ANALYZER_CPLUSPLUS'}) {
388 $LangsAccepted{"c++"} = 1;
389 $LangsAccepted{"objective-c++"} = 1;
392 ##----------------------------------------------------------------------------##
394 ##----------------------------------------------------------------------------##
404 # Forward arguments to gcc.
405 my $Status = system($Compiler,@ARGV);
406 if ($Status) { exit($Status >> 8); }
408 # Get the analysis options.
409 my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
410 if (!defined($Analyses)) { $Analyses = '-analyzer-check-objc-mem'; }
412 # Get the store model.
413 my $StoreModel = $ENV{'CCC_ANALYZER_STORE_MODEL'};
414 if (!defined $StoreModel) { $StoreModel = "region"; }
416 # Get the constraints engine.
417 my $ConstraintsModel = $ENV{'CCC_ANALYZER_CONSTRAINTS_MODEL'};
418 if (!defined $ConstraintsModel) { $ConstraintsModel = "range"; }
420 # Get the output format.
421 my $OutputFormat = $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'};
422 if (!defined $OutputFormat) { $OutputFormat = "html"; }
424 # Determine the level of verbosity.
426 if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; }
427 if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; }
429 # Get the HTML output directory.
430 my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'};
432 my %DisabledArchs = ('ppc' => 1, 'ppc64' => 1);
436 # Process the arguments.
437 foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
439 my ($ArgKey) = split /=/,$Arg,2;
441 # Modes ccc-analyzer supports
442 if ($Arg =~ /^-(E|MM?)$/) { $Action = 'preprocess'; }
443 elsif ($Arg eq '-c') { $Action = 'compile'; }
444 elsif ($Arg =~ /^-print-prog-name/) { exit 0; }
446 # Specially handle duplicate cases of -arch
447 if ($Arg eq "-arch") {
448 my $arch = $ARGV[$i+1];
449 # We don't want to process 'ppc' because of Clang's lack of support
450 # for Altivec (also some #defines won't likely be defined correctly, etc.)
451 if (!(defined $DisabledArchs{$arch})) { $ArchsSeen{$arch} = 1; }
457 # Options with possible arguments that should pass through to compiler.
458 if (defined $CompileOptionMap{$ArgKey}) {
459 my $Cnt = $CompileOptionMap{$ArgKey};
460 push @CompileOpts,$Arg;
461 while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; }
465 # Options with possible arguments that should pass through to linker.
466 if (defined $LinkerOptionMap{$ArgKey}) {
467 my $Cnt = $LinkerOptionMap{$ArgKey};
469 while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
473 # Options with possible arguments that should pass through to both compiler
475 if (defined $CompilerLinkerOptionMap{$ArgKey}) {
476 my $Cnt = $CompilerLinkerOptionMap{$ArgKey};
478 # Check if this is an option that should have a unique value, and if so
479 # determine if the value was checked before.
480 if ($UniqueOptions{$Arg}) {
481 if (defined $Uniqued{$Arg}) {
488 push @CompileOpts,$Arg;
493 push @CompileOpts, $ARGV[$i];
494 push @LinkOpts, $ARGV[$i];
500 if (defined $IgnoredOptionMap{$ArgKey}) {
501 my $Cnt = $IgnoredOptionMap{$ArgKey};
508 # Compile mode flags.
509 if ($Arg =~ /^-[D,I,U](.*)$/) {
512 # FIXME: Check if we are going off the end.
514 $Tmp = $Arg . $ARGV[$i];
516 push @CompileOpts,$Tmp;
534 if ($Arg =~ /^-[l,L,O]/) {
535 if ($Arg eq '-O') { push @LinkOpts,'-O1'; }
536 elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; }
537 else { push @LinkOpts,$Arg; }
541 if ($Arg =~ /^-std=/) {
542 push @CompileOpts,$Arg;
546 # if ($Arg =~ /^-f/) {
547 # # FIXME: Not sure if the remaining -fxxxx options have no arguments.
548 # push @CompileOpts,$Arg;
549 # push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts.
552 # Get the compiler/link mode.
553 if ($Arg =~ /^-F(.+)$/) {
556 # FIXME: Check if we are going off the end.
558 $Tmp = $Arg . $ARGV[$i];
560 push @CompileOpts,$Tmp;
566 if ($Arg eq '-filelist') {
567 # FIXME: Make sure we aren't walking off the end.
568 open(IN, $ARGV[$i+1]);
569 while (<IN>) { s/\015?\012//; push @Files,$_; }
575 # Handle -Wno-. We don't care about extra warnings, but
576 # we should suppress ones that we don't want to see.
577 if ($Arg =~ /^-Wno-/) {
578 push @CompileOpts, $Arg;
582 if (!($Arg =~ /^-/)) {
588 if ($Action eq 'compile' or $Action eq 'link') {
589 my @Archs = keys %ArchsSeen;
590 # Skip the file if we don't support the architectures specified.
591 exit 0 if ($HadArch && scalar(@Archs) == 0);
593 foreach my $file (@Files) {
594 # Determine the language for the file.
595 my $FileLang = $Lang;
597 if (!defined($FileLang)) {
598 # Infer the language from the extension.
599 if ($file =~ /[.]([^.]+)$/) {
600 $FileLang = $LangMap{$1};
604 # FileLang still not defined? Skip the file.
605 next if (!defined $FileLang);
607 # Language not accepted?
608 next if (!defined $LangsAccepted{$FileLang});
613 if ($FileLang ne 'unknown') {
615 push @CmdArgs,$FileLang;
618 if (defined $StoreModel) {
619 push @AnalyzeArgs, "-analyzer-store=$StoreModel";
622 if (defined $ConstraintsModel) {
623 push @AnalyzeArgs, "-analyzer-constraints=$ConstraintsModel";
626 if (defined $OutputFormat) {
627 push @AnalyzeArgs, "-analyzer-output=" . $OutputFormat;
628 if ($OutputFormat =~ /plist/) {
629 # Change "Output" to be a file.
630 my ($h, $f) = tempfile("report-XXXXXX", SUFFIX => ".plist",
637 push @CmdArgs,@CompileOpts;
641 foreach my $arch (@Archs) {
643 push @NewArgs, '-arch';
644 push @NewArgs, $arch;
645 push @NewArgs, @CmdArgs;
646 Analyze($Clang, \@NewArgs, \@AnalyzeArgs, $FileLang, $Output,
647 $Verbose, $HtmlDir, $file, $Analyses);
651 Analyze($Clang, \@CmdArgs, \@AnalyzeArgs, $FileLang, $Output,
652 $Verbose, $HtmlDir, $file, $Analyses);