Commit 3cde5284 authored by Przemyslaw Gorszkowski's avatar Przemyslaw Gorszkowski Committed by Erik Verbruggen

C++: fix local types and members

It fixes code model when we have local type which contains a member
of other type, e.g.:
struct OtherType { int otherTypeMember; };
void foo()
{
    struct LocalType
    {
        int localTypeMember;
        OtherType ot;
    };

    LocalType lt;
    lt.ot.//no code completion
}

Change-Id: I018f492a6c48bfcbba0ef376ef005825e2b13f35
Reviewed-by: default avatarErik Verbruggen <erik.verbruggen@digia.com>
parent 43a33343
...@@ -360,6 +360,15 @@ ClassOrNamespace *LookupContext::lookupType(const Name *name, Scope *scope, ...@@ -360,6 +360,15 @@ ClassOrNamespace *LookupContext::lookupType(const Name *name, Scope *scope,
} else if (ClassOrNamespace *b = bindings()->lookupType(scope, enclosingTemplateInstantiation)) { } else if (ClassOrNamespace *b = bindings()->lookupType(scope, enclosingTemplateInstantiation)) {
return b->lookupType(name); return b->lookupType(name);
} else if (Class *scopeAsClass = scope->asClass()) {
if (scopeAsClass->enclosingScope()->isBlock()) {
if (ClassOrNamespace *b = lookupType(scopeAsClass->name(),
scopeAsClass->enclosingScope(),
enclosingTemplateInstantiation,
typedefsBeingResolved)) {
return b->lookupType(name);
}
}
} }
return 0; return 0;
......
...@@ -2825,3 +2825,205 @@ void CppToolsPlugin::test_completion_lambdaCalls_5() ...@@ -2825,3 +2825,205 @@ void CppToolsPlugin::test_completion_lambdaCalls_5()
QVERIFY(completions.contains(QLatin1String("S"))); QVERIFY(completions.contains(QLatin1String("S")));
QVERIFY(completions.contains(QLatin1String("bar"))); QVERIFY(completions.contains(QLatin1String("bar")));
} }
void CppToolsPlugin::test_completion_local_type_and_member_1()
{
TestData data;
data.srcText =
"struct OtherType { int otherTypeMember; };\n"
"void foo()\n"
"{\n"
" struct LocalType\n"
" {\n"
" int localTypeMember;\n"
" OtherType ot;\n"
" };\n"
" LocalType lt;\n"
" @\n"
" // padding so we get the scope right\n"
"}\n"
;
setup(&data);
Utils::ChangeSet change;
QString txt = QLatin1String("lt.ot.");
change.insert(data.pos, txt);
QTextCursor cursor(data.doc);
change.apply(&cursor);
data.pos += txt.length();
QStringList completions = getCompletions(data);
QCOMPARE(completions.size(), 2);
QVERIFY(completions.contains(QLatin1String("OtherType")));
QVERIFY(completions.contains(QLatin1String("otherTypeMember")));
}
void CppToolsPlugin::test_completion_local_type_and_member_2()
{
TestData data;
data.srcText =
"void foo()\n"
"{\n"
" struct OtherType { int otherTypeMember; };\n"
" struct LocalType\n"
" {\n"
" int localTypeMember;\n"
" OtherType ot;\n"
" };\n"
" LocalType lt;\n"
" @\n"
" // padding so we get the scope right\n"
"}\n"
;
setup(&data);
Utils::ChangeSet change;
QString txt = QLatin1String("lt.ot.");
change.insert(data.pos, txt);
QTextCursor cursor(data.doc);
change.apply(&cursor);
data.pos += txt.length();
QStringList completions = getCompletions(data);
QCOMPARE(completions.size(), 2);
QVERIFY(completions.contains(QLatin1String("OtherType")));
QVERIFY(completions.contains(QLatin1String("otherTypeMember")));
}
void CppToolsPlugin::test_completion_local_type_and_member_3()
{
TestData data;
data.srcText =
"void foo()\n"
"{\n"
" struct OtherType { int otherTypeMember; };\n"
" {\n"
" struct LocalType\n"
" {\n"
" int localTypeMember;\n"
" OtherType ot;\n"
" };\n"
" LocalType lt;\n"
" @\n"
" // padding so we get the scope right\n"
" }\n"
"}\n"
;
setup(&data);
Utils::ChangeSet change;
QString txt = QLatin1String("lt.ot.");
change.insert(data.pos, txt);
QTextCursor cursor(data.doc);
change.apply(&cursor);
data.pos += txt.length();
QStringList completions = getCompletions(data);
QCOMPARE(completions.size(), 2);
QVERIFY(completions.contains(QLatin1String("OtherType")));
QVERIFY(completions.contains(QLatin1String("otherTypeMember")));
}
void CppToolsPlugin::test_completion_local_type_and_member_4()
{
TestData data;
data.srcText =
"namespace NS {struct OtherType { int otherTypeMember; };}\n"
"void foo()\n"
"{\n"
" struct LocalType\n"
" {\n"
" int localTypeMember;\n"
" NS::OtherType ot;\n"
" };\n"
" LocalType lt;\n"
" @\n"
" // padding so we get the scope right\n"
"}\n"
;
setup(&data);
Utils::ChangeSet change;
QString txt = QLatin1String("lt.ot.");
change.insert(data.pos, txt);
QTextCursor cursor(data.doc);
change.apply(&cursor);
data.pos += txt.length();
QStringList completions = getCompletions(data);
QCOMPARE(completions.size(), 2);
QVERIFY(completions.contains(QLatin1String("OtherType")));
QVERIFY(completions.contains(QLatin1String("otherTypeMember")));
}
void CppToolsPlugin::test_completion_local_type_and_member_5()
{
TestData data;
data.srcText =
"namespace NS {struct OtherType { int otherTypeMember; };}\n"
"void foo()\n"
"{\n"
" using namespace NS;\n"
" struct LocalType\n"
" {\n"
" int localTypeMember;\n"
" OtherType ot;\n"
" };\n"
" LocalType lt;\n"
" @\n"
" // padding so we get the scope right\n"
"}\n"
;
setup(&data);
Utils::ChangeSet change;
QString txt = QLatin1String("lt.ot.");
change.insert(data.pos, txt);
QTextCursor cursor(data.doc);
change.apply(&cursor);
data.pos += txt.length();
QStringList completions = getCompletions(data);
QCOMPARE(completions.size(), 2);
QVERIFY(completions.contains(QLatin1String("OtherType")));
QVERIFY(completions.contains(QLatin1String("otherTypeMember")));
}
void CppToolsPlugin::test_completion_local_type_and_member_6()
{
TestData data;
data.srcText =
"namespace NS {struct OtherType { int otherTypeMember; };}\n"
"void foo()\n"
"{\n"
" using NS::OtherType;\n"
" struct LocalType\n"
" {\n"
" int localTypeMember;\n"
" OtherType ot;\n"
" };\n"
" LocalType lt;\n"
" @\n"
" // padding so we get the scope right\n"
"}\n"
;
setup(&data);
Utils::ChangeSet change;
QString txt = QLatin1String("lt.ot.");
change.insert(data.pos, txt);
QTextCursor cursor(data.doc);
change.apply(&cursor);
data.pos += txt.length();
QStringList completions = getCompletions(data);
QCOMPARE(completions.size(), 2);
QVERIFY(completions.contains(QLatin1String("OtherType")));
QVERIFY(completions.contains(QLatin1String("otherTypeMember")));
}
...@@ -163,6 +163,13 @@ private slots: ...@@ -163,6 +163,13 @@ private slots:
void test_completion_lambdaCalls_4(); void test_completion_lambdaCalls_4();
void test_completion_lambdaCalls_5(); void test_completion_lambdaCalls_5();
void test_completion_local_type_and_member_1();
void test_completion_local_type_and_member_2();
void test_completion_local_type_and_member_3();
void test_completion_local_type_and_member_4();
void test_completion_local_type_and_member_5();
void test_completion_local_type_and_member_6();
void test_format_pointerdeclaration_in_simpledeclarations(); void test_format_pointerdeclaration_in_simpledeclarations();
void test_format_pointerdeclaration_in_simpledeclarations_data(); void test_format_pointerdeclaration_in_simpledeclarations_data();
void test_format_pointerdeclaration_in_controlflowstatements(); void test_format_pointerdeclaration_in_controlflowstatements();
......
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