cppmodelmanager.h 9.81 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
**
con's avatar
con committed
9
** No Commercial Usage
10
**
con's avatar
con committed
11 12 13 14
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
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
**
con's avatar
con committed
25 26 27 28 29 30
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
con's avatar
con committed
31
**
32
**************************************************************************/
hjk's avatar
hjk committed
33

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

37 38
#include "cpptools_global.h"
#include <cplusplus/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 52 53 54 55 56 57
#  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
58
#endif
con's avatar
con committed
59 60 61 62 63 64 65 66

namespace Core {
class ICore;
class IEditor;
}

namespace TextEditor {
class ITextEditor;
Roberto Raggi's avatar
Roberto Raggi committed
67
class BaseTextEditor;
con's avatar
con committed
68 69 70 71 72 73
}

namespace ProjectExplorer {
class ProjectExplorerPlugin;
}

74 75 76 77
namespace CPlusPlus {
    class ParseManager;
}

con's avatar
con committed
78
namespace CppTools {
79

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 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 103

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

104 105
    virtual QStringList includesInPath(const QString &path) const;

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

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

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

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

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

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

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

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

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

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

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

134 135 136
    void setHeaderSuffixes(const QStringList &suffixes)
    { m_headerSuffixes = suffixes; }

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 150

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

private:
157
    WorkingCopy buildWorkingCopyList();
158

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

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

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

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

183
    void ensureUpdated();
184 185 186 187
    QStringList internalProjectFiles() const;
    QStringList internalIncludePaths() const;
    QStringList internalFrameworkPaths() const;
    QByteArray internalDefinedMacros() const;
188

189
    void setIncludesInPaths(const QMap<QString, QStringList> &includesInPaths);
190 191 192 193

    static void updateIncludesInPaths(QFutureInterface<void> &future,
                                      CppModelManager *manager,
                                      QStringList paths,
194
                                      QStringList frameworkPaths,
195 196
                                      QStringList suffixes);

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

private:
    Core::ICore *m_core;
203
    CPlusPlus::Snapshot m_snapshot;
con's avatar
con committed
204

205 206
    // cache
    bool m_dirty;
con's avatar
con committed
207
    QStringList m_projectFiles;
208 209 210
    QStringList m_includePaths;
    QStringList m_frameworkPaths;
    QByteArray m_definedMacros;
con's avatar
con committed
211

212 213 214
    QMap<QString, QStringList> m_includesInPaths;
    QStringList m_headerSuffixes;

con's avatar
con committed
215 216 217
    // editor integration
    QMap<TextEditor::ITextEditor *, CppEditorSupport *> m_editorSupport;

218 219
    QSet<AbstractEditorSupport *> m_addtionalEditorSupport;

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

223
    mutable QMutex mutex;
224
    mutable QMutex protectSnapshot;
225

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

    QList<Editor> m_todo;

    QTimer *m_updateEditorSelectionsTimer;
Roberto Raggi's avatar
Roberto Raggi committed
238 239

    QFutureSynchronizer<void> m_synchronizer;
240
    unsigned m_revision;
Roberto Raggi's avatar
Roberto Raggi committed
241 242

    CppFindReferences *m_findReferences;
243
    bool m_indexerEnabled;
con's avatar
con committed
244
};
245 246
#endif

247
class CPPTOOLS_EXPORT CppPreprocessor: public CPlusPlus::Client
248 249 250 251 252 253 254 255 256 257
{
public:
#ifndef ICHECK_BUILD
    CppPreprocessor(QPointer<CppModelManager> modelManager);
#else
    CppPreprocessor(QPointer<CPlusPlus::ParseManager> modelManager);
#endif
    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 272
    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
273
    CPlusPlus::Snapshot snapshot;
274 275 276 277 278 279

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
280
    QString tryIncludeFile_helper(QString &fileName, IncludeType type, unsigned *revision);
281 282 283

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

284 285
    virtual void macroAdded(const CPlusPlus::Macro &macro);
    virtual void passedMacroDefinitionCheck(unsigned offset, const CPlusPlus::Macro &macro);
286 287
    virtual void failedMacroDefinitionCheck(unsigned offset, const QByteArray &name);
    virtual void startExpandingMacro(unsigned offset,
288
                                     const CPlusPlus::Macro &macro,
289 290
                                     const QByteArray &originalText,
                                     bool inCondition,
291 292
                                     const QVector<CPlusPlus::MacroArgumentReference> &actuals);
    virtual void stopExpandingMacro(unsigned offset, const CPlusPlus::Macro &macro);
293 294 295 296 297 298 299 300 301
    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
302 303
    CPlusPlus::Environment env;
    CPlusPlus::Preprocessor preprocess;
304 305
    QStringList m_includePaths;
    QStringList m_systemIncludePaths;
306
    CPlusPlus::CppModelManagerInterface::WorkingCopy m_workingCopy;
307 308 309
    QStringList m_projectFiles;
    QStringList m_frameworkPaths;
    QSet<QString> m_included;
310
    CPlusPlus::Document::Ptr m_currentDoc;
311 312 313
    QSet<QString> m_todo;
    QSet<QString> m_processed;
    unsigned m_revision;
Roberto Raggi's avatar
Roberto Raggi committed
314
    QHash<QString, QString> m_fileNameCache;
315
};
con's avatar
con committed
316 317 318 319 320

} // namespace Internal
} // namespace CppTools

#endif // CPPMODELMANAGER_H