Utils: Replace Environment::size() check by some isValid() function

More descriptive, and later implementable without full expansion.

Change-Id: Ic3b17ea0b07273f292827a7b63e7695b4bd1cf23
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
hjk
2022-06-01 15:19:31 +02:00
parent b72f41c7f0
commit d76458a8b6
14 changed files with 24 additions and 19 deletions

View File

@@ -45,6 +45,11 @@ NameValueItems Environment::diff(const Environment &other, bool checkAppendPrepe
return m_dict.diff(other.m_dict, checkAppendPrepend); return m_dict.diff(other.m_dict, checkAppendPrepend);
} }
int Environment::isValid() const
{
return m_dict.size() != 0;
}
QProcessEnvironment Environment::toProcessEnvironment() const QProcessEnvironment Environment::toProcessEnvironment() const
{ {
QProcessEnvironment result; QProcessEnvironment result;

View File

@@ -57,7 +57,7 @@ public:
void unset(const QString &key) { m_dict.unset(key); } void unset(const QString &key) { m_dict.unset(key); }
void modify(const NameValueItems &items) { m_dict.modify(items); } void modify(const NameValueItems &items) { m_dict.modify(items); }
int size() const { return m_dict.size(); } int isValid() const;
void clear() { return m_dict.clear(); } void clear() { return m_dict.clear(); }
QStringList toStringList() const { return m_dict.toStringList(); } QStringList toStringList() const { return m_dict.toStringList(); }

View File

@@ -611,7 +611,7 @@ public:
Environment fullEnvironment() const Environment fullEnvironment() const
{ {
Environment env = m_setup.m_environment; Environment env = m_setup.m_environment;
if (env.size() == 0) { if (!env.isValid()) {
// FIXME: Either switch to using EnvironmentChange instead of full Environments, or // FIXME: Either switch to using EnvironmentChange instead of full Environments, or
// feed the full environment into the QtcProcess instead of fixing it up here. // feed the full environment into the QtcProcess instead of fixing it up here.
// qWarning("QtcProcess::start: Empty environment set when running '%s'.", // qWarning("QtcProcess::start: Empty environment set when running '%s'.",
@@ -1113,7 +1113,7 @@ QString QtcProcess::toStandaloneCommandLine() const
d->m_setup.m_workingDirectory.path(); d->m_setup.m_workingDirectory.path();
} }
parts.append("-i"); parts.append("-i");
if (d->m_setup.m_environment.size() > 0) { if (d->m_setup.m_environment.isValid()) {
const QStringList envVars = d->m_setup.m_environment.toStringList(); const QStringList envVars = d->m_setup.m_environment.toStringList();
std::transform(envVars.cbegin(), envVars.cend(), std::transform(envVars.cbegin(), envVars.cend(),
std::back_inserter(parts), ProcessArgs::quoteArgUnix); std::back_inserter(parts), ProcessArgs::quoteArgUnix);

View File

@@ -645,7 +645,7 @@ void AndroidRunnerWorker::asyncStartHelper()
<< QString::fromLatin1(appArgs.join(' ').toUtf8().toBase64()); << QString::fromLatin1(appArgs.join(' ').toUtf8().toBase64());
} }
if (m_extraEnvVars.size() > 0) { if (m_extraEnvVars.isValid()) {
args << "-e" << "extraenvvars" args << "-e" << "extraenvvars"
<< QString::fromLatin1(m_extraEnvVars.toStringList().join('\t') << QString::fromLatin1(m_extraEnvVars.toStringList().join('\t')
.toUtf8().toBase64()); .toUtf8().toBase64());

View File

@@ -83,8 +83,8 @@ FilePath ITestConfiguration::executableFilePath() const
if (!hasExecutable()) if (!hasExecutable())
return {}; return {};
const Environment env = m_runnable.environment.size() == 0 ? Environment::systemEnvironment() const Environment env = m_runnable.environment.isValid()
: m_runnable.environment; ? m_runnable.environment : Environment::systemEnvironment();
return env.searchInPath(m_runnable.command.executable().path()); return env.searchInPath(m_runnable.command.executable().path());
} }

View File

@@ -183,7 +183,7 @@ static Environment projectBuildEnvironment(Project *project)
if (BuildConfiguration *buildConfig = target->activeBuildConfiguration()) if (BuildConfiguration *buildConfig = target->activeBuildConfiguration())
env = buildConfig->environment(); env = buildConfig->environment();
} }
if (env.size() == 0) if (!env.isValid())
env = Environment::systemEnvironment(); env = Environment::systemEnvironment();
return env; return env;
} }

View File

@@ -438,8 +438,8 @@ void CdbEngine::setupEngine()
m_outputBuffer.clear(); m_outputBuffer.clear();
m_autoBreakPointCorrection = false; m_autoBreakPointCorrection = false;
Utils::Environment inferiorEnvironment = sp.inferior.environment.size() == 0 Environment inferiorEnvironment = sp.inferior.environment.isValid()
? Utils::Environment::systemEnvironment() : sp.inferior.environment; ? sp.inferior.environment : Environment::systemEnvironment();
// Make sure that QTestLib uses OutputDebugString for logging. // Make sure that QTestLib uses OutputDebugString for logging.
const QString qtLoggingToConsoleKey = QStringLiteral("QT_LOGGING_TO_CONSOLE"); const QString qtLoggingToConsoleKey = QStringLiteral("QT_LOGGING_TO_CONSOLE");

View File

@@ -123,8 +123,8 @@ QDebug operator<<(QDebug str, const DebuggerRunParameters &sp)
nospace << "executable=" << sp.inferior.command.executable() nospace << "executable=" << sp.inferior.command.executable()
<< " coreFile=" << sp.coreFile << " coreFile=" << sp.coreFile
<< " processArgs=" << sp.inferior.command.arguments() << " processArgs=" << sp.inferior.command.arguments()
<< " inferior environment=<" << sp.inferior.environment.size() << " variables>" << " inferior environment=<" << sp.inferior.environment.toStringList().size() << " variables>"
<< " debugger environment=<" << sp.debugger.environment.size() << " variables>" << " debugger environment=<" << sp.debugger.environment.toStringList().size() << " variables>"
<< " workingDir=" << sp.inferior.workingDirectory << " workingDir=" << sp.inferior.workingDirectory
<< " attachPID=" << sp.attachPID.pid() << " attachPID=" << sp.attachPID.pid()
<< " remoteChannel=" << sp.remoteChannel << " remoteChannel=" << sp.remoteChannel

View File

@@ -165,7 +165,7 @@ void DebuggerItem::reinitializeFromFile(const Environment &sysEnv, QString *erro
return; return;
} }
Environment env = sysEnv.size() == 0 ? Environment::systemEnvironment() : sysEnv; Environment env = sysEnv.isValid() ? sysEnv : Environment::systemEnvironment();
// Prevent calling lldb on Windows because the lldb from the llvm package is linked against // Prevent calling lldb on Windows because the lldb from the llvm package is linked against
// python but does not contain a python dll. // python but does not contain a python dll.
const bool isAndroidNdkLldb = DebuggerItem::addAndroidLldbPythonEnv(m_command, env); const bool isAndroidNdkLldb = DebuggerItem::addAndroidLldbPythonEnv(m_command, env);

View File

@@ -1005,10 +1005,10 @@ bool DockerDevice::writeFileContents(const FilePath &filePath, const QByteArray
Environment DockerDevice::systemEnvironment() const Environment DockerDevice::systemEnvironment() const
{ {
if (d->m_cachedEnviroment.size() == 0) if (!d->m_cachedEnviroment.isValid())
d->fetchSystemEnviroment(); d->fetchSystemEnviroment();
QTC_CHECK(d->m_cachedEnviroment.size() != 0); QTC_CHECK(d->m_cachedEnviroment.isValid());
return d->m_cachedEnviroment; return d->m_cachedEnviroment;
} }

View File

@@ -86,7 +86,7 @@ QStringList SshParameters::connectionOptions(const FilePath &binary) const
bool SshParameters::setupSshEnvironment(QtcProcess *process) bool SshParameters::setupSshEnvironment(QtcProcess *process)
{ {
Environment env = process->controlEnvironment(); Environment env = process->controlEnvironment();
if (env.size() == 0) if (!env.isValid())
env = Environment::systemEnvironment(); env = Environment::systemEnvironment();
const bool hasDisplay = env.hasKey("DISPLAY") && (env.value("DISPLAY") != QString(":0")); const bool hasDisplay = env.hasKey("DISPLAY") && (env.value("DISPLAY") != QString(":0"));
if (SshSettings::askpassFilePath().exists()) { if (SshSettings::askpassFilePath().exists()) {

View File

@@ -1145,7 +1145,7 @@ ToolChain::BuiltInHeaderPathsRunner MsvcToolChain::createBuiltInHeaderPathsRunne
void MsvcToolChain::addToEnvironment(Utils::Environment &env) const void MsvcToolChain::addToEnvironment(Utils::Environment &env) const
{ {
// We cache the full environment (incoming + modifications by setup script). // We cache the full environment (incoming + modifications by setup script).
if (!m_resultEnvironment.size() || env != m_lastEnvironment) { if (m_resultEnvironment.isValid() || env != m_lastEnvironment) {
qCDebug(Log) << "addToEnvironment: " << displayName(); qCDebug(Log) << "addToEnvironment: " << displayName();
m_lastEnvironment = env; m_lastEnvironment = env;
m_resultEnvironment = readEnvironmentSetting(env); m_resultEnvironment = readEnvironmentSetting(env);
@@ -2121,7 +2121,7 @@ Utils::optional<QString> MsvcToolChain::generateEnvironmentSettings(const Utils:
// Windows SDK setup scripts require command line switches for environment expansion. // Windows SDK setup scripts require command line switches for environment expansion.
CommandLine cmd(cmdPath, {"/E:ON", "/V:ON", "/c", saver.filePath().toUserOutput()}); CommandLine cmd(cmdPath, {"/E:ON", "/V:ON", "/c", saver.filePath().toUserOutput()});
qCDebug(Log) << "readEnvironmentSetting: " << call << cmd.toUserOutput() qCDebug(Log) << "readEnvironmentSetting: " << call << cmd.toUserOutput()
<< " Env: " << runEnv.size(); << " Env: " << runEnv.toStringList().size();
run.setCodec(QTextCodec::codecForName("UTF-8")); run.setCodec(QTextCodec::codecForName("UTF-8"));
run.setCommand(cmd); run.setCommand(cmd);
run.runBlocking(); run.runBlocking();

View File

@@ -168,7 +168,7 @@ bool MakeInstallStep::init()
"Consider moving it up."))); "Consider moving it up.")));
} }
const MakeInstallCommand cmd = target()->makeInstallCommand(installRoot().toString()); const MakeInstallCommand cmd = target()->makeInstallCommand(installRoot().toString());
if (cmd.environment.size() > 0) { if (cmd.environment.isValid()) {
Environment env = processParameters()->environment(); Environment env = processParameters()->environment();
for (auto it = cmd.environment.constBegin(); it != cmd.environment.constEnd(); ++it) { for (auto it = cmd.environment.constBegin(); it != cmd.environment.constEnd(); ++it) {
if (cmd.environment.isEnabled(it)) { if (cmd.environment.isEnabled(it)) {

View File

@@ -215,7 +215,7 @@ void VcsBaseClientImpl::vcsSynchronousExec(QtcProcess &proc,
QTextCodec *outputCodec) const QTextCodec *outputCodec) const
{ {
Environment env = processEnvironment(); Environment env = processEnvironment();
VcsCommand command(workingDir, env.size() == 0 ? Environment::systemEnvironment() : env); VcsCommand command(workingDir, env.isValid() ? env : Environment::systemEnvironment());
proc.setTimeoutS(vcsTimeoutS()); proc.setTimeoutS(vcsTimeoutS());
command.addFlags(flags); command.addFlags(flags);
command.setCodec(outputCodec); command.setCodec(outputCodec);