forked from qt-creator/qt-creator
Use mappedReduce when searching for the usages of a symbol.
This commit is contained in:
@@ -41,9 +41,8 @@
|
|||||||
|
|
||||||
using namespace CPlusPlus;
|
using namespace CPlusPlus;
|
||||||
|
|
||||||
FindUsages::FindUsages(Document::Ptr doc, const Snapshot &snapshot, QFutureInterface<Usage> *future)
|
FindUsages::FindUsages(Document::Ptr doc, const Snapshot &snapshot)
|
||||||
: ASTVisitor(doc->translationUnit()),
|
: ASTVisitor(doc->translationUnit()),
|
||||||
_future(future),
|
|
||||||
_doc(doc),
|
_doc(doc),
|
||||||
_snapshot(snapshot),
|
_snapshot(snapshot),
|
||||||
_source(_doc->source()),
|
_source(_doc->source()),
|
||||||
@@ -131,12 +130,8 @@ void FindUsages::reportResult(unsigned tokenIndex)
|
|||||||
|
|
||||||
const int len = tk.f.length;
|
const int len = tk.f.length;
|
||||||
|
|
||||||
const Usage u(_doc->fileName(), line, lineText, col, len);
|
const Usage u(_doc->fileName(), lineText, line, col, len);
|
||||||
_usages.append(u);
|
_usages.append(u);
|
||||||
|
|
||||||
if (_future)
|
|
||||||
_future->reportResult(u);
|
|
||||||
|
|
||||||
_references.append(tokenIndex);
|
_references.append(tokenIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -34,9 +34,8 @@
|
|||||||
#include "CppDocument.h"
|
#include "CppDocument.h"
|
||||||
#include "CppBindings.h"
|
#include "CppBindings.h"
|
||||||
#include "Semantic.h"
|
#include "Semantic.h"
|
||||||
|
|
||||||
#include <ASTVisitor.h>
|
#include <ASTVisitor.h>
|
||||||
#include <QtCore/QFutureInterface>
|
#include <QtCore/QSet>
|
||||||
|
|
||||||
namespace CPlusPlus {
|
namespace CPlusPlus {
|
||||||
|
|
||||||
@@ -46,7 +45,7 @@ public:
|
|||||||
Usage()
|
Usage()
|
||||||
: line(0), col(0), len(0) {}
|
: line(0), col(0), len(0) {}
|
||||||
|
|
||||||
Usage(const QString &path, int line, const QString &lineText, int col, int len)
|
Usage(const QString &path, const QString &lineText, int line, int col, int len)
|
||||||
: path(path), lineText(lineText), line(line), col(col), len(len) {}
|
: path(path), lineText(lineText), line(line), col(col), len(len) {}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -60,7 +59,7 @@ public:
|
|||||||
class CPLUSPLUS_EXPORT FindUsages: protected ASTVisitor
|
class CPLUSPLUS_EXPORT FindUsages: protected ASTVisitor
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
FindUsages(Document::Ptr doc, const Snapshot &snapshot, QFutureInterface<Usage> *future);
|
FindUsages(Document::Ptr doc, const Snapshot &snapshot);
|
||||||
|
|
||||||
void setGlobalNamespaceBinding(NamespaceBindingPtr globalNamespaceBinding);
|
void setGlobalNamespaceBinding(NamespaceBindingPtr globalNamespaceBinding);
|
||||||
|
|
||||||
@@ -103,7 +102,6 @@ protected:
|
|||||||
virtual void endVisit(SimpleDeclarationAST *);
|
virtual void endVisit(SimpleDeclarationAST *);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QFutureInterface<Usage> *_future;
|
|
||||||
const Identifier *_id;
|
const Identifier *_id;
|
||||||
Symbol *_declSymbol;
|
Symbol *_declSymbol;
|
||||||
Document::Ptr _doc;
|
Document::Ptr _doc;
|
||||||
|
@@ -54,10 +54,13 @@
|
|||||||
|
|
||||||
#include <QtCore/QTime>
|
#include <QtCore/QTime>
|
||||||
#include <QtCore/QtConcurrentRun>
|
#include <QtCore/QtConcurrentRun>
|
||||||
|
#include <QtCore/QtConcurrentMap>
|
||||||
#include <QtCore/QDir>
|
#include <QtCore/QDir>
|
||||||
#include <QtGui/QApplication>
|
#include <QtGui/QApplication>
|
||||||
#include <qtconcurrent/runextensions.h>
|
#include <qtconcurrent/runextensions.h>
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
using namespace CppTools::Internal;
|
using namespace CppTools::Internal;
|
||||||
using namespace CPlusPlus;
|
using namespace CPlusPlus;
|
||||||
|
|
||||||
@@ -81,7 +84,7 @@ QList<int> CppFindReferences::references(Symbol *symbol,
|
|||||||
{
|
{
|
||||||
QList<int> references;
|
QList<int> references;
|
||||||
|
|
||||||
FindUsages findUsages(doc, snapshot, /*future = */ 0);
|
FindUsages findUsages(doc, snapshot);
|
||||||
findUsages.setGlobalNamespaceBinding(bind(doc, snapshot));
|
findUsages.setGlobalNamespaceBinding(bind(doc, snapshot));
|
||||||
findUsages(symbol);
|
findUsages(symbol);
|
||||||
references = findUsages.references();
|
references = findUsages.references();
|
||||||
@@ -89,6 +92,77 @@ QList<int> CppFindReferences::references(Symbol *symbol,
|
|||||||
return references;
|
return references;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class MyProcess: public std::unary_function<QString, QList<Usage> >
|
||||||
|
{
|
||||||
|
const QMap<QString, QString> wl;
|
||||||
|
const Snapshot snapshot;
|
||||||
|
Symbol *symbol;
|
||||||
|
|
||||||
|
public:
|
||||||
|
MyProcess(const QMap<QString, QString> wl,
|
||||||
|
const Snapshot snapshot,
|
||||||
|
Symbol *symbol)
|
||||||
|
: wl(wl), snapshot(snapshot), symbol(symbol)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
QList<Usage> operator()(const QString &fileName)
|
||||||
|
{
|
||||||
|
QList<Usage> usages;
|
||||||
|
const Identifier *symbolId = symbol->identifier();
|
||||||
|
|
||||||
|
if (Document::Ptr previousDoc = snapshot.value(fileName)) {
|
||||||
|
Control *control = previousDoc->control();
|
||||||
|
if (! control->findIdentifier(symbolId->chars(), symbolId->size()))
|
||||||
|
return usages; // skip this document, it's not using symbolId.
|
||||||
|
}
|
||||||
|
|
||||||
|
QByteArray source;
|
||||||
|
|
||||||
|
if (wl.contains(fileName))
|
||||||
|
source = snapshot.preprocessedCode(wl.value(fileName), fileName);
|
||||||
|
else {
|
||||||
|
QFile file(fileName);
|
||||||
|
if (! file.open(QFile::ReadOnly))
|
||||||
|
return usages;
|
||||||
|
|
||||||
|
const QString contents = QTextStream(&file).readAll(); // ### FIXME
|
||||||
|
source = snapshot.preprocessedCode(contents, fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
Document::Ptr doc = snapshot.documentFromSource(source, fileName);
|
||||||
|
doc->tokenize();
|
||||||
|
|
||||||
|
Control *control = doc->control();
|
||||||
|
if (control->findIdentifier(symbolId->chars(), symbolId->size()) != 0) {
|
||||||
|
doc->check();
|
||||||
|
|
||||||
|
FindUsages process(doc, snapshot);
|
||||||
|
process.setGlobalNamespaceBinding(bind(doc, snapshot));
|
||||||
|
|
||||||
|
process(symbol);
|
||||||
|
usages = process.usages();
|
||||||
|
}
|
||||||
|
|
||||||
|
return usages;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class MyReduce: public std::binary_function<QList<Usage> &, QList<Usage>, void>
|
||||||
|
{
|
||||||
|
QFutureInterface<Usage> *future;
|
||||||
|
|
||||||
|
public:
|
||||||
|
MyReduce(QFutureInterface<Usage> *future): future(future) {}
|
||||||
|
|
||||||
|
void operator()(QList<Usage> &uu, const QList<Usage> &usages)
|
||||||
|
{
|
||||||
|
foreach (const Usage &u, usages)
|
||||||
|
future->reportResult(u);
|
||||||
|
|
||||||
|
future->setProgressValue(future->progressValue() + 1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
static void find_helper(QFutureInterface<Usage> &future,
|
static void find_helper(QFutureInterface<Usage> &future,
|
||||||
const QMap<QString, QString> wl,
|
const QMap<QString, QString> wl,
|
||||||
Snapshot snapshot,
|
Snapshot snapshot,
|
||||||
@@ -121,49 +195,10 @@ static void find_helper(QFutureInterface<Usage> &future,
|
|||||||
|
|
||||||
future.setProgressRange(0, files.size());
|
future.setProgressRange(0, files.size());
|
||||||
|
|
||||||
for (int i = 0; i < files.size(); ++i) {
|
MyProcess process(wl, snapshot, symbol);
|
||||||
if (future.isPaused())
|
MyReduce reduce(&future);
|
||||||
future.waitForResume();
|
|
||||||
|
|
||||||
if (future.isCanceled())
|
QtConcurrent::blockingMappedReduced<QList<Usage> > (files, process, reduce);
|
||||||
break;
|
|
||||||
|
|
||||||
const QString &fileName = files.at(i);
|
|
||||||
future.setProgressValueAndText(i, QFileInfo(fileName).fileName());
|
|
||||||
|
|
||||||
if (Document::Ptr previousDoc = snapshot.value(fileName)) {
|
|
||||||
Control *control = previousDoc->control();
|
|
||||||
const Identifier *id = control->findIdentifier(symbolId->chars(), symbolId->size());
|
|
||||||
if (! id)
|
|
||||||
continue; // skip this document, it's not using symbolId.
|
|
||||||
}
|
|
||||||
|
|
||||||
QByteArray source;
|
|
||||||
|
|
||||||
if (wl.contains(fileName))
|
|
||||||
source = snapshot.preprocessedCode(wl.value(fileName), fileName);
|
|
||||||
else {
|
|
||||||
QFile file(fileName);
|
|
||||||
if (! file.open(QFile::ReadOnly))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
const QString contents = QTextStream(&file).readAll(); // ### FIXME
|
|
||||||
source = snapshot.preprocessedCode(contents, fileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
Document::Ptr doc = snapshot.documentFromSource(source, fileName);
|
|
||||||
doc->tokenize();
|
|
||||||
|
|
||||||
Control *control = doc->control();
|
|
||||||
if (control->findIdentifier(symbolId->chars(), symbolId->size()) != 0) {
|
|
||||||
doc->check();
|
|
||||||
|
|
||||||
FindUsages process(doc, snapshot, &future);
|
|
||||||
process.setGlobalNamespaceBinding(bind(doc, snapshot));
|
|
||||||
|
|
||||||
process(symbol);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
future.setProgressValue(files.size());
|
future.setProgressValue(files.size());
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user