forked from qt-creator/qt-creator
CppEditor: Remove using namespace quickfix: Don't insert inline namespaces
Change-Id: If386d31de723ca928d3c50f55e32205c50937b0a Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
@@ -60,15 +60,22 @@ static void addNames(const Name *name, QList<const Name *> *names, bool addAllNa
|
||||
}
|
||||
}
|
||||
|
||||
static void path_helper(Symbol *symbol, QList<const Name *> *names)
|
||||
static void path_helper(Symbol *symbol,
|
||||
QList<const Name *> *names,
|
||||
LookupContext::InlineNamespacePolicy policy)
|
||||
{
|
||||
if (! symbol)
|
||||
return;
|
||||
|
||||
path_helper(symbol->enclosingScope(), names);
|
||||
path_helper(symbol->enclosingScope(), names, policy);
|
||||
|
||||
if (symbol->name()) {
|
||||
if (symbol->isClass() || symbol->isNamespace()) {
|
||||
if (policy == LookupContext::HideInlineNamespaces) {
|
||||
auto ns = symbol->asNamespace();
|
||||
if (ns && ns->isInline())
|
||||
return;
|
||||
}
|
||||
addNames(symbol->name(), names);
|
||||
|
||||
} else if (symbol->isObjCClass() || symbol->isObjCBaseClass() || symbol->isObjCProtocol()
|
||||
@@ -206,17 +213,17 @@ LookupContext &LookupContext::operator=(const LookupContext &other)
|
||||
return *this;
|
||||
}
|
||||
|
||||
QList<const Name *> LookupContext::fullyQualifiedName(Symbol *symbol)
|
||||
QList<const Name *> LookupContext::fullyQualifiedName(Symbol *symbol, InlineNamespacePolicy policy)
|
||||
{
|
||||
QList<const Name *> qualifiedName = path(symbol->enclosingScope());
|
||||
QList<const Name *> qualifiedName = path(symbol->enclosingScope(), policy);
|
||||
addNames(symbol->name(), &qualifiedName, /*add all names*/ true);
|
||||
return qualifiedName;
|
||||
}
|
||||
|
||||
QList<const Name *> LookupContext::path(Symbol *symbol)
|
||||
QList<const Name *> LookupContext::path(Symbol *symbol, InlineNamespacePolicy policy)
|
||||
{
|
||||
QList<const Name *> names;
|
||||
path_helper(symbol, &names);
|
||||
path_helper(symbol, &names, policy);
|
||||
return names;
|
||||
}
|
||||
|
||||
|
@@ -314,8 +314,11 @@ public:
|
||||
QSharedPointer<CreateBindings> bindings() const
|
||||
{ return _bindings; }
|
||||
|
||||
static QList<const Name *> fullyQualifiedName(Symbol *symbol);
|
||||
static QList<const Name *> path(Symbol *symbol);
|
||||
enum InlineNamespacePolicy { ShowInlineNamespaces, HideInlineNamespaces };
|
||||
static QList<const Name *> fullyQualifiedName(
|
||||
Symbol *symbol, InlineNamespacePolicy policy = ShowInlineNamespaces);
|
||||
static QList<const Name *> path(Symbol *symbol,
|
||||
InlineNamespacePolicy policy = ShowInlineNamespaces);
|
||||
|
||||
static const Name *minimalName(Symbol *symbol, ClassOrNamespace *target, Control *control);
|
||||
|
||||
|
@@ -6792,6 +6792,52 @@ void CppEditorPlugin::test_quickfix_removeUsingNamespace_data()
|
||||
expected1 = expected2 = expected3 = "";
|
||||
QTest::newRow("global scope remove in every file")
|
||||
<< h1 << h2 << h3 << expected1 << expected2 << expected3 << 1;
|
||||
|
||||
// test: dont print inline namespaces
|
||||
h1 = R"--(
|
||||
namespace test {
|
||||
inline namespace test {
|
||||
class Foo{
|
||||
void foo1();
|
||||
void foo2();
|
||||
};
|
||||
inline int TEST = 42;
|
||||
}
|
||||
}
|
||||
)--";
|
||||
h2 = R"--(
|
||||
#include "header1.h"
|
||||
using namespace tes@t;
|
||||
)--";
|
||||
h3 = R"--(
|
||||
#include "header2.h"
|
||||
Foo f1;
|
||||
test::Foo f2;
|
||||
using T1 = Foo;
|
||||
using T2 = test::Foo;
|
||||
int i1 = TEST;
|
||||
int i2 = test::TEST;
|
||||
void Foo::foo1(){};
|
||||
void test::Foo::foo2(){};
|
||||
)--";
|
||||
|
||||
expected1 = h1;
|
||||
expected2 = R"--(
|
||||
#include "header1.h"
|
||||
)--";
|
||||
expected3 = R"--(
|
||||
#include "header2.h"
|
||||
test::Foo f1;
|
||||
test::Foo f2;
|
||||
using T1 = test::Foo;
|
||||
using T2 = test::Foo;
|
||||
int i1 = test::TEST;
|
||||
int i2 = test::TEST;
|
||||
void test::Foo::foo1(){};
|
||||
void test::Foo::foo2(){};
|
||||
)--";
|
||||
QTest::newRow("don't insert inline namespaces")
|
||||
<< h1 << h2 << h3 << expected1 << expected2 << expected3 << 0;
|
||||
}
|
||||
|
||||
void CppEditorPlugin::test_quickfix_removeUsingNamespace()
|
||||
|
@@ -7538,7 +7538,9 @@ private:
|
||||
const QList<LookupItem> localLookup = m_context.lookup(ast->name->name, scope);
|
||||
QList<const Name *> longestName;
|
||||
for (const LookupItem &item : localLookup) {
|
||||
QList<const Name *> names = m_context.fullyQualifiedName(item.declaration());
|
||||
QList<const Name *> names
|
||||
= m_context.fullyQualifiedName(item.declaration(),
|
||||
LookupContext::HideInlineNamespaces);
|
||||
if (names.length() > longestName.length())
|
||||
longestName = names;
|
||||
}
|
||||
@@ -7574,8 +7576,9 @@ private:
|
||||
|
||||
const QList<LookupItem> lookups = m_context.lookup(wantToLookup, scope);
|
||||
if (!lookups.empty()) {
|
||||
QList<const Name *> fullName = m_context.fullyQualifiedName(
|
||||
lookups.first().declaration());
|
||||
QList<const Name *> fullName
|
||||
= m_context.fullyQualifiedName(lookups.first().declaration(),
|
||||
LookupContext::HideInlineNamespaces);
|
||||
const int currentNameCount = countNames(wantToLookup);
|
||||
const bool needNamespace = needMissingNamespaces(std::move(fullName),
|
||||
currentNameCount);
|
||||
|
Reference in New Issue
Block a user