cppsourceprocessor.h 5.31 KB
Newer Older
Nikolai Kosjar's avatar
Nikolai Kosjar committed
1 2 3 4 5 6 7 8 9 10 11 12
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
Eike Ziller's avatar
Eike Ziller committed
13 14
** conditions see http://www.qt.io/licensing.  For further information
** use the contact form at http://www.qt.io/contact-us.
Nikolai Kosjar's avatar
Nikolai Kosjar committed
15 16 17
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
Nikolai Kosjar's avatar
Nikolai Kosjar committed
24 25 26 27 28 29 30
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

31 32
#ifndef CPPSOURCEPROCESSOR_H
#define CPPSOURCEPROCESSOR_H
33

34
#include "cppmodelmanager.h"
35
#include "cppworkingcopy.h"
36 37 38 39 40 41

#include <cplusplus/PreprocessorEnvironment.h>
#include <cplusplus/pp-engine.h>

#include <QHash>
#include <QPointer>
42 43
#include <QSet>
#include <QStringList>
44

hjk's avatar
hjk committed
45 46
#include <functional>

47 48 49 50
QT_BEGIN_NAMESPACE
class QTextCodec;
QT_END_NAMESPACE

51 52 53
namespace CppTools {
namespace Internal {

54
// Documentation inside.
55
class CppSourceProcessor: public CPlusPlus::Client
56
{
57
    Q_DISABLE_COPY(CppSourceProcessor)
58

59 60 61
public:
    typedef std::function<void (const CPlusPlus::Document::Ptr &)> DocumentCallback;

62
public:
63 64
    static QString cleanPath(const QString &path);

65
    CppSourceProcessor(const CPlusPlus::Snapshot &snapshot, DocumentCallback documentFinished);
66
    ~CppSourceProcessor();
67 68

    void setRevision(unsigned revision);
69
    void setWorkingCopy(const CppTools::WorkingCopy &workingCopy);
70
    void setHeaderPaths(const ProjectPart::HeaderPaths &headerPaths);
71
    void setTodo(const QSet<QString> &files);
72

73
    void run(const QString &fileName, const QStringList &initialIncludes = QStringList());
74 75 76
    void removeFromCache(const QString &fileName);
    void resetEnvironment();

77 78
    CPlusPlus::Snapshot snapshot() const { return m_snapshot; }
    const QSet<QString> &todo() const { return m_todo; }
79

80 81
    void setGlobalSnapshot(const CPlusPlus::Snapshot &snapshot) { m_globalSnapshot = snapshot; }

82
private:
83
    void addFrameworkPath(const ProjectPart::HeaderPath &frameworkPath);
84 85

    CPlusPlus::Document::Ptr switchCurrentDocument(CPlusPlus::Document::Ptr doc);
86

87 88
    bool getFileContents(const QString &absoluteFilePath, QByteArray *contents,
                         unsigned *revision) const;
89 90 91 92 93 94
    bool checkFile(const QString &absoluteFilePath) const;
    QString resolveFile(const QString &fileName, IncludeType type);
    QString resolveFile_helper(const QString &fileName, IncludeType type);

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

95
    // Client interface
96
    void macroAdded(const CPlusPlus::Macro &macro) Q_DECL_OVERRIDE;
97
    void passedMacroDefinitionCheck(unsigned bytesOffset, unsigned utf16charsOffset,
98
                                    unsigned line, const CPlusPlus::Macro &macro) Q_DECL_OVERRIDE;
99
    void failedMacroDefinitionCheck(unsigned bytesOffset, unsigned utf16charOffset,
100
                                    const CPlusPlus::ByteArrayRef &name) Q_DECL_OVERRIDE;
101
    void notifyMacroReference(unsigned bytesOffset, unsigned utf16charOffset,
102
                              unsigned line, const CPlusPlus::Macro &macro) Q_DECL_OVERRIDE;
103 104
    void startExpandingMacro(unsigned bytesOffset, unsigned utf16charOffset,
                             unsigned line, const CPlusPlus::Macro &macro,
105 106 107 108 109
                             const QVector<CPlusPlus::MacroArgumentReference> &actuals) Q_DECL_OVERRIDE;
    void stopExpandingMacro(unsigned bytesOffset, const CPlusPlus::Macro &macro) Q_DECL_OVERRIDE;
    void markAsIncludeGuard(const QByteArray &macroName) Q_DECL_OVERRIDE;
    void startSkippingBlocks(unsigned utf16charsOffset) Q_DECL_OVERRIDE;
    void stopSkippingBlocks(unsigned utf16charsOffset) Q_DECL_OVERRIDE;
110
    void sourceNeeded(unsigned line, const QString &fileName, IncludeType type,
111
                      const QStringList &initialIncludes) Q_DECL_OVERRIDE;
112 113 114

private:
    CPlusPlus::Snapshot m_snapshot;
115
    CPlusPlus::Snapshot m_globalSnapshot;
116
    DocumentCallback m_documentFinished;
117 118
    CPlusPlus::Environment m_env;
    CPlusPlus::Preprocessor m_preprocess;
119
    ProjectPart::HeaderPaths m_headerPaths;
120
    CppTools::WorkingCopy m_workingCopy;
121 122 123 124 125 126
    QSet<QString> m_included;
    CPlusPlus::Document::Ptr m_currentDoc;
    QSet<QString> m_todo;
    QSet<QString> m_processed;
    unsigned m_revision;
    QHash<QString, QString> m_fileNameCache;
127
    QTextCodec *m_defaultCodec;
128 129 130 131 132
};

} // namespace Internal
} // namespace CppTools

133
#endif // CPPSOURCEPROCESSOR_H