From 35e883eea00d1144d624cc2c2f606712afa60ef3 Mon Sep 17 00:00:00 2001 From: hjk Date: Tue, 21 Oct 2014 13:19:38 +0200 Subject: Utils: MacroExpander API cosmetics. Make const-correct, add convenience function for commandline parameter expansion. Change-Id: I12c3651e4e7b8a0a9319d1dfbea676b622b1a41a Reviewed-by: Tobias Hunger --- src/libs/utils/macroexpander.cpp | 103 +++++++++++++-------- src/libs/utils/macroexpander.h | 20 ++-- .../cmakeprojectmanager/cmakerunconfiguration.cpp | 2 +- .../coreplugin/dialogs/externaltoolconfig.cpp | 3 +- src/plugins/coreplugin/externaltool.cpp | 2 +- src/plugins/projectexplorer/buildconfiguration.cpp | 4 +- .../jsonwizard/jsonwizardexpander.cpp | 2 +- .../jsonwizard/jsonwizardexpander.h | 2 +- .../localapplicationrunconfiguration.cpp | 4 +- .../projectexplorer/projectmacroexpander.cpp | 2 +- src/plugins/projectexplorer/projectmacroexpander.h | 2 +- .../qbsprojectmanager/qbsrunconfiguration.cpp | 2 +- .../desktopqmakerunconfiguration.cpp | 2 +- .../qtsupport/customexecutablerunconfiguration.cpp | 2 +- 14 files changed, 88 insertions(+), 64 deletions(-) diff --git a/src/libs/utils/macroexpander.cpp b/src/libs/utils/macroexpander.cpp index a7ec1f9b08..fb365242ab 100644 --- a/src/libs/utils/macroexpander.cpp +++ b/src/libs/utils/macroexpander.cpp @@ -29,6 +29,7 @@ ****************************************************************************/ #include "macroexpander.h" +#include "qtcprocess.h" #include "algorithm.h" @@ -45,11 +46,52 @@ const char kPathPostfix[] = ":Path"; const char kFileNamePostfix[] = ":FileName"; const char kFileBaseNamePostfix[] = ":FileBaseName"; -class MacroExpanderPrivate +class MacroExpanderPrivate : public AbstractMacroExpander { public: MacroExpanderPrivate() : m_accumulating(false) {} + bool resolveMacro(const QString &name, QString *ret) + { + bool found; + *ret = value(name.toUtf8(), &found); + if (found) + return true; + + found = Utils::anyOf(m_subProviders, [name, ret] (const MacroExpanderProvider &p) -> bool { + MacroExpander *expander = p ? p() : 0; + return expander && expander->resolveMacro(name, ret); + }); + + if (found) + return true; + + return this == globalMacroExpander()->d ? false : globalMacroExpander()->d->resolveMacro(name, ret); + } + + QString value(const QByteArray &variable, bool *found) + { + MacroExpander::StringFunction sf = m_map.value(variable); + if (sf) { + if (found) + *found = true; + return sf(); + } + + for (auto it = m_prefixMap.constBegin(); it != m_prefixMap.constEnd(); ++it) { + if (variable.startsWith(it.key())) { + MacroExpander::PrefixFunction pf = it.value(); + if (found) + *found = true; + return pf(QString::fromUtf8(variable.mid(it.key().count()))); + } + } + if (found) + *found = false; + + return QString(); + } + QHash m_map; QHash m_prefixMap; QMap m_descriptions; @@ -187,49 +229,18 @@ MacroExpander::~MacroExpander() /*! * \internal */ -bool MacroExpander::resolveMacro(const QString &name, QString *ret) +bool MacroExpander::resolveMacro(const QString &name, QString *ret) const { - bool found; - *ret = value(name.toUtf8(), &found); - if (found) - return true; - - found = Utils::anyOf(d->m_subProviders, [name, ret] (const MacroExpanderProvider &p) -> bool { - MacroExpander *expander = p ? p() : 0; - return expander && expander->resolveMacro(name, ret); - }); - - if (found) - return true; - - return this == globalMacroExpander() ? false : globalMacroExpander()->resolveMacro(name, ret); + return d->resolveMacro(name, ret); } /*! * Returns the value of the given \a variable. If \a found is given, it is * set to true if the variable has a value at all, false if not. */ -QString MacroExpander::value(const QByteArray &variable, bool *found) +QString MacroExpander::value(const QByteArray &variable, bool *found) const { - StringFunction sf = d->m_map.value(variable); - if (sf) { - if (found) - *found = true; - return sf(); - } - - for (auto it = d->m_prefixMap.constBegin(); it != d->m_prefixMap.constEnd(); ++it) { - if (variable.startsWith(it.key())) { - PrefixFunction pf = it.value(); - if (found) - *found = true; - return pf(QString::fromUtf8(variable.mid(it.key().count()))); - } - } - if (found) - *found = false; - - return QString(); + return d->value(variable, found); } /*! @@ -239,18 +250,23 @@ QString MacroExpander::value(const QByteArray &variable, bool *found) * \sa MacroExpander * \sa macroExpander() */ -QString MacroExpander::expand(const QString &stringWithVariables) +QString MacroExpander::expand(const QString &stringWithVariables) const { QString res = stringWithVariables; - Utils::expandMacros(&res, this); + Utils::expandMacros(&res, d); return res; } -QByteArray MacroExpander::expand(const QByteArray &stringWithVariables) +QByteArray MacroExpander::expand(const QByteArray &stringWithVariables) const { return expand(QString::fromLatin1(stringWithVariables)).toLatin1(); } +QString MacroExpander::expandProcessArgs(const QString &argsWithVariables) const +{ + return QtcProcess::expandMacros(argsWithVariables, d); +} + /*! * Makes the given string-valued \a prefix known to the variable manager, * together with a localized \a description. @@ -333,7 +349,7 @@ void MacroExpander::registerFileVariables(const QByteArray &prefix, * \sa registerVariable() * \sa registerFileVariables() */ -QList MacroExpander::variables() +QList MacroExpander::variables() const { return d->m_descriptions.keys(); } @@ -341,7 +357,7 @@ QList MacroExpander::variables() /*! * Returns the description that was registered for the \a variable. */ -QString MacroExpander::variableDescription(const QByteArray &variable) +QString MacroExpander::variableDescription(const QByteArray &variable) const { return d->m_descriptions.value(variable); } @@ -357,6 +373,11 @@ MacroExpanders MacroExpander::subExpanders() const return expanders; } +AbstractMacroExpander *MacroExpander::abstractExpander() const +{ + return d; +} + QString MacroExpander::displayName() const { return d->m_displayName; diff --git a/src/libs/utils/macroexpander.h b/src/libs/utils/macroexpander.h index c11cb92dbd..5e8dec9e01 100644 --- a/src/libs/utils/macroexpander.h +++ b/src/libs/utils/macroexpander.h @@ -47,20 +47,22 @@ class MacroExpander; typedef std::function MacroExpanderProvider; typedef QVector MacroExpanders; -class QTCREATOR_UTILS_EXPORT MacroExpander : public AbstractMacroExpander +class QTCREATOR_UTILS_EXPORT MacroExpander { Q_DECLARE_TR_FUNCTIONS("MacroExpander") public: explicit MacroExpander(); - ~MacroExpander(); + virtual ~MacroExpander(); - bool resolveMacro(const QString &name, QString *ret); + virtual bool resolveMacro(const QString &name, QString *ret) const; - QString value(const QByteArray &variable, bool *found = 0); + QString value(const QByteArray &variable, bool *found = 0) const; - QString expand(const QString &stringWithVariables); - QByteArray expand(const QByteArray &stringWithVariables); + QString expand(const QString &stringWithVariables) const; + QByteArray expand(const QByteArray &stringWithVariables) const; + + QString expandProcessArgs(const QString &argsWithVariables) const; typedef std::function PrefixFunction; typedef std::function StringFunction; @@ -78,10 +80,11 @@ public: void registerFileVariables(const QByteArray &prefix, const QString &heading, const StringFunction &value); - QList variables(); - QString variableDescription(const QByteArray &variable); + QList variables() const; + QString variableDescription(const QByteArray &variable) const; MacroExpanders subExpanders() const; + AbstractMacroExpander *abstractExpander() const; QString displayName() const; void setDisplayName(const QString &displayName); @@ -95,6 +98,7 @@ private: MacroExpander(const MacroExpander &) Q_DECL_EQ_DELETE; void operator=(const MacroExpander &) Q_DECL_EQ_DELETE; + friend class Internal::MacroExpanderPrivate; Internal::MacroExpanderPrivate *d; }; diff --git a/src/plugins/cmakeprojectmanager/cmakerunconfiguration.cpp b/src/plugins/cmakeprojectmanager/cmakerunconfiguration.cpp index 4a52fc4113..64fed2f3d1 100644 --- a/src/plugins/cmakeprojectmanager/cmakerunconfiguration.cpp +++ b/src/plugins/cmakeprojectmanager/cmakerunconfiguration.cpp @@ -135,7 +135,7 @@ QString CMakeRunConfiguration::baseWorkingDirectory() const QString CMakeRunConfiguration::commandLineArguments() const { - return Utils::QtcProcess::expandMacros(m_arguments, macroExpander()); + return macroExpander()->expandProcessArgs(m_arguments); } QString CMakeRunConfiguration::title() const diff --git a/src/plugins/coreplugin/dialogs/externaltoolconfig.cpp b/src/plugins/coreplugin/dialogs/externaltoolconfig.cpp index d0eb4268da..35ba33a297 100644 --- a/src/plugins/coreplugin/dialogs/externaltoolconfig.cpp +++ b/src/plugins/coreplugin/dialogs/externaltoolconfig.cpp @@ -594,6 +594,5 @@ void ExternalToolConfig::addCategory() void ExternalToolConfig::updateEffectiveArguments() { - ui->arguments->setToolTip(Utils::QtcProcess::expandMacros(ui->arguments->text(), - Utils::globalMacroExpander())); + ui->arguments->setToolTip(Utils::globalMacroExpander()->expandProcessArgs(ui->arguments->text())); } diff --git a/src/plugins/coreplugin/externaltool.cpp b/src/plugins/coreplugin/externaltool.cpp index 0423bc75ab..63acf809d0 100644 --- a/src/plugins/coreplugin/externaltool.cpp +++ b/src/plugins/coreplugin/externaltool.cpp @@ -576,7 +576,7 @@ bool ExternalToolRunner::resolve() } } - m_resolvedArguments = QtcProcess::expandMacros(m_tool->arguments(), expander); + m_resolvedArguments = expander->expandProcessArgs(m_tool->arguments()); m_resolvedInput = expander->expand(m_tool->input()); m_resolvedWorkingDirectory = expander->expand(m_tool->workingDirectory()); diff --git a/src/plugins/projectexplorer/buildconfiguration.cpp b/src/plugins/projectexplorer/buildconfiguration.cpp index 28ebe83c90..bc339d9854 100644 --- a/src/plugins/projectexplorer/buildconfiguration.cpp +++ b/src/plugins/projectexplorer/buildconfiguration.cpp @@ -67,12 +67,12 @@ public: bc->displayName()), m_bc(bc) {} - virtual bool resolveMacro(const QString &name, QString *ret); + virtual bool resolveMacro(const QString &name, QString *ret) const; private: const BuildConfiguration *m_bc; }; -bool BuildConfigMacroExpander::resolveMacro(const QString &name, QString *ret) +bool BuildConfigMacroExpander::resolveMacro(const QString &name, QString *ret) const { // legacy variables if (name == QLatin1String("sourceDir")) { diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardexpander.cpp b/src/plugins/projectexplorer/jsonwizard/jsonwizardexpander.cpp index 09493a0bb1..fcbe68b2a4 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizardexpander.cpp +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardexpander.cpp @@ -50,7 +50,7 @@ JsonWizardExpander::JsonWizardExpander(JsonWizard *wizard) : QTC_CHECK(m_wizard); } -bool JsonWizardExpander::resolveMacro(const QString &name, QString *ret) +bool JsonWizardExpander::resolveMacro(const QString &name, QString *ret) const { QVariant v = m_wizard->value(name); if (v.isValid()) { diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardexpander.h b/src/plugins/projectexplorer/jsonwizard/jsonwizardexpander.h index 52b36b6fdf..1a84643354 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizardexpander.h +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardexpander.h @@ -45,7 +45,7 @@ class JsonWizardExpander : public Utils::MacroExpander public: explicit JsonWizardExpander(JsonWizard *wizard); - bool resolveMacro(const QString &name, QString *ret); + bool resolveMacro(const QString &name, QString *ret) const; public: JsonWizard *m_wizard; diff --git a/src/plugins/projectexplorer/localapplicationrunconfiguration.cpp b/src/plugins/projectexplorer/localapplicationrunconfiguration.cpp index 941f2fddda..fc770440d2 100644 --- a/src/plugins/projectexplorer/localapplicationrunconfiguration.cpp +++ b/src/plugins/projectexplorer/localapplicationrunconfiguration.cpp @@ -46,12 +46,12 @@ class FallBackMacroExpander : public Utils::MacroExpander { public: explicit FallBackMacroExpander(const Target *target) : m_target(target) {} - virtual bool resolveMacro(const QString &name, QString *ret); + virtual bool resolveMacro(const QString &name, QString *ret) const; private: const Target *m_target; }; -bool FallBackMacroExpander::resolveMacro(const QString &name, QString *ret) +bool FallBackMacroExpander::resolveMacro(const QString &name, QString *ret) const { if (name == QLatin1String("sourceDir")) { *ret = m_target->project()->projectDirectory().toUserOutput(); diff --git a/src/plugins/projectexplorer/projectmacroexpander.cpp b/src/plugins/projectexplorer/projectmacroexpander.cpp index a102920e49..bddd625062 100644 --- a/src/plugins/projectexplorer/projectmacroexpander.cpp +++ b/src/plugins/projectexplorer/projectmacroexpander.cpp @@ -43,7 +43,7 @@ ProjectMacroExpander::ProjectMacroExpander(const QString &projectFilePath, const : m_projectFile(projectFilePath), m_projectName(projectName), m_kit(k), m_bcName(bcName) { } -bool ProjectMacroExpander::resolveMacro(const QString &name, QString *ret) +bool ProjectMacroExpander::resolveMacro(const QString &name, QString *ret) const { QString result; bool found = false; diff --git a/src/plugins/projectexplorer/projectmacroexpander.h b/src/plugins/projectexplorer/projectmacroexpander.h index aead773845..365e9d2fa8 100644 --- a/src/plugins/projectexplorer/projectmacroexpander.h +++ b/src/plugins/projectexplorer/projectmacroexpander.h @@ -42,7 +42,7 @@ class PROJECTEXPLORER_EXPORT ProjectMacroExpander : public Utils::MacroExpander { public: ProjectMacroExpander(const QString &projectFilePath, const QString &projectName, const Kit *k, const QString &bcName); - bool resolveMacro(const QString &name, QString *ret); + bool resolveMacro(const QString &name, QString *ret) const; private: QFileInfo m_projectFile; diff --git a/src/plugins/qbsprojectmanager/qbsrunconfiguration.cpp b/src/plugins/qbsprojectmanager/qbsrunconfiguration.cpp index 2ed89ca490..e4449c47ea 100644 --- a/src/plugins/qbsprojectmanager/qbsrunconfiguration.cpp +++ b/src/plugins/qbsprojectmanager/qbsrunconfiguration.cpp @@ -282,7 +282,7 @@ QString QbsRunConfiguration::baseWorkingDirectory() const QString QbsRunConfiguration::commandLineArguments() const { - return Utils::QtcProcess::expandMacros(m_commandLineArguments, macroExpander()); + return macroExpander()->expandProcessArgs(m_commandLineArguments); } QString QbsRunConfiguration::rawCommandLineArguments() const diff --git a/src/plugins/qmakeprojectmanager/desktopqmakerunconfiguration.cpp b/src/plugins/qmakeprojectmanager/desktopqmakerunconfiguration.cpp index 554d7eb801..01167b093d 100644 --- a/src/plugins/qmakeprojectmanager/desktopqmakerunconfiguration.cpp +++ b/src/plugins/qmakeprojectmanager/desktopqmakerunconfiguration.cpp @@ -496,7 +496,7 @@ QString DesktopQmakeRunConfiguration::baseWorkingDirectory() const QString DesktopQmakeRunConfiguration::commandLineArguments() const { - return QtcProcess::expandMacros(m_commandLineArguments, macroExpander()); + return macroExpander()->expandProcessArgs(m_commandLineArguments); } QString DesktopQmakeRunConfiguration::rawCommandLineArguments() const diff --git a/src/plugins/qtsupport/customexecutablerunconfiguration.cpp b/src/plugins/qtsupport/customexecutablerunconfiguration.cpp index 4263915dd6..e3222b7560 100644 --- a/src/plugins/qtsupport/customexecutablerunconfiguration.cpp +++ b/src/plugins/qtsupport/customexecutablerunconfiguration.cpp @@ -251,7 +251,7 @@ QString CustomExecutableRunConfiguration::baseWorkingDirectory() const QString CustomExecutableRunConfiguration::commandLineArguments() const { - return Utils::QtcProcess::expandMacros(m_cmdArguments, macroExpander()); + return macroExpander()->expandProcessArgs(m_cmdArguments); } QString CustomExecutableRunConfiguration::rawCommandLineArguments() const -- cgit v1.2.1