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

3
#include "cppchecksymbols.h"
4
#include "cppmodelmanager.h"
5 6
#include "cppprojectfile.h"
#include "cppsnapshotupdater.h"
7
#include "cppsourceprocessor.h"
8
#include "cpptoolsconstants.h"
9
#include "cpptoolsplugin.h"
10
#include "searchsymbols.h"
11 12 13

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

15 16
#include <cplusplus/LookupContext.h>
#include <utils/qtcassert.h>
17 18 19
#include <utils/runextensions.h>

#include <QCoreApplication>
20
#include <QElapsedTimer>
21 22 23 24

using namespace CppTools;
using namespace CppTools::Internal;

25
static const bool DumpFileNameWhileParsing = qgetenv("QTC_DUMP_FILENAME_WHILE_PARSING") == "1";
26
static const bool FindErrorsIndexing = qgetenv("QTC_FIND_ERRORS_INDEXING") == "1";
27

28 29
namespace {

30
class ParseParams
31
{
32 33 34 35 36 37 38 39 40
public:
    ParseParams()
        : dumpFileNameWhileParsing(DumpFileNameWhileParsing)
        , revision(0)
    {}

    int dumpFileNameWhileParsing;
    int revision;
    ProjectPart::HeaderPaths headerPaths;
41
    WorkingCopy workingCopy;
42 43 44
    QStringList sourceFiles;
};

45
class WriteTaskFileForDiagnostics
46
{
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    Q_DISABLE_COPY(WriteTaskFileForDiagnostics)

public:
    WriteTaskFileForDiagnostics()
        : m_processedDiagnostics(0)
    {
        const QString fileName = QDir::tempPath()
                + QLatin1String("/qtc_findErrorsIndexing.diagnostics.")
                + QDateTime::currentDateTime().toString(QLatin1String("yyMMdd_HHmm"))
                + QLatin1String(".tasks");

        m_file.setFileName(fileName);
        Q_ASSERT(m_file.open(QIODevice::WriteOnly | QIODevice::Text));
        m_out.setDevice(&m_file);

        qDebug("FindErrorsIndexing: Task file for diagnostics is \"%s\".",
               qPrintable(m_file.fileName()));
    }

    ~WriteTaskFileForDiagnostics()
    {
        qDebug("FindErrorsIndexing: %d diagnostic messages written to \"%s\".",
               m_processedDiagnostics, qPrintable(m_file.fileName()));
    }

    int processedDiagnostics() const { return m_processedDiagnostics; }

    void process(const CPlusPlus::Document::Ptr document)
    {
        using namespace CPlusPlus;
        const QString fileName = document->fileName();

        foreach (const Document::DiagnosticMessage &message, document->diagnosticMessages()) {
            ++m_processedDiagnostics;

            QString type;
            switch (message.level()) {
            case Document::DiagnosticMessage::Warning:
                type = QLatin1String("warn"); break;
            case Document::DiagnosticMessage::Error:
            case Document::DiagnosticMessage::Fatal:
                type = QLatin1String("err"); break;
            default:
                break;
            }

            // format: file\tline\ttype\tdescription
            m_out << fileName << "\t"
                  << message.line() << "\t"
                  << type << "\t"
                  << message.text() << "\n";
        }
    }

private:
    QFile m_file;
    QTextStream m_out;
    int m_processedDiagnostics;
};

void classifyFiles(const QStringList &files, QStringList *headers, QStringList *sources)
{
    foreach (const QString &file, files) {
        if (ProjectFile::isSource(ProjectFile::classify(file)))
            sources->append(file);
        else
            headers->append(file);
    }
}

void indexFindErrors(QFutureInterface<void> &future, const ParseParams params)
{
    QStringList files = params.sourceFiles;
    files.sort();
    QStringList sources, headers;
    classifyFiles(files, &headers, &sources);
    files = sources + headers;

    WriteTaskFileForDiagnostics taskFileWriter;
    QElapsedTimer timer;
    timer.start();

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

        const QString file = files.at(i);
        qDebug("FindErrorsIndexing: \"%s\"", qPrintable(file));

        // Parse the file as precisely as possible
        SnapshotUpdater updater(file);
        updater.setReleaseSourceAndAST(false);
        updater.update(params.workingCopy);
        CPlusPlus::Document::Ptr document = updater.document();
        QTC_ASSERT(document, return);
144

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
        // Write diagnostic messages
        taskFileWriter.process(document);

        // Look up symbols
        CPlusPlus::LookupContext context(document, updater.snapshot());
        CheckSymbols::go(document, context, QList<CheckSymbols::Result>()).waitForFinished();

        document->releaseSourceAndAST();

        future.setProgressValue(files.size() - (files.size() - (i + 1)));
    }

    const QTime format = QTime(0, 0, 0, 0).addMSecs(timer.elapsed() + 500);
    const QString time = format.toString(QLatin1String("hh:mm:ss"));
    qDebug("FindErrorsIndexing: Finished after %s.", qPrintable(time));
}

void index(QFutureInterface<void> &future, const ParseParams params)
{
164 165 166 167 168 169 170
    QScopedPointer<CppSourceProcessor> sourceProcessor(CppModelManager::createSourceProcessor());
    sourceProcessor->setDumpFileNameWhileParsing(params.dumpFileNameWhileParsing);
    sourceProcessor->setRevision(params.revision);
    sourceProcessor->setHeaderPaths(params.headerPaths);
    sourceProcessor->setWorkingCopy(params.workingCopy);

    QStringList files = params.sourceFiles;
171

172 173
    QStringList sources;
    QStringList headers;
174
    classifyFiles(files, &headers, &sources);
175

176
    foreach (const QString &file, files)
177
        sourceProcessor->removeFromCache(file);
178 179 180 181 182

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

183
    sourceProcessor->setTodo(files);
184

185
    const QString conf = CppModelManagerInterface::configurationFileName();
186 187
    bool processingHeaders = false;

188
    CppModelManager *cmm = CppModelManager::instance();
189
    const ProjectPart::HeaderPaths fallbackHeaderPaths = cmm->headerPaths();
190 191 192 193 194 195 196 197 198 199
    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;
200
        if (isSourceFile) {
201
            (void) sourceProcessor->run(conf);
202
        } else if (!processingHeaders) {
203
            (void) sourceProcessor->run(conf);
204 205 206 207

            processingHeaders = true;
        }

208
        QList<ProjectPart::Ptr> parts = cmm->projectPart(fileName);
209 210 211 212
        ProjectPart::HeaderPaths headerPaths = parts.isEmpty()
                ? fallbackHeaderPaths
                : parts.first()->headerPaths;
        sourceProcessor->setHeaderPaths(headerPaths);
213
        sourceProcessor->run(fileName);
214

215
        future.setProgressValue(files.size() - sourceProcessor->todo().size());
216 217

        if (isSourceFile)
218
            sourceProcessor->resetEnvironment();
219
    }
220 221 222 223 224 225 226 227 228 229 230 231 232 233
}

void parse(QFutureInterface<void> &future, const ParseParams params)
{
    const QStringList files = params.sourceFiles;
    if (files.isEmpty())
        return;

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

    if (FindErrorsIndexing)
        indexFindErrors(future, params);
    else
        index(future, params);
234 235

    future.setProgressValue(files.size());
236
    CppModelManager::instance()->finishedRefreshingSourceFiles(files);
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
}

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()
    {}

252
    void runSearch(QFutureInterface<Core::SearchResultItem> &future)
253 254 255 256 257
    {
        future.setProgressRange(0, m_snapshot.size());
        future.setProgressValue(0);
        int progress = 0;

258
        SearchSymbols search(CppToolsPlugin::stringTable());
259 260 261
        search.setSymbolsToSearchFor(m_parameters.types);
        CPlusPlus::Snapshot::const_iterator it = m_snapshot.begin();

262
        QString findString = (m_parameters.flags & Core::FindRegularExpression
263
                              ? m_parameters.text : QRegExp::escape(m_parameters.text));
264
        if (m_parameters.flags & Core::FindWholeWords)
265
            findString = QString::fromLatin1("\\b%1\\b").arg(findString);
266
        QRegExp matcher(findString, (m_parameters.flags & Core::FindCaseSensitively
267 268 269 270 271 272 273
                                     ? 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())) {
274
                QVector<Core::SearchResultItem> resultItems;
275
                auto filter = [&](const IndexItem::Ptr &info) -> IndexItem::VisitorResult {
276
                    if (matcher.indexIn(info->symbolName()) != -1) {
277 278
                        QString text = info->symbolName();
                        QString scope = info->symbolScope();
279
                        if (info->type() == IndexItem::Function) {
280
                            QString name;
281 282
                            info->unqualifiedNameAndScope(info->symbolName(), &name, &scope);
                            text = name + info->symbolType();
283 284
                        } else if (info->type() == IndexItem::Declaration){
                            text = IndexItem::representDeclaration(info->symbolName(),
285
                                                                       info->symbolType());
286
                        }
287

288
                        Core::SearchResultItem item;
289
                        item.path = scope.split(QLatin1String("::"), QString::SkipEmptyParts);
290
                        item.text = text;
291 292
                        item.textMarkPos = -1;
                        item.textMarkLength = 0;
293
                        item.icon = info->icon();
294 295 296 297
                        item.lineNumber = -1;
                        item.userData = qVariantFromValue(info);
                        resultItems << item;
                    }
298 299 300 301

                    return IndexItem::Recurse;
                };
                search(it.value())->visitAllChildren(filter);
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
                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

321 322
BuiltinIndexingSupport::BuiltinIndexingSupport()
    : m_revision(0)
323 324 325 326 327 328 329
{
    m_synchronizer.setCancelOnWait(true);
}

BuiltinIndexingSupport::~BuiltinIndexingSupport()
{}

330 331
QFuture<void> BuiltinIndexingSupport::refreshSourceFiles(const QStringList &sourceFiles,
    CppModelManagerInterface::ProgressNotificationMode mode)
332 333 334
{
    CppModelManager *mgr = CppModelManager::instance();

335 336 337 338 339
    ParseParams params;
    params.revision = ++m_revision;
    params.headerPaths = mgr->headerPaths();
    params.workingCopy = mgr->workingCopy();
    params.sourceFiles = sourceFiles;
340

341
    QFuture<void> result = QtConcurrent::run(&parse, params);
342 343 344 345 346 347 348

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

        m_synchronizer.clearFutures();

        foreach (const QFuture<void> &future, futures) {
349
            if (!(future.isFinished() || future.isCanceled()))
350 351 352 353 354 355
                m_synchronizer.addFuture(future);
        }
    }

    m_synchronizer.addFuture(result);

356
    if (mode == CppModelManagerInterface::ForcedProgressNotification || sourceFiles.count() > 1) {
357
        Core::ProgressManager::addTask(result, QCoreApplication::translate("CppTools::Internal::BuiltinIndexingSupport", "Parsing C/C++ Files"),
358
                                                CppTools::Constants::TASK_INDEX);
359 360 361 362 363 364 365 366 367
    }

    return result;
}

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

bool BuiltinIndexingSupport::isFindErrorsIndexingActive()
{
    return FindErrorsIndexing;
}