CPlusPlus: Improve type name minimization

... for function parameters. These are located in the scope of the
surrounding class or namespace.
This uncovered a bug in the "Insert Virtual Functions of Base Classes"
quickfix, which we also fix here.

Fixes: QTCREATORBUG-8030
Change-Id: I7f11659dc8e252e3819df8178734e8958fa1b496
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Christian Kandeler
2021-02-02 14:01:15 +01:00
parent f47c7b2e90
commit b066b3029e
4 changed files with 103 additions and 9 deletions

View File

@@ -823,16 +823,31 @@ public:
headerChangeSet.insert(m_insertPosDecl, comment);
first = false;
}
// Construct declaration
// setup rewriting to get minimally qualified names
SubstitutionEnvironment env;
env.setContext(context());
env.switchScope(classItem->klass->enclosingScope());
env.enter(&useMinimalNames);
QString declaration;
const FullySpecifiedType tn = rewriteType(funcItem->function->type(), &env, control);
declaration += printer.prettyType(tn, funcItem->function->unqualifiedName());
// Function type minimalization: As base class and derived class could be in
// different namespaces, we must first make the type fully qualified before
// it can get minimized.
Clone cloner(control);
Function newFunc(&cloner, nullptr, const_cast<Function *>(funcItem->function));
newFunc.setEnclosingScope(const_cast<Class *>(targetClass));
SubstitutionEnvironment envQualified;
envQualified.setContext(context());
envQualified.switchScope(classItem->klass->enclosingScope());
UseQualifiedNames useQualifiedNames;
envQualified.enter(&useQualifiedNames);
newFunc.setReturnType(rewriteType(newFunc.returnType(), &envQualified, control));
const int argc = newFunc.argumentCount();
for (int i = 0; i < argc; ++i) {
Argument * const arg = newFunc.argumentAt(i)->asArgument();
QTC_ASSERT(arg, continue);
arg->setType(rewriteType(arg->type(), &envQualified, control));
}
SubstitutionEnvironment envMinimized;
envMinimized.setContext(context());
envMinimized.switchScope(targetClass->enclosingScope());
envMinimized.enter(&useMinimalNames);
const FullySpecifiedType tn = rewriteType(newFunc.type(), &envMinimized, control);
QString declaration = printer.prettyType(tn, newFunc.unqualifiedName());
if (m_factory->settings()->insertVirtualKeyword)
declaration = QLatin1String("virtual ") + declaration;