CppDocument.h 9.86 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
    Control *control() const;
    TranslationUnit *translationUnit() const;

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

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

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

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

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

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

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

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

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

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

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

    void check(CheckMode mode = FullCheck);
123
124

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

    static Ptr create(const QString &fileName);

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

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

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

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

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

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

    private:
        int _level;
        QString _fileName;
181
182
        unsigned _line;
        unsigned _column;
183
        unsigned _length;
con's avatar
con committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
        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)
        { }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        friend class Document;
    };

    class UndefinedMacroUse: public Block {
        QByteArray _name;

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

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

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

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

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

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

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

con's avatar
con committed
317
318
319
320
321
322
private:
    QString _fileName;
    Control *_control;
    TranslationUnit *_translationUnit;
    Namespace *_globalNamespace;
    QList<DiagnosticMessage> _diagnosticMessages;
323
    QList<Include> _includes;
Roberto Raggi's avatar
Roberto Raggi committed
324
    QList<Macro> _definedMacros;
con's avatar
con committed
325
    QList<Block> _skippedBlocks;
Roberto Raggi's avatar
Roberto Raggi committed
326
    QList<MacroUse> _macroUses;
Christian Kamm's avatar
Christian Kamm committed
327
    QList<UndefinedMacroUse> _undefinedMacroUses;
328
    QByteArray _source;
329
    QDateTime _lastModified;
330
    unsigned _revision;
331
    unsigned _editorRevision;
332
333

    friend class Snapshot;
con's avatar
con committed
334
335
};

336
class CPLUSPLUS_EXPORT Snapshot
337
{
338
    typedef QHash<QString, Document::Ptr> _Base;
339

340
public:
341
342
343
    Snapshot();
    ~Snapshot();

344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
    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;

362
363
    Snapshot simplified(Document::Ptr doc) const;

364
    QByteArray preprocessedCode(const QString &source,
365
366
367
368
369
                                const QString &fileName) const;

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

370
    Symbol *findMatchingDefinition(Symbol *symbol) const;
371
    Class *findMatchingClassDeclaration(Symbol *symbol) const;
372

373
374
private:
    void simplified_helper(Document::Ptr doc, Snapshot *snapshot) const;
375
376
377

private:
    _Base _documents;
378
379
};

con's avatar
con committed
380
381
} // end of namespace CPlusPlus

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