tools/icheck/parser/src/libs/cplusplus/CppDocument.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 #ifndef CPPDOCUMENT_H
       
    43 #define CPPDOCUMENT_H
       
    44 
       
    45 #include <CPlusPlusForwardDeclarations.h>
       
    46 #include "Macro.h"
       
    47 
       
    48 #include <QtCore/QSharedPointer>
       
    49 #include <QtCore/QDateTime>
       
    50 #include <QtCore/QHash>
       
    51 #include <QtCore/QFileInfo>
       
    52 
       
    53 namespace CPlusPlus {
       
    54 
       
    55 class Macro;
       
    56 class MacroArgumentReference;
       
    57 class NamespaceBinding;
       
    58 
       
    59 class CPLUSPLUS_EXPORT Document
       
    60 {
       
    61     Document(const Document &other);
       
    62     void operator =(const Document &other);
       
    63 
       
    64     Document(const QString &fileName);
       
    65 
       
    66 public:
       
    67     typedef QSharedPointer<Document> Ptr;
       
    68 
       
    69 public:
       
    70     ~Document();
       
    71 
       
    72     unsigned revision() const;
       
    73     void setRevision(unsigned revision);
       
    74 
       
    75     unsigned editorRevision() const;
       
    76     void setEditorRevision(unsigned editorRevision);
       
    77 
       
    78     QDateTime lastModified() const;
       
    79     void setLastModified(const QDateTime &lastModified);
       
    80 
       
    81     QString fileName() const;
       
    82 
       
    83     QStringList includedFiles() const;
       
    84     void addIncludeFile(const QString &fileName, unsigned line);
       
    85 
       
    86     void appendMacro(const Macro &macro);
       
    87     void addMacroUse(const Macro &macro, unsigned offset, unsigned length,
       
    88                      unsigned beginLine, const QVector<MacroArgumentReference> &range,
       
    89                      bool inCondition);
       
    90     void addUndefinedMacroUse(const QByteArray &name, unsigned offset);
       
    91 
       
    92     Control *control() const;
       
    93     TranslationUnit *translationUnit() const;
       
    94 
       
    95     bool skipFunctionBody() const;
       
    96     void setSkipFunctionBody(bool skipFunctionBody);
       
    97 
       
    98     unsigned globalSymbolCount() const;
       
    99     Symbol *globalSymbolAt(unsigned index) const;
       
   100     Scope *globalSymbols() const; // ### deprecate?
       
   101     Namespace *globalNamespace() const;
       
   102 
       
   103     QList<Macro> definedMacros() const
       
   104     { return _definedMacros; }
       
   105 
       
   106     Symbol *findSymbolAt(unsigned line, unsigned column) const;
       
   107 
       
   108     QByteArray source() const;
       
   109     void setSource(const QByteArray &source);
       
   110 
       
   111     void startSkippingBlocks(unsigned offset);
       
   112     void stopSkippingBlocks(unsigned offset);
       
   113 
       
   114     enum ParseMode { // ### keep in sync with CPlusPlus::TranslationUnit
       
   115         ParseTranlationUnit,
       
   116         ParseDeclaration,
       
   117         ParseExpression,
       
   118         ParseDeclarator,
       
   119         ParseStatement
       
   120     };
       
   121 
       
   122     bool isTokenized() const;
       
   123     void tokenize();
       
   124 
       
   125     bool isParsed() const;
       
   126     bool parse(ParseMode mode = ParseTranlationUnit);
       
   127 
       
   128     enum CheckMode {
       
   129         FullCheck,
       
   130         FastCheck
       
   131     };
       
   132 
       
   133     void check(CheckMode mode = FullCheck);
       
   134 
       
   135     void releaseSource();
       
   136     void releaseTranslationUnit();
       
   137 
       
   138     static Ptr create(const QString &fileName);
       
   139 
       
   140     class DiagnosticMessage
       
   141     {
       
   142     public:
       
   143         enum Level {
       
   144             Warning,
       
   145             Error,
       
   146             Fatal
       
   147         };
       
   148 
       
   149     public:
       
   150         DiagnosticMessage(int level, const QString &fileName,
       
   151                           int line, int column,
       
   152                           const QString &text)
       
   153             : _level(level),
       
   154               _fileName(fileName),
       
   155               _line(line),
       
   156               _column(column),
       
   157               _text(text)
       
   158         { }
       
   159 
       
   160         int level() const
       
   161         { return _level; }
       
   162 
       
   163         bool isWarning() const
       
   164         { return _level == Warning; }
       
   165 
       
   166         bool isError() const
       
   167         { return _level == Error; }
       
   168 
       
   169         bool isFatal() const
       
   170         { return _level == Fatal; }
       
   171 
       
   172         QString fileName() const
       
   173         { return _fileName; }
       
   174 
       
   175         unsigned line() const
       
   176         { return _line; }
       
   177 
       
   178         unsigned column() const
       
   179         { return _column; }
       
   180 
       
   181         QString text() const
       
   182         { return _text; }
       
   183 
       
   184     private:
       
   185         int _level;
       
   186         QString _fileName;
       
   187         unsigned _line;
       
   188         unsigned _column;
       
   189         QString _text;
       
   190     };
       
   191 
       
   192     void addDiagnosticMessage(const DiagnosticMessage &d)
       
   193     { _diagnosticMessages.append(d); }
       
   194 
       
   195     QList<DiagnosticMessage> diagnosticMessages() const
       
   196     { return _diagnosticMessages; }
       
   197 
       
   198     class Block
       
   199     {
       
   200         unsigned _begin;
       
   201         unsigned _end;
       
   202 
       
   203     public:
       
   204         inline Block(unsigned begin = 0, unsigned end = 0)
       
   205             : _begin(begin), _end(end)
       
   206         { }
       
   207 
       
   208         inline bool isNull() const
       
   209         { return length() == 0; }
       
   210 
       
   211         inline unsigned position() const
       
   212         { return _begin; }
       
   213 
       
   214         inline unsigned length() const
       
   215         { return _end - _begin; }
       
   216 
       
   217         inline unsigned begin() const
       
   218         { return _begin; }
       
   219 
       
   220         inline unsigned end() const
       
   221         { return _end; }
       
   222 
       
   223         bool contains(unsigned pos) const
       
   224         { return pos >= _begin && pos < _end; }
       
   225     };
       
   226 
       
   227     class Include {
       
   228         QString _fileName;
       
   229         unsigned _line;
       
   230 
       
   231     public:
       
   232         Include(const QString &fileName, unsigned line)
       
   233             : _fileName(fileName), _line(line)
       
   234         { }
       
   235 
       
   236         QString fileName() const
       
   237         { return _fileName; }
       
   238 
       
   239         unsigned line() const
       
   240         { return _line; }
       
   241 
       
   242         bool resolved() const
       
   243         { return QFileInfo(_fileName).isAbsolute(); }
       
   244     };
       
   245 
       
   246     class MacroUse: public Block {
       
   247         Macro _macro;
       
   248         QVector<Block> _arguments;
       
   249         bool _inCondition;
       
   250         unsigned _beginLine;
       
   251 
       
   252     public:
       
   253         inline MacroUse(const Macro &macro,
       
   254                         unsigned begin, unsigned end, unsigned beginLine)
       
   255             : Block(begin, end),
       
   256               _macro(macro),
       
   257               _inCondition(false),
       
   258               _beginLine(beginLine)
       
   259         { }
       
   260 
       
   261         const Macro &macro() const
       
   262         { return _macro; }
       
   263 
       
   264         bool isFunctionLike() const
       
   265         { return _macro.isFunctionLike(); }
       
   266 
       
   267         QVector<Block> arguments() const
       
   268         { return _arguments; }
       
   269 
       
   270         bool isInCondition() const
       
   271         { return _inCondition; }
       
   272 
       
   273         unsigned beginLine() const
       
   274         { return _beginLine; }
       
   275 
       
   276     private:
       
   277         void setArguments(const QVector<Block> &arguments)
       
   278         { _arguments = arguments; }
       
   279 
       
   280         void addArgument(const Block &block)
       
   281         { _arguments.append(block); }
       
   282 
       
   283         void setInCondition(bool set)
       
   284         { _inCondition = set; }
       
   285 
       
   286         friend class Document;
       
   287     };
       
   288 
       
   289     class UndefinedMacroUse: public Block {
       
   290         QByteArray _name;
       
   291 
       
   292     public:
       
   293         inline UndefinedMacroUse(
       
   294                 const QByteArray &name,
       
   295                 unsigned begin)
       
   296             : Block(begin, begin + name.length()),
       
   297               _name(name)
       
   298         { }
       
   299 
       
   300         QByteArray name() const
       
   301         {
       
   302             return _name;
       
   303         }
       
   304     };
       
   305 
       
   306     QList<Include> includes() const
       
   307     { return _includes; }
       
   308 
       
   309     QList<Block> skippedBlocks() const
       
   310     { return _skippedBlocks; }
       
   311 
       
   312     QList<MacroUse> macroUses() const
       
   313     { return _macroUses; }
       
   314 
       
   315     QList<UndefinedMacroUse> undefinedMacroUses() const
       
   316     { return _undefinedMacroUses; }
       
   317 
       
   318     const Macro *findMacroDefinitionAt(unsigned line) const;
       
   319     const MacroUse *findMacroUseAt(unsigned offset) const;
       
   320     const UndefinedMacroUse *findUndefinedMacroUseAt(unsigned offset) const;
       
   321 
       
   322 private:
       
   323     Symbol *findSymbolAt(unsigned line, unsigned column, Scope *scope) const;
       
   324 
       
   325 private:
       
   326     QString _fileName;
       
   327     Control *_control;
       
   328     TranslationUnit *_translationUnit;
       
   329     Namespace *_globalNamespace;
       
   330     QList<DiagnosticMessage> _diagnosticMessages;
       
   331     QList<Include> _includes;
       
   332     QList<Macro> _definedMacros;
       
   333     QList<Block> _skippedBlocks;
       
   334     QList<MacroUse> _macroUses;
       
   335     QList<UndefinedMacroUse> _undefinedMacroUses;
       
   336     QByteArray _source;
       
   337     QDateTime _lastModified;
       
   338     unsigned _revision;
       
   339     unsigned _editorRevision;
       
   340 
       
   341     friend class Snapshot;
       
   342 };
       
   343 
       
   344 class CPLUSPLUS_EXPORT Snapshot
       
   345 {
       
   346     typedef QHash<QString, Document::Ptr> _Base;
       
   347 
       
   348 public:
       
   349     Snapshot();
       
   350     ~Snapshot();
       
   351 
       
   352     typedef _Base::const_iterator iterator;
       
   353     typedef _Base::const_iterator const_iterator;
       
   354 
       
   355     int size() const; // ### remove
       
   356     bool isEmpty() const;
       
   357 
       
   358     void insert(Document::Ptr doc); // ### remove
       
   359     void remove(const QString &fileName); // ### remove
       
   360 
       
   361     const_iterator begin() const { return _documents.begin(); }
       
   362     const_iterator end() const { return _documents.end(); }
       
   363 
       
   364     bool contains(const QString &fileName) const;
       
   365     Document::Ptr document(const QString &fileName) const;
       
   366     Document::Ptr operator[](const QString &fileName) const;
       
   367 
       
   368     const_iterator find(const QString &fileName) const;
       
   369 
       
   370     Snapshot simplified(Document::Ptr doc) const;
       
   371 
       
   372     QByteArray preprocessedCode(const QString &source,
       
   373                                 const QString &fileName) const;
       
   374 
       
   375     Document::Ptr documentFromSource(const QByteArray &preprocessedCode,
       
   376                                      const QString &fileName) const;
       
   377 
       
   378     QSharedPointer<NamespaceBinding> globalNamespaceBinding(Document::Ptr doc) const;
       
   379 
       
   380     QStringList filesDependingOn(const QString &fileName) const;
       
   381     QHash<QString, QStringList> dependencyTable() const;
       
   382 
       
   383 private:
       
   384     void simplified_helper(Document::Ptr doc, Snapshot *snapshot) const;
       
   385     void dependency_helper(QVector<QString> &files,
       
   386                            QHash<QString, int> &fileIndex,
       
   387                            QHash<int, QList<int> > &includes,
       
   388                            QVector<QBitArray> &includeMap) const;
       
   389 
       
   390 private:
       
   391     _Base _documents;
       
   392 };
       
   393 
       
   394 } // end of namespace CPlusPlus
       
   395 
       
   396 #endif // CPPDOCUMENT_H