Cleanup confused code that redundantly called "getDeclContext()" twice.
[clang.git] / tools / scan-build / ccc-analyzer
blobf579e56e8025b109ad6351b1acd1fc50f229a8bd
1 #!/usr/bin/env perl
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 ##===----------------------------------------------------------------------===##
15 use strict;
16 use warnings;
17 use FindBin;
18 use Cwd qw/ getcwd abs_path /;
19 use File::Temp qw/ tempfile /;
20 use File::Path qw / mkpath /;
21 use File::Basename;
22 use Text::ParseWords;
24 ##===----------------------------------------------------------------------===##
25 # Compiler command setup.
26 ##===----------------------------------------------------------------------===##
28 my $Compiler;
29 my $Clang;
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++'; }
38 else {
39 $Compiler = $ENV{'CCC_CC'};
40 if (!defined $Compiler) { $Compiler = "gcc"; }
42 $Clang = $ENV{'CLANG'};
43 if (!defined $Clang) { $Clang = 'clang'; }
46 ##===----------------------------------------------------------------------===##
47 # Cleanup.
48 ##===----------------------------------------------------------------------===##
50 my $ReportFailures = $ENV{'CCC_REPORT_FAILURES'};
51 if (!defined $ReportFailures) { $ReportFailures = 1; }
53 my $CleanupFile;
54 my $ResultFile;
56 # Remove any stale files at exit.
57 END {
58 if (defined $CleanupFile && -z $CleanupFile) {
59 `rm -f $CleanupFile`;
63 ##----------------------------------------------------------------------------##
64 # Process Clang Crashes.
65 ##----------------------------------------------------------------------------##
67 sub 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";
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";
84 mkpath $Dir;
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),
97 DIR => $Dir);
98 system $Clang, @$Args, "-E", "-o", $PPFile;
99 close ($PPH);
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);
113 ##----------------------------------------------------------------------------##
114 # Running the analyzer.
115 ##----------------------------------------------------------------------------##
117 sub GetCCArgs {
118 my $Args = shift;
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;
128 close(TO_PARENT);
129 my $line;
130 while (<FROM_CHILD>) {
131 next if (!/-cc1/);
132 $line = $_;
135 waitpid($pid,0);
136 close(FROM_CHILD);
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;
148 sub Analyze {
149 my ($Clang, $Args, $AnalyzeArgs, $Lang, $Output, $Verbose, $HtmlDir,
150 $file, $Analyses) = @_;
152 $Args = GetCCArgs($Args);
154 my $RunAnalyzer = 0;
155 my $Cmd;
156 my @CmdArgs;
157 my @CmdArgsSansAnalyses;
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;
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);
180 if (defined $ENV{"CCC_EXPERIMENTAL_CHECKS"}) {
181 push @CmdArgs,"-analyzer-experimental-internal-checks";
182 push @CmdArgs,"-analyzer-experimental-checks";
185 $RunAnalyzer = 1;
188 # Add the analysis arguments passed down from scan-build.
189 foreach my $Arg (@$AnalyzeArgs) {
190 push @CmdArgs, $Arg;
193 my @PrintArgs;
194 my $dir;
196 if ($RunAnalyzer) {
197 if (defined $ResultFile) {
198 push @CmdArgs,'-o';
199 push @CmdArgs, $ResultFile;
201 elsif (defined $HtmlDir) {
202 push @CmdArgs,'-o';
203 push @CmdArgs, $HtmlDir;
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\'"; }
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";
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);
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;
241 close TO_PARENT;
242 my ($ofh, $ofile) = tempfile("clang_output_XXXXXX", DIR => $HtmlDir);
244 while (<FROM_CHILD>) {
245 print $ofh $_;
246 print STDERR $_;
249 waitpid($pid,0);
250 close(FROM_CHILD);
251 my $Result = $?;
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);
259 elsif ($Result) {
260 if ($IncludeParserRejects && !($file =~/conftest/)) {
261 ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses,
262 $HtmlDir, $ParserRejects, $ofile);
265 else {
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;
274 my $ppfile;
275 while (<CHILD>) {
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
287 # doesn't exist yet.
288 next if (-e $afile);
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);
298 mkpath $dir;
299 open(AFILE, ">$afile");
300 print AFILE "$ppfile\n";
301 close(AFILE);
303 close CHILD;
308 unlink($ofile);
311 ##----------------------------------------------------------------------------##
312 # Lookup tables.
313 ##----------------------------------------------------------------------------##
315 my %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
332 my %LinkerOptionMap = (
333 '-framework' => 1
336 my %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 '='
347 my %IgnoredOptionMap = (
348 '-MT' => 1, # Ignore these preprocessor options.
349 '-MF' => 1,
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
367 my %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'
377 my %UniqueOptions = (
378 '-isysroot' => 0
381 ##----------------------------------------------------------------------------##
382 # Languages accepted.
383 ##----------------------------------------------------------------------------##
385 my %LangsAccepted = (
386 "objective-c" => 1,
387 "c" => 1
390 if (defined $ENV{'CCC_ANALYZER_CPLUSPLUS'}) {
391 $LangsAccepted{"c++"} = 1;
392 $LangsAccepted{"objective-c++"} = 1;
395 ##----------------------------------------------------------------------------##
396 # Main Logic.
397 ##----------------------------------------------------------------------------##
399 my $Action = 'link';
400 my @CompileOpts;
401 my @LinkOpts;
402 my @Files;
403 my $Lang;
404 my $Output;
405 my %Uniqued;
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.
431 my $Verbose = 0;
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);
439 my %ArchsSeen;
440 my $HadArch = 0;
442 # Process the arguments.
443 foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
444 my $Arg = $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; }
458 $HadArch = 1;
459 ++$i;
460 next;
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]; }
468 next;
471 # Options with possible arguments that should pass through to linker.
472 if (defined $LinkerOptionMap{$ArgKey}) {
473 my $Cnt = $LinkerOptionMap{$ArgKey};
474 push @LinkOpts,$Arg;
475 while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
476 next;
479 # Options with possible arguments that should pass through to both compiler
480 # and the linker.
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}) {
488 $i += $Cnt;
489 next;
491 $Uniqued{$Arg} = 1;
494 push @CompileOpts,$Arg;
495 push @LinkOpts,$Arg;
497 while ($Cnt > 0) {
498 ++$i; --$Cnt;
499 push @CompileOpts, $ARGV[$i];
500 push @LinkOpts, $ARGV[$i];
502 next;
505 # Ignored options.
506 if (defined $IgnoredOptionMap{$ArgKey}) {
507 my $Cnt = $IgnoredOptionMap{$ArgKey};
508 while ($Cnt > 0) {
509 ++$i; --$Cnt;
511 next;
514 # Compile mode flags.
515 if ($Arg =~ /^-[D,I,U](.*)$/) {
516 my $Tmp = $Arg;
517 if ($1 eq '') {
518 # FIXME: Check if we are going off the end.
519 ++$i;
520 $Tmp = $Arg . $ARGV[$i];
522 push @CompileOpts,$Tmp;
523 next;
526 # Language.
527 if ($Arg eq '-x') {
528 $Lang = $ARGV[$i+1];
529 ++$i; next;
532 # Output file.
533 if ($Arg eq '-o') {
534 ++$i;
535 $Output = $ARGV[$i];
536 next;
539 # Get the link mode.
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; }
544 next;
547 if ($Arg =~ /^-std=/) {
548 push @CompileOpts,$Arg;
549 next;
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(.+)$/) {
560 my $Tmp = $Arg;
561 if ($1 eq '') {
562 # FIXME: Check if we are going off the end.
563 ++$i;
564 $Tmp = $Arg . $ARGV[$i];
566 push @CompileOpts,$Tmp;
567 push @LinkOpts,$Tmp;
568 next;
571 # Input files.
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,$_; }
576 close(IN);
577 ++$i;
578 next;
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;
585 next;
588 if (!($Arg =~ /^-/)) {
589 push @Files, $Arg;
590 next;
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});
616 my @CmdArgs;
617 my @AnalyzeArgs;
619 if ($FileLang ne 'unknown') {
620 push @CmdArgs,'-x';
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",
637 DIR => $HtmlDir);
638 $ResultFile = $f;
639 $CleanupFile = $f;
643 push @CmdArgs,@CompileOpts;
644 push @CmdArgs,$file;
646 if (scalar @Archs) {
647 foreach my $arch (@Archs) {
648 my @NewArgs;
649 push @NewArgs, '-arch';
650 push @NewArgs, $arch;
651 push @NewArgs, @CmdArgs;
652 Analyze($Clang, \@NewArgs, \@AnalyzeArgs, $FileLang, $Output,
653 $Verbose, $HtmlDir, $file, $Analyses);
656 else {
657 Analyze($Clang, \@CmdArgs, \@AnalyzeArgs, $FileLang, $Output,
658 $Verbose, $HtmlDir, $file, $Analyses);
663 exit($Status >> 8);