C++: fix replacing dot(.) with arrow(->)

Fix replacing operator dot(.) with operator arrow(->)
for typedef pointer.

Task-number: QTCREATORBUG-8488
Change-Id: Ic4462bc437a4aa37adfed4fa50b32d9bc20fe194
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@digia.com>
parent e9f219c3
......@@ -978,10 +978,23 @@ ClassOrNamespace *ResolveExpression::baseExpression(const QList<LookupItem> &bas
}
} else if (accessOp == T_DOT) {
if (replacedDotOperator) {
if (PointerType *ptrTy = ty->asPointerType()) {
// replace . with ->
*replacedDotOperator = originalType->isPointerType() || ty->isPointerType();
// replace . with ->
if (PointerType *ptrTy = originalType->asPointerType()) {
// case when original type is a pointer and
// typedef is for type
// e.g.:
// typedef S SType;
// SType *p;
ty = ptrTy->elementType();
}
else if (PointerType *ptrTy = ty->asPointerType()) {
// case when original type is a type and
// typedef is for pointer of type
// e.g.:
// typedef S* SPTR;
// SPTR p;
ty = ptrTy->elementType();
*replacedDotOperator = true;
}
}
......
......@@ -1354,3 +1354,59 @@ void CppToolsPlugin::test_completion_member_access_operator_1()
QVERIFY(completions.contains(QLatin1String("t")));
QVERIFY(replaceAccessOperator);
}
void CppToolsPlugin::test_completion_typedef_of_type_and_replace_access_operator()
{
TestData data;
data.srcText = "\n"
"struct S { int m; };\n"
"typedef S SType;\n"
"SType *p;\n"
"@\n"
"}\n"
;
setup(&data);
Utils::ChangeSet change;
QString txt = QLatin1String("p.");
change.insert(data.pos, txt);
QTextCursor cursor(data.doc);
change.apply(&cursor);
data.pos += txt.length();
bool replaceAccessOperator = false;
QStringList completions = getCompletions(data, &replaceAccessOperator);
QCOMPARE(completions.size(), 2);
QVERIFY(completions.contains(QLatin1String("S")));
QVERIFY(completions.contains(QLatin1String("m")));
QVERIFY(replaceAccessOperator);
}
void CppToolsPlugin::test_completion_typedef_of_pointer_of_type_and_replace_access_operator()
{
TestData data;
data.srcText = "\n"
"struct S { int m; };\n"
"typedef S* SPtr;\n"
"SPtr p;\n"
"@\n"
"}\n"
;
setup(&data);
Utils::ChangeSet change;
QString txt = QLatin1String("p.");
change.insert(data.pos, txt);
QTextCursor cursor(data.doc);
change.apply(&cursor);
data.pos += txt.length();
bool replaceAccessOperator = false;
QStringList completions = getCompletions(data, &replaceAccessOperator);
QCOMPARE(completions.size(), 2);
QVERIFY(completions.contains(QLatin1String("S")));
QVERIFY(completions.contains(QLatin1String("m")));
QVERIFY(replaceAccessOperator);
}
......@@ -114,6 +114,8 @@ private slots:
void test_completion_instantiate_nested_class_when_enclosing_is_template();
void test_completion_instantiate_nested_of_nested_class_when_enclosing_is_template();
void test_completion_member_access_operator_1();
void test_completion_typedef_of_type_and_replace_access_operator();
void test_completion_typedef_of_pointer_of_type_and_replace_access_operator();
void test_format_pointerdeclaration_in_simpledeclarations();
void test_format_pointerdeclaration_in_simpledeclarations_data();
......
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