2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-07-27 15:29:16 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2010-07-27 15:29:16 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-07-27 15:29:16 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2010-07-27 15:29:16 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-07-27 15:29:16 +02:00
|
|
|
|
2013-03-12 12:06:41 +01:00
|
|
|
#include "insertionpointlocator.h"
|
2013-03-27 18:54:03 +01:00
|
|
|
|
2015-03-05 08:22:48 +01:00
|
|
|
#include "cppprojectfile.h"
|
2012-10-10 21:00:48 +02:00
|
|
|
#include "cpptoolsreuse.h"
|
2012-01-20 14:43:21 +01:00
|
|
|
#include "symbolfinder.h"
|
2013-03-27 10:32:28 +04:00
|
|
|
#include "cpptoolsconstants.h"
|
2010-07-27 15:29:16 +02:00
|
|
|
|
2010-10-06 14:01:59 +02:00
|
|
|
#include <coreplugin/icore.h>
|
2013-03-27 18:54:03 +01:00
|
|
|
|
2020-11-12 12:12:13 +01:00
|
|
|
#include <cplusplus/ASTPath.h>
|
2015-03-05 08:22:48 +01:00
|
|
|
#include <cplusplus/LookupContext.h>
|
|
|
|
|
|
2012-01-23 17:44:49 +01:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2010-07-27 15:29:16 +02:00
|
|
|
using namespace CPlusPlus;
|
|
|
|
|
|
2021-02-01 15:44:56 +01:00
|
|
|
namespace CppTools {
|
2010-07-27 15:29:16 +02:00
|
|
|
namespace {
|
|
|
|
|
|
2010-07-28 12:09:50 +02:00
|
|
|
static int ordering(InsertionPointLocator::AccessSpec xsSpec)
|
2010-07-27 15:29:16 +02:00
|
|
|
{
|
2010-07-28 12:09:50 +02:00
|
|
|
static QList<InsertionPointLocator::AccessSpec> order = QList<InsertionPointLocator::AccessSpec>()
|
2010-07-27 15:29:16 +02:00
|
|
|
<< InsertionPointLocator::Public
|
|
|
|
|
<< InsertionPointLocator::PublicSlot
|
|
|
|
|
<< InsertionPointLocator::Signals
|
|
|
|
|
<< InsertionPointLocator::Protected
|
|
|
|
|
<< InsertionPointLocator::ProtectedSlot
|
|
|
|
|
<< InsertionPointLocator::PrivateSlot
|
|
|
|
|
<< InsertionPointLocator::Private
|
|
|
|
|
;
|
|
|
|
|
|
2010-07-28 12:09:50 +02:00
|
|
|
return order.indexOf(xsSpec);
|
2010-07-27 15:29:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct AccessRange
|
|
|
|
|
{
|
2019-07-24 18:40:10 +02:00
|
|
|
int start = 0;
|
2021-01-04 04:41:36 +01:00
|
|
|
int beforeStart = 0;
|
2019-01-14 01:40:53 +01:00
|
|
|
unsigned end = 0;
|
|
|
|
|
InsertionPointLocator::AccessSpec xsSpec = InsertionPointLocator::Invalid;
|
|
|
|
|
unsigned colonToken = 0;
|
2010-07-27 15:29:16 +02:00
|
|
|
|
2013-01-23 15:41:15 +01:00
|
|
|
AccessRange(unsigned start, unsigned end, InsertionPointLocator::AccessSpec xsSpec, unsigned colonToken)
|
2010-07-27 15:29:16 +02:00
|
|
|
: start(start)
|
|
|
|
|
, end(end)
|
|
|
|
|
, xsSpec(xsSpec)
|
2013-01-23 15:41:15 +01:00
|
|
|
, colonToken(colonToken)
|
2010-07-27 15:29:16 +02:00
|
|
|
{}
|
2013-01-23 15:41:15 +01:00
|
|
|
|
|
|
|
|
bool isEmpty() const
|
|
|
|
|
{
|
|
|
|
|
unsigned contentStart = 1 + (colonToken ? colonToken : start);
|
|
|
|
|
return contentStart == end;
|
|
|
|
|
}
|
2010-07-27 15:29:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class FindInClass: public ASTVisitor
|
|
|
|
|
{
|
|
|
|
|
public:
|
2021-01-04 04:41:36 +01:00
|
|
|
FindInClass(TranslationUnit *tu, const Class *clazz)
|
|
|
|
|
: ASTVisitor(tu)
|
2010-07-27 15:29:16 +02:00
|
|
|
, _clazz(clazz)
|
|
|
|
|
{}
|
|
|
|
|
|
2021-01-04 04:41:36 +01:00
|
|
|
ClassSpecifierAST *operator()()
|
2010-07-27 15:29:16 +02:00
|
|
|
{
|
2021-01-04 04:41:36 +01:00
|
|
|
_result = nullptr;
|
2010-07-27 15:29:16 +02:00
|
|
|
|
|
|
|
|
AST *ast = translationUnit()->ast();
|
|
|
|
|
accept(ast);
|
|
|
|
|
|
|
|
|
|
return _result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
using ASTVisitor::visit;
|
|
|
|
|
|
2019-01-14 01:40:53 +01:00
|
|
|
bool visit(ClassSpecifierAST *ast) override
|
2010-07-27 15:29:16 +02:00
|
|
|
{
|
|
|
|
|
if (!ast->lbrace_token || !ast->rbrace_token)
|
|
|
|
|
return true;
|
2014-05-15 12:00:13 -04:00
|
|
|
if (!ast->symbol || !ast->symbol->match(_clazz))
|
2010-07-27 15:29:16 +02:00
|
|
|
return true;
|
2021-01-04 04:41:36 +01:00
|
|
|
_result = ast;
|
2010-07-27 15:29:16 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-04 04:41:36 +01:00
|
|
|
private:
|
|
|
|
|
const Class *_clazz;
|
|
|
|
|
ClassSpecifierAST *_result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void findMatch(const QList<AccessRange> &ranges,
|
|
|
|
|
InsertionPointLocator::AccessSpec xsSpec,
|
|
|
|
|
InsertionPointLocator::Position positionInAccessSpec,
|
2021-02-05 13:12:41 +01:00
|
|
|
InsertionPointLocator::ForceAccessSpec forceAccessSpec,
|
2021-01-04 04:41:36 +01:00
|
|
|
unsigned &beforeToken,
|
|
|
|
|
bool &needsLeadingEmptyLine,
|
|
|
|
|
bool &needsPrefix,
|
|
|
|
|
bool &needsSuffix)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(!ranges.isEmpty(), return );
|
|
|
|
|
const int lastIndex = ranges.size() - 1;
|
|
|
|
|
const bool atEnd = positionInAccessSpec == InsertionPointLocator::AccessSpecEnd;
|
|
|
|
|
needsLeadingEmptyLine = false;
|
|
|
|
|
|
2021-02-05 13:12:41 +01:00
|
|
|
// Try an exact match. Ignore the default access spec unless there is no explicit one.
|
|
|
|
|
const int firstIndex = ranges.size() == 1
|
|
|
|
|
&& forceAccessSpec == InsertionPointLocator::ForceAccessSpec::No ? 0 : 1;
|
|
|
|
|
for (int i = lastIndex; i >= firstIndex; --i) {
|
2021-01-04 04:41:36 +01:00
|
|
|
const AccessRange &range = ranges.at(i);
|
|
|
|
|
if (range.xsSpec == xsSpec) {
|
|
|
|
|
beforeToken = atEnd ? range.end : range.beforeStart;
|
|
|
|
|
needsLeadingEmptyLine = !atEnd;
|
|
|
|
|
needsPrefix = false;
|
|
|
|
|
needsSuffix = (i != lastIndex);
|
|
|
|
|
return;
|
2010-07-27 15:29:16 +02:00
|
|
|
}
|
2021-01-04 04:41:36 +01:00
|
|
|
}
|
2010-07-27 15:29:16 +02:00
|
|
|
|
2021-01-04 04:41:36 +01:00
|
|
|
// try to find a fitting access spec to insert XXX:
|
|
|
|
|
for (int i = lastIndex; i > 0; --i) {
|
|
|
|
|
const AccessRange ¤t = ranges.at(i);
|
2010-07-28 12:09:50 +02:00
|
|
|
|
2021-01-04 04:41:36 +01:00
|
|
|
if (ordering(xsSpec) > ordering(current.xsSpec)) {
|
|
|
|
|
beforeToken = atEnd ? current.end : current.end - 1;
|
|
|
|
|
needsLeadingEmptyLine = !atEnd;
|
|
|
|
|
needsPrefix = true;
|
|
|
|
|
needsSuffix = (i != lastIndex);
|
|
|
|
|
return;
|
2010-07-27 15:29:16 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-04 04:41:36 +01:00
|
|
|
// otherwise:
|
|
|
|
|
beforeToken = atEnd ? ranges.first().end : ranges.first().end - 1;
|
|
|
|
|
needsLeadingEmptyLine = !ranges.first().isEmpty();
|
|
|
|
|
needsPrefix = true;
|
|
|
|
|
needsSuffix = (ranges.size() != 1);
|
|
|
|
|
}
|
2010-07-27 15:29:16 +02:00
|
|
|
|
2021-01-04 04:41:36 +01:00
|
|
|
QList<AccessRange> collectAccessRanges(const CPlusPlus::TranslationUnit *tu,
|
|
|
|
|
DeclarationListAST *decls,
|
|
|
|
|
InsertionPointLocator::AccessSpec initialXs,
|
|
|
|
|
int firstRangeStart,
|
|
|
|
|
int lastRangeEnd)
|
|
|
|
|
{
|
|
|
|
|
QList<AccessRange> ranges;
|
|
|
|
|
ranges.append(AccessRange(firstRangeStart, lastRangeEnd, initialXs, 0));
|
|
|
|
|
|
|
|
|
|
for (DeclarationListAST *iter = decls; iter; iter = iter->next) {
|
|
|
|
|
DeclarationAST *decl = iter->value;
|
|
|
|
|
|
|
|
|
|
if (AccessDeclarationAST *xsDecl = decl->asAccessDeclaration()) {
|
|
|
|
|
const unsigned token = xsDecl->access_specifier_token;
|
|
|
|
|
InsertionPointLocator::AccessSpec newXsSpec = initialXs;
|
|
|
|
|
bool isSlot = xsDecl->slots_token && tu->tokenKind(xsDecl->slots_token) == T_Q_SLOTS;
|
|
|
|
|
|
|
|
|
|
switch (tu->tokenKind(token)) {
|
|
|
|
|
case T_PUBLIC:
|
|
|
|
|
newXsSpec = isSlot ? InsertionPointLocator::PublicSlot
|
|
|
|
|
: InsertionPointLocator::Public;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case T_PROTECTED:
|
|
|
|
|
newXsSpec = isSlot ? InsertionPointLocator::ProtectedSlot
|
|
|
|
|
: InsertionPointLocator::Protected;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case T_PRIVATE:
|
|
|
|
|
newXsSpec = isSlot ? InsertionPointLocator::PrivateSlot
|
|
|
|
|
: InsertionPointLocator::Private;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case T_Q_SIGNALS:
|
|
|
|
|
newXsSpec = InsertionPointLocator::Signals;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case T_Q_SLOTS: {
|
|
|
|
|
newXsSpec = (InsertionPointLocator::AccessSpec)(ranges.last().xsSpec
|
|
|
|
|
| InsertionPointLocator::SlotBit);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2010-07-27 15:29:16 +02:00
|
|
|
|
2021-01-04 04:41:36 +01:00
|
|
|
default:
|
|
|
|
|
break;
|
2010-07-27 15:29:16 +02:00
|
|
|
}
|
|
|
|
|
|
2021-01-04 04:41:36 +01:00
|
|
|
if (newXsSpec != ranges.last().xsSpec || ranges.size() == 1) {
|
|
|
|
|
ranges.last().end = token;
|
|
|
|
|
AccessRange r(token, lastRangeEnd, newXsSpec, xsDecl->colon_token);
|
|
|
|
|
r.beforeStart = xsDecl->lastToken() - 1;
|
|
|
|
|
ranges.append(r);
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-07-27 15:29:16 +02:00
|
|
|
}
|
|
|
|
|
|
2021-01-04 04:41:36 +01:00
|
|
|
ranges.last().end = lastRangeEnd;
|
|
|
|
|
return ranges;
|
|
|
|
|
}
|
2010-07-27 15:29:16 +02:00
|
|
|
|
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
2019-02-07 10:09:21 +01:00
|
|
|
InsertionLocation::InsertionLocation() = default;
|
2010-07-27 15:29:16 +02:00
|
|
|
|
2010-09-30 12:09:38 +02:00
|
|
|
InsertionLocation::InsertionLocation(const QString &fileName,
|
|
|
|
|
const QString &prefix,
|
|
|
|
|
const QString &suffix,
|
2019-07-24 18:40:10 +02:00
|
|
|
int line, int column)
|
2010-09-30 12:09:38 +02:00
|
|
|
: m_fileName(fileName)
|
|
|
|
|
, m_prefix(prefix)
|
2010-07-28 12:09:50 +02:00
|
|
|
, m_suffix(suffix)
|
2010-07-27 15:29:16 +02:00
|
|
|
, m_line(line)
|
|
|
|
|
, m_column(column)
|
|
|
|
|
{}
|
|
|
|
|
|
2013-04-29 23:15:50 +02:00
|
|
|
QString InsertionPointLocator::accessSpecToString(InsertionPointLocator::AccessSpec xsSpec)
|
|
|
|
|
{
|
|
|
|
|
switch (xsSpec) {
|
|
|
|
|
default:
|
|
|
|
|
case InsertionPointLocator::Public:
|
2015-02-08 10:40:41 +01:00
|
|
|
return QLatin1String("public");
|
2013-04-29 23:15:50 +02:00
|
|
|
|
|
|
|
|
case InsertionPointLocator::Protected:
|
2015-02-08 10:40:41 +01:00
|
|
|
return QLatin1String("protected");
|
2013-04-29 23:15:50 +02:00
|
|
|
|
|
|
|
|
case InsertionPointLocator::Private:
|
2015-02-08 10:40:41 +01:00
|
|
|
return QLatin1String("private");
|
2013-04-29 23:15:50 +02:00
|
|
|
|
|
|
|
|
case InsertionPointLocator::PublicSlot:
|
2015-02-08 10:40:41 +01:00
|
|
|
return QLatin1String("public slots");
|
2013-04-29 23:15:50 +02:00
|
|
|
|
|
|
|
|
case InsertionPointLocator::ProtectedSlot:
|
2015-02-08 10:40:41 +01:00
|
|
|
return QLatin1String("protected slots");
|
2013-04-29 23:15:50 +02:00
|
|
|
|
|
|
|
|
case InsertionPointLocator::PrivateSlot:
|
2015-02-08 10:40:41 +01:00
|
|
|
return QLatin1String("private slots");
|
2013-04-29 23:15:50 +02:00
|
|
|
|
|
|
|
|
case InsertionPointLocator::Signals:
|
2015-02-08 10:40:41 +01:00
|
|
|
return QLatin1String("signals");
|
2013-04-29 23:15:50 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
InsertionPointLocator::InsertionPointLocator(const CppRefactoringChanges &refactoringChanges)
|
2010-09-30 12:09:38 +02:00
|
|
|
: m_refactoringChanges(refactoringChanges)
|
2010-07-27 15:29:16 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-27 18:01:04 +02:00
|
|
|
InsertionLocation InsertionPointLocator::methodDeclarationInClass(
|
|
|
|
|
const QString &fileName,
|
|
|
|
|
const Class *clazz,
|
2021-02-05 13:12:41 +01:00
|
|
|
AccessSpec xsSpec,
|
|
|
|
|
ForceAccessSpec forceAccessSpec) const
|
2010-07-27 15:29:16 +02:00
|
|
|
{
|
2021-05-28 12:02:36 +02:00
|
|
|
const Document::Ptr doc = m_refactoringChanges.file(Utils::FilePath::fromString(fileName))
|
|
|
|
|
->cppDocument();
|
2010-09-27 18:01:04 +02:00
|
|
|
if (doc) {
|
2021-01-04 04:41:36 +01:00
|
|
|
FindInClass find(doc->translationUnit(), clazz);
|
|
|
|
|
ClassSpecifierAST *classAST = find();
|
2021-02-05 13:12:41 +01:00
|
|
|
return methodDeclarationInClass(doc->translationUnit(), classAST, xsSpec, AccessSpecEnd,
|
|
|
|
|
forceAccessSpec);
|
2010-09-27 18:01:04 +02:00
|
|
|
} else {
|
|
|
|
|
return InsertionLocation();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-05 13:12:41 +01:00
|
|
|
InsertionLocation InsertionPointLocator::methodDeclarationInClass(const TranslationUnit *tu,
|
2021-01-04 04:41:36 +01:00
|
|
|
const ClassSpecifierAST *clazz,
|
|
|
|
|
InsertionPointLocator::AccessSpec xsSpec,
|
2021-02-05 13:12:41 +01:00
|
|
|
Position pos,
|
|
|
|
|
ForceAccessSpec forceAccessSpec) const
|
2021-01-04 04:41:36 +01:00
|
|
|
{
|
|
|
|
|
if (!clazz)
|
|
|
|
|
return {};
|
|
|
|
|
QList<AccessRange> ranges = collectAccessRanges(tu,
|
|
|
|
|
clazz->member_specifier_list,
|
|
|
|
|
tu->tokenKind(clazz->classkey_token) == T_CLASS
|
|
|
|
|
? InsertionPointLocator::Private
|
|
|
|
|
: InsertionPointLocator::Public,
|
|
|
|
|
clazz->lbrace_token,
|
|
|
|
|
clazz->rbrace_token);
|
|
|
|
|
|
|
|
|
|
unsigned beforeToken = 0;
|
|
|
|
|
bool needsLeadingEmptyLine = false;
|
|
|
|
|
bool needsPrefix = false;
|
|
|
|
|
bool needsSuffix = false;
|
2021-02-05 13:12:41 +01:00
|
|
|
findMatch(ranges, xsSpec, pos, forceAccessSpec, beforeToken, needsLeadingEmptyLine,
|
|
|
|
|
needsPrefix, needsSuffix);
|
2021-01-04 04:41:36 +01:00
|
|
|
|
|
|
|
|
int line = 0, column = 0;
|
|
|
|
|
if (pos == InsertionPointLocator::AccessSpecEnd)
|
|
|
|
|
tu->getTokenStartPosition(beforeToken, &line, &column);
|
|
|
|
|
else
|
|
|
|
|
tu->getTokenEndPosition(beforeToken, &line, &column);
|
|
|
|
|
|
|
|
|
|
QString prefix;
|
|
|
|
|
if (needsLeadingEmptyLine)
|
|
|
|
|
prefix += QLatin1String("\n");
|
|
|
|
|
if (needsPrefix)
|
|
|
|
|
prefix += InsertionPointLocator::accessSpecToString(xsSpec) + QLatin1String(":\n");
|
|
|
|
|
|
|
|
|
|
QString suffix;
|
|
|
|
|
if (needsSuffix)
|
|
|
|
|
suffix = QLatin1Char('\n');
|
|
|
|
|
const QString fileName = QString::fromUtf8(tu->fileName(), tu->fileNameLength());
|
|
|
|
|
return InsertionLocation(fileName, prefix, suffix, line, column);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
InsertionPointLocator::AccessSpec symbolsAccessSpec(Symbol *symbol)
|
|
|
|
|
{
|
|
|
|
|
if (symbol->isPrivate())
|
|
|
|
|
return InsertionPointLocator::Private;
|
|
|
|
|
if (symbol->isProtected())
|
|
|
|
|
return InsertionPointLocator::Protected;
|
|
|
|
|
if (symbol->isPublic())
|
|
|
|
|
return InsertionPointLocator::Public;
|
|
|
|
|
return InsertionPointLocator::Invalid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
InsertionLocation InsertionPointLocator::constructorDeclarationInClass(
|
|
|
|
|
const CPlusPlus::TranslationUnit *tu,
|
|
|
|
|
const ClassSpecifierAST *clazz,
|
|
|
|
|
InsertionPointLocator::AccessSpec xsSpec,
|
|
|
|
|
int constructorArgumentCount) const
|
|
|
|
|
{
|
|
|
|
|
std::map<int, std::pair<DeclarationAST *, DeclarationAST *>> constructors;
|
|
|
|
|
for (DeclarationAST *rootDecl : clazz->member_specifier_list) {
|
|
|
|
|
if (SimpleDeclarationAST *ast = rootDecl->asSimpleDeclaration()) {
|
|
|
|
|
if (!ast->symbols)
|
|
|
|
|
continue;
|
|
|
|
|
if (symbolsAccessSpec(ast->symbols->value) != xsSpec)
|
|
|
|
|
continue;
|
|
|
|
|
if (ast->symbols->value->name() != clazz->name->name)
|
|
|
|
|
continue;
|
|
|
|
|
for (DeclaratorAST *d : ast->declarator_list) {
|
|
|
|
|
for (PostfixDeclaratorAST *decl : d->postfix_declarator_list) {
|
|
|
|
|
if (FunctionDeclaratorAST *func = decl->asFunctionDeclarator()) {
|
|
|
|
|
int params = 0;
|
|
|
|
|
if (func->parameter_declaration_clause) {
|
|
|
|
|
params = size(
|
|
|
|
|
func->parameter_declaration_clause->parameter_declaration_list);
|
|
|
|
|
}
|
|
|
|
|
auto &entry = constructors[params];
|
|
|
|
|
if (!entry.first)
|
|
|
|
|
entry.first = rootDecl;
|
|
|
|
|
entry.second = rootDecl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (constructors.empty())
|
|
|
|
|
return methodDeclarationInClass(tu, clazz, xsSpec, AccessSpecBegin);
|
|
|
|
|
|
|
|
|
|
auto iter = constructors.lower_bound(constructorArgumentCount);
|
|
|
|
|
if (iter == constructors.end()) {
|
|
|
|
|
// we have a constructor with x arguments but there are only ones with < x arguments
|
|
|
|
|
--iter; // select greatest one (in terms of argument count)
|
|
|
|
|
}
|
|
|
|
|
const QString fileName = QString::fromUtf8(tu->fileName(), tu->fileNameLength());
|
|
|
|
|
int line, column;
|
|
|
|
|
if (iter->first <= constructorArgumentCount) {
|
|
|
|
|
tu->getTokenEndPosition(iter->second.second->lastToken() - 1, &line, &column);
|
|
|
|
|
return InsertionLocation(fileName, "\n", "", line, column);
|
|
|
|
|
}
|
|
|
|
|
// before iter
|
|
|
|
|
// end pos of firstToken-1 instead of start pos of firstToken to skip leading commend
|
|
|
|
|
tu->getTokenEndPosition(iter->second.first->firstToken() - 1, &line, &column);
|
|
|
|
|
return InsertionLocation(fileName, "\n", "", line, column);
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-28 22:11:38 +02:00
|
|
|
namespace {
|
|
|
|
|
template <class Key, class Value>
|
|
|
|
|
class HighestValue
|
|
|
|
|
{
|
|
|
|
|
Key _key;
|
|
|
|
|
Value _value;
|
2019-02-07 10:09:21 +01:00
|
|
|
bool _set = false;
|
2011-07-28 22:11:38 +02:00
|
|
|
public:
|
2019-02-07 10:09:21 +01:00
|
|
|
HighestValue() = default;
|
2011-07-28 22:11:38 +02:00
|
|
|
|
|
|
|
|
HighestValue(const Key &initialKey, const Value &initialValue)
|
|
|
|
|
: _key(initialKey)
|
|
|
|
|
, _value(initialValue)
|
|
|
|
|
, _set(true)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
void maybeSet(const Key &key, const Value &value)
|
|
|
|
|
{
|
|
|
|
|
if (!_set || key > _key) {
|
|
|
|
|
_value = value;
|
|
|
|
|
_key = key;
|
|
|
|
|
_set = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Value &get() const
|
|
|
|
|
{
|
2012-01-24 13:50:35 +01:00
|
|
|
QTC_CHECK(_set);
|
2011-07-28 22:11:38 +02:00
|
|
|
return _value;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class FindMethodDefinitionInsertPoint : protected ASTVisitor
|
|
|
|
|
{
|
|
|
|
|
QList<const Identifier *> _namespaceNames;
|
2017-06-01 13:33:47 +02:00
|
|
|
int _currentDepth = 0;
|
2013-09-06 09:31:21 +02:00
|
|
|
HighestValue<int, unsigned> _bestToken;
|
2011-07-28 22:11:38 +02:00
|
|
|
|
|
|
|
|
public:
|
2019-01-14 01:40:53 +01:00
|
|
|
explicit FindMethodDefinitionInsertPoint(TranslationUnit *translationUnit)
|
2011-07-28 22:11:38 +02:00
|
|
|
: ASTVisitor(translationUnit)
|
|
|
|
|
{}
|
|
|
|
|
|
2019-07-24 18:40:10 +02:00
|
|
|
void operator()(Symbol *decl, int *line, int *column)
|
2011-07-28 22:11:38 +02:00
|
|
|
{
|
|
|
|
|
// default to end of file
|
2013-09-06 09:31:21 +02:00
|
|
|
const unsigned lastToken = translationUnit()->ast()->lastToken();
|
|
|
|
|
_bestToken.maybeSet(-1, lastToken);
|
2012-01-31 13:41:39 +01:00
|
|
|
|
2013-09-06 09:31:21 +02:00
|
|
|
if (lastToken >= 2) {
|
2012-01-31 13:41:39 +01:00
|
|
|
QList<const Name *> names = LookupContext::fullyQualifiedName(decl);
|
|
|
|
|
foreach (const Name *name, names) {
|
|
|
|
|
const Identifier *id = name->asNameId();
|
|
|
|
|
if (!id)
|
|
|
|
|
break;
|
|
|
|
|
_namespaceNames += id;
|
|
|
|
|
}
|
|
|
|
|
_currentDepth = 0;
|
|
|
|
|
|
|
|
|
|
accept(translationUnit()->ast());
|
|
|
|
|
}
|
2013-09-06 09:31:21 +02:00
|
|
|
|
|
|
|
|
if (lastToken == _bestToken.get()) // No matching namespace found
|
|
|
|
|
translationUnit()->getTokenStartPosition(lastToken, line, column);
|
|
|
|
|
else // Insert at end of matching namespace
|
|
|
|
|
translationUnit()->getTokenEndPosition(_bestToken.get(), line, column);
|
2011-07-28 22:11:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
2019-01-14 01:40:53 +01:00
|
|
|
bool preVisit(AST *ast) override
|
2011-07-28 22:11:38 +02:00
|
|
|
{
|
|
|
|
|
return ast->asNamespace() || ast->asTranslationUnit() || ast->asLinkageBody();
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 01:40:53 +01:00
|
|
|
bool visit(NamespaceAST *ast) override
|
2011-07-28 22:11:38 +02:00
|
|
|
{
|
|
|
|
|
if (_currentDepth >= _namespaceNames.size())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// ignore anonymous namespaces
|
|
|
|
|
if (!ast->identifier_token)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
const Identifier *name = translationUnit()->identifier(ast->identifier_token);
|
|
|
|
|
if (!name->equalTo(_namespaceNames.at(_currentDepth)))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// found a good namespace
|
|
|
|
|
_bestToken.maybeSet(_currentDepth, ast->lastToken() - 2);
|
|
|
|
|
|
|
|
|
|
++_currentDepth;
|
|
|
|
|
accept(ast->linkage_body);
|
|
|
|
|
--_currentDepth;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
2011-08-29 09:53:52 +02:00
|
|
|
|
|
|
|
|
class FindFunctionDefinition : protected ASTVisitor
|
|
|
|
|
{
|
2016-11-29 15:17:44 +01:00
|
|
|
FunctionDefinitionAST *_result = nullptr;
|
2019-07-24 18:40:10 +02:00
|
|
|
int _line = 0;
|
|
|
|
|
int _column = 0;
|
2011-08-29 09:53:52 +02:00
|
|
|
public:
|
2019-01-14 01:40:53 +01:00
|
|
|
explicit FindFunctionDefinition(TranslationUnit *translationUnit)
|
2011-08-29 09:53:52 +02:00
|
|
|
: ASTVisitor(translationUnit)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-24 18:40:10 +02:00
|
|
|
FunctionDefinitionAST *operator()(int line, int column)
|
2011-08-29 09:53:52 +02:00
|
|
|
{
|
2016-11-29 15:17:44 +01:00
|
|
|
_result = nullptr;
|
2011-08-29 09:53:52 +02:00
|
|
|
_line = line;
|
|
|
|
|
_column = column;
|
|
|
|
|
accept(translationUnit()->ast());
|
|
|
|
|
return _result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
2019-01-14 01:40:53 +01:00
|
|
|
bool preVisit(AST *ast) override
|
2011-08-29 09:53:52 +02:00
|
|
|
{
|
|
|
|
|
if (_result)
|
|
|
|
|
return false;
|
2019-07-24 18:40:10 +02:00
|
|
|
int line, column;
|
2011-08-29 09:53:52 +02:00
|
|
|
translationUnit()->getTokenStartPosition(ast->firstToken(), &line, &column);
|
|
|
|
|
if (line > _line || (line == _line && column > _column))
|
|
|
|
|
return false;
|
|
|
|
|
translationUnit()->getTokenEndPosition(ast->lastToken() - 1, &line, &column);
|
|
|
|
|
if (line < _line || (line == _line && column < _column))
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 01:40:53 +01:00
|
|
|
bool visit(FunctionDefinitionAST *ast) override
|
2011-08-29 09:53:52 +02:00
|
|
|
{
|
|
|
|
|
_result = ast;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2011-07-28 22:11:38 +02:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
2011-08-29 09:53:52 +02:00
|
|
|
static Declaration *isNonVirtualFunctionDeclaration(Symbol *s)
|
|
|
|
|
{
|
|
|
|
|
if (!s)
|
2019-01-14 01:40:53 +01:00
|
|
|
return nullptr;
|
2011-08-29 09:53:52 +02:00
|
|
|
Declaration *declaration = s->asDeclaration();
|
|
|
|
|
if (!declaration)
|
2019-01-14 01:40:53 +01:00
|
|
|
return nullptr;
|
2011-08-29 09:53:52 +02:00
|
|
|
Function *type = s->type()->asFunctionType();
|
|
|
|
|
if (!type || type->isPureVirtual())
|
2019-01-14 01:40:53 +01:00
|
|
|
return nullptr;
|
2011-08-29 09:53:52 +02:00
|
|
|
return declaration;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-07 16:44:08 +02:00
|
|
|
static InsertionLocation nextToSurroundingDefinitions(Symbol *declaration,
|
|
|
|
|
const CppRefactoringChanges &changes,
|
|
|
|
|
const QString& destinationFile)
|
2011-08-29 09:53:52 +02:00
|
|
|
{
|
|
|
|
|
InsertionLocation noResult;
|
|
|
|
|
Class *klass = declaration->enclosingClass();
|
|
|
|
|
if (!klass)
|
|
|
|
|
return noResult;
|
|
|
|
|
|
|
|
|
|
// find the index of declaration
|
|
|
|
|
int declIndex = -1;
|
2019-07-24 18:40:10 +02:00
|
|
|
for (int i = 0; i < klass->memberCount(); ++i) {
|
2011-08-29 09:53:52 +02:00
|
|
|
Symbol *s = klass->memberAt(i);
|
|
|
|
|
if (s == declaration) {
|
|
|
|
|
declIndex = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (declIndex == -1)
|
|
|
|
|
return noResult;
|
|
|
|
|
|
2013-06-07 16:44:08 +02:00
|
|
|
// scan preceding declarations for a function declaration (and see if it is defined)
|
2015-02-04 17:01:07 +02:00
|
|
|
SymbolFinder symbolFinder;
|
2019-01-14 01:40:53 +01:00
|
|
|
Function *definitionFunction = nullptr;
|
2011-08-29 09:53:52 +02:00
|
|
|
QString prefix, suffix;
|
2019-01-14 01:40:53 +01:00
|
|
|
Declaration *surroundingFunctionDecl = nullptr;
|
2011-08-29 09:53:52 +02:00
|
|
|
for (int i = declIndex - 1; i >= 0; --i) {
|
|
|
|
|
Symbol *s = klass->memberAt(i);
|
2013-07-24 14:41:01 +02:00
|
|
|
if (s->isGenerated() || !(surroundingFunctionDecl = isNonVirtualFunctionDeclaration(s)))
|
2013-06-07 16:44:08 +02:00
|
|
|
continue;
|
|
|
|
|
if ((definitionFunction = symbolFinder.findMatchingDefinition(surroundingFunctionDecl,
|
|
|
|
|
changes.snapshot())))
|
|
|
|
|
{
|
|
|
|
|
if (destinationFile.isEmpty() || destinationFile == QString::fromUtf8(
|
|
|
|
|
definitionFunction->fileName(), definitionFunction->fileNameLength())) {
|
|
|
|
|
prefix = QLatin1String("\n\n");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-01-14 01:40:53 +01:00
|
|
|
definitionFunction = nullptr;
|
2011-08-29 09:53:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
2013-06-07 16:44:08 +02:00
|
|
|
if (!definitionFunction) {
|
2011-08-29 09:53:52 +02:00
|
|
|
// try to find one below
|
2019-07-24 18:40:10 +02:00
|
|
|
for (int i = declIndex + 1; i < klass->memberCount(); ++i) {
|
2011-08-29 09:53:52 +02:00
|
|
|
Symbol *s = klass->memberAt(i);
|
|
|
|
|
surroundingFunctionDecl = isNonVirtualFunctionDeclaration(s);
|
2013-06-07 16:44:08 +02:00
|
|
|
if (!surroundingFunctionDecl)
|
|
|
|
|
continue;
|
|
|
|
|
if ((definitionFunction = symbolFinder.findMatchingDefinition(surroundingFunctionDecl,
|
|
|
|
|
changes.snapshot())))
|
|
|
|
|
{
|
|
|
|
|
if (destinationFile.isEmpty() || destinationFile == QString::fromUtf8(
|
|
|
|
|
definitionFunction->fileName(), definitionFunction->fileNameLength())) {
|
|
|
|
|
suffix = QLatin1String("\n\n");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-01-14 01:40:53 +01:00
|
|
|
definitionFunction = nullptr;
|
2011-08-29 09:53:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-14 11:04:38 +02:00
|
|
|
if (!definitionFunction)
|
2011-08-29 09:53:52 +02:00
|
|
|
return noResult;
|
|
|
|
|
|
2019-07-24 18:40:10 +02:00
|
|
|
int line, column;
|
2011-08-29 09:53:52 +02:00
|
|
|
if (suffix.isEmpty()) {
|
2013-05-14 11:04:38 +02:00
|
|
|
Document::Ptr targetDoc = changes.snapshot().document(QString::fromUtf8(definitionFunction->fileName()));
|
2011-08-29 09:53:52 +02:00
|
|
|
if (!targetDoc)
|
|
|
|
|
return noResult;
|
|
|
|
|
|
|
|
|
|
targetDoc->translationUnit()->getPosition(definitionFunction->endOffset(), &line, &column);
|
|
|
|
|
} else {
|
|
|
|
|
// we don't have an offset to the start of the function definition, so we need to manually find it...
|
2021-05-28 12:02:36 +02:00
|
|
|
CppRefactoringFilePtr targetFile = changes.file(
|
|
|
|
|
Utils::FilePath::fromString(QString::fromUtf8(definitionFunction->fileName())));
|
2011-08-29 09:53:52 +02:00
|
|
|
if (!targetFile->isValid())
|
|
|
|
|
return noResult;
|
|
|
|
|
|
|
|
|
|
FindFunctionDefinition finder(targetFile->cppDocument()->translationUnit());
|
2013-05-14 11:04:38 +02:00
|
|
|
FunctionDefinitionAST *functionDefinition = finder(definitionFunction->line(), definitionFunction->column());
|
2011-08-29 09:53:52 +02:00
|
|
|
if (!functionDefinition)
|
|
|
|
|
return noResult;
|
|
|
|
|
|
|
|
|
|
targetFile->cppDocument()->translationUnit()->getTokenStartPosition(functionDefinition->firstToken(), &line, &column);
|
2020-11-12 12:12:13 +01:00
|
|
|
const QList<AST *> path = ASTPath(targetFile->cppDocument())(line, column);
|
|
|
|
|
for (auto it = path.rbegin(); it != path.rend(); ++it) {
|
|
|
|
|
if (const auto templateDecl = (*it)->asTemplateDeclaration()) {
|
|
|
|
|
if (templateDecl->declaration == functionDefinition) {
|
|
|
|
|
targetFile->cppDocument()->translationUnit()->getTokenStartPosition(
|
|
|
|
|
templateDecl->firstToken(), &line, &column);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-08-29 09:53:52 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-14 11:04:38 +02:00
|
|
|
return InsertionLocation(QString::fromUtf8(definitionFunction->fileName()), prefix, suffix, line, column);
|
2011-08-29 09:53:52 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-22 17:19:28 +02:00
|
|
|
const QList<InsertionLocation> InsertionPointLocator::methodDefinition(
|
|
|
|
|
Symbol *declaration, bool useSymbolFinder, const QString &destinationFile) const
|
2010-09-27 18:01:04 +02:00
|
|
|
{
|
|
|
|
|
QList<InsertionLocation> result;
|
2010-09-30 12:09:38 +02:00
|
|
|
if (!declaration)
|
|
|
|
|
return result;
|
2010-09-27 18:01:04 +02:00
|
|
|
|
2013-05-30 17:32:15 +02:00
|
|
|
if (useSymbolFinder) {
|
2015-02-04 17:01:07 +02:00
|
|
|
SymbolFinder symbolFinder;
|
2013-05-30 17:32:15 +02:00
|
|
|
if (symbolFinder.findMatchingDefinition(declaration, m_refactoringChanges.snapshot(), true))
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2011-08-29 09:53:52 +02:00
|
|
|
|
2013-06-07 16:44:08 +02:00
|
|
|
const InsertionLocation location = nextToSurroundingDefinitions(declaration,
|
|
|
|
|
m_refactoringChanges,
|
|
|
|
|
destinationFile);
|
2011-08-29 09:53:52 +02:00
|
|
|
if (location.isValid()) {
|
|
|
|
|
result += location;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-06 14:01:59 +02:00
|
|
|
const QString declFileName = QString::fromUtf8(declaration->fileName(),
|
|
|
|
|
declaration->fileNameLength());
|
|
|
|
|
QString target = declFileName;
|
2013-03-27 10:32:28 +04:00
|
|
|
if (!ProjectFile::isSource(ProjectFile::classify(declFileName))) {
|
2012-10-10 21:00:48 +02:00
|
|
|
QString candidate = CppTools::correspondingHeaderOrSource(declFileName);
|
2010-10-07 15:22:32 +02:00
|
|
|
if (!candidate.isEmpty())
|
|
|
|
|
target = candidate;
|
2010-10-06 14:01:59 +02:00
|
|
|
}
|
2010-09-27 18:01:04 +02:00
|
|
|
|
2021-05-28 12:02:36 +02:00
|
|
|
CppRefactoringFilePtr targetFile = m_refactoringChanges.file(
|
|
|
|
|
Utils::FilePath::fromString(target));
|
2012-01-31 13:41:39 +01:00
|
|
|
Document::Ptr doc = targetFile->cppDocument();
|
2010-09-30 12:09:38 +02:00
|
|
|
if (doc.isNull())
|
|
|
|
|
return result;
|
|
|
|
|
|
2019-07-24 18:40:10 +02:00
|
|
|
int line = 0, column = 0;
|
2011-07-28 22:11:38 +02:00
|
|
|
FindMethodDefinitionInsertPoint finder(doc->translationUnit());
|
|
|
|
|
finder(declaration, &line, &column);
|
2010-09-30 12:09:38 +02:00
|
|
|
|
2012-09-12 12:21:44 +02:00
|
|
|
// Force empty lines before and after the new definition.
|
|
|
|
|
QString prefix;
|
2012-01-31 13:41:39 +01:00
|
|
|
QString suffix;
|
2012-09-12 12:21:44 +02:00
|
|
|
if (!line) {
|
|
|
|
|
// Totally empty file.
|
|
|
|
|
line = 1;
|
|
|
|
|
column = 1;
|
|
|
|
|
prefix = suffix = QLatin1Char('\n');
|
2012-01-31 13:41:39 +01:00
|
|
|
} else {
|
2012-09-12 12:21:44 +02:00
|
|
|
QTC_ASSERT(column, return result);
|
|
|
|
|
|
|
|
|
|
int firstNonSpace = targetFile->position(line, column);
|
2015-02-01 11:32:45 +01:00
|
|
|
prefix = QLatin1String("\n\n");
|
|
|
|
|
// Only one new line if at the end of file
|
|
|
|
|
if (const QTextDocument *doc = targetFile->document()) {
|
|
|
|
|
if (firstNonSpace + 1 == doc->characterCount() /* + 1 because zero based index */
|
|
|
|
|
&& doc->characterAt(firstNonSpace) == QChar::ParagraphSeparator) {
|
|
|
|
|
prefix = QLatin1String("\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-12 12:21:44 +02:00
|
|
|
QChar c = targetFile->charAt(firstNonSpace);
|
|
|
|
|
while (c == QLatin1Char(' ') || c == QLatin1Char('\t')) {
|
|
|
|
|
++firstNonSpace;
|
|
|
|
|
c = targetFile->charAt(firstNonSpace);
|
|
|
|
|
}
|
|
|
|
|
if (targetFile->charAt(firstNonSpace) != QChar::ParagraphSeparator) {
|
|
|
|
|
suffix.append(QLatin1String("\n\n"));
|
|
|
|
|
} else {
|
|
|
|
|
++firstNonSpace;
|
|
|
|
|
if (targetFile->charAt(firstNonSpace) != QChar::ParagraphSeparator)
|
|
|
|
|
suffix.append(QLatin1Char('\n'));
|
|
|
|
|
}
|
2012-01-31 13:41:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result += InsertionLocation(target, prefix, suffix, line, column);
|
2010-09-27 18:01:04 +02:00
|
|
|
|
|
|
|
|
return result;
|
2010-07-27 15:29:16 +02:00
|
|
|
}
|
2021-02-01 15:44:56 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief getListOfMissingNamespacesForLocation checks which namespaces are present at a given
|
|
|
|
|
* location and returns a list of namespace names that are needed to get the wanted namespace
|
|
|
|
|
* @param file The file of the location
|
|
|
|
|
* @param wantedNamespaces the namespace as list that should exists at the insert location
|
|
|
|
|
* @param loc The location that should be checked (the namespaces should be available there)
|
|
|
|
|
* @return A list of namespaces that are missing to reach the wanted namespaces.
|
|
|
|
|
*/
|
|
|
|
|
static QStringList getListOfMissingNamespacesForLocation(const CppRefactoringFile *file,
|
|
|
|
|
const QStringList &wantedNamespaces,
|
|
|
|
|
InsertionLocation loc)
|
|
|
|
|
{
|
|
|
|
|
NSCheckerVisitor visitor(file, wantedNamespaces, file->position(loc.line(), loc.column()));
|
|
|
|
|
visitor.accept(file->cppDocument()->translationUnit()->ast());
|
|
|
|
|
return visitor.remainingNamespaces();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief getNamespaceNames Returns a list of namespaces for an enclosing namespaces of a
|
|
|
|
|
* namespace (contains the namespace itself)
|
|
|
|
|
* @param firstNamespace the starting namespace (included in the list)
|
|
|
|
|
* @return the enclosing namespaces, the outermost namespace is at the first index, the innermost
|
|
|
|
|
* at the last index
|
|
|
|
|
*/
|
|
|
|
|
QStringList getNamespaceNames(const Namespace *firstNamespace)
|
|
|
|
|
{
|
|
|
|
|
QStringList namespaces;
|
|
|
|
|
for (const Namespace *scope = firstNamespace; scope; scope = scope->enclosingNamespace()) {
|
|
|
|
|
if (scope->name() && scope->name()->identifier()) {
|
|
|
|
|
namespaces.prepend(QString::fromUtf8(scope->name()->identifier()->chars(),
|
|
|
|
|
scope->name()->identifier()->size()));
|
|
|
|
|
} else {
|
|
|
|
|
namespaces.prepend(""); // an unnamed namespace
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
namespaces.pop_front(); // the "global namespace" is one namespace, but not an unnamed
|
|
|
|
|
return namespaces;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief getNamespaceNames Returns a list of enclosing namespaces for a symbol
|
|
|
|
|
* @param symbol a symbol from which we want the enclosing namespaces
|
|
|
|
|
* @return the enclosing namespaces, the outermost namespace is at the first index, the innermost
|
|
|
|
|
* at the last index
|
|
|
|
|
*/
|
|
|
|
|
QStringList getNamespaceNames(const Symbol *symbol)
|
|
|
|
|
{
|
|
|
|
|
return getNamespaceNames(symbol->enclosingNamespace());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
InsertionLocation insertLocationForMethodDefinition(Symbol *symbol,
|
|
|
|
|
const bool useSymbolFinder,
|
|
|
|
|
NamespaceHandling namespaceHandling,
|
|
|
|
|
const CppRefactoringChanges &refactoring,
|
|
|
|
|
const QString &fileName,
|
|
|
|
|
QStringList *insertedNamespaces)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(symbol, return InsertionLocation());
|
|
|
|
|
|
2021-05-28 12:02:36 +02:00
|
|
|
CppRefactoringFilePtr file = refactoring.file(Utils::FilePath::fromString(fileName));
|
2021-02-01 15:44:56 +01:00
|
|
|
QStringList requiredNamespaces;
|
|
|
|
|
if (namespaceHandling == NamespaceHandling::CreateMissing) {
|
|
|
|
|
requiredNamespaces = getNamespaceNames(symbol);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try to find optimal location
|
|
|
|
|
// FIXME: The locator should not return a valid location if the namespaces don't match
|
|
|
|
|
// (or provide enough context).
|
|
|
|
|
const InsertionPointLocator locator(refactoring);
|
|
|
|
|
const QList<InsertionLocation> list
|
|
|
|
|
= locator.methodDefinition(symbol, useSymbolFinder, fileName);
|
|
|
|
|
const bool isHeader = ProjectFile::isHeader(ProjectFile::classify(fileName));
|
|
|
|
|
const bool hasIncludeGuard = isHeader
|
|
|
|
|
&& !file->cppDocument()->includeGuardMacroName().isEmpty();
|
|
|
|
|
int lastLine;
|
|
|
|
|
if (hasIncludeGuard) {
|
|
|
|
|
const TranslationUnit * const tu = file->cppDocument()->translationUnit();
|
|
|
|
|
tu->getTokenStartPosition(tu->ast()->lastToken(), &lastLine);
|
|
|
|
|
}
|
|
|
|
|
int i = 0;
|
|
|
|
|
for ( ; i < list.count(); ++i) {
|
|
|
|
|
InsertionLocation location = list.at(i);
|
|
|
|
|
if (!location.isValid() || location.fileName() != fileName)
|
|
|
|
|
continue;
|
|
|
|
|
if (hasIncludeGuard && location.line() == lastLine)
|
|
|
|
|
continue;
|
|
|
|
|
if (!requiredNamespaces.isEmpty()) {
|
|
|
|
|
QStringList missing = getListOfMissingNamespacesForLocation(file.get(),
|
|
|
|
|
requiredNamespaces,
|
|
|
|
|
location);
|
|
|
|
|
if (!missing.isEmpty())
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
return location;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ...failed,
|
|
|
|
|
// if class member try to get position right after class
|
|
|
|
|
int line = 0, column = 0;
|
|
|
|
|
if (Class *clazz = symbol->enclosingClass()) {
|
|
|
|
|
if (symbol->fileName() == fileName.toUtf8()) {
|
|
|
|
|
file->cppDocument()->translationUnit()->getPosition(clazz->endOffset(), &line, &column);
|
|
|
|
|
if (line != 0) {
|
|
|
|
|
++column; // Skipping the ";"
|
|
|
|
|
return InsertionLocation(fileName, QLatin1String("\n\n"), QLatin1String(""),
|
|
|
|
|
line, column);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// fall through: position at end of file, unless we find a matching namespace
|
|
|
|
|
const QTextDocument *doc = file->document();
|
|
|
|
|
int pos = qMax(0, doc->characterCount() - 1);
|
|
|
|
|
QString prefix = "\n\n";
|
|
|
|
|
QString suffix = "\n\n";
|
|
|
|
|
NSVisitor visitor(file.data(), requiredNamespaces, pos);
|
|
|
|
|
visitor.accept(file->cppDocument()->translationUnit()->ast());
|
|
|
|
|
if (visitor.enclosingNamespace())
|
|
|
|
|
pos = file->startOf(visitor.enclosingNamespace()->linkage_body) + 1;
|
|
|
|
|
for (const QString &ns : visitor.remainingNamespaces()) {
|
|
|
|
|
prefix += "namespace " + ns + " {\n";
|
|
|
|
|
suffix += "}\n";
|
|
|
|
|
}
|
|
|
|
|
if (insertedNamespaces)
|
|
|
|
|
*insertedNamespaces = visitor.remainingNamespaces();
|
|
|
|
|
|
|
|
|
|
//TODO watch for moc-includes
|
|
|
|
|
|
|
|
|
|
file->lineAndColumn(pos, &line, &column);
|
|
|
|
|
return InsertionLocation(fileName, prefix, suffix, line, column);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace CppTools;
|