Commit f820c1c7 authored by Roberto Raggi's avatar Roberto Raggi
Browse files

Removed CppSemanticSearch & co. Find usages of a Symbol is way more powerful.

parent edcbb6a1
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include "cppsemanticsearch.h"
#include "cppmodelmanager.h"
#include <AST.h>
#include <Literals.h>
#include <TranslationUnit.h>
#include <QtCore/QDir>
#include <QtCore/QPointer>
#include <QtCore/QtConcurrentRun>
#include <QtCore/QFutureSynchronizer>
#include <qtconcurrent/runextensions.h>
using namespace CppTools::Internal;
using namespace CPlusPlus;
namespace {
class SearchClass: public SemanticSearch
{
QString _text;
QTextDocument::FindFlags _findFlags;
public:
SearchClass(QFutureInterface<Utils::FileSearchResult> &future,
Document::Ptr doc, Snapshot snapshot)
: SemanticSearch(future, doc, snapshot)
{ }
void setText(const QString &text)
{ _text = text; }
void setFindFlags(QTextDocument::FindFlags findFlags)
{ _findFlags = findFlags; }
virtual void run(AST *ast)
{ accept(ast); }
protected:
using ASTVisitor::visit;
bool match(NameAST *name)
{
if (! name)
return false;
else if (SimpleNameAST *simpleName = name->asSimpleName()) {
if (Identifier *id = identifier(simpleName->identifier_token)) {
Qt::CaseSensitivity cs = Qt::CaseInsensitive;
if (_findFlags & QTextDocument::FindCaseSensitively)
cs = Qt::CaseSensitive;
QString s = QString::fromUtf8(id->chars(), id->size());
int index = s.indexOf(_text, 0, cs);
if (index != -1) {
reportResult(simpleName->identifier_token, index, _text.length());
return true;
}
}
}
else if (QualifiedNameAST *q = name->asQualifiedName()) {
return match(q->unqualified_name);
}
return false;
}
virtual bool visit(ElaboratedTypeSpecifierAST *ast)
{
if (tokenKind(ast->classkey_token) != T_ENUM) {
match(ast->name);
}
return true;
}
virtual bool visit(ClassSpecifierAST *ast)
{
match(ast->name);
return true;
}
};
class SearchFunctionCall: public SemanticSearch
{
QString _text;
QTextDocument::FindFlags _findFlags;
public:
SearchFunctionCall(QFutureInterface<Utils::FileSearchResult> &future,
Document::Ptr doc, Snapshot snapshot)
: SemanticSearch(future, doc, snapshot)
{ }
void setText(const QString &text)
{ _text = text; }
void setFindFlags(QTextDocument::FindFlags findFlags)
{ _findFlags = findFlags; }
virtual void run(AST *ast)
{ accept(ast); }
protected:
using ASTVisitor::visit;
bool match(NameAST *name)
{
if (! name)
return false;
else if (SimpleNameAST *simpleName = name->asSimpleName()) {
if (Identifier *id = identifier(simpleName->identifier_token)) {
Qt::CaseSensitivity cs = Qt::CaseInsensitive;
if (_findFlags & QTextDocument::FindCaseSensitively)
cs = Qt::CaseSensitive;
QString s = QString::fromUtf8(id->chars(), id->size());
int index = s.indexOf(_text, 0, cs);
if (index != -1) {
reportResult(simpleName->identifier_token, index, _text.length());
return true;
}
}
}
else if (QualifiedNameAST *q = name->asQualifiedName()) {
return match(q->unqualified_name);
}
return false;
}
virtual bool visit(PostfixExpressionAST *ast)
{
NameAST *name = 0;
if (ast->base_expression)
name = ast->base_expression->asName();
for (PostfixAST *fx = ast->postfix_expressions; fx; fx = fx->next) {
if (fx->asCall() != 0) {
match(name);
} else if (MemberAccessAST *mem = fx->asMemberAccess()) {
name = mem->member_name;
}
}
return true;
}
};
} // end of anonymous namespace
SemanticSearch::SemanticSearch(QFutureInterface<Utils::FileSearchResult> &future,
Document::Ptr doc,
Snapshot snapshot)
: ASTVisitor(doc->control()),
_future(future),
_doc(doc),
_snapshot(snapshot)
{
_thisDocument = _snapshot.value(_doc->fileName());
}
SemanticSearch::~SemanticSearch()
{ }
const QByteArray &SemanticSearch::source() const
{ return _source; }
void SemanticSearch::setSource(const QByteArray &source)
{ _source = source; }
QString SemanticSearch::matchingLine(const Token &tk) const
{
const char *beg = _source.constData();
const char *cp = beg + tk.offset;
for (; cp != beg - 1; --cp) {
if (*cp == '\n')
break;
}
++cp;
const char *lineEnd = cp + 1;
for (; *lineEnd; ++lineEnd) {
if (*lineEnd == '\n')
break;
}
const QString matchingLine = QString::fromUtf8(cp, lineEnd - cp);
return matchingLine;
}
void SemanticSearch::reportResult(unsigned tokenIndex, int offset, int len)
{
const Token &tk = tokenAt(tokenIndex);
const QString lineText = matchingLine(tk);
unsigned line, col;
getTokenStartPosition(tokenIndex, &line, &col);
if (col)
--col; // adjust the column position.
_future.reportResult(Utils::FileSearchResult(QDir::toNativeSeparators(_doc->fileName()),
line, lineText, col + offset, len));
}
SemanticSearch *SearchClassDeclarationsFactory::create(QFutureInterface<Utils::FileSearchResult> &future,
Document::Ptr doc,
Snapshot snapshot)
{
SearchClass *search = new SearchClass(future, doc, snapshot);
search->setText(_text);
search->setFindFlags(_findFlags);
return search;
}
SemanticSearch *SearchFunctionCallFactory::create(QFutureInterface<Utils::FileSearchResult> &future,
Document::Ptr doc,
Snapshot snapshot)
{
SearchFunctionCall *search = new SearchFunctionCall(future, doc, snapshot);
search->setText(_text);
search->setFindFlags(_findFlags);
return search;
}
static void semanticSearch_helper(QFutureInterface<Utils::FileSearchResult> &future,
QPointer<CppModelManager> modelManager,
QMap<QString, QString> wl,
SemanticSearchFactory::Ptr factory)
{
const Snapshot snapshot = modelManager->snapshot();
future.setProgressRange(0, snapshot.size());
future.setProgressValue(0);
int progress = 0;
foreach (Document::Ptr doc, snapshot) {
if (future.isPaused())
future.waitForResume();
if (future.isCanceled())
break;
const QString fileName = doc->fileName();
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 newDoc = snapshot.documentFromSource(source, fileName);
newDoc->parse();
if (SemanticSearch *search = factory->create(future, newDoc, snapshot)) {
search->setSource(source);
search->run(newDoc->translationUnit()->ast());
delete search;
}
future.setProgressValue(++progress);
}
}
QFuture<Utils::FileSearchResult> CppTools::Internal::semanticSearch(QPointer<CppModelManager> modelManager,
SemanticSearchFactory::Ptr factory)
{
return QtConcurrent::run(&semanticSearch_helper, modelManager,
modelManager->buildWorkingCopyList(), factory);
}
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#ifndef CPPSEMANTICSEARCH_H
#define CPPSEMANTICSEARCH_H
#include <ASTVisitor.h>
#include <cplusplus/CppDocument.h>
#include <utils/filesearch.h>
#include <QtCore/QFutureInterface>
#include <QtCore/QPointer>
#include <QtCore/QSharedPointer>
#include <QtGui/QTextDocument>
namespace CppTools {
namespace Internal {
class CppModelManager;
class SemanticSearchFactory;
class SemanticSearch: protected CPlusPlus::ASTVisitor
{
QFutureInterface<Utils::FileSearchResult> &_future;
CPlusPlus::Document::Ptr _doc;
CPlusPlus::Snapshot _snapshot;
CPlusPlus::Document::Ptr _thisDocument;
QByteArray _source;
public:
SemanticSearch(QFutureInterface<Utils::FileSearchResult> &future,
CPlusPlus::Document::Ptr doc,
CPlusPlus::Snapshot snapshot);
virtual ~SemanticSearch();
virtual void run(CPlusPlus::AST *ast) = 0;
const QByteArray &source() const;
void setSource(const QByteArray &source);
protected:
QString matchingLine(const CPlusPlus::Token &tk) const;
void reportResult(unsigned tokenIndex, int offset, int len);
};
class SemanticSearchFactory
{
Q_DISABLE_COPY(SemanticSearchFactory)
public:
typedef QSharedPointer<SemanticSearchFactory> Ptr;
SemanticSearchFactory() {}
virtual ~SemanticSearchFactory() {}
virtual SemanticSearch *create(QFutureInterface<Utils::FileSearchResult> &future,
CPlusPlus::Document::Ptr doc,
CPlusPlus::Snapshot snapshot) = 0;
};
class SearchClassDeclarationsFactory: public SemanticSearchFactory
{
QString _text;
QTextDocument::FindFlags _findFlags;
public:
SearchClassDeclarationsFactory(const QString &text, QTextDocument::FindFlags findFlags)
: _text(text), _findFlags(findFlags)
{ }
virtual SemanticSearch *create(QFutureInterface<Utils::FileSearchResult> &future,
CPlusPlus::Document::Ptr doc,
CPlusPlus::Snapshot snapshot);
};
class SearchFunctionCallFactory: public SemanticSearchFactory
{
QString _text;
QTextDocument::FindFlags _findFlags;
public:
SearchFunctionCallFactory(const QString &text, QTextDocument::FindFlags findFlags)
: _text(text), _findFlags(findFlags)
{ }
virtual SemanticSearch *create(QFutureInterface<Utils::FileSearchResult> &future,
CPlusPlus::Document::Ptr doc,
CPlusPlus::Snapshot snapshot);
};
QFuture<Utils::FileSearchResult> semanticSearch(QPointer<CppModelManager> modelManager,
SemanticSearchFactory::Ptr factory);
} // end of namespace Internal
} // end of namespace CppTools
#endif // CPPSEMANTICSEARCH_H
...@@ -23,7 +23,6 @@ HEADERS += completionsettingspage.h \ ...@@ -23,7 +23,6 @@ HEADERS += completionsettingspage.h \
searchsymbols.h \ searchsymbols.h \
cppdoxygen.h \ cppdoxygen.h \
cppfilesettingspage.h \ cppfilesettingspage.h \
cppsemanticsearch.h \
cppfindreferences.h cppfindreferences.h
SOURCES += completionsettingspage.cpp \ SOURCES += completionsettingspage.cpp \
...@@ -39,7 +38,6 @@ SOURCES += completionsettingspage.cpp \ ...@@ -39,7 +38,6 @@ SOURCES += completionsettingspage.cpp \
cppdoxygen.cpp \ cppdoxygen.cpp \
cppfilesettingspage.cpp \ cppfilesettingspage.cpp \
abstracteditorsupport.cpp \ abstracteditorsupport.cpp \
cppsemanticsearch.cpp \
cppfindreferences.cpp cppfindreferences.cpp
FORMS += completionsettingspage.ui \ FORMS += completionsettingspage.ui \
......
...@@ -37,7 +37,6 @@ ...@@ -37,7 +37,6 @@
#include "cppmodelmanager.h" #include "cppmodelmanager.h"
#include "cpptoolsconstants.h" #include "cpptoolsconstants.h"
#include "cppquickopenfilter.h" #include "cppquickopenfilter.h"
#include "cppsemanticsearch.h"
#include <extensionsystem/pluginmanager.h> #include <extensionsystem/pluginmanager.h>
...@@ -75,113 +74,6 @@ enum { debug = 0 }; ...@@ -75,113 +74,6 @@ enum { debug = 0 };
CppToolsPlugin *CppToolsPlugin::m_instance = 0; CppToolsPlugin *CppToolsPlugin::m_instance = 0;
FindClassDeclarations::FindClassDeclarations(CppModelManager *modelManager)
: _modelManager(modelManager),
_resultWindow(ExtensionSystem::PluginManager::instance()->getObject<Find::SearchResultWindow>())
{
m_watcher.setPendingResultsLimit(1);
connect(&m_watcher, SIGNAL(resultReadyAt(int)), this, SLOT(displayResult(int)));
connect(&m_watcher, SIGNAL(finished()), this, SLOT(searchFinished()));
}
void FindClassDeclarations::findAll(const QString &text, QTextDocument::FindFlags findFlags)
{
Find::SearchResult *search = _resultWindow->startNewSearch();
connect(search, SIGNAL(activated(Find::SearchResultItem)),
this, SLOT(openEditor(Find::SearchResultItem)));
_resultWindow->popup(true);
Core::ProgressManager *progressManager = Core::ICore::instance()->progressManager();
SemanticSearchFactory::Ptr factory(new SearchClassDeclarationsFactory(text, findFlags));
QFuture<Utils::FileSearchResult> result = semanticSearch(_modelManager, factory);
m_watcher.setFuture(result);
Core::FutureProgress *progress = progressManager->addTask(result, tr("Search class"),
CppTools::Constants::TASK_INDEX,
Core::ProgressManager::CloseOnSuccess);
connect(progress, SIGNAL(clicked()), _resultWindow, SLOT(popup()));
}
void FindClassDeclarations::displayResult(int index)
{
Utils::FileSearchResult result = m_watcher.future().resultAt(index);
_resultWindow->addResult(result.fileName,
result.lineNumber,
result.matchingLine,
result.matchStart,
result.matchLength);
}
void FindClassDeclarations::searchFinished()
{
emit changed();
}
void FindClassDeclarations::openEditor(const Find::SearchResultItem &item)
{
TextEditor::BaseTextEditor::openEditorAt(item.fileName, item.lineNumber, item.searchTermStart);
}
//////
FindFunctionCalls::FindFunctionCalls(CppModelManager *modelManager)
: _modelManager(modelManager),
_resultWindow(ExtensionSystem::PluginManager::instance()->getObject<Find::SearchResultWindow>())
{
m_watcher.setPendingResultsLimit(1);
connect(&m_watcher, SIGNAL(resultReadyAt(int)), this, SLOT(displayResult(int)));
connect(&m_watcher, SIGNAL(finished()), this, SLOT(searchFinished()));
}
void FindFunctionCalls::findAll(const QString &text, QTextDocument::FindFlags findFlags)
{
Find::SearchResult *search = _resultWindow->startNewSearch();
connect(search, SIGNAL(activated(Find::SearchResultItem)),
this, SLOT(openEditor(Find::SearchResultItem)));
_resultWindow->popup(true);
Core::ProgressManager *progressManager = Core::ICore::instance()->progressManager();
SemanticSearchFactory::Ptr factory(new SearchFunctionCallFactory(text, findFlags));
QFuture<Utils::FileSearchResult> result = semanticSearch(_modelManager, factory);
m_watcher.setFuture(result);
Core::FutureProgress *progress = progressManager->addTask(result, tr("Search functions"),
CppTools::Constants::TASK_INDEX,
Core::ProgressManager::CloseOnSuccess);
connect(progress, SIGNAL(clicked()), _resultWindow, SLOT(popup()));
}
void FindFunctionCalls::displayResult(int index)
{
Utils::FileSearchResult result = m_watcher.future().resultAt(index);
_resultWindow->addResult(result.fileName,
result.lineNumber,
result.matchingLine,
result.matchStart,
result.matchLength);
}
void FindFunctionCalls::searchFinished()
{
emit changed();
}
void FindFunctionCalls::openEditor(const Find::SearchResultItem &item)
{
TextEditor::BaseTextEditor::openEditorAt(item.fileName, item.lineNumber, item.searchTermStart);
}
CppToolsPlugin::CppToolsPlugin() : CppToolsPlugin::CppToolsPlugin() :
m_context(-1), m_context(-1),
m_modelManager(0), m_modelManager(0),
...@@ -221,9 +113,6 @@ bool CppToolsPlugin::initialize(const QStringList &arguments, QString *error) ...@@ -221,9 +113,6 @@ bool CppToolsPlugin::initialize(const QStringList &arguments, QString *error)
addAutoReleasedObject(new CompletionSettingsPage(m_completion)); addAutoReleasedObject(new CompletionSettingsPage(m_completion));
addAutoReleasedObject(new CppFileSettingsPage(m_fileSettings)); addAutoReleasedObject(new CppFileSettingsPage(m_fileSettings));
addAutoReleasedObject(new FindClassDeclarations(m_modelManager));
addAutoReleasedObject(new FindFunctionCalls(m_modelManager));
// Menus // Menus
Core::ActionContainer *mtools = am->actionContainer(Core::Constants::M_TOOLS); Core::ActionContainer *mtools = am->actionContainer(Core::Constants::M_TOOLS);
Core::ActionContainer *mcpptools = am->createMenu(CppTools::Constants::M_TOOLS_CPP);