Improve QMake JSON error

We can not improve the result from JSON parsing without changing API,
so instead recalculate the line and column based on input and offset.

Change-Id: I54149233f71023aa5d30deff854d6f3406c5c48c
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@qt.io>
This commit is contained in:
Allan Sandfeld Jensen 2016-11-11 15:22:45 +01:00
parent e133f0cca4
commit 887e260a93
2 changed files with 37 additions and 4 deletions

View File

@ -396,14 +396,47 @@ static void addJsonValue(const QJsonValue &value, const QString &keyPrefix, ProV
}
}
struct ErrorPosition {
int line;
int column;
};
static ErrorPosition calculateErrorPosition(const QByteArray &json, int offset)
{
ErrorPosition pos = { 0, 0 };
offset--; // offset is 1-based, switching to 0-based
for (int i = 0; i < offset; ++i) {
switch (json.at(i)) {
case '\n':
pos.line++;
pos.column = 0;
break;
case '\r':
break;
case '\t':
pos.column = (pos.column + 8) & ~7;
break;
default:
pos.column++;
break;
}
}
// Lines and columns in text editors are 1-based:
pos.line++;
pos.column++;
return pos;
}
QMakeEvaluator::VisitReturn QMakeEvaluator::parseJsonInto(const QByteArray &json, const QString &into, ProValueMap *value)
{
QJsonParseError error;
QJsonDocument document = QJsonDocument::fromJson(json, &error);
if (document.isNull()) {
if (error.error != QJsonParseError::NoError)
evalError(fL1S("Error parsing json at offset %1: %2")
.arg(error.offset).arg(error.errorString()));
if (error.error != QJsonParseError::NoError) {
ErrorPosition errorPos = calculateErrorPosition(json, error.offset);
evalError(fL1S("Error parsing JSON at %1:%2: %3")
.arg(errorPos.line).arg(errorPos.column).arg(error.errorString()));
}
return QMakeEvaluator::ReturnFalse;
}

View File

@ -2259,7 +2259,7 @@ void tst_qmakelib::addTestFunctions(const QString &qindir)
<< "jsontext = not good\n"
"parseJson(jsontext, json): OK = 1"
<< "OK = UNDEF"
<< "##:2: Error parsing json at offset 1: illegal value"
<< "##:2: Error parsing JSON at 1:1: illegal value"
<< true;
QTest::newRow("parseJson(): bad number of arguments")