CppDocument.cpp 10 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 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
26
** contact the sales department at http://www.qtsoftware.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
con's avatar
con committed
29
30

#include "CppDocument.h"
31
32
#include "CppBindings.h"
#include "FastPreprocessor.h"
hjk's avatar
hjk committed
33

con's avatar
con committed
34
35
36
37
38
39
40
41
#include <Control.h>
#include <TranslationUnit.h>
#include <DiagnosticClient.h>
#include <Semantic.h>
#include <Literals.h>
#include <Symbols.h>
#include <AST.h>
#include <Scope.h>
hjk's avatar
hjk committed
42

con's avatar
con committed
43
44
45
46
47
48
49
50
#include <QByteArray>
#include <QFile>
#include <QtDebug>

using namespace CPlusPlus;

namespace {

hjk's avatar
hjk committed
51
52
53
54
55
56
57
class DocumentDiagnosticClient : public DiagnosticClient
{
    enum { MAX_MESSAGE_COUNT = 10 };

public:
    DocumentDiagnosticClient(Document *doc, QList<Document::DiagnosticMessage> *messages)
        : doc(doc),
Roberto Raggi's avatar
Roberto Raggi committed
58
59
          messages(messages),
          errorCount(0)
hjk's avatar
hjk committed
60
61
62
63
64
65
66
    { }

    virtual void report(int level,
                        StringLiteral *fileId,
                        unsigned line, unsigned column,
                        const char *format, va_list ap)
    {
Roberto Raggi's avatar
Roberto Raggi committed
67
68
69
70
71
72
        if (level == Error) {
            ++errorCount;

            if (errorCount >= MAX_MESSAGE_COUNT)
                return; // ignore the error
        }
con's avatar
con committed
73

hjk's avatar
hjk committed
74
        const QString fileName = QString::fromUtf8(fileId->chars(), fileId->size());
con's avatar
con committed
75

hjk's avatar
hjk committed
76
77
        if (fileName != doc->fileName())
            return;
con's avatar
con committed
78

hjk's avatar
hjk committed
79
80
        QString message;
        message.vsprintf(format, ap);
con's avatar
con committed
81

hjk's avatar
hjk committed
82
83
84
85
        Document::DiagnosticMessage m(convertLevel(level), doc->fileName(),
                                      line, column, message);
        messages->append(m);
    }
con's avatar
con committed
86

hjk's avatar
hjk committed
87
88
89
90
91
92
    static int convertLevel(int level) {
        switch (level) {
            case Warning: return Document::DiagnosticMessage::Warning;
            case Error:   return Document::DiagnosticMessage::Error;
            case Fatal:   return Document::DiagnosticMessage::Fatal;
            default:      return Document::DiagnosticMessage::Error;
con's avatar
con committed
93
        }
hjk's avatar
hjk committed
94
    }
con's avatar
con committed
95

Roberto Raggi's avatar
Roberto Raggi committed
96
private:
hjk's avatar
hjk committed
97
98
    Document *doc;
    QList<Document::DiagnosticMessage> *messages;
Roberto Raggi's avatar
Roberto Raggi committed
99
    int errorCount;
hjk's avatar
hjk committed
100
};
con's avatar
con committed
101

hjk's avatar
hjk committed
102
} // anonymous namespace
con's avatar
con committed
103
104
105
106
107
108
109
110
111
112

Document::Document(const QString &fileName)
    : _fileName(fileName),
      _globalNamespace(0)
{
    _control = new Control();

    _control->setDiagnosticClient(new DocumentDiagnosticClient(this, &_diagnosticMessages));

    const QByteArray localFileName = fileName.toUtf8();
Roberto Raggi's avatar
Roberto Raggi committed
113
114
    StringLiteral *fileId = _control->findOrInsertStringLiteral(localFileName.constData(),
                                                                localFileName.size());
con's avatar
con committed
115
116
    _translationUnit = new TranslationUnit(_control, fileId);
    _translationUnit->setQtMocRunEnabled(true);
Roberto Raggi's avatar
Roberto Raggi committed
117
    _translationUnit->setObjCEnabled(true);
con's avatar
con committed
118
119
120
121
122
123
124
125
126
127
128
    (void) _control->switchTranslationUnit(_translationUnit);
}

Document::~Document()
{
    delete _translationUnit;
    delete _control->diagnosticClient();
    delete _control;
}

Control *Document::control() const
hjk's avatar
hjk committed
129
130
131
{
    return _control;
}
con's avatar
con committed
132
133

QString Document::fileName() const
hjk's avatar
hjk committed
134
135
136
{
    return _fileName;
}
con's avatar
con committed
137
138

QStringList Document::includedFiles() const
hjk's avatar
hjk committed
139
{
140
141
142
143
144
    QStringList files;
    foreach (const Include &i, _includes)
        files.append(i.fileName());
    files.removeDuplicates();
    return files;
hjk's avatar
hjk committed
145
}
con's avatar
con committed
146

147
void Document::addIncludeFile(const QString &fileName, unsigned line)
hjk's avatar
hjk committed
148
{
149
    _includes.append(Include(fileName, line));
hjk's avatar
hjk committed
150
}
con's avatar
con committed
151

Roberto Raggi's avatar
Roberto Raggi committed
152
void Document::appendMacro(const Macro &macro)
hjk's avatar
hjk committed
153
{
Roberto Raggi's avatar
Roberto Raggi committed
154
    _definedMacros.append(macro);
con's avatar
con committed
155
156
}

157
158
void Document::addMacroUse(const Macro &macro, unsigned offset, unsigned length,
                           const QVector<MacroArgumentReference> &actuals)
159
{
160
161
162
163
164
165
166
167
168
    MacroUse use(macro, offset, offset + length);

    foreach (const MacroArgumentReference &actual, actuals) {
        const Block arg(actual.position(), actual.position() + actual.length());

        use.addArgument(arg);
    }

    _macroUses.append(use);
169
170
}

con's avatar
con committed
171
TranslationUnit *Document::translationUnit() const
hjk's avatar
hjk committed
172
173
174
{
    return _translationUnit;
}
con's avatar
con committed
175
176

bool Document::skipFunctionBody() const
hjk's avatar
hjk committed
177
178
179
{
    return _translationUnit->skipFunctionBody();
}
con's avatar
con committed
180
181

void Document::setSkipFunctionBody(bool skipFunctionBody)
hjk's avatar
hjk committed
182
183
184
{
    _translationUnit->setSkipFunctionBody(skipFunctionBody);
}
con's avatar
con committed
185
186
187
188
189
190
191
192
193
194

unsigned Document::globalSymbolCount() const
{
    if (! _globalNamespace)
        return 0;

    return _globalNamespace->memberCount();
}

Symbol *Document::globalSymbolAt(unsigned index) const
hjk's avatar
hjk committed
195
196
197
{
    return _globalNamespace->memberAt(index);
}
con's avatar
con committed
198
199
200
201
202
203
204
205
206
207

Scope *Document::globalSymbols() const
{
    if (! _globalNamespace)
        return 0;

    return _globalNamespace->members();
}

Namespace *Document::globalNamespace() const
hjk's avatar
hjk committed
208
209
210
{
    return _globalNamespace;
}
con's avatar
con committed
211
212

Symbol *Document::findSymbolAt(unsigned line, unsigned column) const
hjk's avatar
hjk committed
213
214
215
{
    return findSymbolAt(line, column, globalSymbols());
}
con's avatar
con committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244

Symbol *Document::findSymbolAt(unsigned line, unsigned column, Scope *scope) const
{
    Symbol *previousSymbol = 0;

    for (unsigned i = 0; i < scope->symbolCount(); ++i) {
        Symbol *symbol = scope->symbolAt(i);
        if (symbol->line() > line)
            break;

        previousSymbol = symbol;
    }

    if (previousSymbol) {
        if (ScopedSymbol *scoped = previousSymbol->asScopedSymbol()) {
            if (Symbol *member = findSymbolAt(line, column, scoped->members()))
                return member;
        }
    }

    return previousSymbol;
}

Document::Ptr Document::create(const QString &fileName)
{
    Document::Ptr doc(new Document(fileName));
    return doc;
}

Roberto Raggi's avatar
Roberto Raggi committed
245
246
247
QByteArray Document::source() const
{ return _source; }

con's avatar
con committed
248
void Document::setSource(const QByteArray &source)
hjk's avatar
hjk committed
249
{
250
251
    _source = source;
    _translationUnit->setSource(_source.constBegin(), _source.size());
hjk's avatar
hjk committed
252
}
con's avatar
con committed
253
254

void Document::startSkippingBlocks(unsigned start)
hjk's avatar
hjk committed
255
256
257
{
    _skippedBlocks.append(Block(start, 0));
}
con's avatar
con committed
258
259
260

void Document::stopSkippingBlocks(unsigned stop)
{
Roberto Raggi's avatar
Roberto Raggi committed
261
262
263
    if (_skippedBlocks.isEmpty())
        return;

con's avatar
con committed
264
265
266
267
268
269
270
    unsigned start = _skippedBlocks.back().begin();
    if (start > stop)
        _skippedBlocks.removeLast(); // Ignore this block, it's invalid.
    else
        _skippedBlocks.back() = Block(start, stop);
}

271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
bool Document::isTokenized() const
{
    return _translationUnit->isTokenized();
}

void Document::tokenize()
{
    _translationUnit->tokenize();
}

bool Document::isParsed() const
{
    return _translationUnit->isParsed();
}

286
bool Document::parse(ParseMode mode)
hjk's avatar
hjk committed
287
{
288
289
290
291
292
293
294
295
296
297
298
299
300
301
    TranslationUnit::ParseMode m = TranslationUnit::ParseTranlationUnit;
    switch (mode) {
    case ParseTranlationUnit:
        m = TranslationUnit::ParseTranlationUnit;
        break;

    case ParseDeclaration:
        m = TranslationUnit::ParseDeclaration;
        break;

    case ParseExpression:
        m = TranslationUnit::ParseExpression;
        break;

302
303
304
305
    case ParseDeclarator:
        m = TranslationUnit::ParseDeclarator;
        break;

306
307
308
309
310
311
312
313
314
    case ParseStatement:
        m = TranslationUnit::ParseStatement;
        break;

    default:
        break;
    }

    return _translationUnit->parse(m);
hjk's avatar
hjk committed
315
}
con's avatar
con committed
316

317
void Document::check(CheckMode mode)
con's avatar
con committed
318
{
319
    Q_ASSERT(!_globalNamespace);
con's avatar
con committed
320
321

    Semantic semantic(_control);
322
323
    if (mode == FastCheck)
        semantic.setSkipFunctionBodies(true);
con's avatar
con committed
324
325
326

    _globalNamespace = _control->newNamespace(0);
    Scope *globals = _globalNamespace->members();
327
328
329
330
    if (! _translationUnit->ast())
        return; // nothing to do.

    if (TranslationUnitAST *ast = _translationUnit->ast()->asTranslationUnit()) {
Roberto Raggi's avatar
Roberto Raggi committed
331
332
        for (DeclarationListAST *decl = ast->declarations; decl; decl = decl->next) {
            semantic.check(decl->declaration, globals);
con's avatar
con committed
333
        }
334
335
    } else if (ExpressionAST *ast = _translationUnit->ast()->asExpression()) {
        semantic.check(ast, globals);
con's avatar
con committed
336
337
338
    }
}

339
340
341
342
343
void Document::releaseSource()
{
    _source.clear();
}

con's avatar
con committed
344
void Document::releaseTranslationUnit()
hjk's avatar
hjk committed
345
346
347
{
    _translationUnit->release();
}
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362

Snapshot::Snapshot()
{
}

Snapshot::~Snapshot()
{
}

void Snapshot::insert(Document::Ptr doc)
{
    if (doc)
        insert(doc->fileName(), doc);
}

363
QByteArray Snapshot::preprocessedCode(const QString &source, const QString &fileName) const
364
365
366
367
368
369
370
371
{
    FastPreprocessor pp(*this);
    return pp.run(fileName, source);
}

Document::Ptr Snapshot::documentFromSource(const QByteArray &preprocessedCode,
                                           const QString &fileName) const
{
Roberto Raggi's avatar
Roberto Raggi committed
372
373
    FastPreprocessor pp(*this);
    Document::Ptr newDoc = Document::create(fileName);
374

Roberto Raggi's avatar
Roberto Raggi committed
375
    if (Document::Ptr thisDocument = value(fileName)) {
376
377
378
379
        newDoc->_includes = thisDocument->_includes;
        newDoc->_definedMacros = thisDocument->_definedMacros;
    }

Roberto Raggi's avatar
Roberto Raggi committed
380
381
382
    newDoc->setSource(preprocessedCode);
    newDoc->parse();
    return newDoc;
383
384
385
386
387
388
}

QSharedPointer<NamespaceBinding> Snapshot::globalNamespaceBinding(Document::Ptr doc) const
{
    return CPlusPlus::bind(doc, *this);
}
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410

Snapshot Snapshot::simplified(Document::Ptr doc) const
{
    Snapshot snapshot;
    simplified_helper(doc, &snapshot);
    return snapshot;
}

void Snapshot::simplified_helper(Document::Ptr doc, Snapshot *snapshot) const
{
    if (! doc)
        return;

    if (! snapshot->contains(doc->fileName())) {
        snapshot->insert(doc);

        foreach (const Document::Include &incl, doc->includes()) {
            Document::Ptr includedDoc = value(incl.fileName());
            simplified_helper(includedDoc, snapshot);
        }
    }
}