pp-engine.h 6.72 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
    QByteArray operator()(const QString &filename, const QString &source);
83
    QByteArray operator()(const QString &filename, const QByteArray &source, bool noLines = false, bool markGeneratedTokens = true);
con's avatar
con committed
84

85
    void preprocess(const QString &filename,
86
                    const QByteArray &source,
87
                    QByteArray *result, bool noLines, bool markGeneratedTokens, bool inCondition);
88

89
90
91
    bool expandMacros() const;
    void setExpandMacros(bool expandMacros);

92
93
94
    bool keepComments() const;
    void setKeepComments(bool keepComments);

95
96
97
98
private:
    enum { MAX_LEVEL = 512 };

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

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

104
        QString m_currentFileName;
105

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

115
116
        QByteArray *m_result;
        bool m_markGeneratedTokens;
117

118
119
120
121
122
123
124
125
126
127
128
        bool m_noLines;
        bool m_inCondition;
        bool m_inDefine;
    };

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

130
131
    bool skipping() const
    { return m_state.m_skipping[m_state.m_ifLevel]; }
132
133
134

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

135
136
    bool collectActualArguments(PPToken *tk, QVector<QVector<PPToken> > *actuals);
    void scanActualArgument(PPToken *tk, QVector<PPToken> *tokens);
137

138
139
140
141
142
143
144
145
146
147
148
    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);
149

150
    static bool isQtReservedWord(const ByteArrayRef &name);
151

152
153
    void pushState(const State &newState);
    void popState();
154

155
    State createStateFromSource(const QString &fileName, const QByteArray &source, QByteArray *result, bool noLines, bool markGeneratedTokens, bool inCondition) const;
156

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

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

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

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

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

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

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

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

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

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

190
    QList<State> m_savedStates;
191

192
193
194
    QString m_originalSource;
    bool m_expandMacros;
    bool m_keepComments;
195

196
    State m_state;
197
198
};

199
} // namespace CPlusPlus
con's avatar
con committed
200

201
#endif // CPLUSPLUS_PP_ENGINE_H