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, $OriginalArgs, $AnalyzeArgs, $Lang, $Output, $Verbose, $HtmlDir,
153 my @Args = @$OriginalArgs;
156 my @CmdArgsSansAnalyses;
158 if ($Lang =~ /header/) {
159 exit 0 if (!defined ($Output));
161 push @CmdArgs, $file;
162 # Remove the PCH extension.
163 $Output =~ s/[.]gch$//;
164 push @CmdArgs, $Output;
165 @CmdArgsSansAnalyses = @CmdArgs;
169 if ($Lang eq "objective-c" || $Lang eq "objective-c++") {
170 push @Args,'-DIBOutlet=__attribute__((iboutlet))';
171 push @Args,'-DIBOutletCollection(ClassName)=__attribute__((iboutletcollection)))';
172 push @Args,'-DIBAction=void)__attribute__((ibaction)';
175 # Create arguments for doing regular parsing.
176 my $SyntaxArgs = GetCCArgs("-fsyntax-only", \@Args);
177 @CmdArgsSansAnalyses = @$SyntaxArgs;
179 # Create arguments for doing static analysis.
180 if (defined $ResultFile) {
181 push @Args, '-o', $ResultFile;
183 elsif (defined $HtmlDir) {
184 push @Args, '-o', $HtmlDir;
187 push @Args, "-Xclang", "-analyzer-display-progress";
190 foreach my $arg (@$AnalyzeArgs) {
191 push @Args, "-Xclang", $arg;
194 # Display Ubiviz graph?
195 if (defined $ENV{'CCC_UBI'}) {
196 push @Args, "-Xclang", "-analyzer-viz-egraph-ubigraph";
199 my $AnalysisArgs = GetCCArgs("--analyze", \@Args);
200 @CmdArgs = @$AnalysisArgs;
208 print STDERR "\n[LOCATION]: $dir\n";
209 push @PrintArgs,"'$Cmd'";
210 foreach my $arg (@CmdArgs) {
211 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 # Capture the STDERR of clang and send it to a temporary file.
225 # Capture the STDOUT of clang and reroute it to ccc-analyzer's STDERR.
226 # We save the output file in the 'crashes' directory if clang encounters
227 # any problems with the file.
228 pipe (FROM_CHILD, TO_PARENT);
232 open(STDOUT,">&", \*TO_PARENT);
233 open(STDERR,">&", \*TO_PARENT);
238 my ($ofh, $ofile) = tempfile("clang_output_XXXXXX", DIR => $HtmlDir);
240 while (<FROM_CHILD>) {
249 # Did the command die because of a signal?
250 if ($ReportFailures) {
251 if ($Result & 127 and $Cmd eq $Clang and defined $HtmlDir) {
252 ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses,
253 $HtmlDir, "Crash", $ofile);
256 if ($IncludeParserRejects && !($file =~/conftest/)) {
257 ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses,
258 $HtmlDir, $ParserRejects, $ofile);
262 # Check if there were any unhandled attributes.
263 if (open(CHILD, $ofile)) {
264 my %attributes_not_handled;
266 # Don't flag warnings about the following attributes that we
267 # know are currently not supported by Clang.
268 $attributes_not_handled{"cdecl"} = 1;
272 next if (! /warning: '([^\']+)' attribute ignored/);
274 # Have we already spotted this unhandled attribute?
275 next if (defined $attributes_not_handled{$1});
276 $attributes_not_handled{$1} = 1;
278 # Get the name of the attribute file.
279 my $dir = "$HtmlDir/failures";
280 my $afile = "$dir/attribute_ignored_$1.txt";
282 # Only create another preprocessed file if the attribute file
286 # Add this file to the list of files that contained this attribute.
287 # Generate a preprocessed file if we haven't already.
288 if (!(defined $ppfile)) {
289 $ppfile = ProcessClangFailure($Clang, $Lang, $file,
290 \@CmdArgsSansAnalyses,
291 $HtmlDir, $AttributeIgnored, $ofile);
295 open(AFILE, ">$afile");
296 print AFILE "$ppfile\n";
307 ##----------------------------------------------------------------------------##
309 ##----------------------------------------------------------------------------##
311 my %CompileOptionMap = (
315 '-fobjc-gc-only' => 0,
317 '-ffreestanding' => 0,
325 '-iwithprefixbefore' => 1
328 my %LinkerOptionMap = (
332 my %CompilerLinkerOptionMap = (
338 '-fpascal-strings' => 0,
339 '-mmacosx-version-min' => 0, # This is really a 1 argument, but always has '='
340 '-miphoneos-version-min' => 0 # This is really a 1 argument, but always has '='
343 my %IgnoredOptionMap = (
344 '-MT' => 1, # Ignore these preprocessor options.
347 '-fsyntax-only' => 0,
349 '-install_name' => 1,
350 '-exported_symbols_list' => 1,
351 '-current_version' => 1,
352 '-compatibility_version' => 1,
356 '-bundle_loader' => 1,
357 '-multiply_defined' => 1,
368 'i' => 'c-cpp-output',
369 'm' => 'objective-c',
370 'mi' => 'objective-c-cpp-output'
373 my %UniqueOptions = (
377 ##----------------------------------------------------------------------------##
378 # Languages accepted.
379 ##----------------------------------------------------------------------------##
381 my %LangsAccepted = (
386 if (defined $ENV{'CCC_ANALYZER_CPLUSPLUS'}) {
387 $LangsAccepted{"c++"} = 1;
388 $LangsAccepted{"objective-c++"} = 1;
391 ##----------------------------------------------------------------------------##
393 ##----------------------------------------------------------------------------##
403 # Forward arguments to gcc.
404 my $Status = system($Compiler,@ARGV);
405 if (defined $ENV{'CCC_ANALYZER_LOG'}) {
406 print "$Compiler @ARGV\n";
408 if ($Status) { exit($Status >> 8); }
410 # Get the analysis options.
411 my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
413 # Get the store model.
414 my $StoreModel = $ENV{'CCC_ANALYZER_STORE_MODEL'};
416 # Get the constraints engine.
417 my $ConstraintsModel = $ENV{'CCC_ANALYZER_CONSTRAINTS_MODEL'};
419 # Get the output format.
420 my $OutputFormat = $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'};
421 if (!defined $OutputFormat) { $OutputFormat = "html"; }
423 # Determine the level of verbosity.
425 if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; }
426 if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; }
428 # Get the HTML output directory.
429 my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'};
431 my %DisabledArchs = ('ppc' => 1, 'ppc64' => 1);
435 # Process the arguments.
436 foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
438 my ($ArgKey) = split /=/,$Arg,2;
440 # Modes ccc-analyzer supports
441 if ($Arg =~ /^-(E|MM?)$/) { $Action = 'preprocess'; }
442 elsif ($Arg eq '-c') { $Action = 'compile'; }
443 elsif ($Arg =~ /^-print-prog-name/) { exit 0; }
445 # Specially handle duplicate cases of -arch
446 if ($Arg eq "-arch") {
447 my $arch = $ARGV[$i+1];
448 # We don't want to process 'ppc' because of Clang's lack of support
449 # for Altivec (also some #defines won't likely be defined correctly, etc.)
450 if (!(defined $DisabledArchs{$arch})) { $ArchsSeen{$arch} = 1; }
456 # Options with possible arguments that should pass through to compiler.
457 if (defined $CompileOptionMap{$ArgKey}) {
458 my $Cnt = $CompileOptionMap{$ArgKey};
459 push @CompileOpts,$Arg;
460 while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; }
464 # Options with possible arguments that should pass through to linker.
465 if (defined $LinkerOptionMap{$ArgKey}) {
466 my $Cnt = $LinkerOptionMap{$ArgKey};
468 while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
472 # Options with possible arguments that should pass through to both compiler
474 if (defined $CompilerLinkerOptionMap{$ArgKey}) {
475 my $Cnt = $CompilerLinkerOptionMap{$ArgKey};
477 # Check if this is an option that should have a unique value, and if so
478 # determine if the value was checked before.
479 if ($UniqueOptions{$Arg}) {
480 if (defined $Uniqued{$Arg}) {
487 push @CompileOpts,$Arg;
492 push @CompileOpts, $ARGV[$i];
493 push @LinkOpts, $ARGV[$i];
499 if (defined $IgnoredOptionMap{$ArgKey}) {
500 my $Cnt = $IgnoredOptionMap{$ArgKey};
507 # Compile mode flags.
508 if ($Arg =~ /^-[D,I,U](.*)$/) {
511 # FIXME: Check if we are going off the end.
513 $Tmp = $Arg . $ARGV[$i];
515 push @CompileOpts,$Tmp;
533 if ($Arg =~ /^-[l,L,O]/) {
534 if ($Arg eq '-O') { push @LinkOpts,'-O1'; }
535 elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; }
536 else { push @LinkOpts,$Arg; }
540 if ($Arg =~ /^-std=/) {
541 push @CompileOpts,$Arg;
545 # if ($Arg =~ /^-f/) {
546 # # FIXME: Not sure if the remaining -fxxxx options have no arguments.
547 # push @CompileOpts,$Arg;
548 # push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts.
551 # Get the compiler/link mode.
552 if ($Arg =~ /^-F(.+)$/) {
555 # FIXME: Check if we are going off the end.
557 $Tmp = $Arg . $ARGV[$i];
559 push @CompileOpts,$Tmp;
565 if ($Arg eq '-filelist') {
566 # FIXME: Make sure we aren't walking off the end.
567 open(IN, $ARGV[$i+1]);
568 while (<IN>) { s/\015?\012//; push @Files,$_; }
574 # Handle -Wno-. We don't care about extra warnings, but
575 # we should suppress ones that we don't want to see.
576 if ($Arg =~ /^-Wno-/) {
577 push @CompileOpts, $Arg;
581 if (!($Arg =~ /^-/)) {
587 if ($Action eq 'compile' or $Action eq 'link') {
588 my @Archs = keys %ArchsSeen;
589 # Skip the file if we don't support the architectures specified.
590 exit 0 if ($HadArch && scalar(@Archs) == 0);
592 foreach my $file (@Files) {
593 # Determine the language for the file.
594 my $FileLang = $Lang;
596 if (!defined($FileLang)) {
597 # Infer the language from the extension.
598 if ($file =~ /[.]([^.]+)$/) {
599 $FileLang = $LangMap{$1};
603 # FileLang still not defined? Skip the file.
604 next if (!defined $FileLang);
606 # Language not accepted?
607 next if (!defined $LangsAccepted{$FileLang});
612 if ($FileLang ne 'unknown') {
613 push @CmdArgs, '-x', $FileLang;
616 if (defined $StoreModel) {
617 push @AnalyzeArgs, "-analyzer-store=$StoreModel";
620 if (defined $ConstraintsModel) {
621 push @AnalyzeArgs, "-analyzer-constraints=$ConstraintsModel";
624 # if (defined $Analyses) {
625 # push @AnalyzeArgs, split '\s+', $Analyses;
628 if (defined $OutputFormat) {
629 push @AnalyzeArgs, "-analyzer-output=" . $OutputFormat;
630 if ($OutputFormat =~ /plist/) {
631 # Change "Output" to be a file.
632 my ($h, $f) = tempfile("report-XXXXXX", SUFFIX => ".plist",
639 push @CmdArgs, @CompileOpts;
640 push @CmdArgs, $file;
643 foreach my $arch (@Archs) {
645 push @NewArgs, '-arch', $arch;
646 push @NewArgs, @CmdArgs;
647 Analyze($Clang, \@NewArgs, \@AnalyzeArgs, $FileLang, $Output,
648 $Verbose, $HtmlDir, $file);
652 Analyze($Clang, \@CmdArgs, \@AnalyzeArgs, $FileLang, $Output,
653 $Verbose, $HtmlDir, $file);