CppDocument.cpp 17.8 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) 2012 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
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
con's avatar
con committed
30
**
31
**************************************************************************/
con's avatar
con committed
32 33

#include "CppDocument.h"
34
#include "FastPreprocessor.h"
35 36
#include "LookupContext.h"
#include "Overview.h"
Roberto Raggi's avatar
Roberto Raggi committed
37
#include "Bind.h"
hjk's avatar
hjk committed
38

con's avatar
con committed
39 40 41 42 43
#include <Control.h>
#include <TranslationUnit.h>
#include <DiagnosticClient.h>
#include <Literals.h>
#include <Symbols.h>
44
#include <Names.h>
con's avatar
con committed
45
#include <AST.h>
46 47
#include <ASTPatternBuilder.h>
#include <ASTMatcher.h>
con's avatar
con committed
48
#include <Scope.h>
49
#include <SymbolVisitor.h>
50 51 52
#include <NameVisitor.h>
#include <TypeVisitor.h>
#include <CoreTypes.h>
53
#include <LookupContext.h>
hjk's avatar
hjk committed
54

55 56 57 58
#include <QByteArray>
#include <QBitArray>
#include <QDir>
#include <QtDebug>
con's avatar
con committed
59

Christian Kamm's avatar
Christian Kamm committed
60 61 62 63 64
/*!
    \namespace CPlusPlus
    The namespace for C++ related tools.
*/

con's avatar
con committed
65 66 67 68
using namespace CPlusPlus;

namespace {

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
class LastVisibleSymbolAt: protected SymbolVisitor
{
    Symbol *root;
    unsigned line;
    unsigned column;
    Symbol *symbol;

public:
    LastVisibleSymbolAt(Symbol *root)
        : root(root), line(0), column(0), symbol(0) {}

    Symbol *operator()(unsigned line, unsigned column)
    {
        this->line = line;
        this->column = column;
        this->symbol = 0;
        accept(root);
        if (! symbol)
            symbol = root;
        return symbol;
    }

protected:
    bool preVisit(Symbol *s)
    {
        if (s->asBlock()) {
            if (s->line() < line || (s->line() == line && s->column() <= column)) {
                return true;
            }
            // skip blocks
        } if (s->line() < line || (s->line() == line && s->column() <= column)) {
            symbol = s;
            return true;
        }

        return false;
    }
};

108 109 110 111 112 113 114 115
class FindScopeAt: protected SymbolVisitor
{
    TranslationUnit *_unit;
    unsigned _line;
    unsigned _column;
    Scope *_scope;

public:
Erik Verbruggen's avatar
Erik Verbruggen committed
116
    /** line and column should be 1-based */
117 118 119 120 121 122 123 124 125 126
    FindScopeAt(TranslationUnit *unit, unsigned line, unsigned column)
        : _unit(unit), _line(line), _column(column), _scope(0) {}

    Scope *operator()(Symbol *symbol)
    {
        accept(symbol);
        return _scope;
    }

protected:
Roberto Raggi's avatar
Roberto Raggi committed
127
    bool process(Scope *symbol)
128 129
    {
        if (! _scope) {
Roberto Raggi's avatar
Roberto Raggi committed
130
            Scope *scope = symbol;
131

Roberto Raggi's avatar
Roberto Raggi committed
132 133
            for (unsigned i = 0; i < scope->memberCount(); ++i) {
                accept(scope->memberAt(i));
134 135 136 137 138 139

                if (_scope)
                    return false;
            }

            unsigned startLine, startColumn;
140
            _unit->getPosition(scope->startOffset(), &startLine, &startColumn);
141 142 143

            if (_line > startLine || (_line == startLine && _column >= startColumn)) {
                unsigned endLine, endColumn;
144
                _unit->getPosition(scope->endOffset(), &endLine, &endColumn);
145

146
                if (_line < endLine || (_line == endLine && _column < endColumn))
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
                    _scope = scope;
            }
        }

        return false;
    }

    using SymbolVisitor::visit;

    virtual bool preVisit(Symbol *)
    { return ! _scope; }

    virtual bool visit(UsingNamespaceDirective *) { return false; }
    virtual bool visit(UsingDeclaration *) { return false; }
    virtual bool visit(NamespaceAlias *) { return false; }
    virtual bool visit(Declaration *) { return false; }
    virtual bool visit(Argument *) { return false; }
    virtual bool visit(TypenameArgument *) { return false; }
    virtual bool visit(BaseClass *) { return false; }
    virtual bool visit(ForwardClassDeclaration *) { return false; }

    virtual bool visit(Enum *symbol)
    { return process(symbol); }

    virtual bool visit(Function *symbol)
    { return process(symbol); }

    virtual bool visit(Namespace *symbol)
    { return process(symbol); }

    virtual bool visit(Class *symbol)
    { return process(symbol); }

    virtual bool visit(Block *symbol)
    { return process(symbol); }

    // Objective-C
    virtual bool visit(ObjCBaseClass *) { return false; }
    virtual bool visit(ObjCBaseProtocol *) { return false; }
    virtual bool visit(ObjCForwardClassDeclaration *) { return false; }
    virtual bool visit(ObjCForwardProtocolDeclaration *) { return false; }
    virtual bool visit(ObjCPropertyDeclaration *) { return false; }
Erik Verbruggen's avatar
Erik Verbruggen committed
189 190 191 192 193 194 195 196 197

    virtual bool visit(ObjCClass *symbol)
    { return process(symbol); }

    virtual bool visit(ObjCProtocol *symbol)
    { return process(symbol); }

    virtual bool visit(ObjCMethod *symbol)
    { return process(symbol); }
198 199
};

200

hjk's avatar
hjk committed
201 202 203 204 205 206 207
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
208 209
          messages(messages),
          errorCount(0)
hjk's avatar
hjk committed
210 211 212
    { }

    virtual void report(int level,
Roberto Raggi's avatar
Roberto Raggi committed
213
                        const StringLiteral *fileId,
hjk's avatar
hjk committed
214 215 216
                        unsigned line, unsigned column,
                        const char *format, va_list ap)
    {
Roberto Raggi's avatar
Roberto Raggi committed
217 218 219 220 221 222
        if (level == Error) {
            ++errorCount;

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

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

hjk's avatar
hjk committed
226 227
        if (fileName != doc->fileName())
            return;
con's avatar
con committed
228

hjk's avatar
hjk committed
229 230
        QString message;
        message.vsprintf(format, ap);
con's avatar
con committed
231

hjk's avatar
hjk committed
232 233 234 235
        Document::DiagnosticMessage m(convertLevel(level), doc->fileName(),
                                      line, column, message);
        messages->append(m);
    }
con's avatar
con committed
236

hjk's avatar
hjk committed
237 238 239 240 241 242
    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
243
        }
hjk's avatar
hjk committed
244
    }
con's avatar
con committed
245

Roberto Raggi's avatar
Roberto Raggi committed
246
private:
hjk's avatar
hjk committed
247 248
    Document *doc;
    QList<Document::DiagnosticMessage> *messages;
Roberto Raggi's avatar
Roberto Raggi committed
249
    int errorCount;
hjk's avatar
hjk committed
250
};
con's avatar
con committed
251

hjk's avatar
hjk committed
252
} // anonymous namespace
con's avatar
con committed
253

Christian Kamm's avatar
Christian Kamm committed
254

con's avatar
con committed
255
Document::Document(const QString &fileName)
256
    : _fileName(QDir::cleanPath(fileName)),
257
      _globalNamespace(0),
258
      _revision(0),
259 260
      _editorRevision(0),
      _checkMode(0)
con's avatar
con committed
261 262 263 264 265 266
{
    _control = new Control();

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

    const QByteArray localFileName = fileName.toUtf8();
267
    const StringLiteral *fileId = _control->stringLiteral(localFileName.constData(),
Roberto Raggi's avatar
Roberto Raggi committed
268
                                                                      localFileName.size());
con's avatar
con committed
269 270
    _translationUnit = new TranslationUnit(_control, fileId);
    _translationUnit->setQtMocRunEnabled(true);
Roberto Raggi's avatar
Roberto Raggi committed
271
    _translationUnit->setCxxOxEnabled(true);
Roberto Raggi's avatar
Roberto Raggi committed
272
    _translationUnit->setObjCEnabled(true);
con's avatar
con committed
273 274 275 276 277 278 279 280 281 282 283
    (void) _control->switchTranslationUnit(_translationUnit);
}

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

Control *Document::control() const
hjk's avatar
hjk committed
284 285 286
{
    return _control;
}
con's avatar
con committed
287

288 289 290 291 292 293 294 295 296 297
unsigned Document::revision() const
{
    return _revision;
}

void Document::setRevision(unsigned revision)
{
    _revision = revision;
}

298 299 300 301 302 303 304 305 306 307
unsigned Document::editorRevision() const
{
    return _editorRevision;
}

void Document::setEditorRevision(unsigned editorRevision)
{
    _editorRevision = editorRevision;
}

308 309 310 311 312 313 314 315 316 317
QDateTime Document::lastModified() const
{
    return _lastModified;
}

void Document::setLastModified(const QDateTime &lastModified)
{
    _lastModified = lastModified;
}

con's avatar
con committed
318
QString Document::fileName() const
hjk's avatar
hjk committed
319 320 321
{
    return _fileName;
}
con's avatar
con committed
322 323

QStringList Document::includedFiles() const
hjk's avatar
hjk committed
324
{
325 326 327 328 329
    QStringList files;
    foreach (const Include &i, _includes)
        files.append(i.fileName());
    files.removeDuplicates();
    return files;
hjk's avatar
hjk committed
330
}
con's avatar
con committed
331

332
void Document::addIncludeFile(const QString &fileName, unsigned line)
hjk's avatar
hjk committed
333
{
334
    _includes.append(Include(QDir::cleanPath(fileName), line));
hjk's avatar
hjk committed
335
}
con's avatar
con committed
336

Roberto Raggi's avatar
Roberto Raggi committed
337
void Document::appendMacro(const Macro &macro)
hjk's avatar
hjk committed
338
{
Roberto Raggi's avatar
Roberto Raggi committed
339
    _definedMacros.append(macro);
con's avatar
con committed
340 341
}

342
void Document::addMacroUse(const Macro &macro, unsigned offset, unsigned length,
343
                           unsigned beginLine,
Christian Kamm's avatar
Christian Kamm committed
344
                           const QVector<MacroArgumentReference> &actuals, bool inCondition)
345
{
346
    MacroUse use(macro, offset, offset + length, beginLine);
Christian Kamm's avatar
Christian Kamm committed
347
    use.setInCondition(inCondition);
348 349 350 351 352 353 354 355

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

        use.addArgument(arg);
    }

    _macroUses.append(use);
356 357
}

Christian Kamm's avatar
Christian Kamm committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
void Document::addUndefinedMacroUse(const QByteArray &name, unsigned offset)
{
    QByteArray copy(name.data(), name.size());
    UndefinedMacroUse use(copy, offset);
    _undefinedMacroUses.append(use);
}

/*!
    \class Document::MacroUse
    \brief Represents the usage of a macro in a \l {Document}.
    \sa Document::UndefinedMacroUse
*/

/*!
    \class Document::UndefinedMacroUse
    \brief Represents a macro that was looked up, but not found.

    Holds data about the reference to a macro in an \tt{#ifdef} or \tt{#ifndef}
    or argument to the \tt{defined} operator inside an \tt{#if} or \tt{#elif} that does
    not exist.

    \sa Document::undefinedMacroUses(), Document::MacroUse, Macro
*/

/*!
    \fn QByteArray Document::UndefinedMacroUse::name() const

    Returns the name of the macro that was not found.
*/

/*!
    \fn QList<UndefinedMacroUse> Document::undefinedMacroUses() const

    Returns a list of referenced but undefined macros.

    \sa Document::macroUses(), Document::definedMacros(), Macro
*/

/*!
    \fn QList<MacroUse> Document::macroUses() const

    Returns a list of macros used.

    \sa Document::undefinedMacroUses(), Document::definedMacros(), Macro
*/

/*!
    \fn QList<Macro> Document::definedMacros() const

    Returns the list of macros defined.

    \sa Document::macroUses(), Document::undefinedMacroUses()
*/

con's avatar
con committed
412
TranslationUnit *Document::translationUnit() const
hjk's avatar
hjk committed
413 414 415
{
    return _translationUnit;
}
con's avatar
con committed
416 417

bool Document::skipFunctionBody() const
hjk's avatar
hjk committed
418 419 420
{
    return _translationUnit->skipFunctionBody();
}
con's avatar
con committed
421 422

void Document::setSkipFunctionBody(bool skipFunctionBody)
hjk's avatar
hjk committed
423 424 425
{
    _translationUnit->setSkipFunctionBody(skipFunctionBody);
}
con's avatar
con committed
426 427 428 429 430 431 432 433 434 435

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

    return _globalNamespace->memberCount();
}

Symbol *Document::globalSymbolAt(unsigned index) const
hjk's avatar
hjk committed
436 437 438
{
    return _globalNamespace->memberAt(index);
}
con's avatar
con committed
439 440

Namespace *Document::globalNamespace() const
hjk's avatar
hjk committed
441 442 443
{
    return _globalNamespace;
}
con's avatar
con committed
444

445 446 447 448 449
void Document::setGlobalNamespace(Namespace *globalNamespace)
{
    _globalNamespace = globalNamespace;
}

450 451 452
Scope *Document::scopeAt(unsigned line, unsigned column)
{
    FindScopeAt findScopeAt(_translationUnit, line, column);
453 454
    if (Scope *scope = findScopeAt(_globalNamespace))
        return scope;
Roberto Raggi's avatar
Roberto Raggi committed
455
    return globalNamespace();
456 457
}

458
Symbol *Document::lastVisibleSymbolAt(unsigned line, unsigned column) const
hjk's avatar
hjk committed
459
{
460 461
    LastVisibleSymbolAt lastVisibleSymbolAt(globalNamespace());
    return lastVisibleSymbolAt(line, column);
con's avatar
con committed
462 463
}

464 465 466 467 468 469 470 471 472 473 474 475
const Macro *Document::findMacroDefinitionAt(unsigned line) const
{
    foreach (const Macro &macro, _definedMacros) {
        if (macro.line() == line)
            return &macro;
    }
    return 0;
}

const Document::MacroUse *Document::findMacroUseAt(unsigned offset) const
{
    foreach (const Document::MacroUse &use, _macroUses) {
476
        if (use.contains(offset) && (offset < use.begin() + use.macro().name().length()))
477 478 479 480 481 482 483 484
            return &use;
    }
    return 0;
}

const Document::UndefinedMacroUse *Document::findUndefinedMacroUseAt(unsigned offset) const
{
    foreach (const Document::UndefinedMacroUse &use, _undefinedMacroUses) {
485
        if (use.contains(offset) && (offset < use.begin() + use.name().length()))
486 487 488 489 490
            return &use;
    }
    return 0;
}

con's avatar
con committed
491 492 493 494 495 496
Document::Ptr Document::create(const QString &fileName)
{
    Document::Ptr doc(new Document(fileName));
    return doc;
}

497
QByteArray Document::utf8Source() const
Roberto Raggi's avatar
Roberto Raggi committed
498 499
{ return _source; }

500
void Document::setUtf8Source(const QByteArray &source)
hjk's avatar
hjk committed
501
{
502 503
    _source = source;
    _translationUnit->setSource(_source.constBegin(), _source.size());
hjk's avatar
hjk committed
504
}
con's avatar
con committed
505 506

void Document::startSkippingBlocks(unsigned start)
hjk's avatar
hjk committed
507 508 509
{
    _skippedBlocks.append(Block(start, 0));
}
con's avatar
con committed
510 511 512

void Document::stopSkippingBlocks(unsigned stop)
{
Roberto Raggi's avatar
Roberto Raggi committed
513 514 515
    if (_skippedBlocks.isEmpty())
        return;

con's avatar
con committed
516 517 518 519 520 521 522
    unsigned start = _skippedBlocks.back().begin();
    if (start > stop)
        _skippedBlocks.removeLast(); // Ignore this block, it's invalid.
    else
        _skippedBlocks.back() = Block(start, stop);
}

523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
bool Document::isTokenized() const
{
    return _translationUnit->isTokenized();
}

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

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

538
bool Document::parse(ParseMode mode)
hjk's avatar
hjk committed
539
{
540 541 542 543 544 545 546 547 548 549 550 551 552 553
    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;

554 555 556 557
    case ParseDeclarator:
        m = TranslationUnit::ParseDeclarator;
        break;

558 559 560 561 562 563 564 565 566
    case ParseStatement:
        m = TranslationUnit::ParseStatement;
        break;

    default:
        break;
    }

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

569
void Document::check(CheckMode mode)
con's avatar
con committed
570
{
571
    Q_ASSERT(!_globalNamespace);
con's avatar
con committed
572

573 574
    _checkMode = mode;

575 576 577
    if (! isParsed())
        parse();

Roberto Raggi's avatar
Roberto Raggi committed
578 579
    _globalNamespace = _control->newNamespace(0);
    Bind semantic(_translationUnit);
580
    if (mode == FastCheck)
581
        semantic.setSkipFunctionBodies(true);
con's avatar
con committed
582

583 584 585 586
    if (! _translationUnit->ast())
        return; // nothing to do.

    if (TranslationUnitAST *ast = _translationUnit->ast()->asTranslationUnit()) {
Roberto Raggi's avatar
Roberto Raggi committed
587
        semantic(ast, _globalNamespace);
588
    } else if (ExpressionAST *ast = _translationUnit->ast()->asExpression()) {
Roberto Raggi's avatar
Roberto Raggi committed
589
        semantic(ast, _globalNamespace);
590 591
    } else if (DeclarationAST *ast = translationUnit()->ast()->asDeclaration()) {
        semantic(ast, _globalNamespace);
con's avatar
con committed
592 593 594
    }
}

595
void Document::keepSourceAndAST()
596
{
597
    _keepSourceAndASTCount.ref();
598 599
}

600
void Document::releaseSourceAndAST()
hjk's avatar
hjk committed
601
{
602 603 604
    if (!_keepSourceAndASTCount.deref()) {
        _source.clear();
        _translationUnit->release();
605
        _control->squeeze();
606
    }
hjk's avatar
hjk committed
607
}
608

609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
bool Document::DiagnosticMessage::operator==(const Document::DiagnosticMessage &other) const
{
    return
            _line == other._line &&
            _column == other._column &&
            _length == other._length &&
            _level == other._level &&
            _fileName == other._fileName &&
            _text == other._text;
}

bool Document::DiagnosticMessage::operator!=(const Document::DiagnosticMessage &other) const
{
    return !operator==(other);
}

625 626 627 628 629 630 631 632
Snapshot::Snapshot()
{
}

Snapshot::~Snapshot()
{
}

633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
int Snapshot::size() const
{
    return _documents.size();
}

bool Snapshot::isEmpty() const
{
    return _documents.isEmpty();
}

Document::Ptr Snapshot::operator[](const QString &fileName) const
{
    return _documents.value(fileName, Document::Ptr());
}

Snapshot::const_iterator Snapshot::find(const QString &fileName) const
{
    return _documents.find(fileName);
}

void Snapshot::remove(const QString &fileName)
{
    _documents.remove(fileName);
}

bool Snapshot::contains(const QString &fileName) const
{
    return _documents.contains(fileName);
}

663 664 665
void Snapshot::insert(Document::Ptr doc)
{
    if (doc)
666
        _documents.insert(doc->fileName(), doc);
667 668
}

669
QByteArray Snapshot::preprocessedCode(const QString &source, const QString &fileName) const
670 671 672 673 674 675 676 677
{
    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
678
    Document::Ptr newDoc = Document::create(fileName);
679

680
    if (Document::Ptr thisDocument = document(fileName)) {
681
        newDoc->_revision = thisDocument->_revision;
682
        newDoc->_editorRevision = thisDocument->_editorRevision;
683
        newDoc->_lastModified = thisDocument->_lastModified;
684 685
        newDoc->_includes = thisDocument->_includes;
        newDoc->_definedMacros = thisDocument->_definedMacros;
686
        newDoc->_macroUses = thisDocument->_macroUses;
687 688
    }

689
    newDoc->setUtf8Source(preprocessedCode);
Roberto Raggi's avatar
Roberto Raggi committed
690
    return newDoc;
691 692
}

693
Document::Ptr Snapshot::document(const QString &fileName) const
694
{
695
    return _documents.value(fileName);
696 697
}

698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
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()) {
714
            Document::Ptr includedDoc = document(incl.fileName());
715 716 717 718
            simplified_helper(includedDoc, snapshot);
        }
    }
}
719