tools/icheck/parser/src/shared/cplusplus/TranslationUnit.cpp
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 // Copyright (c) 2008 Roberto Raggi <roberto.raggi@gmail.com>
       
    42 //
       
    43 // Permission is hereby granted, free of charge, to any person obtaining a copy
       
    44 // of this software and associated documentation files (the "Software"), to deal
       
    45 // in the Software without restriction, including without limitation the rights
       
    46 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
       
    47 // copies of the Software, and to permit persons to whom the Software is
       
    48 // furnished to do so, subject to the following conditions:
       
    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 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
       
    57 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
       
    58 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
       
    59 // THE SOFTWARE.
       
    60 
       
    61 #include "TranslationUnit.h"
       
    62 #include "Control.h"
       
    63 #include "Parser.h"
       
    64 #include "Lexer.h"
       
    65 #include "MemoryPool.h"
       
    66 #include "AST.h"
       
    67 #include "Literals.h"
       
    68 #include "DiagnosticClient.h"
       
    69 #include <stack>
       
    70 #include <cstdarg>
       
    71 #include <algorithm>
       
    72 
       
    73 using namespace CPlusPlus;
       
    74 
       
    75 TranslationUnit::TranslationUnit(Control *control, const StringLiteral *fileId)
       
    76     : _control(control),
       
    77       _fileId(fileId),
       
    78       _firstSourceChar(0),
       
    79       _lastSourceChar(0),
       
    80       _pool(0),
       
    81       _ast(0),
       
    82       _flags(0)
       
    83 {
       
    84     _tokens = new Array<Token, 8>();
       
    85     _previousTranslationUnit = control->switchTranslationUnit(this);
       
    86     _pool = new MemoryPool();
       
    87 }
       
    88 
       
    89 TranslationUnit::~TranslationUnit()
       
    90 {
       
    91     (void) _control->switchTranslationUnit(_previousTranslationUnit);
       
    92     delete _tokens;
       
    93     delete _pool;
       
    94 }
       
    95 
       
    96 bool TranslationUnit::qtMocRunEnabled() const
       
    97 { return f._qtMocRunEnabled; }
       
    98 
       
    99 void TranslationUnit::setQtMocRunEnabled(bool onoff)
       
   100 { f._qtMocRunEnabled = onoff; }
       
   101 
       
   102 bool TranslationUnit::objCEnabled() const
       
   103 { return f._objCEnabled; }
       
   104 
       
   105 void TranslationUnit::setObjCEnabled(bool onoff)
       
   106 { f._objCEnabled = onoff; }
       
   107 
       
   108 Control *TranslationUnit::control() const
       
   109 { return _control; }
       
   110 
       
   111 const StringLiteral *TranslationUnit::fileId() const
       
   112 { return _fileId; }
       
   113 
       
   114 const char *TranslationUnit::fileName() const
       
   115 { return _fileId->chars(); }
       
   116 
       
   117 unsigned TranslationUnit::fileNameLength() const
       
   118 { return _fileId->size(); }
       
   119 
       
   120 const char *TranslationUnit::firstSourceChar() const
       
   121 { return _firstSourceChar; }
       
   122 
       
   123 const char *TranslationUnit::lastSourceChar() const
       
   124 { return _lastSourceChar; }
       
   125 
       
   126 unsigned TranslationUnit::sourceLength() const
       
   127 { return _lastSourceChar - _firstSourceChar; }
       
   128 
       
   129 void TranslationUnit::setSource(const char *source, unsigned size)
       
   130 {
       
   131     _firstSourceChar = source;
       
   132     _lastSourceChar = source + size;
       
   133 }
       
   134 
       
   135 unsigned TranslationUnit::tokenCount() const
       
   136 { return _tokens->size(); }
       
   137 
       
   138 const Token &TranslationUnit::tokenAt(unsigned index) const
       
   139 { return _tokens->at(index); }
       
   140 
       
   141 int TranslationUnit::tokenKind(unsigned index) const
       
   142 { return _tokens->at(index).f.kind; }
       
   143 
       
   144 const char *TranslationUnit::spell(unsigned index) const
       
   145 {
       
   146     if (! index)
       
   147         return 0;
       
   148 
       
   149     return _tokens->at(index).spell();
       
   150 }
       
   151 
       
   152 const Identifier *TranslationUnit::identifier(unsigned index) const
       
   153 { return _tokens->at(index).identifier; }
       
   154 
       
   155 const Literal *TranslationUnit::literal(unsigned index) const
       
   156 { return _tokens->at(index).literal; }
       
   157 
       
   158 const StringLiteral *TranslationUnit::stringLiteral(unsigned index) const
       
   159 { return _tokens->at(index).string; }
       
   160 
       
   161 const NumericLiteral *TranslationUnit::numericLiteral(unsigned index) const
       
   162 { return _tokens->at(index).number; }
       
   163 
       
   164 unsigned TranslationUnit::matchingBrace(unsigned index) const
       
   165 { return _tokens->at(index).close_brace; }
       
   166 
       
   167 MemoryPool *TranslationUnit::memoryPool() const
       
   168 { return _pool; }
       
   169 
       
   170 AST *TranslationUnit::ast() const
       
   171 { return _ast; }
       
   172 
       
   173 bool TranslationUnit::isTokenized() const
       
   174 { return f._tokenized; }
       
   175 
       
   176 bool TranslationUnit::isParsed() const
       
   177 { return f._parsed; }
       
   178 
       
   179 void TranslationUnit::tokenize()
       
   180 {
       
   181     if (isTokenized())
       
   182         return;
       
   183 
       
   184     f._tokenized = true;
       
   185 
       
   186     Lexer lex(this);
       
   187     lex.setQtMocRunEnabled(f._qtMocRunEnabled);
       
   188     lex.setObjCEnabled(f._objCEnabled);
       
   189 
       
   190     std::stack<unsigned> braces;
       
   191     _tokens->push_back(Token()); // the first token needs to be invalid!
       
   192 
       
   193     pushLineOffset(0);
       
   194     pushPreprocessorLine(0, 1, fileId());
       
   195 
       
   196     const Identifier *lineId   = control()->findOrInsertIdentifier("line");
       
   197     const Identifier *genId    = control()->findOrInsertIdentifier("gen");
       
   198 
       
   199     bool generated = false;
       
   200     Token tk;
       
   201     do {
       
   202         lex(&tk);
       
   203 
       
   204       _Lrecognize:
       
   205         if (tk.is(T_POUND)) {
       
   206             unsigned offset = tk.offset;
       
   207             lex(&tk);
       
   208 
       
   209             if (! tk.f.newline && tk.is(T_IDENTIFIER) && tk.identifier == genId) {
       
   210                 // it's a gen directive.
       
   211                 lex(&tk);
       
   212 
       
   213                 if (! tk.f.newline && tk.is(T_TRUE)) {
       
   214                     lex(&tk);
       
   215                     generated = true;
       
   216                 } else {
       
   217                     generated = false;
       
   218                 }
       
   219             } else {
       
   220                 if (! tk.f.newline && tk.is(T_IDENTIFIER) && tk.identifier == lineId)
       
   221                     lex(&tk);
       
   222                 if (! tk.f.newline && tk.is(T_NUMERIC_LITERAL)) {
       
   223                     unsigned line = (unsigned) strtoul(tk.spell(), 0, 0);
       
   224                     lex(&tk);
       
   225                     if (! tk.f.newline && tk.is(T_STRING_LITERAL)) {
       
   226                         const StringLiteral *fileName = control()->findOrInsertStringLiteral(tk.string->chars(),
       
   227                                                                                              tk.string->size());
       
   228                         pushPreprocessorLine(offset, line, fileName);
       
   229                         lex(&tk);
       
   230                     }
       
   231                 }
       
   232             }
       
   233             while (tk.isNot(T_EOF_SYMBOL) && ! tk.f.newline)
       
   234                 lex(&tk);
       
   235             goto _Lrecognize;
       
   236         } else if (tk.f.kind == T_LBRACE) {
       
   237             braces.push(_tokens->size());
       
   238         } else if (tk.f.kind == T_RBRACE && ! braces.empty()) {
       
   239             const unsigned open_brace_index = braces.top();
       
   240             braces.pop();
       
   241             (*_tokens)[open_brace_index].close_brace = _tokens->size();
       
   242         }
       
   243         tk.f.generated = generated;
       
   244         _tokens->push_back(tk);
       
   245     } while (tk.f.kind);
       
   246 
       
   247     for (; ! braces.empty(); braces.pop()) {
       
   248         unsigned open_brace_index = braces.top();
       
   249         (*_tokens)[open_brace_index].close_brace = _tokens->size();
       
   250     }
       
   251 }
       
   252 
       
   253 bool TranslationUnit::skipFunctionBody() const
       
   254 { return f._skipFunctionBody; }
       
   255 
       
   256 void TranslationUnit::setSkipFunctionBody(bool skipFunctionBody)
       
   257 { f._skipFunctionBody = skipFunctionBody; }
       
   258 
       
   259 bool TranslationUnit::parse(ParseMode mode)
       
   260 {
       
   261     if (isParsed())
       
   262         return false;
       
   263 
       
   264     if (! isTokenized())
       
   265         tokenize();
       
   266 
       
   267     f._parsed = true;
       
   268 
       
   269     Parser parser(this);
       
   270     parser.setQtMocRunEnabled(f._qtMocRunEnabled);
       
   271     parser.setObjCEnabled(f._objCEnabled);
       
   272 
       
   273     bool parsed = false;
       
   274 
       
   275     switch (mode) {
       
   276     case ParseTranlationUnit: {
       
   277         TranslationUnitAST *node = 0;
       
   278         parsed = parser.parseTranslationUnit(node);
       
   279         _ast = node;
       
   280     } break;
       
   281 
       
   282     case ParseDeclaration: {
       
   283         DeclarationAST *node = 0;
       
   284         parsed = parser.parseDeclaration(node);
       
   285         _ast = node;
       
   286     } break;
       
   287 
       
   288     case ParseExpression: {
       
   289         ExpressionAST *node = 0;
       
   290         parsed = parser.parseExpression(node);
       
   291         _ast = node;
       
   292     } break;
       
   293 
       
   294     case ParseDeclarator: {
       
   295         DeclaratorAST *node = 0;
       
   296         parsed = parser.parseDeclarator(node);
       
   297         _ast = node;
       
   298     } break;
       
   299 
       
   300     case ParseStatement: {
       
   301         StatementAST *node = 0;
       
   302         parsed = parser.parseStatement(node);
       
   303         _ast = node;
       
   304     } break;
       
   305 
       
   306     default:
       
   307         break;
       
   308     } // switch
       
   309 
       
   310     return parsed;
       
   311 }
       
   312 
       
   313 void TranslationUnit::pushLineOffset(unsigned offset)
       
   314 { _lineOffsets.push_back(offset); }
       
   315 
       
   316 void TranslationUnit::pushPreprocessorLine(unsigned offset,
       
   317                                            unsigned line,
       
   318                                            const StringLiteral *fileName)
       
   319 { _ppLines.push_back(PPLine(offset, line, fileName)); }
       
   320 
       
   321 unsigned TranslationUnit::findLineNumber(unsigned offset) const
       
   322 {
       
   323     std::vector<unsigned>::const_iterator it =
       
   324         std::lower_bound(_lineOffsets.begin(), _lineOffsets.end(), offset);
       
   325 
       
   326     if (it != _lineOffsets.begin())
       
   327         --it;
       
   328 
       
   329     return it - _lineOffsets.begin();
       
   330 }
       
   331 
       
   332 TranslationUnit::PPLine TranslationUnit::findPreprocessorLine(unsigned offset) const
       
   333 {
       
   334     std::vector<PPLine>::const_iterator it =
       
   335         std::lower_bound(_ppLines.begin(), _ppLines.end(), PPLine(offset));
       
   336 
       
   337     if (it != _ppLines.begin())
       
   338         --it;
       
   339 
       
   340     return *it;
       
   341 }
       
   342 
       
   343 unsigned TranslationUnit::findColumnNumber(unsigned offset, unsigned lineNumber) const
       
   344 {
       
   345     if (! offset)
       
   346         return 0;
       
   347 
       
   348     return offset - _lineOffsets[lineNumber];
       
   349 }
       
   350 
       
   351 void TranslationUnit::getTokenPosition(unsigned index,
       
   352                                        unsigned *line,
       
   353                                        unsigned *column,
       
   354                                        const StringLiteral **fileName) const
       
   355 { return getPosition(tokenAt(index).offset, line, column, fileName); }
       
   356 
       
   357 void TranslationUnit::getTokenStartPosition(unsigned index, unsigned *line,
       
   358                                             unsigned *column,
       
   359                                             const StringLiteral **fileName) const
       
   360 { return getPosition(tokenAt(index).begin(), line, column, fileName); }
       
   361 
       
   362 void TranslationUnit::getTokenEndPosition(unsigned index, unsigned *line,
       
   363                                           unsigned *column,
       
   364                                           const StringLiteral **fileName) const
       
   365 { return getPosition(tokenAt(index).end(), line, column, fileName); }
       
   366 
       
   367 void TranslationUnit::getPosition(unsigned tokenOffset,
       
   368                                   unsigned *line,
       
   369                                   unsigned *column,
       
   370                                   const StringLiteral **fileName) const
       
   371 {
       
   372     unsigned lineNumber = findLineNumber(tokenOffset);
       
   373     unsigned columnNumber = findColumnNumber(tokenOffset, lineNumber);
       
   374     const PPLine ppLine = findPreprocessorLine(tokenOffset);
       
   375 
       
   376     lineNumber -= findLineNumber(ppLine.offset) + 1;
       
   377     lineNumber += ppLine.line;
       
   378 
       
   379     if (line)
       
   380         *line = lineNumber;
       
   381 
       
   382     if (column)
       
   383         *column = columnNumber;
       
   384 
       
   385     if (fileName)
       
   386        *fileName = ppLine.fileName;
       
   387 }
       
   388 
       
   389 bool TranslationUnit::blockErrors(bool block)
       
   390 {
       
   391     bool previous = f._blockErrors;
       
   392     f._blockErrors = block;
       
   393     return previous;
       
   394 }
       
   395 
       
   396 void TranslationUnit::warning(unsigned index, const char *format, ...)
       
   397 {
       
   398     if (f._blockErrors)
       
   399         return;
       
   400 
       
   401     index = std::min(index, tokenCount() - 1);
       
   402 
       
   403     unsigned line = 0, column = 0;
       
   404     const StringLiteral *fileName = 0;
       
   405     getTokenPosition(index, &line, &column, &fileName);
       
   406 
       
   407     if (DiagnosticClient *client = control()->diagnosticClient()) {
       
   408         va_list args;
       
   409         va_start(args, format);
       
   410         client->report(DiagnosticClient::Warning, fileName, line, column,
       
   411                        format, args);
       
   412         va_end(args);
       
   413     } else {
       
   414         fprintf(stderr, "%s:%d: ", fileName->chars(), line);
       
   415         fprintf(stderr, "warning: ");
       
   416 
       
   417         va_list args;
       
   418         va_start(args, format);
       
   419         vfprintf(stderr, format, args);
       
   420         va_end(args);
       
   421         fputc('\n', stderr);
       
   422 
       
   423         showErrorLine(index, column, stderr);
       
   424     }
       
   425 }
       
   426 
       
   427 void TranslationUnit::error(unsigned index, const char *format, ...)
       
   428 {
       
   429     if (f._blockErrors)
       
   430         return;
       
   431 
       
   432     index = std::min(index, tokenCount() - 1);
       
   433 
       
   434     unsigned line = 0, column = 0;
       
   435     const StringLiteral *fileName = 0;
       
   436     getTokenPosition(index, &line, &column, &fileName);
       
   437 
       
   438     if (DiagnosticClient *client = control()->diagnosticClient()) {
       
   439         va_list args;
       
   440         va_start(args, format);
       
   441         client->report(DiagnosticClient::Error, fileName, line, column,
       
   442                        format, args);
       
   443         va_end(args);
       
   444     } else {
       
   445         fprintf(stderr, "%s:%d: ", fileName->chars(), line);
       
   446         fprintf(stderr, "error: ");
       
   447 
       
   448         va_list args;
       
   449         va_start(args, format);
       
   450         vfprintf(stderr, format, args);
       
   451         va_end(args);
       
   452         fputc('\n', stderr);
       
   453 
       
   454         showErrorLine(index, column, stderr);
       
   455     }
       
   456 }
       
   457 
       
   458 void TranslationUnit::fatal(unsigned index, const char *format, ...)
       
   459 {
       
   460     if (f._blockErrors)
       
   461         return;
       
   462 
       
   463     index = std::min(index, tokenCount() - 1);
       
   464 
       
   465     unsigned line = 0, column = 0;
       
   466     const StringLiteral *fileName = 0;
       
   467     getTokenPosition(index, &line, &column, &fileName);
       
   468 
       
   469     if (DiagnosticClient *client = control()->diagnosticClient()) {
       
   470         va_list args;
       
   471         va_start(args, format);
       
   472         client->report(DiagnosticClient::Fatal, fileName, line, column,
       
   473                        format, args);
       
   474         va_end(args);
       
   475     } else {
       
   476         fprintf(stderr, "%s:%d: ", fileName->chars(), line);
       
   477         fprintf(stderr, "fatal: ");
       
   478 
       
   479         va_list args;
       
   480         va_start(args, format);
       
   481         vfprintf(stderr, format, args);
       
   482         va_end(args);
       
   483         fputc('\n', stderr);
       
   484 
       
   485         showErrorLine(index, column, stderr);
       
   486     }
       
   487 
       
   488     exit(EXIT_FAILURE);
       
   489 }
       
   490 
       
   491 unsigned TranslationUnit::findPreviousLineOffset(unsigned tokenIndex) const
       
   492 {
       
   493     unsigned lineOffset = _lineOffsets[findLineNumber(_tokens->at(tokenIndex).offset)];
       
   494     return lineOffset;
       
   495 }
       
   496 
       
   497 void TranslationUnit::showErrorLine(unsigned index, unsigned column, FILE *out)
       
   498 {
       
   499     unsigned lineOffset = _lineOffsets[findLineNumber(_tokens->at(index).offset)];
       
   500     for (const char *cp = _firstSourceChar + lineOffset + 1; *cp && *cp != '\n'; ++cp) {
       
   501         fputc(*cp, out);
       
   502     }
       
   503     fputc('\n', out);
       
   504 
       
   505     const char *end = _firstSourceChar + lineOffset + 1 + column - 1;
       
   506     for (const char *cp = _firstSourceChar + lineOffset + 1; cp != end; ++cp) {
       
   507         if (*cp != '\t')
       
   508             fputc(' ', out);
       
   509         else
       
   510             fputc('\t', out);
       
   511     }
       
   512     fputc('^', out);
       
   513     fputc('\n', out);
       
   514 }
       
   515 
       
   516 void TranslationUnit::resetAST()
       
   517 {
       
   518     delete _pool;
       
   519     _pool = 0;
       
   520     _ast = 0;
       
   521 }
       
   522 
       
   523 void TranslationUnit::release()
       
   524 {
       
   525     resetAST();
       
   526     delete _tokens;
       
   527     _tokens = 0;
       
   528 }
       
   529 
       
   530