tools/icheck/parser/src/libs/cplusplus/pp-engine.h
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    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
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 /*
       
    42   Copyright 2005 Roberto Raggi <roberto@kdevelop.org>
       
    43 
       
    44   Permission to use, copy, modify, distribute, and sell this software and its
       
    45   documentation for any purpose is hereby granted without fee, provided that
       
    46   the above copyright notice appear in all copies and that both that
       
    47   copyright notice and this permission notice appear in supporting
       
    48   documentation.
       
    49 
       
    50   The above copyright notice and this permission notice shall be included in
       
    51   all copies or substantial portions of the Software.
       
    52 
       
    53   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
       
    54   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
       
    55   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
       
    56   KDEVELOP TEAM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
       
    57   AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
       
    58   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
       
    59 */
       
    60 
       
    61 #ifndef CPLUSPLUS_PP_ENGINE_H
       
    62 #define CPLUSPLUS_PP_ENGINE_H
       
    63 
       
    64 #include "PreprocessorClient.h"
       
    65 #include "pp-macro-expander.h"
       
    66 
       
    67 #include <Token.h>
       
    68 #include <QVector>
       
    69 #include <QBitArray>
       
    70 
       
    71 namespace CPlusPlus {
       
    72 
       
    73 struct Value;
       
    74 class Environment;
       
    75 
       
    76 class CPLUSPLUS_EXPORT Preprocessor
       
    77 {
       
    78 public:
       
    79     Preprocessor(Client *client, Environment *env);
       
    80 
       
    81     QByteArray operator()(const QString &filename, const QString &source);
       
    82     QByteArray operator()(const QString &filename, const QByteArray &source);
       
    83 
       
    84     void preprocess(const QString &filename,
       
    85                     const QByteArray &source,
       
    86                     QByteArray *result);
       
    87 
       
    88     bool expandMacros() const;
       
    89     void setExpandMacros(bool expandMacros);
       
    90 
       
    91 private:
       
    92     enum { MAX_LEVEL = 512 };
       
    93 
       
    94     enum PP_DIRECTIVE_TYPE
       
    95     {
       
    96         PP_UNKNOWN_DIRECTIVE,
       
    97         PP_DEFINE,
       
    98         PP_IMPORT,
       
    99         PP_INCLUDE,
       
   100         PP_INCLUDE_NEXT,
       
   101         PP_ELIF,
       
   102         PP_ELSE,
       
   103         PP_ENDIF,
       
   104         PP_IF,
       
   105         PP_IFDEF,
       
   106         PP_IFNDEF,
       
   107         PP_UNDEF
       
   108     };
       
   109 
       
   110     typedef const CPlusPlus::Token *TokenIterator;
       
   111 
       
   112     struct State {
       
   113         QByteArray source;
       
   114         QVector<CPlusPlus::Token> tokens;
       
   115         TokenIterator dot;
       
   116     };
       
   117 
       
   118     bool markGeneratedTokens(bool markGeneratedTokens, TokenIterator dot = 0);
       
   119 
       
   120     QByteArray expand(const QByteArray &source);
       
   121     void expand(const QByteArray &source, QByteArray *result);
       
   122     void expand(const char *first, const char *last, QByteArray *result);
       
   123     void expandBuiltinMacro(TokenIterator identifierToken,
       
   124                             const QByteArray &spell);
       
   125     void expandObjectLikeMacro(TokenIterator identifierToken,
       
   126                                const QByteArray &spell,
       
   127                                Macro *m, QByteArray *result);
       
   128     void expandFunctionLikeMacro(TokenIterator identifierToken, Macro *m,
       
   129                                  const QVector<MacroArgumentReference> &actuals);
       
   130 
       
   131     void resetIfLevel();
       
   132     bool testIfLevel();
       
   133     int skipping() const;
       
   134 
       
   135     PP_DIRECTIVE_TYPE classifyDirective(const QByteArray &directive) const;
       
   136 
       
   137     Value evalExpression(TokenIterator firstToken,
       
   138                          TokenIterator lastToken,
       
   139                          const QByteArray &source) const;
       
   140 
       
   141     QVector<CPlusPlus::Token> tokenize(const QByteArray &text) const;
       
   142 
       
   143     const char *startOfToken(const CPlusPlus::Token &token) const;
       
   144     const char *endOfToken(const CPlusPlus::Token &token) const;
       
   145 
       
   146     QByteArray tokenSpell(const CPlusPlus::Token &token) const;
       
   147     QByteArray tokenText(const CPlusPlus::Token &token) const; // does a deep copy
       
   148 
       
   149     void collectActualArguments(QVector<MacroArgumentReference> *actuals);
       
   150     MacroArgumentReference collectOneActualArgument();
       
   151 
       
   152     void processNewline(bool force = false);
       
   153 
       
   154     void processSkippingBlocks(bool skippingBlocks,
       
   155                                TokenIterator dot, TokenIterator lastToken);
       
   156 
       
   157     Macro *processObjectLikeMacro(TokenIterator identifierToken,
       
   158                                   const QByteArray &spell,
       
   159                                   Macro *m);
       
   160 
       
   161     void processDirective(TokenIterator dot, TokenIterator lastToken);
       
   162     void processInclude(bool skipCurrentPath,
       
   163                         TokenIterator dot, TokenIterator lastToken,
       
   164                         bool acceptMacros = true);
       
   165     void processDefine(TokenIterator dot, TokenIterator lastToken);
       
   166     void processIf(TokenIterator dot, TokenIterator lastToken);
       
   167     void processElse(TokenIterator dot, TokenIterator lastToken);
       
   168     void processElif(TokenIterator dot, TokenIterator lastToken);
       
   169     void processEndif(TokenIterator dot, TokenIterator lastToken);
       
   170     void processIfdef(bool checkUndefined,
       
   171                       TokenIterator dot, TokenIterator lastToken);
       
   172     void processUndef(TokenIterator dot, TokenIterator lastToken);
       
   173 
       
   174     bool isQtReservedWord(const QByteArray &name) const;
       
   175 
       
   176     State state() const;
       
   177     void pushState(const State &state);
       
   178     void popState();
       
   179 
       
   180     State createStateFromSource(const QByteArray &source) const;
       
   181 
       
   182     void out(const QByteArray &text);
       
   183     void out(char ch);
       
   184     void out(const char *s);
       
   185 
       
   186     QString string(const char *first, int len) const;
       
   187 
       
   188 private:
       
   189     Client *client;
       
   190     Environment *env;
       
   191     MacroExpander _expand;
       
   192 
       
   193     QBitArray _skipping; // ### move in state
       
   194     QBitArray _trueTest; // ### move in state
       
   195     int iflevel; // ### move in state
       
   196 
       
   197     QList<State> _savedStates;
       
   198 
       
   199     QByteArray _source;
       
   200     QVector<CPlusPlus::Token> _tokens;
       
   201     TokenIterator _dot;
       
   202 
       
   203     QByteArray *_result;
       
   204     bool _markGeneratedTokens;
       
   205 
       
   206     QString _originalSource;
       
   207     bool _expandMacros;
       
   208 };
       
   209 
       
   210 } // namespace CPlusPlus
       
   211 
       
   212 #endif // CPLUSPLUS_PP_ENGINE_H