Commit d808ebed authored by Nikolai Kosjar's avatar Nikolai Kosjar
Browse files

CppEditor: Fix crash in CompleteSwitchCaseStatement



Task-number: QTCREATORBUG-10366

Change-Id: I6d5af5e7a59f3867141c8d7f098128d3db532ee5
Reviewed-by: default avatarErik Verbruggen <erik.verbruggen@digia.com>
parent c79a605b
......@@ -864,6 +864,10 @@ ClassOrNamespace *ClassOrNamespace::lookupType_helper(const Name *name,
return this;
}
}
foreach (Enum *e, unscopedEnums()) {
if (e->identifier() && e->identifier()->isEqualTo(name->identifier()))
return this;
}
if (ClassOrNamespace *e = nestedType(name, origin))
return e;
......
......@@ -156,6 +156,8 @@ private slots:
void test_quickfix_CompleteSwitchCaseStatement_basic1();
void test_quickfix_CompleteSwitchCaseStatement_basic2();
void test_quickfix_CompleteSwitchCaseStatement_oneValueMissing();
void test_quickfix_CompleteSwitchCaseStatement_QTCREATORBUG10366_1();
void test_quickfix_CompleteSwitchCaseStatement_QTCREATORBUG10366_2();
void test_quickfix_GenerateGetterSetter_basicGetterWithPrefix();
void test_quickfix_GenerateGetterSetter_basicGetterWithPrefixAndNamespace();
......
......@@ -461,6 +461,74 @@ void CppEditorPlugin::test_quickfix_CompleteSwitchCaseStatement_oneValueMissing(
data.run(&factory);
}
/// Checks: Find the correct enum type despite there being a declaration with the same name.
void CppEditorPlugin::test_quickfix_CompleteSwitchCaseStatement_QTCREATORBUG10366_1()
{
const QByteArray original =
"enum test { TEST_1, TEST_2 };\n"
"\n"
"void f() {\n"
" enum test test;\n"
" @switch (test) {\n"
" }\n"
"}\n"
;
const QByteArray expected =
"enum test { TEST_1, TEST_2 };\n"
"\n"
"void f() {\n"
" enum test test;\n"
" switch (test) {\n"
" case TEST_1:\n"
" break;\n"
" case TEST_2:\n"
" break;\n"
" }\n"
"}\n"
"\n"
;
CompleteSwitchCaseStatement factory;
TestCase data(original, expected);
data.run(&factory);
}
/// Checks: Find the correct enum type despite there being a declaration with the same name.
void CppEditorPlugin::test_quickfix_CompleteSwitchCaseStatement_QTCREATORBUG10366_2()
{
const QByteArray original =
"enum test1 { Wrong11, Wrong12 };\n"
"enum test { Right1, Right2 };\n"
"enum test2 { Wrong21, Wrong22 };\n"
"\n"
"int main() {\n"
" enum test test;\n"
" @switch (test) {\n"
" }\n"
"}\n"
;
const QByteArray expected =
"enum test1 { Wrong11, Wrong12 };\n"
"enum test { Right1, Right2 };\n"
"enum test2 { Wrong21, Wrong22 };\n"
"\n"
"int main() {\n"
" enum test test;\n"
" switch (test) {\n"
" case Right1:\n"
" break;\n"
" case Right2:\n"
" break;\n"
" }\n"
"}\n"
"\n"
;
CompleteSwitchCaseStatement factory;
TestCase data(original, expected);
data.run(&factory);
}
/// Checks:
/// 1. If the name does not start with ("m_" or "_") and does not
/// end with "_", we are forced to prefix the getter with "get".
......
......@@ -2295,9 +2295,16 @@ Enum *findEnum(const QList<LookupItem> &results, const LookupContext &ctxt)
if (Enum *e = type->asEnumType())
return e;
if (const NamedType *namedType = type->asNamedType()) {
const QList<LookupItem> candidates =
ctxt.lookup(namedType->name(), result.scope());
return findEnum(candidates, ctxt);
if (ClassOrNamespace *con = ctxt.lookupType(namedType->name(), result.scope())) {
const QList<Enum *> enums = con->unscopedEnums();
const Name *referenceName = namedType->name();
foreach (Enum *e, enums) {
if (const Name *candidateName = e->name()) {
if (candidateName->isEqualTo(referenceName))
return e;
}
}
}
}
}
......
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