cppsourceprocessor.h 5.38 KB
Newer Older
Nikolai Kosjar's avatar
Nikolai Kosjar committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/****************************************************************************
**
** 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
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** 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.
**
** 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.
**
****************************************************************************/

30 31
#ifndef CPPSOURCEPROCESSOR_H
#define CPPSOURCEPROCESSOR_H
32

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

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

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

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

46 47 48 49
QT_BEGIN_NAMESPACE
class QTextCodec;
QT_END_NAMESPACE

50 51 52
namespace CppTools {
namespace Internal {

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

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

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

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

67 68 69
    void setDumpFileNameWhileParsing(bool onoff)
    { m_dumpFileNameWhileParsing = onoff; }

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

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

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

82 83
    void setGlobalSnapshot(const CPlusPlus::Snapshot &snapshot) { m_globalSnapshot = snapshot; }

84
private:
85
    void addFrameworkPath(const ProjectPart::HeaderPath &frameworkPath);
86 87

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

89 90
    bool getFileContents(const QString &absoluteFilePath, QByteArray *contents,
                         unsigned *revision) const;
91 92 93 94 95 96
    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);

97
    // Client interface
98
    void macroAdded(const CPlusPlus::Macro &macro) Q_DECL_OVERRIDE;
99
    void passedMacroDefinitionCheck(unsigned bytesOffset, unsigned utf16charsOffset,
100
                                    unsigned line, const CPlusPlus::Macro &macro) Q_DECL_OVERRIDE;
101
    void failedMacroDefinitionCheck(unsigned bytesOffset, unsigned utf16charOffset,
102
                                    const CPlusPlus::ByteArrayRef &name) Q_DECL_OVERRIDE;
103
    void notifyMacroReference(unsigned bytesOffset, unsigned utf16charOffset,
104
                              unsigned line, const CPlusPlus::Macro &macro) Q_DECL_OVERRIDE;
105 106
    void startExpandingMacro(unsigned bytesOffset, unsigned utf16charOffset,
                             unsigned line, const CPlusPlus::Macro &macro,
107 108 109 110 111
                             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;
112
    void sourceNeeded(unsigned line, const QString &fileName, IncludeType type,
113
                      const QStringList &initialIncludes) Q_DECL_OVERRIDE;
114 115 116

private:
    CPlusPlus::Snapshot m_snapshot;
117
    CPlusPlus::Snapshot m_globalSnapshot;
118
    DocumentCallback m_documentFinished;
119 120 121
    bool m_dumpFileNameWhileParsing;
    CPlusPlus::Environment m_env;
    CPlusPlus::Preprocessor m_preprocess;
122
    ProjectPart::HeaderPaths m_headerPaths;
123
    CppTools::WorkingCopy m_workingCopy;
124 125 126 127 128 129
    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;
130
    QTextCodec *m_defaultCodec;
131 132 133 134 135
};

} // namespace Internal
} // namespace CppTools

136
#endif // CPPSOURCEPROCESSOR_H