Clang/C++: Use qtcEnvironmentVariable* instead of qEnvironmentVariable*

And instead of qgetenv.
Takes Qt Creator's setting at "Environment > System > Environment" into
account, which makes it easier on some platforms to set them (e.g.
macOS), can be configured differently in different settings paths, and
potentially can be changed at runtime (depending on usage).

Change-Id: I7678b8e429b5eff79f87eb637f6f2131be43d904
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Eike Ziller
2022-08-24 14:53:40 +02:00
parent fe4e74af75
commit 57745407de
15 changed files with 41 additions and 30 deletions

View File

@@ -33,9 +33,10 @@
#include <texteditor/textmark.h> #include <texteditor/textmark.h>
#include <utils/temporarydirectory.h> #include <utils/environment.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/runextensions.h> #include <utils/runextensions.h>
#include <utils/temporarydirectory.h>
using namespace Utils; using namespace Utils;
@@ -174,7 +175,7 @@ void ClangCodeModelPlugin::createCompilationDBButton()
void ClangCodeModelPlugin::maybeHandleBatchFileAndExit() const void ClangCodeModelPlugin::maybeHandleBatchFileAndExit() const
{ {
#ifdef WITH_TESTS #ifdef WITH_TESTS
const QString batchFilePath = QString::fromLocal8Bit(qgetenv("QTC_CLANG_BATCH")); const QString batchFilePath = qtcEnvironmentVariable("QTC_CLANG_BATCH");
if (!batchFilePath.isEmpty() && QTC_GUARD(QFileInfo::exists(batchFilePath))) { if (!batchFilePath.isEmpty() && QTC_GUARD(QFileInfo::exists(batchFilePath))) {
const bool runSucceeded = runClangBatchFile(batchFilePath); const bool runSucceeded = runClangBatchFile(batchFilePath);
QCoreApplication::exit(!runSucceeded); QCoreApplication::exit(!runSucceeded);

View File

@@ -22,6 +22,7 @@
#include <texteditor/textdocument.h> #include <texteditor/textdocument.h>
#include <texteditor/texteditor.h> #include <texteditor/texteditor.h>
#include <utils/environment.h>
#include <utils/executeondestruction.h> #include <utils/executeondestruction.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
@@ -42,10 +43,9 @@ static Q_LOGGING_CATEGORY(debug, "qtc.clangcodemodel.batch", QtWarningMsg);
static int timeOutFromEnvironmentVariable() static int timeOutFromEnvironmentVariable()
{ {
const QByteArray timeoutAsByteArray = qgetenv("QTC_CLANG_BATCH_TIMEOUT");
bool isConversionOk = false; bool isConversionOk = false;
const int intervalAsInt = timeoutAsByteArray.toInt(&isConversionOk); const int intervalAsInt = Utils::qtcEnvironmentVariableIntValue("QTC_CLANG_BATCH_TIMEOUT",
&isConversionOk);
if (!isConversionOk) { if (!isConversionOk) {
qCDebug(debug, "Environment variable QTC_CLANG_BATCH_TIMEOUT is not set, assuming 30000."); qCDebug(debug, "Environment variable QTC_CLANG_BATCH_TIMEOUT is not set, assuming 30000.");
return 30000; return 30000;

View File

@@ -7,23 +7,24 @@
#include "../clangdclient.h" #include "../clangdclient.h"
#include "../clangmodelmanagersupport.h" #include "../clangmodelmanagersupport.h"
#include <coreplugin/editormanager/editormanager.h>
#include <cplusplus/FindUsages.h> #include <cplusplus/FindUsages.h>
#include <cppeditor/cppcodemodelsettings.h> #include <cppeditor/cppcodemodelsettings.h>
#include <cppeditor/cpptoolsreuse.h> #include <cppeditor/cpptoolsreuse.h>
#include <cppeditor/cpptoolstestcase.h> #include <cppeditor/cpptoolstestcase.h>
#include <cppeditor/semantichighlighter.h> #include <cppeditor/semantichighlighter.h>
#include <coreplugin/editormanager/editormanager.h>
#include <languageclient/languageclientmanager.h> #include <languageclient/languageclientmanager.h>
#include <projectexplorer/kitmanager.h> #include <projectexplorer/kitmanager.h>
#include <projectexplorer/project.h> #include <projectexplorer/project.h>
#include <projectexplorer/projectexplorer.h> #include <projectexplorer/projectexplorer.h>
#include <projectexplorer/target.h> #include <projectexplorer/target.h>
#include <qtsupport/qtkitinformation.h>
#include <texteditor/codeassist/assistproposaliteminterface.h> #include <texteditor/codeassist/assistproposaliteminterface.h>
#include <texteditor/codeassist/textdocumentmanipulatorinterface.h> #include <texteditor/codeassist/textdocumentmanipulatorinterface.h>
#include <utils/algorithm.h> #include <utils/algorithm.h>
#include <utils/environment.h>
#include <utils/filepath.h> #include <utils/filepath.h>
#include <utils/textutils.h> #include <utils/textutils.h>
#include <qtsupport/qtkitinformation.h>
#include <QElapsedTimer> #include <QElapsedTimer>
#include <QEventLoop> #include <QEventLoop>
@@ -113,7 +114,7 @@ void ClangdTest::waitForNewClient(bool withIndex)
void ClangdTest::initTestCase() void ClangdTest::initTestCase()
{ {
const QString clangdFromEnv = qEnvironmentVariable("QTC_CLANGD"); const QString clangdFromEnv = Utils::qtcEnvironmentVariable("QTC_CLANGD");
if (!clangdFromEnv.isEmpty()) if (!clangdFromEnv.isEmpty())
CppEditor::ClangdSettings::setClangdFilePath(Utils::FilePath::fromString(clangdFromEnv)); CppEditor::ClangdSettings::setClangdFilePath(Utils::FilePath::fromString(clangdFromEnv));
const auto clangd = CppEditor::ClangdSettings::instance().clangdFilePath(); const auto clangd = CppEditor::ClangdSettings::instance().clangdFilePath();

View File

@@ -38,6 +38,7 @@
#include <projectexplorer/toolchain.h> #include <projectexplorer/toolchain.h>
#include <utils/algorithm.h> #include <utils/algorithm.h>
#include <utils/environment.h>
#include <utils/hostosinfo.h> #include <utils/hostosinfo.h>
#include <utils/qtcprocess.h> #include <utils/qtcprocess.h>
#include <utils/stringutils.h> #include <utils/stringutils.h>
@@ -148,7 +149,8 @@ ClangToolRunWorker::ClangToolRunWorker(RunControl *runControl,
, m_fileInfos(fileInfos) , m_fileInfos(fileInfos)
, m_temporaryDir("clangtools-XXXXXX") , m_temporaryDir("clangtools-XXXXXX")
{ {
m_temporaryDir.setAutoRemove(qEnvironmentVariable("QTC_CLANG_DONT_DELETE_OUTPUT_FILES") != "1"); m_temporaryDir.setAutoRemove(qtcEnvironmentVariable("QTC_CLANG_DONT_DELETE_OUTPUT_FILES")
!= "1");
setId("ClangTidyClazyRunner"); setId("ClangTidyClazyRunner");
setSupportsReRunning(false); setSupportsReRunning(false);

View File

@@ -22,6 +22,7 @@
#include <qtsupport/qtkitinformation.h> #include <qtsupport/qtkitinformation.h>
#include <utils/environment.h>
#include <utils/executeondestruction.h> #include <utils/executeondestruction.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
@@ -166,7 +167,7 @@ void ClangToolsUnitTests::addTestRow(const QByteArray &relativeFilePath,
int ClangToolsUnitTests::getTimeout() int ClangToolsUnitTests::getTimeout()
{ {
const int t = qEnvironmentVariableIntValue("QTC_CLANGTOOLS_TEST_TIMEOUT"); const int t = qtcEnvironmentVariableIntValue("QTC_CLANGTOOLS_TEST_TIMEOUT");
return t > 0 ? t : 480000; return t > 0 ? t : 480000;
} }

View File

@@ -287,11 +287,11 @@ ClangDiagnosticConfig diagnosticConfig(const Utils::Id &diagConfigId)
return configs.configWithId(diagConfigId); return configs.configWithId(diagConfigId);
} }
static QStringList extraOptions(const char *envVar) static QStringList extraOptions(const QString &envVar)
{ {
if (!qEnvironmentVariableIsSet(envVar)) if (!qtcEnvironmentVariableIsSet(envVar))
return QStringList(); return QStringList();
QString arguments = qEnvironmentVariable(envVar); QString arguments = qtcEnvironmentVariable(envVar);
return Utils::ProcessArgs::splitArgs(arguments); return Utils::ProcessArgs::splitArgs(arguments);
} }

View File

@@ -18,6 +18,7 @@
#include <coreplugin/progressmanager/progressmanager.h> #include <coreplugin/progressmanager/progressmanager.h>
#include <cplusplus/LookupContext.h> #include <cplusplus/LookupContext.h>
#include <utils/environment.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/runextensions.h> #include <utils/runextensions.h>
#include <utils/stringutils.h> #include <utils/stringutils.h>
@@ -29,7 +30,6 @@
namespace CppEditor::Internal { namespace CppEditor::Internal {
static const bool FindErrorsIndexing = qgetenv("QTC_FIND_ERRORS_INDEXING") == "1";
static Q_LOGGING_CATEGORY(indexerLog, "qtc.cppeditor.indexer", QtWarningMsg) static Q_LOGGING_CATEGORY(indexerLog, "qtc.cppeditor.indexer", QtWarningMsg)
namespace { namespace {
@@ -228,7 +228,7 @@ void parse(QFutureInterface<void> &indexingFuture, const ParseParams params)
indexingFuture.setProgressRange(0, files.size()); indexingFuture.setProgressRange(0, files.size());
if (FindErrorsIndexing) if (BuiltinIndexingSupport::isFindErrorsIndexingActive())
indexFindErrors(indexingFuture, params); indexFindErrors(indexingFuture, params);
else else
index(indexingFuture, params); index(indexingFuture, params);
@@ -353,7 +353,7 @@ SymbolSearcher *BuiltinIndexingSupport::createSymbolSearcher(
bool BuiltinIndexingSupport::isFindErrorsIndexingActive() bool BuiltinIndexingSupport::isFindErrorsIndexingActive()
{ {
return FindErrorsIndexing; return Utils::qtcEnvironmentVariable("QTC_FIND_ERRORS_INDEXING") == "1";
} }
} // namespace CppEditor::Internal } // namespace CppEditor::Internal

View File

@@ -7,6 +7,7 @@
#include "ui_clangdiagnosticconfigswidget.h" #include "ui_clangdiagnosticconfigswidget.h"
#include "ui_clangbasechecks.h" #include "ui_clangbasechecks.h"
#include <utils/environment.h>
#include <utils/executeondestruction.h> #include <utils/executeondestruction.h>
#include <utils/stringutils.h> #include <utils/stringutils.h>
#include <utils/treemodel.h> #include <utils/treemodel.h>
@@ -220,7 +221,7 @@ static bool isValidOption(const QString &option)
static QString validateDiagnosticOptions(const QStringList &options) static QString validateDiagnosticOptions(const QStringList &options)
{ {
// This is handy for testing, allow disabling validation. // This is handy for testing, allow disabling validation.
if (qEnvironmentVariableIntValue("QTC_CLANG_NO_DIAGNOSTIC_CHECK")) if (Utils::qtcEnvironmentVariableIntValue("QTC_CLANG_NO_DIAGNOSTIC_CHECK"))
return QString(); return QString();
for (const QString &option : options) { for (const QString &option : options) {

View File

@@ -17,6 +17,7 @@
#include <utils/algorithm.h> #include <utils/algorithm.h>
#include <utils/cpplanguage_details.h> #include <utils/cpplanguage_details.h>
#include <utils/environment.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/stringutils.h> #include <utils/stringutils.h>
@@ -378,7 +379,7 @@ void CompilerOptionsBuilder::addPrecompiledHeaderOptions(UsePrecompiledHeaders u
void CompilerOptionsBuilder::addProjectMacros() void CompilerOptionsBuilder::addProjectMacros()
{ {
static const int useMacros = qEnvironmentVariableIntValue("QTC_CLANG_USE_TOOLCHAIN_MACROS"); const int useMacros = qtcEnvironmentVariableIntValue("QTC_CLANG_USE_TOOLCHAIN_MACROS");
if (m_projectPart.toolchainType == ProjectExplorer::Constants::CUSTOM_TOOLCHAIN_TYPEID if (m_projectPart.toolchainType == ProjectExplorer::Constants::CUSTOM_TOOLCHAIN_TYPEID
|| m_projectPart.toolchainType == Qnx::Constants::QNX_TOOLCHAIN_ID || m_projectPart.toolchainType == Qnx::Constants::QNX_TOOLCHAIN_ID
@@ -772,8 +773,7 @@ void CompilerOptionsBuilder::reset()
// QMakeProject: -pipe -Whello -g -std=gnu++11 -Wall -W -D_REENTRANT -fPIC // QMakeProject: -pipe -Whello -g -std=gnu++11 -Wall -W -D_REENTRANT -fPIC
void CompilerOptionsBuilder::evaluateCompilerFlags() void CompilerOptionsBuilder::evaluateCompilerFlags()
{ {
static QStringList userBlackList = QString::fromLocal8Bit( const QStringList userBlackList = qtcEnvironmentVariable("QTC_CLANG_CMD_OPTIONS_BLACKLIST")
qgetenv("QTC_CLANG_CMD_OPTIONS_BLACKLIST"))
.split(';', Qt::SkipEmptyParts); .split(';', Qt::SkipEmptyParts);
const Id toolChain = m_projectPart.toolchainType; const Id toolChain = m_projectPart.toolchainType;

View File

@@ -493,7 +493,7 @@ int ClangdSettings::Data::defaultCompletionResults()
{ {
// Default clangd --limit-results value is 100 // Default clangd --limit-results value is 100
bool ok = false; bool ok = false;
const int userValue = qEnvironmentVariableIntValue("QTC_CLANGD_COMPLETION_RESULTS", &ok); const int userValue = qtcEnvironmentVariableIntValue("QTC_CLANGD_COMPLETION_RESULTS", &ok);
return ok ? userValue : 100; return ok ? userValue : 100;
} }

View File

@@ -9,9 +9,10 @@
#include "cpptoolstestcase.h" #include "cpptoolstestcase.h"
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/locator/locatorfiltertest.h>
#include <coreplugin/testdatadir.h> #include <coreplugin/testdatadir.h>
#include <extensionsystem/pluginmanager.h> #include <extensionsystem/pluginmanager.h>
#include <coreplugin/locator/locatorfiltertest.h> #include <utils/environment.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <QDebug> #include <QDebug>
@@ -26,7 +27,7 @@ using namespace Utils;
namespace CppEditor::Internal { namespace CppEditor::Internal {
namespace { namespace {
const bool debug = qEnvironmentVariable("QTC_DEBUG_CPPLOCATORFILTERTESTCASE") == "1"; const bool debug = qtcEnvironmentVariable("QTC_DEBUG_CPPLOCATORFILTERTESTCASE") == "1";
QTC_DECLARE_MYTESTDATADIR("../../../tests/cpplocators/") QTC_DECLARE_MYTESTDATADIR("../../../tests/cpplocators/")

View File

@@ -44,6 +44,7 @@
#include <texteditor/textdocument.h> #include <texteditor/textdocument.h>
#include <utils/environment.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <utils/hostosinfo.h> #include <utils/hostosinfo.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
@@ -66,12 +67,12 @@
#include <sstream> #include <sstream>
#endif #endif
static const bool DumpProjectInfo = qgetenv("QTC_DUMP_PROJECT_INFO") == "1";
using namespace CPlusPlus; using namespace CPlusPlus;
using namespace ProjectExplorer; using namespace ProjectExplorer;
using namespace Utils; using namespace Utils;
static const bool DumpProjectInfo = qtcEnvironmentVariable("QTC_DUMP_PROJECT_INFO") == "1";
#ifdef QTCREATOR_WITH_DUMP_AST #ifdef QTCREATOR_WITH_DUMP_AST
#include <cxxabi.h> #include <cxxabi.h>
@@ -617,7 +618,7 @@ CppModelManager::CppModelManager()
this, &CppModelManager::onSourceFilesRefreshed); this, &CppModelManager::onSourceFilesRefreshed);
d->m_findReferences = new CppFindReferences(this); d->m_findReferences = new CppFindReferences(this);
d->m_indexerEnabled = qgetenv("QTC_NO_CODE_INDEXER") != "1"; d->m_indexerEnabled = qtcEnvironmentVariable("QTC_NO_CODE_INDEXER") != "1";
d->m_dirty = true; d->m_dirty = true;

View File

@@ -25,6 +25,7 @@
#include <texteditor/codeassist/iassistproposalmodel.h> #include <texteditor/codeassist/iassistproposalmodel.h>
#include <texteditor/storagesettings.h> #include <texteditor/storagesettings.h>
#include <utils/environment.h>
#include <utils/executeondestruction.h> #include <utils/executeondestruction.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <utils/hostosinfo.h> #include <utils/hostosinfo.h>
@@ -482,9 +483,9 @@ QString TemporaryCopiedDir::absolutePath(const QByteArray &relativePath) const
int clangdIndexingTimeout() int clangdIndexingTimeout()
{ {
const QByteArray timeoutAsByteArray = qgetenv("QTC_CLANGD_INDEXING_TIMEOUT");
bool isConversionOk = false; bool isConversionOk = false;
const int intervalAsInt = timeoutAsByteArray.toInt(&isConversionOk); const int intervalAsInt = qtcEnvironmentVariableIntValue("QTC_CLANGD_INDEXING_TIMEOUT",
&isConversionOk);
if (!isConversionOk) if (!isConversionOk)
return Utils::HostOsInfo::isWindowsHost() ? 20000 : 10000; return Utils::HostOsInfo::isWindowsHost() ? 20000 : 10000;
return intervalAsInt; return intervalAsInt;

View File

@@ -24,6 +24,7 @@
#include <cplusplus/CppDocument.h> #include <cplusplus/CppDocument.h>
#include <cplusplus/TranslationUnit.h> #include <cplusplus/TranslationUnit.h>
#include <utils/algorithm.h> #include <utils/algorithm.h>
#include <utils/environment.h>
#include <QApplication> #include <QApplication>
#include <QDebug> #include <QDebug>
@@ -123,7 +124,7 @@ TestActionsTestCase::TestActionsTestCase(const Actions &tokenActions, const Acti
{ {
QVERIFY(succeededSoFar()); QVERIFY(succeededSoFar());
if (qgetenv("QTC_TEST_WAIT_FOR_LOADED_PROJECT") != "1") if (Utils::qtcEnvironmentVariable("QTC_TEST_WAIT_FOR_LOADED_PROJECT") != "1")
QSKIP("Environment variable QTC_TEST_WAIT_FOR_LOADED_PROJECT=1 not set."); QSKIP("Environment variable QTC_TEST_WAIT_FOR_LOADED_PROJECT=1 not set.");
QVERIFY(waitUntilAProjectIsLoaded()); QVERIFY(waitUntilAProjectIsLoaded());

View File

@@ -25,6 +25,7 @@
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/idocument.h> #include <coreplugin/idocument.h>
#include <utils/environment.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <QDebug> #include <QDebug>
@@ -483,7 +484,7 @@ namespace CppEditor::Internal::Tests {
void FollowSymbolTest::initTestCase() void FollowSymbolTest::initTestCase()
{ {
const QString clangdFromEnv = qEnvironmentVariable("QTC_CLANGD"); const QString clangdFromEnv = Utils::qtcEnvironmentVariable("QTC_CLANGD");
if (clangdFromEnv.isEmpty()) if (clangdFromEnv.isEmpty())
return; return;
ClangdSettings::setClangdFilePath(Utils::FilePath::fromString(clangdFromEnv)); ClangdSettings::setClangdFilePath(Utils::FilePath::fromString(clangdFromEnv));