cppsourceprocessor.h 5.39 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 "cppmodelmanagerinterface.h"
34
#include "cppworkingcopy.h"
35 36 37

#include <cplusplus/PreprocessorEnvironment.h>
#include <cplusplus/pp-engine.h>
38
#include <utils/qtcoverride.h>
39 40 41

#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 69 70
    void setDumpFileNameWhileParsing(bool onoff)
    { m_dumpFileNameWhileParsing = onoff; }

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

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

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

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

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

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

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

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

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

} // namespace Internal
} // namespace CppTools

137
#endif // CPPSOURCEPROCESSOR_H