2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2009-08-07 13:02:36 +02:00
|
|
|
|
|
|
|
|
#include "cppfindreferences.h"
|
2013-03-27 18:54:03 +01:00
|
|
|
|
2021-05-26 17:24:18 +02:00
|
|
|
#include "cppcodemodelsettings.h"
|
2021-08-30 10:58:08 +02:00
|
|
|
#include "cppeditorconstants.h"
|
2023-01-11 20:43:10 +01:00
|
|
|
#include "cppeditortr.h"
|
2014-09-15 00:12:27 +02:00
|
|
|
#include "cppmodelmanager.h"
|
2021-05-26 17:24:18 +02:00
|
|
|
#include "cpptoolsreuse.h"
|
2014-07-30 16:29:02 +02:00
|
|
|
#include "cppworkingcopy.h"
|
2009-08-07 13:02:36 +02:00
|
|
|
|
2013-05-30 17:26:51 +02:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2013-03-27 18:54:03 +01:00
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
#include <coreplugin/progressmanager/futureprogress.h>
|
|
|
|
|
#include <coreplugin/progressmanager/progressmanager.h>
|
2015-09-30 18:04:17 +02:00
|
|
|
#include <projectexplorer/projectexplorer.h>
|
|
|
|
|
#include <projectexplorer/projectnodes.h>
|
2017-12-04 13:53:38 +01:00
|
|
|
#include <projectexplorer/projecttree.h>
|
2021-02-11 16:54:36 +01:00
|
|
|
#include <projectexplorer/session.h>
|
2013-03-27 18:54:03 +01:00
|
|
|
#include <texteditor/basefilefind.h>
|
|
|
|
|
|
2015-11-03 13:52:52 +01:00
|
|
|
#include <utils/algorithm.h>
|
2011-12-20 10:44:13 +01:00
|
|
|
#include <utils/qtcassert.h>
|
2013-03-27 18:54:03 +01:00
|
|
|
#include <utils/runextensions.h>
|
|
|
|
|
#include <utils/textfileformat.h>
|
2009-08-07 13:02:36 +02:00
|
|
|
|
2013-03-27 18:54:03 +01:00
|
|
|
#include <cplusplus/Overview.h>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QtConcurrentMap>
|
2015-09-30 18:04:17 +02:00
|
|
|
#include <QCheckBox>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDir>
|
2020-11-12 10:05:19 +01:00
|
|
|
#include <QFutureWatcher>
|
2020-12-11 16:14:43 +01:00
|
|
|
#include <QVBoxLayout>
|
2009-08-07 13:02:36 +02:00
|
|
|
|
2009-12-04 13:04:20 +01:00
|
|
|
#include <functional>
|
|
|
|
|
|
2013-08-30 09:22:42 +02:00
|
|
|
using namespace Core;
|
2015-09-30 18:04:17 +02:00
|
|
|
using namespace ProjectExplorer;
|
2021-08-10 16:19:02 +02:00
|
|
|
using namespace Utils;
|
2009-08-07 13:02:36 +02:00
|
|
|
|
2022-07-20 12:27:23 +02:00
|
|
|
using namespace std::placeholders;
|
|
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
namespace CppEditor {
|
2021-04-29 15:02:25 +02:00
|
|
|
|
2022-11-02 12:16:38 +01:00
|
|
|
SearchResultColor::Style colorStyleForUsageType(CPlusPlus::Usage::Tags tags)
|
2021-04-29 15:02:25 +02:00
|
|
|
{
|
2022-11-02 12:16:38 +01:00
|
|
|
if (tags.testFlag(CPlusPlus::Usage::Tag::Read))
|
2021-04-29 15:02:25 +02:00
|
|
|
return SearchResultColor::Style::Alt1;
|
2022-11-02 12:16:38 +01:00
|
|
|
if (tags.testAnyFlags({CPlusPlus::Usage::Tag::Write, CPlusPlus::Usage::Tag::WritableRef}))
|
2021-04-29 15:02:25 +02:00
|
|
|
return SearchResultColor::Style::Alt2;
|
2022-11-02 12:16:38 +01:00
|
|
|
return SearchResultColor::Style::Default;
|
2021-04-29 15:02:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QWidget *CppSearchResultFilter::createWidget()
|
|
|
|
|
{
|
|
|
|
|
const auto widget = new QWidget;
|
|
|
|
|
const auto layout = new QVBoxLayout(widget);
|
|
|
|
|
layout->setContentsMargins(0, 0, 0, 0);
|
2023-01-11 20:43:10 +01:00
|
|
|
const auto readsCheckBox = new QCheckBox(Tr::tr("Reads"));
|
2021-04-29 15:02:25 +02:00
|
|
|
readsCheckBox->setChecked(m_showReads);
|
2023-01-11 20:43:10 +01:00
|
|
|
const auto writesCheckBox = new QCheckBox(Tr::tr("Writes"));
|
2021-04-29 15:02:25 +02:00
|
|
|
writesCheckBox->setChecked(m_showWrites);
|
2023-01-11 20:43:10 +01:00
|
|
|
const auto declsCheckBox = new QCheckBox(Tr::tr("Declarations"));
|
2021-04-29 15:02:25 +02:00
|
|
|
declsCheckBox->setChecked(m_showDecls);
|
2023-01-11 20:43:10 +01:00
|
|
|
const auto otherCheckBox = new QCheckBox(Tr::tr("Other"));
|
2021-04-29 15:02:25 +02:00
|
|
|
otherCheckBox->setChecked(m_showOther);
|
|
|
|
|
layout->addWidget(readsCheckBox);
|
|
|
|
|
layout->addWidget(writesCheckBox);
|
|
|
|
|
layout->addWidget(declsCheckBox);
|
|
|
|
|
layout->addWidget(otherCheckBox);
|
|
|
|
|
connect(readsCheckBox, &QCheckBox::toggled,
|
|
|
|
|
this, [this](bool checked) { setValue(m_showReads, checked); });
|
|
|
|
|
connect(writesCheckBox, &QCheckBox::toggled,
|
|
|
|
|
this, [this](bool checked) { setValue(m_showWrites, checked); });
|
|
|
|
|
connect(declsCheckBox, &QCheckBox::toggled,
|
|
|
|
|
this, [this](bool checked) { setValue(m_showDecls, checked); });
|
|
|
|
|
connect(otherCheckBox, &QCheckBox::toggled,
|
|
|
|
|
this, [this](bool checked) { setValue(m_showOther, checked); });
|
|
|
|
|
return widget;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CppSearchResultFilter::matches(const SearchResultItem &item) const
|
|
|
|
|
{
|
2022-11-02 12:16:38 +01:00
|
|
|
const auto usageTags = CPlusPlus::Usage::Tags::fromInt(item.userData().toInt());
|
|
|
|
|
if (usageTags.testFlag(CPlusPlus::Usage::Tag::Read))
|
2021-04-29 15:02:25 +02:00
|
|
|
return m_showReads;
|
2022-11-02 12:16:38 +01:00
|
|
|
if (usageTags.testAnyFlags({CPlusPlus::Usage::Tag::Write, CPlusPlus::Usage::Tag::WritableRef}))
|
2021-04-29 15:02:25 +02:00
|
|
|
return m_showWrites;
|
2022-11-02 12:16:38 +01:00
|
|
|
if (usageTags.testFlag(CPlusPlus::Usage::Tag::Declaration))
|
2021-04-29 15:02:25 +02:00
|
|
|
return m_showDecls;
|
2022-11-02 12:16:38 +01:00
|
|
|
return m_showOther;
|
2021-04-29 15:02:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppSearchResultFilter::setValue(bool &member, bool value)
|
|
|
|
|
{
|
|
|
|
|
member = value;
|
|
|
|
|
emit filterChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
static QByteArray getSource(const Utils::FilePath &fileName,
|
2014-07-30 16:29:02 +02:00
|
|
|
const WorkingCopy &workingCopy)
|
2009-12-21 14:54:10 +01:00
|
|
|
{
|
|
|
|
|
if (workingCopy.contains(fileName)) {
|
|
|
|
|
return workingCopy.source(fileName);
|
|
|
|
|
} else {
|
2012-04-14 13:29:03 +08:00
|
|
|
QString fileContents;
|
|
|
|
|
Utils::TextFileFormat format;
|
|
|
|
|
QString error;
|
2013-08-30 09:22:42 +02:00
|
|
|
QTextCodec *defaultCodec = EditorManager::defaultTextCodec();
|
2012-04-14 13:29:03 +08:00
|
|
|
Utils::TextFileFormat::ReadResult result = Utils::TextFileFormat::readFile(
|
2021-05-18 07:57:14 +02:00
|
|
|
fileName, defaultCodec, &fileContents, &format, &error);
|
2012-04-14 13:29:03 +08:00
|
|
|
if (result != Utils::TextFileFormat::ReadSuccess)
|
|
|
|
|
qWarning() << "Could not read " << fileName << ". Error: " << error;
|
2009-12-21 14:54:10 +01:00
|
|
|
|
2013-08-19 15:47:51 +02:00
|
|
|
return fileContents.toUtf8();
|
2009-12-21 14:54:10 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 12:52:31 +02:00
|
|
|
static QByteArray typeId(CPlusPlus::Symbol *symbol)
|
2013-10-18 12:42:46 +02:00
|
|
|
{
|
|
|
|
|
if (symbol->asEnum()) {
|
|
|
|
|
return QByteArray("e");
|
|
|
|
|
} else if (symbol->asFunction()) {
|
|
|
|
|
return QByteArray("f");
|
|
|
|
|
} else if (symbol->asNamespace()) {
|
|
|
|
|
return QByteArray("n");
|
|
|
|
|
} else if (symbol->asTemplate()) {
|
|
|
|
|
return QByteArray("t");
|
|
|
|
|
} else if (symbol->asNamespaceAlias()) {
|
|
|
|
|
return QByteArray("na");
|
|
|
|
|
} else if (symbol->asClass()) {
|
|
|
|
|
return QByteArray("c");
|
|
|
|
|
} else if (symbol->asBlock()) {
|
|
|
|
|
return QByteArray("b");
|
|
|
|
|
} else if (symbol->asUsingNamespaceDirective()) {
|
|
|
|
|
return QByteArray("u");
|
|
|
|
|
} else if (symbol->asUsingDeclaration()) {
|
|
|
|
|
return QByteArray("ud");
|
|
|
|
|
} else if (symbol->asDeclaration()) {
|
|
|
|
|
QByteArray temp("d,");
|
2017-10-04 12:52:31 +02:00
|
|
|
CPlusPlus::Overview pretty;
|
2014-05-05 11:43:24 -04:00
|
|
|
temp.append(pretty.prettyType(symbol->type()).toUtf8());
|
2013-10-18 12:42:46 +02:00
|
|
|
return temp;
|
|
|
|
|
} else if (symbol->asArgument()) {
|
|
|
|
|
return QByteArray("a");
|
|
|
|
|
} else if (symbol->asTypenameArgument()) {
|
|
|
|
|
return QByteArray("ta");
|
|
|
|
|
} else if (symbol->asBaseClass()) {
|
|
|
|
|
return QByteArray("bc");
|
|
|
|
|
} else if (symbol->asForwardClassDeclaration()) {
|
|
|
|
|
return QByteArray("fcd");
|
|
|
|
|
} else if (symbol->asQtPropertyDeclaration()) {
|
|
|
|
|
return QByteArray("qpd");
|
|
|
|
|
} else if (symbol->asQtEnum()) {
|
|
|
|
|
return QByteArray("qe");
|
|
|
|
|
} else if (symbol->asObjCBaseClass()) {
|
|
|
|
|
return QByteArray("ocbc");
|
|
|
|
|
} else if (symbol->asObjCBaseProtocol()) {
|
|
|
|
|
return QByteArray("ocbp");
|
|
|
|
|
} else if (symbol->asObjCClass()) {
|
|
|
|
|
return QByteArray("occ");
|
|
|
|
|
} else if (symbol->asObjCForwardClassDeclaration()) {
|
|
|
|
|
return QByteArray("ocfd");
|
|
|
|
|
} else if (symbol->asObjCProtocol()) {
|
|
|
|
|
return QByteArray("ocp");
|
|
|
|
|
} else if (symbol->asObjCForwardProtocolDeclaration()) {
|
|
|
|
|
return QByteArray("ocfpd");
|
|
|
|
|
} else if (symbol->asObjCMethod()) {
|
|
|
|
|
return QByteArray("ocm");
|
|
|
|
|
} else if (symbol->asObjCPropertyDeclaration()) {
|
|
|
|
|
return QByteArray("ocpd");
|
|
|
|
|
}
|
|
|
|
|
return QByteArray("unknown");
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 12:52:31 +02:00
|
|
|
static QByteArray idForSymbol(CPlusPlus::Symbol *symbol)
|
2013-10-18 12:42:46 +02:00
|
|
|
{
|
|
|
|
|
QByteArray uid(typeId(symbol));
|
2017-10-04 12:52:31 +02:00
|
|
|
if (const CPlusPlus::Identifier *id = symbol->identifier()) {
|
2013-10-18 12:42:46 +02:00
|
|
|
uid.append("|");
|
|
|
|
|
uid.append(QByteArray(id->chars(), id->size()));
|
2017-10-04 12:52:31 +02:00
|
|
|
} else if (CPlusPlus::Scope *scope = symbol->enclosingScope()) {
|
2013-10-18 12:42:46 +02:00
|
|
|
// add the index of this symbol within its enclosing scope
|
|
|
|
|
// (counting symbols without identifier of the same type)
|
|
|
|
|
int count = 0;
|
2017-10-04 12:52:31 +02:00
|
|
|
CPlusPlus::Scope::iterator it = scope->memberBegin();
|
2015-01-13 17:29:34 +01:00
|
|
|
while (it != scope->memberEnd() && *it != symbol) {
|
2017-10-04 12:52:31 +02:00
|
|
|
CPlusPlus::Symbol *val = *it;
|
2013-10-18 12:42:46 +02:00
|
|
|
++it;
|
|
|
|
|
if (val->identifier() || typeId(val) != uid)
|
|
|
|
|
continue;
|
|
|
|
|
++count;
|
|
|
|
|
}
|
|
|
|
|
uid.append(QString::number(count).toLocal8Bit());
|
|
|
|
|
}
|
|
|
|
|
return uid;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 12:52:31 +02:00
|
|
|
static QList<QByteArray> fullIdForSymbol(CPlusPlus::Symbol *symbol)
|
2013-10-18 12:42:46 +02:00
|
|
|
{
|
|
|
|
|
QList<QByteArray> uid;
|
2017-10-04 12:52:31 +02:00
|
|
|
CPlusPlus::Symbol *current = symbol;
|
2013-10-18 12:42:46 +02:00
|
|
|
do {
|
|
|
|
|
uid.prepend(idForSymbol(current));
|
|
|
|
|
current = current->enclosingScope();
|
|
|
|
|
} while (current);
|
|
|
|
|
return uid;
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-07 12:18:22 +01:00
|
|
|
namespace {
|
2009-09-30 13:25:40 +02:00
|
|
|
|
2018-05-31 11:31:59 +02:00
|
|
|
class ProcessFile
|
2009-12-04 13:04:20 +01:00
|
|
|
{
|
2014-07-30 16:29:02 +02:00
|
|
|
const WorkingCopy workingCopy;
|
2017-10-04 12:52:31 +02:00
|
|
|
const CPlusPlus::Snapshot snapshot;
|
|
|
|
|
CPlusPlus::Document::Ptr symbolDocument;
|
|
|
|
|
CPlusPlus::Symbol *symbol;
|
|
|
|
|
QFutureInterface<CPlusPlus::Usage> *future;
|
2021-05-26 17:24:18 +02:00
|
|
|
const bool categorize;
|
2009-12-04 13:04:20 +01:00
|
|
|
|
|
|
|
|
public:
|
2018-05-31 11:31:59 +02:00
|
|
|
// needed by QtConcurrent
|
2019-05-28 13:49:26 +02:00
|
|
|
using argument_type = const Utils::FilePath &;
|
2018-05-31 11:31:59 +02:00
|
|
|
using result_type = QList<CPlusPlus::Usage>;
|
|
|
|
|
|
2014-07-30 16:29:02 +02:00
|
|
|
ProcessFile(const WorkingCopy &workingCopy,
|
2017-10-04 12:52:31 +02:00
|
|
|
const CPlusPlus::Snapshot snapshot,
|
|
|
|
|
CPlusPlus::Document::Ptr symbolDocument,
|
|
|
|
|
CPlusPlus::Symbol *symbol,
|
2021-05-26 17:24:18 +02:00
|
|
|
QFutureInterface<CPlusPlus::Usage> *future,
|
|
|
|
|
bool categorize)
|
2011-12-20 11:15:23 +01:00
|
|
|
: workingCopy(workingCopy),
|
|
|
|
|
snapshot(snapshot),
|
|
|
|
|
symbolDocument(symbolDocument),
|
|
|
|
|
symbol(symbol),
|
2021-05-26 17:24:18 +02:00
|
|
|
future(future),
|
|
|
|
|
categorize(categorize)
|
2009-12-04 13:04:20 +01:00
|
|
|
{ }
|
|
|
|
|
|
2022-11-21 16:48:50 +01:00
|
|
|
QList<CPlusPlus::Usage> operator()(const Utils::FilePath &filePath)
|
2009-12-04 13:04:20 +01:00
|
|
|
{
|
2017-10-04 12:52:31 +02:00
|
|
|
QList<CPlusPlus::Usage> usages;
|
2012-05-25 16:45:18 +02:00
|
|
|
if (future->isPaused())
|
|
|
|
|
future->waitForResume();
|
2011-12-20 11:15:23 +01:00
|
|
|
if (future->isCanceled())
|
|
|
|
|
return usages;
|
2017-10-04 12:52:31 +02:00
|
|
|
const CPlusPlus::Identifier *symbolId = symbol->identifier();
|
2009-12-04 13:04:20 +01:00
|
|
|
|
2022-11-21 16:48:50 +01:00
|
|
|
if (CPlusPlus::Document::Ptr previousDoc = snapshot.document(filePath)) {
|
2017-10-04 12:52:31 +02:00
|
|
|
CPlusPlus::Control *control = previousDoc->control();
|
2013-07-24 11:59:39 +02:00
|
|
|
if (!control->findIdentifier(symbolId->chars(), symbolId->size()))
|
2009-12-04 13:04:20 +01:00
|
|
|
return usages; // skip this document, it's not using symbolId.
|
|
|
|
|
}
|
2017-10-04 12:52:31 +02:00
|
|
|
CPlusPlus::Document::Ptr doc;
|
2022-11-21 16:48:50 +01:00
|
|
|
const QByteArray unpreprocessedSource = getSource(filePath, workingCopy);
|
2009-12-04 13:04:20 +01:00
|
|
|
|
2022-11-21 16:48:50 +01:00
|
|
|
if (symbolDocument && filePath == symbolDocument->filePath()) {
|
2010-08-10 14:40:27 +02:00
|
|
|
doc = symbolDocument;
|
2011-12-20 18:45:29 +01:00
|
|
|
} else {
|
2022-11-21 16:48:50 +01:00
|
|
|
doc = snapshot.preprocessedDocument(unpreprocessedSource, filePath);
|
2010-08-10 14:40:27 +02:00
|
|
|
doc->tokenize();
|
|
|
|
|
}
|
2009-12-04 13:04:20 +01:00
|
|
|
|
2017-10-04 12:52:31 +02:00
|
|
|
CPlusPlus::Control *control = doc->control();
|
2019-01-14 01:40:53 +01:00
|
|
|
if (control->findIdentifier(symbolId->chars(), symbolId->size()) != nullptr) {
|
2010-08-10 14:40:27 +02:00
|
|
|
if (doc != symbolDocument)
|
|
|
|
|
doc->check();
|
2009-12-04 13:04:20 +01:00
|
|
|
|
2021-05-26 17:24:18 +02:00
|
|
|
CPlusPlus::FindUsages process(unpreprocessedSource, doc, snapshot, categorize);
|
2009-12-04 13:04:20 +01:00
|
|
|
process(symbol);
|
2010-08-10 14:40:27 +02:00
|
|
|
|
2009-12-04 13:04:20 +01:00
|
|
|
usages = process.usages();
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-25 16:45:18 +02:00
|
|
|
if (future->isPaused())
|
|
|
|
|
future->waitForResume();
|
2009-12-04 13:04:20 +01:00
|
|
|
return usages;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-05-31 11:31:59 +02:00
|
|
|
class UpdateUI
|
2009-12-04 13:04:20 +01:00
|
|
|
{
|
2017-10-04 12:52:31 +02:00
|
|
|
QFutureInterface<CPlusPlus::Usage> *future;
|
2009-12-04 13:04:20 +01:00
|
|
|
|
|
|
|
|
public:
|
2019-01-14 01:40:53 +01:00
|
|
|
explicit UpdateUI(QFutureInterface<CPlusPlus::Usage> *future): future(future) {}
|
2009-12-04 13:04:20 +01:00
|
|
|
|
2017-10-04 12:52:31 +02:00
|
|
|
void operator()(QList<CPlusPlus::Usage> &, const QList<CPlusPlus::Usage> &usages)
|
2009-12-04 13:04:20 +01:00
|
|
|
{
|
2022-05-05 15:51:12 +02:00
|
|
|
for (const CPlusPlus::Usage &u : usages)
|
2009-12-04 13:04:20 +01:00
|
|
|
future->reportResult(u);
|
|
|
|
|
|
|
|
|
|
future->setProgressValue(future->progressValue() + 1);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2009-12-07 12:18:22 +01:00
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
2014-09-15 00:12:27 +02:00
|
|
|
CppFindReferences::CppFindReferences(CppModelManager *modelManager)
|
2009-12-07 12:18:22 +01:00
|
|
|
: QObject(modelManager),
|
2013-10-10 10:26:39 +02:00
|
|
|
m_modelManager(modelManager)
|
2009-12-07 12:18:22 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 01:40:53 +01:00
|
|
|
CppFindReferences::~CppFindReferences() = default;
|
2009-12-07 12:18:22 +01:00
|
|
|
|
2017-10-04 12:52:31 +02:00
|
|
|
QList<int> CppFindReferences::references(CPlusPlus::Symbol *symbol,
|
|
|
|
|
const CPlusPlus::LookupContext &context) const
|
2009-12-07 12:18:22 +01:00
|
|
|
{
|
|
|
|
|
QList<int> references;
|
|
|
|
|
|
2017-10-04 12:52:31 +02:00
|
|
|
CPlusPlus::FindUsages findUsages(context);
|
2009-12-07 12:18:22 +01:00
|
|
|
findUsages(symbol);
|
|
|
|
|
references = findUsages.references();
|
|
|
|
|
|
|
|
|
|
return references;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 12:52:31 +02:00
|
|
|
static void find_helper(QFutureInterface<CPlusPlus::Usage> &future,
|
2014-07-30 16:29:02 +02:00
|
|
|
const WorkingCopy workingCopy,
|
2019-01-14 01:40:53 +01:00
|
|
|
const CPlusPlus::LookupContext &context,
|
2021-05-26 17:24:18 +02:00
|
|
|
CPlusPlus::Symbol *symbol,
|
|
|
|
|
bool categorize)
|
2009-08-07 13:02:36 +02:00
|
|
|
{
|
2017-10-04 12:52:31 +02:00
|
|
|
const CPlusPlus::Identifier *symbolId = symbol->identifier();
|
2019-01-14 01:40:53 +01:00
|
|
|
QTC_ASSERT(symbolId != nullptr, return);
|
2009-09-25 14:19:43 +02:00
|
|
|
|
2017-10-04 12:52:31 +02:00
|
|
|
const CPlusPlus::Snapshot snapshot = context.snapshot();
|
2010-09-10 14:16:06 +02:00
|
|
|
|
2022-11-24 17:20:48 +01:00
|
|
|
const FilePath sourceFile = symbol->filePath();
|
|
|
|
|
FilePaths files{sourceFile};
|
2009-10-06 17:22:52 +02:00
|
|
|
|
2022-06-23 16:56:36 +02:00
|
|
|
if (symbol->asClass()
|
|
|
|
|
|| symbol->asForwardClassDeclaration()
|
2013-07-24 11:59:39 +02:00
|
|
|
|| (symbol->enclosingScope()
|
|
|
|
|
&& !symbol->isStatic()
|
2022-06-23 16:56:36 +02:00
|
|
|
&& symbol->enclosingScope()->asNamespace())) {
|
2017-10-04 12:52:31 +02:00
|
|
|
const CPlusPlus::Snapshot snapshotFromContext = context.snapshot();
|
2014-11-27 12:11:46 +01:00
|
|
|
for (auto i = snapshotFromContext.begin(), ei = snapshotFromContext.end(); i != ei; ++i) {
|
|
|
|
|
if (i.key() == sourceFile)
|
2009-10-06 17:22:52 +02:00
|
|
|
continue;
|
|
|
|
|
|
2017-10-04 12:52:31 +02:00
|
|
|
const CPlusPlus::Control *control = i.value()->control();
|
2009-10-06 17:22:52 +02:00
|
|
|
|
|
|
|
|
if (control->findIdentifier(symbolId->chars(), symbolId->size()))
|
2014-11-27 12:11:46 +01:00
|
|
|
files.append(i.key());
|
2009-10-06 17:22:52 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
2014-09-11 13:15:44 +02:00
|
|
|
files += snapshot.filesDependingOn(sourceFile);
|
2009-10-06 17:22:52 +02:00
|
|
|
}
|
2015-11-03 13:52:52 +01:00
|
|
|
files = Utils::filteredUnique(files);
|
2009-08-07 13:02:36 +02:00
|
|
|
|
|
|
|
|
future.setProgressRange(0, files.size());
|
|
|
|
|
|
2021-05-26 17:24:18 +02:00
|
|
|
ProcessFile process(workingCopy, snapshot, context.thisDocument(), symbol, &future, categorize);
|
2009-12-07 12:18:22 +01:00
|
|
|
UpdateUI reduce(&future);
|
2011-12-20 11:15:23 +01:00
|
|
|
// This thread waits for blockingMappedReduced to finish, so reduce the pool's used thread count
|
|
|
|
|
// so the blockingMappedReduced can use one more thread, and increase it again afterwards.
|
|
|
|
|
QThreadPool::globalInstance()->releaseThread();
|
2017-10-04 12:52:31 +02:00
|
|
|
QtConcurrent::blockingMappedReduced<QList<CPlusPlus::Usage> > (files, process, reduce);
|
2011-12-20 11:15:23 +01:00
|
|
|
QThreadPool::globalInstance()->reserveThread();
|
2009-08-07 13:02:36 +02:00
|
|
|
future.setProgressValue(files.size());
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 12:52:31 +02:00
|
|
|
void CppFindReferences::findUsages(CPlusPlus::Symbol *symbol,
|
|
|
|
|
const CPlusPlus::LookupContext &context)
|
2011-12-20 18:45:29 +01:00
|
|
|
{
|
|
|
|
|
findUsages(symbol, context, QString(), false);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 12:52:31 +02:00
|
|
|
void CppFindReferences::findUsages(CPlusPlus::Symbol *symbol,
|
|
|
|
|
const CPlusPlus::LookupContext &context,
|
2011-12-20 18:45:29 +01:00
|
|
|
const QString &replacement,
|
|
|
|
|
bool replace)
|
2009-10-05 15:17:25 +02:00
|
|
|
{
|
2017-10-04 12:52:31 +02:00
|
|
|
CPlusPlus::Overview overview;
|
2023-01-11 20:43:10 +01:00
|
|
|
SearchResult *search = SearchResultWindow::instance()->startNewSearch(Tr::tr("C++ Usages:"),
|
2011-09-09 16:10:57 +02:00
|
|
|
QString(),
|
2020-11-18 22:42:51 +01:00
|
|
|
overview.prettyName(CPlusPlus::LookupContext::fullyQualifiedName(symbol)),
|
2015-02-04 17:01:07 +02:00
|
|
|
replace ? SearchResultWindow::SearchAndReplace
|
|
|
|
|
: SearchResultWindow::SearchOnly,
|
|
|
|
|
SearchResultWindow::PreserveCaseDisabled,
|
2011-12-20 18:45:29 +01:00
|
|
|
QLatin1String("CppEditor"));
|
|
|
|
|
search->setTextToReplace(replacement);
|
2021-05-26 17:24:18 +02:00
|
|
|
if (codeModelSettings()->categorizeFindReferences())
|
|
|
|
|
search->setFilter(new CppSearchResultFilter);
|
2022-07-20 12:27:23 +02:00
|
|
|
setupSearch(search);
|
2011-12-20 18:45:29 +01:00
|
|
|
search->setSearchAgainSupported(true);
|
2022-07-20 12:27:23 +02:00
|
|
|
connect(search, &SearchResult::searchAgainRequested, this,
|
|
|
|
|
std::bind(&CppFindReferences::searchAgain, this, search));
|
2011-12-20 18:45:29 +01:00
|
|
|
CppFindReferencesParameters parameters;
|
2013-10-18 12:42:46 +02:00
|
|
|
parameters.symbolId = fullIdForSymbol(symbol);
|
2022-11-24 15:21:15 +01:00
|
|
|
parameters.symbolFilePath = symbol->filePath();
|
2021-05-26 17:24:18 +02:00
|
|
|
parameters.categorize = codeModelSettings()->categorizeFindReferences();
|
2015-09-30 18:04:17 +02:00
|
|
|
|
2022-06-23 16:56:36 +02:00
|
|
|
if (symbol->asClass() || symbol->asForwardClassDeclaration()) {
|
2017-10-04 12:52:31 +02:00
|
|
|
CPlusPlus::Overview overview;
|
2020-11-18 22:42:51 +01:00
|
|
|
parameters.prettySymbolName =
|
|
|
|
|
overview.prettyName(CPlusPlus::LookupContext::path(symbol).constLast());
|
2015-09-30 18:04:17 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-27 13:32:20 +02:00
|
|
|
search->setUserData(QVariant::fromValue(parameters));
|
2021-05-26 17:24:18 +02:00
|
|
|
findAll_helper(search, symbol, context, codeModelSettings()->categorizeFindReferences());
|
2009-10-05 15:17:25 +02:00
|
|
|
}
|
|
|
|
|
|
2017-10-04 12:52:31 +02:00
|
|
|
void CppFindReferences::renameUsages(CPlusPlus::Symbol *symbol,
|
|
|
|
|
const CPlusPlus::LookupContext &context,
|
2010-07-20 11:02:37 +02:00
|
|
|
const QString &replacement)
|
2009-08-07 13:02:36 +02:00
|
|
|
{
|
2017-10-04 12:52:31 +02:00
|
|
|
if (const CPlusPlus::Identifier *id = symbol->identifier()) {
|
2010-07-20 11:02:37 +02:00
|
|
|
const QString textToReplace = replacement.isEmpty()
|
|
|
|
|
? QString::fromUtf8(id->chars(), id->size()) : replacement;
|
2011-12-20 18:45:29 +01:00
|
|
|
findUsages(symbol, context, textToReplace, true);
|
2009-10-06 16:00:32 +02:00
|
|
|
}
|
2009-10-05 15:17:25 +02:00
|
|
|
}
|
|
|
|
|
|
2017-10-04 12:52:31 +02:00
|
|
|
void CppFindReferences::findAll_helper(SearchResult *search, CPlusPlus::Symbol *symbol,
|
2021-05-26 17:24:18 +02:00
|
|
|
const CPlusPlus::LookupContext &context, bool categorize)
|
2009-10-05 15:17:25 +02:00
|
|
|
{
|
2013-10-18 12:42:46 +02:00
|
|
|
if (!(symbol && symbol->identifier())) {
|
2012-05-16 15:59:16 +02:00
|
|
|
search->finishSearch(false);
|
2009-10-26 13:45:01 +01:00
|
|
|
return;
|
2011-12-20 10:44:13 +01:00
|
|
|
}
|
2016-03-12 22:53:45 +02:00
|
|
|
connect(search, &SearchResult::activated,
|
2017-09-22 16:36:26 +02:00
|
|
|
[](const SearchResultItem& item) {
|
|
|
|
|
Core::EditorManager::openEditorAtSearchResult(item);
|
|
|
|
|
});
|
2009-10-26 13:45:01 +01:00
|
|
|
|
2015-02-04 17:01:07 +02:00
|
|
|
SearchResultWindow::instance()->popup(IOutputPane::ModeSwitch | IOutputPane::WithFocus);
|
2014-07-30 16:29:02 +02:00
|
|
|
const WorkingCopy workingCopy = m_modelManager->workingCopy();
|
2017-10-04 12:52:31 +02:00
|
|
|
QFuture<CPlusPlus::Usage> result;
|
2016-02-17 23:27:41 +02:00
|
|
|
result = Utils::runAsync(m_modelManager->sharedThreadPool(), find_helper,
|
2021-05-26 17:24:18 +02:00
|
|
|
workingCopy, context, symbol, categorize);
|
2011-12-20 10:44:13 +01:00
|
|
|
createWatcher(result, search);
|
|
|
|
|
|
2023-01-11 20:43:10 +01:00
|
|
|
FutureProgress *progress = ProgressManager::addTask(result, Tr::tr("Searching for Usages"),
|
|
|
|
|
CppEditor::Constants::TASK_SEARCH);
|
2009-08-07 13:02:36 +02:00
|
|
|
|
2016-03-12 22:53:45 +02:00
|
|
|
connect(progress, &FutureProgress::clicked, search, &SearchResult::popup);
|
2009-08-07 13:02:36 +02:00
|
|
|
}
|
|
|
|
|
|
2022-07-20 12:27:23 +02:00
|
|
|
void CppFindReferences::setupSearch(Core::SearchResult *search)
|
|
|
|
|
{
|
|
|
|
|
auto renameFilesCheckBox = new QCheckBox();
|
|
|
|
|
renameFilesCheckBox->setVisible(false);
|
|
|
|
|
search->setAdditionalReplaceWidget(renameFilesCheckBox);
|
|
|
|
|
connect(search, &SearchResult::replaceButtonClicked, this,
|
|
|
|
|
std::bind(&CppFindReferences::onReplaceButtonClicked, this, search, _1, _2, _3));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppFindReferences::onReplaceButtonClicked(Core::SearchResult *search,
|
|
|
|
|
const QString &text,
|
2015-02-04 17:01:07 +02:00
|
|
|
const QList<SearchResultItem> &items,
|
2012-11-30 16:15:07 +01:00
|
|
|
bool preserveCase)
|
2009-10-05 18:02:46 +02:00
|
|
|
{
|
2021-06-22 08:57:36 +02:00
|
|
|
const Utils::FilePaths filePaths = TextEditor::BaseFileFind::replaceAll(text, items, preserveCase);
|
|
|
|
|
if (!filePaths.isEmpty()) {
|
2022-11-24 19:16:47 +01:00
|
|
|
m_modelManager->updateSourceFiles(Utils::toSet(filePaths));
|
2015-02-04 17:01:07 +02:00
|
|
|
SearchResultWindow::instance()->hide();
|
2009-10-05 18:02:46 +02:00
|
|
|
}
|
2015-09-30 18:04:17 +02:00
|
|
|
|
|
|
|
|
CppFindReferencesParameters parameters = search->userData().value<CppFindReferencesParameters>();
|
|
|
|
|
if (parameters.filesToRename.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
auto renameFilesCheckBox = qobject_cast<QCheckBox *>(search->additionalReplaceWidget());
|
|
|
|
|
if (!renameFilesCheckBox || !renameFilesCheckBox->isChecked())
|
|
|
|
|
return;
|
|
|
|
|
|
2022-09-21 13:38:42 +02:00
|
|
|
ProjectExplorerPlugin::renameFilesForSymbol(
|
|
|
|
|
parameters.prettySymbolName, text, parameters.filesToRename,
|
|
|
|
|
preferLowerCaseFileNames());
|
2009-10-05 18:02:46 +02:00
|
|
|
}
|
|
|
|
|
|
2022-07-20 12:27:23 +02:00
|
|
|
void CppFindReferences::searchAgain(SearchResult *search)
|
2011-12-20 18:45:29 +01:00
|
|
|
{
|
|
|
|
|
CppFindReferencesParameters parameters = search->userData().value<CppFindReferencesParameters>();
|
2015-09-30 18:04:17 +02:00
|
|
|
parameters.filesToRename.clear();
|
2017-10-04 12:52:31 +02:00
|
|
|
CPlusPlus::Snapshot snapshot = CppModelManager::instance()->snapshot();
|
2012-02-07 15:44:12 +01:00
|
|
|
search->restart();
|
2017-10-04 12:52:31 +02:00
|
|
|
CPlusPlus::LookupContext context;
|
|
|
|
|
CPlusPlus::Symbol *symbol = findSymbol(parameters, snapshot, &context);
|
2013-10-18 12:42:46 +02:00
|
|
|
if (!symbol) {
|
2012-05-16 15:59:16 +02:00
|
|
|
search->finishSearch(false);
|
2011-12-20 18:45:29 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2021-05-26 17:24:18 +02:00
|
|
|
findAll_helper(search, symbol, context, parameters.categorize);
|
2011-12-20 18:45:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace {
|
2017-10-04 12:52:31 +02:00
|
|
|
class UidSymbolFinder : public CPlusPlus::SymbolVisitor
|
2011-12-20 18:45:29 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2019-01-14 01:40:53 +01:00
|
|
|
explicit UidSymbolFinder(const QList<QByteArray> &uid) : m_uid(uid) { }
|
2017-10-04 12:52:31 +02:00
|
|
|
CPlusPlus::Symbol *result() const { return m_result; }
|
2011-12-20 18:45:29 +01:00
|
|
|
|
2019-01-14 01:40:53 +01:00
|
|
|
bool preVisit(CPlusPlus::Symbol *symbol) override
|
2011-12-20 18:45:29 +01:00
|
|
|
{
|
|
|
|
|
if (m_result)
|
|
|
|
|
return false;
|
|
|
|
|
int index = m_index;
|
|
|
|
|
if (symbol->asScope())
|
|
|
|
|
++m_index;
|
|
|
|
|
if (index >= m_uid.size())
|
|
|
|
|
return false;
|
|
|
|
|
if (idForSymbol(symbol) != m_uid.at(index))
|
|
|
|
|
return false;
|
|
|
|
|
if (index == m_uid.size() - 1) {
|
|
|
|
|
// symbol found
|
|
|
|
|
m_result = symbol;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 01:40:53 +01:00
|
|
|
void postVisit(CPlusPlus::Symbol *symbol) override
|
2011-12-20 18:45:29 +01:00
|
|
|
{
|
|
|
|
|
if (symbol->asScope())
|
|
|
|
|
--m_index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2012-11-21 22:36:47 +02:00
|
|
|
QList<QByteArray> m_uid;
|
2019-01-14 01:40:53 +01:00
|
|
|
int m_index = 0;
|
|
|
|
|
CPlusPlus::Symbol *m_result = nullptr;
|
2011-12-20 18:45:29 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 12:52:31 +02:00
|
|
|
CPlusPlus::Symbol *CppFindReferences::findSymbol(const CppFindReferencesParameters ¶meters,
|
|
|
|
|
const CPlusPlus::Snapshot &snapshot,
|
|
|
|
|
CPlusPlus::LookupContext *context)
|
2011-12-20 18:45:29 +01:00
|
|
|
{
|
2019-01-14 01:40:53 +01:00
|
|
|
QTC_ASSERT(context, return nullptr);
|
2022-11-24 15:21:15 +01:00
|
|
|
if (!snapshot.contains(parameters.symbolFilePath))
|
2019-01-14 01:40:53 +01:00
|
|
|
return nullptr;
|
2011-12-20 18:45:29 +01:00
|
|
|
|
2022-11-24 15:21:15 +01:00
|
|
|
CPlusPlus::Document::Ptr newSymbolDocument = snapshot.document(parameters.symbolFilePath);
|
2011-12-20 18:45:29 +01:00
|
|
|
// document is not parsed and has no bindings yet, do it
|
2022-11-21 16:48:50 +01:00
|
|
|
QByteArray source = getSource(newSymbolDocument->filePath(), m_modelManager->workingCopy());
|
2017-10-04 12:52:31 +02:00
|
|
|
CPlusPlus::Document::Ptr doc =
|
2022-11-21 16:48:50 +01:00
|
|
|
snapshot.preprocessedDocument(source, newSymbolDocument->filePath());
|
2011-12-20 18:45:29 +01:00
|
|
|
doc->check();
|
|
|
|
|
|
|
|
|
|
// find matching symbol in new document and return the new parameters
|
2015-10-09 17:42:56 +02:00
|
|
|
UidSymbolFinder finder(parameters.symbolId);
|
2011-12-20 18:45:29 +01:00
|
|
|
finder.accept(doc->globalNamespace());
|
|
|
|
|
if (finder.result()) {
|
2017-10-04 12:52:31 +02:00
|
|
|
*context = CPlusPlus::LookupContext(doc, snapshot);
|
2013-10-18 12:42:46 +02:00
|
|
|
return finder.result();
|
2011-12-20 18:45:29 +01:00
|
|
|
}
|
2019-01-14 01:40:53 +01:00
|
|
|
return nullptr;
|
2011-12-20 18:45:29 +01:00
|
|
|
}
|
|
|
|
|
|
2022-05-07 00:44:03 +03:00
|
|
|
static void displayResults(SearchResult *search,
|
|
|
|
|
QFutureWatcher<CPlusPlus::Usage> *watcher,
|
|
|
|
|
int first,
|
|
|
|
|
int last)
|
2009-08-07 13:02:36 +02:00
|
|
|
{
|
2015-09-30 18:04:17 +02:00
|
|
|
CppFindReferencesParameters parameters = search->userData().value<CppFindReferencesParameters>();
|
|
|
|
|
|
2009-12-01 17:07:35 +01:00
|
|
|
for (int index = first; index != last; ++index) {
|
2020-09-10 14:33:14 +02:00
|
|
|
const CPlusPlus::Usage result = watcher->future().resultAt(index);
|
2021-02-11 16:22:48 +01:00
|
|
|
SearchResultItem item;
|
|
|
|
|
item.setFilePath(result.path);
|
|
|
|
|
item.setMainRange(result.line, result.col, result.len);
|
|
|
|
|
item.setLineText(result.lineText);
|
2022-11-02 12:16:38 +01:00
|
|
|
item.setUserData(result.tags.toInt());
|
2022-06-10 10:47:38 +02:00
|
|
|
item.setContainingFunctionName(result.containingFunction);
|
2022-11-02 12:16:38 +01:00
|
|
|
item.setStyle(colorStyleForUsageType(result.tags));
|
2021-03-02 09:10:08 +01:00
|
|
|
item.setUseTextEditorFont(true);
|
2021-02-11 16:54:36 +01:00
|
|
|
if (search->supportsReplace())
|
|
|
|
|
item.setSelectForReplacement(SessionManager::projectForFile(result.path));
|
2021-02-11 16:22:48 +01:00
|
|
|
search->addResult(item);
|
2015-09-30 18:04:17 +02:00
|
|
|
|
|
|
|
|
if (parameters.prettySymbolName.isEmpty())
|
|
|
|
|
continue;
|
|
|
|
|
|
2022-09-21 13:38:42 +02:00
|
|
|
if (parameters.filesToRename.contains(result.path))
|
2015-09-30 18:04:17 +02:00
|
|
|
continue;
|
|
|
|
|
|
2022-09-21 13:38:42 +02:00
|
|
|
if (!SessionManager::projectForFile(result.path))
|
2015-09-30 18:04:17 +02:00
|
|
|
continue;
|
|
|
|
|
|
2022-09-21 13:38:42 +02:00
|
|
|
if (result.path.baseName().compare(parameters.prettySymbolName, Qt::CaseInsensitive) == 0)
|
|
|
|
|
parameters.filesToRename.append(result.path);
|
2009-12-01 17:07:35 +01:00
|
|
|
}
|
2015-09-30 18:04:17 +02:00
|
|
|
|
2019-05-27 13:32:20 +02:00
|
|
|
search->setUserData(QVariant::fromValue(parameters));
|
2015-09-30 18:04:17 +02:00
|
|
|
}
|
|
|
|
|
|
2017-10-04 12:52:31 +02:00
|
|
|
static void searchFinished(SearchResult *search, QFutureWatcher<CPlusPlus::Usage> *watcher)
|
2015-09-30 18:04:17 +02:00
|
|
|
{
|
|
|
|
|
search->finishSearch(watcher->isCanceled());
|
|
|
|
|
|
|
|
|
|
CppFindReferencesParameters parameters = search->userData().value<CppFindReferencesParameters>();
|
|
|
|
|
if (!parameters.filesToRename.isEmpty()) {
|
|
|
|
|
const QStringList filesToRename
|
2022-09-21 13:38:42 +02:00
|
|
|
= Utils::transform<QList>(parameters.filesToRename, &FilePath::toUserOutput);
|
2015-09-30 18:04:17 +02:00
|
|
|
|
|
|
|
|
auto renameCheckBox = qobject_cast<QCheckBox *>(search->additionalReplaceWidget());
|
|
|
|
|
if (renameCheckBox) {
|
2023-01-11 20:43:10 +01:00
|
|
|
renameCheckBox->setText(Tr::tr("Re&name %n files", nullptr, filesToRename.size()));
|
|
|
|
|
renameCheckBox->setToolTip(Tr::tr("Files:\n%1").arg(filesToRename.join('\n')));
|
2015-09-30 18:04:17 +02:00
|
|
|
renameCheckBox->setVisible(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
watcher->deleteLater();
|
2009-08-07 13:02:36 +02:00
|
|
|
}
|
|
|
|
|
|
2009-12-21 14:54:10 +01:00
|
|
|
namespace {
|
|
|
|
|
|
2018-05-31 11:31:59 +02:00
|
|
|
class FindMacroUsesInFile
|
2009-12-21 14:54:10 +01:00
|
|
|
{
|
2014-07-30 16:29:02 +02:00
|
|
|
const WorkingCopy workingCopy;
|
2017-10-04 12:52:31 +02:00
|
|
|
const CPlusPlus::Snapshot snapshot;
|
2017-02-07 15:00:38 +01:00
|
|
|
const CPlusPlus::Macro ¯o;
|
2017-10-04 12:52:31 +02:00
|
|
|
QFutureInterface<CPlusPlus::Usage> *future;
|
2009-12-21 14:54:10 +01:00
|
|
|
|
|
|
|
|
public:
|
2018-05-31 11:31:59 +02:00
|
|
|
// needed by QtConcurrent
|
2019-05-28 13:49:26 +02:00
|
|
|
using argument_type = const Utils::FilePath &;
|
2018-05-31 11:31:59 +02:00
|
|
|
using result_type = QList<CPlusPlus::Usage>;
|
|
|
|
|
|
2014-07-30 16:29:02 +02:00
|
|
|
FindMacroUsesInFile(const WorkingCopy &workingCopy,
|
2017-10-04 12:52:31 +02:00
|
|
|
const CPlusPlus::Snapshot snapshot,
|
2017-02-07 15:00:38 +01:00
|
|
|
const CPlusPlus::Macro ¯o,
|
2017-10-04 12:52:31 +02:00
|
|
|
QFutureInterface<CPlusPlus::Usage> *future)
|
2011-12-20 11:15:23 +01:00
|
|
|
: workingCopy(workingCopy), snapshot(snapshot), macro(macro), future(future)
|
2009-12-21 14:54:10 +01:00
|
|
|
{ }
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
QList<CPlusPlus::Usage> operator()(const Utils::FilePath &fileName)
|
2009-12-21 14:54:10 +01:00
|
|
|
{
|
2017-10-04 12:52:31 +02:00
|
|
|
QList<CPlusPlus::Usage> usages;
|
|
|
|
|
CPlusPlus::Document::Ptr doc = snapshot.document(fileName);
|
2013-08-19 15:47:51 +02:00
|
|
|
QByteArray source;
|
2012-10-11 16:16:01 +02:00
|
|
|
|
2013-02-01 11:56:40 +01:00
|
|
|
restart_search:
|
2012-05-25 16:45:18 +02:00
|
|
|
if (future->isPaused())
|
|
|
|
|
future->waitForResume();
|
2011-12-20 11:15:23 +01:00
|
|
|
if (future->isCanceled())
|
|
|
|
|
return usages;
|
2009-12-21 14:54:10 +01:00
|
|
|
|
2012-10-11 16:16:01 +02:00
|
|
|
usages.clear();
|
2022-08-10 09:41:42 +02:00
|
|
|
for (const CPlusPlus::Document::MacroUse &use : doc->macroUses()) {
|
2017-02-07 15:00:38 +01:00
|
|
|
const CPlusPlus::Macro &useMacro = use.macro();
|
2012-10-11 16:16:01 +02:00
|
|
|
|
2022-11-28 13:06:13 +01:00
|
|
|
if (useMacro.filePath() == macro.filePath()) { // Check if this is a match, but possibly against an outdated document.
|
2012-10-21 20:16:58 +02:00
|
|
|
if (source.isEmpty())
|
|
|
|
|
source = getSource(fileName, workingCopy);
|
|
|
|
|
|
2012-10-11 16:16:01 +02:00
|
|
|
if (macro.fileRevision() > useMacro.fileRevision()) {
|
|
|
|
|
// yes, it is outdated, so re-preprocess and start from scratch for this file.
|
|
|
|
|
doc = snapshot.preprocessedDocument(source, fileName);
|
2013-02-01 11:56:40 +01:00
|
|
|
usages.clear();
|
|
|
|
|
goto restart_search;
|
2012-10-11 16:16:01 +02:00
|
|
|
}
|
|
|
|
|
|
2012-10-21 20:16:58 +02:00
|
|
|
if (macro.name() == useMacro.name()) {
|
2014-05-09 10:04:13 -04:00
|
|
|
unsigned column;
|
|
|
|
|
const QString &lineSource = matchingLine(use.bytesBegin(), source, &column);
|
2022-06-10 10:47:38 +02:00
|
|
|
usages.append(CPlusPlus::Usage(fileName, lineSource, {},
|
2022-11-02 12:16:38 +01:00
|
|
|
{}, use.beginLine(),
|
2020-08-28 14:07:16 +02:00
|
|
|
column, useMacro.nameToQString().size()));
|
2012-10-21 20:16:58 +02:00
|
|
|
}
|
2009-12-21 14:54:10 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-25 16:45:18 +02:00
|
|
|
if (future->isPaused())
|
|
|
|
|
future->waitForResume();
|
2009-12-21 14:54:10 +01:00
|
|
|
return usages;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-09 10:04:13 -04:00
|
|
|
static QString matchingLine(unsigned bytesOffsetOfUseStart, const QByteArray &utf8Source,
|
2019-01-14 01:40:53 +01:00
|
|
|
unsigned *columnOfUseStart = nullptr)
|
2009-12-21 14:54:10 +01:00
|
|
|
{
|
2014-05-09 10:04:13 -04:00
|
|
|
int lineBegin = utf8Source.lastIndexOf('\n', bytesOffsetOfUseStart) + 1;
|
|
|
|
|
int lineEnd = utf8Source.indexOf('\n', bytesOffsetOfUseStart);
|
2012-10-08 18:18:28 +02:00
|
|
|
if (lineEnd == -1)
|
2014-05-09 10:04:13 -04:00
|
|
|
lineEnd = utf8Source.length();
|
|
|
|
|
|
|
|
|
|
if (columnOfUseStart) {
|
|
|
|
|
*columnOfUseStart = 0;
|
|
|
|
|
const char *startOfUse = utf8Source.constData() + bytesOffsetOfUseStart;
|
|
|
|
|
QTC_ASSERT(startOfUse < utf8Source.constData() + lineEnd, return QString());
|
|
|
|
|
const char *currentSourceByte = utf8Source.constData() + lineBegin;
|
|
|
|
|
unsigned char yychar = *currentSourceByte;
|
|
|
|
|
while (currentSourceByte != startOfUse)
|
2017-10-04 12:52:31 +02:00
|
|
|
CPlusPlus::Lexer::yyinp_utf8(currentSourceByte, yychar, *columnOfUseStart);
|
2014-05-09 10:04:13 -04:00
|
|
|
}
|
2009-12-21 14:54:10 +01:00
|
|
|
|
2014-05-09 10:04:13 -04:00
|
|
|
const QByteArray matchingLine = utf8Source.mid(lineBegin, lineEnd - lineBegin);
|
|
|
|
|
return QString::fromUtf8(matchingLine, matchingLine.size());
|
2009-12-21 14:54:10 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
2017-10-04 12:52:31 +02:00
|
|
|
static void findMacroUses_helper(QFutureInterface<CPlusPlus::Usage> &future,
|
2014-07-30 16:29:02 +02:00
|
|
|
const WorkingCopy workingCopy,
|
2017-10-04 12:52:31 +02:00
|
|
|
const CPlusPlus::Snapshot snapshot,
|
2017-02-07 15:00:38 +01:00
|
|
|
const CPlusPlus::Macro macro)
|
2009-12-21 14:54:10 +01:00
|
|
|
{
|
2022-11-23 17:33:36 +01:00
|
|
|
const FilePath sourceFile = macro.filePath();
|
|
|
|
|
FilePaths files{sourceFile};
|
2015-11-03 13:52:52 +01:00
|
|
|
files = Utils::filteredUnique(files + snapshot.filesDependingOn(sourceFile));
|
2009-12-21 14:54:10 +01:00
|
|
|
|
|
|
|
|
future.setProgressRange(0, files.size());
|
2011-12-20 11:15:23 +01:00
|
|
|
FindMacroUsesInFile process(workingCopy, snapshot, macro, &future);
|
2009-12-21 14:54:10 +01:00
|
|
|
UpdateUI reduce(&future);
|
2011-12-20 11:15:23 +01:00
|
|
|
// This thread waits for blockingMappedReduced to finish, so reduce the pool's used thread count
|
|
|
|
|
// so the blockingMappedReduced can use one more thread, and increase it again afterwards.
|
|
|
|
|
QThreadPool::globalInstance()->releaseThread();
|
2017-10-04 12:52:31 +02:00
|
|
|
QtConcurrent::blockingMappedReduced<QList<CPlusPlus::Usage> > (files, process, reduce);
|
2011-12-20 11:15:23 +01:00
|
|
|
QThreadPool::globalInstance()->reserveThread();
|
2009-12-21 14:54:10 +01:00
|
|
|
future.setProgressValue(files.size());
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-07 15:00:38 +01:00
|
|
|
void CppFindReferences::findMacroUses(const CPlusPlus::Macro ¯o)
|
2012-03-17 13:26:27 +01:00
|
|
|
{
|
|
|
|
|
findMacroUses(macro, QString(), false);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-07 15:00:38 +01:00
|
|
|
void CppFindReferences::findMacroUses(const CPlusPlus::Macro ¯o, const QString &replacement,
|
|
|
|
|
bool replace)
|
2009-12-21 14:54:10 +01:00
|
|
|
{
|
2015-02-04 17:01:07 +02:00
|
|
|
SearchResult *search = SearchResultWindow::instance()->startNewSearch(
|
2023-01-11 20:43:10 +01:00
|
|
|
Tr::tr("C++ Macro Usages:"),
|
2011-09-09 16:10:57 +02:00
|
|
|
QString(),
|
2014-05-09 10:04:13 -04:00
|
|
|
macro.nameToQString(),
|
2015-02-04 17:01:07 +02:00
|
|
|
replace ? SearchResultWindow::SearchAndReplace
|
|
|
|
|
: SearchResultWindow::SearchOnly,
|
|
|
|
|
SearchResultWindow::PreserveCaseDisabled,
|
2012-03-17 13:26:27 +01:00
|
|
|
QLatin1String("CppEditor"));
|
|
|
|
|
|
|
|
|
|
search->setTextToReplace(replacement);
|
2022-07-20 12:27:23 +02:00
|
|
|
setupSearch(search);
|
2015-02-04 17:01:07 +02:00
|
|
|
SearchResultWindow::instance()->popup(IOutputPane::ModeSwitch | IOutputPane::WithFocus);
|
2009-12-21 14:54:10 +01:00
|
|
|
|
2016-03-12 22:53:45 +02:00
|
|
|
connect(search, &SearchResult::activated,
|
2017-09-22 16:36:26 +02:00
|
|
|
[](const Core::SearchResultItem& item) {
|
|
|
|
|
Core::EditorManager::openEditorAtSearchResult(item);
|
|
|
|
|
});
|
2009-12-21 14:54:10 +01:00
|
|
|
|
2017-10-04 12:52:31 +02:00
|
|
|
const CPlusPlus::Snapshot snapshot = m_modelManager->snapshot();
|
2014-07-30 16:29:02 +02:00
|
|
|
const WorkingCopy workingCopy = m_modelManager->workingCopy();
|
2009-12-21 14:54:10 +01:00
|
|
|
|
|
|
|
|
// add the macro definition itself
|
|
|
|
|
{
|
2022-11-28 13:06:13 +01:00
|
|
|
const QByteArray &source = getSource(macro.filePath(), workingCopy);
|
2014-05-09 10:04:13 -04:00
|
|
|
unsigned column;
|
|
|
|
|
const QString line = FindMacroUsesInFile::matchingLine(macro.bytesOffset(), source,
|
|
|
|
|
&column);
|
2021-02-11 16:22:48 +01:00
|
|
|
SearchResultItem item;
|
2022-11-28 13:06:13 +01:00
|
|
|
const FilePath filePath = macro.filePath();
|
2021-02-11 16:54:36 +01:00
|
|
|
item.setFilePath(filePath);
|
2021-02-11 16:22:48 +01:00
|
|
|
item.setLineText(line);
|
|
|
|
|
item.setMainRange(macro.line(), column, macro.nameToQString().length());
|
2021-03-02 09:10:08 +01:00
|
|
|
item.setUseTextEditorFont(true);
|
2021-02-11 16:54:36 +01:00
|
|
|
if (search->supportsReplace())
|
|
|
|
|
item.setSelectForReplacement(SessionManager::projectForFile(filePath));
|
2021-02-11 16:22:48 +01:00
|
|
|
search->addResult(item);
|
2009-12-21 14:54:10 +01:00
|
|
|
}
|
|
|
|
|
|
2017-10-04 12:52:31 +02:00
|
|
|
QFuture<CPlusPlus::Usage> result;
|
2016-02-17 23:27:41 +02:00
|
|
|
result = Utils::runAsync(m_modelManager->sharedThreadPool(), findMacroUses_helper,
|
|
|
|
|
workingCopy, snapshot, macro);
|
2011-12-20 10:44:13 +01:00
|
|
|
createWatcher(result, search);
|
2009-12-21 14:54:10 +01:00
|
|
|
|
2023-01-11 20:43:10 +01:00
|
|
|
FutureProgress *progress = ProgressManager::addTask(result, Tr::tr("Searching for Usages"),
|
|
|
|
|
CppEditor::Constants::TASK_SEARCH);
|
2016-03-12 22:53:45 +02:00
|
|
|
connect(progress, &FutureProgress::clicked, search, &SearchResult::popup);
|
2009-12-21 14:54:10 +01:00
|
|
|
}
|
|
|
|
|
|
2017-02-07 15:00:38 +01:00
|
|
|
void CppFindReferences::renameMacroUses(const CPlusPlus::Macro ¯o, const QString &replacement)
|
2012-03-17 13:26:27 +01:00
|
|
|
{
|
2014-05-09 10:04:13 -04:00
|
|
|
const QString textToReplace = replacement.isEmpty() ? macro.nameToQString() : replacement;
|
2012-03-17 13:26:27 +01:00
|
|
|
findMacroUses(macro, textToReplace, true);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-27 14:49:15 +02:00
|
|
|
void CppFindReferences::checkUnused(Core::SearchResult *search, const Link &link,
|
|
|
|
|
CPlusPlus::Symbol *symbol,
|
|
|
|
|
const CPlusPlus::LookupContext &context,
|
|
|
|
|
const LinkHandler &callback)
|
|
|
|
|
{
|
|
|
|
|
const auto isProperUsage = [symbol](const CPlusPlus::Usage &usage) {
|
|
|
|
|
if (!usage.tags.testFlag(CPlusPlus::Usage::Tag::Declaration))
|
|
|
|
|
return usage.containingFunctionSymbol != symbol;
|
|
|
|
|
return usage.tags.testAnyFlags({CPlusPlus::Usage::Tag::Override,
|
|
|
|
|
CPlusPlus::Usage::Tag::MocInvokable,
|
|
|
|
|
CPlusPlus::Usage::Tag::Template,
|
|
|
|
|
CPlusPlus::Usage::Tag::Operator,
|
|
|
|
|
CPlusPlus::Usage::Tag::ConstructorDestructor});
|
|
|
|
|
};
|
|
|
|
|
const auto watcher = new QFutureWatcher<CPlusPlus::Usage>();
|
|
|
|
|
connect(watcher, &QFutureWatcherBase::finished, watcher,
|
|
|
|
|
[watcher, link, callback, search, isProperUsage] {
|
|
|
|
|
watcher->deleteLater();
|
|
|
|
|
if (watcher->isCanceled())
|
|
|
|
|
return callback(link);
|
|
|
|
|
for (int i = 0; i < watcher->future().resultCount(); ++i) {
|
|
|
|
|
if (isProperUsage(watcher->resultAt(i)))
|
|
|
|
|
return callback(link);
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < watcher->future().resultCount(); ++i) {
|
|
|
|
|
const CPlusPlus::Usage usage = watcher->resultAt(i);
|
|
|
|
|
SearchResultItem item;
|
|
|
|
|
item.setFilePath(usage.path);
|
|
|
|
|
item.setLineText(usage.lineText);
|
|
|
|
|
item.setMainRange(usage.line, usage.col, usage.len);
|
|
|
|
|
item.setUseTextEditorFont(true);
|
|
|
|
|
search->addResult(item);
|
|
|
|
|
}
|
|
|
|
|
callback(link);
|
|
|
|
|
});
|
|
|
|
|
connect(watcher, &QFutureWatcherBase::resultsReadyAt, search,
|
|
|
|
|
[watcher, isProperUsage](int first, int end) {
|
|
|
|
|
for (int i = first; i < end; ++i) {
|
|
|
|
|
if (isProperUsage(watcher->resultAt(i))) {
|
|
|
|
|
watcher->cancel();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
connect(search, &SearchResult::canceled, watcher, [watcher] { watcher->cancel(); });
|
|
|
|
|
connect(search, &SearchResult::destroyed, watcher, [watcher] { watcher->cancel(); });
|
|
|
|
|
watcher->setFuture(Utils::runAsync(m_modelManager->sharedThreadPool(), find_helper,
|
|
|
|
|
m_modelManager->workingCopy(), context, symbol, true));
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 12:52:31 +02:00
|
|
|
void CppFindReferences::createWatcher(const QFuture<CPlusPlus::Usage> &future, SearchResult *search)
|
2011-12-20 10:44:13 +01:00
|
|
|
{
|
2019-01-14 01:40:53 +01:00
|
|
|
auto watcher = new QFutureWatcher<CPlusPlus::Usage>();
|
2016-10-24 15:01:45 +02:00
|
|
|
// auto-delete:
|
2015-09-30 18:04:17 +02:00
|
|
|
connect(watcher, &QFutureWatcherBase::finished, watcher, [search, watcher]() {
|
|
|
|
|
searchFinished(search, watcher);
|
|
|
|
|
});
|
2016-10-24 15:01:45 +02:00
|
|
|
|
|
|
|
|
connect(watcher, &QFutureWatcherBase::resultsReadyAt, search,
|
|
|
|
|
[search, watcher](int first, int last) {
|
|
|
|
|
displayResults(search, watcher, first, last);
|
|
|
|
|
});
|
|
|
|
|
connect(watcher, &QFutureWatcherBase::finished, search, [search, watcher]() {
|
|
|
|
|
search->finishSearch(watcher->isCanceled());
|
|
|
|
|
});
|
2022-07-20 12:46:13 +02:00
|
|
|
connect(search, &SearchResult::canceled, watcher, [watcher]() { watcher->cancel(); });
|
2016-10-24 15:01:45 +02:00
|
|
|
connect(search, &SearchResult::paused, watcher, [watcher](bool paused) {
|
|
|
|
|
if (!paused || watcher->isRunning()) // guard against pausing when the search is finished
|
|
|
|
|
watcher->setPaused(paused);
|
|
|
|
|
});
|
2011-12-20 10:44:13 +01:00
|
|
|
watcher->setPendingResultsLimit(1);
|
2011-12-20 17:28:46 +01:00
|
|
|
watcher->setFuture(future);
|
2011-12-20 10:44:13 +01:00
|
|
|
}
|
2021-04-29 15:02:25 +02:00
|
|
|
|
|
|
|
|
} // namespace Internal
|
2021-08-30 10:58:08 +02:00
|
|
|
} // namespace CppEditor
|