| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| use strict; |
| use warnings; |
| use FindBin; |
| use Cwd qw/ getcwd abs_path /; |
| use File::Temp qw/ tempfile /; |
| use File::Path qw / mkpath /; |
| use File::Basename; |
| use Text::ParseWords; |
|
|
| |
| |
| |
|
|
| sub silent_system { |
| my $HtmlDir = shift; |
| my $Command = shift; |
|
|
| |
| open OLDOUT, ">&", \*STDOUT; |
| open OLDERR, ">&", \*STDERR; |
| my ($TmpFH, $TmpFile) = tempfile("temp_buf_XXXXXX", |
| DIR => $HtmlDir, |
| UNLINK => 1); |
| open(STDOUT, ">$TmpFile"); |
| open(STDERR, ">&", \*STDOUT); |
|
|
| |
| system $Command, @_; |
|
|
| |
| open STDOUT, ">&", \*OLDOUT; |
| open STDERR, ">&", \*OLDERR; |
|
|
| return $TmpFH; |
| } |
|
|
| |
| |
| |
|
|
| |
| sub SearchInPath { |
| my $file = shift; |
| foreach my $dir (split (':', $ENV{PATH})) { |
| if (-x "$dir/$file") { |
| return 1; |
| } |
| } |
| return 0; |
| } |
|
|
| my $Compiler; |
| my $Clang; |
| my $DefaultCCompiler; |
| my $DefaultCXXCompiler; |
| my $IsCXX; |
| my $AnalyzerTarget; |
|
|
| |
| my $UseXCRUN = 0; |
|
|
| if (`uname -s` =~ m/Darwin/) { |
| $DefaultCCompiler = 'clang'; |
| $DefaultCXXCompiler = 'clang++'; |
| |
| |
| if (-x "/usr/bin/xcrun") { |
| $UseXCRUN = 1; |
| } |
| } elsif (`uname -s` =~ m/(FreeBSD|OpenBSD)/) { |
| $DefaultCCompiler = 'cc'; |
| $DefaultCXXCompiler = 'c++'; |
| } else { |
| $DefaultCCompiler = 'gcc'; |
| $DefaultCXXCompiler = 'g++'; |
| } |
|
|
| if ($FindBin::Script =~ /c\+\+-analyzer/) { |
| $Compiler = $ENV{'CCC_CXX'}; |
| if (!defined $Compiler || (! -x $Compiler && ! SearchInPath($Compiler))) { $Compiler = $DefaultCXXCompiler; } |
|
|
| $Clang = $ENV{'CLANG_CXX'}; |
| if (!defined $Clang || ! -x $Clang) { $Clang = 'clang++'; } |
|
|
| $IsCXX = 1 |
| } |
| else { |
| $Compiler = $ENV{'CCC_CC'}; |
| if (!defined $Compiler || (! -x $Compiler && ! SearchInPath($Compiler))) { $Compiler = $DefaultCCompiler; } |
|
|
| $Clang = $ENV{'CLANG'}; |
| if (!defined $Clang || ! -x $Clang) { $Clang = 'clang'; } |
|
|
| $IsCXX = 0 |
| } |
|
|
| $AnalyzerTarget = $ENV{'CLANG_ANALYZER_TARGET'}; |
|
|
| |
| |
| |
|
|
| my $ReportFailures = $ENV{'CCC_REPORT_FAILURES'}; |
| if (!defined $ReportFailures) { $ReportFailures = 1; } |
|
|
| my $CleanupFile; |
| my $ResultFile; |
|
|
| |
| END { |
| if (defined $ResultFile && -z $ResultFile) { |
| unlink($ResultFile); |
| } |
| if (defined $CleanupFile) { |
| unlink($CleanupFile); |
| } |
| } |
|
|
| |
| |
| |
|
|
| sub GetPPExt { |
| my $Lang = shift; |
| if ($Lang =~ /objective-c\+\+/) { return ".mii" }; |
| if ($Lang =~ /objective-c/) { return ".mi"; } |
| if ($Lang =~ /c\+\+/) { return ".ii"; } |
| return ".i"; |
| } |
|
|
| |
| my $IncludeParserRejects = 0; |
| my $ParserRejects = "Parser Rejects"; |
| my $AttributeIgnored = "Attribute Ignored"; |
| my $OtherError = "Other Error"; |
|
|
| sub ProcessClangFailure { |
| my ($Clang, $Lang, $file, $Args, $HtmlDir, $ErrorType, $ofile) = @_; |
| my $Dir = "$HtmlDir/failures"; |
| mkpath $Dir; |
|
|
| my $prefix = "clang_crash"; |
| if ($ErrorType eq $ParserRejects) { |
| $prefix = "clang_parser_rejects"; |
| } |
| elsif ($ErrorType eq $AttributeIgnored) { |
| $prefix = "clang_attribute_ignored"; |
| } |
| elsif ($ErrorType eq $OtherError) { |
| $prefix = "clang_other_error"; |
| } |
|
|
| |
| my ($PPH, $PPFile) = tempfile( $prefix . "_XXXXXX", |
| SUFFIX => GetPPExt($Lang), |
| DIR => $Dir); |
| close ($PPH); |
| system $Clang, @$Args, "-E", "-o", $PPFile; |
|
|
| |
| open (OUT, ">", "$PPFile.info.txt") or die "Cannot open $PPFile.info.txt\n"; |
| print OUT abs_path($file), "\n"; |
| print OUT "$ErrorType\n"; |
| print OUT "@$Args\n"; |
| close OUT; |
| `uname -a >> $PPFile.info.txt 2>&1`; |
| `"$Compiler" -v >> $PPFile.info.txt 2>&1`; |
| rename($ofile, "$PPFile.stderr.txt"); |
| return (basename $PPFile); |
| } |
|
|
| |
| |
| |
|
|
| sub GetCCArgs { |
| my $HtmlDir = shift; |
| my $mode = shift; |
| my $Args = shift; |
| my $line; |
| my $OutputStream = silent_system($HtmlDir, $Clang, "-###", $mode, @$Args); |
| while (<$OutputStream>) { |
| next if (!/\s"?-cc1"?\s/); |
| $line = $_; |
| } |
| die "could not find clang line\n" if (!defined $line); |
| |
| $line =~ s/^\s+|\s+$//g; |
| my @items = quotewords('\s+', 0, $line); |
| my $cmd = shift @items; |
| die "cannot find 'clang' in 'clang' command\n" if (!($cmd =~ /clang/ || basename($cmd) =~ /llvm/)); |
| |
| |
| if (basename($cmd) =~ /llvm/) { |
| die "Expected first arg to llvm driver to be 'clang'" if $items[0] ne "clang"; |
| shift @items; |
| } |
| return \@items; |
| } |
|
|
| sub Analyze { |
| my ($Clang, $OriginalArgs, $AnalyzeArgs, $Lang, $Output, $Verbose, $HtmlDir, |
| $file) = @_; |
|
|
| my @Args = @$OriginalArgs; |
| my $Cmd; |
| my @CmdArgs; |
| my @CmdArgsSansAnalyses; |
|
|
| if ($Lang =~ /header/) { |
| exit 0 if (!defined ($Output)); |
| $Cmd = 'cp'; |
| push @CmdArgs, $file; |
| |
| $Output =~ s/[.]gch$//; |
| push @CmdArgs, $Output; |
| @CmdArgsSansAnalyses = @CmdArgs; |
| } |
| else { |
| $Cmd = $Clang; |
|
|
| |
| my $SyntaxArgs = GetCCArgs($HtmlDir, "-fsyntax-only", \@Args); |
| @CmdArgsSansAnalyses = @$SyntaxArgs; |
|
|
| |
| if (defined $ResultFile) { |
| push @Args, '-o', $ResultFile; |
| } |
| elsif (defined $HtmlDir) { |
| push @Args, '-o', $HtmlDir; |
| } |
| if ($Verbose) { |
| push @Args, "-Xclang", "-analyzer-display-progress"; |
| } |
|
|
| foreach my $arg (@$AnalyzeArgs) { |
| push @Args, "-Xclang", $arg; |
| } |
|
|
| if (defined $AnalyzerTarget) { |
| push @Args, "-target", $AnalyzerTarget; |
| } |
|
|
| my $AnalysisArgs = GetCCArgs($HtmlDir, "--analyze", \@Args); |
| @CmdArgs = @$AnalysisArgs; |
| } |
|
|
| my @PrintArgs; |
| my $dir; |
|
|
| if ($Verbose) { |
| $dir = getcwd(); |
| print STDERR "\n[LOCATION]: $dir\n"; |
| push @PrintArgs,"'$Cmd'"; |
| foreach my $arg (@CmdArgs) { |
| push @PrintArgs,"\'$arg\'"; |
| } |
| } |
| if ($Verbose == 1) { |
| |
| |
| print STDERR join(' ', @PrintArgs); |
| print STDERR "\n"; |
| } |
| elsif ($Verbose == 2) { |
| print STDERR "#SHELL (cd '$dir' && @PrintArgs)\n"; |
| } |
|
|
| |
| |
| |
| |
| my ($ofh, $ofile) = tempfile("clang_output_XXXXXX", DIR => $HtmlDir); |
|
|
| my $OutputStream = silent_system($HtmlDir, $Cmd, @CmdArgs); |
| while ( <$OutputStream> ) { |
| print $ofh $_; |
| print STDERR $_; |
| } |
| my $Result = $?; |
| close $ofh; |
|
|
| |
| if ($ReportFailures) { |
| if ($Result & 127 and $Cmd eq $Clang and defined $HtmlDir) { |
| ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses, |
| $HtmlDir, "Crash", $ofile); |
| } |
| elsif ($Result) { |
| if ($IncludeParserRejects && !($file =~/conftest/)) { |
| ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses, |
| $HtmlDir, $ParserRejects, $ofile); |
| } else { |
| ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses, |
| $HtmlDir, $OtherError, $ofile); |
| } |
| } |
| else { |
| |
| if (open(CHILD, $ofile)) { |
| my %attributes_not_handled; |
|
|
| |
| |
| $attributes_not_handled{"cdecl"} = 1; |
|
|
| my $ppfile; |
| while (<CHILD>) { |
| next if (! /warning: '([^\']+)' attribute ignored/); |
|
|
| |
| next if (defined $attributes_not_handled{$1}); |
| $attributes_not_handled{$1} = 1; |
|
|
| |
| my $dir = "$HtmlDir/failures"; |
| my $afile = "$dir/attribute_ignored_$1.txt"; |
|
|
| |
| |
| next if (-e $afile); |
|
|
| |
| |
| if (!(defined $ppfile)) { |
| $ppfile = ProcessClangFailure($Clang, $Lang, $file, |
| \@CmdArgsSansAnalyses, |
| $HtmlDir, $AttributeIgnored, $ofile); |
| } |
|
|
| mkpath $dir; |
| open(AFILE, ">$afile"); |
| print AFILE "$ppfile\n"; |
| close(AFILE); |
| } |
| close CHILD; |
| } |
| } |
| } |
|
|
| unlink($ofile); |
| } |
|
|
| |
| |
| |
|
|
| my %CompileOptionMap = ( |
| '-nostdinc' => 0, |
| '-nostdlibinc' => 0, |
| '-include' => 1, |
| '-idirafter' => 1, |
| '-imacros' => 1, |
| '-iprefix' => 1, |
| '-iquote' => 1, |
| '-iwithprefix' => 1, |
| '-iwithprefixbefore' => 1 |
| ); |
|
|
| my %LinkerOptionMap = ( |
| '-framework' => 1, |
| '-fobjc-link-runtime' => 0 |
| ); |
|
|
| my %CompilerLinkerOptionMap = ( |
| '-Wwrite-strings' => 0, |
| '-ftrapv-handler' => 1, |
| '-mios-simulator-version-min' => 0, |
| '-isysroot' => 1, |
| '-arch' => 1, |
| '-m32' => 0, |
| '-m64' => 0, |
| '-stdlib' => 0, |
| '--sysroot' => 1, |
| '-target' => 1, |
| '-v' => 0, |
| '-mmacosx-version-min' => 0, |
| '-mmacos-version-min' => 0, |
| '-miphoneos-version-min' => 0, |
| '--target' => 0 |
| ); |
|
|
| my %IgnoredOptionMap = ( |
| '-MT' => 1, |
| '-MF' => 1, |
|
|
| '-fsyntax-only' => 0, |
| '-save-temps' => 0, |
| '-install_name' => 1, |
| '-exported_symbols_list' => 1, |
| '-current_version' => 1, |
| '-compatibility_version' => 1, |
| '-init' => 1, |
| '-e' => 1, |
| '-seg1addr' => 1, |
| '-bundle_loader' => 1, |
| '-multiply_defined' => 1, |
| '-sectorder' => 3, |
| '--param' => 1, |
| '-u' => 1, |
| '--serialize-diagnostics' => 1 |
| ); |
|
|
| my %LangMap = ( |
| 'c' => $IsCXX ? 'c++' : 'c', |
| 'cp' => 'c++', |
| 'cpp' => 'c++', |
| 'cxx' => 'c++', |
| 'txx' => 'c++', |
| 'cc' => 'c++', |
| 'C' => 'c++', |
| 'ii' => 'c++-cpp-output', |
| 'i' => $IsCXX ? 'c++-cpp-output' : 'cpp-output', |
| 'm' => 'objective-c', |
| 'mi' => 'objective-c-cpp-output', |
| 'mm' => 'objective-c++', |
| 'mii' => 'objective-c++-cpp-output', |
| ); |
|
|
| my %UniqueOptions = ( |
| '-isysroot' => 0 |
| ); |
|
|
| |
| |
| |
|
|
| my %LangsAccepted = ( |
| "objective-c" => 1, |
| "c" => 1, |
| "c++" => 1, |
| "objective-c++" => 1, |
| "cpp-output" => 1, |
| "objective-c-cpp-output" => 1, |
| "c++-cpp-output" => 1 |
| ); |
|
|
| |
| |
| |
|
|
| my $Action = 'link'; |
| my @CompileOpts; |
| my @LinkOpts; |
| my @Files; |
| my $Lang; |
| my $Output; |
| my %Uniqued; |
|
|
| |
| my $Status = system($Compiler,@ARGV); |
| if (defined $ENV{'CCC_ANALYZER_LOG'}) { |
| print STDERR "$Compiler @ARGV\n"; |
| } |
| if ($Status) { exit($Status >> 8); } |
|
|
| |
| my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'}; |
|
|
| |
| my $Plugins = $ENV{'CCC_ANALYZER_PLUGINS'}; |
|
|
| |
| my $ConstraintsModel = $ENV{'CCC_ANALYZER_CONSTRAINTS_MODEL'}; |
|
|
| |
| my $InternalStats = $ENV{'CCC_ANALYZER_INTERNAL_STATS'}; |
|
|
| |
| my $OutputFormat = $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'}; |
| if (!defined $OutputFormat) { $OutputFormat = "html"; } |
|
|
| |
| my $ConfigOptions = $ENV{'CCC_ANALYZER_CONFIG'}; |
|
|
| |
| my $Verbose = 0; |
| if (defined $ENV{'CCC_ANALYZER_VERBOSE'}) { $Verbose = 1; } |
| if (defined $ENV{'CCC_ANALYZER_LOG'}) { $Verbose = 2; } |
|
|
| |
| my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'}; |
|
|
| |
| my $ForceAnalyzeDebugCode = $ENV{'CCC_ANALYZER_FORCE_ANALYZE_DEBUG_CODE'}; |
|
|
| my %DisabledArchs = ('ppc' => 1, 'ppc64' => 1); |
| my %ArchsSeen; |
| my $HadArch = 0; |
| my $HasSDK = 0; |
|
|
| |
| foreach (my $i = 0; $i < scalar(@ARGV); ++$i) { |
| my $Arg = $ARGV[$i]; |
| my @ArgParts = split /=/,$Arg,2; |
| my $ArgKey = $ArgParts[0]; |
|
|
| |
| if (!defined($ArgKey)) { |
| next; |
| } |
|
|
| |
| if ($Arg =~ /^-(E|MM?)$/) { $Action = 'preprocess'; } |
| elsif ($Arg eq '-c') { $Action = 'compile'; } |
| elsif ($Arg =~ /^-print-prog-name/) { exit 0; } |
|
|
| |
| if ($Arg eq "-arch") { |
| my $arch = $ARGV[$i+1]; |
| |
| |
| if (!(defined $DisabledArchs{$arch})) { $ArchsSeen{$arch} = 1; } |
| $HadArch = 1; |
| ++$i; |
| next; |
| } |
|
|
| |
| |
| if ($Arg =~ /^-isysroot/) { |
| $HasSDK = 1; |
| } |
|
|
| |
| if (defined $CompileOptionMap{$ArgKey}) { |
| my $Cnt = $CompileOptionMap{$ArgKey}; |
| push @CompileOpts,$Arg; |
| while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; } |
| next; |
| } |
| |
| if ($Arg =~ /^-iquote.*/) { |
| push @CompileOpts,$Arg; |
| next; |
| } |
|
|
| |
| if (defined $LinkerOptionMap{$ArgKey}) { |
| my $Cnt = $LinkerOptionMap{$ArgKey}; |
| push @LinkOpts,$Arg; |
| while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; } |
| next; |
| } |
|
|
| |
| |
| if (defined $CompilerLinkerOptionMap{$ArgKey}) { |
| my $Cnt = $CompilerLinkerOptionMap{$ArgKey}; |
|
|
| |
| |
| if ($UniqueOptions{$Arg}) { |
| if (defined $Uniqued{$Arg}) { |
| $i += $Cnt; |
| next; |
| } |
| $Uniqued{$Arg} = 1; |
| } |
|
|
| push @CompileOpts,$Arg; |
| push @LinkOpts,$Arg; |
|
|
| if (scalar @ArgParts == 1) { |
| while ($Cnt > 0) { |
| ++$i; --$Cnt; |
| push @CompileOpts, $ARGV[$i]; |
| push @LinkOpts, $ARGV[$i]; |
| } |
| } |
| next; |
| } |
|
|
| |
| if (defined $IgnoredOptionMap{$ArgKey}) { |
| my $Cnt = $IgnoredOptionMap{$ArgKey}; |
| while ($Cnt > 0) { |
| ++$i; --$Cnt; |
| } |
| next; |
| } |
|
|
| |
| if ($Arg =~ /^-(?:[DIU]|isystem)(.*)$/) { |
| my $Tmp = $Arg; |
| if ($1 eq '') { |
| |
| ++$i; |
| $Tmp = $Arg . $ARGV[$i]; |
| } |
| push @CompileOpts,$Tmp; |
| next; |
| } |
|
|
| if ($Arg =~ /^-m.*/) { |
| push @CompileOpts,$Arg; |
| next; |
| } |
|
|
| |
| if ($Arg eq '-x') { |
| $Lang = $ARGV[$i+1]; |
| ++$i; next; |
| } |
|
|
| |
| if ($Arg eq '-o') { |
| ++$i; |
| $Output = $ARGV[$i]; |
| next; |
| } |
|
|
| |
| if ($Arg =~ /^-[l,L,O]/) { |
| if ($Arg eq '-O') { push @LinkOpts,'-O1'; } |
| elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; } |
| else { push @LinkOpts,$Arg; } |
|
|
| |
| if ($Arg =~ /^-O/) { push @CompileOpts,$Arg; } |
| next; |
| } |
|
|
| if ($Arg =~ /^-std=/) { |
| push @CompileOpts,$Arg; |
| next; |
| } |
|
|
| |
| if ($Arg =~ /^-F(.+)$/) { |
| my $Tmp = $Arg; |
| if ($1 eq '') { |
| |
| ++$i; |
| $Tmp = $Arg . $ARGV[$i]; |
| } |
| push @CompileOpts,$Tmp; |
| push @LinkOpts,$Tmp; |
| next; |
| } |
|
|
| |
| if ($Arg eq '-filelist') { |
| |
| open(IN, $ARGV[$i+1]); |
| while (<IN>) { s/\015?\012//; push @Files,$_; } |
| close(IN); |
| ++$i; |
| next; |
| } |
|
|
| if ($Arg =~ /^-f/) { |
| push @CompileOpts,$Arg; |
| push @LinkOpts,$Arg; |
| next; |
| } |
|
|
| |
| |
| if ($Arg =~ /^-Wno-/) { |
| push @CompileOpts, $Arg; |
| next; |
| } |
|
|
| |
| if ($Arg =~ /^-Xclang$/) { |
| |
| ++$i; |
| push @CompileOpts, $Arg; |
| push @CompileOpts, $ARGV[$i]; |
| next; |
| } |
|
|
| if (!($Arg =~ /^-/)) { |
| push @Files, $Arg; |
| next; |
| } |
| } |
|
|
| |
| if ($ForceAnalyzeDebugCode) { |
| push @CompileOpts, '-UNDEBUG'; |
| } |
|
|
| |
| |
| |
| if (not $HasSDK and $UseXCRUN) { |
| my $sdk = `/usr/bin/xcrun --show-sdk-path -sdk macosx`; |
| chomp $sdk; |
| push @CompileOpts, "-isysroot", $sdk; |
| } |
|
|
| if ($Action eq 'compile' or $Action eq 'link') { |
| my @Archs = keys %ArchsSeen; |
| |
| exit 0 if ($HadArch && scalar(@Archs) == 0); |
|
|
| foreach my $file (@Files) { |
| |
| my $FileLang = $Lang; |
|
|
| if (!defined($FileLang)) { |
| |
| if ($file =~ /[.]([^.]+)$/) { |
| $FileLang = $LangMap{$1}; |
| } |
| } |
|
|
| |
| next if (!defined $FileLang); |
|
|
| |
| next if (!defined $LangsAccepted{$FileLang}); |
|
|
| my @CmdArgs; |
| my @AnalyzeArgs; |
|
|
| if ($FileLang ne 'unknown') { |
| push @CmdArgs, '-x', $FileLang; |
| } |
|
|
| if (defined $ConstraintsModel) { |
| push @AnalyzeArgs, "-analyzer-constraints=$ConstraintsModel"; |
| } |
|
|
| if (defined $InternalStats) { |
| push @AnalyzeArgs, "-analyzer-stats"; |
| } |
|
|
| if (defined $Analyses) { |
| push @AnalyzeArgs, split '\s+', $Analyses; |
| } |
|
|
| if (defined $Plugins) { |
| push @AnalyzeArgs, split '\s+', $Plugins; |
| } |
|
|
| if (defined $OutputFormat) { |
| push @AnalyzeArgs, "-analyzer-output=" . $OutputFormat; |
| if ($OutputFormat =~ /plist/ || $OutputFormat =~ /sarif/) { |
| |
| my $Suffix = $OutputFormat =~ /plist/ ? ".plist" : ".sarif"; |
| my ($h, $f) = tempfile("report-XXXXXX", SUFFIX => $Suffix, |
| DIR => $HtmlDir); |
| $ResultFile = $f; |
| |
| if (!defined $HtmlDir || $HtmlDir eq "") { |
| $CleanupFile = $f; |
| } |
| } |
| } |
| if (defined $ConfigOptions) { |
| push @AnalyzeArgs, split '\s+', $ConfigOptions; |
| } |
|
|
| push @CmdArgs, @CompileOpts; |
| push @CmdArgs, $file; |
|
|
| if (scalar @Archs) { |
| foreach my $arch (@Archs) { |
| my @NewArgs; |
| push @NewArgs, '-arch', $arch; |
| push @NewArgs, @CmdArgs; |
| Analyze($Clang, \@NewArgs, \@AnalyzeArgs, $FileLang, $Output, |
| $Verbose, $HtmlDir, $file); |
| } |
| } |
| else { |
| Analyze($Clang, \@CmdArgs, \@AnalyzeArgs, $FileLang, $Output, |
| $Verbose, $HtmlDir, $file); |
| } |
| } |
| } |
|
|