CppDocument.h 11.3 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2013 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
    void addMacroUse(const Macro &macro, unsigned offset, unsigned length,
81
82
                     unsigned beginLine,
                     const QVector<MacroArgumentReference> &range);
Christian Kamm's avatar
Christian Kamm committed
83
    void addUndefinedMacroUse(const QByteArray &name, unsigned offset);
84

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

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

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

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

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

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

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

con's avatar
con committed
107
108
109
    void startSkippingBlocks(unsigned offset);
    void stopSkippingBlocks(unsigned offset);

110
111
112
113
    enum ParseMode { // ### keep in sync with CPlusPlus::TranslationUnit
        ParseTranlationUnit,
        ParseDeclaration,
        ParseExpression,
114
        ParseDeclarator,
115
116
117
        ParseStatement
    };

118
119
120
121
    bool isTokenized() const;
    void tokenize();

    bool isParsed() const;
122
    bool parse(ParseMode mode = ParseTranlationUnit);
123

124
    enum CheckMode {
125
        Unchecked,
126
127
128
129
130
        FullCheck,
        FastCheck
    };

    void check(CheckMode mode = FullCheck);
131

con's avatar
con committed
132
133
    static Ptr create(const QString &fileName);

Leandro Melo's avatar
Leandro Melo committed
134
    class CPLUSPLUS_EXPORT DiagnosticMessage
con's avatar
con committed
135
136
137
138
139
140
141
142
143
144
    {
    public:
        enum Level {
            Warning,
            Error,
            Fatal
        };

    public:
        DiagnosticMessage(int level, const QString &fileName,
145
146
147
                          unsigned line, unsigned column,
                          const QString &text,
                          unsigned length = 0)
con's avatar
con committed
148
149
            : _level(level),
              _line(line),
150
              _fileName(fileName),
con's avatar
con committed
151
              _column(column),
152
              _length(length),
con's avatar
con committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
              _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; }

171
        unsigned line() const
con's avatar
con committed
172
173
        { return _line; }

174
        unsigned column() const
con's avatar
con committed
175
176
        { return _column; }

177
178
179
        unsigned length() const
        { return _length; }

con's avatar
con committed
180
181
182
        QString text() const
        { return _text; }

183
184
185
        bool operator==(const DiagnosticMessage &other) const;
        bool operator!=(const DiagnosticMessage &other) const;

con's avatar
con committed
186
187
    private:
        int _level;
188
        unsigned _line;
189
        QString _fileName;
190
        unsigned _column;
191
        unsigned _length;
con's avatar
con committed
192
193
194
195
196
197
        QString _text;
    };

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

198
199
200
    void clearDiagnosticMessages()
    { _diagnosticMessages.clear(); }

con's avatar
con committed
201
202
203
204
205
206
207
208
209
210
211
212
213
    QList<DiagnosticMessage> diagnosticMessages() const
    { return _diagnosticMessages; }

    class Block
    {
        unsigned _begin;
        unsigned _end;

    public:
        inline Block(unsigned begin = 0, unsigned end = 0)
            : _begin(begin), _end(end)
        { }

214
215
216
217
218
219
220
221
222
        inline bool isNull() const
        { return length() == 0; }

        inline unsigned position() const
        { return _begin; }

        inline unsigned length() const
        { return _end - _begin; }

con's avatar
con committed
223
224
225
226
227
        inline unsigned begin() const
        { return _begin; }

        inline unsigned end() const
        { return _end; }
Roberto Raggi's avatar
Roberto Raggi committed
228
229
230
231
232

        bool contains(unsigned pos) const
        { return pos >= _begin && pos < _end; }
    };

233
    class Include {
234
235
        QString _resolvedFileName;
        QString _unresolvedFileName;
236
        unsigned _line;
237
        Client::IncludeType _type;
238
239

    public:
240
241
242
243
244
245
        Include(const QString &unresolvedFileName, const QString &resolvedFileName, unsigned line,
                Client::IncludeType type)
            : _resolvedFileName(resolvedFileName)
            , _unresolvedFileName(unresolvedFileName)
            , _line(line)
            , _type(type)
246
247
        { }

248
249
250
251
252
        QString resolvedFileName() const
        { return _resolvedFileName; }

        QString unresolvedFileName() const
        { return _unresolvedFileName; }
253
254
255

        unsigned line() const
        { return _line; }
256

257
258
        Client::IncludeType type() const
        { return _type; }
259
260
    };

Roberto Raggi's avatar
Roberto Raggi committed
261
262
    class MacroUse: public Block {
        Macro _macro;
263
        QVector<Block> _arguments;
264
        unsigned _beginLine;
Roberto Raggi's avatar
Roberto Raggi committed
265
266

    public:
267
        inline MacroUse(const Macro &macro, unsigned begin, unsigned end, unsigned beginLine)
Roberto Raggi's avatar
Roberto Raggi committed
268
            : Block(begin, end),
Christian Kamm's avatar
Christian Kamm committed
269
              _macro(macro),
270
              _beginLine(beginLine)
Roberto Raggi's avatar
Roberto Raggi committed
271
272
273
274
        { }

        const Macro &macro() const
        { return _macro; }
275
276
277
278
279
280
281

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

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

282
283
284
        unsigned beginLine() const
        { return _beginLine; }

Christian Kamm's avatar
Christian Kamm committed
285
    private:
286
287
288
289
290
        void setArguments(const QVector<Block> &arguments)
        { _arguments = arguments; }

        void addArgument(const Block &block)
        { _arguments.append(block); }
Christian Kamm's avatar
Christian Kamm committed
291
292
293
294
295
296
297
298
299
300

        friend class Document;
    };

    class UndefinedMacroUse: public Block {
        QByteArray _name;

    public:
        inline UndefinedMacroUse(
                const QByteArray &name,
301
                unsigned begin)
Christian Kamm's avatar
Christian Kamm committed
302
303
304
305
306
307
308
309
            : Block(begin, begin + name.length()),
              _name(name)
        { }

        QByteArray name() const
        {
            return _name;
        }
con's avatar
con committed
310
311
    };

312
313
314
    QStringList includedFiles() const;
    void addIncludeFile(const Include &include);

315
316
317
318
319
    QList<Include> resolvedIncludes() const
    { return _resolvedIncludes; }

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

con's avatar
con committed
321
322
323
    QList<Block> skippedBlocks() const
    { return _skippedBlocks; }

Roberto Raggi's avatar
Roberto Raggi committed
324
    QList<MacroUse> macroUses() const
325
326
    { return _macroUses; }

Christian Kamm's avatar
Christian Kamm committed
327
328
329
    QList<UndefinedMacroUse> undefinedMacroUses() const
    { return _undefinedMacroUses; }

330
331
332
333
334
    void setIncludeGuardMacroName(const QByteArray &includeGuardMacroName)
    { _includeGuardMacroName = includeGuardMacroName; }
    QByteArray includeGuardMacroName() const
    { return _includeGuardMacroName; }

335
336
337
338
    const Macro *findMacroDefinitionAt(unsigned line) const;
    const MacroUse *findMacroUseAt(unsigned offset) const;
    const UndefinedMacroUse *findUndefinedMacroUseAt(unsigned offset) const;

339
340
    void keepSourceAndAST();
    void releaseSourceAndAST();
341

342
343
344
    CheckMode checkMode() const
    { return static_cast<CheckMode>(_checkMode); }

con's avatar
con committed
345
346
347
348
349
private:
    QString _fileName;
    Control *_control;
    TranslationUnit *_translationUnit;
    Namespace *_globalNamespace;
350
351

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

354
355
    QList<Include> _resolvedIncludes;
    QList<Include> _unresolvedIncludes;
Roberto Raggi's avatar
Roberto Raggi committed
356
    QList<Macro> _definedMacros;
con's avatar
con committed
357
    QList<Block> _skippedBlocks;
Roberto Raggi's avatar
Roberto Raggi committed
358
    QList<MacroUse> _macroUses;
Christian Kamm's avatar
Christian Kamm committed
359
    QList<UndefinedMacroUse> _undefinedMacroUses;
360
361
362
363

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

364
    QByteArray _source;
365
    QDateTime _lastModified;
366
    QAtomicInt _keepSourceAndASTCount;
367
    unsigned _revision;
368
    unsigned _editorRevision;
369
    quint8 _checkMode;
370
371

    friend class Snapshot;
con's avatar
con committed
372
373
};

374
class CPLUSPLUS_EXPORT Snapshot
375
{
376
    typedef QHash<QString, Document::Ptr> _Base;
377

378
public:
379
380
381
    Snapshot();
    ~Snapshot();

382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
    typedef _Base::const_iterator iterator;
    typedef _Base::const_iterator const_iterator;

    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;

399
400
    Snapshot simplified(Document::Ptr doc) const;

401
    Document::Ptr preprocessedDocument(const QByteArray &source,
402
                                       const QString &fileName) const;
403

404
    Document::Ptr documentFromSource(const QByteArray &preprocessedDocument,
405
406
                                     const QString &fileName) const;

407
408
    QSet<QString> allIncludesForDocument(const QString &fileName) const;

409
private:
410
    void allIncludesForDocument_helper(const QString &fileName, QSet<QString> &result) const;
411
412
413

private:
    _Base _documents;
414
415
};

416
} // namespace CPlusPlus
con's avatar
con committed
417

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