| 
									
										
										
										
											2009-02-25 09:15:00 +01:00
										 |  |  | /**************************************************************************
 | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | ** | 
					
						
							|  |  |  | ** This file is part of Qt Creator | 
					
						
							|  |  |  | ** | 
					
						
							| 
									
										
										
										
											2012-01-26 18:33:46 +01:00
										 |  |  | ** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2011-11-02 15:59:12 +01:00
										 |  |  | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2008-12-02 14:17:16 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2009-02-25 09:15:00 +01:00
										 |  |  | ** GNU Lesser General Public License Usage | 
					
						
							| 
									
										
										
										
											2008-12-02 14:17:16 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2011-04-13 08:42:33 +02:00
										 |  |  | ** 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.
 | 
					
						
							| 
									
										
										
										
											2008-12-02 14:17:16 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2010-12-17 16:01:08 +01:00
										 |  |  | ** In addition, as a special exception, Nokia gives you certain additional | 
					
						
							| 
									
										
										
										
											2011-04-13 08:42:33 +02:00
										 |  |  | ** rights. These rights are described in the Nokia Qt LGPL Exception | 
					
						
							| 
									
										
										
										
											2010-12-17 16:01:08 +01:00
										 |  |  | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | 
					
						
							|  |  |  | ** | 
					
						
							| 
									
										
										
										
											2011-04-13 08:42:33 +02:00
										 |  |  | ** 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. | 
					
						
							|  |  |  | ** | 
					
						
							| 
									
										
										
										
											2010-12-17 16:01:08 +01:00
										 |  |  | ** If you have questions regarding the use of this file, please contact | 
					
						
							| 
									
										
										
										
											2011-11-02 15:59:12 +01:00
										 |  |  | ** Nokia at qt-info@nokia.com. | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2009-02-25 09:15:00 +01:00
										 |  |  | **************************************************************************/ | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |   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. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-22 11:40:53 +01:00
										 |  |  | #ifndef CPLUSPLUS_PP_ENGINE_H
 | 
					
						
							|  |  |  | #define CPLUSPLUS_PP_ENGINE_H
 | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  | #include "PPToken.h"
 | 
					
						
							| 
									
										
										
										
											2008-12-22 14:10:47 +01:00
										 |  |  | #include "PreprocessorClient.h"
 | 
					
						
							| 
									
										
										
										
											2008-12-02 15:08:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  | #include <Lexer.h>
 | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | #include <Token.h>
 | 
					
						
							|  |  |  | #include <QVector>
 | 
					
						
							| 
									
										
										
										
											2009-10-21 16:18:34 +02:00
										 |  |  | #include <QBitArray>
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  | #include <QByteArray>
 | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 12:17:39 +01:00
										 |  |  | namespace CPlusPlus { | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-15 15:38:20 +02:00
										 |  |  | class Environment; | 
					
						
							| 
									
										
										
										
											2009-03-02 19:03:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  | namespace Internal { | 
					
						
							|  |  |  | class PPToken; | 
					
						
							|  |  |  | struct TokenBuffer; | 
					
						
							|  |  |  | struct Value; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-02 19:03:07 +01:00
										 |  |  | class CPLUSPLUS_EXPORT Preprocessor | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  |     typedef Internal::PPToken PPToken; | 
					
						
							|  |  |  |     typedef Internal::Value Value; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-02 19:03:07 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2009-03-03 10:41:20 +01:00
										 |  |  |     Preprocessor(Client *client, Environment *env); | 
					
						
							| 
									
										
										
										
											2009-03-02 19:03:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-15 15:38:20 +02:00
										 |  |  |     QByteArray operator()(const QString &filename, const QString &source); | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  |     QByteArray operator()(const QString &filename, const QByteArray &source, bool noLines = false, bool markGeneratedTokens = true); | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-27 15:34:13 +02:00
										 |  |  |     void preprocess(const QString &filename, | 
					
						
							| 
									
										
										
										
											2009-05-13 15:28:11 +02:00
										 |  |  |                     const QByteArray &source, | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  |                     QByteArray *result, bool noLines, bool markGeneratedTokens, bool inCondition); | 
					
						
							| 
									
										
										
										
											2009-05-13 15:28:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-04 20:54:31 +02:00
										 |  |  |     bool expandMacros() const; | 
					
						
							|  |  |  |     void setExpandMacros(bool expandMacros); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-23 14:05:47 +02:00
										 |  |  |     bool keepComments() const; | 
					
						
							|  |  |  |     void setKeepComments(bool keepComments); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-02 19:03:07 +01:00
										 |  |  | private: | 
					
						
							|  |  |  |     enum { MAX_LEVEL = 512 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     struct State { | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  |         State(); | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  |         QString m_currentFileName; | 
					
						
							| 
									
										
										
										
											2009-03-04 14:00:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  |         QByteArray m_source; | 
					
						
							|  |  |  |         Lexer *m_lexer; | 
					
						
							|  |  |  |         QBitArray m_skipping; | 
					
						
							|  |  |  |         QBitArray m_trueTest; | 
					
						
							|  |  |  |         int m_ifLevel; | 
					
						
							|  |  |  |         Internal::TokenBuffer *m_tokenBuffer; | 
					
						
							|  |  |  |         bool m_inPreprocessorDirective; | 
					
						
							| 
									
										
										
										
											2009-03-02 19:03:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  |         QByteArray *m_result; | 
					
						
							|  |  |  |         bool m_markGeneratedTokens; | 
					
						
							| 
									
										
										
										
											2009-03-02 19:03:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  |         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); | 
					
						
							| 
									
										
										
										
											2009-03-02 19:03:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  |     bool skipping() const | 
					
						
							|  |  |  |     { return m_state.m_skipping[m_state.m_ifLevel]; } | 
					
						
							| 
									
										
										
										
											2009-03-02 19:03:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     QVector<CPlusPlus::Token> tokenize(const QByteArray &text) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  |     bool collectActualArguments(PPToken *tk, QVector<QVector<PPToken> > *actuals); | 
					
						
							|  |  |  |     void scanActualArgument(PPToken *tk, QVector<PPToken> *tokens); | 
					
						
							| 
									
										
										
										
											2009-03-02 19:03:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  |     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 £Token); | 
					
						
							|  |  |  |     void handleElseDirective(PPToken *tk, const PPToken £Token); | 
					
						
							|  |  |  |     void handleEndIfDirective(PPToken *tk, const PPToken £Token); | 
					
						
							|  |  |  |     void handleIfDefDirective(bool checkUndefined, PPToken *tk); | 
					
						
							|  |  |  |     void handleUndefDirective(PPToken *tk); | 
					
						
							| 
									
										
										
										
											2009-03-02 19:03:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  |     static bool isQtReservedWord(const Internal::ByteArrayRef &name); | 
					
						
							| 
									
										
										
										
											2009-03-03 16:59:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  |     void pushState(const State &newState); | 
					
						
							|  |  |  |     void popState(); | 
					
						
							| 
									
										
										
										
											2009-03-03 16:56:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  |     State createStateFromSource(const QString &fileName, const QByteArray &source, QByteArray *result, bool noLines, bool markGeneratedTokens, bool inCondition) const; | 
					
						
							| 
									
										
										
										
											2009-03-03 10:27:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  |     inline bool atStartOfOutputLine() const | 
					
						
							|  |  |  |     { return (m_state.m_result && !m_state.m_result->isEmpty()) ? m_state.m_result->end()[-1] == '\n' : true; } | 
					
						
							| 
									
										
										
										
											2009-03-03 16:56:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  |     inline void startNewOutputLine() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (m_state.m_result && !m_state.m_result->isEmpty() && m_state.m_result->end()[-1] != '\n') | 
					
						
							|  |  |  |             out('\n'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-03-02 19:03:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  |     inline void out(const QByteArray &text) const | 
					
						
							|  |  |  |     { if (m_state.m_result) m_state.m_result->append(text); } | 
					
						
							| 
									
										
										
										
											2009-03-02 19:03:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  |     inline void out(char ch) const | 
					
						
							|  |  |  |     { if (m_state.m_result) m_state.m_result->append(ch); } | 
					
						
							| 
									
										
										
										
											2009-03-02 19:03:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  |     inline void out(const char *s) const | 
					
						
							|  |  |  |     { if (m_state.m_result) m_state.m_result->append(s); } | 
					
						
							| 
									
										
										
										
											2009-03-02 19:03:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  |     inline void out(const Internal::ByteArrayRef &ref) const | 
					
						
							|  |  |  |     { if (m_state.m_result) m_state.m_result->append(ref.start(), ref.length()); } | 
					
						
							| 
									
										
										
										
											2009-05-13 15:28:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-15 15:38:20 +02:00
										 |  |  |     QString string(const char *first, int len) const; | 
					
						
							| 
									
										
										
										
											2011-08-30 14:29:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  |     PPToken generateToken(enum Kind kind, const Internal::ByteArrayRef &content, unsigned lineno, bool addQuotes); | 
					
						
							|  |  |  |     PPToken generateConcatenated(const PPToken &leftTk, const PPToken &rightTk); | 
					
						
							| 
									
										
										
										
											2009-03-02 19:03:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  |     void startSkippingBlocks(const PPToken &tk) const; | 
					
						
							| 
									
										
										
										
											2009-03-02 19:03:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  | private: | 
					
						
							|  |  |  |     Client *m_client; | 
					
						
							|  |  |  |     Environment *m_env; | 
					
						
							|  |  |  |     QByteArray m_scratchBuffer; | 
					
						
							| 
									
										
										
										
											2009-03-02 19:03:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  |     QList<State> m_savedStates; | 
					
						
							| 
									
										
										
										
											2009-03-02 19:03:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  |     QString m_originalSource; | 
					
						
							|  |  |  |     bool m_expandMacros; | 
					
						
							|  |  |  |     bool m_keepComments; | 
					
						
							| 
									
										
										
										
											2009-06-15 15:38:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 15:18:01 +02:00
										 |  |  |     State m_state; | 
					
						
							| 
									
										
										
										
											2009-03-02 19:03:07 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 12:17:39 +01:00
										 |  |  | } // namespace CPlusPlus
 | 
					
						
							| 
									
										
										
										
											2008-12-02 12:01:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-22 11:40:53 +01:00
										 |  |  | #endif // CPLUSPLUS_PP_ENGINE_H
 |