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 ##----------------------------------------------------------------------------##
121 pipe (FROM_CHILD, TO_PARENT);
125 open(STDOUT,">&", \*TO_PARENT);
126 open(STDERR,">&", \*TO_PARENT);
127 exec $Clang, "-###", $mode, @$Args;
131 while (<FROM_CHILD>) {
139 die "could not find clang line\n" if (!defined $line);
140 # Strip the newline and initial whitspace
143 my @items = quotewords('\s+', 0, $line);
144 my $cmd = shift @items;
145 die "cannot find 'clang' in 'clang' command\n" if (!($cmd =~ /clang/));
150 my ($Clang, $Args, $AnalyzeArgs, $Lang, $Output, $Verbose, $HtmlDir,
155 my @CmdArgsSansAnalyses;
157 if ($Lang =~ /header/) {
158 exit 0 if (!defined ($Output));
160 push @CmdArgs, $file;
161 # Remove the PCH extension.
162 $Output =~ s/[.]gch$//;
163 push @CmdArgs, $Output;
164 @CmdArgsSansAnalyses = @CmdArgs;
168 if ($Lang eq "objective-c" || $Lang eq "objective-c++") {
169 push @$Args,'-DIBOutlet=__attribute__((iboutlet))';
170 push @$Args,'-DIBOutletCollection(ClassName)=__attribute__((iboutletcollection)))';
171 push @$Args,'-DIBAction=void)__attribute__((ibaction)';
174 # Create arguments for doing regular parsing.
175 my $SyntaxArgs = GetCCArgs("-fsyntax-only", $Args);
176 @CmdArgsSansAnalyses = @CmdArgs;
177 push @CmdArgsSansAnalyses, @$SyntaxArgs;
179 # Create arguments for doing static analysis.
180 if (defined $ResultFile) {
182 push @$Args, $ResultFile;
184 elsif (defined $HtmlDir) {
186 push @$Args, $HtmlDir;
188 push @$Args,"-Xclang";
189 push @$Args,"-analyzer-display-progress";
191 foreach my $arg (@$AnalyzeArgs) {
192 push @$Args, "-Xclang";
196 # Display Ubiviz graph?
197 if (defined $ENV{'CCC_UBI'}) {
198 push @$Args, "-Xclang";
199 push @$Args,"-analyzer-viz-egraph-ubigraph";
202 my $AnalysisArgs = GetCCArgs("--analyze", $Args);
203 push @CmdArgs, @$AnalysisArgs;
211 print STDERR "\n[LOCATION]: $dir\n";
212 push @PrintArgs,"'$Cmd'";
213 foreach my $arg (@CmdArgs) {
214 push @PrintArgs,"\'$arg\'";
218 # We MUST print to stderr. Some clients use the stdout output of
219 # gcc for various purposes.
220 print STDERR join(' ',@PrintArgs);
223 elsif ($Verbose == 2) {
224 print STDERR "#SHELL (cd '$dir' && @PrintArgs)\n";
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);
235 open(STDOUT,">&", \*TO_PARENT);
236 open(STDERR,">&", \*TO_PARENT);
241 my ($ofh, $ofile) = tempfile("clang_output_XXXXXX", DIR => $HtmlDir);
243 while (<FROM_CHILD>) {
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);
259 if ($IncludeParserRejects && !($file =~/conftest/)) {
260 ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses,
261 $HtmlDir, $ParserRejects, $ofile);
265 # Check if there were any unhandled attributes.
266 if (open(CHILD, $ofile)) {
267 my %attributes_not_handled;
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;
275 next if (! /warning: '([^\']+)' attribute ignored/);
277 # Have we already spotted this unhandled attribute?
278 next if (defined $attributes_not_handled{$1});
279 $attributes_not_handled{$1} = 1;
281 # Get the name of the attribute file.
282 my $dir = "$HtmlDir/failures";
283 my $afile = "$dir/attribute_ignored_$1.txt";
285 # Only create another preprocessed file if the attribute file
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);
298 open(AFILE, ">$afile");
299 print AFILE "$ppfile\n";
310 ##----------------------------------------------------------------------------##
312 ##----------------------------------------------------------------------------##
314 my %CompileOptionMap = (
318 '-fobjc-gc-only' => 0,
320 '-ffreestanding' => 0,
328 '-iwithprefixbefore' => 1
331 my %LinkerOptionMap = (
335 my %CompilerLinkerOptionMap = (
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 '='
346 my %IgnoredOptionMap = (
347 '-MT' => 1, # Ignore these preprocessor options.
350 '-fsyntax-only' => 0,
352 '-install_name' => 1,
353 '-exported_symbols_list' => 1,
354 '-current_version' => 1,
355 '-compatibility_version' => 1,
359 '-bundle_loader' => 1,
360 '-multiply_defined' => 1,
371 'i' => 'c-cpp-output',
372 'm' => 'objective-c',
373 'mi' => 'objective-c-cpp-output'
376 my %UniqueOptions = (
380 ##----------------------------------------------------------------------------##
381 # Languages accepted.
382 ##----------------------------------------------------------------------------##
384 my %LangsAccepted = (
389 if (defined $ENV{'CCC_ANALYZER_CPLUSPLUS'}) {
390 $LangsAccepted{"c++"} = 1;
391 $LangsAccepted{"objective-c++"} = 1;
394 ##----------------------------------------------------------------------------##
396 ##----------------------------------------------------------------------------##
406 # Forward arguments to gcc.
407 my $Status = system($Compiler,@ARGV);
408 if (defined $ENV{'CCC_ANALYZER_LOG'}) {
409 print "$Compiler @ARGV\n";
411 if ($Status) { exit($Status >> 8); }
413 # Get the analysis options.
414 my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
416 # Get the store model.
417 my $StoreModel = $ENV{'CCC_ANALYZER_STORE_MODEL'};
419 # Get the constraints engine.
420 my $ConstraintsModel = $ENV{'CCC_ANALYZER_CONSTRAINTS_MODEL'};
422 # Get the output format.
423 my $OutputFormat = $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'};
424 if (!defined $OutputFormat) { $OutputFormat = "html"; }
426 # Determine the level of verbosity.
428 if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; }
429 if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; }
431 # Get the HTML output directory.
432 my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'};
434 my %DisabledArchs = ('ppc' => 1, 'ppc64' => 1);
438 # Process the arguments.
439 foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
441 my ($ArgKey) = split /=/,$Arg,2;
443 # Modes ccc-analyzer supports
444 if ($Arg =~ /^-(E|MM?)$/) { $Action = 'preprocess'; }
445 elsif ($Arg eq '-c') { $Action = 'compile'; }
446 elsif ($Arg =~ /^-print-prog-name/) { exit 0; }
448 # Specially handle duplicate cases of -arch
449 if ($Arg eq "-arch") {
450 my $arch = $ARGV[$i+1];
451 # We don't want to process 'ppc' because of Clang's lack of support
452 # for Altivec (also some #defines won't likely be defined correctly, etc.)
453 if (!(defined $DisabledArchs{$arch})) { $ArchsSeen{$arch} = 1; }
459 # Options with possible arguments that should pass through to compiler.
460 if (defined $CompileOptionMap{$ArgKey}) {
461 my $Cnt = $CompileOptionMap{$ArgKey};
462 push @CompileOpts,$Arg;
463 while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; }
467 # Options with possible arguments that should pass through to linker.
468 if (defined $LinkerOptionMap{$ArgKey}) {
469 my $Cnt = $LinkerOptionMap{$ArgKey};
471 while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
475 # Options with possible arguments that should pass through to both compiler
477 if (defined $CompilerLinkerOptionMap{$ArgKey}) {
478 my $Cnt = $CompilerLinkerOptionMap{$ArgKey};
480 # Check if this is an option that should have a unique value, and if so
481 # determine if the value was checked before.
482 if ($UniqueOptions{$Arg}) {
483 if (defined $Uniqued{$Arg}) {
490 push @CompileOpts,$Arg;
495 push @CompileOpts, $ARGV[$i];
496 push @LinkOpts, $ARGV[$i];
502 if (defined $IgnoredOptionMap{$ArgKey}) {
503 my $Cnt = $IgnoredOptionMap{$ArgKey};
510 # Compile mode flags.
511 if ($Arg =~ /^-[D,I,U](.*)$/) {
514 # FIXME: Check if we are going off the end.
516 $Tmp = $Arg . $ARGV[$i];
518 push @CompileOpts,$Tmp;
536 if ($Arg =~ /^-[l,L,O]/) {
537 if ($Arg eq '-O') { push @LinkOpts,'-O1'; }
538 elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; }
539 else { push @LinkOpts,$Arg; }
543 if ($Arg =~ /^-std=/) {
544 push @CompileOpts,$Arg;
548 # if ($Arg =~ /^-f/) {
549 # # FIXME: Not sure if the remaining -fxxxx options have no arguments.
550 # push @CompileOpts,$Arg;
551 # push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts.
554 # Get the compiler/link mode.
555 if ($Arg =~ /^-F(.+)$/) {
558 # FIXME: Check if we are going off the end.
560 $Tmp = $Arg . $ARGV[$i];
562 push @CompileOpts,$Tmp;
568 if ($Arg eq '-filelist') {
569 # FIXME: Make sure we aren't walking off the end.
570 open(IN, $ARGV[$i+1]);
571 while (<IN>) { s/\015?\012//; push @Files,$_; }
577 # Handle -Wno-. We don't care about extra warnings, but
578 # we should suppress ones that we don't want to see.
579 if ($Arg =~ /^-Wno-/) {
580 push @CompileOpts, $Arg;
584 if (!($Arg =~ /^-/)) {
590 if ($Action eq 'compile' or $Action eq 'link') {
591 my @Archs = keys %ArchsSeen;
592 # Skip the file if we don't support the architectures specified.
593 exit 0 if ($HadArch && scalar(@Archs) == 0);
595 foreach my $file (@Files) {
596 # Determine the language for the file.
597 my $FileLang = $Lang;
599 if (!defined($FileLang)) {
600 # Infer the language from the extension.
601 if ($file =~ /[.]([^.]+)$/) {
602 $FileLang = $LangMap{$1};
606 # FileLang still not defined? Skip the file.
607 next if (!defined $FileLang);
609 # Language not accepted?
610 next if (!defined $LangsAccepted{$FileLang});
615 if ($FileLang ne 'unknown') {
617 push @CmdArgs,$FileLang;
620 if (defined $StoreModel) {
621 push @AnalyzeArgs, "-analyzer-store=$StoreModel";
624 if (defined $ConstraintsModel) {
625 push @AnalyzeArgs, "-analyzer-constraints=$ConstraintsModel";
628 # if (defined $Analyses) {
629 # push @AnalyzeArgs, split '\s+', $Analyses;
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;
644 push @CmdArgs, $file;
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);
657 Analyze($Clang, \@CmdArgs, \@AnalyzeArgs, $FileLang, $Output,
658 $Verbose, $HtmlDir, $file);