[clang][Tooling] Optimize addTargetAndMode in case of invalid modes

This skips searching for `target` related flags in the existing args if
we don't have a valid target to insert.

Depends on D85076

Differential Revision: https://reviews.llvm.org/D85077
This commit is contained in:
Kadir Cetinkaya 2020-08-01 19:03:40 +02:00
parent 3de9323001
commit 76c3ec814d
No known key found for this signature in database
GPG Key ID: E39E36B8D2057ED6
1 changed files with 10 additions and 9 deletions

View File

@ -258,22 +258,23 @@ void addTargetAndModeForProgramName(std::vector<std::string> &CommandLine,
// --driver-mode=X
const std::string DriverModeOPT =
Table.getOption(driver::options::OPT_driver_mode).getPrefixedName();
bool AlreadyHasTarget = false;
bool AlreadyHasMode = false;
auto TargetMode =
driver::ToolChain::getTargetAndModeFromProgramName(InvokedAs);
// No need to search for target args if we don't have a target/mode to insert.
bool ShouldAddTarget = TargetMode.TargetIsValid;
bool ShouldAddMode = TargetMode.DriverMode != nullptr;
// Skip CommandLine[0].
for (auto Token = ++CommandLine.begin(); Token != CommandLine.end();
++Token) {
StringRef TokenRef(*Token);
AlreadyHasTarget |=
TokenRef.startswith(TargetOPT) || TokenRef.equals(TargetOPTLegacy);
AlreadyHasMode |= TokenRef.startswith(DriverModeOPT);
ShouldAddTarget = ShouldAddTarget && !TokenRef.startswith(TargetOPT) &&
!TokenRef.equals(TargetOPTLegacy);
ShouldAddMode = ShouldAddMode && !TokenRef.startswith(DriverModeOPT);
}
auto TargetMode =
driver::ToolChain::getTargetAndModeFromProgramName(InvokedAs);
if (!AlreadyHasMode && TargetMode.DriverMode) {
if (ShouldAddMode) {
CommandLine.insert(++CommandLine.begin(), TargetMode.DriverMode);
}
if (!AlreadyHasTarget && TargetMode.TargetIsValid) {
if (ShouldAddTarget) {
CommandLine.insert(++CommandLine.begin(),
TargetOPT + TargetMode.TargetPrefix);
}