CppDocument.h 10.7 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
36
#include <cplusplus/CPlusPlusForwardDeclarations.h>

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

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

con's avatar
con committed
48
49
namespace CPlusPlus {

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

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

    Document(const QString &fileName);

public:
    typedef QSharedPointer<Document> Ptr;

public:
    ~Document();

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

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

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

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

    QStringList includedFiles() const;
79
    void addIncludeFile(const QString &fileName, unsigned line);
con's avatar
con committed
80

Roberto Raggi's avatar
Roberto Raggi committed
81
    void appendMacro(const Macro &macro);
82
    void addMacroUse(const Macro &macro, unsigned offset, unsigned length,
83
84
                     unsigned beginLine,
                     const QVector<MacroArgumentReference> &range);
Christian Kamm's avatar
Christian Kamm committed
85
    void addUndefinedMacroUse(const QByteArray &name, unsigned offset);
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
103
    Symbol *lastVisibleSymbolAt(unsigned line, unsigned column = 0) const;
    Scope *scopeAt(unsigned line, unsigned column = 0);
con's avatar
con committed
104

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

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

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

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

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

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

    void check(CheckMode mode = FullCheck);
132

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

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

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

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

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

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

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

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

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

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

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

con's avatar
con committed
202
203
204
205
206
207
208
209
210
211
212
213
214
    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)
        { }

215
216
217
218
219
220
221
222
223
        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
224
225
226
227
228
        inline unsigned begin() const
        { return _begin; }

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

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

234
235
236
237
238
239
240
241
242
243
244
245
246
247
    class Include {
        QString _fileName;
        unsigned _line;

    public:
        Include(const QString &fileName, unsigned line)
            : _fileName(fileName), _line(line)
        { }

        QString fileName() const
        { return _fileName; }

        unsigned line() const
        { return _line; }
248
249
250

        bool resolved() const
        { return QFileInfo(_fileName).isAbsolute(); }
251
252
    };

Roberto Raggi's avatar
Roberto Raggi committed
253
254
    class MacroUse: public Block {
        Macro _macro;
255
        QVector<Block> _arguments;
256
        unsigned _beginLine;
Roberto Raggi's avatar
Roberto Raggi committed
257
258

    public:
259
        inline MacroUse(const Macro &macro, unsigned begin, unsigned end, unsigned beginLine)
Roberto Raggi's avatar
Roberto Raggi committed
260
            : Block(begin, end),
Christian Kamm's avatar
Christian Kamm committed
261
              _macro(macro),
262
              _beginLine(beginLine)
Roberto Raggi's avatar
Roberto Raggi committed
263
264
265
266
        { }

        const Macro &macro() const
        { return _macro; }
267
268
269
270
271
272
273

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

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

274
275
276
        unsigned beginLine() const
        { return _beginLine; }

Christian Kamm's avatar
Christian Kamm committed
277
    private:
278
279
280
281
282
        void setArguments(const QVector<Block> &arguments)
        { _arguments = arguments; }

        void addArgument(const Block &block)
        { _arguments.append(block); }
Christian Kamm's avatar
Christian Kamm committed
283
284
285
286
287
288
289
290
291
292

        friend class Document;
    };

    class UndefinedMacroUse: public Block {
        QByteArray _name;

    public:
        inline UndefinedMacroUse(
                const QByteArray &name,
293
                unsigned begin)
Christian Kamm's avatar
Christian Kamm committed
294
295
296
297
298
299
300
301
            : Block(begin, begin + name.length()),
              _name(name)
        { }

        QByteArray name() const
        {
            return _name;
        }
con's avatar
con committed
302
303
    };

304
305
306
    QList<Include> includes() const
    { return _includes; }

con's avatar
con committed
307
308
309
    QList<Block> skippedBlocks() const
    { return _skippedBlocks; }

Roberto Raggi's avatar
Roberto Raggi committed
310
    QList<MacroUse> macroUses() const
311
312
    { return _macroUses; }

Christian Kamm's avatar
Christian Kamm committed
313
314
315
    QList<UndefinedMacroUse> undefinedMacroUses() const
    { return _undefinedMacroUses; }

316
317
318
319
320
    void setIncludeGuardMacroName(const QByteArray &includeGuardMacroName)
    { _includeGuardMacroName = includeGuardMacroName; }
    QByteArray includeGuardMacroName() const
    { return _includeGuardMacroName; }

321
322
323
324
    const Macro *findMacroDefinitionAt(unsigned line) const;
    const MacroUse *findMacroUseAt(unsigned offset) const;
    const UndefinedMacroUse *findUndefinedMacroUseAt(unsigned offset) const;

325
326
    void keepSourceAndAST();
    void releaseSourceAndAST();
327

328
329
330
    CheckMode checkMode() const
    { return static_cast<CheckMode>(_checkMode); }

con's avatar
con committed
331
332
333
334
335
private:
    QString _fileName;
    Control *_control;
    TranslationUnit *_translationUnit;
    Namespace *_globalNamespace;
336
337

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

340
    QList<Include> _includes;
Roberto Raggi's avatar
Roberto Raggi committed
341
    QList<Macro> _definedMacros;
con's avatar
con committed
342
    QList<Block> _skippedBlocks;
Roberto Raggi's avatar
Roberto Raggi committed
343
    QList<MacroUse> _macroUses;
Christian Kamm's avatar
Christian Kamm committed
344
    QList<UndefinedMacroUse> _undefinedMacroUses;
345
346
347
348

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

349
    QByteArray _source;
350
    QDateTime _lastModified;
351
    QAtomicInt _keepSourceAndASTCount;
352
    unsigned _revision;
353
    unsigned _editorRevision;
354
    quint8 _checkMode;
355
356

    friend class Snapshot;
con's avatar
con committed
357
358
};

359
class CPLUSPLUS_EXPORT Snapshot
360
{
361
    typedef QHash<QString, Document::Ptr> _Base;
362

363
public:
364
365
366
    Snapshot();
    ~Snapshot();

367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
    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;

384
385
    Snapshot simplified(Document::Ptr doc) const;

386
387
    Document::Ptr preprocessedDocument(const QString &source,
                                       const QString &fileName) const;
388

389
    Document::Ptr documentFromSource(const QByteArray &preprocessedDocument,
390
391
                                     const QString &fileName) const;

392
393
    QSet<QString> allIncludesForDocument(const QString &fileName) const;

394
private:
395
    void allIncludesForDocument_helper(const QString &fileName, QSet<QString> &result) const;
396
397
398

private:
    _Base _documents;
399
400
};

401
} // namespace CPlusPlus
con's avatar
con committed
402

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