classviewparser.cpp 19.3 KB
Newer Older
1
2
/**************************************************************************
**
3
** Copyright (c) 2013 Denis Mingulov
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
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
****************************************************************************/
29
30
31
32
33
34

#include "classviewparser.h"
#include "classviewconstants.h"
#include "classviewutils.h"

// cplusplus shared library. the same folder (cplusplus)
35
36
37
38
#include <cplusplus/Symbol.h>
#include <cplusplus/Symbols.h>
#include <cplusplus/Scope.h>
#include <cplusplus/Name.h>
39
40

// other
41
#include <cpptools/ModelManagerInterface.h>
42
43
44
45
46
47
#include <cplusplus/Overview.h>
#include <cplusplus/Icons.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/session.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectnodes.h>
48
#include <coreplugin/idocument.h>
49
50
#include <utils/qtcassert.h>

51
52
53
54
55
56
57
58
#include <QStandardItem>
#include <QDebug>
#include <QHash>
#include <QSet>
#include <QTimer>
#include <QReadWriteLock>
#include <QReadLocker>
#include <QWriteLocker>
59
60
61
62
63
64
65
66
67
68
69
70
71

enum { debug = false };

namespace ClassView {
namespace Internal {

// ----------------------------- ParserPrivate ---------------------------------

/*!
   \struct ParserPrivate
   \brief Private class data for \a Parser
   \sa Parser
 */
72
class ParserPrivate
73
{
74
public:
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
    //! Constructor
    ParserPrivate() : flatMode(false) {}

    //! Get document from documentList
    CPlusPlus::Document::Ptr document(const QString &fileName) const;

    CPlusPlus::Overview overview;

    //! timer
    QPointer<QTimer> timer;

    // documents
    //! Documents read write lock
    QReadWriteLock docLocker;

    //! Current document list
    QHash<QString, CPlusPlus::Document::Ptr> documentList;

    //! Parsed documents' revision - to speed up computations
    QHash<QString, unsigned> cachedDocTreesRevision;

    //! Parsed documents - to speed up computations
    QHash<QString, ParserTreeItem::ConstPtr> cachedDocTrees;

    // project trees
    //! Projects read write lock
    QReadWriteLock prjLocker;

    //! Parsed projects' revision - to speed up computations
    QHash<QString, unsigned> cachedPrjTreesRevision;

    //! Merged trees for projects. Not const - projects might be substracted/added
    QHash<QString, ParserTreeItem::Ptr> cachedPrjTrees;

    //! Cached file lists for projects (non-flat mode)
    QHash<QString, QStringList> cachedPrjFileLists;

    // other
    //! List for files which has to be parsed
    QSet<QString> fileList;

    //! Root item read write lock
    QReadWriteLock rootItemLocker;

    //! Parsed root item
    ParserTreeItem::ConstPtr rootItem;

    //! Flat mode
    bool flatMode;
};

CPlusPlus::Document::Ptr ParserPrivate::document(const QString &fileName) const
{
    if (!documentList.contains(fileName))
        return CPlusPlus::Document::Ptr();
    return documentList[fileName];
}

// ----------------------------- Parser ---------------------------------

Parser::Parser(QObject *parent)
    : QObject(parent),
137
    d(new ParserPrivate())
138
{
139
140
    d->timer = new QTimer(this);
    d->timer->setSingleShot(true);
141
142
143
144
145
146

    // connect signal/slots
    // internal data reset
    connect(this, SIGNAL(resetDataDone()), SLOT(onResetDataDone()), Qt::QueuedConnection);

    // timer for emitting changes
147
    connect(d->timer, SIGNAL(timeout()), SLOT(requestCurrentState()), Qt::QueuedConnection);
148
149
150
151
}

Parser::~Parser()
{
152
    delete d;
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
}

bool Parser::canFetchMore(QStandardItem *item) const
{
    ParserTreeItem::ConstPtr ptr = findItemByRoot(item);
    if (ptr.isNull())
        return false;
    return ptr->canFetchMore(item);
}

void Parser::fetchMore(QStandardItem *item, bool skipRoot) const
{
    ParserTreeItem::ConstPtr ptr = findItemByRoot(item, skipRoot);
    if (ptr.isNull())
        return;
    ptr->fetchMore(item);
}

void Parser::setFlatMode(bool flatMode)
{
173
    if (flatMode == d->flatMode)
174
175
176
        return;

    // change internal
177
    d->flatMode = flatMode;
178
179
180
181
182
183
184
185
186
187
188
189
190

    // regenerate and resend current tree
    emitCurrentTree();
}

ParserTreeItem::ConstPtr Parser::findItemByRoot(const QStandardItem *item, bool skipRoot) const
{
    if (!item)
        return ParserTreeItem::ConstPtr();

    // go item by item to the root
    QList<const QStandardItem *> uiList;
    const QStandardItem *cur = item;
191
    while (cur) {
192
193
194
195
196
197
198
        uiList.append(cur);
        cur = cur->parent();
    }

    if (skipRoot && uiList.count() > 0)
        uiList.removeLast();

199
    QReadLocker locker(&d->rootItemLocker);
200
201

    // using internal root - search correct item
202
    ParserTreeItem::ConstPtr internal = d->rootItem;
203

204
    while (uiList.count() > 0) {
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
        cur = uiList.last();
        uiList.removeLast();
        const SymbolInformation &inf = Utils::symbolInformationFromItem(cur);
        internal = internal->child(inf);
        if (internal.isNull())
            break;
    }

    return internal;
}

ParserTreeItem::ConstPtr Parser::parse()
{
    QTime time;
    if (debug)
        time.start();

    ParserTreeItem::Ptr rootItem(new ParserTreeItem());

    // check all projects
    QList<ProjectExplorer::Project *> projects = getProjectList();
226
    foreach (const ProjectExplorer::Project *prj, projects) {
227
228
229
230
        if (!prj)
            continue;

        ParserTreeItem::Ptr item;
231
        if (!d->flatMode)
232
233
234
235
            item = ParserTreeItem::Ptr(new ParserTreeItem());

        QString prjName(prj->displayName());
        QString prjType(prjName);
236
237
        if (prj->document())
            prjType = prj->document()->fileName();
238
239
240
241
        SymbolInformation inf(prjName, prjType);

        QStringList projectList = addProjectNode(item, prj->rootProjectNode());

242
        if (d->flatMode) {
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
            // use prj path (prjType) as a project id
//            addProject(item, prj->files(ProjectExplorer::Project::ExcludeGeneratedFiles), prjType);
            //! \todo return back, works too long
            ParserTreeItem::Ptr flatItem = createFlatTree(projectList);
            item.swap(flatItem);
        }
        item->setIcon(prj->rootProjectNode()->icon());
        rootItem->appendChild(item, inf);
    }

    if (debug)
        qDebug() << "Class View:" << QDateTime::currentDateTime().toString()
            << "Parsed in " << time.elapsed() << "msecs.";

    return rootItem;
}

void Parser::addProject(const ParserTreeItem::Ptr &item, const QStringList &fileList,
                                 const QString &projectId)
{
    // recalculate cache tree if needed
    ParserTreeItem::Ptr prj(getCachedOrParseProjectTree(fileList, projectId));
    if (item.isNull())
        return;

    // if there is an item - copy project tree to that item
    item->copy(prj);
}

void Parser::addSymbol(const ParserTreeItem::Ptr &item, const CPlusPlus::Symbol *symbol)
{
    if (item.isNull() || !symbol)
        return;

    // easy solution - lets add any scoped symbol and
    // any symbol which does not contain :: in the name

//    if (symbol->isDeclaration())
//        return;

    //! \todo collect statistics and reorder to optimize
    if (symbol->isForwardClassDeclaration()
        || symbol->isExtern()
        || symbol->isFriend()
        || symbol->isGenerated()
        || symbol->isUsingNamespaceDirective()
        || symbol->isUsingDeclaration()
        )
        return;

    // skip static local functions
Roberto Raggi's avatar
Roberto Raggi committed
294
//    if ((!symbol->scope() || symbol->scope()->isClass())
295
296
297
298
299
300
301
302
//        && symbol->isStatic() && symbol->isFunction())
//        return;


    const CPlusPlus::Name *symbolName = symbol->name();
    if (symbolName && symbolName->isQualifiedNameId())
        return;

303
304
    QString name = d->overview.prettyName(symbol->name()).trimmed();
    QString type = d->overview.prettyType(symbol->type()).trimmed();
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
    int iconType = CPlusPlus::Icons::iconTypeForSymbol(symbol);

    SymbolInformation information(name, type, iconType);

    ParserTreeItem::Ptr itemAdd;

    // If next line will be removed, 5% speed up for the initial parsing.
    // But there might be a problem for some files ???
    // Better to improve qHash timing
    itemAdd = item->child(information);

    if (itemAdd.isNull())
        itemAdd = ParserTreeItem::Ptr(new ParserTreeItem());

    // locations are 1-based in Symbol, start with 0 for the editor
320
321
    SymbolLocation location(QString::fromUtf8(symbol->fileName() , symbol->fileNameLength()),
                            symbol->line(), symbol->column() - 1);
322
323
324
325
    itemAdd->addSymbolLocation(location);

    // prevent showing a content of the functions
    if (!symbol->isFunction()) {
Roberto Raggi's avatar
Roberto Raggi committed
326
327
328
329
330
331
332
333
334
335
336
337
338
        const CPlusPlus::Scope *scope = symbol->asScope();
        if (scope) {
            CPlusPlus::Scope::iterator cur = scope->firstMember();
            while (cur != scope->lastMember()) {
                const CPlusPlus::Symbol *curSymbol = *cur;
                ++cur;
                if (!curSymbol)
                    continue;

                //                if (!symbol->isClass() && curSymbol->isStatic() && curSymbol->isFunction())
                //                        return;

                addSymbol(itemAdd, curSymbol);
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
            }
        }
    }

    bool appendChild = true;

    // if item is empty and has not to be added
    if (symbol->isNamespace() && itemAdd->childCount() == 0)
        appendChild = false;

    if (appendChild)
        item->appendChild(itemAdd, information);
}

ParserTreeItem::Ptr Parser::createFlatTree(const QStringList &projectList)
{
355
    QReadLocker locker(&d->prjLocker);
356
357

    ParserTreeItem::Ptr item(new ParserTreeItem());
358
    foreach (const QString &project, projectList) {
359
        if (!d->cachedPrjTrees.contains(project))
360
            continue;
361
        ParserTreeItem::ConstPtr list = d->cachedPrjTrees[project];
362
363
364
365
366
367
368
369
370
371
372
373
        item->add(list);
    }
    return item;
}

ParserTreeItem::Ptr Parser::getParseProjectTree(const QStringList &fileList,
                                                const QString &projectId)
{
    //! \todo Way to optimize - for documentUpdate - use old cached project and subtract
    //! changed files only (old edition), and add curent editions
    ParserTreeItem::Ptr item(new ParserTreeItem());
    unsigned revision = 0;
374
    foreach (const QString &file, fileList) {
375
        // ? locker for document?..
376
        const CPlusPlus::Document::Ptr &doc = d->document(file);
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
        if (doc.isNull())
            continue;

        revision += doc->revision();

        ParserTreeItem::ConstPtr list = getCachedOrParseDocumentTree(doc);
        if (list.isNull())
            continue;

        // add list to out document
        item->add(list);
    }

    // update the cache
    if (!projectId.isEmpty()) {
392
        QWriteLocker locker(&d->prjLocker);
393

394
395
        d->cachedPrjTrees[projectId] = item;
        d->cachedPrjTreesRevision[projectId] = revision;
396
397
398
399
400
401
402
    }
    return item;
}

ParserTreeItem::Ptr Parser::getCachedOrParseProjectTree(const QStringList &fileList,
                                                const QString &projectId)
{
403
    d->prjLocker.lockForRead();
404
405

    // calculate current revision
406
    if (!projectId.isEmpty() && d->cachedPrjTrees.contains(projectId)) {
407
408
        // calculate project's revision
        unsigned revision = 0;
409
        foreach (const QString &file, fileList) {
410
            const CPlusPlus::Document::Ptr &doc = d->document(file);
411
412
413
414
415
416
            if (doc.isNull())
                continue;
            revision += doc->revision();
        }

        // if even revision is the same, return cached project
417
418
419
        if (revision == d->cachedPrjTreesRevision[projectId]) {
            d->prjLocker.unlock();
            return d->cachedPrjTrees[projectId];
420
421
422
        }
    }

423
    d->prjLocker.unlock();
424
425
426
427
428
429
430
431
432
    return getParseProjectTree(fileList, projectId);
}

ParserTreeItem::ConstPtr Parser::getParseDocumentTree(const CPlusPlus::Document::Ptr &doc)
{
    if (doc.isNull())
        return ParserTreeItem::ConstPtr();

    const QString &fileName = doc->fileName();
433
    if (!d->fileList.contains(fileName))
434
435
436
437
438
        return ParserTreeItem::ConstPtr();

    ParserTreeItem::Ptr itemPtr(new ParserTreeItem());

    unsigned total = doc->globalSymbolCount();
Friedemann Kleint's avatar
Friedemann Kleint committed
439
    for (unsigned i = 0; i < total; ++i)
440
441
        addSymbol(itemPtr, doc->globalSymbolAt(i));

442
    QWriteLocker locker(&d->docLocker);
443

444
445
446
    d->cachedDocTrees[fileName] = itemPtr;
    d->cachedDocTreesRevision[fileName] = doc->revision();
    d->documentList[fileName] = doc;
447
448
449
450
451
452
453
454
455
456

    return itemPtr;
}

ParserTreeItem::ConstPtr Parser::getCachedOrParseDocumentTree(const CPlusPlus::Document::Ptr &doc)
{
    if (doc.isNull())
        return ParserTreeItem::ConstPtr();

    const QString &fileName = doc->fileName();
457
458
459
460
461
462
    d->docLocker.lockForRead();
    if (d->cachedDocTrees.contains(fileName)
        && d->cachedDocTreesRevision.contains(fileName)
        && d->cachedDocTreesRevision[fileName] == doc->revision()) {
        d->docLocker.unlock();
        return d->cachedDocTrees[fileName];
463
    }
hjk's avatar
hjk committed
464
465
    d->docLocker.unlock();
    return getParseDocumentTree(doc);
466
467
468
469
470
471
472
473
474
475
}

void Parser::parseDocument(const CPlusPlus::Document::Ptr &doc)
{
    if (doc.isNull())
        return;

    const QString &name = doc->fileName();

    // if it is external file (not in any of our projects)
476
    if (!d->fileList.contains(name))
477
478
479
480
        return;

    getParseDocumentTree(doc);

481
    QTC_ASSERT(d->timer, return);
482

483
484
    if (!d->timer->isActive())
        d->timer->start(400); //! Delay in msecs before an update
485
486
487
488
489
    return;
}

void Parser::clearCacheAll()
{
490
    d->docLocker.lockForWrite();
491

492
493
494
    d->cachedDocTrees.clear();
    d->cachedDocTreesRevision.clear();
    d->documentList.clear();
495

496
    d->docLocker.unlock();
497
498
499
500
501
502

    clearCache();
}

void Parser::clearCache()
{
503
    QWriteLocker locker(&d->prjLocker);
504
505

    // remove cached trees
506
    d->cachedPrjFileLists.clear();
507
508
509

    //! \todo where better to clear project's trees?
    //! When file is add/removed from a particular project?..
510
511
    d->cachedPrjTrees.clear();
    d->cachedPrjTreesRevision.clear();
512
513
514
515
}

void Parser::setFileList(const QStringList &fileList)
{
516
517
    d->fileList.clear();
    d->fileList = QSet<QString>::fromList(fileList);
518
519
520
521
522
523
524
}

void Parser::removeFiles(const QStringList &fileList)
{
    if (fileList.count() == 0)
        return;

525
526
    QWriteLocker lockerPrj(&d->prjLocker);
    QWriteLocker lockerDoc(&d->docLocker);
hjk's avatar
hjk committed
527
    foreach (const QString &name, fileList) {
528
529
530
531
532
533
        d->fileList.remove(name);
        d->cachedDocTrees.remove(name);
        d->cachedDocTreesRevision.remove(name);
        d->documentList.remove(name);
        d->cachedPrjTrees.remove(name);
        d->cachedPrjFileLists.clear();
534
535
536
537
538
539
540
541
542
543
    }

    emit filesAreRemoved();
}

void Parser::resetData(const CPlusPlus::Snapshot &snapshot)
{
    // clear internal cache
    clearCache();

544
    d->docLocker.lockForWrite();
545
546
547
548

    // copy snapshot's documents
    CPlusPlus::Snapshot::const_iterator cur = snapshot.begin();
    CPlusPlus::Snapshot::const_iterator end = snapshot.end();
hjk's avatar
hjk committed
549
    for (; cur != end; cur++)
550
        d->documentList[cur.key()] = cur.value();
551

552
    d->docLocker.unlock();
553
554
555
556
557
558

    // recalculate file list
    QStringList fileList;

    // check all projects
    QList<ProjectExplorer::Project *> projects = getProjectList();
hjk's avatar
hjk committed
559
    foreach (const ProjectExplorer::Project *prj, projects) {
560
561
562
563
564
565
566
567
568
569
570
        if (prj)
            fileList += prj->files(ProjectExplorer::Project::ExcludeGeneratedFiles);
    }
    setFileList(fileList);

    emit resetDataDone();
}

void Parser::resetDataToCurrentState()
{
    // get latest data
571
    CppTools::CppModelManagerInterface *codeModel = CppTools::CppModelManagerInterface::instance();
572
573
574
575
576
577
    if (codeModel)
        resetData(codeModel->snapshot());
}

void Parser::onResetDataDone()
{
Tobias Hunger's avatar
Tobias Hunger committed
578
    // internal data is reset, update a tree and send it back
579
580
581
582
583
584
585
586
587
588
589
    emitCurrentTree();
}

void Parser::requestCurrentState()
{
    emitCurrentTree();
}

void Parser::emitCurrentTree()
{
    // stop timer if it is active right now
590
    d->timer->stop();
591

592
593
594
    d->rootItemLocker.lockForWrite();
    d->rootItem = parse();
    d->rootItemLocker.unlock();
595
596
597
598

    // convert
    QSharedPointer<QStandardItem> std(new QStandardItem());

599
    d->rootItem->convertTo(std.data());
600
601
602
603
604
605
606
607
608
609
610
611
612

    emit treeDataUpdate(std);
}

QStringList Parser::projectNodeFileList(const ProjectExplorer::FolderNode *node) const
{
    QStringList list;
    if (!node)
        return list;

    QList<ProjectExplorer::FileNode *> fileNodes = node->fileNodes();
    QList<ProjectExplorer::FolderNode *> subFolderNodes = node->subFolderNodes();

613
    foreach (const ProjectExplorer::FileNode *file, fileNodes) {
614
615
616
617
618
619
        if (file->isGenerated())
            continue;

        list << file->path();
    }

620
    foreach (const ProjectExplorer::FolderNode *folder, subFolderNodes) {
Daniel Teske's avatar
Daniel Teske committed
621
622
        if (folder->nodeType() != ProjectExplorer::FolderNodeType
                && folder->nodeType() != ProjectExplorer::VirtualFolderNodeType)
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
            continue;
        list << projectNodeFileList(folder);
    }

    return list;
}

QStringList Parser::addProjectNode(const ParserTreeItem::Ptr &item,
                                     const ProjectExplorer::ProjectNode *node)
{
    QStringList projectList;
    if (!node)
        return projectList;

    const QString &nodePath = node->path();

    // our own files
    QStringList fileList;

    // try to improve parsing speed by internal cache
643
644
    if (d->cachedPrjFileLists.contains(nodePath)) {
        fileList = d->cachedPrjFileLists[nodePath];
645
646
    } else {
        fileList = projectNodeFileList(node);
647
        d->cachedPrjFileLists[nodePath] = fileList;
648
649
650
651
652
653
654
655
656
    }
    if (fileList.count() > 0) {
        addProject(item, fileList, node->path());
        projectList << node->path();
    }

    // subnodes
    QList<ProjectExplorer::ProjectNode *> projectNodes = node->subProjectNodes();

657
    foreach (const ProjectExplorer::ProjectNode *project, projectNodes) {
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
        ParserTreeItem::Ptr itemPrj(new ParserTreeItem());
        SymbolInformation information(project->displayName(), project->path());

        projectList += addProjectNode(itemPrj, project);

        itemPrj->setIcon(project->icon());

        // append child if item is not null and there is at least 1 child
        if (!item.isNull() && itemPrj->childCount() > 0)
            item->appendChild(itemPrj, information);
    }

    return projectList;
}

QList<ProjectExplorer::Project *> Parser::getProjectList() const
{
    QList<ProjectExplorer::Project *> list;

    // check all projects
    ProjectExplorer::SessionManager *sessionManager
            = ProjectExplorer::ProjectExplorerPlugin::instance()->session();

    list = sessionManager->projects();

    return list;
}

} // namespace Internal
} // namespace ClassView