Commit 857457e0 authored by Erik Verbruggen's avatar Erik Verbruggen Committed by Nikolai Kosjar

C++: handle non-arguments in function parameter scope gracefuly

Task-number: QTCREATORBUG-8316
Change-Id: I8897f75f30d3aeaf049cc792060c582ab1e3343d
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@digia.com>
parent 9d7e786b
......@@ -284,17 +284,32 @@ bool Function::hasReturnType() const
unsigned Function::argumentCount() const
{
unsigned c = memberCount();
if (c > 0 && memberAt(0)->type()->isVoidType())
const unsigned memCnt = memberCount();
if (memCnt > 0 && memberAt(0)->type()->isVoidType())
return 0;
// Definitions with function-try-blocks will have more than a block.
while (c > 0 && memberAt(c - 1)->isBlock())
--c;
return c;
// Definitions with function-try-blocks will have more than a block, and
// arguments with a lambda as default argument will also have more blocks.
unsigned argc = 0;
for (unsigned it = 0; it < memCnt; ++it)
if (memberAt(it)->isArgument())
++argc;
return argc;
}
Symbol *Function::argumentAt(unsigned index) const
{ return memberAt(index); }
{
for (unsigned it = 0, eit = memberCount(); it < eit; ++it) {
if (Argument *arg = memberAt(it)->asArgument()) {
if (index == 0)
return arg;
else
--index;
}
}
return 0;
}
bool Function::hasArguments() const
{
......@@ -381,7 +396,7 @@ bool Function::maybeValidPrototype(unsigned actualArgumentCount) const
for (; minNumberArguments < argc; ++minNumberArguments) {
Argument *arg = argumentAt(minNumberArguments)->asArgument();
if (! arg) // TODO: Fix me properly - QTCREATORBUG-8316
if (! arg)
return false;
if (arg->hasInitializer())
......
......@@ -177,6 +177,7 @@ private slots:
void q_enum_1();
void lambda_1();
void lambda_2();
void diagnostic_error();
};
......@@ -722,6 +723,33 @@ void tst_Semantic::lambda_1()
QCOMPARE(doc->globals->memberCount(), 1U);
}
void tst_Semantic::lambda_2()
{
QSharedPointer<Document> doc = document(
"\n"
"class A {\n"
" void f(int i = [](){});\n"
"};\n"
, false, false, true);
QCOMPARE(doc->errorCount, 0U);
QCOMPARE(doc->globals->memberCount(), 1U);
Class *A = doc->globals->memberAt(0)->asClass();
QVERIFY(A);
QCOMPARE(A->memberCount(), 1U);
Declaration *d = A->memberAt(0)->asDeclaration();
QCOMPARE(d->name()->identifier()->chars(), "f");
Function *ty = d->type()->asFunctionType();
QVERIFY(ty);
QCOMPARE(ty->argumentCount(), 1U);
Argument *arg = ty->argumentAt(0)->asArgument();
QVERIFY(arg);
const StringLiteral *init = arg->initializer();
QVERIFY(init);
QCOMPARE(init->chars(), " [](){}");
}
void tst_Semantic::diagnostic_error()
{
QSharedPointer<Document> doc = document("\n"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment