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