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
7 ##===----------------------------------------------------------------------===##
9 # A script designed to interpose between the build system and gcc. It invokes
10 # both gcc and the static analyzer.
12 ##===----------------------------------------------------------------------===##
17 use Cwd qw
/ getcwd abs_path /;
18 use File
::Temp qw
/ tempfile /;
19 use File
::Path qw
/ mkpath /;
23 ##===----------------------------------------------------------------------===##
24 # List form 'system' with STDOUT and STDERR captured.
25 ##===----------------------------------------------------------------------===##
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",
37 open(STDOUT
, ">$TmpFile");
38 open(STDERR
, ">&", \
*STDOUT
);
40 # Invoke 'system', STDOUT and STDERR are output to a temporary file.
43 # Restore STDOUT and STDERR.
44 open STDOUT
, ">&", \
*OLDOUT
;
45 open STDERR
, ">&", \
*OLDERR
;
50 ##===----------------------------------------------------------------------===##
51 # Compiler command setup.
52 ##===----------------------------------------------------------------------===##
54 # Search in the PATH if the compiler exists
57 foreach my $dir (split (':', $ENV{PATH
})) {
58 if (-x
"$dir/$file") {
68 my $DefaultCXXCompiler;
72 # If on OSX, use xcrun to determine the SDK root.
75 if (`uname -s` =~ 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") {
83 } elsif (`uname -s` =~ m/(FreeBSD|OpenBSD)/) {
84 $DefaultCCompiler = 'cc';
85 $DefaultCXXCompiler = 'c++';
87 $DefaultCCompiler = 'gcc';
88 $DefaultCXXCompiler = 'g++';
91 if ($FindBin::Script
=~ /c\+\+-analyzer/) {
92 $Compiler = $ENV{'CCC_CXX'};
93 if (!defined $Compiler || (! -x
$Compiler && ! SearchInPath
($Compiler))) { $Compiler = $DefaultCXXCompiler; }
95 $Clang = $ENV{'CLANG_CXX'};
96 if (!defined $Clang || ! -x
$Clang) { $Clang = 'clang++'; }
101 $Compiler = $ENV{'CCC_CC'};
102 if (!defined $Compiler || (! -x
$Compiler && ! SearchInPath
($Compiler))) { $Compiler = $DefaultCCompiler; }
104 $Clang = $ENV{'CLANG'};
105 if (!defined $Clang || ! -x
$Clang) { $Clang = 'clang'; }
110 $AnalyzerTarget = $ENV{'CLANG_ANALYZER_TARGET'};
112 ##===----------------------------------------------------------------------===##
114 ##===----------------------------------------------------------------------===##
116 my $ReportFailures = $ENV{'CCC_REPORT_FAILURES'};
117 if (!defined $ReportFailures) { $ReportFailures = 1; }
122 # Remove any stale files at exit.
124 if (defined $ResultFile && -z
$ResultFile) {
127 if (defined $CleanupFile) {
128 unlink($CleanupFile);
132 ##----------------------------------------------------------------------------##
133 # Process Clang Crashes.
134 ##----------------------------------------------------------------------------##
138 if ($Lang =~ /objective-c\+\+/) { return ".mii" };
139 if ($Lang =~ /objective-c/) { return ".mi"; }
140 if ($Lang =~ /c\+\+/) { return ".ii"; }
144 # Set this to 1 if we want to include 'parser rejects' files.
145 my $IncludeParserRejects = 0;
146 my $ParserRejects = "Parser Rejects";
147 my $AttributeIgnored = "Attribute Ignored";
148 my $OtherError = "Other Error";
150 sub ProcessClangFailure
{
151 my ($Clang, $Lang, $file, $Args, $HtmlDir, $ErrorType, $ofile) = @_;
152 my $Dir = "$HtmlDir/failures";
155 my $prefix = "clang_crash";
156 if ($ErrorType eq $ParserRejects) {
157 $prefix = "clang_parser_rejects";
159 elsif ($ErrorType eq $AttributeIgnored) {
160 $prefix = "clang_attribute_ignored";
162 elsif ($ErrorType eq $OtherError) {
163 $prefix = "clang_other_error";
166 # Generate the preprocessed file with Clang.
167 my ($PPH, $PPFile) = tempfile
( $prefix . "_XXXXXX",
168 SUFFIX
=> GetPPExt
($Lang),
171 system $Clang, @
$Args, "-E", "-o", $PPFile;
173 # Create the info file.
174 open (OUT
, ">", "$PPFile.info.txt") or die "Cannot open $PPFile.info.txt\n";
175 print OUT abs_path
($file), "\n";
176 print OUT
"$ErrorType\n";
177 print OUT
"@$Args\n";
179 `uname -a >> $PPFile.info.txt 2>&1`;
180 `"$Compiler" -v >> $PPFile.info.txt 2>&1`;
181 rename($ofile, "$PPFile.stderr.txt");
182 return (basename
$PPFile);
185 ##----------------------------------------------------------------------------##
186 # Running the analyzer.
187 ##----------------------------------------------------------------------------##
194 my $OutputStream = silent_system
($HtmlDir, $Clang, "-###", $mode, @
$Args);
195 while (<$OutputStream>) {
196 next if (!/\s"?-cc1"?\s/);
199 die "could not find clang line\n" if (!defined $line);
200 # Strip leading and trailing whitespace characters.
201 $line =~ s/^\s+|\s+$//g;
202 my @items = quotewords
('\s+', 0, $line);
203 my $cmd = shift @items;
204 die "cannot find 'clang' in 'clang' command\n" if (!($cmd =~ /clang/ || basename
($cmd) =~ /llvm/));
205 # If this is the llvm-driver the internal command will look like "llvm clang ...".
206 # Later this will be invoked like "clang clang ...", so skip over it.
207 if (basename
($cmd) =~ /llvm/) {
208 die "Expected first arg to llvm driver to be 'clang'" if $items[0] ne "clang";
215 my ($Clang, $OriginalArgs, $AnalyzeArgs, $Lang, $Output, $Verbose, $HtmlDir,
218 my @Args = @
$OriginalArgs;
221 my @CmdArgsSansAnalyses;
223 if ($Lang =~ /header/) {
224 exit 0 if (!defined ($Output));
226 push @CmdArgs, $file;
227 # Remove the PCH extension.
228 $Output =~ s/[.]gch$//;
229 push @CmdArgs, $Output;
230 @CmdArgsSansAnalyses = @CmdArgs;
235 # Create arguments for doing regular parsing.
236 my $SyntaxArgs = GetCCArgs
($HtmlDir, "-fsyntax-only", \
@Args);
237 @CmdArgsSansAnalyses = @
$SyntaxArgs;
239 # Create arguments for doing static analysis.
240 if (defined $ResultFile) {
241 push @Args, '-o', $ResultFile;
243 elsif (defined $HtmlDir) {
244 push @Args, '-o', $HtmlDir;
247 push @Args, "-Xclang", "-analyzer-display-progress";
250 foreach my $arg (@
$AnalyzeArgs) {
251 push @Args, "-Xclang", $arg;
254 if (defined $AnalyzerTarget) {
255 push @Args, "-target", $AnalyzerTarget;
258 my $AnalysisArgs = GetCCArgs
($HtmlDir, "--analyze", \
@Args);
259 @CmdArgs = @
$AnalysisArgs;
267 print STDERR
"\n[LOCATION]: $dir\n";
268 push @PrintArgs,"'$Cmd'";
269 foreach my $arg (@CmdArgs) {
270 push @PrintArgs,"\'$arg\'";
274 # We MUST print to stderr. Some clients use the stdout output of
275 # gcc for various purposes.
276 print STDERR
join(' ', @PrintArgs);
279 elsif ($Verbose == 2) {
280 print STDERR
"#SHELL (cd '$dir' && @PrintArgs)\n";
283 # Save STDOUT and STDERR of clang to a temporary file and reroute
284 # all clang output to ccc-analyzer's STDERR.
285 # We save the output file in the 'crashes' directory if clang encounters
286 # any problems with the file.
287 my ($ofh, $ofile) = tempfile
("clang_output_XXXXXX", DIR
=> $HtmlDir);
289 my $OutputStream = silent_system
($HtmlDir, $Cmd, @CmdArgs);
290 while ( <$OutputStream> ) {
297 # Did the command die because of a signal?
298 if ($ReportFailures) {
299 if ($Result & 127 and $Cmd eq $Clang and defined $HtmlDir) {
300 ProcessClangFailure
($Clang, $Lang, $file, \
@CmdArgsSansAnalyses,
301 $HtmlDir, "Crash", $ofile);
304 if ($IncludeParserRejects && !($file =~/conftest/)) {
305 ProcessClangFailure
($Clang, $Lang, $file, \
@CmdArgsSansAnalyses,
306 $HtmlDir, $ParserRejects, $ofile);
308 ProcessClangFailure
($Clang, $Lang, $file, \
@CmdArgsSansAnalyses,
309 $HtmlDir, $OtherError, $ofile);
313 # Check if there were any unhandled attributes.
314 if (open(CHILD
, $ofile)) {
315 my %attributes_not_handled;
317 # Don't flag warnings about the following attributes that we
318 # know are currently not supported by Clang.
319 $attributes_not_handled{"cdecl"} = 1;
323 next if (! /warning: '([^\']+)' attribute ignored/);
325 # Have we already spotted this unhandled attribute?
326 next if (defined $attributes_not_handled{$1});
327 $attributes_not_handled{$1} = 1;
329 # Get the name of the attribute file.
330 my $dir = "$HtmlDir/failures";
331 my $afile = "$dir/attribute_ignored_$1.txt";
333 # Only create another preprocessed file if the attribute file
337 # Add this file to the list of files that contained this attribute.
338 # Generate a preprocessed file if we haven't already.
339 if (!(defined $ppfile)) {
340 $ppfile = ProcessClangFailure
($Clang, $Lang, $file,
341 \
@CmdArgsSansAnalyses,
342 $HtmlDir, $AttributeIgnored, $ofile);
346 open(AFILE
, ">$afile");
347 print AFILE
"$ppfile\n";
358 ##----------------------------------------------------------------------------##
360 ##----------------------------------------------------------------------------##
362 my %CompileOptionMap = (
371 '-iwithprefixbefore' => 1
374 my %LinkerOptionMap = (
376 '-fobjc-link-runtime' => 0
379 my %CompilerLinkerOptionMap = (
380 '-Wwrite-strings' => 0,
381 '-ftrapv-handler' => 1, # specifically call out separated -f flag
382 '-mios-simulator-version-min' => 0, # This really has 1 argument, but always has '='
387 '-stdlib' => 0, # This is really a 1 argument, but always has '='
391 '-mmacosx-version-min' => 0, # This is really a 1 argument, but always has '='
392 '-mmacos-version-min' => 0, # This is really a 1 argument, but always has '='
393 '-miphoneos-version-min' => 0, # This is really a 1 argument, but always has '='
397 my %IgnoredOptionMap = (
398 '-MT' => 1, # Ignore these preprocessor options.
401 '-fsyntax-only' => 0,
403 '-install_name' => 1,
404 '-exported_symbols_list' => 1,
405 '-current_version' => 1,
406 '-compatibility_version' => 1,
410 '-bundle_loader' => 1,
411 '-multiply_defined' => 1,
415 '--serialize-diagnostics' => 1
419 'c' => $IsCXX ?
'c++' : 'c',
426 'ii' => 'c++-cpp-output',
427 'i' => $IsCXX ?
'c++-cpp-output' : 'cpp-output',
428 'm' => 'objective-c',
429 'mi' => 'objective-c-cpp-output',
430 'mm' => 'objective-c++',
431 'mii' => 'objective-c++-cpp-output',
434 my %UniqueOptions = (
438 ##----------------------------------------------------------------------------##
439 # Languages accepted.
440 ##----------------------------------------------------------------------------##
442 my %LangsAccepted = (
446 "objective-c++" => 1,
448 "objective-c-cpp-output" => 1,
449 "c++-cpp-output" => 1
452 ##----------------------------------------------------------------------------##
454 ##----------------------------------------------------------------------------##
464 # Forward arguments to gcc.
465 my $Status = system($Compiler,@ARGV);
466 if (defined $ENV{'CCC_ANALYZER_LOG'}) {
467 print STDERR
"$Compiler @ARGV\n";
469 if ($Status) { exit($Status >> 8); }
471 # Get the analysis options.
472 my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
474 # Get the plugins to load.
475 my $Plugins = $ENV{'CCC_ANALYZER_PLUGINS'};
477 # Get the constraints engine.
478 my $ConstraintsModel = $ENV{'CCC_ANALYZER_CONSTRAINTS_MODEL'};
480 #Get the internal stats setting.
481 my $InternalStats = $ENV{'CCC_ANALYZER_INTERNAL_STATS'};
483 # Get the output format.
484 my $OutputFormat = $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'};
485 if (!defined $OutputFormat) { $OutputFormat = "html"; }
487 # Get the config options.
488 my $ConfigOptions = $ENV{'CCC_ANALYZER_CONFIG'};
490 # Determine the level of verbosity.
492 if (defined $ENV{'CCC_ANALYZER_VERBOSE'}) { $Verbose = 1; }
493 if (defined $ENV{'CCC_ANALYZER_LOG'}) { $Verbose = 2; }
495 # Get the HTML output directory.
496 my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'};
498 # Get force-analyze-debug-code option.
499 my $ForceAnalyzeDebugCode = $ENV{'CCC_ANALYZER_FORCE_ANALYZE_DEBUG_CODE'};
501 my %DisabledArchs = ('ppc' => 1, 'ppc64' => 1);
506 # Process the arguments.
507 foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
509 my @ArgParts = split /=/,$Arg,2;
510 my $ArgKey = $ArgParts[0];
512 # Be friendly to "" in the argument list.
513 if (!defined($ArgKey)) {
517 # Modes ccc-analyzer supports
518 if ($Arg =~ /^-(E|MM?)$/) { $Action = 'preprocess'; }
519 elsif ($Arg eq '-c') { $Action = 'compile'; }
520 elsif ($Arg =~ /^-print-prog-name/) { exit 0; }
522 # Specially handle duplicate cases of -arch
523 if ($Arg eq "-arch") {
524 my $arch = $ARGV[$i+1];
525 # We don't want to process 'ppc' because of Clang's lack of support
526 # for Altivec (also some #defines won't likely be defined correctly, etc.)
527 if (!(defined $DisabledArchs{$arch})) { $ArchsSeen{$arch} = 1; }
533 # On OSX/iOS, record if an SDK path was specified. This
534 # is innocuous for other platforms, so the check just happens.
535 if ($Arg =~ /^-isysroot/) {
539 # Options with possible arguments that should pass through to compiler.
540 if (defined $CompileOptionMap{$ArgKey}) {
541 my $Cnt = $CompileOptionMap{$ArgKey};
542 push @CompileOpts,$Arg;
543 while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; }
546 # Handle the case where there isn't a space after -iquote
547 if ($Arg =~ /^-iquote.*/) {
548 push @CompileOpts,$Arg;
552 # Options with possible arguments that should pass through to linker.
553 if (defined $LinkerOptionMap{$ArgKey}) {
554 my $Cnt = $LinkerOptionMap{$ArgKey};
556 while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
560 # Options with possible arguments that should pass through to both compiler
562 if (defined $CompilerLinkerOptionMap{$ArgKey}) {
563 my $Cnt = $CompilerLinkerOptionMap{$ArgKey};
565 # Check if this is an option that should have a unique value, and if so
566 # determine if the value was checked before.
567 if ($UniqueOptions{$Arg}) {
568 if (defined $Uniqued{$Arg}) {
575 push @CompileOpts,$Arg;
578 if (scalar @ArgParts == 1) {
581 push @CompileOpts, $ARGV[$i];
582 push @LinkOpts, $ARGV[$i];
589 if (defined $IgnoredOptionMap{$ArgKey}) {
590 my $Cnt = $IgnoredOptionMap{$ArgKey};
597 # Compile mode flags.
598 if ($Arg =~ /^-(?:[DIU]|isystem)(.*)$/) {
601 # FIXME: Check if we are going off the end.
603 $Tmp = $Arg . $ARGV[$i];
605 push @CompileOpts,$Tmp;
609 if ($Arg =~ /^-m.*/) {
610 push @CompileOpts,$Arg;
628 if ($Arg =~ /^-[l,L,O]/) {
629 if ($Arg eq '-O') { push @LinkOpts,'-O1'; }
630 elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; }
631 else { push @LinkOpts,$Arg; }
633 # Must pass this along for the __OPTIMIZE__ macro
634 if ($Arg =~ /^-O/) { push @CompileOpts,$Arg; }
638 if ($Arg =~ /^-std=/) {
639 push @CompileOpts,$Arg;
643 # Get the compiler/link mode.
644 if ($Arg =~ /^-F(.+)$/) {
647 # FIXME: Check if we are going off the end.
649 $Tmp = $Arg . $ARGV[$i];
651 push @CompileOpts,$Tmp;
657 if ($Arg eq '-filelist') {
658 # FIXME: Make sure we aren't walking off the end.
659 open(IN
, $ARGV[$i+1]);
660 while (<IN
>) { s/\015?\012//; push @Files,$_; }
667 push @CompileOpts,$Arg;
672 # Handle -Wno-. We don't care about extra warnings, but
673 # we should suppress ones that we don't want to see.
674 if ($Arg =~ /^-Wno-/) {
675 push @CompileOpts, $Arg;
679 # Handle -Xclang some-arg. Add both arguments to the compiler options.
680 if ($Arg =~ /^-Xclang$/) {
681 # FIXME: Check if we are going off the end.
683 push @CompileOpts, $Arg;
684 push @CompileOpts, $ARGV[$i];
688 if (!($Arg =~ /^-/)) {
694 # Forcedly enable debugging if requested by user.
695 if ($ForceAnalyzeDebugCode) {
696 push @CompileOpts, '-UNDEBUG';
699 # If we are on OSX and have an installation where the
700 # default SDK is inferred by xcrun use xcrun to infer
702 if (not $HasSDK and $UseXCRUN) {
703 my $sdk = `/usr/bin/xcrun --show-sdk-path -sdk macosx`;
705 push @CompileOpts, "-isysroot", $sdk;
708 if ($Action eq 'compile' or $Action eq 'link') {
709 my @Archs = keys %ArchsSeen;
710 # Skip the file if we don't support the architectures specified.
711 exit 0 if ($HadArch && scalar(@Archs) == 0);
713 foreach my $file (@Files) {
714 # Determine the language for the file.
715 my $FileLang = $Lang;
717 if (!defined($FileLang)) {
718 # Infer the language from the extension.
719 if ($file =~ /[.]([^.]+)$/) {
720 $FileLang = $LangMap{$1};
724 # FileLang still not defined? Skip the file.
725 next if (!defined $FileLang);
727 # Language not accepted?
728 next if (!defined $LangsAccepted{$FileLang});
733 if ($FileLang ne 'unknown') {
734 push @CmdArgs, '-x', $FileLang;
737 if (defined $ConstraintsModel) {
738 push @AnalyzeArgs, "-analyzer-constraints=$ConstraintsModel";
741 if (defined $InternalStats) {
742 push @AnalyzeArgs, "-analyzer-stats";
745 if (defined $Analyses) {
746 push @AnalyzeArgs, split '\s+', $Analyses;
749 if (defined $Plugins) {
750 push @AnalyzeArgs, split '\s+', $Plugins;
753 if (defined $OutputFormat) {
754 push @AnalyzeArgs, "-analyzer-output=" . $OutputFormat;
755 if ($OutputFormat =~ /plist/ || $OutputFormat =~ /sarif/) {
756 # Change "Output" to be a file.
757 my $Suffix = $OutputFormat =~ /plist/ ?
".plist" : ".sarif";
758 my ($h, $f) = tempfile
("report-XXXXXX", SUFFIX
=> $Suffix,
761 # If the HtmlDir is not set, we should clean up the plist files.
762 if (!defined $HtmlDir || $HtmlDir eq "") {
767 if (defined $ConfigOptions) {
768 push @AnalyzeArgs, split '\s+', $ConfigOptions;
771 push @CmdArgs, @CompileOpts;
772 push @CmdArgs, $file;
775 foreach my $arch (@Archs) {
777 push @NewArgs, '-arch', $arch;
778 push @NewArgs, @CmdArgs;
779 Analyze
($Clang, \
@NewArgs, \
@AnalyzeArgs, $FileLang, $Output,
780 $Verbose, $HtmlDir, $file);
784 Analyze
($Clang, \
@CmdArgs, \
@AnalyzeArgs, $FileLang, $Output,
785 $Verbose, $HtmlDir, $file);