cppmodelmanager.h 10.9 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
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
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
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
con's avatar
con committed
30
**
31
**************************************************************************/
hjk's avatar
hjk committed
32

con's avatar
con committed
33 34 35
#ifndef CPPMODELMANAGER_H
#define CPPMODELMANAGER_H

36
#include "cpptools_global.h"
37
#include "cpptoolsconstants.h"
38
#include "ModelManagerInterface.h"
Wolfgang Beck's avatar
Wolfgang Beck committed
39 40 41
#ifndef ICHECK_BUILD
#  include <projectexplorer/project.h>
#endif
con's avatar
con committed
42
#include <cplusplus/CppDocument.h>
43
#include <cplusplus/PreprocessorClient.h>
Wolfgang Beck's avatar
Wolfgang Beck committed
44 45 46
#ifndef ICHECK_BUILD
#  include <texteditor/basetexteditor.h>
#endif
47 48 49
#include <cplusplus/PreprocessorEnvironment.h>
#include <cplusplus/pp-engine.h>
#ifdef ICHECK_BUILD
Wolfgang Beck's avatar
Wolfgang Beck committed
50 51
#  include "parsemanager.h"
#else
52 53 54 55 56 57
#  include <QHash>
#  include <QFutureInterface>
#  include <QFutureSynchronizer>
#  include <QMutex>
#  include <QTimer>
#  include <QTextEdit> // for QTextEdit::ExtraSelection
58
#endif
con's avatar
con committed
59 60 61 62 63 64 65

namespace Core {
class IEditor;
}

namespace TextEditor {
class ITextEditor;
66
class BaseTextEditorWidget;
con's avatar
con committed
67 68 69 70 71 72
}

namespace ProjectExplorer {
class ProjectExplorerPlugin;
}

73 74 75 76
namespace CPlusPlus {
    class ParseManager;
}

con's avatar
con committed
77
namespace CppTools {
78

79 80 81
class CppCompletionSupportFactory;
class CppHighlightingSupportFactory;

con's avatar
con committed
82 83 84
namespace Internal {

class CppEditorSupport;
85
class CppPreprocessor;
Roberto Raggi's avatar
Roberto Raggi committed
86
class CppFindReferences;
con's avatar
con committed
87

88
#ifndef ICHECK_BUILD
89
class CPPTOOLS_EXPORT CppModelManager : public CPlusPlus::CppModelManagerInterface
con's avatar
con committed
90 91 92 93 94 95 96
{
    Q_OBJECT

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

97 98
    static CppModelManager *instance();

dt's avatar
dt committed
99
    virtual QFuture<void> updateSourceFiles(const QStringList &sourceFiles);
100
    virtual WorkingCopy workingCopy() const;
101 102 103 104

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

107
    virtual CPlusPlus::Snapshot snapshot() const;
con's avatar
con committed
108 109 110 111
    virtual void GC();

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

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

Roberto Raggi's avatar
Roberto Raggi committed
114 115 116
    CppEditorSupport *editorSupport(TextEditor::ITextEditor *editor) const
    { return m_editorSupport.value(editor); }

con's avatar
con committed
117 118
    void emitDocumentUpdated(CPlusPlus::Document::Ptr doc);

Roberto Raggi's avatar
Roberto Raggi committed
119 120 121
    void stopEditorSelectionsUpdate()
    { m_updateEditorSelectionsTimer->stop(); }

122 123 124
    virtual void addEditorSupport(AbstractEditorSupport *editorSupport);
    virtual void removeEditorSupport(AbstractEditorSupport *editorSupport);

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

127 128
    virtual void renameUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context,
                              const QString &replacement = QString());
129
    virtual void findUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context);
Roberto Raggi's avatar
Roberto Raggi committed
130

Christian Kamm's avatar
Christian Kamm committed
131
    virtual void findMacroUsages(const CPlusPlus::Macro &macro);
132
    virtual void renameMacroUsages(const CPlusPlus::Macro &macro, const QString &replacement);
Christian Kamm's avatar
Christian Kamm committed
133

134 135 136 137 138
    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;

139 140
    void finishedRefreshingSourceFiles(const QStringList &files);

141
    virtual CppCompletionSupport *completionSupport(Core::IEditor *editor) const;
142
    virtual void setCppCompletionAssistProvider(CppCompletionAssistProvider *completionAssistProvider);
143

144
    virtual CppHighlightingSupport *highlightingSupport(Core::IEditor *editor) const;
145
    virtual void setHighlightingSupportFactory(CppHighlightingSupportFactory *highlightingFactory);
146

con's avatar
con committed
147 148 149 150 151 152 153 154
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);
155
    virtual void updateModifiedSourceFiles();
con's avatar
con committed
156 157 158 159

private Q_SLOTS:
    // this should be executed in the GUI thread.
    void onDocumentUpdated(CPlusPlus::Document::Ptr doc);
160
    void onExtraDiagnosticsUpdated(const QString &fileName);
con's avatar
con committed
161
    void onAboutToRemoveProject(ProjectExplorer::Project *project);
162
    void onAboutToUnloadSession();
163
    void onProjectAdded(ProjectExplorer::Project *project);
Roberto Raggi's avatar
Roberto Raggi committed
164 165
    void postEditorUpdate();
    void updateEditorSelections();
con's avatar
con committed
166 167

private:
168 169
    void updateEditor(CPlusPlus::Document::Ptr doc);

170
    WorkingCopy buildWorkingCopyList();
171

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    QStringList projectFiles()
    {
        ensureUpdated();
        return m_projectFiles;
    }

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

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

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

196
    void ensureUpdated();
197 198 199 200
    QStringList internalProjectFiles() const;
    QStringList internalIncludePaths() const;
    QStringList internalFrameworkPaths() const;
    QByteArray internalDefinedMacros() const;
201

con's avatar
con committed
202
    static void parse(QFutureInterface<void> &future,
203 204
                      CppPreprocessor *preproc,
                      QStringList files);
con's avatar
con committed
205 206

private:
207
    CPlusPlus::Snapshot m_snapshot;
con's avatar
con committed
208

209 210
    // cache
    bool m_dirty;
con's avatar
con committed
211
    QStringList m_projectFiles;
212 213 214
    QStringList m_includePaths;
    QStringList m_frameworkPaths;
    QByteArray m_definedMacros;
con's avatar
con committed
215 216 217 218

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

219 220
    QSet<AbstractEditorSupport *> m_addtionalEditorSupport;

con's avatar
con committed
221 222 223
    // project integration
    QMap<ProjectExplorer::Project *, ProjectInfo> m_projects;

224
    mutable QMutex mutex;
225
    mutable QMutex protectSnapshot;
226

Roberto Raggi's avatar
Roberto Raggi committed
227
    struct Editor {
228
        Editor()
229 230 231
            : revision(-1)
            , updateSelections(true)
        {}
232
        int revision;
233
        bool updateSelections;
234
        QPointer<TextEditor::ITextEditor> textEditor;
Roberto Raggi's avatar
Roberto Raggi committed
235
        QList<QTextEdit::ExtraSelection> selections;
236
        QList<TextEditor::BaseTextEditorWidget::BlockRange> ifdefedOutBlocks;
Roberto Raggi's avatar
Roberto Raggi committed
237 238 239 240 241
    };

    QList<Editor> m_todo;

    QTimer *m_updateEditorSelectionsTimer;
Roberto Raggi's avatar
Roberto Raggi committed
242 243

    QFutureSynchronizer<void> m_synchronizer;
244
    unsigned m_revision;
Roberto Raggi's avatar
Roberto Raggi committed
245 246

    CppFindReferences *m_findReferences;
247
    bool m_indexerEnabled;
248
    bool m_dumpFileNameWhileParsing;
249 250 251

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

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

255 256
    CppCompletionAssistProvider *m_completionAssistProvider;
    CppCompletionAssistProvider *m_completionFallback;
257 258
    CppHighlightingSupportFactory *m_highlightingFactory;
    CppHighlightingSupportFactory *m_highlightingFallback;
con's avatar
con committed
259
};
260 261
#endif

262
class CPPTOOLS_EXPORT CppPreprocessor: public CPlusPlus::Client
263 264 265
{
public:
#ifndef ICHECK_BUILD
266
    CppPreprocessor(QPointer<CppModelManager> modelManager, bool dumpFileNameWhileParsing = false);
267 268 269 270 271 272
#else
    CppPreprocessor(QPointer<CPlusPlus::ParseManager> modelManager);
#endif
    virtual ~CppPreprocessor();

    void setRevision(unsigned revision);
273
    void setWorkingCopy(const CPlusPlus::CppModelManagerInterface::WorkingCopy &workingCopy);
274 275
    void setIncludePaths(const QStringList &includePaths);
    void setFrameworkPaths(const QStringList &frameworkPaths);
276
    void addFrameworkPath(const QString &frameworkPath);
277 278 279 280 281 282 283 284 285 286
    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; }

287 288 289
    CppModelManager *modelManager() const
    { return m_modelManager.data(); }

290
public: // attributes
291
    CPlusPlus::Snapshot snapshot;
292 293 294 295 296 297

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
298
    QString tryIncludeFile_helper(QString &fileName, IncludeType type, unsigned *revision);
299 300 301

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

302
    virtual void macroAdded(const CPlusPlus::Macro &macro);
303 304
    virtual void passedMacroDefinitionCheck(unsigned offset, unsigned line,
                                            const CPlusPlus::Macro &macro);
305
    virtual void failedMacroDefinitionCheck(unsigned offset, const CPlusPlus::ByteArrayRef &name);
306 307
    virtual void notifyMacroReference(unsigned offset, unsigned line,
                                      const CPlusPlus::Macro &macro);
308
    virtual void startExpandingMacro(unsigned offset,
309
                                     unsigned line,
310 311 312
                                     const CPlusPlus::Macro &macro,
                                     const QVector<CPlusPlus::MacroArgumentReference> &actuals);
    virtual void stopExpandingMacro(unsigned offset, const CPlusPlus::Macro &macro);
313 314
    virtual void startSkippingBlocks(unsigned offset);
    virtual void stopSkippingBlocks(unsigned offset);
315
    virtual void sourceNeeded(unsigned line, QString &fileName, IncludeType type);
316 317 318 319 320

private:
#ifndef ICHECK_BUILD
    QPointer<CppModelManager> m_modelManager;
#endif
321
    bool m_dumpFileNameWhileParsing;
322 323
    CPlusPlus::Environment env;
    CPlusPlus::Preprocessor preprocess;
324 325
    QStringList m_includePaths;
    QStringList m_systemIncludePaths;
326
    CPlusPlus::CppModelManagerInterface::WorkingCopy m_workingCopy;
327 328 329
    QStringList m_projectFiles;
    QStringList m_frameworkPaths;
    QSet<QString> m_included;
330
    CPlusPlus::Document::Ptr m_currentDoc;
331 332 333
    QSet<QString> m_todo;
    QSet<QString> m_processed;
    unsigned m_revision;
Roberto Raggi's avatar
Roberto Raggi committed
334
    QHash<QString, QString> m_fileNameCache;
335
};
con's avatar
con committed
336 337 338 339 340

} // namespace Internal
} // namespace CppTools

#endif // CPPMODELMANAGER_H