CppDocument.h 12.3 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22
** Alternatively, 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.
23
**
hjk's avatar
hjk committed
24 25
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

Roberto Raggi's avatar
Roberto Raggi committed
30 31
#ifndef CPLUSPLUS_CPPDOCUMENT_H
#define CPLUSPLUS_CPPDOCUMENT_H
con's avatar
con committed
32

33
#include "Macro.h"
Roberto Raggi's avatar
Roberto Raggi committed
34

35
#include <cplusplus/CPlusPlusForwardDeclarations.h>
36
#include <cplusplus/PreprocessorClient.h>
37

38 39 40 41 42
#include <QSharedPointer>
#include <QDateTime>
#include <QHash>
#include <QFileInfo>
#include <QAtomicInt>
con's avatar
con committed
43

44 45 46 47 48
// in debug mode: make dumpers widely available without an extra include
#ifdef QT_DEBUG
#include "Dumpers.h"
#endif

con's avatar
con committed
49 50
namespace CPlusPlus {

Roberto Raggi's avatar
Roberto Raggi committed
51
class Macro;
52
class MacroArgumentReference;
53
class LookupContext;
Roberto Raggi's avatar
Roberto Raggi committed
54

con's avatar
con committed
55 56 57 58 59 60 61 62 63 64 65 66 67
class CPLUSPLUS_EXPORT Document
{
    Document(const Document &other);
    void operator =(const Document &other);

    Document(const QString &fileName);

public:
    typedef QSharedPointer<Document> Ptr;

public:
    ~Document();

68 69 70
    unsigned revision() const;
    void setRevision(unsigned revision);

71 72 73
    unsigned editorRevision() const;
    void setEditorRevision(unsigned editorRevision);

74 75 76
    QDateTime lastModified() const;
    void setLastModified(const QDateTime &lastModified);

con's avatar
con committed
77 78
    QString fileName() const;

Roberto Raggi's avatar
Roberto Raggi committed
79
    void appendMacro(const Macro &macro);
80 81 82 83 84 85
    void addMacroUse(const Macro &macro,
                     unsigned bytesOffset, unsigned bytesLength,
                     unsigned utf16charsOffset, unsigned utf16charLength,
                     unsigned beginLine, const QVector<MacroArgumentReference> &range);
    void addUndefinedMacroUse(const QByteArray &name,
                              unsigned bytesOffset, unsigned utf16charsOffset);
86

con's avatar
con committed
87 88 89 90 91 92 93 94
    Control *control() const;
    TranslationUnit *translationUnit() const;

    bool skipFunctionBody() const;
    void setSkipFunctionBody(bool skipFunctionBody);

    unsigned globalSymbolCount() const;
    Symbol *globalSymbolAt(unsigned index) const;
95

con's avatar
con committed
96
    Namespace *globalNamespace() const;
97
    void setGlobalNamespace(Namespace *globalNamespace); // ### internal
con's avatar
con committed
98

Roberto Raggi's avatar
Roberto Raggi committed
99 100 101
    QList<Macro> definedMacros() const
    { return _definedMacros; }

102
    QString functionAt(int line, int column) const;
103 104
    Symbol *lastVisibleSymbolAt(unsigned line, unsigned column = 0) const;
    Scope *scopeAt(unsigned line, unsigned column = 0);
con's avatar
con committed
105

106 107
    QByteArray utf8Source() const;
    void setUtf8Source(const QByteArray &utf8Source);
Roberto Raggi's avatar
Roberto Raggi committed
108

109 110 111 112
    QByteArray fingerprint() const { return m_fingerprint; }
    void setFingerprint(const QByteArray &fingerprint)
    { m_fingerprint = fingerprint; }

113 114
    void startSkippingBlocks(unsigned utf16charsOffset);
    void stopSkippingBlocks(unsigned utf16charsOffset);
con's avatar
con committed
115

116 117 118 119
    enum ParseMode { // ### keep in sync with CPlusPlus::TranslationUnit
        ParseTranlationUnit,
        ParseDeclaration,
        ParseExpression,
120
        ParseDeclarator,
121 122 123
        ParseStatement
    };

124 125 126 127
    bool isTokenized() const;
    void tokenize();

    bool isParsed() const;
128
    bool parse(ParseMode mode = ParseTranlationUnit);
129

130
    enum CheckMode {
131
        Unchecked,
132 133 134 135 136
        FullCheck,
        FastCheck
    };

    void check(CheckMode mode = FullCheck);
137

con's avatar
con committed
138 139
    static Ptr create(const QString &fileName);

Leandro Melo's avatar
Leandro Melo committed
140
    class CPLUSPLUS_EXPORT DiagnosticMessage
con's avatar
con committed
141 142 143 144 145 146 147 148 149 150
    {
    public:
        enum Level {
            Warning,
            Error,
            Fatal
        };

    public:
        DiagnosticMessage(int level, const QString &fileName,
151 152 153
                          unsigned line, unsigned column,
                          const QString &text,
                          unsigned length = 0)
con's avatar
con committed
154 155
            : _level(level),
              _line(line),
156
              _fileName(fileName),
con's avatar
con committed
157
              _column(column),
158
              _length(length),
con's avatar
con committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
              _text(text)
        { }

        int level() const
        { return _level; }

        bool isWarning() const
        { return _level == Warning; }

        bool isError() const
        { return _level == Error; }

        bool isFatal() const
        { return _level == Fatal; }

        QString fileName() const
        { return _fileName; }

177
        unsigned line() const
con's avatar
con committed
178 179
        { return _line; }

180
        unsigned column() const
con's avatar
con committed
181 182
        { return _column; }

183 184 185
        unsigned length() const
        { return _length; }

con's avatar
con committed
186 187 188
        QString text() const
        { return _text; }

189 190 191
        bool operator==(const DiagnosticMessage &other) const;
        bool operator!=(const DiagnosticMessage &other) const;

con's avatar
con committed
192 193
    private:
        int _level;
194
        unsigned _line;
195
        QString _fileName;
196
        unsigned _column;
197
        unsigned _length;
con's avatar
con committed
198 199 200 201 202 203
        QString _text;
    };

    void addDiagnosticMessage(const DiagnosticMessage &d)
    { _diagnosticMessages.append(d); }

204 205 206
    void clearDiagnosticMessages()
    { _diagnosticMessages.clear(); }

con's avatar
con committed
207 208 209 210 211
    QList<DiagnosticMessage> diagnosticMessages() const
    { return _diagnosticMessages; }

    class Block
    {
212 213 214 215
        unsigned _bytesBegin;
        unsigned _bytesEnd;
        unsigned _utf16charsBegin;
        unsigned _utf16charsEnd;
con's avatar
con committed
216 217

    public:
218 219 220 221 222 223 224 225 226 227 228 229 230
        inline Block(unsigned bytesBegin = 0, unsigned bytesEnd = 0,
                     unsigned utf16charsBegin = 0, unsigned utf16charsEnd = 0)
            : _bytesBegin(bytesBegin),
              _bytesEnd(bytesEnd),
              _utf16charsBegin(utf16charsBegin),
              _utf16charsEnd(utf16charsEnd)
        {}

        inline unsigned bytesBegin() const
        { return _bytesBegin; }

        inline unsigned bytesEnd() const
        { return _bytesEnd; }
con's avatar
con committed
231

232 233
        inline unsigned utf16charsBegin() const
        { return _utf16charsBegin; }
con's avatar
con committed
234

235 236
        inline unsigned utf16charsEnd() const
        { return _utf16charsEnd; }
Roberto Raggi's avatar
Roberto Raggi committed
237

238 239
        bool containsUtf16charOffset(unsigned utf16charOffset) const
        { return utf16charOffset >= _utf16charsBegin && utf16charOffset < _utf16charsEnd; }
Roberto Raggi's avatar
Roberto Raggi committed
240 241
    };

242
    class Include {
243 244
        QString _resolvedFileName;
        QString _unresolvedFileName;
245
        unsigned _line;
246
        Client::IncludeType _type;
247 248

    public:
249 250 251 252 253 254
        Include(const QString &unresolvedFileName, const QString &resolvedFileName, unsigned line,
                Client::IncludeType type)
            : _resolvedFileName(resolvedFileName)
            , _unresolvedFileName(unresolvedFileName)
            , _line(line)
            , _type(type)
255 256
        { }

257 258 259 260 261
        QString resolvedFileName() const
        { return _resolvedFileName; }

        QString unresolvedFileName() const
        { return _unresolvedFileName; }
262 263 264

        unsigned line() const
        { return _line; }
265

266 267
        Client::IncludeType type() const
        { return _type; }
268 269
    };

Roberto Raggi's avatar
Roberto Raggi committed
270 271
    class MacroUse: public Block {
        Macro _macro;
272
        QVector<Block> _arguments;
273
        unsigned _beginLine;
Roberto Raggi's avatar
Roberto Raggi committed
274 275

    public:
276 277 278 279 280
        inline MacroUse(const Macro &macro,
                        unsigned bytesBegin, unsigned bytesEnd,
                        unsigned utf16charsBegin, unsigned utf16charsEnd,
                        unsigned beginLine)
            : Block(bytesBegin, bytesEnd, utf16charsBegin, utf16charsEnd),
Christian Kamm's avatar
Christian Kamm committed
281
              _macro(macro),
282
              _beginLine(beginLine)
Roberto Raggi's avatar
Roberto Raggi committed
283 284 285 286
        { }

        const Macro &macro() const
        { return _macro; }
287 288 289 290 291 292 293

        bool isFunctionLike() const
        { return _macro.isFunctionLike(); }

        QVector<Block> arguments() const
        { return _arguments; }

294 295 296
        unsigned beginLine() const
        { return _beginLine; }

Christian Kamm's avatar
Christian Kamm committed
297
    private:
298 299 300 301 302
        void setArguments(const QVector<Block> &arguments)
        { _arguments = arguments; }

        void addArgument(const Block &block)
        { _arguments.append(block); }
Christian Kamm's avatar
Christian Kamm committed
303 304 305 306 307 308 309 310 311 312

        friend class Document;
    };

    class UndefinedMacroUse: public Block {
        QByteArray _name;

    public:
        inline UndefinedMacroUse(
                const QByteArray &name,
313 314 315 316 317 318
                unsigned bytesBegin,
                unsigned utf16charsBegin)
            : Block(bytesBegin,
                    bytesBegin + name.length(),
                    utf16charsBegin,
                    utf16charsBegin + QString::fromUtf8(name, name.size()).size()),
Christian Kamm's avatar
Christian Kamm committed
319 320 321 322 323 324 325
              _name(name)
        { }

        QByteArray name() const
        {
            return _name;
        }
con's avatar
con committed
326 327
    };

328 329 330
    QStringList includedFiles() const;
    void addIncludeFile(const Include &include);

331 332 333 334 335
    QList<Include> resolvedIncludes() const
    { return _resolvedIncludes; }

    QList<Include> unresolvedIncludes() const
    { return _unresolvedIncludes; }
336

con's avatar
con committed
337 338 339
    QList<Block> skippedBlocks() const
    { return _skippedBlocks; }

Roberto Raggi's avatar
Roberto Raggi committed
340
    QList<MacroUse> macroUses() const
341 342
    { return _macroUses; }

Christian Kamm's avatar
Christian Kamm committed
343 344 345
    QList<UndefinedMacroUse> undefinedMacroUses() const
    { return _undefinedMacroUses; }

346 347 348 349 350
    void setIncludeGuardMacroName(const QByteArray &includeGuardMacroName)
    { _includeGuardMacroName = includeGuardMacroName; }
    QByteArray includeGuardMacroName() const
    { return _includeGuardMacroName; }

351
    const Macro *findMacroDefinitionAt(unsigned line) const;
352 353
    const MacroUse *findMacroUseAt(unsigned utf16charsOffset) const;
    const UndefinedMacroUse *findUndefinedMacroUseAt(unsigned utf16charsOffset) const;
354

355 356
    void keepSourceAndAST();
    void releaseSourceAndAST();
357

358 359 360
    CheckMode checkMode() const
    { return static_cast<CheckMode>(_checkMode); }

con's avatar
con committed
361 362 363 364 365
private:
    QString _fileName;
    Control *_control;
    TranslationUnit *_translationUnit;
    Namespace *_globalNamespace;
366 367

    /// All messages generated during lexical/syntactic/semantic analysis.
con's avatar
con committed
368
    QList<DiagnosticMessage> _diagnosticMessages;
369

370 371
    QList<Include> _resolvedIncludes;
    QList<Include> _unresolvedIncludes;
Roberto Raggi's avatar
Roberto Raggi committed
372
    QList<Macro> _definedMacros;
con's avatar
con committed
373
    QList<Block> _skippedBlocks;
Roberto Raggi's avatar
Roberto Raggi committed
374
    QList<MacroUse> _macroUses;
Christian Kamm's avatar
Christian Kamm committed
375
    QList<UndefinedMacroUse> _undefinedMacroUses;
376 377 378 379

     /// the macro name of the include guard, if there is one.
    QByteArray _includeGuardMacroName;

380 381
    QByteArray m_fingerprint;

382
    QByteArray _source;
383
    QDateTime _lastModified;
384
    QAtomicInt _keepSourceAndASTCount;
385
    unsigned _revision;
386
    unsigned _editorRevision;
387
    quint8 _checkMode;
388 389

    friend class Snapshot;
con's avatar
con committed
390 391
};

392
class CPLUSPLUS_EXPORT Snapshot
393
{
Erik Verbruggen's avatar
Erik Verbruggen committed
394
    typedef QHash<QString, Document::Ptr> Base;
395

396
public:
397 398 399
    Snapshot();
    ~Snapshot();

Erik Verbruggen's avatar
Erik Verbruggen committed
400 401
    typedef Base::const_iterator iterator;
    typedef Base::const_iterator const_iterator;
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416

    int size() const; // ### remove
    bool isEmpty() const;

    void insert(Document::Ptr doc); // ### remove
    void remove(const QString &fileName); // ### remove

    const_iterator begin() const { return _documents.begin(); }
    const_iterator end() const { return _documents.end(); }

    bool contains(const QString &fileName) const;
    Document::Ptr document(const QString &fileName) const;

    const_iterator find(const QString &fileName) const;

417 418
    Snapshot simplified(Document::Ptr doc) const;

419
    Document::Ptr preprocessedDocument(const QByteArray &source,
420
                                       const QString &fileName) const;
421

422
    Document::Ptr documentFromSource(const QByteArray &preprocessedDocument,
423 424
                                     const QString &fileName) const;

425 426
    QSet<QString> allIncludesForDocument(const QString &fileName) const;

427
private:
428
    void allIncludesForDocument_helper(const QString &fileName, QSet<QString> &result) const;
429 430

private:
Erik Verbruggen's avatar
Erik Verbruggen committed
431
    Base _documents;
432 433
};

434
} // namespace CPlusPlus
con's avatar
con committed
435

Roberto Raggi's avatar
Roberto Raggi committed
436
#endif // CPLUSPLUS_CPPDOCUMENT_H