C++: fix auto declaration in if condition

Fix for auto completion in case of auto declaration inside if condition:

struct Foo { int bar; };
void func()
{
    if (auto s = new Foo)
        s->; // auto completion does not work
}

Task-number: QTCREATORBUG-13805
Change-Id: Ia1776e8cc04e6040a6bf5f43cf82cfd6ce6dde7a
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@theqtcompany.com>
parent 7d4b9c4b
......@@ -1634,6 +1634,15 @@ bool Bind::visit(ConditionAST *ast)
unsigned sourceLocation = location(declaratorId->name, ast->firstToken());
Declaration *decl = control()->newDeclaration(sourceLocation, declaratorId->name->name);
decl->setType(type);
if (type.isAuto() && translationUnit()->languageFeatures().cxx11Enabled) {
const ExpressionAST *initializer = ast->declarator->initializer;
const unsigned startOfExpression = initializer->firstToken();
const unsigned endOfExpression = initializer->lastToken();
decl->setInitializer(asStringLiteral(startOfExpression, endOfExpression));
}
_scope->addMember(decl);
}
......
......@@ -2384,6 +2384,17 @@ void CppToolsPlugin::test_completion_data()
"}\n"
) << _("s.") << (QStringList()
<< QLatin1String("S"));
QTest::newRow("auto_declaration_in_if_condition") << _(
"struct Foo { int bar; };\n"
"void fun() {\n"
" if (auto s = new Foo) {\n"
" @\n"
" }\n"
"}\n"
) << _("s->") << (QStringList()
<< QLatin1String("Foo")
<< QLatin1String("bar"));
}
void CppToolsPlugin::test_completion_member_access_operator()
......
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