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