diff options
author | Christian Kandeler <christian.kandeler@qt.io> | 2018-02-21 14:59:38 +0100 |
---|---|---|
committer | Christian Kandeler <christian.kandeler@qt.io> | 2018-02-23 08:34:08 +0000 |
commit | 153b9d8b4117bcf8e0b10f7368991e4d4f676a50 (patch) | |
tree | 48cfe7f7b4924fa4337d2c73b36d66d5271b1b2e /src | |
parent | 8ba501a462058202f913b742f35e2d858e6c11e7 (diff) |
Add a location to errors in conditions of Properties items
These can be exceedingly difficult to debug otherwise.
Change-Id: Ie3bc42fbd60e11be105fc23624c77a623dc5f1aa
Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/corelib/language/astpropertiesitemhandler.cpp | 15 | ||||
-rw-r--r-- | src/lib/corelib/language/evaluatorscriptclass.cpp | 13 | ||||
-rw-r--r-- | src/lib/corelib/language/value.h | 15 |
3 files changed, 31 insertions, 12 deletions
diff --git a/src/lib/corelib/language/astpropertiesitemhandler.cpp b/src/lib/corelib/language/astpropertiesitemhandler.cpp index 99768a181..b2520ae39 100644 --- a/src/lib/corelib/language/astpropertiesitemhandler.cpp +++ b/src/lib/corelib/language/astpropertiesitemhandler.cpp @@ -77,7 +77,8 @@ void ASTPropertiesItemHandler::setupAlternatives() class PropertiesBlockConverter { public: - PropertiesBlockConverter(const QString &condition, const QString &overrideListProperties, + PropertiesBlockConverter(const JSSourceValue::AltProperty &condition, + const JSSourceValue::AltProperty &overrideListProperties, Item *propertiesBlockContainer, const Item *propertiesBlock) : m_propertiesBlockContainer(propertiesBlockContainer) , m_propertiesBlock(propertiesBlock) @@ -144,7 +145,7 @@ private: } }; -static QString getPropertyString(const Item *propertiesItem, const QString &name) +static JSSourceValue::AltProperty getPropertyData(const Item *propertiesItem, const QString &name) { const ValuePtr value = propertiesItem->property(name); if (!value) { @@ -152,7 +153,8 @@ static QString getPropertyString(const Item *propertiesItem, const QString &name throw ErrorInfo(Tr::tr("Properties.condition must be provided."), propertiesItem->location()); } - return StringConstants::falseValue(); + return JSSourceValue::AltProperty(StringConstants::falseValue(), + propertiesItem->location()); } if (Q_UNLIKELY(value->type() != Value::JSSourceValueType)) { throw ErrorInfo(Tr::tr("Properties.%1 must be a value binding.").arg(name), @@ -172,14 +174,13 @@ static QString getPropertyString(const Item *propertiesItem, const QString &name } const JSSourceValuePtr srcval = std::static_pointer_cast<JSSourceValue>(value); - return srcval->sourceCodeForEvaluation(); + return JSSourceValue::AltProperty(srcval->sourceCodeForEvaluation(), srcval->location()); } void ASTPropertiesItemHandler::handlePropertiesBlock(const Item *propertiesItem) { - const QString condition = getPropertyString(propertiesItem, - StringConstants::conditionProperty()); - const QString overrideListProperties = getPropertyString(propertiesItem, + const auto condition = getPropertyData(propertiesItem, StringConstants::conditionProperty()); + const auto overrideListProperties = getPropertyData(propertiesItem, StringConstants::overrideListPropertiesProperty()); PropertiesBlockConverter(condition, overrideListProperties, m_parentItem, propertiesItem).apply(); diff --git a/src/lib/corelib/language/evaluatorscriptclass.cpp b/src/lib/corelib/language/evaluatorscriptclass.cpp index 0a3f4d4c5..5a0eec865 100644 --- a/src/lib/corelib/language/evaluatorscriptclass.cpp +++ b/src/lib/corelib/language/evaluatorscriptclass.cpp @@ -252,6 +252,12 @@ private: bool hasError = false; }; + void injectErrorLocation(QScriptValue &sv, const CodeLocation &loc) + { + if (sv.isError() && !engine->lastErrorLocation(sv).isValid()) + sv = engine->currentContext()->throwError(engine->lastError(sv, loc).toString()); + } + JSSourceValueEvaluationResult evaluateJSSourceValue(const JSSourceValue *value, Item *outerItem, const JSSourceValue::Alternative *alternative = nullptr, JSSourceValue *elseCaseValue = nullptr, QScriptValue *outerScriptValue = nullptr) @@ -283,10 +289,11 @@ private: pushScope(maybeExtraScope.first); pushScope(fileCtxScopes.importScope); if (alternative) { - QScriptValue sv = engine->evaluate(alternative->condition); + QScriptValue sv = engine->evaluate(alternative->condition.value); if (engine->hasErrorOrException(sv)) { result.scriptValue = sv; result.hasError = true; + injectErrorLocation(result.scriptValue, alternative->condition.location); return result; } if (sv.toBool()) { @@ -297,10 +304,12 @@ private: result.tryNextAlternative = true; return result; } - sv = engine->evaluate(alternative->overrideListProperties); + sv = engine->evaluate(alternative->overrideListProperties.value); if (engine->hasErrorOrException(sv)) { result.scriptValue = sv; result.hasError = true; + injectErrorLocation(result.scriptValue, + alternative->overrideListProperties.location); return result; } if (sv.toBool()) diff --git a/src/lib/corelib/language/value.h b/src/lib/corelib/language/value.h index a1a5be967..6e7eca2f8 100644 --- a/src/lib/corelib/language/value.h +++ b/src/lib/corelib/language/value.h @@ -148,8 +148,16 @@ public: struct Alternative { + struct PropertyData + { + PropertyData() = default; + PropertyData(const QString &v, const CodeLocation &l) : value(v), location(l) {} + QString value; + CodeLocation location; + }; + Alternative() { } - Alternative(const QString &c, const QString &o, const JSSourceValuePtr &v) + Alternative(const PropertyData &c, const PropertyData &o, const JSSourceValuePtr &v) : condition(c), overrideListProperties(o), value(v) {} Alternative clone() const { @@ -157,10 +165,11 @@ public: std::static_pointer_cast<JSSourceValue>(value->clone())); } - QString condition; - QString overrideListProperties; + PropertyData condition; + PropertyData overrideListProperties; JSSourceValuePtr value; }; + using AltProperty = Alternative::PropertyData; const std::vector<Alternative> &alternatives() const { return m_alternatives; } void addAlternative(const Alternative &alternative) { m_alternatives.push_back(alternative); } |