Utils: MacroExpander API cosmetics.

Make const-correct, add convenience function for commandline parameter
expansion.

Change-Id: I12c3651e4e7b8a0a9319d1dfbea676b622b1a41a
Reviewed-by: Tobias Hunger <tobias.hunger@theqtcompany.com>
This commit is contained in:
hjk
2014-10-21 13:19:38 +02:00
parent 1b3db2f57b
commit 35e883eea0
14 changed files with 88 additions and 64 deletions

View File

@@ -29,6 +29,7 @@
****************************************************************************/ ****************************************************************************/
#include "macroexpander.h" #include "macroexpander.h"
#include "qtcprocess.h"
#include "algorithm.h" #include "algorithm.h"
@@ -45,11 +46,52 @@ const char kPathPostfix[] = ":Path";
const char kFileNamePostfix[] = ":FileName"; const char kFileNamePostfix[] = ":FileName";
const char kFileBaseNamePostfix[] = ":FileBaseName"; const char kFileBaseNamePostfix[] = ":FileBaseName";
class MacroExpanderPrivate class MacroExpanderPrivate : public AbstractMacroExpander
{ {
public: public:
MacroExpanderPrivate() : m_accumulating(false) {} 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<QByteArray, MacroExpander::StringFunction> m_map; QHash<QByteArray, MacroExpander::StringFunction> m_map;
QHash<QByteArray, MacroExpander::PrefixFunction> m_prefixMap; QHash<QByteArray, MacroExpander::PrefixFunction> m_prefixMap;
QMap<QByteArray, QString> m_descriptions; QMap<QByteArray, QString> m_descriptions;
@@ -187,49 +229,18 @@ MacroExpander::~MacroExpander()
/*! /*!
* \internal * \internal
*/ */
bool MacroExpander::resolveMacro(const QString &name, QString *ret) bool MacroExpander::resolveMacro(const QString &name, QString *ret) const
{ {
bool found; return d->resolveMacro(name, ret);
*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);
} }
/*! /*!
* Returns the value of the given \a variable. If \a found is given, it is * 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. * 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); return d->value(variable, found);
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();
} }
/*! /*!
@@ -239,18 +250,23 @@ QString MacroExpander::value(const QByteArray &variable, bool *found)
* \sa MacroExpander * \sa MacroExpander
* \sa macroExpander() * \sa macroExpander()
*/ */
QString MacroExpander::expand(const QString &stringWithVariables) QString MacroExpander::expand(const QString &stringWithVariables) const
{ {
QString res = stringWithVariables; QString res = stringWithVariables;
Utils::expandMacros(&res, this); Utils::expandMacros(&res, d);
return res; return res;
} }
QByteArray MacroExpander::expand(const QByteArray &stringWithVariables) QByteArray MacroExpander::expand(const QByteArray &stringWithVariables) const
{ {
return expand(QString::fromLatin1(stringWithVariables)).toLatin1(); 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, * Makes the given string-valued \a prefix known to the variable manager,
* together with a localized \a description. * together with a localized \a description.
@@ -333,7 +349,7 @@ void MacroExpander::registerFileVariables(const QByteArray &prefix,
* \sa registerVariable() * \sa registerVariable()
* \sa registerFileVariables() * \sa registerFileVariables()
*/ */
QList<QByteArray> MacroExpander::variables() QList<QByteArray> MacroExpander::variables() const
{ {
return d->m_descriptions.keys(); return d->m_descriptions.keys();
} }
@@ -341,7 +357,7 @@ QList<QByteArray> MacroExpander::variables()
/*! /*!
* Returns the description that was registered for the \a variable. * 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); return d->m_descriptions.value(variable);
} }
@@ -357,6 +373,11 @@ MacroExpanders MacroExpander::subExpanders() const
return expanders; return expanders;
} }
AbstractMacroExpander *MacroExpander::abstractExpander() const
{
return d;
}
QString MacroExpander::displayName() const QString MacroExpander::displayName() const
{ {
return d->m_displayName; return d->m_displayName;

View File

@@ -47,20 +47,22 @@ class MacroExpander;
typedef std::function<MacroExpander *()> MacroExpanderProvider; typedef std::function<MacroExpander *()> MacroExpanderProvider;
typedef QVector<MacroExpander *> MacroExpanders; typedef QVector<MacroExpander *> MacroExpanders;
class QTCREATOR_UTILS_EXPORT MacroExpander : public AbstractMacroExpander class QTCREATOR_UTILS_EXPORT MacroExpander
{ {
Q_DECLARE_TR_FUNCTIONS("MacroExpander") Q_DECLARE_TR_FUNCTIONS("MacroExpander")
public: public:
explicit MacroExpander(); 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); QString expand(const QString &stringWithVariables) const;
QByteArray expand(const QByteArray &stringWithVariables); QByteArray expand(const QByteArray &stringWithVariables) const;
QString expandProcessArgs(const QString &argsWithVariables) const;
typedef std::function<QString(QString)> PrefixFunction; typedef std::function<QString(QString)> PrefixFunction;
typedef std::function<QString()> StringFunction; typedef std::function<QString()> StringFunction;
@@ -78,10 +80,11 @@ public:
void registerFileVariables(const QByteArray &prefix, void registerFileVariables(const QByteArray &prefix,
const QString &heading, const StringFunction &value); const QString &heading, const StringFunction &value);
QList<QByteArray> variables(); QList<QByteArray> variables() const;
QString variableDescription(const QByteArray &variable); QString variableDescription(const QByteArray &variable) const;
MacroExpanders subExpanders() const; MacroExpanders subExpanders() const;
AbstractMacroExpander *abstractExpander() const;
QString displayName() const; QString displayName() const;
void setDisplayName(const QString &displayName); void setDisplayName(const QString &displayName);
@@ -95,6 +98,7 @@ private:
MacroExpander(const MacroExpander &) Q_DECL_EQ_DELETE; MacroExpander(const MacroExpander &) Q_DECL_EQ_DELETE;
void operator=(const MacroExpander &) Q_DECL_EQ_DELETE; void operator=(const MacroExpander &) Q_DECL_EQ_DELETE;
friend class Internal::MacroExpanderPrivate;
Internal::MacroExpanderPrivate *d; Internal::MacroExpanderPrivate *d;
}; };

View File

@@ -135,7 +135,7 @@ QString CMakeRunConfiguration::baseWorkingDirectory() const
QString CMakeRunConfiguration::commandLineArguments() const QString CMakeRunConfiguration::commandLineArguments() const
{ {
return Utils::QtcProcess::expandMacros(m_arguments, macroExpander()); return macroExpander()->expandProcessArgs(m_arguments);
} }
QString CMakeRunConfiguration::title() const QString CMakeRunConfiguration::title() const

View File

@@ -594,6 +594,5 @@ void ExternalToolConfig::addCategory()
void ExternalToolConfig::updateEffectiveArguments() void ExternalToolConfig::updateEffectiveArguments()
{ {
ui->arguments->setToolTip(Utils::QtcProcess::expandMacros(ui->arguments->text(), ui->arguments->setToolTip(Utils::globalMacroExpander()->expandProcessArgs(ui->arguments->text()));
Utils::globalMacroExpander()));
} }

View File

@@ -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_resolvedInput = expander->expand(m_tool->input());
m_resolvedWorkingDirectory = expander->expand(m_tool->workingDirectory()); m_resolvedWorkingDirectory = expander->expand(m_tool->workingDirectory());

View File

@@ -67,12 +67,12 @@ public:
bc->displayName()), bc->displayName()),
m_bc(bc) m_bc(bc)
{} {}
virtual bool resolveMacro(const QString &name, QString *ret); virtual bool resolveMacro(const QString &name, QString *ret) const;
private: private:
const BuildConfiguration *m_bc; const BuildConfiguration *m_bc;
}; };
bool BuildConfigMacroExpander::resolveMacro(const QString &name, QString *ret) bool BuildConfigMacroExpander::resolveMacro(const QString &name, QString *ret) const
{ {
// legacy variables // legacy variables
if (name == QLatin1String("sourceDir")) { if (name == QLatin1String("sourceDir")) {

View File

@@ -50,7 +50,7 @@ JsonWizardExpander::JsonWizardExpander(JsonWizard *wizard) :
QTC_CHECK(m_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); QVariant v = m_wizard->value(name);
if (v.isValid()) { if (v.isValid()) {

View File

@@ -45,7 +45,7 @@ class JsonWizardExpander : public Utils::MacroExpander
public: public:
explicit JsonWizardExpander(JsonWizard *wizard); explicit JsonWizardExpander(JsonWizard *wizard);
bool resolveMacro(const QString &name, QString *ret); bool resolveMacro(const QString &name, QString *ret) const;
public: public:
JsonWizard *m_wizard; JsonWizard *m_wizard;

View File

@@ -46,12 +46,12 @@ class FallBackMacroExpander : public Utils::MacroExpander
{ {
public: public:
explicit FallBackMacroExpander(const Target *target) : m_target(target) {} 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: private:
const Target *m_target; 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")) { if (name == QLatin1String("sourceDir")) {
*ret = m_target->project()->projectDirectory().toUserOutput(); *ret = m_target->project()->projectDirectory().toUserOutput();

View File

@@ -43,7 +43,7 @@ ProjectMacroExpander::ProjectMacroExpander(const QString &projectFilePath, const
: m_projectFile(projectFilePath), m_projectName(projectName), m_kit(k), m_bcName(bcName) : 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; QString result;
bool found = false; bool found = false;

View File

@@ -42,7 +42,7 @@ class PROJECTEXPLORER_EXPORT ProjectMacroExpander : public Utils::MacroExpander
{ {
public: public:
ProjectMacroExpander(const QString &projectFilePath, const QString &projectName, const Kit *k, const QString &bcName); 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: private:
QFileInfo m_projectFile; QFileInfo m_projectFile;

View File

@@ -282,7 +282,7 @@ QString QbsRunConfiguration::baseWorkingDirectory() const
QString QbsRunConfiguration::commandLineArguments() const QString QbsRunConfiguration::commandLineArguments() const
{ {
return Utils::QtcProcess::expandMacros(m_commandLineArguments, macroExpander()); return macroExpander()->expandProcessArgs(m_commandLineArguments);
} }
QString QbsRunConfiguration::rawCommandLineArguments() const QString QbsRunConfiguration::rawCommandLineArguments() const

View File

@@ -496,7 +496,7 @@ QString DesktopQmakeRunConfiguration::baseWorkingDirectory() const
QString DesktopQmakeRunConfiguration::commandLineArguments() const QString DesktopQmakeRunConfiguration::commandLineArguments() const
{ {
return QtcProcess::expandMacros(m_commandLineArguments, macroExpander()); return macroExpander()->expandProcessArgs(m_commandLineArguments);
} }
QString DesktopQmakeRunConfiguration::rawCommandLineArguments() const QString DesktopQmakeRunConfiguration::rawCommandLineArguments() const

View File

@@ -251,7 +251,7 @@ QString CustomExecutableRunConfiguration::baseWorkingDirectory() const
QString CustomExecutableRunConfiguration::commandLineArguments() const QString CustomExecutableRunConfiguration::commandLineArguments() const
{ {
return Utils::QtcProcess::expandMacros(m_cmdArguments, macroExpander()); return macroExpander()->expandProcessArgs(m_cmdArguments);
} }
QString CustomExecutableRunConfiguration::rawCommandLineArguments() const QString CustomExecutableRunConfiguration::rawCommandLineArguments() const