ccc-analyzer revision 544450e449c2fa1af63199b047b2d378be1760ec
1#!/usr/bin/env perl
2#
3#                     The LLVM Compiler Infrastructure
4#
5# This file is distributed under the University of Illinois Open Source
6# License. See LICENSE.TXT for details.
7#
8##===----------------------------------------------------------------------===##
9#
10#  A script designed to interpose between the build system and gcc.  It invokes
11#  both gcc and the static analyzer.
12#
13##===----------------------------------------------------------------------===##
14
15use strict;
16use warnings;
17use FindBin;
18use Cwd qw/ getcwd abs_path /;
19use File::Temp qw/ tempfile /;
20use File::Path qw / mkpath /;
21use File::Basename;
22use Text::ParseWords;
23
24##===----------------------------------------------------------------------===##
25# Compiler command setup.
26##===----------------------------------------------------------------------===##
27
28my $Compiler;
29my $Clang;
30my $DefaultCCompiler;
31my $DefaultCXXCompiler;
32
33if (`uname -a` =~ m/Darwin/) { 
34  $DefaultCCompiler = 'clang';
35  $DefaultCXXCompiler = 'clang++';
36} else {
37  $DefaultCCompiler = 'gcc';
38  $DefaultCXXCompiler = 'g++';
39}
40
41if ($FindBin::Script =~ /c\+\+-analyzer/) {
42  $Compiler = $ENV{'CCC_CXX'};
43  if (!defined $Compiler) { $Compiler = $DefaultCXXCompiler; }
44  
45  $Clang = $ENV{'CLANG_CXX'};
46  if (!defined $Clang) { $Clang = 'clang++'; }
47}
48else {
49  $Compiler = $ENV{'CCC_CC'};
50  if (!defined $Compiler) { $Compiler = $DefaultCCompiler; }
51
52  $Clang = $ENV{'CLANG'};
53  if (!defined $Clang) { $Clang = 'clang'; }
54}
55
56##===----------------------------------------------------------------------===##
57# Cleanup.
58##===----------------------------------------------------------------------===##
59
60my $ReportFailures = $ENV{'CCC_REPORT_FAILURES'};
61if (!defined $ReportFailures) { $ReportFailures = 1; }
62
63my $CleanupFile;
64my $ResultFile;
65
66# Remove any stale files at exit.
67END { 
68  if (defined $ResultFile && -z $ResultFile) {
69    `rm -f $ResultFile`;
70  }
71  if (defined $CleanupFile) {
72    `rm -f $CleanupFile`;
73  }
74}
75
76##----------------------------------------------------------------------------##
77#  Process Clang Crashes.
78##----------------------------------------------------------------------------##
79
80sub GetPPExt {
81  my $Lang = shift;
82  if ($Lang =~ /objective-c\+\+/) { return ".mii" };
83  if ($Lang =~ /objective-c/) { return ".mi"; }
84  if ($Lang =~ /c\+\+/) { return ".ii"; }
85  return ".i";
86}
87
88# Set this to 1 if we want to include 'parser rejects' files.
89my $IncludeParserRejects = 0;
90my $ParserRejects = "Parser Rejects";
91my $AttributeIgnored = "Attribute Ignored";
92my $OtherError = "Other Error";
93
94sub ProcessClangFailure {
95  my ($Clang, $Lang, $file, $Args, $HtmlDir, $ErrorType, $ofile) = @_;
96  my $Dir = "$HtmlDir/failures";
97  mkpath $Dir;
98  
99  my $prefix = "clang_crash";
100  if ($ErrorType eq $ParserRejects) {
101    $prefix = "clang_parser_rejects";
102  }
103  elsif ($ErrorType eq $AttributeIgnored) {
104    $prefix = "clang_attribute_ignored";
105  }
106  elsif ($ErrorType eq $OtherError) {
107    $prefix = "clang_other_error";
108  }
109
110  # Generate the preprocessed file with Clang.
111  my ($PPH, $PPFile) = tempfile( $prefix . "_XXXXXX",
112                                 SUFFIX => GetPPExt($Lang),
113                                 DIR => $Dir);
114  system $Clang, @$Args, "-E", "-o", $PPFile;
115  close ($PPH);
116  
117  # Create the info file.
118  open (OUT, ">", "$PPFile.info.txt") or die "Cannot open $PPFile.info.txt\n";
119  print OUT abs_path($file), "\n";
120  print OUT "$ErrorType\n";
121  print OUT "@$Args\n";
122  close OUT;
123  `uname -a >> $PPFile.info.txt 2>&1`;
124  `$Compiler -v >> $PPFile.info.txt 2>&1`;
125  system 'mv',$ofile,"$PPFile.stderr.txt";
126  return (basename $PPFile);
127}
128
129##----------------------------------------------------------------------------##
130#  Running the analyzer.
131##----------------------------------------------------------------------------##
132
133sub GetCCArgs {
134  my $mode = shift;
135  my $Args = shift;
136  
137  pipe (FROM_CHILD, TO_PARENT);
138  my $pid = fork();
139  if ($pid == 0) {
140    close FROM_CHILD;
141    open(STDOUT,">&", \*TO_PARENT);
142    open(STDERR,">&", \*TO_PARENT);
143    exec $Clang, "-###", $mode, @$Args;
144  }  
145  close(TO_PARENT);
146  my $line;
147  while (<FROM_CHILD>) {
148    next if (!/-cc1/);
149    $line = $_;
150  }
151
152  waitpid($pid,0);
153  close(FROM_CHILD);
154  
155  die "could not find clang line\n" if (!defined $line);
156  # Strip the newline and initial whitspace
157  chomp $line;
158  $line =~ s/^\s+//;
159  my @items = quotewords('\s+', 0, $line);
160  my $cmd = shift @items;
161  die "cannot find 'clang' in 'clang' command\n" if (!($cmd =~ /clang/));
162  return \@items;
163}
164
165sub Analyze {
166  my ($Clang, $OriginalArgs, $AnalyzeArgs, $Lang, $Output, $Verbose, $HtmlDir,
167      $file) = @_;
168
169  my @Args = @$OriginalArgs;
170  my $Cmd;
171  my @CmdArgs;
172  my @CmdArgsSansAnalyses;
173
174  if ($Lang =~ /header/) {
175    exit 0 if (!defined ($Output));
176    $Cmd = 'cp';
177    push @CmdArgs, $file;
178    # Remove the PCH extension.
179    $Output =~ s/[.]gch$//;
180    push @CmdArgs, $Output;
181    @CmdArgsSansAnalyses = @CmdArgs;
182  }
183  else {
184    $Cmd = $Clang;
185
186    # Create arguments for doing regular parsing.
187    my $SyntaxArgs = GetCCArgs("-fsyntax-only", \@Args);
188    @CmdArgsSansAnalyses = @$SyntaxArgs;
189
190    # Create arguments for doing static analysis.
191    if (defined $ResultFile) {
192      push @Args, '-o', $ResultFile;
193    }
194    elsif (defined $HtmlDir) {
195      push @Args, '-o', $HtmlDir;
196    }
197    if ($Verbose) {
198      push @Args, "-Xclang", "-analyzer-display-progress";
199    }
200
201    foreach my $arg (@$AnalyzeArgs) {
202      push @Args, "-Xclang", $arg;
203    }
204
205    # Display Ubiviz graph?
206    if (defined $ENV{'CCC_UBI'}) {   
207      push @Args, "-Xclang", "-analyzer-viz-egraph-ubigraph";
208    }
209
210    my $AnalysisArgs = GetCCArgs("--analyze", \@Args);
211    @CmdArgs = @$AnalysisArgs;
212  }
213
214  my @PrintArgs;
215  my $dir;
216
217  if ($Verbose) {
218    $dir = getcwd();
219    print STDERR "\n[LOCATION]: $dir\n";
220    push @PrintArgs,"'$Cmd'";
221    foreach my $arg (@CmdArgs) {
222        push @PrintArgs,"\'$arg\'";
223    }
224  }
225  if ($Verbose == 1) {
226    # We MUST print to stderr.  Some clients use the stdout output of
227    # gcc for various purposes. 
228    print STDERR join(' ', @PrintArgs);
229    print STDERR "\n";
230  }
231  elsif ($Verbose == 2) {
232    print STDERR "#SHELL (cd '$dir' && @PrintArgs)\n";
233  }
234
235  # Capture the STDERR of clang and send it to a temporary file.
236  # Capture the STDOUT of clang and reroute it to ccc-analyzer's STDERR.
237  # We save the output file in the 'crashes' directory if clang encounters
238  # any problems with the file.  
239  pipe (FROM_CHILD, TO_PARENT);
240  my $pid = fork();
241  if ($pid == 0) {
242    close FROM_CHILD;
243    open(STDOUT,">&", \*TO_PARENT);
244    open(STDERR,">&", \*TO_PARENT);
245    exec $Cmd, @CmdArgs;
246  }
247
248  close TO_PARENT;
249  my ($ofh, $ofile) = tempfile("clang_output_XXXXXX", DIR => $HtmlDir);
250  
251  while (<FROM_CHILD>) {
252    print $ofh $_;
253    print STDERR $_;
254  }
255  close $ofh;
256
257  waitpid($pid,0);
258  close(FROM_CHILD);
259  my $Result = $?;
260
261  # Did the command die because of a signal?
262  if ($ReportFailures) {
263    if ($Result & 127 and $Cmd eq $Clang and defined $HtmlDir) {
264      ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses,
265                          $HtmlDir, "Crash", $ofile);
266    }
267    elsif ($Result) {
268      if ($IncludeParserRejects && !($file =~/conftest/)) {
269        ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses,
270                            $HtmlDir, $ParserRejects, $ofile);
271      } else {
272        ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses,
273                            $HtmlDir, $OtherError, $ofile);
274      }
275    }
276    else {
277      # Check if there were any unhandled attributes.
278      if (open(CHILD, $ofile)) {
279        my %attributes_not_handled;
280
281        # Don't flag warnings about the following attributes that we
282        # know are currently not supported by Clang.
283        $attributes_not_handled{"cdecl"} = 1;
284
285        my $ppfile;
286        while (<CHILD>) {
287          next if (! /warning: '([^\']+)' attribute ignored/);
288
289          # Have we already spotted this unhandled attribute?
290          next if (defined $attributes_not_handled{$1});
291          $attributes_not_handled{$1} = 1;
292        
293          # Get the name of the attribute file.
294          my $dir = "$HtmlDir/failures";
295          my $afile = "$dir/attribute_ignored_$1.txt";
296        
297          # Only create another preprocessed file if the attribute file
298          # doesn't exist yet.
299          next if (-e $afile);
300        
301          # Add this file to the list of files that contained this attribute.
302          # Generate a preprocessed file if we haven't already.
303          if (!(defined $ppfile)) {
304            $ppfile = ProcessClangFailure($Clang, $Lang, $file,
305                                          \@CmdArgsSansAnalyses,
306                                          $HtmlDir, $AttributeIgnored, $ofile);
307          }
308
309          mkpath $dir;
310          open(AFILE, ">$afile");
311          print AFILE "$ppfile\n";
312          close(AFILE);
313        }
314        close CHILD;
315      }
316    }
317  }
318  
319  unlink($ofile);
320}
321
322##----------------------------------------------------------------------------##
323#  Lookup tables.
324##----------------------------------------------------------------------------##
325
326my %CompileOptionMap = (
327  '-nostdinc' => 0,
328  '-fblocks' => 0,
329  '-fno-builtin' => 0,
330  '-fobjc-gc-only' => 0,
331  '-fobjc-gc' => 0,
332  '-ffreestanding' => 0,
333  '-include' => 1,
334  '-idirafter' => 1,
335  '-imacros' => 1,
336  '-iprefix' => 1,
337  '-iquote' => 1,
338  '-isystem' => 1,
339  '-iwithprefix' => 1,
340  '-iwithprefixbefore' => 1
341);
342
343my %LinkerOptionMap = (
344  '-framework' => 1,
345  '-fobjc-link-runtime' => 0
346);
347
348my %CompilerLinkerOptionMap = (
349  '-fobjc-arc' => 0,
350  '-fno-objc-arc' => 0,
351  '-fobjc-abi-version' => 0, # This is really a 1 argument, but always has '='
352  '-fobjc-legacy-dispatch' => 0,
353  '-mios-simulator-version-min' => 0, # This really has 1 argument, but always has '='
354  '-isysroot' => 1,
355  '-arch' => 1,
356  '-m32' => 0,
357  '-m64' => 0,
358  '-stdlib' => 0, # This is really a 1 argument, but always has '='
359  '-v' => 0,
360  '-fpascal-strings' => 0,
361  '-mmacosx-version-min' => 0, # This is really a 1 argument, but always has '='
362  '-miphoneos-version-min' => 0 # This is really a 1 argument, but always has '='
363);
364
365my %IgnoredOptionMap = (
366  '-MT' => 1,  # Ignore these preprocessor options.
367  '-MF' => 1,
368
369  '-fsyntax-only' => 0,
370  '-save-temps' => 0,
371  '-install_name' => 1,
372  '-exported_symbols_list' => 1,
373  '-current_version' => 1,
374  '-compatibility_version' => 1,
375  '-init' => 1,
376  '-e' => 1,
377  '-seg1addr' => 1,
378  '-bundle_loader' => 1,
379  '-multiply_defined' => 1,
380  '-sectorder' => 3,
381  '--param' => 1,
382  '-u' => 1,
383  '--serialize-diagnostics' => 1
384);
385
386my %LangMap = (
387  'c'   => 'c',
388  'cp'  => 'c++',
389  'cpp' => 'c++',
390  'cxx' => 'c++',
391  'txx' => 'c++',
392  'cc'  => 'c++',
393  'C'   => 'c++',
394  'ii'  => 'c++',
395  'i'   => 'c-cpp-output',
396  'm'   => 'objective-c',
397  'mi'  => 'objective-c-cpp-output',
398  'mm'  => 'objective-c++'
399);
400
401my %UniqueOptions = (
402  '-isysroot' => 0  
403);
404
405##----------------------------------------------------------------------------##
406# Languages accepted.
407##----------------------------------------------------------------------------##
408
409my %LangsAccepted = (
410  "objective-c" => 1,
411  "c" => 1,
412  "c++" => 1,
413  "objective-c++" => 1
414);
415
416##----------------------------------------------------------------------------##
417#  Main Logic.
418##----------------------------------------------------------------------------##
419
420my $Action = 'link';
421my @CompileOpts;
422my @LinkOpts;
423my @Files;
424my $Lang;
425my $Output;
426my %Uniqued;
427
428# Forward arguments to gcc.
429my $Status = system($Compiler,@ARGV);
430if  (defined $ENV{'CCC_ANALYZER_LOG'}) {
431  print "$Compiler @ARGV\n";
432}
433if ($Status) { exit($Status >> 8); }
434
435# Get the analysis options.
436my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
437
438# Get the plugins to load.
439my $Plugins = $ENV{'CCC_ANALYZER_PLUGINS'};
440
441# Get the store model.
442my $StoreModel = $ENV{'CCC_ANALYZER_STORE_MODEL'};
443
444# Get the constraints engine.
445my $ConstraintsModel = $ENV{'CCC_ANALYZER_CONSTRAINTS_MODEL'};
446
447#Get the internal stats setting.
448my $InternalStats = $ENV{'CCC_ANALYZER_INTERNAL_STATS'};
449
450# Get the output format.
451my $OutputFormat = $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'};
452if (!defined $OutputFormat) { $OutputFormat = "html"; }
453
454# Determine the level of verbosity.
455my $Verbose = 0;
456if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; }
457if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; }
458
459# Get the HTML output directory.
460my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'};
461
462my %DisabledArchs = ('ppc' => 1, 'ppc64' => 1);
463my %ArchsSeen;
464my $HadArch = 0;
465
466# Process the arguments.
467foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
468  my $Arg = $ARGV[$i];  
469  my ($ArgKey) = split /=/,$Arg,2;
470
471  # Modes ccc-analyzer supports
472  if ($Arg =~ /^-(E|MM?)$/) { $Action = 'preprocess'; }
473  elsif ($Arg eq '-c') { $Action = 'compile'; }
474  elsif ($Arg =~ /^-print-prog-name/) { exit 0; }
475
476  # Specially handle duplicate cases of -arch
477  if ($Arg eq "-arch") {
478    my $arch = $ARGV[$i+1];
479    # We don't want to process 'ppc' because of Clang's lack of support
480    # for Altivec (also some #defines won't likely be defined correctly, etc.)
481    if (!(defined $DisabledArchs{$arch})) { $ArchsSeen{$arch} = 1; }
482    $HadArch = 1;
483    ++$i;
484    next;
485  }
486
487  # Options with possible arguments that should pass through to compiler.
488  if (defined $CompileOptionMap{$ArgKey}) {
489    my $Cnt = $CompileOptionMap{$ArgKey};
490    push @CompileOpts,$Arg;
491    while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; }
492    next;
493  }
494  if ($Arg =~ /-msse.*/) {
495    push @CompileOpts,$Arg;
496    next;
497  }
498  # Handle the case where there isn't a space after -iquote
499  if ($Arg =~ /-iquote.*/) {
500    push @CompileOpts,$Arg;
501    next;
502  }
503
504  # Options with possible arguments that should pass through to linker.
505  if (defined $LinkerOptionMap{$ArgKey}) {
506    my $Cnt = $LinkerOptionMap{$ArgKey};
507    push @LinkOpts,$Arg;
508    while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
509    next;
510  }
511
512  # Options with possible arguments that should pass through to both compiler
513  # and the linker.
514  if (defined $CompilerLinkerOptionMap{$ArgKey}) {
515    my $Cnt = $CompilerLinkerOptionMap{$ArgKey};
516    
517    # Check if this is an option that should have a unique value, and if so
518    # determine if the value was checked before.
519    if ($UniqueOptions{$Arg}) {
520      if (defined $Uniqued{$Arg}) {
521        $i += $Cnt;
522        next;
523      }
524      $Uniqued{$Arg} = 1;
525    }
526    
527    push @CompileOpts,$Arg;    
528    push @LinkOpts,$Arg;
529
530    while ($Cnt > 0) {
531      ++$i; --$Cnt;
532      push @CompileOpts, $ARGV[$i];
533      push @LinkOpts, $ARGV[$i];
534    }
535    next;
536  }
537  
538  # Ignored options.
539  if (defined $IgnoredOptionMap{$ArgKey}) {
540    my $Cnt = $IgnoredOptionMap{$ArgKey};
541    while ($Cnt > 0) {
542      ++$i; --$Cnt;
543    }
544    next;
545  }
546  
547  # Compile mode flags.
548  if ($Arg =~ /^-[D,I,U](.*)$/) {
549    my $Tmp = $Arg;    
550    if ($1 eq '') {
551      # FIXME: Check if we are going off the end.
552      ++$i;
553      $Tmp = $Arg . $ARGV[$i];
554    }
555    push @CompileOpts,$Tmp;
556    next;
557  }
558  
559  # Language.
560  if ($Arg eq '-x') {
561    $Lang = $ARGV[$i+1];
562    ++$i; next;
563  }
564
565  # Output file.
566  if ($Arg eq '-o') {
567    ++$i;
568    $Output = $ARGV[$i];
569    next;
570  }
571  
572  # Get the link mode.
573  if ($Arg =~ /^-[l,L,O]/) {
574    if ($Arg eq '-O') { push @LinkOpts,'-O1'; }
575    elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; }
576    else { push @LinkOpts,$Arg; }
577    next;
578  }
579  
580  if ($Arg =~ /^-std=/) {
581    push @CompileOpts,$Arg;
582    next;
583  }
584  
585#  if ($Arg =~ /^-f/) {
586#    # FIXME: Not sure if the remaining -fxxxx options have no arguments.
587#    push @CompileOpts,$Arg;
588#    push @LinkOpts,$Arg;  # FIXME: Not sure if these are link opts.
589#  }
590  
591  # Get the compiler/link mode.
592  if ($Arg =~ /^-F(.+)$/) {
593    my $Tmp = $Arg;
594    if ($1 eq '') {
595      # FIXME: Check if we are going off the end.
596      ++$i;
597      $Tmp = $Arg . $ARGV[$i];
598    }
599    push @CompileOpts,$Tmp;
600    push @LinkOpts,$Tmp;
601    next;
602  }
603
604  # Input files.
605  if ($Arg eq '-filelist') {
606    # FIXME: Make sure we aren't walking off the end.
607    open(IN, $ARGV[$i+1]);
608    while (<IN>) { s/\015?\012//; push @Files,$_; }
609    close(IN);
610    ++$i;
611    next;
612  }
613  
614  # Handle -Wno-.  We don't care about extra warnings, but
615  # we should suppress ones that we don't want to see.
616  if ($Arg =~ /^-Wno-/) {
617    push @CompileOpts, $Arg;
618    next;
619  }
620
621  if (!($Arg =~ /^-/)) {
622    push @Files, $Arg;
623    next;
624  }
625}
626
627if ($Action eq 'compile' or $Action eq 'link') {
628  my @Archs = keys %ArchsSeen;
629  # Skip the file if we don't support the architectures specified.
630  exit 0 if ($HadArch && scalar(@Archs) == 0);
631
632  foreach my $file (@Files) {
633    # Determine the language for the file.
634    my $FileLang = $Lang;
635
636    if (!defined($FileLang)) {
637      # Infer the language from the extension.
638      if ($file =~ /[.]([^.]+)$/) {
639        $FileLang = $LangMap{$1};
640      }
641    }
642    
643    # FileLang still not defined?  Skip the file.
644    next if (!defined $FileLang);
645
646    # Language not accepted?
647    next if (!defined $LangsAccepted{$FileLang});
648
649    my @CmdArgs;
650    my @AnalyzeArgs;    
651    
652    if ($FileLang ne 'unknown') {
653      push @CmdArgs, '-x', $FileLang;
654    }
655
656    if (defined $StoreModel) {
657      push @AnalyzeArgs, "-analyzer-store=$StoreModel";
658    }
659
660    if (defined $ConstraintsModel) {
661      push @AnalyzeArgs, "-analyzer-constraints=$ConstraintsModel";
662    }
663
664    if (defined $InternalStats) {
665      push @AnalyzeArgs, "-analyzer-stats";
666    }
667    
668    if (defined $Analyses) {
669      push @AnalyzeArgs, split '\s+', $Analyses;
670    }
671
672    if (defined $Plugins) {
673      push @AnalyzeArgs, split '\s+', $Plugins;
674    }
675
676    if (defined $OutputFormat) {
677      push @AnalyzeArgs, "-analyzer-output=" . $OutputFormat;
678      if ($OutputFormat =~ /plist/) {
679        # Change "Output" to be a file.
680        my ($h, $f) = tempfile("report-XXXXXX", SUFFIX => ".plist",
681                               DIR => $HtmlDir);
682        $ResultFile = $f;
683        # If the HtmlDir is not set, we sould clean up the plist files.
684        if (!defined $HtmlDir || -z $HtmlDir) {
685          $CleanupFile = $f;
686        }
687      }
688    }
689
690    push @CmdArgs, @CompileOpts;
691    push @CmdArgs, $file;
692
693    if (scalar @Archs) {
694      foreach my $arch (@Archs) {
695        my @NewArgs;
696        push @NewArgs, '-arch', $arch;
697        push @NewArgs, @CmdArgs;
698        Analyze($Clang, \@NewArgs, \@AnalyzeArgs, $FileLang, $Output,
699                $Verbose, $HtmlDir, $file);
700      }
701    }
702    else {
703      Analyze($Clang, \@CmdArgs, \@AnalyzeArgs, $FileLang, $Output,
704              $Verbose, $HtmlDir, $file);
705    }
706  }
707}
708
709exit($Status >> 8);
710
711