forked from qt-creator/qt-creator
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: Nikolai Kosjar <nikolai.kosjar@theqtcompany.com>
This commit is contained in:
9
src/libs/3rdparty/cplusplus/Bind.cpp
vendored
9
src/libs/3rdparty/cplusplus/Bind.cpp
vendored
@@ -1634,6 +1634,15 @@ bool Bind::visit(ConditionAST *ast)
|
|||||||
unsigned sourceLocation = location(declaratorId->name, ast->firstToken());
|
unsigned sourceLocation = location(declaratorId->name, ast->firstToken());
|
||||||
Declaration *decl = control()->newDeclaration(sourceLocation, declaratorId->name->name);
|
Declaration *decl = control()->newDeclaration(sourceLocation, declaratorId->name->name);
|
||||||
decl->setType(type);
|
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);
|
_scope->addMember(decl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2384,6 +2384,17 @@ void CppToolsPlugin::test_completion_data()
|
|||||||
"}\n"
|
"}\n"
|
||||||
) << _("s.") << (QStringList()
|
) << _("s.") << (QStringList()
|
||||||
<< QLatin1String("S"));
|
<< 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()
|
void CppToolsPlugin::test_completion_member_access_operator()
|
||||||
|
|||||||
Reference in New Issue
Block a user