forked from qt-creator/qt-creator
CppEditor: generate valid code via "Convert to Pointer/Stack Variable"
Adding a "= new <TypeName>" after converting a stack variable without assignment or initializer to pointer. Also remove the assignment when converting from pointer to stack variable as this works better with explicit constructors. Fixes: QTCREATORBUG-23181 Change-Id: I377ec32a1b66cf4b96db14cfcb4b71fb96c80c98 Reviewed-by: David Schulz <david.schulz@qt.io> Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
@@ -755,7 +755,7 @@
|
|||||||
as
|
as
|
||||||
|
|
||||||
\code
|
\code
|
||||||
QByteArray foo = "foo";
|
QByteArray foo("foo");
|
||||||
foo.append("bar");
|
foo.append("bar");
|
||||||
\endcode
|
\endcode
|
||||||
|
|
||||||
|
@@ -1578,7 +1578,7 @@ void CppEditorPlugin::test_quickfix_data()
|
|||||||
" f2(&str);\n"
|
" f2(&str);\n"
|
||||||
"}\n")
|
"}\n")
|
||||||
<< _("void foo() {\n"
|
<< _("void foo() {\n"
|
||||||
" QString *str;\n"
|
" QString *str = new QString;\n"
|
||||||
" if (!str->isEmpty())\n"
|
" if (!str->isEmpty())\n"
|
||||||
" str->clear();\n"
|
" str->clear();\n"
|
||||||
" f1(*str);\n"
|
" f1(*str);\n"
|
||||||
@@ -1612,7 +1612,7 @@ void CppEditorPlugin::test_quickfix_data()
|
|||||||
" str->clear();\n"
|
" str->clear();\n"
|
||||||
"}\n")
|
"}\n")
|
||||||
<< _("void foo() {\n"
|
<< _("void foo() {\n"
|
||||||
" QString str = QLatin1String(\"schnurz\");\n"
|
" QString str(QLatin1String(\"schnurz\"));\n"
|
||||||
" if (!str.isEmpty())\n"
|
" if (!str.isEmpty())\n"
|
||||||
" str.clear();\n"
|
" str.clear();\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
@@ -1731,7 +1731,7 @@ void CppEditorPlugin::test_quickfix_data()
|
|||||||
" f1(str);\n"
|
" f1(str);\n"
|
||||||
"}\n")
|
"}\n")
|
||||||
<< _("void foo() {\n"
|
<< _("void foo() {\n"
|
||||||
" QString *str;\n"
|
" QString *str = new QString;\n"
|
||||||
" str->clear();\n"
|
" str->clear();\n"
|
||||||
" {\n"
|
" {\n"
|
||||||
" QString str;\n"
|
" QString str;\n"
|
||||||
@@ -1808,6 +1808,45 @@ void CppEditorPlugin::test_quickfix_data()
|
|||||||
" BAR = *foo;\n"
|
" BAR = *foo;\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
|
|
||||||
|
QString testObjAndFunc = "struct Object\n"
|
||||||
|
"{\n"
|
||||||
|
" Object(%1){}\n"
|
||||||
|
"};\n"
|
||||||
|
"void func()\n"
|
||||||
|
"{\n"
|
||||||
|
" %2\n"
|
||||||
|
"}\n";
|
||||||
|
|
||||||
|
QTest::newRow("ConvertToStack1_QTCREATORBUG23181")
|
||||||
|
<< CppQuickFixFactoryPtr(new ConvertFromAndToPointer)
|
||||||
|
<< _(testObjAndFunc.arg("int").arg("Object *@obj = new Object(0);").toUtf8())
|
||||||
|
<< _(testObjAndFunc.arg("int").arg("Object obj(0);").toUtf8());
|
||||||
|
|
||||||
|
QTest::newRow("ConvertToStack2_QTCREATORBUG23181")
|
||||||
|
<< CppQuickFixFactoryPtr(new ConvertFromAndToPointer)
|
||||||
|
<< _(testObjAndFunc.arg("int").arg("Object *@obj = new Object{0};").toUtf8())
|
||||||
|
<< _(testObjAndFunc.arg("int").arg("Object obj{0};").toUtf8());
|
||||||
|
|
||||||
|
QTest::newRow("ConvertToPointer1_QTCREATORBUG23181")
|
||||||
|
<< CppQuickFixFactoryPtr(new ConvertFromAndToPointer)
|
||||||
|
<< _(testObjAndFunc.arg("").arg("Object @obj;").toUtf8())
|
||||||
|
<< _(testObjAndFunc.arg("").arg("Object *obj = new Object;").toUtf8());
|
||||||
|
|
||||||
|
QTest::newRow("ConvertToPointer2_QTCREATORBUG23181")
|
||||||
|
<< CppQuickFixFactoryPtr(new ConvertFromAndToPointer)
|
||||||
|
<< _(testObjAndFunc.arg("").arg("Object @obj();").toUtf8())
|
||||||
|
<< _(testObjAndFunc.arg("").arg("Object *obj = new Object();").toUtf8());
|
||||||
|
|
||||||
|
QTest::newRow("ConvertToPointer3_QTCREATORBUG23181")
|
||||||
|
<< CppQuickFixFactoryPtr(new ConvertFromAndToPointer)
|
||||||
|
<< _(testObjAndFunc.arg("").arg("Object @obj{};").toUtf8())
|
||||||
|
<< _(testObjAndFunc.arg("").arg("Object *obj = new Object{};").toUtf8());
|
||||||
|
|
||||||
|
QTest::newRow("ConvertToPointer4_QTCREATORBUG23181")
|
||||||
|
<< CppQuickFixFactoryPtr(new ConvertFromAndToPointer)
|
||||||
|
<< _(testObjAndFunc.arg("int").arg("Object @obj(0);").toUtf8())
|
||||||
|
<< _(testObjAndFunc.arg("int").arg("Object *obj = new Object(0);").toUtf8());
|
||||||
|
|
||||||
QTest::newRow("InsertQtPropertyMembers_noTriggerInvalidCode")
|
QTest::newRow("InsertQtPropertyMembers_noTriggerInvalidCode")
|
||||||
<< CppQuickFixFactoryPtr(new InsertQtPropertyMembers)
|
<< CppQuickFixFactoryPtr(new InsertQtPropertyMembers)
|
||||||
<< _("class C { @Q_PROPERTY(typeid foo READ foo) };\n")
|
<< _("class C { @Q_PROPERTY(typeid foo READ foo) };\n")
|
||||||
|
@@ -4153,20 +4153,21 @@ private:
|
|||||||
|
|
||||||
void removeNewExpression(ChangeSet &changes, NewExpressionAST *newExprAST) const
|
void removeNewExpression(ChangeSet &changes, NewExpressionAST *newExprAST) const
|
||||||
{
|
{
|
||||||
ExpressionListParenAST *exprlist = newExprAST->new_initializer
|
ExpressionListAST *exprlist = nullptr;
|
||||||
? newExprAST->new_initializer->asExpressionListParen()
|
if (newExprAST->new_initializer) {
|
||||||
: nullptr;
|
if (ExpressionListParenAST *ast = newExprAST->new_initializer->asExpressionListParen())
|
||||||
|
exprlist = ast->expression_list;
|
||||||
|
else if (BracedInitializerAST *ast = newExprAST->new_initializer->asBracedInitializer())
|
||||||
|
exprlist = ast->expression_list;
|
||||||
|
}
|
||||||
|
|
||||||
if (exprlist && exprlist->expression_list) {
|
if (exprlist) {
|
||||||
// remove 'new' keyword and type before initializer
|
// remove 'new' keyword and type before initializer
|
||||||
changes.remove(m_file->startOf(newExprAST->new_token),
|
changes.remove(m_file->startOf(newExprAST->new_token),
|
||||||
m_file->startOf(newExprAST->new_initializer));
|
m_file->startOf(newExprAST->new_initializer));
|
||||||
|
|
||||||
// remove parenthesis around initializer
|
changes.remove(m_file->endOf(m_declaratorAST->equal_token - 1),
|
||||||
int pos = m_file->startOf(exprlist->lparen_token);
|
m_file->startOf(m_declaratorAST->equal_token + 1));
|
||||||
changes.remove(pos, pos + 1);
|
|
||||||
pos = m_file->startOf(exprlist->rparen_token);
|
|
||||||
changes.remove(pos, pos + 1);
|
|
||||||
} else {
|
} else {
|
||||||
// remove the whole new expression
|
// remove the whole new expression
|
||||||
changes.remove(m_file->endOf(m_identifierAST->firstToken()),
|
changes.remove(m_file->endOf(m_identifierAST->firstToken()),
|
||||||
@@ -4272,24 +4273,30 @@ private:
|
|||||||
return overview.prettyName(namedType->name->name);
|
return overview.prettyName(namedType->name->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
void insertNewExpression(ChangeSet &changes, CallAST *callAST) const
|
void insertNewExpression(ChangeSet &changes, ExpressionAST *ast) const
|
||||||
{
|
{
|
||||||
const QString typeName = typeNameOfDeclaration();
|
const QString typeName = typeNameOfDeclaration();
|
||||||
if (typeName.isEmpty()) {
|
if (CallAST *callAST = ast->asCall()) {
|
||||||
changes.insert(m_file->startOf(callAST), QLatin1String("new "));
|
if (typeName.isEmpty()) {
|
||||||
|
changes.insert(m_file->startOf(callAST), QLatin1String("new "));
|
||||||
|
} else {
|
||||||
|
changes.insert(m_file->startOf(callAST),
|
||||||
|
QLatin1String("new ") + typeName + QLatin1Char('('));
|
||||||
|
changes.insert(m_file->startOf(callAST->lastToken()), QLatin1String(")"));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
changes.insert(m_file->startOf(callAST),
|
if (typeName.isEmpty())
|
||||||
QLatin1String("new ") + typeName + QLatin1Char('('));
|
return;
|
||||||
changes.insert(m_file->startOf(callAST->lastToken()), QLatin1String(")"));
|
changes.insert(m_file->startOf(ast), QLatin1String(" = new ") + typeName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void insertNewExpression(ChangeSet &changes, ExpressionListParenAST *exprListAST) const
|
void insertNewExpression(ChangeSet &changes) const
|
||||||
{
|
{
|
||||||
const QString typeName = typeNameOfDeclaration();
|
const QString typeName = typeNameOfDeclaration();
|
||||||
if (typeName.isEmpty())
|
if (typeName.isEmpty())
|
||||||
return;
|
return;
|
||||||
changes.insert(m_file->startOf(exprListAST),
|
changes.insert(m_file->endOf(m_identifierAST->firstToken()),
|
||||||
QLatin1String(" = new ") + typeName);
|
QLatin1String(" = new ") + typeName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4301,10 +4308,15 @@ private:
|
|||||||
changes.insert(m_file->startOf(idExprAST), QLatin1String("&"));
|
changes.insert(m_file->startOf(idExprAST), QLatin1String("&"));
|
||||||
} else if (CallAST *callAST = m_declaratorAST->initializer->asCall()) {
|
} else if (CallAST *callAST = m_declaratorAST->initializer->asCall()) {
|
||||||
insertNewExpression(changes, callAST);
|
insertNewExpression(changes, callAST);
|
||||||
} else if (ExpressionListParenAST *exprListAST
|
} else if (ExpressionListParenAST *exprListAST = m_declaratorAST->initializer
|
||||||
= m_declaratorAST->initializer->asExpressionListParen()) {
|
->asExpressionListParen()) {
|
||||||
insertNewExpression(changes, exprListAST);
|
insertNewExpression(changes, exprListAST);
|
||||||
|
} else if (BracedInitializerAST *bracedInitializerAST = m_declaratorAST->initializer
|
||||||
|
->asBracedInitializer()) {
|
||||||
|
insertNewExpression(changes, bracedInitializerAST);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
insertNewExpression(changes);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fix all occurrences of the identifier in this function.
|
// Fix all occurrences of the identifier in this function.
|
||||||
|
Reference in New Issue
Block a user