C++: Safe accessing of elements of TranslationUnit::_tokens

Until now std::vector::at() was used to access the elements. This is
handy for debugging since an exception is thrown for invalid indices,
but it does not stop Qt Creator from crashing because exceptions are not
caught.

This is especially a problem for the parser, which has to look ahead via
LA(n), which accesses TranslationUnit::_tokens.

With this patch, explicit bounds checking is done before accessing the
elements and thus calls to

  std::vector::at() // bounds checking, throwing out_of_range

were replaced by calls to

  std::vector::operator[]() // no bounds checking, not throwing out_of_range

Measuring the parse time for the Qt Creator project shows that there is
no slowdown. In both cases, with and without the patch, about 15s are
needed on the authors machine.

Task-number: QTCREATORBUG-10453

Change-Id: I32b12a526ff7199bcadfc21a3deb5354063a3e3b
Reviewed-by: Erik Verbruggen <erik.verbruggen@digia.com>
This commit is contained in:
Nikolai Kosjar
2013-10-22 11:24:29 +02:00
parent 2448cab1c4
commit 1a5c7d8ef5
3 changed files with 43 additions and 23 deletions

View File

@@ -180,6 +180,8 @@ private slots:
// Qt "keywords"
void q_enum_1();
void incomplete_ast();
};
void tst_AST::gcc_attributes_1()
@@ -1723,6 +1725,13 @@ void tst_AST::q_enum_1()
QCOMPARE(unit->spell(e->identifier_token), "e");
}
void tst_AST::incomplete_ast()
{
QSharedPointer<TranslationUnit> unit(parseStatement("class A { virtual void a() =\n"));
AST *ast = unit->ast();
QVERIFY(ast);
}
void tst_AST::initTestCase()
{
control.setDiagnosticClient(&diag);