CppEditor: Fix "insert definition" for templates with value parameters

Fixes: QTCREATORBUG-26113
Change-Id: I2d2a1c1bdcffd67072bbda99dabbbfbfafe115c5
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Christian Kandeler
2021-08-16 17:52:02 +02:00
parent 837a707ec7
commit d7717f1eca
5 changed files with 36 additions and 3 deletions

View File

@@ -95,6 +95,7 @@ public:
FullySpecifiedType &type() { return _expressionTy; } FullySpecifiedType &type() { return _expressionTy; }
const NumericLiteral *numericLiteral() const { return _numericLiteral; } const NumericLiteral *numericLiteral() const { return _numericLiteral; }
void setNumericLiteral(const NumericLiteral *l) { _numericLiteral = l; }
bool operator==(const TemplateArgument &other) const bool operator==(const TemplateArgument &other) const
{ {

View File

@@ -440,8 +440,11 @@ void CloneName::visit(const AnonymousNameId *name)
void CloneName::visit(const TemplateNameId *name) void CloneName::visit(const TemplateNameId *name)
{ {
std::vector<TemplateArgument> args(name->templateArgumentCount()); std::vector<TemplateArgument> args(name->templateArgumentCount());
for (int i = 0; i < int(args.size()); ++i) for (int i = 0; i < int(args.size()); ++i) {
args[i].type() = _clone->type(name->templateArgumentAt(i).type(), _subst); args[i].type() = _clone->type(name->templateArgumentAt(i).type(), _subst);
args[i].setNumericLiteral(_clone->numericLiteral(
name->templateArgumentAt(i).numericLiteral()));
}
if (args.empty()) if (args.empty())
_name = _control->templateNameId(_clone->identifier(name->identifier()), name->isSpecialization()); _name = _control->templateNameId(_clone->identifier(name->identifier()), name->isSpecialization());
else else

View File

@@ -274,8 +274,15 @@ public:
void visit(const TemplateNameId *name) override void visit(const TemplateNameId *name) override
{ {
QVarLengthArray<TemplateArgument, 8> args(name->templateArgumentCount()); QVarLengthArray<TemplateArgument, 8> args(name->templateArgumentCount());
for (int i = 0; i < name->templateArgumentCount(); ++i) for (int i = 0; i < name->templateArgumentCount(); ++i) {
args[i] = rewrite->rewriteType(name->templateArgumentAt(i).type()); const TemplateArgument &oldArg = name->templateArgumentAt(i);
args[i] = rewrite->rewriteType(oldArg.type());
const NumericLiteral * const number = oldArg.numericLiteral();
if (number) {
args[i].setNumericLiteral(control()->numericLiteral(number->chars(),
number->size()));
}
}
temps.append(control()->templateNameId(identifier(name->identifier()), name->isSpecialization(), temps.append(control()->templateNameId(identifier(name->identifier()), name->isSpecialization(),
args.data(), args.size())); args.data(), args.size()));
} }

View File

@@ -169,6 +169,7 @@ private slots:
void test_quickfix_InsertDefFromDecl_findImplementationFile(); void test_quickfix_InsertDefFromDecl_findImplementationFile();
void test_quickfix_InsertDefFromDecl_unicodeIdentifier(); void test_quickfix_InsertDefFromDecl_unicodeIdentifier();
void test_quickfix_InsertDefFromDecl_templateClass(); void test_quickfix_InsertDefFromDecl_templateClass();
void test_quickfix_InsertDefFromDecl_templateClassWithValueParam();
void test_quickfix_InsertDefFromDecl_templateFunction(); void test_quickfix_InsertDefFromDecl_templateFunction();
void test_quickfix_InsertDefFromDecl_notTriggeredForFriendFunc(); void test_quickfix_InsertDefFromDecl_notTriggeredForFriendFunc();
void test_quickfix_InsertDefFromDecl_minimalFunctionParameterType(); void test_quickfix_InsertDefFromDecl_minimalFunctionParameterType();

View File

@@ -4605,6 +4605,27 @@ void CppEditorPlugin::test_quickfix_InsertDefFromDecl_templateClass()
QuickFixOperationTest(singleDocument(original, expected), &factory); QuickFixOperationTest(singleDocument(original, expected), &factory);
} }
void CppEditorPlugin::test_quickfix_InsertDefFromDecl_templateClassWithValueParam()
{
QList<QuickFixTestDocument::Ptr> testDocuments;
QByteArray original =
"template<typename T, int size> struct MyArray {};\n"
"MyArray<int, 1> @foo();";
QByteArray expected = original;
testDocuments << QuickFixTestDocument::create("file.h", original, expected);
original = "#include \"file.h\"\n";
expected =
"#include \"file.h\"\n\n"
"MyArray<int, 1> foo()\n"
"{\n\n"
"}\n";
testDocuments << QuickFixTestDocument::create("file.cpp", original, expected);
InsertDefFromDecl factory;
QuickFixOperationTest(testDocuments, &factory);
}
void CppEditorPlugin::test_quickfix_InsertDefFromDecl_templateFunction() void CppEditorPlugin::test_quickfix_InsertDefFromDecl_templateFunction()
{ {
QByteArray original = QByteArray original =