cppmodelmanager.h 10.5 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
Eike Ziller's avatar
Eike Ziller committed
7
** Contact: http://www.qt-project.org/
con's avatar
con committed
8
**
9
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
**
29
**************************************************************************/
hjk's avatar
hjk committed
30

con's avatar
con committed
31 32 33
#ifndef CPPMODELMANAGER_H
#define CPPMODELMANAGER_H

34
#include "cpptools_global.h"
35
#include "cpptoolsconstants.h"
36
#include "ModelManagerInterface.h"
37
#include <projectexplorer/project.h>
con's avatar
con committed
38
#include <cplusplus/CppDocument.h>
39
#include <cplusplus/PreprocessorClient.h>
40
#include <texteditor/basetexteditor.h>
41 42
#include <cplusplus/PreprocessorEnvironment.h>
#include <cplusplus/pp-engine.h>
43 44 45 46 47 48 49

#include <QHash>
#include <QFutureInterface>
#include <QFutureSynchronizer>
#include <QMutex>
#include <QTimer>
#include <QTextEdit> // for QTextEdit::ExtraSelection
con's avatar
con committed
50 51 52 53 54 55 56

namespace Core {
class IEditor;
}

namespace TextEditor {
class ITextEditor;
57
class BaseTextEditorWidget;
con's avatar
con committed
58 59 60 61 62 63
}

namespace ProjectExplorer {
class ProjectExplorerPlugin;
}

64 65 66 67
namespace CPlusPlus {
    class ParseManager;
}

con's avatar
con committed
68
namespace CppTools {
69

70 71 72
class CppCompletionSupportFactory;
class CppHighlightingSupportFactory;

con's avatar
con committed
73 74 75
namespace Internal {

class CppEditorSupport;
76
class CppPreprocessor;
Roberto Raggi's avatar
Roberto Raggi committed
77
class CppFindReferences;
con's avatar
con committed
78

79
class CPPTOOLS_EXPORT CppModelManager : public CPlusPlus::CppModelManagerInterface
con's avatar
con committed
80 81 82 83 84 85 86
{
    Q_OBJECT

public:
    CppModelManager(QObject *parent);
    virtual ~CppModelManager();

87 88
    static CppModelManager *instance();

dt's avatar
dt committed
89
    virtual QFuture<void> updateSourceFiles(const QStringList &sourceFiles);
90
    virtual WorkingCopy workingCopy() const;
91 92 93 94

    virtual QList<ProjectInfo> projectInfos() const;
    virtual ProjectInfo projectInfo(ProjectExplorer::Project *project) const;
    virtual void updateProjectInfo(const ProjectInfo &pinfo);
95
    virtual QList<ProjectPart::Ptr> projectPart(const QString &fileName) const;
96

97
    virtual CPlusPlus::Snapshot snapshot() const;
con's avatar
con committed
98 99 100 101
    virtual void GC();

    QFuture<void> refreshSourceFiles(const QStringList &sourceFiles);

102
    virtual bool isCppEditor(Core::IEditor *editor) const;
con's avatar
con committed
103

Roberto Raggi's avatar
Roberto Raggi committed
104 105 106
    CppEditorSupport *editorSupport(TextEditor::ITextEditor *editor) const
    { return m_editorSupport.value(editor); }

con's avatar
con committed
107 108
    void emitDocumentUpdated(CPlusPlus::Document::Ptr doc);

Roberto Raggi's avatar
Roberto Raggi committed
109 110 111
    void stopEditorSelectionsUpdate()
    { m_updateEditorSelectionsTimer->stop(); }

112 113 114
    virtual void addEditorSupport(AbstractEditorSupport *editorSupport);
    virtual void removeEditorSupport(AbstractEditorSupport *editorSupport);

115
    virtual QList<int> references(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context);
116

117 118
    virtual void renameUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context,
                              const QString &replacement = QString());
119
    virtual void findUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context);
Roberto Raggi's avatar
Roberto Raggi committed
120

Christian Kamm's avatar
Christian Kamm committed
121
    virtual void findMacroUsages(const CPlusPlus::Macro &macro);
122
    virtual void renameMacroUsages(const CPlusPlus::Macro &macro, const QString &replacement);
Christian Kamm's avatar
Christian Kamm committed
123

124 125 126 127 128
    virtual void setExtraDiagnostics(const QString &fileName, int key,
                                     const QList<CPlusPlus::Document::DiagnosticMessage> &diagnostics);
    virtual QList<CPlusPlus::Document::DiagnosticMessage> extraDiagnostics(
            const QString &fileName, int key = AllExtraDiagnostics) const;

129 130
    void finishedRefreshingSourceFiles(const QStringList &files);

131
    virtual CppCompletionSupport *completionSupport(Core::IEditor *editor) const;
132
    virtual void setCppCompletionAssistProvider(CppCompletionAssistProvider *completionAssistProvider);
133

134
    virtual CppHighlightingSupport *highlightingSupport(Core::IEditor *editor) const;
135
    virtual void setHighlightingSupportFactory(CppHighlightingSupportFactory *highlightingFactory);
136

con's avatar
con committed
137 138 139 140 141 142 143 144
Q_SIGNALS:
    void projectPathChanged(const QString &projectPath);

    void aboutToRemoveFiles(const QStringList &files);

public Q_SLOTS:
    void editorOpened(Core::IEditor *editor);
    void editorAboutToClose(Core::IEditor *editor);
145
    virtual void updateModifiedSourceFiles();
con's avatar
con committed
146 147 148 149

private Q_SLOTS:
    // this should be executed in the GUI thread.
    void onDocumentUpdated(CPlusPlus::Document::Ptr doc);
150
    void onExtraDiagnosticsUpdated(const QString &fileName);
con's avatar
con committed
151
    void onAboutToRemoveProject(ProjectExplorer::Project *project);
152
    void onAboutToUnloadSession();
153
    void onProjectAdded(ProjectExplorer::Project *project);
Roberto Raggi's avatar
Roberto Raggi committed
154 155
    void postEditorUpdate();
    void updateEditorSelections();
con's avatar
con committed
156 157

private:
158 159
    void updateEditor(CPlusPlus::Document::Ptr doc);

160
    WorkingCopy buildWorkingCopyList();
161

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    QStringList projectFiles()
    {
        ensureUpdated();
        return m_projectFiles;
    }

    QStringList includePaths()
    {
        ensureUpdated();
        return m_includePaths;
    }

    QStringList frameworkPaths()
    {
        ensureUpdated();
        return m_frameworkPaths;
    }

    QByteArray definedMacros()
    {
        ensureUpdated();
        return m_definedMacros;
    }

186
    void ensureUpdated();
187 188 189 190
    QStringList internalProjectFiles() const;
    QStringList internalIncludePaths() const;
    QStringList internalFrameworkPaths() const;
    QByteArray internalDefinedMacros() const;
191

con's avatar
con committed
192
    static void parse(QFutureInterface<void> &future,
193 194
                      CppPreprocessor *preproc,
                      QStringList files);
con's avatar
con committed
195 196

private:
197
    CPlusPlus::Snapshot m_snapshot;
con's avatar
con committed
198

199 200
    // cache
    bool m_dirty;
con's avatar
con committed
201
    QStringList m_projectFiles;
202 203 204
    QStringList m_includePaths;
    QStringList m_frameworkPaths;
    QByteArray m_definedMacros;
con's avatar
con committed
205 206 207 208

    // editor integration
    QMap<TextEditor::ITextEditor *, CppEditorSupport *> m_editorSupport;

209 210
    QSet<AbstractEditorSupport *> m_addtionalEditorSupport;

con's avatar
con committed
211 212 213
    // project integration
    QMap<ProjectExplorer::Project *, ProjectInfo> m_projects;

214
    mutable QMutex mutex;
215
    mutable QMutex protectSnapshot;
216

Roberto Raggi's avatar
Roberto Raggi committed
217
    struct Editor {
218
        Editor()
219 220 221
            : revision(-1)
            , updateSelections(true)
        {}
222
        int revision;
223
        bool updateSelections;
224
        QPointer<TextEditor::ITextEditor> textEditor;
Roberto Raggi's avatar
Roberto Raggi committed
225
        QList<QTextEdit::ExtraSelection> selections;
226
        QList<TextEditor::BaseTextEditorWidget::BlockRange> ifdefedOutBlocks;
Roberto Raggi's avatar
Roberto Raggi committed
227 228 229 230 231
    };

    QList<Editor> m_todo;

    QTimer *m_updateEditorSelectionsTimer;
Roberto Raggi's avatar
Roberto Raggi committed
232 233

    QFutureSynchronizer<void> m_synchronizer;
234
    unsigned m_revision;
Roberto Raggi's avatar
Roberto Raggi committed
235 236

    CppFindReferences *m_findReferences;
237
    bool m_indexerEnabled;
238
    bool m_dumpFileNameWhileParsing;
239 240 241

    mutable QMutex protectExtraDiagnostics;
    QHash<QString, QHash<int, QList<CPlusPlus::Document::DiagnosticMessage> > > m_extraDiagnostics;
242 243

    QMap<QString, QList<ProjectPart::Ptr> > m_srcToProjectPart;
244

245 246
    CppCompletionAssistProvider *m_completionAssistProvider;
    CppCompletionAssistProvider *m_completionFallback;
247 248
    CppHighlightingSupportFactory *m_highlightingFactory;
    CppHighlightingSupportFactory *m_highlightingFallback;
con's avatar
con committed
249
};
250

251
class CPPTOOLS_EXPORT CppPreprocessor: public CPlusPlus::Client
252 253
{
public:
254
    CppPreprocessor(QPointer<CppModelManager> modelManager, bool dumpFileNameWhileParsing = false);
255 256 257
    virtual ~CppPreprocessor();

    void setRevision(unsigned revision);
258
    void setWorkingCopy(const CPlusPlus::CppModelManagerInterface::WorkingCopy &workingCopy);
259 260
    void setIncludePaths(const QStringList &includePaths);
    void setFrameworkPaths(const QStringList &frameworkPaths);
261
    void addFrameworkPath(const QString &frameworkPath);
262 263 264 265 266 267 268 269 270 271
    void setProjectFiles(const QStringList &files);
    void setTodo(const QStringList &files);

    void run(const QString &fileName);

    void resetEnvironment();

    const QSet<QString> &todo() const
    { return m_todo; }

272 273 274
    CppModelManager *modelManager() const
    { return m_modelManager.data(); }

275
public: // attributes
276
    CPlusPlus::Snapshot snapshot;
277 278 279 280 281 282

protected:
    CPlusPlus::Document::Ptr switchDocument(CPlusPlus::Document::Ptr doc);

    bool includeFile(const QString &absoluteFilePath, QString *result, unsigned *revision);
    QString tryIncludeFile(QString &fileName, IncludeType type, unsigned *revision);
Roberto Raggi's avatar
Roberto Raggi committed
283
    QString tryIncludeFile_helper(QString &fileName, IncludeType type, unsigned *revision);
284 285 286

    void mergeEnvironment(CPlusPlus::Document::Ptr doc);

287
    virtual void macroAdded(const CPlusPlus::Macro &macro);
288 289
    virtual void passedMacroDefinitionCheck(unsigned offset, unsigned line,
                                            const CPlusPlus::Macro &macro);
290
    virtual void failedMacroDefinitionCheck(unsigned offset, const CPlusPlus::ByteArrayRef &name);
291 292
    virtual void notifyMacroReference(unsigned offset, unsigned line,
                                      const CPlusPlus::Macro &macro);
293
    virtual void startExpandingMacro(unsigned offset,
294
                                     unsigned line,
295 296 297
                                     const CPlusPlus::Macro &macro,
                                     const QVector<CPlusPlus::MacroArgumentReference> &actuals);
    virtual void stopExpandingMacro(unsigned offset, const CPlusPlus::Macro &macro);
298 299
    virtual void startSkippingBlocks(unsigned offset);
    virtual void stopSkippingBlocks(unsigned offset);
300
    virtual void sourceNeeded(unsigned line, QString &fileName, IncludeType type);
301 302 303

private:
    QPointer<CppModelManager> m_modelManager;
304
    bool m_dumpFileNameWhileParsing;
305 306
    CPlusPlus::Environment env;
    CPlusPlus::Preprocessor preprocess;
307 308
    QStringList m_includePaths;
    QStringList m_systemIncludePaths;
309
    CPlusPlus::CppModelManagerInterface::WorkingCopy m_workingCopy;
310 311 312
    QStringList m_projectFiles;
    QStringList m_frameworkPaths;
    QSet<QString> m_included;
313
    CPlusPlus::Document::Ptr m_currentDoc;
314 315 316
    QSet<QString> m_todo;
    QSet<QString> m_processed;
    unsigned m_revision;
Roberto Raggi's avatar
Roberto Raggi committed
317
    QHash<QString, QString> m_fileNameCache;
318
};
con's avatar
con committed
319 320 321 322 323

} // namespace Internal
} // namespace CppTools

#endif // CPPMODELMANAGER_H