cppmodelmanager.h 10.1 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

hjk's avatar
hjk committed
26
#pragma once
con's avatar
con committed
27

28 29
#include "cpptools_global.h"

30
#include "refactoringengineinterface.h"
31
#include "projectinfo.h"
32 33
#include "projectpart.h"
#include "projectpartheaderpath.h"
34

35 36 37 38 39
#include <cplusplus/cppmodelmanagerbase.h>

#include <QFuture>
#include <QObject>
#include <QStringList>
con's avatar
con committed
40

41 42 43 44
namespace Core {
class IDocument;
class IEditor;
}
45 46
namespace CPlusPlus { class LookupContext; }
namespace ProjectExplorer { class Project; }
47
namespace TextEditor { class TextDocument; }
con's avatar
con committed
48 49

namespace CppTools {
50

51 52 53
class AbstractEditorSupport;
class BaseEditorDocumentProcessor;
class CppCompletionAssistProvider;
54
class CppEditorDocumentHandle;
55
class CppIndexingSupport;
56
class ModelManagerSupportProvider;
57
class FollowSymbolInterface;
58
class SymbolFinder;
59
class WorkingCopy;
con's avatar
con committed
60

61
namespace Internal {
62
class CppSourceProcessor;
63
class CppModelManagerPrivate;
64 65 66
}

namespace Tests {
hjk's avatar
hjk committed
67
class ModelManagerTestHelper;
68
}
con's avatar
con committed
69

70 71 72 73 74 75 76 77 78
enum class RefactoringEngineType : int
{
    BuiltIn = 0,
    ClangCodeModel = 1,
    ClangRefactoring = 2
};

class CPPTOOLS_EXPORT CppModelManager final : public CPlusPlus::CppModelManagerBase,
        public RefactoringEngineInterface
con's avatar
con committed
79 80 81
{
    Q_OBJECT

82 83 84
public:
    typedef CPlusPlus::Document Document;

con's avatar
con committed
85
public:
86
    CppModelManager(QObject *parent = 0);
87
    ~CppModelManager();
con's avatar
con committed
88

89 90
    static CppModelManager *instance();

91 92 93 94 95 96 97
     // Documented in source file.
     enum ProgressNotificationMode {
        ForcedProgressNotification,
        ReservedProgressNotification
    };

    QFuture<void> updateSourceFiles(const QSet<QString> &sourceFiles,
98 99 100 101
                                    ProgressNotificationMode mode = ReservedProgressNotification);
    QFuture<void> updateSourceFiles(const QFutureInterface<void> &superFuture,
                                    const QSet<QString> &sourceFiles,
                                    ProgressNotificationMode mode = ReservedProgressNotification);
102
    void updateCppEditorDocuments(bool projectsUpdated = false) const;
103 104
    WorkingCopy workingCopy() const;
    QByteArray codeModelConfiguration() const;
105

106 107 108
    QList<ProjectInfo> projectInfos() const;
    ProjectInfo projectInfo(ProjectExplorer::Project *project) const;
    QFuture<void> updateProjectInfo(const ProjectInfo &newProjectInfo);
109 110 111
    QFuture<void> updateProjectInfo(QFutureInterface<void> &futureInterface,
                                    const ProjectInfo &newProjectInfo);

112 113
    ProjectInfo updateCompilerCallDataForProject(ProjectExplorer::Project *project,
                                                 ProjectInfo::CompilerCallData &compilerCallData);
114

115
    /// \return The project part with the given project file
116
    ProjectPart::Ptr projectPartForId(const QString &projectPartId) const;
117
    /// \return All project parts that mention the given file name as one of the sources/headers.
118 119 120
    QList<ProjectPart::Ptr> projectPart(const Utils::FileName &fileName) const;
    QList<ProjectPart::Ptr> projectPart(const QString &fileName) const
    { return projectPart(Utils::FileName::fromString(fileName)); }
121 122
    /// This is a fall-back function: find all files that includes the file directly or indirectly,
    /// and return its \c ProjectPart list for use with this file.
123
    QList<ProjectPart::Ptr> projectPartFromDependencies(const Utils::FileName &fileName) const;
124 125
    /// \return A synthetic \c ProjectPart which consists of all defines/includes/frameworks from
    ///         all loaded projects.
126
    ProjectPart::Ptr fallbackProjectPart();
127

128 129
    CPlusPlus::Snapshot snapshot() const;
    Document::Ptr document(const QString &fileName) const;
130
    bool replaceDocument(Document::Ptr newDoc);
con's avatar
con committed
131 132

    void emitDocumentUpdated(CPlusPlus::Document::Ptr doc);
133 134 135
    void emitAbstractEditorSupportContentsUpdated(const QString &filePath,
                                                  const QByteArray &contents);
    void emitAbstractEditorSupportRemoved(const QString &filePath);
con's avatar
con committed
136

137
    static bool isCppEditor(Core::IEditor *editor);
138
    bool isClangCodeModelActive() const;
139

140
    QSet<AbstractEditorSupport*> abstractEditorSupports() const;
141 142
    void addExtraEditorSupport(AbstractEditorSupport *editorSupport);
    void removeExtraEditorSupport(AbstractEditorSupport *editorSupport);
143

144
    QList<CppEditorDocumentHandle *> cppEditorDocuments() const;
145 146 147
    CppEditorDocumentHandle *cppEditorDocument(const QString &filePath) const;
    void registerCppEditorDocument(CppEditorDocumentHandle *cppEditorDocument);
    void unregisterCppEditorDocument(const QString &filePath);
148

149
    QList<int> references(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context);
150

151 152 153
    void startLocalRenaming(const CursorInEditor &data,
                            CppTools::ProjectPart *projectPart,
                            RenameCallback &&renameSymbolsCallback) final;
154 155
    void globalRename(const CursorInEditor &data, UsagesCallback &&renameCallback,
                      const QString &replacement) final;
156 157
    void findUsages(const CppTools::CursorInEditor &data,
                    UsagesCallback &&showUsagesCallback) const final;
158 159 160 161 162
    Link globalFollowSymbol(const CursorInEditor &data,
                            const CPlusPlus::Snapshot &snapshot,
                            const CPlusPlus::Document::Ptr &documentFromSemanticInfo,
                            SymbolFinder *symbolFinder,
                            bool inNextSplit) const final;
163

164 165 166
    void renameUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context,
                      const QString &replacement = QString());
    void findUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context);
Roberto Raggi's avatar
Roberto Raggi committed
167

168 169
    void findMacroUsages(const CPlusPlus::Macro &macro);
    void renameMacroUsages(const CPlusPlus::Macro &macro, const QString &replacement);
Christian Kamm's avatar
Christian Kamm committed
170

171
    void finishedRefreshingSourceFiles(const QSet<QString> &files);
172

173
    void activateClangCodeModel(ModelManagerSupportProvider *modelManagerSupportProvider);
174
    CppCompletionAssistProvider *completionAssistProvider() const;
175
    BaseEditorDocumentProcessor *editorDocumentProcessor(
176
        TextEditor::TextDocument *baseTextDocument) const;
177
    FollowSymbolInterface &followSymbolInterface() const;
178

179 180
    void setIndexingSupport(CppIndexingSupport *indexingSupport);
    CppIndexingSupport *indexingSupport();
181

182
    QStringList projectFiles();
183

184
    ProjectPartHeaderPaths headerPaths();
185

186
    // Use this *only* for auto tests
187
    void setHeaderPaths(const ProjectPartHeaderPaths &headerPaths);
188

189
    ProjectExplorer::Macros definedMacros();
190

191 192
    void enableGarbageCollector(bool enable);

193 194
    SymbolFinder *symbolFinder();

195 196
    QThreadPool *sharedThreadPool();

197
    static QSet<QString> timeStampModifiedFiles(const QList<Document::Ptr> &documentsToCheck);
198

199 200 201
    static Internal::CppSourceProcessor *createSourceProcessor();
    static QString configurationFileName();
    static QString editorConfigurationFileName();
202

203 204 205
    static void addRefactoringEngine(RefactoringEngineType type,
                                     RefactoringEngineInterface *refactoringEngine);
    static void removeRefactoringEngine(RefactoringEngineType type);
Marco Bubke's avatar
Marco Bubke committed
206

207 208
    void renameIncludes(const QString &oldFileName, const QString &newFileName);

209
signals:
210 211 212 213 214 215 216
    /// Project data might be locked while this is emitted.
    void aboutToRemoveFiles(const QStringList &files);

    void documentUpdated(CPlusPlus::Document::Ptr doc);
    void sourceFilesRefreshed(const QSet<QString> &files);

    void projectPartsUpdated(ProjectExplorer::Project *project);
217
    void projectPartsRemoved(const QStringList &projectPartIds);
218 219 220

    void globalSnapshotChanged();

221
    void gcFinished(); // Needed for tests.
222

223 224 225
    void abstractEditorSupportContentsUpdated(const QString &filePath, const QByteArray &contents);
    void abstractEditorSupportRemoved(const QString &filePath);

226
public slots:
Nikolai Kosjar's avatar
Nikolai Kosjar committed
227 228
    void updateModifiedSourceFiles();
    void GC();
229

230
private:
231
    // This should be executed in the GUI thread.
232
    friend class Tests::ModelManagerTestHelper;
233
    void onAboutToLoadSession();
234
    void onProjectAdded(ProjectExplorer::Project *project);
235
    void onAboutToRemoveProject(ProjectExplorer::Project *project);
236
    void onActiveProjectChanged(ProjectExplorer::Project *project);
237
    void onSourceFilesRefreshed() const;
238
    void onCurrentEditorChanged(Core::IEditor *editor);
239
    void onCoreAboutToClose();
240

241
    void initializeBuiltinModelManagerSupport();
242
    void delayedGC();
243
    void recalculateProjectPartMappings();
244 245
    void watchForCanceledProjectIndexer(const QVector<QFuture<void> > &futures,
                                        ProjectExplorer::Project *project);
246

247
    void replaceSnapshot(const CPlusPlus::Snapshot &newSnapshot);
248 249 250
    void removeFilesFromSnapshot(const QSet<QString> &removedFiles);
    void removeProjectInfoFilesAndIncludesFromSnapshot(const ProjectInfo &projectInfo);

251 252
    WorkingCopy buildWorkingCopyList();

253
    void ensureUpdated();
254
    QStringList internalProjectFiles() const;
255
    ProjectPartHeaderPaths internalHeaderPaths() const;
256
    ProjectExplorer::Macros internalDefinedMacros() const;
257

258
    void dumpModelManagerConfiguration(const QString &logFileId);
259

260
private:
261
    Internal::CppModelManagerPrivate *d;
con's avatar
con committed
262
};
263

con's avatar
con committed
264
} // namespace CppTools