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