pp-engine.h 6.77 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.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
Tobias Hunger's avatar
Tobias Hunger committed
29
** Nokia at info@qt.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 "PreprocessorClient.h"
56
#include "pp-macro-expander.h"
hjk's avatar
hjk committed
57

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

62
namespace CPlusPlus {
con's avatar
con committed
63

Roberto Raggi's avatar
Roberto Raggi committed
64
struct Value;
65
class Environment;
66
67
68
69

class CPLUSPLUS_EXPORT Preprocessor
{
public:
Roberto Raggi's avatar
Roberto Raggi committed
70
    Preprocessor(Client *client, Environment *env);
71

72
73
    QByteArray operator()(const QString &filename, const QString &source);
    QByteArray operator()(const QString &filename, const QByteArray &source);
con's avatar
con committed
74

75
    void preprocess(const QString &filename,
76
77
78
                    const QByteArray &source,
                    QByteArray *result);

79
80
81
    bool expandMacros() const;
    void setExpandMacros(bool expandMacros);

82
83
84
85
private:
    enum { MAX_LEVEL = 512 };

    enum PP_DIRECTIVE_TYPE
con's avatar
con committed
86
    {
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
        PP_UNKNOWN_DIRECTIVE,
        PP_DEFINE,
        PP_IMPORT,
        PP_INCLUDE,
        PP_INCLUDE_NEXT,
        PP_ELIF,
        PP_ELSE,
        PP_ENDIF,
        PP_IF,
        PP_IFDEF,
        PP_IFNDEF,
        PP_UNDEF
    };

    typedef const CPlusPlus::Token *TokenIterator;

    struct State {
        QByteArray source;
        QVector<CPlusPlus::Token> tokens;
        TokenIterator dot;
con's avatar
con committed
107
108
    };

109
110
    bool markGeneratedTokens(bool markGeneratedTokens, TokenIterator dot = 0);

111
    QByteArray expand(const QByteArray &source);
112
113
    void expand(const QByteArray &source, QByteArray *result);
    void expand(const char *first, const char *last, QByteArray *result);
114
115
    void expandBuiltinMacro(TokenIterator identifierToken,
                            const QByteArray &spell);
116
117
118
    void expandObjectLikeMacro(TokenIterator identifierToken,
                               const QByteArray &spell,
                               Macro *m, QByteArray *result);
119
120
    void expandFunctionLikeMacro(TokenIterator identifierToken, Macro *m,
                                 const QVector<MacroArgumentReference> &actuals);
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

    void resetIfLevel();
    bool testIfLevel();
    int skipping() const;

    PP_DIRECTIVE_TYPE classifyDirective(const QByteArray &directive) const;

    Value evalExpression(TokenIterator firstToken,
                         TokenIterator lastToken,
                         const QByteArray &source) const;

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

    const char *startOfToken(const CPlusPlus::Token &token) const;
    const char *endOfToken(const CPlusPlus::Token &token) const;

    QByteArray tokenSpell(const CPlusPlus::Token &token) const;
    QByteArray tokenText(const CPlusPlus::Token &token) const; // does a deep copy

140
141
    void collectActualArguments(QVector<MacroArgumentReference> *actuals);
    MacroArgumentReference collectOneActualArgument();
142

143
    void processNewline(bool force = false);
144

145
146
147
    void processSkippingBlocks(bool skippingBlocks,
                               TokenIterator dot, TokenIterator lastToken);

148
149
150
151
    Macro *processObjectLikeMacro(TokenIterator identifierToken,
                                  const QByteArray &spell,
                                  Macro *m);

152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
    void processDirective(TokenIterator dot, TokenIterator lastToken);
    void processInclude(bool skipCurrentPath,
                        TokenIterator dot, TokenIterator lastToken,
                        bool acceptMacros = true);
    void processDefine(TokenIterator dot, TokenIterator lastToken);
    void processIf(TokenIterator dot, TokenIterator lastToken);
    void processElse(TokenIterator dot, TokenIterator lastToken);
    void processElif(TokenIterator dot, TokenIterator lastToken);
    void processEndif(TokenIterator dot, TokenIterator lastToken);
    void processIfdef(bool checkUndefined,
                      TokenIterator dot, TokenIterator lastToken);
    void processUndef(TokenIterator dot, TokenIterator lastToken);

    bool isQtReservedWord(const QByteArray &name) const;

    State state() const;
    void pushState(const State &state);
    void popState();

    State createStateFromSource(const QByteArray &source) const;

173
174
175
176
    void out(const QByteArray &text);
    void out(char ch);
    void out(const char *s);

177
    QString string(const char *first, int len) const;
178
    bool maybeAfterComment() const;
179

180
181
private:
    Client *client;
Roberto Raggi's avatar
Roberto Raggi committed
182
    Environment *env;
183
184
    MacroExpander _expand;

185
    QBitArray _skipping; // ### move in state
186
    QBitArray _trueTest; // ### move in state
187
188
189
190
191
192
193
194
195
    int iflevel; // ### move in state

    QList<State> _savedStates;

    QByteArray _source;
    QVector<CPlusPlus::Token> _tokens;
    TokenIterator _dot;

    QByteArray *_result;
196
    bool _markGeneratedTokens;
197
198

    QString _originalSource;
199
    bool _expandMacros;
200
201
};

202
} // namespace CPlusPlus
con's avatar
con committed
203

204
#endif // CPLUSPLUS_PP_ENGINE_H