CppDocument.h 10 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** 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.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
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 <CPlusPlusForwardDeclarations.h>
34
#include "Macro.h"
Roberto Raggi's avatar
Roberto Raggi committed
35

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

namespace CPlusPlus {

Roberto Raggi's avatar
Roberto Raggi committed
43
class Macro;
44
class MacroArgumentReference;
Roberto Raggi's avatar
Roberto Raggi committed
45

con's avatar
con committed
46
47
48
49
50
51
52
53
54
55
56
57
58
class CPLUSPLUS_EXPORT Document
{
    Document(const Document &other);
    void operator =(const Document &other);

    Document(const QString &fileName);

public:
    typedef QSharedPointer<Document> Ptr;

public:
    ~Document();

59
60
61
    unsigned revision() const;
    void setRevision(unsigned revision);

62
63
64
    unsigned editorRevision() const;
    void setEditorRevision(unsigned editorRevision);

65
66
67
    QDateTime lastModified() const;
    void setLastModified(const QDateTime &lastModified);

con's avatar
con committed
68
69
70
    QString fileName() const;

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

Roberto Raggi's avatar
Roberto Raggi committed
73
    void appendMacro(const Macro &macro);
74
    void addMacroUse(const Macro &macro, unsigned offset, unsigned length,
75
76
                     unsigned beginLine, const QVector<MacroArgumentReference> &range,
                     bool inCondition);
Christian Kamm's avatar
Christian Kamm committed
77
    void addUndefinedMacroUse(const QByteArray &name, unsigned offset);
78

con's avatar
con committed
79
80
81
82
83
84
85
86
87
    Control *control() const;
    TranslationUnit *translationUnit() const;

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

    unsigned globalSymbolCount() const;
    Symbol *globalSymbolAt(unsigned index) const;
    Scope *globalSymbols() const; // ### deprecate?
88

con's avatar
con committed
89
    Namespace *globalNamespace() const;
90
    void setGlobalNamespace(Namespace *globalNamespace); // ### internal
con's avatar
con committed
91

Roberto Raggi's avatar
Roberto Raggi committed
92
93
94
    QList<Macro> definedMacros() const
    { return _definedMacros; }

95
    Symbol *lastVisibleSymbolAt(unsigned line, unsigned column) const;
96
    Scope *scopeAt(unsigned line, unsigned column);
con's avatar
con committed
97

Roberto Raggi's avatar
Roberto Raggi committed
98
    QByteArray source() const;
con's avatar
con committed
99
    void setSource(const QByteArray &source);
Roberto Raggi's avatar
Roberto Raggi committed
100

con's avatar
con committed
101
102
103
    void startSkippingBlocks(unsigned offset);
    void stopSkippingBlocks(unsigned offset);

104
105
106
107
    enum ParseMode { // ### keep in sync with CPlusPlus::TranslationUnit
        ParseTranlationUnit,
        ParseDeclaration,
        ParseExpression,
108
        ParseDeclarator,
109
110
111
        ParseStatement
    };

112
113
114
115
    bool isTokenized() const;
    void tokenize();

    bool isParsed() const;
116
    bool parse(ParseMode mode = ParseTranlationUnit);
117

118
119
120
121
122
123
    enum CheckMode {
        FullCheck,
        FastCheck
    };

    void check(CheckMode mode = FullCheck);
124
125

    void releaseSource();
con's avatar
con committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
    void releaseTranslationUnit();

    static Ptr create(const QString &fileName);

    class DiagnosticMessage
    {
    public:
        enum Level {
            Warning,
            Error,
            Fatal
        };

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

167
        unsigned line() const
con's avatar
con committed
168
169
        { return _line; }

170
        unsigned column() const
con's avatar
con committed
171
172
        { return _column; }

173
174
175
        unsigned length() const
        { return _length; }

con's avatar
con committed
176
177
178
179
180
181
        QString text() const
        { return _text; }

    private:
        int _level;
        QString _fileName;
182
183
        unsigned _line;
        unsigned _column;
184
        unsigned _length;
con's avatar
con committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
        QString _text;
    };

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

    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)
        { }

204
205
206
207
208
209
210
211
212
        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
213
214
215
216
217
        inline unsigned begin() const
        { return _begin; }

        inline unsigned end() const
        { return _end; }
Roberto Raggi's avatar
Roberto Raggi committed
218
219
220
221
222

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

223
224
225
226
227
228
229
230
231
232
233
234
235
236
    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; }
237
238
239

        bool resolved() const
        { return QFileInfo(_fileName).isAbsolute(); }
240
241
    };

Roberto Raggi's avatar
Roberto Raggi committed
242
243
    class MacroUse: public Block {
        Macro _macro;
244
        QVector<Block> _arguments;
Christian Kamm's avatar
Christian Kamm committed
245
        bool _inCondition;
246
        unsigned _beginLine;
Roberto Raggi's avatar
Roberto Raggi committed
247
248
249

    public:
        inline MacroUse(const Macro &macro,
250
                        unsigned begin, unsigned end, unsigned beginLine)
Roberto Raggi's avatar
Roberto Raggi committed
251
            : Block(begin, end),
Christian Kamm's avatar
Christian Kamm committed
252
              _macro(macro),
253
254
              _inCondition(false),
              _beginLine(beginLine)
Roberto Raggi's avatar
Roberto Raggi committed
255
256
257
258
        { }

        const Macro &macro() const
        { return _macro; }
259
260
261
262
263
264
265

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

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

Christian Kamm's avatar
Christian Kamm committed
266
267
268
        bool isInCondition() const
        { return _inCondition; }

269
270
271
        unsigned beginLine() const
        { return _beginLine; }

Christian Kamm's avatar
Christian Kamm committed
272
    private:
273
274
275
276
277
        void setArguments(const QVector<Block> &arguments)
        { _arguments = arguments; }

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

        void setInCondition(bool set)
        { _inCondition = set; }

        friend class Document;
    };

    class UndefinedMacroUse: public Block {
        QByteArray _name;

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

        QByteArray name() const
        {
            return _name;
        }
con's avatar
con committed
300
301
    };

302
303
304
    QList<Include> includes() const
    { return _includes; }

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

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

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

314
315
316
317
    const Macro *findMacroDefinitionAt(unsigned line) const;
    const MacroUse *findMacroUseAt(unsigned offset) const;
    const UndefinedMacroUse *findUndefinedMacroUseAt(unsigned offset) const;

con's avatar
con committed
318
private:
319
    Symbol *lastVisibleSymbolAt(unsigned line, unsigned column, Scope *scope) const;
con's avatar
con committed
320
321
322
323
324
325
326

private:
    QString _fileName;
    Control *_control;
    TranslationUnit *_translationUnit;
    Namespace *_globalNamespace;
    QList<DiagnosticMessage> _diagnosticMessages;
327
    QList<Include> _includes;
Roberto Raggi's avatar
Roberto Raggi committed
328
    QList<Macro> _definedMacros;
con's avatar
con committed
329
    QList<Block> _skippedBlocks;
Roberto Raggi's avatar
Roberto Raggi committed
330
    QList<MacroUse> _macroUses;
Christian Kamm's avatar
Christian Kamm committed
331
    QList<UndefinedMacroUse> _undefinedMacroUses;
332
    QByteArray _source;
333
    QDateTime _lastModified;
334
    unsigned _revision;
335
    unsigned _editorRevision;
336
337

    friend class Snapshot;
con's avatar
con committed
338
339
};

340
class CPLUSPLUS_EXPORT Snapshot
341
{
342
    typedef QHash<QString, Document::Ptr> _Base;
343

344
public:
345
346
347
    Snapshot();
    ~Snapshot();

348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
    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;
    Document::Ptr operator[](const QString &fileName) const;

    const_iterator find(const QString &fileName) const;

366
367
    Snapshot simplified(Document::Ptr doc) const;

368
    QByteArray preprocessedCode(const QString &source,
369
370
371
372
373
                                const QString &fileName) const;

    Document::Ptr documentFromSource(const QByteArray &preprocessedCode,
                                     const QString &fileName) const;

374
    Symbol *findMatchingDefinition(Symbol *symbol) const;
375
    Class *findMatchingClassDeclaration(Symbol *symbol) const;
376

377
378
private:
    void simplified_helper(Document::Ptr doc, Snapshot *snapshot) const;
379
380
381

private:
    _Base _documents;
382
383
};

con's avatar
con committed
384
385
} // end of namespace CPlusPlus

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