CppEditor: Fix Typedef Handling with 'Remove Using Directive' QuickFix

Previously, typedefs were ignored and the new code became invalid after
applying the quickfix.

Change-Id: I0d4295e90d02dfacc3edac5ac3f96d9edbeaf662
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Leander Schulten
2020-10-09 15:08:05 +02:00
parent 7ec2fd482e
commit 15f39cf37c
3 changed files with 87 additions and 2 deletions

View File

@@ -7290,6 +7290,33 @@ private:
int counter;
};
/**
* @brief getBaseName returns the base name of a qualified name or nullptr.
* E.g.: foo::bar => foo; bar => bar
* @param name The Name, maybe qualified
* @return The base name of the qualified name or nullptr
*/
const Identifier *getBaseName(const Name *name)
{
class GetBaseName : public NameVisitor
{
void visit(const Identifier *name) override { baseName = name; }
void visit(const QualifiedNameId *name) override
{
if (name->base())
accept(name->base());
else
accept(name->name());
}
public:
const Identifier *baseName = nullptr;
};
GetBaseName getter;
getter.accept(name);
return getter.baseName;
}
/**
* @brief countNames counts the parts of the Name.
* E.g. if the name is std::vector, the function returns 2, if the name is variant, returns 1
@@ -7489,11 +7516,24 @@ private:
{
if (m_start) {
Scope *scope = m_file->scopeAt(ast->firstToken());
const QList<LookupItem> lookups = m_context.lookup(ast->name->name, scope);
const Name *wantToLookup = ast->name->name;
// first check if the base name is a typedef. Consider the following example:
// using namespace std;
// using vec = std::vector<int>;
// vec::iterator it; // we have to lookup 'vec' and not iterator (would result in
// std::vector<int>::iterator => std::vec::iterator, which is wrong)
const Name *baseName = getBaseName(wantToLookup);
QList<LookupItem> typedefCandidates = m_context.lookup(baseName, scope);
if (!typedefCandidates.isEmpty()) {
if (typedefCandidates.front().declaration()->isTypedef())
wantToLookup = baseName;
}
const QList<LookupItem> lookups = m_context.lookup(wantToLookup, scope);
if (!lookups.empty()) {
QList<const Name *> fullName = m_context.fullyQualifiedName(
lookups.first().declaration());
const int currentNameCount = countNames(ast->name->name);
const int currentNameCount = countNames(wantToLookup);
const bool needNamespace = needMissingNamespaces(std::move(fullName),
currentNameCount);
if (needNamespace)