pp-engine.h 6.65 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) 2012 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
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
con's avatar
con committed
30
**
31
**************************************************************************/
con's avatar
con committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/*
  Copyright 2005 Roberto Raggi <roberto@kdevelop.org>

  Permission to use, copy, modify, distribute, and sell this software and its
  documentation for any purpose is hereby granted without fee, provided that
  the above copyright notice appear in all copies and that both that
  copyright notice and this permission notice appear in supporting
  documentation.

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  KDEVELOP TEAM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

52 53
#ifndef CPLUSPLUS_PP_ENGINE_H
#define CPLUSPLUS_PP_ENGINE_H
con's avatar
con committed
54

55
#include "PPToken.h"
56
#include "PreprocessorClient.h"
hjk's avatar
hjk committed
57

58
#include <Lexer.h>
con's avatar
con committed
59 60
#include <Token.h>
#include <QVector>
61
#include <QBitArray>
62
#include <QByteArray>
con's avatar
con committed
63

64
namespace CPlusPlus {
con's avatar
con committed
65

66
class Environment;
67

68 69 70 71 72 73
namespace Internal {
class PPToken;
struct TokenBuffer;
struct Value;
}

74 75
class CPLUSPLUS_EXPORT Preprocessor
{
76 77 78
    typedef Internal::PPToken PPToken;
    typedef Internal::Value Value;

79
public:
Roberto Raggi's avatar
Roberto Raggi committed
80
    Preprocessor(Client *client, Environment *env);
81

82 83
    QByteArray run(const QString &filename, const QString &source);
    QByteArray run(const QString &filename, const QByteArray &source, bool noLines = false, bool markGeneratedTokens = true);
con's avatar
con committed
84

85 86 87
    bool expandMacros() const;
    void setExpandMacros(bool expandMacros);

88 89 90
    bool keepComments() const;
    void setKeepComments(bool keepComments);

91
private:
92 93
    void preprocess(const QString &filename,
                    const QByteArray &source,
94
                    QByteArray *result, bool noLines, bool markGeneratedTokens, bool inCondition,
Leandro Melo's avatar
Leandro Melo committed
95
                    unsigned offsetRef = 0, unsigned envLineRef = 1);
96

97 98 99
    enum { MAX_LEVEL = 512 };

    struct State {
100
        State();
con's avatar
con committed
101

102 103 104
        void pushTokenBuffer(const PPToken *start, const PPToken *end, const Macro *macro);
        void popTokenBuffer();

105
        QString m_currentFileName;
106

107 108 109 110 111 112
        QByteArray m_source;
        Lexer *m_lexer;
        QBitArray m_skipping;
        QBitArray m_trueTest;
        int m_ifLevel;
        Internal::TokenBuffer *m_tokenBuffer;
113
        unsigned m_tokenBufferDepth;
114
        bool m_inPreprocessorDirective;
115

116 117
        QByteArray *m_result;
        bool m_markGeneratedTokens;
118

119 120 121
        bool m_noLines;
        bool m_inCondition;
        bool m_inDefine;
122 123

        unsigned m_offsetRef;
Leandro Melo's avatar
Leandro Melo committed
124
        unsigned m_envLineRef;
125 126 127 128 129 130 131
    };

    void handleDefined(PPToken *tk);
    void pushToken(PPToken *tk);
    void lex(PPToken *tk);
    void skipPreprocesorDirective(PPToken *tk);
    bool handleIdentifier(PPToken *tk);
132 133 134
    bool handleFunctionLikeMacro(PPToken *tk, const Macro *macro, QVector<PPToken> &body,
                                 bool addWhitespaceMarker,
                                 const QVector<QVector<PPToken> > &actuals);
135

136 137
    bool skipping() const
    { return m_state.m_skipping[m_state.m_ifLevel]; }
138 139 140

    QVector<CPlusPlus::Token> tokenize(const QByteArray &text) const;

141 142
    bool collectActualArguments(PPToken *tk, QVector<QVector<PPToken> > *actuals);
    void scanActualArgument(PPToken *tk, QVector<PPToken> *tokens);
143

144 145 146 147 148 149 150 151 152 153 154
    void handlePreprocessorDirective(PPToken *tk);
    void handleIncludeDirective(PPToken *tk);
    void handleDefineDirective(PPToken *tk);
    QByteArray expand(PPToken *tk, PPToken *lastConditionToken = 0);
    const Internal::PPToken evalExpression(PPToken *tk, Value &result);
    void handleIfDirective(PPToken *tk);
    void handleElifDirective(PPToken *tk, const PPToken &poundToken);
    void handleElseDirective(PPToken *tk, const PPToken &poundToken);
    void handleEndIfDirective(PPToken *tk, const PPToken &poundToken);
    void handleIfDefDirective(bool checkUndefined, PPToken *tk);
    void handleUndefDirective(PPToken *tk);
155

156
    static bool isQtReservedWord(const ByteArrayRef &name);
157

158 159
    inline bool atStartOfOutputLine() const
    { return (m_state.m_result && !m_state.m_result->isEmpty()) ? m_state.m_result->end()[-1] == '\n' : true; }
160

161 162 163 164 165
    inline void startNewOutputLine() const
    {
        if (m_state.m_result && !m_state.m_result->isEmpty() && m_state.m_result->end()[-1] != '\n')
            out('\n');
    }
166

167 168
    void genLine(unsigned lineno, const QByteArray &fileName) const;

169 170
    inline void out(const QByteArray &text) const
    { if (m_state.m_result) m_state.m_result->append(text); }
171

172 173
    inline void out(char ch) const
    { if (m_state.m_result) m_state.m_result->append(ch); }
174

175 176
    inline void out(const char *s) const
    { if (m_state.m_result) m_state.m_result->append(s); }
177

178
    inline void out(const ByteArrayRef &ref) const
179
    { if (m_state.m_result) m_state.m_result->append(ref.start(), ref.length()); }
180

181
    PPToken generateToken(enum Kind kind, const char *content, int len, unsigned lineno, bool addQuotes);
182
    PPToken generateConcatenated(const PPToken &leftTk, const PPToken &rightTk);
183

184
    void startSkippingBlocks(const PPToken &tk) const;
185

186 187 188 189
private:
    Client *m_client;
    Environment *m_env;
    QByteArray m_scratchBuffer;
190

191 192
    bool m_expandMacros;
    bool m_keepComments;
193

194
    State m_state;
195 196
};

197
} // namespace CPlusPlus
con's avatar
con committed
198

199
#endif // CPLUSPLUS_PP_ENGINE_H