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

namespace Core {
class IEditor;
}

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

namespace ProjectExplorer {
class ProjectExplorerPlugin;
}

71 72 73 74
namespace CPlusPlus {
    class ParseManager;
}

con's avatar
con committed
75
namespace CppTools {
76

77 78 79
class CppCompletionSupportFactory;
class CppHighlightingSupportFactory;

con's avatar
con committed
80 81 82
namespace Internal {

class CppEditorSupport;
83
class CppPreprocessor;
Roberto Raggi's avatar
Roberto Raggi committed
84
class CppFindReferences;
con's avatar
con committed
85

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

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

95 96
    static CppModelManager *instance();

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

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

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

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

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

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

con's avatar
con committed
115 116
    void emitDocumentUpdated(CPlusPlus::Document::Ptr doc);

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

120 121 122
    virtual void addEditorSupport(AbstractEditorSupport *editorSupport);
    virtual void removeEditorSupport(AbstractEditorSupport *editorSupport);

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

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

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

132 133 134 135 136
    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;

137 138
    void finishedRefreshingSourceFiles(const QStringList &files);

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

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

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

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

private:
166 167
    void updateEditor(CPlusPlus::Document::Ptr doc);

168
    WorkingCopy buildWorkingCopyList();
169

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

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

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

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

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

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

private:
205
    CPlusPlus::Snapshot m_snapshot;
con's avatar
con committed
206

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

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

217 218
    QSet<AbstractEditorSupport *> m_addtionalEditorSupport;

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

222
    mutable QMutex mutex;
223
    mutable QMutex protectSnapshot;
224

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

    QList<Editor> m_todo;

    QTimer *m_updateEditorSelectionsTimer;
Roberto Raggi's avatar
Roberto Raggi committed
240 241

    QFutureSynchronizer<void> m_synchronizer;
242
    unsigned m_revision;
Roberto Raggi's avatar
Roberto Raggi committed
243 244

    CppFindReferences *m_findReferences;
245
    bool m_indexerEnabled;
246
    bool m_dumpFileNameWhileParsing;
247 248 249

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

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

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

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

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

285 286 287
    CppModelManager *modelManager() const
    { return m_modelManager.data(); }

288
public: // attributes
289
    CPlusPlus::Snapshot snapshot;
290 291 292 293 294 295

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

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

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

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

} // namespace Internal
} // namespace CppTools

#endif // CPPMODELMANAGER_H