builtinindexingsupport.cpp 6.93 KB
Newer Older
1
#include "builtinindexingsupport.h"
2

3
#include "cppmodelmanager.h"
4
#include "cpppreprocessor.h"
5
#include "searchsymbols.h"
6 7
#include "cpptoolsconstants.h"
#include "cppprojectfile.h"
8 9 10

#include <coreplugin/icore.h>
#include <coreplugin/progressmanager/progressmanager.h>
11

12 13 14 15 16 17 18 19 20 21 22
#include <utils/runextensions.h>

#include <QCoreApplication>

using namespace CppTools;
using namespace CppTools::Internal;

namespace {

static void parse(QFutureInterface<void> &future,
                  CppPreprocessor *preproc,
23
                  QStringList files)
24 25 26 27 28 29 30 31
{
    if (files.isEmpty())
        return;

    QStringList sources;
    QStringList headers;

    foreach (const QString &file, files) {
32
        preproc->removeFromCache(file);
33
        if (ProjectFile::isSource(ProjectFile::classify(file)))
34 35 36 37 38 39 40 41 42 43 44 45 46
            sources.append(file);
        else
            headers.append(file);
    }

    const int sourceCount = sources.size();
    files = sources;
    files += headers;

    preproc->setTodo(files);

    future.setProgressRange(0, files.size());

47
    const QString conf = CppModelManagerInterface::configurationFileName();
48 49 50 51 52 53 54 55 56 57 58 59
    bool processingHeaders = false;

    for (int i = 0; i < files.size(); ++i) {
        if (future.isPaused())
            future.waitForResume();

        if (future.isCanceled())
            break;

        const QString fileName = files.at(i);

        const bool isSourceFile = i < sourceCount;
60
        if (isSourceFile) {
61
            (void) preproc->run(conf);
62
        } else if (!processingHeaders) {
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
            (void) preproc->run(conf);

            processingHeaders = true;
        }

        preproc->run(fileName);

        future.setProgressValue(files.size() - preproc->todo().size());

        if (isSourceFile)
            preproc->resetEnvironment();
    }

    future.setProgressValue(files.size());
    preproc->modelManager()->finishedRefreshingSourceFiles(files);

    delete preproc;
}

class BuiltinSymbolSearcher: public SymbolSearcher
{
public:
    BuiltinSymbolSearcher(const CPlusPlus::Snapshot &snapshot,
                          Parameters parameters, QSet<QString> fileNames)
        : m_snapshot(snapshot)
        , m_parameters(parameters)
        , m_fileNames(fileNames)
    {}

    ~BuiltinSymbolSearcher()
    {}

    void runSearch(QFutureInterface<Find::SearchResultItem> &future)
    {
        future.setProgressRange(0, m_snapshot.size());
        future.setProgressValue(0);
        int progress = 0;

        SearchSymbols search;
        search.setSymbolsToSearchFor(m_parameters.types);
        CPlusPlus::Snapshot::const_iterator it = m_snapshot.begin();

        QString findString = (m_parameters.flags & Find::FindRegularExpression
                              ? m_parameters.text : QRegExp::escape(m_parameters.text));
        if (m_parameters.flags & Find::FindWholeWords)
            findString = QString::fromLatin1("\\b%1\\b").arg(findString);
        QRegExp matcher(findString, (m_parameters.flags & Find::FindCaseSensitively
                                     ? Qt::CaseSensitive : Qt::CaseInsensitive));
        while (it != m_snapshot.end()) {
            if (future.isPaused())
                future.waitForResume();
            if (future.isCanceled())
                break;
            if (m_fileNames.isEmpty() || m_fileNames.contains(it.value()->fileName())) {
                QVector<Find::SearchResultItem> resultItems;
                QList<ModelItemInfo> modelInfos = search(it.value());
                foreach (const ModelItemInfo &info, modelInfos) {
                    int index = matcher.indexIn(info.symbolName);
                    if (index != -1) {
122 123 124 125 126 127 128 129 130 131
                        QString text = info.symbolName;
                        QString scope = info.symbolScope;
                        if (info.type == ModelItemInfo::Method) {
                            QString name;
                            info.unqualifiedNameAndScope(info.symbolName, &name, &scope);
                            text = name + info.symbolType;
                        } else if (info.type == ModelItemInfo::Declaration){
                            text = ModelItemInfo::representDeclaration(info.symbolName,
                                                                       info.symbolType);
                        }
132

133
                        Find::SearchResultItem item;
134
                        item.path = scope.split(QLatin1String("::"), QString::SkipEmptyParts);
135
                        item.text = text;
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
                        item.textMarkPos = -1;
                        item.textMarkLength = 0;
                        item.icon = info.icon;
                        item.lineNumber = -1;
                        item.userData = qVariantFromValue(info);
                        resultItems << item;
                    }
                }
                if (!resultItems.isEmpty())
                    future.reportResults(resultItems);
            }
            ++it;
            ++progress;
            future.setProgressValue(progress);
        }
        if (future.isPaused())
            future.waitForResume();
    }

private:
    const CPlusPlus::Snapshot m_snapshot;
    const Parameters m_parameters;
    const QSet<QString> m_fileNames;
};

} // anonymous namespace

163 164
BuiltinIndexingSupport::BuiltinIndexingSupport()
    : m_revision(0)
165 166 167 168 169 170 171 172
{
    m_synchronizer.setCancelOnWait(true);
    m_dumpFileNameWhileParsing = !qgetenv("QTCREATOR_DUMP_FILENAME_WHILE_PARSING").isNull();
}

BuiltinIndexingSupport::~BuiltinIndexingSupport()
{}

173 174
QFuture<void> BuiltinIndexingSupport::refreshSourceFiles(const QStringList &sourceFiles,
    CppModelManagerInterface::ProgressNotificationMode mode)
175 176 177 178 179 180 181 182 183 184
{
    CppModelManager *mgr = CppModelManager::instance();
    const WorkingCopy workingCopy = mgr->workingCopy();

    CppPreprocessor *preproc = new CppPreprocessor(mgr, m_dumpFileNameWhileParsing);
    preproc->setRevision(++m_revision);
    preproc->setIncludePaths(mgr->includePaths());
    preproc->setFrameworkPaths(mgr->frameworkPaths());
    preproc->setWorkingCopy(workingCopy);

185
    QFuture<void> result = QtConcurrent::run(&parse, preproc, sourceFiles);
186 187 188 189 190 191 192

    if (m_synchronizer.futures().size() > 10) {
        QList<QFuture<void> > futures = m_synchronizer.futures();

        m_synchronizer.clearFutures();

        foreach (const QFuture<void> &future, futures) {
193
            if (!(future.isFinished() || future.isCanceled()))
194 195 196 197 198 199
                m_synchronizer.addFuture(future);
        }
    }

    m_synchronizer.addFuture(result);

200
    if (mode == CppModelManagerInterface::ForcedProgressNotification || sourceFiles.count() > 1) {
201
        Core::ProgressManager::addTask(result, QCoreApplication::translate("CppTools::Internal::BuiltinIndexingSupport", "Parsing"),
202
                                                CppTools::Constants::TASK_INDEX);
203 204 205 206 207 208 209 210 211
    }

    return result;
}

SymbolSearcher *BuiltinIndexingSupport::createSymbolSearcher(SymbolSearcher::Parameters parameters, QSet<QString> fileNames)
{
    return new BuiltinSymbolSearcher(CppModelManager::instance()->snapshot(), parameters, fileNames);
}