Skip to content

Commit cc5f829

Browse files
authored
[clang][scan-build] Treat --use-cc and --use-c++ as shell commands (#131932)
So that things like --use-cc="ccache gcc" work. Fixes #26594. Also use the slightly simpler shellwords instead of quotewords.
1 parent 27d9a3a commit cc5f829

File tree

1 file changed

+36
-58
lines changed

1 file changed

+36
-58
lines changed

clang/tools/scan-build/libexec/ccc-analyzer

Lines changed: 36 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -51,63 +51,40 @@ sub silent_system {
5151
# Compiler command setup.
5252
##===----------------------------------------------------------------------===##
5353

54-
# Search in the PATH if the compiler exists
55-
sub SearchInPath {
56-
my $file = shift;
57-
foreach my $dir (split (':', $ENV{PATH})) {
58-
if (-x "$dir/$file") {
59-
return 1;
60-
}
61-
}
62-
return 0;
63-
}
64-
65-
my $Compiler;
66-
my $Clang;
67-
my $DefaultCCompiler;
68-
my $DefaultCXXCompiler;
69-
my $IsCXX;
70-
my $AnalyzerTarget;
71-
72-
# If on OSX, use xcrun to determine the SDK root.
73-
my $UseXCRUN = 0;
74-
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") {
81-
$UseXCRUN = 1;
82-
}
83-
} elsif (`uname -s` =~ m/(FreeBSD|OpenBSD)/) {
84-
$DefaultCCompiler = 'cc';
85-
$DefaultCXXCompiler = 'c++';
86-
} else {
87-
$DefaultCCompiler = 'gcc';
88-
$DefaultCXXCompiler = 'g++';
89-
}
90-
91-
if ($FindBin::Script =~ /c\+\+-analyzer/) {
92-
$Compiler = $ENV{'CCC_CXX'};
93-
if (!defined $Compiler || (! -x $Compiler && ! SearchInPath($Compiler))) { $Compiler = $DefaultCXXCompiler; }
94-
95-
$Clang = $ENV{'CLANG_CXX'};
96-
if (!defined $Clang || ! -x $Clang) { $Clang = 'clang++'; }
97-
98-
$IsCXX = 1
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+
}
9975
}
100-
else {
101-
$Compiler = $ENV{'CCC_CC'};
102-
if (!defined $Compiler || (! -x $Compiler && ! SearchInPath($Compiler))) { $Compiler = $DefaultCCompiler; }
103-
104-
$Clang = $ENV{'CLANG'};
105-
if (!defined $Clang || ! -x $Clang) { $Clang = 'clang'; }
10676

107-
$IsCXX = 0
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;
10882
}
10983

110-
$AnalyzerTarget = $ENV{'CLANG_ANALYZER_TARGET'};
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'};
11188

11289
##===----------------------------------------------------------------------===##
11390
# Cleanup.
@@ -199,7 +176,7 @@ sub GetCCArgs {
199176
die "could not find clang line\n" if (!defined $line);
200177
# Strip leading and trailing whitespace characters.
201178
$line =~ s/^\s+|\s+$//g;
202-
my @items = quotewords('\s+', 0, $line);
179+
my @items = shellwords($line);
203180
my $cmd = shift @items;
204181
die "cannot find 'clang' in 'clang' command\n" if (!($cmd =~ /clang/ || basename($cmd) =~ /llvm/));
205182
# If this is the llvm-driver the internal command will look like "llvm clang ...".
@@ -462,9 +439,9 @@ my $Output;
462439
my %Uniqued;
463440

464441
# Forward arguments to gcc.
465-
my $Status = system($Compiler,@ARGV);
442+
my $Status = system($Compiler,@CompilerArgs,@ARGV);
466443
if (defined $ENV{'CCC_ANALYZER_LOG'}) {
467-
print STDERR "$Compiler @ARGV\n";
444+
print STDERR "$Compiler @CompilerArgs @ARGV\n";
468445
}
469446
if ($Status) { exit($Status >> 8); }
470447

@@ -698,8 +675,9 @@ if ($ForceAnalyzeDebugCode) {
698675

699676
# If we are on OSX and have an installation where the
700677
# default SDK is inferred by xcrun use xcrun to infer
701-
# the SDK.
702-
if (not $HasSDK and $UseXCRUN) {
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') {
703681
my $sdk = `/usr/bin/xcrun --show-sdk-path -sdk macosx`;
704682
chomp $sdk;
705683
push @CompileOpts, "-isysroot", $sdk;

0 commit comments

Comments
 (0)