cppmodelmanager.h 9.52 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) 2010 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
** Commercial Usage
10
**
11 12 13 14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18 19 20 21 22 23
** Alternatively, 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.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

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

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

namespace Core {
class ICore;
class IEditor;
}

namespace TextEditor {
class ITextEditor;
Roberto Raggi's avatar
Roberto Raggi committed
62
class BaseTextEditor;
con's avatar
con committed
63 64 65 66 67 68
}

namespace ProjectExplorer {
class ProjectExplorerPlugin;
}

69 70 71 72
namespace CPlusPlus {
    class ParseManager;
}

con's avatar
con committed
73
namespace CppTools {
74

con's avatar
con committed
75 76 77
namespace Internal {

class CppEditorSupport;
78
class CppPreprocessor;
Roberto Raggi's avatar
Roberto Raggi committed
79
class CppFindReferences;
con's avatar
con committed
80

81
#ifndef ICHECK_BUILD
con's avatar
con committed
82 83 84 85 86 87 88 89
class CppModelManager : public CppModelManagerInterface
{
    Q_OBJECT

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

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

    virtual QList<ProjectInfo> projectInfos() const;
    virtual ProjectInfo projectInfo(ProjectExplorer::Project *project) const;
    virtual void updateProjectInfo(const ProjectInfo &pinfo);

97 98
    virtual QStringList includesInPath(const QString &path) const;

99
    virtual CPlusPlus::Snapshot snapshot() const;
con's avatar
con committed
100 101 102 103 104 105
    virtual void GC();

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

    inline Core::ICore *core() const { return m_core; }

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

Roberto Raggi's avatar
Roberto Raggi committed
108 109 110
    CppEditorSupport *editorSupport(TextEditor::ITextEditor *editor) const
    { return m_editorSupport.value(editor); }

con's avatar
con committed
111 112
    void emitDocumentUpdated(CPlusPlus::Document::Ptr doc);

Roberto Raggi's avatar
Roberto Raggi committed
113 114 115
    void stopEditorSelectionsUpdate()
    { m_updateEditorSelectionsTimer->stop(); }

116 117 118
    virtual void addEditorSupport(AbstractEditorSupport *editorSupport);
    virtual void removeEditorSupport(AbstractEditorSupport *editorSupport);

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

121 122
    virtual void renameUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context,
                              const QString &replacement = QString());
123
    virtual void findUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context);
Roberto Raggi's avatar
Roberto Raggi committed
124

Christian Kamm's avatar
Christian Kamm committed
125 126
    virtual void findMacroUsages(const CPlusPlus::Macro &macro);

127 128 129
    void setHeaderSuffixes(const QStringList &suffixes)
    { m_headerSuffixes = suffixes; }

con's avatar
con committed
130 131 132 133 134 135 136 137 138 139 140 141 142
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);

private Q_SLOTS:
    // this should be executed in the GUI thread.
    void onDocumentUpdated(CPlusPlus::Document::Ptr doc);
    void onAboutToRemoveProject(ProjectExplorer::Project *project);
143
    void onAboutToUnloadSession();
144
    void onProjectAdded(ProjectExplorer::Project *project);
Roberto Raggi's avatar
Roberto Raggi committed
145 146
    void postEditorUpdate();
    void updateEditorSelections();
con's avatar
con committed
147 148

private:
149
    WorkingCopy buildWorkingCopyList();
150

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    QStringList projectFiles()
    {
        ensureUpdated();
        return m_projectFiles;
    }

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

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

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

175
    void ensureUpdated();
176 177 178 179
    QStringList internalProjectFiles() const;
    QStringList internalIncludePaths() const;
    QStringList internalFrameworkPaths() const;
    QByteArray internalDefinedMacros() const;
180

181
    void setIncludesInPaths(const QMap<QString, QStringList> &includesInPaths);
182 183 184 185

    static void updateIncludesInPaths(QFutureInterface<void> &future,
                                      CppModelManager *manager,
                                      QStringList paths,
186
                                      QStringList frameworkPaths,
187 188
                                      QStringList suffixes);

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

private:
    Core::ICore *m_core;
195
    CPlusPlus::Snapshot m_snapshot;
con's avatar
con committed
196

197 198
    // cache
    bool m_dirty;
con's avatar
con committed
199
    QStringList m_projectFiles;
200 201 202
    QStringList m_includePaths;
    QStringList m_frameworkPaths;
    QByteArray m_definedMacros;
con's avatar
con committed
203

204 205 206
    QMap<QString, QStringList> m_includesInPaths;
    QStringList m_headerSuffixes;

con's avatar
con committed
207 208 209
    // editor integration
    QMap<TextEditor::ITextEditor *, CppEditorSupport *> m_editorSupport;

210 211
    QSet<AbstractEditorSupport *> m_addtionalEditorSupport;

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

215
    mutable QMutex mutex;
216
    mutable QMutex protectSnapshot;
217

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

    QList<Editor> m_todo;

    QTimer *m_updateEditorSelectionsTimer;
Roberto Raggi's avatar
Roberto Raggi committed
230 231

    QFutureSynchronizer<void> m_synchronizer;
232
    unsigned m_revision;
Roberto Raggi's avatar
Roberto Raggi committed
233 234

    CppFindReferences *m_findReferences;
235
    bool m_indexerEnabled;
con's avatar
con committed
236
};
237 238
#endif

239
class CPPTOOLS_EXPORT CppPreprocessor: public CPlusPlus::Client
240 241 242 243 244 245 246 247 248 249 250 251 252
{
public:
#ifndef ICHECK_BUILD
    CppPreprocessor(QPointer<CppModelManager> modelManager);
#else
    CppPreprocessor(QPointer<CPlusPlus::ParseManager> modelManager);
#endif
    virtual ~CppPreprocessor();

    void setRevision(unsigned revision);
    void setWorkingCopy(const CppModelManagerInterface::WorkingCopy &workingCopy);
    void setIncludePaths(const QStringList &includePaths);
    void setFrameworkPaths(const QStringList &frameworkPaths);
253
    void addFrameworkPath(const QString &frameworkPath);
254 255 256 257 258 259 260 261 262 263 264
    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; }

public: // attributes
265
    CPlusPlus::Snapshot snapshot;
266 267 268 269 270 271

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
272
    QString tryIncludeFile_helper(QString &fileName, IncludeType type, unsigned *revision);
273 274 275

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

276 277
    virtual void macroAdded(const CPlusPlus::Macro &macro);
    virtual void passedMacroDefinitionCheck(unsigned offset, const CPlusPlus::Macro &macro);
278 279
    virtual void failedMacroDefinitionCheck(unsigned offset, const QByteArray &name);
    virtual void startExpandingMacro(unsigned offset,
280
                                     const CPlusPlus::Macro &macro,
281 282
                                     const QByteArray &originalText,
                                     bool inCondition,
283 284
                                     const QVector<CPlusPlus::MacroArgumentReference> &actuals);
    virtual void stopExpandingMacro(unsigned offset, const CPlusPlus::Macro &macro);
285 286 287 288 289 290 291 292 293
    virtual void startSkippingBlocks(unsigned offset);
    virtual void stopSkippingBlocks(unsigned offset);
    virtual void sourceNeeded(QString &fileName, IncludeType type,
                              unsigned line);

private:
#ifndef ICHECK_BUILD
    QPointer<CppModelManager> m_modelManager;
#endif
294 295
    CPlusPlus::Environment env;
    CPlusPlus::Preprocessor preprocess;
296 297 298 299 300 301
    QStringList m_includePaths;
    QStringList m_systemIncludePaths;
    CppModelManagerInterface::WorkingCopy m_workingCopy;
    QStringList m_projectFiles;
    QStringList m_frameworkPaths;
    QSet<QString> m_included;
302
    CPlusPlus::Document::Ptr m_currentDoc;
303 304 305
    QSet<QString> m_todo;
    QSet<QString> m_processed;
    unsigned m_revision;
Roberto Raggi's avatar
Roberto Raggi committed
306
    QHash<QString, QString> m_fileNameCache;
307
};
con's avatar
con committed
308 309 310 311 312

} // namespace Internal
} // namespace CppTools

#endif // CPPMODELMANAGER_H