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