cppmodelmanager.cpp 44.8 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8 9 10 11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
15
**
16 17 18 19 20 21 22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
con's avatar
con committed
23
**
hjk's avatar
hjk committed
24
****************************************************************************/
hjk's avatar
hjk committed
25

con's avatar
con committed
26
#include "cppmodelmanager.h"
27

28
#include "abstracteditorsupport.h"
29
#include "baseeditordocumentprocessor.h"
30
#include "builtinindexingsupport.h"
31
#include "cppcodemodelinspectordumper.h"
32
#include "cppfindreferences.h"
33
#include "cppindexingsupport.h"
34
#include "cppmodelmanagersupportinternal.h"
35
#include "cpprefactoringchanges.h"
36
#include "cpprefactoringengine.h"
37
#include "cppsourceprocessor.h"
38
#include "cpptoolsconstants.h"
39
#include "cpptoolsplugin.h"
40
#include "cpptoolsreuse.h"
41
#include "editordocumenthandle.h"
42
#include "symbolfinder.h"
43
#include "followsymbolinterface.h"
con's avatar
con committed
44

45
#include <coreplugin/documentmanager.h>
46 47
#include <coreplugin/icore.h>
#include <coreplugin/progressmanager/progressmanager.h>
48 49
#include <coreplugin/editormanager/editormanager.h>
#include <texteditor/textdocument.h>
50
#include <projectexplorer/project.h>
51
#include <projectexplorer/projectexplorer.h>
52
#include <projectexplorer/projectmacro.h>
53
#include <projectexplorer/session.h>
54
#include <extensionsystem/pluginmanager.h>
55
#include <utils/fileutils.h>
hjk's avatar
hjk committed
56
#include <utils/qtcassert.h>
con's avatar
con committed
57

58 59
#include <QCoreApplication>
#include <QDebug>
60
#include <QDir>
61
#include <QFutureWatcher>
62 63
#include <QMutexLocker>
#include <QTextBlock>
64
#include <QThreadPool>
65
#include <QTimer>
66

67 68
#if defined(QTCREATOR_WITH_DUMP_AST) && defined(Q_CC_GNU)
#define WITH_AST_DUMP
69 70
#include <iostream>
#include <sstream>
71
#endif
hjk's avatar
hjk committed
72

73 74
static const bool DumpProjectInfo = qgetenv("QTC_DUMP_PROJECT_INFO") == "1";

75 76
using namespace CppTools;
using namespace CppTools::Internal;
con's avatar
con committed
77 78
using namespace CPlusPlus;

79
#ifdef QTCREATOR_WITH_DUMP_AST
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

#include <cxxabi.h>

class DumpAST: protected ASTVisitor
{
public:
    int depth;

    DumpAST(Control *control)
        : ASTVisitor(control), depth(0)
    { }

    void operator()(AST *ast)
    { accept(ast); }

protected:
    virtual bool preVisit(AST *ast)
    {
        std::ostringstream s;
        PrettyPrinter pp(control(), s);
        pp(ast);
        QString code = QString::fromStdString(s.str());
        code.replace('\n', ' ');
        code.replace(QRegExp("\\s+"), " ");

        const char *name = abi::__cxa_demangle(typeid(*ast).name(), 0, 0, 0) + 11;

        QByteArray ind(depth, ' ');
        ind += name;

        printf("%-40s %s\n", ind.constData(), qPrintable(code));
        ++depth;
        return true;
    }

    virtual void postVisit(AST *)
    { --depth; }
};

Roberto Raggi's avatar
Roberto Raggi committed
119
#endif // QTCREATOR_WITH_DUMP_AST
120

121
namespace CppTools {
122 123 124

using REType = RefactoringEngineType;

125 126 127 128 129 130 131 132 133 134
namespace Internal {

static QMutex m_instanceMutex;
static CppModelManager *m_instance;

class CppModelManagerPrivate
{
public:
    // Snapshot
    mutable QMutex m_snapshotMutex;
135
    Snapshot m_snapshot;
136 137 138 139

    // Project integration
    mutable QMutex m_projectMutex;
    QMap<ProjectExplorer::Project *, ProjectInfo> m_projectToProjectsInfo;
140
    QHash<ProjectExplorer::Project *, bool> m_projectToIndexerCanceled;
141
    QMap<Utils::FileName, QList<ProjectPart::Ptr> > m_fileToProjectParts;
142
    QMap<QString, ProjectPart::Ptr> m_projectPartIdToProjectProjectPart;
143 144 145
    // The members below are cached/(re)calculated from the projects and/or their project parts
    bool m_dirty;
    QStringList m_projectFiles;
146
    ProjectPartHeaderPaths m_headerPaths;
147
    ProjectExplorer::Macros m_definedMacros;
148 149

    // Editor integration
150 151
    mutable QMutex m_cppEditorDocumentsMutex;
    QMap<QString, CppEditorDocumentHandle *> m_cppEditorDocuments;
152 153
    QSet<AbstractEditorSupport *> m_extraEditorSupports;

154 155 156
    // Model Manager Supports for e.g. completion and highlighting
    ModelManagerSupport::Ptr m_builtinModelManagerSupport;
    ModelManagerSupport::Ptr m_activeModelManagerSupport;
157 158 159 160 161 162 163 164

    // Indexing
    CppIndexingSupport *m_indexingSupporter;
    CppIndexingSupport *m_internalIndexingSupport;
    bool m_indexerEnabled;

    CppFindReferences *m_findReferences;

165
    SymbolFinder m_symbolFinder;
166
    QThreadPool m_threadPool;
167

168 169
    bool m_enableGC;
    QTimer m_delayedGcTimer;
Marco Bubke's avatar
Marco Bubke committed
170 171

    // Refactoring
172
    using REHash = QMap<REType, RefactoringEngineInterface *>;
173
    REHash m_refactoringEngines;
174 175 176 177 178
};

} // namespace Internal

const char pp_configuration[] =
con's avatar
con committed
179
    "# 1 \"<configuration>\"\n"
180
    "#define Q_CREATOR_RUN 1\n"
con's avatar
con committed
181 182 183 184 185 186
    "#define __cplusplus 1\n"
    "#define __extension__\n"
    "#define __context__\n"
    "#define __range__\n"
    "#define   restrict\n"
    "#define __restrict\n"
187
    "#define __restrict__\n"
con's avatar
con committed
188

189 190 191 192
    "#define __complex__\n"
    "#define __imag__\n"
    "#define __real__\n"

193 194
    "#define __builtin_va_arg(a,b) ((b)0)\n"

195 196
    "#define _Pragma(x)\n" // C99 _Pragma operator

197 198 199 200 201 202
    "#define __func__ \"\"\n"

    // ### add macros for gcc
    "#define __PRETTY_FUNCTION__ \"\"\n"
    "#define __FUNCTION__ \"\"\n"

con's avatar
con committed
203 204
    // ### add macros for win32
    "#define __cdecl\n"
205
    "#define __stdcall\n"
206
    "#define __thiscall\n"
con's avatar
con committed
207 208 209 210 211
    "#define QT_WA(x) x\n"
    "#define CALLBACK\n"
    "#define STDMETHODCALLTYPE\n"
    "#define __RPC_FAR\n"
    "#define __declspec(a)\n"
212 213 214
    "#define STDMETHOD(method) virtual HRESULT STDMETHODCALLTYPE method\n"
    "#define __try try\n"
    "#define __except catch\n"
215 216
    "#define __finally\n"
    "#define __inline inline\n"
217
    "#define __forceinline inline\n"
218 219 220 221 222 223 224 225
    "#define __pragma(x)\n"
    "#define __w64\n"
    "#define __int64 long long\n"
    "#define __int32 long\n"
    "#define __int16 short\n"
    "#define __int8 char\n"
    "#define __ptr32\n"
    "#define __ptr64\n";
con's avatar
con committed
226

227
QSet<QString> CppModelManager::timeStampModifiedFiles(const QList<Document::Ptr> &documentsToCheck)
228
{
229
    QSet<QString> sourceFiles;
230

231
    foreach (const Document::Ptr doc, documentsToCheck) {
232 233
        const QDateTime lastModified = doc->lastModified();

234
        if (!lastModified.isNull()) {
235 236 237
            QFileInfo fileInfo(doc->fileName());

            if (fileInfo.exists() && fileInfo.lastModified() != lastModified)
238
                sourceFiles.insert(doc->fileName());
239 240 241
        }
    }

242 243 244
    return sourceFiles;
}

245 246 247 248 249 250 251 252 253 254 255 256
/*!
 * \brief createSourceProcessor Create a new source processor, which will signal the
 * model manager when a document has been processed.
 *
 * Indexed file is truncated version of fully parsed document: copy of source
 * code and full AST will be dropped when indexing is done.
 *
 * \return a new source processor object, which the caller needs to delete when finished.
 */
CppSourceProcessor *CppModelManager::createSourceProcessor()
{
    CppModelManager *that = instance();
Orgad Shaneh's avatar
Orgad Shaneh committed
257
    return new CppSourceProcessor(that->snapshot(), [that](const Document::Ptr &doc) {
258 259 260 261 262
        const Document::Ptr previousDocument = that->document(doc->fileName());
        const unsigned newRevision = previousDocument.isNull()
                ? 1U
                : previousDocument->revision() + 1;
        doc->setRevision(newRevision);
263 264 265 266 267
        that->emitDocumentUpdated(doc);
        doc->releaseSourceAndAST();
    });
}

268 269 270 271 272
QString CppModelManager::editorConfigurationFileName()
{
    return QLatin1String("<per-editor-defines>");
}

273 274 275
static RefactoringEngineInterface *getRefactoringEngine(
        CppModelManagerPrivate::REHash &engines, bool excludeClangCodeModel = true)
{
276
    QTC_ASSERT(!engines.empty(), return nullptr;);
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
    RefactoringEngineInterface *currentEngine = engines[REType::BuiltIn];
    if (!excludeClangCodeModel && engines.find(REType::ClangCodeModel) != engines.end()) {
        currentEngine = engines[REType::ClangCodeModel];
    } else if (engines.find(REType::ClangRefactoring) != engines.end()) {
        RefactoringEngineInterface *engine = engines[REType::ClangRefactoring];
        if (engine->isRefactoringEngineAvailable())
            currentEngine = engine;
    }
    return currentEngine;
}

void CppModelManager::startLocalRenaming(const CursorInEditor &data,
                                         CppTools::ProjectPart *projectPart,
                                         RenameCallback &&renameSymbolsCallback)
{
292
    RefactoringEngineInterface *engine = getRefactoringEngine(d->m_refactoringEngines,
293
                                                              false);
294
    QTC_ASSERT(engine, return;);
295 296 297
    engine->startLocalRenaming(data, projectPart, std::move(renameSymbolsCallback));
}

298 299
void CppModelManager::globalRename(const CursorInEditor &data, UsagesCallback &&renameCallback,
                                   const QString &replacement)
300
{
301
    RefactoringEngineInterface *engine = getRefactoringEngine(d->m_refactoringEngines);
302
    QTC_ASSERT(engine, return;);
303
    engine->globalRename(data, std::move(renameCallback), replacement);
304
}
305

306 307 308
void CppModelManager::findUsages(const CppTools::CursorInEditor &data,
                                 UsagesCallback &&showUsagesCallback) const
{
309
    RefactoringEngineInterface *engine = getRefactoringEngine(d->m_refactoringEngines);
310
    QTC_ASSERT(engine, return;);
311 312 313
    engine->findUsages(data, std::move(showUsagesCallback));
}

314 315 316 317 318 319 320 321 322 323 324 325 326
CppModelManager::Link CppModelManager::globalFollowSymbol(
        const CursorInEditor &data,
        const CPlusPlus::Snapshot &snapshot,
        const CPlusPlus::Document::Ptr &documentFromSemanticInfo,
        SymbolFinder *symbolFinder,
        bool inNextSplit) const
{
    RefactoringEngineInterface *engine = getRefactoringEngine(d->m_refactoringEngines);
    QTC_ASSERT(engine, return Link(););
    return engine->globalFollowSymbol(data, snapshot, documentFromSemanticInfo,
            symbolFinder, inNextSplit);
}

327 328
void CppModelManager::addRefactoringEngine(RefactoringEngineType type,
                                           RefactoringEngineInterface *refactoringEngine)
Marco Bubke's avatar
Marco Bubke committed
329
{
330
    instance()->d->m_refactoringEngines[type] = refactoringEngine;
Marco Bubke's avatar
Marco Bubke committed
331 332
}

333
void CppModelManager::removeRefactoringEngine(RefactoringEngineType type)
Marco Bubke's avatar
Marco Bubke committed
334
{
335
    instance()->d->m_refactoringEngines.remove(type);
Marco Bubke's avatar
Marco Bubke committed
336 337
}

338
FollowSymbolInterface &CppModelManager::followSymbolInterface() const
339 340 341 342
{
    return d->m_activeModelManagerSupport->followSymbolInterface();
}

343 344
QString CppModelManager::configurationFileName()
{
345
    return Preprocessor::configurationFileName();
346 347
}

348 349 350 351 352 353 354
void CppModelManager::updateModifiedSourceFiles()
{
    const Snapshot snapshot = this->snapshot();
    QList<Document::Ptr> documentsToCheck;
    foreach (const Document::Ptr document, snapshot)
        documentsToCheck << document;

355
    updateSourceFiles(timeStampModifiedFiles(documentsToCheck));
356 357
}

con's avatar
con committed
358 359
/*!
    \class CppTools::CppModelManager
360 361 362
    \brief The CppModelManager keeps tracks of the source files the code model is aware of.

    The CppModelManager manages the source files in a Snapshot object.
con's avatar
con committed
363

364 365 366 367
    The snapshot is updated in case e.g.
        * New files are opened/edited (Editor integration)
        * A project manager pushes updated project information (Project integration)
        * Files are garbage collected
con's avatar
con committed
368 369
*/

370 371
CppModelManager *CppModelManager::instance()
{
372 373 374 375 376 377 378 379
    if (m_instance)
        return m_instance;

    QMutexLocker locker(&m_instanceMutex);
    if (!m_instance)
        m_instance = new CppModelManager;

    return m_instance;
380 381
}

382
void CppModelManager::initializeBuiltinModelManagerSupport()
383 384 385 386
{
    d->m_builtinModelManagerSupport
            = ModelManagerSupportProviderInternal().createModelManagerSupport();
    d->m_activeModelManagerSupport = d->m_builtinModelManagerSupport;
387 388
    d->m_refactoringEngines[RefactoringEngineType::BuiltIn] =
            &d->m_activeModelManagerSupport->refactoringEngineInterface();
389 390
}

391
CppModelManager::CppModelManager(QObject *parent)
392
    : CppModelManagerBase(parent), d(new CppModelManagerPrivate)
con's avatar
con committed
393
{
394 395 396
    d->m_indexingSupporter = 0;
    d->m_enableGC = true;

397
    qRegisterMetaType<QSet<QString> >();
398 399
    connect(this, &CppModelManager::sourceFilesRefreshed,
            this, &CppModelManager::onSourceFilesRefreshed);
Erik Verbruggen's avatar
Erik Verbruggen committed
400

401 402
    d->m_findReferences = new CppFindReferences(this);
    d->m_indexerEnabled = qgetenv("QTC_NO_CODE_INDEXER") != "1";
Roberto Raggi's avatar
Roberto Raggi committed
403

404
    d->m_dirty = true;
405

406 407
    d->m_delayedGcTimer.setObjectName(QLatin1String("CppModelManager::m_delayedGcTimer"));
    d->m_delayedGcTimer.setSingleShot(true);
408
    connect(&d->m_delayedGcTimer, &QTimer::timeout, this, &CppModelManager::GC);
409

410 411 412 413 414 415 416
    auto sessionManager = ProjectExplorer::SessionManager::instance();
    connect(sessionManager, &ProjectExplorer::SessionManager::projectAdded,
            this, &CppModelManager::onProjectAdded);
    connect(sessionManager, &ProjectExplorer::SessionManager::aboutToRemoveProject,
            this, &CppModelManager::onAboutToRemoveProject);
    connect(sessionManager, &ProjectExplorer::SessionManager::aboutToLoadSession,
            this, &CppModelManager::onAboutToLoadSession);
417 418
    connect(sessionManager, &ProjectExplorer::SessionManager::startupProjectChanged,
            this, &CppModelManager::onActiveProjectChanged);
419 420 421 422

    connect(Core::EditorManager::instance(), &Core::EditorManager::currentEditorChanged,
            this, &CppModelManager::onCurrentEditorChanged);

423 424
    connect(Core::DocumentManager::instance(), &Core::DocumentManager::allDocumentsRenamed,
            this, &CppModelManager::renameIncludes);
con's avatar
con committed
425

426 427
    connect(Core::ICore::instance(), &Core::ICore::coreAboutToClose,
            this, &CppModelManager::onCoreAboutToClose);
428

con's avatar
con committed
429
    qRegisterMetaType<CPlusPlus::Document::Ptr>("CPlusPlus::Document::Ptr");
430
    qRegisterMetaType<QList<Document::DiagnosticMessage>>(
431
                "QList<CPlusPlus::Document::DiagnosticMessage>");
con's avatar
con committed
432

433
    initializeBuiltinModelManagerSupport();
434

435
    d->m_internalIndexingSupport = new BuiltinIndexingSupport;
con's avatar
con committed
436 437 438
}

CppModelManager::~CppModelManager()
439
{
440
    delete d->m_internalIndexingSupport;
441
    delete d;
442
}
con's avatar
con committed
443

444
Snapshot CppModelManager::snapshot() const
445
{
446 447
    QMutexLocker locker(&d->m_snapshotMutex);
    return d->m_snapshot;
448
}
con's avatar
con committed
449

450 451
Document::Ptr CppModelManager::document(const QString &fileName) const
{
452 453
    QMutexLocker locker(&d->m_snapshotMutex);
    return d->m_snapshot.document(fileName);
454 455 456 457 458 459 460
}

/// Replace the document in the snapshot.
///
/// \returns true if successful, false if the new document is out-dated.
bool CppModelManager::replaceDocument(Document::Ptr newDoc)
{
461
    QMutexLocker locker(&d->m_snapshotMutex);
462

463
    Document::Ptr previous = d->m_snapshot.document(newDoc->fileName());
464 465 466 467
    if (previous && (newDoc->revision() != 0 && newDoc->revision() < previous->revision()))
        // the new document is outdated
        return false;

468
    d->m_snapshot.insert(newDoc);
469 470 471
    return true;
}

472
/// Make sure that m_projectMutex is locked when calling this.
473 474
void CppModelManager::ensureUpdated()
{
475
    if (!d->m_dirty)
476 477
        return;

478 479 480 481
    d->m_projectFiles = internalProjectFiles();
    d->m_headerPaths = internalHeaderPaths();
    d->m_definedMacros = internalDefinedMacros();
    d->m_dirty = false;
482 483
}

484
QStringList CppModelManager::internalProjectFiles() const
con's avatar
con committed
485 486
{
    QStringList files;
487
    QMapIterator<ProjectExplorer::Project *, ProjectInfo> it(d->m_projectToProjectsInfo);
con's avatar
con committed
488 489
    while (it.hasNext()) {
        it.next();
490
        const ProjectInfo pinfo = it.value();
491
        foreach (const ProjectPart::Ptr &part, pinfo.projectParts()) {
492 493
            foreach (const ProjectFile &file, part->files)
                files += file.path;
494
        }
con's avatar
con committed
495
    }
496
    files.removeDuplicates();
con's avatar
con committed
497 498 499
    return files;
}

500
ProjectPartHeaderPaths CppModelManager::internalHeaderPaths() const
con's avatar
con committed
501
{
502
    ProjectPartHeaderPaths headerPaths;
503
    QMapIterator<ProjectExplorer::Project *, ProjectInfo> it(d->m_projectToProjectsInfo);
con's avatar
con committed
504 505
    while (it.hasNext()) {
        it.next();
506
        const ProjectInfo pinfo = it.value();
507
        foreach (const ProjectPart::Ptr &part, pinfo.projectParts()) {
508 509
            foreach (const ProjectPartHeaderPath &path, part->headerPaths) {
                const ProjectPartHeaderPath hp(QDir::cleanPath(path.path), path.type);
510 511 512 513
                if (!headerPaths.contains(hp))
                    headerPaths += hp;
            }
        }
con's avatar
con committed
514
    }
515
    return headerPaths;
con's avatar
con committed
516 517
}

518 519 520
static void addUnique(const ProjectExplorer::Macros &newMacros,
                      ProjectExplorer::Macros &macros,
                      QSet<ProjectExplorer::Macro> &alreadyIn)
521
{
522 523 524 525
    for (const ProjectExplorer::Macro &macro : newMacros) {
        if (!alreadyIn.contains(macro)) {
            macros += macro;
            alreadyIn.insert(macro);
526 527 528 529
        }
    }
}

530
ProjectExplorer::Macros CppModelManager::internalDefinedMacros() const
con's avatar
con committed
531
{
532 533
    ProjectExplorer::Macros macros;
    QSet<ProjectExplorer::Macro> alreadyIn;
534
    QMapIterator<ProjectExplorer::Project *, ProjectInfo> it(d->m_projectToProjectsInfo);
con's avatar
con committed
535 536
    while (it.hasNext()) {
        it.next();
537
        const ProjectInfo pinfo = it.value();
538 539 540
        for (const ProjectPart::Ptr &part : pinfo.projectParts()) {
            addUnique(part->toolChainMacros, macros, alreadyIn);
            addUnique(part->projectMacros, macros, alreadyIn);
541
        }
con's avatar
con committed
542 543 544 545
    }
    return macros;
}

546
/// This function will acquire mutexes!
547
void CppModelManager::dumpModelManagerConfiguration(const QString &logFileId)
548
{
549 550 551 552
    const Snapshot globalSnapshot = snapshot();
    const QString globalSnapshotTitle
        = QString::fromLatin1("Global/Indexing Snapshot (%1 Documents)").arg(globalSnapshot.size());

553
    CppCodeModelInspector::Dumper dumper(globalSnapshot, logFileId);
554 555 556
    dumper.dumpProjectInfos(projectInfos());
    dumper.dumpSnapshot(globalSnapshot, globalSnapshotTitle, /*isGlobalSnapshot=*/ true);
    dumper.dumpWorkingCopy(workingCopy());
557 558
    dumper.dumpMergedEntities(headerPaths(),
                              ProjectExplorer:: Macro::toByteArray(definedMacros()));
559 560
}

561 562 563 564 565
QSet<AbstractEditorSupport *> CppModelManager::abstractEditorSupports() const
{
    return d->m_extraEditorSupports;
}

566
void CppModelManager::addExtraEditorSupport(AbstractEditorSupport *editorSupport)
567
{
568
    d->m_extraEditorSupports.insert(editorSupport);
569 570
}

571
void CppModelManager::removeExtraEditorSupport(AbstractEditorSupport *editorSupport)
572
{
573
    d->m_extraEditorSupports.remove(editorSupport);
574 575
}

576
CppEditorDocumentHandle *CppModelManager::cppEditorDocument(const QString &filePath) const
577
{
578 579
    if (filePath.isEmpty())
        return 0;
580

581 582
    QMutexLocker locker(&d->m_cppEditorDocumentsMutex);
    return d->m_cppEditorDocuments.value(filePath, 0);
583 584
}

585
void CppModelManager::registerCppEditorDocument(CppEditorDocumentHandle *editorDocument)
586
{
587 588 589
    QTC_ASSERT(editorDocument, return);
    const QString filePath = editorDocument->filePath();
    QTC_ASSERT(!filePath.isEmpty(), return);
590

591 592 593
    QMutexLocker locker(&d->m_cppEditorDocumentsMutex);
    QTC_ASSERT(d->m_cppEditorDocuments.value(filePath, 0) == 0, return);
    d->m_cppEditorDocuments.insert(filePath, editorDocument);
594
}
595

596
void CppModelManager::unregisterCppEditorDocument(const QString &filePath)
597 598
{
    QTC_ASSERT(!filePath.isEmpty(), return);
599

600 601
    static short closedCppDocuments = 0;
    int openCppDocuments = 0;
602

603
    {
604 605 606 607
        QMutexLocker locker(&d->m_cppEditorDocumentsMutex);
        QTC_ASSERT(d->m_cppEditorDocuments.value(filePath, 0), return);
        QTC_CHECK(d->m_cppEditorDocuments.remove(filePath) == 1);
        openCppDocuments = d->m_cppEditorDocuments.size();
608 609
    }

610 611 612
    ++closedCppDocuments;
    if (openCppDocuments == 0 || closedCppDocuments == 5) {
        closedCppDocuments = 0;
613
        delayedGC();
614 615 616
    }
}

617
QList<int> CppModelManager::references(Symbol *symbol, const LookupContext &context)
618
{
619
    return d->m_findReferences->references(symbol, context);
620 621
}

622
void CppModelManager::findUsages(Symbol *symbol, const LookupContext &context)
Roberto Raggi's avatar
Roberto Raggi committed
623
{
624
    if (symbol->identifier())
625
        d->m_findReferences->findUsages(symbol, context);
626 627
}

628 629
void CppModelManager::renameUsages(Symbol *symbol,
                                   const LookupContext &context,
630
                                   const QString &replacement)
631 632
{
    if (symbol->identifier())
633
        d->m_findReferences->renameUsages(symbol, context, replacement);
Roberto Raggi's avatar
Roberto Raggi committed
634 635
}

636
void CppModelManager::findMacroUsages(const CPlusPlus::Macro &macro)
Christian Kamm's avatar
Christian Kamm committed
637
{
638
    d->m_findReferences->findMacroUses(macro);
Christian Kamm's avatar
Christian Kamm committed
639 640
}

641
void CppModelManager::renameMacroUsages(const CPlusPlus::Macro &macro, const QString &replacement)
642
{
643
    d->m_findReferences->renameMacroUses(macro, replacement);
644 645
}

646
void CppModelManager::replaceSnapshot(const Snapshot &newSnapshot)
647
{
648 649
    QMutexLocker snapshotLocker(&d->m_snapshotMutex);
    d->m_snapshot = newSnapshot;
650 651
}

652
WorkingCopy CppModelManager::buildWorkingCopyList()
con's avatar
con committed
653
{
654
    WorkingCopy workingCopy;
655

656 657 658 659 660
    foreach (const CppEditorDocumentHandle *cppEditorDocument, cppEditorDocuments()) {
        workingCopy.insert(cppEditorDocument->filePath(),
                           cppEditorDocument->contents(),
                           cppEditorDocument->revision());
    }
con's avatar
con committed
661

662
    QSetIterator<AbstractEditorSupport *> it(d->m_extraEditorSupports);
663 664
    while (it.hasNext()) {
        AbstractEditorSupport *es = it.next();
665
        workingCopy.insert(es->fileName(), es->contents(), es->revision());
666 667
    }

668 669
    // Add the project configuration file
    QByteArray conf = codeModelConfiguration();
670
    conf += ProjectExplorer::Macro::toByteArray(definedMacros());
671 672
    workingCopy.insert(configurationFileName(), conf);

con's avatar
con committed
673 674 675
    return workingCopy;
}

676
WorkingCopy CppModelManager::workingCopy() const
677 678 679 680
{
    return const_cast<CppModelManager *>(this)->buildWorkingCopyList();
}

681 682 683 684 685
QByteArray CppModelManager::codeModelConfiguration() const
{
    return QByteArray::fromRawData(pp_configuration, qstrlen(pp_configuration));
}

686
static QSet<QString> tooBigFilesRemoved(const QSet<QString> &files, int fileSizeLimitInMb)
687
{
688
    if (fileSizeLimitInMb <= 0)
689 690 691 692 693 694 695 696 697
        return files;

    QSet<QString> result;
    QFileInfo fileInfo;

    QSetIterator<QString> i(files);
    while (i.hasNext()) {
        const QString filePath = i.next();
        fileInfo.setFile(filePath);
698
        if (fileSizeExceedsLimit(fileInfo, fileSizeLimitInMb))
699 700 701 702 703 704 705 706
            continue;

        result << filePath;
    }

    return result;
}

707
QFuture<void> CppModelManager::updateSourceFiles(const QSet<QString> &sourceFiles,
708
                                                 ProgressNotificationMode mode)
709 710 711 712 713 714 715 716
{
    const QFutureInterface<void> dummy;
    return updateSourceFiles(dummy, sourceFiles, mode);
}

QFuture<void> CppModelManager::updateSourceFiles(const QFutureInterface<void> &superFuture,
                                                 const QSet<QString> &sourceFiles,
                                                 ProgressNotificationMode mode)
717
{
718
    if (sourceFiles.isEmpty() || !d->m_indexerEnabled)
719 720
        return QFuture<void>();

721
    const QSet<QString> filteredFiles = tooBigFilesRemoved(sourceFiles, indexerFileSizeLimitInMb());
722

723
    if (d->m_indexingSupporter)
724 725
        d->m_indexingSupporter->refreshSourceFiles(superFuture, filteredFiles, mode);
    return d->m_internalIndexingSupport->refreshSourceFiles(superFuture, filteredFiles, mode);
726
}
con's avatar
con committed
727

728
QList<ProjectInfo> CppModelManager::projectInfos() const
729
{
730 731
    QMutexLocker locker(&d->m_projectMutex);
    return d->m_projectToProjectsInfo.values();
732 733
}

734
ProjectInfo CppModelManager::projectInfo(ProjectExplorer::Project *project) const
735
{
736
    QMutexLocker locker(&d->m_projectMutex);
737
    return d->m_projectToProjectsInfo.value(project, ProjectInfo());
738 739
}

740 741 742 743 744 745
/// \brief Remove all files and their includes (recursively) of given ProjectInfo from the snapshot.
void CppModelManager::removeProjectInfoFilesAndIncludesFromSnapshot(const ProjectInfo &projectInfo)
{
    if (!projectInfo.isValid())
        return;

746
    QMutexLocker snapshotLocker(&d->m_snapshotMutex);
747 748
    foreach (const ProjectPart::Ptr &projectPart, projectInfo.projectParts()) {
        foreach (const ProjectFile &cxxFile, projectPart->files) {
749 750 751
            foreach (const QString &fileName, d->m_snapshot.allIncludesForDocument(cxxFile.path))
                d->m_snapshot.remove(fileName);
            d->m_snapshot.remove(cxxFile.path);
752 753 754 755
        }
    }
}

756
QList<CppEditorDocumentHandle *> CppModelManager::cppEditorDocuments() const
757
{
758 759
    QMutexLocker locker(&d->m_cppEditorDocumentsMutex);
    return d->m_cppEditorDocuments.values();
760 761
}

762 763 764
/// \brief Remove all given files from the snapshot.
void CppModelManager::removeFilesFromSnapshot(const QSet<QString> &filesToRemove)
{
765
    QMutexLocker snapshotLocker(&d->m_snapshotMutex);
766 767
    QSetIterator<QString> i(filesToRemove);
    while (i.hasNext())
768
        d->m_snapshot.remove(i.next());
769 770 771
}

class ProjectInfoComparer
772
{
773
public:
774 775
    ProjectInfoComparer(const ProjectInfo &oldProjectInfo,
                        const ProjectInfo &newProjectInfo)
776
        : m_old(oldProjectInfo)
777
        , m_oldSourceFiles(oldProjectInfo.sourceFiles())
778
        , m_new(newProjectInfo)
779
        , m_newSourceFiles(newProjectInfo.sourceFiles())
780 781
    {}

782 783 784
    bool definesChanged() const { return m_new.definesChanged(m_old); }
    bool configurationChanged() const { return m_new.configurationChanged(m_old); }
    bool configurationOrFilesChanged() const { return m_new.configurationOrFilesChanged(m_old); }
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799

    QSet<QString> addedFiles() const
    {
        QSet<QString> addedFilesSet = m_newSourceFiles;
        addedFilesSet.subtract(m_oldSourceFiles);
        return addedFilesSet;
    }

    QSet<QString> removedFiles() const
    {
        QSet<QString> removedFilesSet = m_oldSourceFiles;
        removedFilesSet.subtract(m_newSourceFiles);
        return removedFilesSet;
    }

800 801
    QStringList removedProjectParts()
    {
802 803
        QSet<QString> removed = projectPartIds(m_old.projectParts());
        removed.subtract(projectPartIds(m_new.projectParts()));
804
        return removed.toList();
805 806
    }

807 808 809 810 811 812 813 814 815 816 817 818 819 820
    /// Returns a list of common files that have a changed timestamp.
    QSet<QString> timeStampModifiedFiles(const Snapshot &snapshot) const
    {
        QSet<QString> commonSourceFiles = m_newSourceFiles;
        commonSourceFiles.intersect(m_oldSourceFiles);

        QList<Document::Ptr> documentsToCheck;
        QSetIterator<QString> i(commonSourceFiles);
        while (i.hasNext()) {
            const QString file = i.next();
            if (Document::Ptr document = snapshot.document(file))
                documentsToCheck << document;
        }

821
        return CppModelManager::timeStampModifiedFiles(documentsToCheck);
822 823
    }

824 825 826 827 828 829 830 831 832 833 834
private:
    static QSet<QString> projectPartIds(const QVector<ProjectPart::Ptr> &projectParts)
    {
        QSet<QString> ids;

        foreach (const ProjectPart::Ptr &projectPart, projectParts)
            ids.insert(projectPart->id());

        return ids;
    }

835
private:
836
    const ProjectInfo &m_old;
837 838
    const QSet<QString> m_oldSourceFiles;

839
    const ProjectInfo &m_new;
840 841 842
    const QSet<QString> m_newSourceFiles;
};

843
/// Make sure that m_projectMutex is locked when calling this.
844
void CppModelManager::recalculateProjectPartMappings()
845
{
846
    d->m_projectPartIdToProjectProjectPart.clear();
847 848
    d->m_fileToProjectParts.clear();
    foreach (const ProjectInfo &projectInfo, d->m_projectToProjectsInfo) {
849
        foreach (const ProjectPart::Ptr &projectPart, projectInfo.projectParts()) {
850
            d->m_projectPartIdToProjectProjectPart[projectPart->id()] = projectPart;
851
            foreach (const ProjectFile &cxxFile, projectPart->files)
852 853
                d->m_fileToProjectParts[Utils::FileName::fromString(cxxFile.path)].append(
                            projectPart);
854

855 856
        }
    }
857 858

    d->m_symbolFinder.clearCache();
859 860
}

861
void CppModelManager::watchForCanceledProjectIndexer(const QVector<QFuture<void>> &futures,
862 863 864 865
                                                     ProjectExplorer::Project *project)
{
    d->m_projectToIndexerCanceled.insert(project, false);

866 867 868
    for (const QFuture<void> &future : futures) {
        if (future.isCanceled() || future.isFinished())
            continue;
869

870 871 872 873 874 875 876 877 878 879
        QFutureWatcher<void> *watcher = new QFutureWatcher<void>();
        connect(watcher, &QFutureWatcher<void>::canceled, this, [this, project]() {
            if (d->m_projectToIndexerCanceled.contains(project)) // Project not yet removed
                d->m_projectToIndexerCanceled.insert(project, true);
        });
        connect(watcher, &QFutureWatcher<void>::finished, this, [watcher]() {
            watcher->deleteLater();
        });
        watcher->setFuture(future);
    }
880 881
}

882
void CppModelManager::updateCppEditorDocuments(bool projectsUpdated) const
883
{
Nikolai Kosjar's avatar