diff options
author | Christian Stenger <christian.stenger@qt.io> | 2020-04-20 15:23:12 +0200 |
---|---|---|
committer | Christian Stenger <christian.stenger@qt.io> | 2020-05-01 20:56:26 +0000 |
commit | dc56e57cc52de79668ab0612534de527f5fea75a (patch) | |
tree | c57f9df36e003537b3f57dc9a4fc1476b69df998 /tests | |
parent | 0e393fa01fd86e7ffeb35f25c9644f47d99d811a (diff) |
Fix lexer handling of escape sequences in string
The lexer handled escape sequences inside string literals
wrongly which could led to follow-up problems like wrong
offsets or even lines of tokens.
Change-Id: Ief14dda77f9079931a7d19ea549017a1d59c2d0b
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/qml/qqmlparser/tst_qqmlparser.cpp | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp b/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp index e4bcfa7796..10e9e49aa5 100644 --- a/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp +++ b/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp @@ -288,7 +288,7 @@ void tst_qqmlparser::stringLiteral() Engine engine; Lexer lexer(&engine); - QLatin1String code("'hello string'"); + QString code("'hello string'"); lexer.setCode(code , 1); Parser parser(&engine); QVERIFY(parser.parseExpression()); @@ -299,6 +299,35 @@ void tst_qqmlparser::stringLiteral() QCOMPARE(literal->value, "hello string"); QCOMPARE(literal->firstSourceLocation().begin(), 0u); QCOMPARE(literal->lastSourceLocation().end(), quint32(code.size())); + + // test for correct handling escape sequences inside strings + QLatin1String leftCode("'hello\\n\\tstring'"); + QLatin1String plusCode(" + "); + QLatin1String rightCode("'\\nbye'"); + code = leftCode + plusCode + rightCode; + lexer.setCode(code , 1); + QVERIFY(parser.parseExpression()); + + expression = parser.expression(); + QVERIFY(expression); + auto *binaryExpression = QQmlJS::AST::cast<QQmlJS::AST::BinaryExpression *>(expression); + QVERIFY(binaryExpression); + + literal = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(binaryExpression->left); + QVERIFY(literal); + QCOMPARE(literal->value, "hello\n\tstring"); + QCOMPARE(literal->firstSourceLocation().begin(), 0u); + QCOMPARE(literal->firstSourceLocation().startLine, 1u); + QCOMPARE(literal->lastSourceLocation().end(), quint32(leftCode.size())); + + QVERIFY(binaryExpression->right); + literal = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(binaryExpression->right); + QVERIFY(literal); + QCOMPARE(literal->value, "\nbye"); + quint32 offset = quint32(leftCode.size() + plusCode.size()); + QCOMPARE(literal->firstSourceLocation().begin(), offset); + QCOMPARE(literal->firstSourceLocation().startLine, 1u); + QCOMPARE(literal->lastSourceLocation().end(), quint32(code.size())); } void tst_qqmlparser::noSubstitutionTemplateLiteral() |