1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 Copyright 2005 Roberto Raggi <roberto@kdevelop.org>
35 Permission to use, copy, modify, distribute, and sell this software and its
36 documentation for any purpose is hereby granted without fee, provided that
37 the above copyright notice appear in all copies and that both that
38 copyright notice and this permission notice appear in supporting
41 The above copyright notice and this permission notice shall be included in
42 all copies or substantial portions of the Software.
44 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
47 KDEVELOP TEAM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
48 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
49 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
52 #ifndef CPLUSPLUS_PP_ENGINE_H
53 #define CPLUSPLUS_PP_ENGINE_H
55 #include "PreprocessorClient.h"
56 #include "pp-macro-expander.h"
67 class CPLUSPLUS_EXPORT Preprocessor
70 Preprocessor(Client *client, Environment *env);
72 QByteArray operator()(const QString &filename, const QString &source);
73 QByteArray operator()(const QString &filename, const QByteArray &source);
75 void preprocess(const QString &filename,
76 const QByteArray &source,
79 bool expandMacros() const;
80 void setExpandMacros(bool expandMacros);
83 enum { MAX_LEVEL = 512 };
85 enum PP_DIRECTIVE_TYPE
101 typedef const CPlusPlus::Token *TokenIterator;
105 QVector<CPlusPlus::Token> tokens;
109 bool markGeneratedTokens(bool markGeneratedTokens, TokenIterator dot = 0);
111 QByteArray expand(const QByteArray &source);
112 void expand(const QByteArray &source, QByteArray *result);
113 void expand(const char *first, const char *last, QByteArray *result);
114 void expandBuiltinMacro(TokenIterator identifierToken,
115 const QByteArray &spell);
116 void expandObjectLikeMacro(TokenIterator identifierToken,
117 const QByteArray &spell,
118 Macro *m, QByteArray *result);
119 void expandFunctionLikeMacro(TokenIterator identifierToken, Macro *m,
120 const QVector<MacroArgumentReference> &actuals);
124 int skipping() const;
126 PP_DIRECTIVE_TYPE classifyDirective(const QByteArray &directive) const;
128 Value evalExpression(TokenIterator firstToken,
129 TokenIterator lastToken,
130 const QByteArray &source) const;
132 QVector<CPlusPlus::Token> tokenize(const QByteArray &text) const;
134 const char *startOfToken(const CPlusPlus::Token &token) const;
135 const char *endOfToken(const CPlusPlus::Token &token) const;
137 QByteArray tokenSpell(const CPlusPlus::Token &token) const;
138 QByteArray tokenText(const CPlusPlus::Token &token) const; // does a deep copy
140 void collectActualArguments(QVector<MacroArgumentReference> *actuals);
141 MacroArgumentReference collectOneActualArgument();
143 void processNewline(bool force = false);
145 void processSkippingBlocks(bool skippingBlocks,
146 TokenIterator dot, TokenIterator lastToken);
148 Macro *processObjectLikeMacro(TokenIterator identifierToken,
149 const QByteArray &spell,
152 void processDirective(TokenIterator dot, TokenIterator lastToken);
153 void processInclude(bool skipCurrentPath,
154 TokenIterator dot, TokenIterator lastToken,
155 bool acceptMacros = true);
156 void processDefine(TokenIterator dot, TokenIterator lastToken);
157 void processIf(TokenIterator dot, TokenIterator lastToken);
158 void processElse(TokenIterator dot, TokenIterator lastToken);
159 void processElif(TokenIterator dot, TokenIterator lastToken);
160 void processEndif(TokenIterator dot, TokenIterator lastToken);
161 void processIfdef(bool checkUndefined,
162 TokenIterator dot, TokenIterator lastToken);
163 void processUndef(TokenIterator dot, TokenIterator lastToken);
165 bool isQtReservedWord(const QByteArray &name) const;
168 void pushState(const State &state);
171 State createStateFromSource(const QByteArray &source) const;
173 void out(const QByteArray &text);
175 void out(const char *s);
177 QString string(const char *first, int len) const;
178 bool maybeAfterComment() const;
183 MacroExpander _expand;
185 QBitArray _skipping; // ### move in state
186 QBitArray _trueTest; // ### move in state
187 int iflevel; // ### move in state
189 QList<State> _savedStates;
192 QVector<CPlusPlus::Token> _tokens;
196 bool _markGeneratedTokens;
198 QString _originalSource;
202 } // namespace CPlusPlus
204 #endif // CPLUSPLUS_PP_ENGINE_H