aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/classview/classviewparser.cpp
blob: 4ebcca5e7dcd84058dad9fe2a6c869b5d5c4d7e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
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
137
138
139
140
141
142
143
144
145
146
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Copyright (C) 2016 Denis Mingulov
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "classviewparser.h"

#include <cppeditor/cppmodelmanager.h>

#include <QElapsedTimer>
#include <QDebug>
#include <QHash>
#include <QSet>

enum { debug = false };

using namespace ProjectExplorer;
using namespace Utils;

namespace ClassView {
namespace Internal {

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

/*!
   \class ParserPrivate
   \brief The ParserPrivate class defines private class data for the Parser
   class.
   \sa Parser
 */

/*!
   \class Parser
   \brief The Parser class parses C++ information. Multithreading is supported.
*/

class ParserPrivate
{
public:
    //! Get document from documentList
    CPlusPlus::Document::Ptr document(const FilePath &fileName) const;

    struct DocumentCache {
        unsigned treeRevision = 0;
        ParserTreeItem::ConstPtr tree;
        CPlusPlus::Document::Ptr document;
    };
    struct ProjectCache {
        unsigned treeRevision = 0;
        ParserTreeItem::ConstPtr tree;
        QString projectName;
        QSet<FilePath> fileNames;
    };

    // Project file path to its cached data
    QHash<FilePath, DocumentCache> m_documentCache;
    // Project file path to its cached data
    QHash<FilePath, ProjectCache> m_projectCache;

    //! Flat mode
    bool flatMode = false;
};

CPlusPlus::Document::Ptr ParserPrivate::document(const FilePath &fileName) const
{
    return m_documentCache.value(fileName).document;
}

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

/*!
    Constructs the parser object.
*/

Parser::Parser(QObject *parent)
    : QObject(parent),
    d(new ParserPrivate())
{
}

/*!
    Destructs the parser object.
*/

Parser::~Parser()
{
    delete d;
}

/*!
    Switches to flat mode (without subprojects) if \a flat returns \c true.
*/

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

    // change internal
    d->flatMode = flatMode;

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

/*!
    Parses the class and produces a new tree.

    \sa addProject
*/

ParserTreeItem::ConstPtr Parser::parse()
{
    QScopedPointer<QElapsedTimer> timer;
    if (debug) {
        timer.reset(new QElapsedTimer());
        timer->start();
    }

    QHash<SymbolInformation, ParserTreeItem::ConstPtr> projectTrees;

    for (auto it = d->m_projectCache.cbegin(); it != d->m_projectCache.cend(); ++it) {
        const ParserPrivate::ProjectCache &projectCache = it.value();
        const FilePath projectPath = it.key();
        const SymbolInformation projectInfo = { projectCache.projectName, projectPath.toString() };
        ParserTreeItem::ConstPtr item = getCachedOrParseProjectTree(projectPath, projectCache.fileNames);
        if (!item)
            continue;
        projectTrees.insert(projectInfo, item);
    }

    ParserTreeItem::ConstPtr rootItem(new ParserTreeItem(projectTrees));

    if (debug) {
        qDebug() << "Class View:" << QDateTime::currentDateTime().toString()
                 << "Parsed in " << timer->elapsed() << "msecs.";
    }

    return rootItem;
}

/*!
    Parses the project with the \a projectId and adds the documents from the
    \a fileList to the project. Updates the internal cached tree for this
    project.
*/

ParserTreeItem::ConstPtr Parser::getParseProjectTree(const FilePath &projectPath,
                                                     const QSet<FilePath> &filesInProject)
{
    //! \todo Way to optimize - for documentUpdate - use old cached project and subtract
    //! changed files only (old edition), and add curent editions

    QList<ParserTreeItem::ConstPtr> docTrees;
    unsigned revision = 0;
    for (const FilePath &fileInProject : filesInProject) {
        const CPlusPlus::Document::Ptr &doc = d->document(fileInProject);
        if (doc.isNull())
            continue;

        revision += doc->revision();

        const ParserTreeItem::ConstPtr docTree = getCachedOrParseDocumentTree(doc);
        if (!docTree)
            continue;
        docTrees.append(docTree);
    }

    ParserTreeItem::ConstPtr item = ParserTreeItem::mergeTrees(projectPath, docTrees);

    // update the cache
    if (!projectPath.isEmpty()) {
        ParserPrivate::ProjectCache &projectCache = d->m_projectCache[projectPath];
        projectCache.tree = item;
        projectCache.treeRevision = revision;
    }
    return item;
}

/*!
    Gets the project with \a projectId from the cache if it is valid or parses
    the project and adds the documents from the \a fileList to the project.
    Updates the internal cached tree for this project.
*/

ParserTreeItem::ConstPtr Parser::getCachedOrParseProjectTree(const FilePath &projectPath,
                                                             const QSet<FilePath> &filesInProject)
{
    const auto it = d->m_projectCache.constFind(projectPath);
    if (it != d->m_projectCache.constEnd() && it.value().tree) {
        // calculate project's revision
        unsigned revision = 0;
        for (const FilePath &fileInProject : filesInProject) {
            const CPlusPlus::Document::Ptr &doc = d->document(fileInProject);
            if (doc.isNull())
                continue;
            revision += doc->revision();
        }

        // if even revision is the same, return cached project
        if (revision == it.value().treeRevision)
            return it.value().tree;
    }

    return getParseProjectTree(projectPath, filesInProject);
}

/*!
    Parses the document \a doc if it is in the project files and adds a tree to
    the internal storage. Updates the internal cached tree for this document.

    \sa parseDocument
*/

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

    const FilePath fileName = doc->filePath();

    ParserTreeItem::ConstPtr itemPtr = ParserTreeItem::parseDocument(doc);

    d->m_documentCache.insert(fileName, { doc->revision(), itemPtr, doc } );
    return itemPtr;
}

/*!
    Gets the document \a doc from the cache or parses it if it is in the project
    files and adds a tree to the internal storage.

    \sa parseDocument
*/

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

    const auto it = d->m_documentCache.constFind(doc->filePath());
    if (it != d->m_documentCache.constEnd() && it.value().tree
            && it.value().treeRevision == doc->revision()) {
        return it.value().tree;
    }
    return getParseDocumentTree(doc);
}

/*!
    Parses the document list \a docs if they are in the project files and adds a tree to
    the internal storage.
*/

void Parser::updateDocuments(const QSet<FilePath> &documentPaths)
{
    updateDocumentsFromSnapshot(documentPaths, CppEditor::CppModelManager::snapshot());
}

void Parser::updateDocumentsFromSnapshot(const QSet<FilePath> &documentPaths,
                                 const CPlusPlus::Snapshot &snapshot)
{
    for (const FilePath &documentPath : documentPaths) {
        CPlusPlus::Document::Ptr doc = snapshot.document(documentPath);
        if (doc.isNull())
            continue;

        getParseDocumentTree(doc);
    }
    requestCurrentState();
}

/*!
    Removes the files defined in the \a fileList from the parsing.
*/

void Parser::removeFiles(const QStringList &fileList)
{
    if (fileList.isEmpty())
        return;

    for (const QString &name : fileList) {
        const FilePath filePath = FilePath::fromString(name);
        d->m_documentCache.remove(filePath);
        d->m_projectCache.remove(filePath);
        for (auto it = d->m_projectCache.begin(); it != d->m_projectCache.end(); ++it)
            it.value().fileNames.remove(filePath);
    }
    requestCurrentState();
}

/*!
    Fully resets the internal state of the code parser to \a snapshot.
*/
void Parser::resetData(const QHash<FilePath, QPair<QString, FilePaths>> &projects)
{
    d->m_projectCache.clear();
    d->m_documentCache.clear();

    const CPlusPlus::Snapshot &snapshot = CppEditor::CppModelManager::snapshot();
    for (auto it = projects.cbegin(); it != projects.cend(); ++it) {
        const auto projectData = it.value();
        QSet<FilePath> commonFiles;
        for (const auto &fileInProject : projectData.second) {
            CPlusPlus::Document::Ptr doc = snapshot.document(fileInProject);
            if (doc.isNull())
                continue;
            commonFiles.insert(fileInProject);
            d->m_documentCache[fileInProject].document = doc;
        }
        d->m_projectCache.insert(it.key(), { 0, nullptr, projectData.first, commonFiles });
    }

    requestCurrentState();
}

void Parser::addProject(const FilePath &projectPath, const QString &projectName,
                        const FilePaths &filesInProject)
{
    const CPlusPlus::Snapshot &snapshot = CppEditor::CppModelManager::snapshot();
    QSet<FilePath> commonFiles;
    for (const auto &fileInProject : filesInProject) {
        CPlusPlus::Document::Ptr doc = snapshot.document(fileInProject);
        if (doc.isNull())
            continue;
        commonFiles.insert(fileInProject);
        d->m_documentCache[fileInProject].document = doc;
    }
    d->m_projectCache.insert(projectPath, { 0, nullptr, projectName, commonFiles });
    updateDocumentsFromSnapshot(commonFiles, snapshot);
}

void Parser::removeProject(const FilePath &projectPath)
{
    auto it = d->m_projectCache.find(projectPath);
    if (it == d->m_projectCache.end())
        return;

    const QSet<FilePath> &filesInProject = it.value().fileNames;
    for (const FilePath &fileInProject : filesInProject)
        d->m_documentCache.remove(fileInProject);

    d->m_projectCache.erase(it);

    requestCurrentState();
}

/*!
    Requests to emit a signal with the current tree state.
*/
void Parser::requestCurrentState()
{
    emit treeRegenerated(parse());
}

} // namespace Internal
} // namespace ClassView