From d76458a8b6a03ba3b5657c88221884c05c848467 Mon Sep 17 00:00:00 2001 From: hjk Date: Wed, 1 Jun 2022 15:19:31 +0200 Subject: [PATCH] Utils: Replace Environment::size() check by some isValid() function More descriptive, and later implementable without full expansion. Change-Id: Ic3b17ea0b07273f292827a7b63e7695b4bd1cf23 Reviewed-by: Christian Stenger --- src/libs/utils/environment.cpp | 5 +++++ src/libs/utils/environment.h | 2 +- src/libs/utils/qtcprocess.cpp | 4 ++-- src/plugins/android/androidrunnerworker.cpp | 2 +- src/plugins/autotest/testconfiguration.cpp | 4 ++-- src/plugins/clangtools/documentclangtoolrunner.cpp | 2 +- src/plugins/debugger/cdb/cdbengine.cpp | 4 ++-- src/plugins/debugger/debuggerengine.cpp | 4 ++-- src/plugins/debugger/debuggeritem.cpp | 2 +- src/plugins/docker/dockerdevice.cpp | 4 ++-- src/plugins/projectexplorer/devicesupport/sshparameters.cpp | 2 +- src/plugins/projectexplorer/msvctoolchain.cpp | 4 ++-- src/plugins/remotelinux/makeinstallstep.cpp | 2 +- src/plugins/vcsbase/vcsbaseclient.cpp | 2 +- 14 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/libs/utils/environment.cpp b/src/libs/utils/environment.cpp index 8c13585155a..3220fe4a83b 100644 --- a/src/libs/utils/environment.cpp +++ b/src/libs/utils/environment.cpp @@ -45,6 +45,11 @@ NameValueItems Environment::diff(const Environment &other, bool checkAppendPrepe return m_dict.diff(other.m_dict, checkAppendPrepend); } +int Environment::isValid() const +{ + return m_dict.size() != 0; +} + QProcessEnvironment Environment::toProcessEnvironment() const { QProcessEnvironment result; diff --git a/src/libs/utils/environment.h b/src/libs/utils/environment.h index 6429ba90536..13fab9605f2 100644 --- a/src/libs/utils/environment.h +++ b/src/libs/utils/environment.h @@ -57,7 +57,7 @@ public: void unset(const QString &key) { m_dict.unset(key); } 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(); } QStringList toStringList() const { return m_dict.toStringList(); } diff --git a/src/libs/utils/qtcprocess.cpp b/src/libs/utils/qtcprocess.cpp index c4910ddec04..10340eb0efe 100644 --- a/src/libs/utils/qtcprocess.cpp +++ b/src/libs/utils/qtcprocess.cpp @@ -611,7 +611,7 @@ public: Environment fullEnvironment() const { Environment env = m_setup.m_environment; - if (env.size() == 0) { + if (!env.isValid()) { // FIXME: Either switch to using EnvironmentChange instead of full Environments, or // feed the full environment into the QtcProcess instead of fixing it up here. // qWarning("QtcProcess::start: Empty environment set when running '%s'.", @@ -1113,7 +1113,7 @@ QString QtcProcess::toStandaloneCommandLine() const d->m_setup.m_workingDirectory.path(); } 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(); std::transform(envVars.cbegin(), envVars.cend(), std::back_inserter(parts), ProcessArgs::quoteArgUnix); diff --git a/src/plugins/android/androidrunnerworker.cpp b/src/plugins/android/androidrunnerworker.cpp index a4e5a3c61f9..4734ec50244 100644 --- a/src/plugins/android/androidrunnerworker.cpp +++ b/src/plugins/android/androidrunnerworker.cpp @@ -645,7 +645,7 @@ void AndroidRunnerWorker::asyncStartHelper() << QString::fromLatin1(appArgs.join(' ').toUtf8().toBase64()); } - if (m_extraEnvVars.size() > 0) { + if (m_extraEnvVars.isValid()) { args << "-e" << "extraenvvars" << QString::fromLatin1(m_extraEnvVars.toStringList().join('\t') .toUtf8().toBase64()); diff --git a/src/plugins/autotest/testconfiguration.cpp b/src/plugins/autotest/testconfiguration.cpp index 4599fc7606b..e00c2bf2fcf 100644 --- a/src/plugins/autotest/testconfiguration.cpp +++ b/src/plugins/autotest/testconfiguration.cpp @@ -83,8 +83,8 @@ FilePath ITestConfiguration::executableFilePath() const if (!hasExecutable()) return {}; - const Environment env = m_runnable.environment.size() == 0 ? Environment::systemEnvironment() - : m_runnable.environment; + const Environment env = m_runnable.environment.isValid() + ? m_runnable.environment : Environment::systemEnvironment(); return env.searchInPath(m_runnable.command.executable().path()); } diff --git a/src/plugins/clangtools/documentclangtoolrunner.cpp b/src/plugins/clangtools/documentclangtoolrunner.cpp index b0794676d0a..28a4d0df29b 100644 --- a/src/plugins/clangtools/documentclangtoolrunner.cpp +++ b/src/plugins/clangtools/documentclangtoolrunner.cpp @@ -183,7 +183,7 @@ static Environment projectBuildEnvironment(Project *project) if (BuildConfiguration *buildConfig = target->activeBuildConfiguration()) env = buildConfig->environment(); } - if (env.size() == 0) + if (!env.isValid()) env = Environment::systemEnvironment(); return env; } diff --git a/src/plugins/debugger/cdb/cdbengine.cpp b/src/plugins/debugger/cdb/cdbengine.cpp index e2da844b0e8..99f9b14e7fd 100644 --- a/src/plugins/debugger/cdb/cdbengine.cpp +++ b/src/plugins/debugger/cdb/cdbengine.cpp @@ -438,8 +438,8 @@ void CdbEngine::setupEngine() m_outputBuffer.clear(); m_autoBreakPointCorrection = false; - Utils::Environment inferiorEnvironment = sp.inferior.environment.size() == 0 - ? Utils::Environment::systemEnvironment() : sp.inferior.environment; + Environment inferiorEnvironment = sp.inferior.environment.isValid() + ? sp.inferior.environment : Environment::systemEnvironment(); // Make sure that QTestLib uses OutputDebugString for logging. const QString qtLoggingToConsoleKey = QStringLiteral("QT_LOGGING_TO_CONSOLE"); diff --git a/src/plugins/debugger/debuggerengine.cpp b/src/plugins/debugger/debuggerengine.cpp index 25ffb16122e..e95ee836eae 100644 --- a/src/plugins/debugger/debuggerengine.cpp +++ b/src/plugins/debugger/debuggerengine.cpp @@ -123,8 +123,8 @@ QDebug operator<<(QDebug str, const DebuggerRunParameters &sp) nospace << "executable=" << sp.inferior.command.executable() << " coreFile=" << sp.coreFile << " processArgs=" << sp.inferior.command.arguments() - << " inferior environment=<" << sp.inferior.environment.size() << " variables>" - << " debugger environment=<" << sp.debugger.environment.size() << " variables>" + << " inferior environment=<" << sp.inferior.environment.toStringList().size() << " variables>" + << " debugger environment=<" << sp.debugger.environment.toStringList().size() << " variables>" << " workingDir=" << sp.inferior.workingDirectory << " attachPID=" << sp.attachPID.pid() << " remoteChannel=" << sp.remoteChannel diff --git a/src/plugins/debugger/debuggeritem.cpp b/src/plugins/debugger/debuggeritem.cpp index 973a436cc6c..c8f6bdcb7ae 100644 --- a/src/plugins/debugger/debuggeritem.cpp +++ b/src/plugins/debugger/debuggeritem.cpp @@ -165,7 +165,7 @@ void DebuggerItem::reinitializeFromFile(const Environment &sysEnv, QString *erro 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 // python but does not contain a python dll. const bool isAndroidNdkLldb = DebuggerItem::addAndroidLldbPythonEnv(m_command, env); diff --git a/src/plugins/docker/dockerdevice.cpp b/src/plugins/docker/dockerdevice.cpp index 952005a99d9..eed68feafc0 100644 --- a/src/plugins/docker/dockerdevice.cpp +++ b/src/plugins/docker/dockerdevice.cpp @@ -1005,10 +1005,10 @@ bool DockerDevice::writeFileContents(const FilePath &filePath, const QByteArray Environment DockerDevice::systemEnvironment() const { - if (d->m_cachedEnviroment.size() == 0) + if (!d->m_cachedEnviroment.isValid()) d->fetchSystemEnviroment(); - QTC_CHECK(d->m_cachedEnviroment.size() != 0); + QTC_CHECK(d->m_cachedEnviroment.isValid()); return d->m_cachedEnviroment; } diff --git a/src/plugins/projectexplorer/devicesupport/sshparameters.cpp b/src/plugins/projectexplorer/devicesupport/sshparameters.cpp index 224a4c6595d..90d650c1016 100644 --- a/src/plugins/projectexplorer/devicesupport/sshparameters.cpp +++ b/src/plugins/projectexplorer/devicesupport/sshparameters.cpp @@ -86,7 +86,7 @@ QStringList SshParameters::connectionOptions(const FilePath &binary) const bool SshParameters::setupSshEnvironment(QtcProcess *process) { Environment env = process->controlEnvironment(); - if (env.size() == 0) + if (!env.isValid()) env = Environment::systemEnvironment(); const bool hasDisplay = env.hasKey("DISPLAY") && (env.value("DISPLAY") != QString(":0")); if (SshSettings::askpassFilePath().exists()) { diff --git a/src/plugins/projectexplorer/msvctoolchain.cpp b/src/plugins/projectexplorer/msvctoolchain.cpp index bfafaa83f65..d6eaa3bbaf7 100644 --- a/src/plugins/projectexplorer/msvctoolchain.cpp +++ b/src/plugins/projectexplorer/msvctoolchain.cpp @@ -1145,7 +1145,7 @@ ToolChain::BuiltInHeaderPathsRunner MsvcToolChain::createBuiltInHeaderPathsRunne void MsvcToolChain::addToEnvironment(Utils::Environment &env) const { // 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(); m_lastEnvironment = env; m_resultEnvironment = readEnvironmentSetting(env); @@ -2121,7 +2121,7 @@ Utils::optional MsvcToolChain::generateEnvironmentSettings(const Utils: // Windows SDK setup scripts require command line switches for environment expansion. CommandLine cmd(cmdPath, {"/E:ON", "/V:ON", "/c", saver.filePath().toUserOutput()}); qCDebug(Log) << "readEnvironmentSetting: " << call << cmd.toUserOutput() - << " Env: " << runEnv.size(); + << " Env: " << runEnv.toStringList().size(); run.setCodec(QTextCodec::codecForName("UTF-8")); run.setCommand(cmd); run.runBlocking(); diff --git a/src/plugins/remotelinux/makeinstallstep.cpp b/src/plugins/remotelinux/makeinstallstep.cpp index 08a14ed7dab..a6046382492 100644 --- a/src/plugins/remotelinux/makeinstallstep.cpp +++ b/src/plugins/remotelinux/makeinstallstep.cpp @@ -168,7 +168,7 @@ bool MakeInstallStep::init() "Consider moving it up."))); } const MakeInstallCommand cmd = target()->makeInstallCommand(installRoot().toString()); - if (cmd.environment.size() > 0) { + if (cmd.environment.isValid()) { Environment env = processParameters()->environment(); for (auto it = cmd.environment.constBegin(); it != cmd.environment.constEnd(); ++it) { if (cmd.environment.isEnabled(it)) { diff --git a/src/plugins/vcsbase/vcsbaseclient.cpp b/src/plugins/vcsbase/vcsbaseclient.cpp index 3c79eee4a81..35e61845028 100644 --- a/src/plugins/vcsbase/vcsbaseclient.cpp +++ b/src/plugins/vcsbase/vcsbaseclient.cpp @@ -215,7 +215,7 @@ void VcsBaseClientImpl::vcsSynchronousExec(QtcProcess &proc, QTextCodec *outputCodec) const { Environment env = processEnvironment(); - VcsCommand command(workingDir, env.size() == 0 ? Environment::systemEnvironment() : env); + VcsCommand command(workingDir, env.isValid() ? env : Environment::systemEnvironment()); proc.setTimeoutS(vcsTimeoutS()); command.addFlags(flags); command.setCodec(outputCodec);