2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2012-03-02 16:26:22 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2012-03-02 16:26:22 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2012-03-02 16:26:22 +01: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.
|
2012-03-02 16:26:22 +01: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.
|
2012-03-02 16:26:22 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2012-03-02 16:26:22 +01:00
|
|
|
|
2012-01-20 14:43:21 +01:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
|
#pragma warning(disable:4996)
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include "symbolfinder.h"
|
|
|
|
|
|
2015-10-08 13:32:36 +02:00
|
|
|
#include "cppmodelmanager.h"
|
|
|
|
|
|
2013-03-27 18:54:03 +01:00
|
|
|
#include <cplusplus/LookupContext.h>
|
2012-01-20 14:43:21 +01:00
|
|
|
|
2012-01-23 17:44:49 +01:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2013-03-27 18:54:03 +01:00
|
|
|
#include <QDebug>
|
2020-06-11 16:43:25 +02:00
|
|
|
#include <QPair>
|
2013-03-27 18:54:03 +01:00
|
|
|
|
2012-01-20 14:43:21 +01:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
|
|
using namespace CPlusPlus;
|
|
|
|
|
using namespace CppTools;
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2020-08-10 18:10:28 +02:00
|
|
|
struct Hit {
|
|
|
|
|
Hit(Function *func, bool exact) : func(func), exact(exact) {}
|
|
|
|
|
Hit() = default;
|
|
|
|
|
|
|
|
|
|
Function *func = nullptr;
|
|
|
|
|
bool exact = false;
|
|
|
|
|
};
|
|
|
|
|
|
2012-01-20 14:43:21 +01:00
|
|
|
class FindMatchingDefinition: public SymbolVisitor
|
|
|
|
|
{
|
2019-01-14 01:40:53 +01:00
|
|
|
Symbol *_declaration = nullptr;
|
|
|
|
|
const OperatorNameId *_oper = nullptr;
|
2020-11-18 18:04:21 +01:00
|
|
|
const ConversionNameId *_conv = nullptr;
|
2020-08-10 18:10:28 +02:00
|
|
|
const bool _strict;
|
|
|
|
|
QList<Hit> _result;
|
2012-01-20 14:43:21 +01:00
|
|
|
|
|
|
|
|
public:
|
2020-08-10 18:10:28 +02:00
|
|
|
explicit FindMatchingDefinition(Symbol *declaration, bool strict)
|
|
|
|
|
: _declaration(declaration), _strict(strict)
|
2012-01-20 14:43:21 +01:00
|
|
|
{
|
2020-11-18 18:04:21 +01:00
|
|
|
if (_declaration->name()) {
|
2012-01-20 14:43:21 +01:00
|
|
|
_oper = _declaration->name()->asOperatorNameId();
|
2020-11-18 18:04:21 +01:00
|
|
|
_conv = _declaration->name()->asConversionNameId();
|
|
|
|
|
}
|
2012-01-20 14:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
2020-08-10 18:10:28 +02:00
|
|
|
const QList<Hit> result() const { return _result; }
|
2012-01-20 14:43:21 +01:00
|
|
|
|
|
|
|
|
using SymbolVisitor::visit;
|
|
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
bool visit(Function *fun) override
|
2012-01-20 14:43:21 +01:00
|
|
|
{
|
2020-11-18 18:04:21 +01:00
|
|
|
if (_oper || _conv) {
|
2012-01-20 14:43:21 +01:00
|
|
|
if (const Name *name = fun->unqualifiedName()) {
|
2020-11-18 18:04:21 +01:00
|
|
|
if ((_oper && _oper->match(name)) || (_conv && _conv->match(name)))
|
|
|
|
|
_result.append({fun, true});
|
2012-01-20 14:43:21 +01:00
|
|
|
}
|
2014-02-23 23:53:22 +02:00
|
|
|
} else if (Function *decl = _declaration->type()->asFunctionType()) {
|
2020-08-10 18:10:28 +02:00
|
|
|
if (fun->match(decl)) {
|
|
|
|
|
_result.prepend({fun, true});
|
|
|
|
|
} else if (!_strict
|
|
|
|
|
&& Matcher::match(fun->unqualifiedName(), decl->unqualifiedName())) {
|
|
|
|
|
_result.append({fun, false});
|
|
|
|
|
}
|
2012-01-20 14:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
bool visit(Block *) override
|
2012-01-20 14:43:21 +01:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-06-11 16:43:25 +02:00
|
|
|
class FindMatchingVarDefinition: public SymbolVisitor
|
|
|
|
|
{
|
|
|
|
|
Symbol *_declaration = nullptr;
|
|
|
|
|
QList<Declaration *> _result;
|
|
|
|
|
const Identifier *_className = nullptr;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
explicit FindMatchingVarDefinition(Symbol *declaration)
|
|
|
|
|
: _declaration(declaration)
|
|
|
|
|
{
|
|
|
|
|
if (declaration->isStatic() && declaration->enclosingScope()->asClass()
|
|
|
|
|
&& declaration->enclosingClass()->asClass()->name()) {
|
|
|
|
|
_className = declaration->enclosingScope()->name()->identifier();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QList<Declaration *> result() const { return _result; }
|
|
|
|
|
|
|
|
|
|
using SymbolVisitor::visit;
|
|
|
|
|
|
|
|
|
|
bool visit(Declaration *decl) override
|
|
|
|
|
{
|
|
|
|
|
if (!decl->type()->match(_declaration->type().type()))
|
|
|
|
|
return false;
|
|
|
|
|
if (!_declaration->identifier()->equalTo(decl->identifier()))
|
|
|
|
|
return false;
|
|
|
|
|
if (_className) {
|
|
|
|
|
const QualifiedNameId * const qualName = decl->name()->asQualifiedNameId();
|
|
|
|
|
if (!qualName)
|
|
|
|
|
return false;
|
|
|
|
|
if (!qualName->base() || !qualName->base()->identifier()->equalTo(_className))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
_result.append(decl);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool visit(Block *) override { return false; }
|
|
|
|
|
};
|
|
|
|
|
|
2012-01-20 14:43:21 +01:00
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
2012-01-24 11:07:26 +01:00
|
|
|
static const int kMaxCacheSize = 10;
|
2012-01-20 14:43:21 +01:00
|
|
|
|
2019-01-14 01:40:53 +01:00
|
|
|
SymbolFinder::SymbolFinder() = default;
|
2012-01-20 14:43:21 +01:00
|
|
|
|
|
|
|
|
// strict means the returned symbol has to match exactly,
|
2013-05-14 11:04:38 +02:00
|
|
|
// including argument count, argument types, constness and volatileness.
|
|
|
|
|
Function *SymbolFinder::findMatchingDefinition(Symbol *declaration,
|
2012-01-20 14:43:21 +01:00
|
|
|
const Snapshot &snapshot,
|
|
|
|
|
bool strict)
|
|
|
|
|
{
|
|
|
|
|
if (!declaration)
|
2019-01-14 01:40:53 +01:00
|
|
|
return nullptr;
|
2012-01-20 14:43:21 +01:00
|
|
|
|
|
|
|
|
QString declFile = QString::fromUtf8(declaration->fileName(), declaration->fileNameLength());
|
|
|
|
|
|
|
|
|
|
Document::Ptr thisDocument = snapshot.document(declFile);
|
2013-07-24 11:59:39 +02:00
|
|
|
if (!thisDocument) {
|
2012-01-20 14:43:21 +01:00
|
|
|
qWarning() << "undefined document:" << declaration->fileName();
|
2019-01-14 01:40:53 +01:00
|
|
|
return nullptr;
|
2012-01-20 14:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Function *declarationTy = declaration->type()->asFunctionType();
|
2013-07-24 11:59:39 +02:00
|
|
|
if (!declarationTy) {
|
2012-01-20 14:43:21 +01:00
|
|
|
qWarning() << "not a function:" << declaration->fileName()
|
|
|
|
|
<< declaration->line() << declaration->column();
|
2019-01-14 01:40:53 +01:00
|
|
|
return nullptr;
|
2012-01-20 14:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
2020-08-10 18:10:28 +02:00
|
|
|
Hit best;
|
2012-01-23 16:08:01 +01:00
|
|
|
foreach (const QString &fileName, fileIterationOrder(declFile, snapshot)) {
|
2012-01-20 14:43:21 +01:00
|
|
|
Document::Ptr doc = snapshot.document(fileName);
|
|
|
|
|
if (!doc) {
|
2012-01-23 16:08:01 +01:00
|
|
|
clearCache(declFile, fileName);
|
2012-01-20 14:43:21 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Identifier *id = declaration->identifier();
|
2013-07-24 11:59:39 +02:00
|
|
|
if (id && !doc->control()->findIdentifier(id->chars(), id->size()))
|
2012-01-20 14:43:21 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (!id) {
|
2020-11-18 18:04:21 +01:00
|
|
|
const Name * const name = declaration->name();
|
|
|
|
|
if (!name)
|
2012-01-20 14:43:21 +01:00
|
|
|
continue;
|
2020-11-18 18:04:21 +01:00
|
|
|
if (const OperatorNameId * const oper = name->asOperatorNameId()) {
|
|
|
|
|
if (!doc->control()->findOperatorNameId(oper->kind()))
|
|
|
|
|
continue;
|
|
|
|
|
} else if (const ConversionNameId * const conv = name->asConversionNameId()) {
|
|
|
|
|
if (!doc->control()->findConversionNameId(conv->type()))
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
2012-01-20 14:43:21 +01:00
|
|
|
continue;
|
2020-11-18 18:04:21 +01:00
|
|
|
}
|
2012-01-20 14:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
2020-08-10 18:10:28 +02:00
|
|
|
FindMatchingDefinition candidates(declaration, strict);
|
2012-01-20 14:43:21 +01:00
|
|
|
candidates.accept(doc->globalNamespace());
|
|
|
|
|
|
2020-08-10 18:10:28 +02:00
|
|
|
const QList<Hit> result = candidates.result();
|
|
|
|
|
if (result.isEmpty())
|
|
|
|
|
continue;
|
2012-12-07 18:41:09 +08:00
|
|
|
|
2020-08-10 18:10:28 +02:00
|
|
|
LookupContext context(doc, snapshot);
|
|
|
|
|
ClassOrNamespace *enclosingType = context.lookupType(declaration);
|
|
|
|
|
if (!enclosingType)
|
|
|
|
|
continue; // nothing to do
|
2012-01-20 14:43:21 +01:00
|
|
|
|
2020-08-10 18:10:28 +02:00
|
|
|
for (const Hit &hit : result) {
|
|
|
|
|
QTC_CHECK(!strict || hit.exact);
|
2012-01-20 14:43:21 +01:00
|
|
|
|
2020-08-10 18:10:28 +02:00
|
|
|
const QList<LookupItem> declarations = context.lookup(hit.func->name(),
|
|
|
|
|
hit.func->enclosingScope());
|
|
|
|
|
if (declarations.isEmpty())
|
2012-01-20 14:43:21 +01:00
|
|
|
continue;
|
2020-08-10 18:10:28 +02:00
|
|
|
if (enclosingType != context.lookupType(declarations.first().declaration()))
|
2012-01-20 14:43:21 +01:00
|
|
|
continue;
|
|
|
|
|
|
2020-08-10 18:10:28 +02:00
|
|
|
if (hit.exact)
|
|
|
|
|
return hit.func;
|
|
|
|
|
|
|
|
|
|
if (!best.func || hit.func->argumentCount() == declarationTy->argumentCount())
|
|
|
|
|
best = hit;
|
2012-01-20 14:43:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-10 18:10:28 +02:00
|
|
|
QTC_CHECK(!best.exact);
|
|
|
|
|
return strict ? nullptr : best.func;
|
2020-06-11 16:43:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Symbol *SymbolFinder::findMatchingVarDefinition(Symbol *declaration, const Snapshot &snapshot)
|
|
|
|
|
{
|
|
|
|
|
if (!declaration)
|
|
|
|
|
return nullptr;
|
|
|
|
|
for (const Scope *s = declaration->enclosingScope(); s; s = s->enclosingScope()) {
|
|
|
|
|
if (s->asBlock())
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString declFile = QString::fromUtf8(declaration->fileName(), declaration->fileNameLength());
|
|
|
|
|
const Document::Ptr thisDocument = snapshot.document(declFile);
|
|
|
|
|
if (!thisDocument) {
|
|
|
|
|
qWarning() << "undefined document:" << declaration->fileName();
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using SymbolWithPriority = QPair<Symbol *, bool>;
|
|
|
|
|
QList<SymbolWithPriority> candidates;
|
|
|
|
|
QList<SymbolWithPriority> fallbacks;
|
|
|
|
|
foreach (const QString &fileName, fileIterationOrder(declFile, snapshot)) {
|
|
|
|
|
Document::Ptr doc = snapshot.document(fileName);
|
|
|
|
|
if (!doc) {
|
|
|
|
|
clearCache(declFile, fileName);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Identifier *id = declaration->identifier();
|
|
|
|
|
if (id && !doc->control()->findIdentifier(id->chars(), id->size()))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
FindMatchingVarDefinition finder(declaration);
|
|
|
|
|
finder.accept(doc->globalNamespace());
|
|
|
|
|
if (finder.result().isEmpty())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
LookupContext context(doc, snapshot);
|
|
|
|
|
ClassOrNamespace * const enclosingType = context.lookupType(declaration);
|
|
|
|
|
for (Symbol * const symbol : finder.result()) {
|
|
|
|
|
const QList<LookupItem> items = context.lookup(symbol->name(),
|
|
|
|
|
symbol->enclosingScope());
|
|
|
|
|
bool addFallback = true;
|
|
|
|
|
for (const LookupItem &item : items) {
|
|
|
|
|
if (item.declaration() == symbol)
|
|
|
|
|
addFallback = false;
|
|
|
|
|
candidates << qMakePair(item.declaration(),
|
|
|
|
|
context.lookupType(item.declaration()) == enclosingType);
|
|
|
|
|
}
|
|
|
|
|
// TODO: This is a workaround for static member definitions not being found by
|
|
|
|
|
// the lookup() function.
|
|
|
|
|
if (addFallback)
|
|
|
|
|
fallbacks << qMakePair(symbol, context.lookupType(symbol) == enclosingType);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
candidates << fallbacks;
|
|
|
|
|
SymbolWithPriority best;
|
|
|
|
|
for (const auto &candidate : qAsConst(candidates)) {
|
|
|
|
|
if (candidate.first == declaration)
|
|
|
|
|
continue;
|
|
|
|
|
if (QLatin1String(candidate.first->fileName()) == declFile
|
|
|
|
|
&& candidate.first->sourceLocation() == declaration->sourceLocation())
|
|
|
|
|
continue;
|
|
|
|
|
if (!candidate.first->asDeclaration())
|
|
|
|
|
continue;
|
|
|
|
|
if (declaration->isExtern() && candidate.first->isStatic())
|
|
|
|
|
continue;
|
|
|
|
|
if (!best.first) {
|
|
|
|
|
best = candidate;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (!best.second && candidate.second) {
|
|
|
|
|
best = candidate;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (best.first->isExtern() && !candidate.first->isExtern())
|
|
|
|
|
best = candidate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return best.first;
|
2012-01-20 14:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
2015-11-19 13:49:26 +01:00
|
|
|
Class *SymbolFinder::findMatchingClassDeclaration(Symbol *declaration, const Snapshot &snapshot)
|
2012-01-20 14:43:21 +01:00
|
|
|
{
|
2013-07-24 11:59:39 +02:00
|
|
|
if (!declaration->identifier())
|
2019-01-14 01:40:53 +01:00
|
|
|
return nullptr;
|
2012-01-20 14:43:21 +01:00
|
|
|
|
|
|
|
|
QString declFile = QString::fromUtf8(declaration->fileName(), declaration->fileNameLength());
|
|
|
|
|
|
2012-01-23 16:08:01 +01:00
|
|
|
foreach (const QString &file, fileIterationOrder(declFile, snapshot)) {
|
2012-01-20 14:43:21 +01:00
|
|
|
Document::Ptr doc = snapshot.document(file);
|
|
|
|
|
if (!doc) {
|
2012-01-23 16:08:01 +01:00
|
|
|
clearCache(declFile, file);
|
2012-01-20 14:43:21 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-24 11:59:39 +02:00
|
|
|
if (!doc->control()->findIdentifier(declaration->identifier()->chars(),
|
|
|
|
|
declaration->identifier()->size()))
|
2012-01-20 14:43:21 +01:00
|
|
|
continue;
|
|
|
|
|
|
2015-11-19 13:49:26 +01:00
|
|
|
LookupContext context(doc, snapshot);
|
2012-01-20 14:43:21 +01:00
|
|
|
|
2015-11-19 13:49:26 +01:00
|
|
|
ClassOrNamespace *type = context.lookupType(declaration);
|
2012-01-20 14:43:21 +01:00
|
|
|
if (!type)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
foreach (Symbol *s, type->symbols()) {
|
|
|
|
|
if (Class *c = s->asClass())
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 01:40:53 +01:00
|
|
|
return nullptr;
|
2012-01-20 14:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
2013-09-16 12:50:13 +02:00
|
|
|
static void findDeclarationOfSymbol(Symbol *s,
|
|
|
|
|
Function *functionType,
|
|
|
|
|
QList<Declaration *> *typeMatch,
|
|
|
|
|
QList<Declaration *> *argumentCountMatch,
|
|
|
|
|
QList<Declaration *> *nameMatch)
|
|
|
|
|
{
|
|
|
|
|
if (Declaration *decl = s->asDeclaration()) {
|
|
|
|
|
if (Function *declFunTy = decl->type()->asFunctionType()) {
|
2014-05-15 12:00:13 -04:00
|
|
|
if (functionType->match(declFunTy))
|
2013-09-16 12:50:13 +02:00
|
|
|
typeMatch->prepend(decl);
|
|
|
|
|
else if (functionType->argumentCount() == declFunTy->argumentCount())
|
|
|
|
|
argumentCountMatch->prepend(decl);
|
|
|
|
|
else
|
|
|
|
|
nameMatch->append(decl);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-20 14:43:21 +01:00
|
|
|
void SymbolFinder::findMatchingDeclaration(const LookupContext &context,
|
|
|
|
|
Function *functionType,
|
|
|
|
|
QList<Declaration *> *typeMatch,
|
|
|
|
|
QList<Declaration *> *argumentCountMatch,
|
|
|
|
|
QList<Declaration *> *nameMatch)
|
|
|
|
|
{
|
2012-02-22 15:05:45 +01:00
|
|
|
if (!functionType)
|
|
|
|
|
return;
|
|
|
|
|
|
2012-01-20 14:43:21 +01:00
|
|
|
Scope *enclosingScope = functionType->enclosingScope();
|
2013-07-24 11:59:39 +02:00
|
|
|
while (!(enclosingScope->isNamespace() || enclosingScope->isClass()))
|
2012-01-20 14:43:21 +01:00
|
|
|
enclosingScope = enclosingScope->enclosingScope();
|
2019-01-14 01:40:53 +01:00
|
|
|
QTC_ASSERT(enclosingScope != nullptr, return);
|
2012-01-20 14:43:21 +01:00
|
|
|
|
|
|
|
|
const Name *functionName = functionType->name();
|
2013-07-24 11:59:39 +02:00
|
|
|
if (!functionName)
|
2014-07-17 11:39:16 +02:00
|
|
|
return;
|
2012-01-20 14:43:21 +01:00
|
|
|
|
2019-01-14 01:40:53 +01:00
|
|
|
ClassOrNamespace *binding = nullptr;
|
2012-01-20 14:43:21 +01:00
|
|
|
const QualifiedNameId *qName = functionName->asQualifiedNameId();
|
|
|
|
|
if (qName) {
|
|
|
|
|
if (qName->base())
|
|
|
|
|
binding = context.lookupType(qName->base(), enclosingScope);
|
|
|
|
|
else
|
|
|
|
|
binding = context.globalNamespace();
|
|
|
|
|
functionName = qName->name();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!binding) { // declaration for a global function
|
|
|
|
|
binding = context.lookupType(enclosingScope);
|
|
|
|
|
|
|
|
|
|
if (!binding)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Identifier *funcId = functionName->identifier();
|
2013-09-16 12:50:13 +02:00
|
|
|
OperatorNameId::Kind operatorNameId = OperatorNameId::InvalidOp;
|
|
|
|
|
|
|
|
|
|
if (!funcId) {
|
|
|
|
|
if (!qName)
|
|
|
|
|
return;
|
|
|
|
|
const OperatorNameId * const onid = qName->name()->asOperatorNameId();
|
|
|
|
|
if (!onid)
|
|
|
|
|
return;
|
|
|
|
|
operatorNameId = onid->kind();
|
|
|
|
|
}
|
2012-01-20 14:43:21 +01:00
|
|
|
|
|
|
|
|
foreach (Symbol *s, binding->symbols()) {
|
|
|
|
|
Scope *scope = s->asScope();
|
|
|
|
|
if (!scope)
|
|
|
|
|
continue;
|
|
|
|
|
|
2013-09-16 12:50:13 +02:00
|
|
|
if (funcId) {
|
|
|
|
|
for (Symbol *s = scope->find(funcId); s; s = s->next()) {
|
2014-05-15 12:00:13 -04:00
|
|
|
if (!s->name() || !funcId->match(s->identifier()) || !s->type()->isFunctionType())
|
2013-09-16 12:50:13 +02:00
|
|
|
continue;
|
|
|
|
|
findDeclarationOfSymbol(s, functionType, typeMatch, argumentCountMatch, nameMatch);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
for (Symbol *s = scope->find(operatorNameId); s; s = s->next()) {
|
|
|
|
|
if (!s->name() || !s->type()->isFunctionType())
|
|
|
|
|
continue;
|
|
|
|
|
findDeclarationOfSymbol(s, functionType, typeMatch, argumentCountMatch, nameMatch);
|
2012-01-20 14:43:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<Declaration *> SymbolFinder::findMatchingDeclaration(const LookupContext &context,
|
|
|
|
|
Function *functionType)
|
|
|
|
|
{
|
|
|
|
|
QList<Declaration *> result;
|
|
|
|
|
QList<Declaration *> nameMatch, argumentCountMatch, typeMatch;
|
|
|
|
|
findMatchingDeclaration(context, functionType, &typeMatch, &argumentCountMatch, &nameMatch);
|
|
|
|
|
result.append(typeMatch);
|
2020-08-10 18:10:28 +02:00
|
|
|
|
|
|
|
|
// For member functions not defined inline, add fuzzy matches as fallbacks. We cannot do
|
|
|
|
|
// this for free functions, because there is no guarantee that there's a separate declaration.
|
|
|
|
|
QList<Declaration *> fuzzyMatches = argumentCountMatch + nameMatch;
|
|
|
|
|
if (!functionType->enclosingScope() || !functionType->enclosingScope()->isClass()) {
|
|
|
|
|
for (Declaration * const d : fuzzyMatches) {
|
|
|
|
|
if (d->enclosingScope() && d->enclosingScope()->isClass())
|
|
|
|
|
result.append(d);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-01-20 14:43:21 +01:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-23 16:08:01 +01:00
|
|
|
QStringList SymbolFinder::fileIterationOrder(const QString &referenceFile, const Snapshot &snapshot)
|
2012-01-20 14:43:21 +01:00
|
|
|
{
|
2012-01-23 16:08:01 +01:00
|
|
|
if (m_filePriorityCache.contains(referenceFile)) {
|
|
|
|
|
checkCacheConsistency(referenceFile, snapshot);
|
2012-01-20 14:43:21 +01:00
|
|
|
} else {
|
2012-01-23 16:08:01 +01:00
|
|
|
foreach (Document::Ptr doc, snapshot)
|
|
|
|
|
insertCache(referenceFile, doc->fileName());
|
2012-01-20 14:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
2015-10-08 13:32:36 +02:00
|
|
|
QStringList files = m_filePriorityCache.value(referenceFile).toStringList();
|
2012-01-23 20:49:00 +01:00
|
|
|
|
2012-01-23 16:08:01 +01:00
|
|
|
trackCacheUse(referenceFile);
|
|
|
|
|
|
2012-01-23 20:49:00 +01:00
|
|
|
return files;
|
2012-01-20 14:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
2015-10-08 13:32:36 +02:00
|
|
|
void SymbolFinder::clearCache()
|
|
|
|
|
{
|
|
|
|
|
m_filePriorityCache.clear();
|
|
|
|
|
m_fileMetaCache.clear();
|
|
|
|
|
m_recent.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-23 16:08:01 +01:00
|
|
|
void SymbolFinder::checkCacheConsistency(const QString &referenceFile, const Snapshot &snapshot)
|
2012-01-20 14:43:21 +01:00
|
|
|
{
|
|
|
|
|
// We only check for "new" files, which which are in the snapshot but not in the cache.
|
|
|
|
|
// The counterpart validation for "old" files is done when one tries to access the
|
|
|
|
|
// corresponding document and notices it's now null.
|
2012-01-23 16:08:01 +01:00
|
|
|
const QSet<QString> &meta = m_fileMetaCache.value(referenceFile);
|
2012-01-20 14:43:21 +01:00
|
|
|
foreach (const Document::Ptr &doc, snapshot) {
|
2012-01-23 16:08:01 +01:00
|
|
|
if (!meta.contains(doc->fileName()))
|
|
|
|
|
insertCache(referenceFile, doc->fileName());
|
2012-01-20 14:43:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-08 13:32:36 +02:00
|
|
|
const QString projectPartIdForFile(const QString &filePath)
|
|
|
|
|
{
|
|
|
|
|
const QList<ProjectPart::Ptr> parts = CppModelManager::instance()->projectPart(filePath);
|
|
|
|
|
if (!parts.isEmpty())
|
|
|
|
|
return parts.first()->id();
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-23 16:08:01 +01:00
|
|
|
void SymbolFinder::clearCache(const QString &referenceFile, const QString &comparingFile)
|
2012-01-20 14:43:21 +01:00
|
|
|
{
|
2015-10-08 13:32:36 +02:00
|
|
|
m_filePriorityCache[referenceFile].remove(comparingFile, projectPartIdForFile(comparingFile));
|
2012-01-23 16:08:01 +01:00
|
|
|
m_fileMetaCache[referenceFile].remove(comparingFile);
|
2012-01-20 14:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
2012-01-23 16:08:01 +01:00
|
|
|
void SymbolFinder::insertCache(const QString &referenceFile, const QString &comparingFile)
|
2012-01-20 14:43:21 +01:00
|
|
|
{
|
2015-10-08 13:32:36 +02:00
|
|
|
FileIterationOrder &order = m_filePriorityCache[referenceFile];
|
|
|
|
|
if (!order.isValid()) {
|
|
|
|
|
const auto projectPartId = projectPartIdForFile(referenceFile);
|
|
|
|
|
order.setReference(referenceFile, projectPartId);
|
|
|
|
|
}
|
|
|
|
|
order.insert(comparingFile, projectPartIdForFile(comparingFile));
|
|
|
|
|
|
2012-01-23 16:08:01 +01:00
|
|
|
m_fileMetaCache[referenceFile].insert(comparingFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SymbolFinder::trackCacheUse(const QString &referenceFile)
|
|
|
|
|
{
|
|
|
|
|
if (!m_recent.isEmpty()) {
|
|
|
|
|
if (m_recent.last() == referenceFile)
|
|
|
|
|
return;
|
|
|
|
|
m_recent.removeOne(referenceFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_recent.append(referenceFile);
|
|
|
|
|
|
|
|
|
|
// We don't want this to grow too much.
|
2012-01-24 11:07:26 +01:00
|
|
|
if (m_recent.size() > kMaxCacheSize) {
|
2012-01-23 20:49:00 +01:00
|
|
|
const QString &oldest = m_recent.takeFirst();
|
2012-01-23 16:08:01 +01:00
|
|
|
m_filePriorityCache.remove(oldest);
|
|
|
|
|
m_fileMetaCache.remove(oldest);
|
|
|
|
|
}
|
2012-01-20 14:43:21 +01:00
|
|
|
}
|