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