1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 Copyright 2005 Roberto Raggi <roberto@kdevelop.org>
36 Permission to use, copy, modify, distribute, and sell this software and its
37 documentation for any purpose is hereby granted without fee, provided that
38 the above copyright notice appear in all copies and that both that
39 copyright notice and this permission notice appear in supporting
42 The above copyright notice and this permission notice shall be included in
43 all copies or substantial portions of the Software.
45 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
46 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
47 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
48 KDEVELOP TEAM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
49 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
50 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
53 #ifndef CPLUSPLUS_PP_ENGINE_H
54 #define CPLUSPLUS_PP_ENGINE_H
56 #include "PreprocessorClient.h"
57 #include "pp-macro-expander.h"
68 class CPLUSPLUS_EXPORT Preprocessor
71 Preprocessor(Client *client, Environment *env);
73 QByteArray operator()(const QString &filename, const QString &source);
74 QByteArray operator()(const QString &filename, const QByteArray &source);
76 void preprocess(const QString &filename,
77 const QByteArray &source,
80 bool expandMacros() const;
81 void setExpandMacros(bool expandMacros);
84 enum { MAX_LEVEL = 512 };
86 enum PP_DIRECTIVE_TYPE
102 typedef const CPlusPlus::Token *TokenIterator;
106 QVector<CPlusPlus::Token> tokens;
110 bool markGeneratedTokens(bool markGeneratedTokens, TokenIterator dot = 0);
112 QByteArray expand(const QByteArray &source);
113 void expand(const QByteArray &source, QByteArray *result);
114 void expand(const char *first, const char *last, QByteArray *result);
115 void expandBuiltinMacro(TokenIterator identifierToken,
116 const QByteArray &spell);
117 void expandObjectLikeMacro(TokenIterator identifierToken,
118 const QByteArray &spell,
119 Macro *m, QByteArray *result);
120 void expandFunctionLikeMacro(TokenIterator identifierToken, Macro *m,
121 const QVector<MacroArgumentReference> &actuals);
125 int skipping() const;
127 PP_DIRECTIVE_TYPE classifyDirective(const QByteArray &directive) const;
129 Value evalExpression(TokenIterator firstToken,
130 TokenIterator lastToken,
131 const QByteArray &source) const;
133 QVector<CPlusPlus::Token> tokenize(const QByteArray &text) const;
135 const char *startOfToken(const CPlusPlus::Token &token) const;
136 const char *endOfToken(const CPlusPlus::Token &token) const;
138 QByteArray tokenSpell(const CPlusPlus::Token &token) const;
139 QByteArray tokenText(const CPlusPlus::Token &token) const; // does a deep copy
141 void collectActualArguments(QVector<MacroArgumentReference> *actuals);
142 MacroArgumentReference collectOneActualArgument();
144 void processNewline(bool force = false);
146 void processSkippingBlocks(bool skippingBlocks,
147 TokenIterator dot, TokenIterator lastToken);
149 Macro *processObjectLikeMacro(TokenIterator identifierToken,
150 const QByteArray &spell,
153 void processDirective(TokenIterator dot, TokenIterator lastToken);
154 void processInclude(bool skipCurrentPath,
155 TokenIterator dot, TokenIterator lastToken,
156 bool acceptMacros = true);
157 void processDefine(TokenIterator dot, TokenIterator lastToken);
158 void processIf(TokenIterator dot, TokenIterator lastToken);
159 void processElse(TokenIterator dot, TokenIterator lastToken);
160 void processElif(TokenIterator dot, TokenIterator lastToken);
161 void processEndif(TokenIterator dot, TokenIterator lastToken);
162 void processIfdef(bool checkUndefined,
163 TokenIterator dot, TokenIterator lastToken);
164 void processUndef(TokenIterator dot, TokenIterator lastToken);
166 bool isQtReservedWord(const QByteArray &name) const;
169 void pushState(const State &state);
172 State createStateFromSource(const QByteArray &source) const;
174 void out(const QByteArray &text);
176 void out(const char *s);
178 QString string(const char *first, int len) const;
179 bool maybeAfterComment() const;
184 MacroExpander _expand;
186 QBitArray _skipping; // ### move in state
187 QBitArray _trueTest; // ### move in state
188 int iflevel; // ### move in state
190 QList<State> _savedStates;
193 QVector<CPlusPlus::Token> _tokens;
197 bool _markGeneratedTokens;
199 QString _originalSource;
203 } // namespace CPlusPlus
205 #endif // CPLUSPLUS_PP_ENGINE_H