File: main.cpp

package info (click to toggle)
kdevelop 4%3A22.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 70,096 kB
  • sloc: cpp: 284,635; javascript: 3,558; python: 3,422; sh: 1,319; ansic: 685; xml: 331; php: 95; lisp: 66; makefile: 39; sed: 12
file content (318 lines) | stat: -rw-r--r-- 12,241 bytes parent folder | download
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
/*
    SPDX-FileCopyrightText: 2009 David Nolden <david.nolden.kdevelop@art-master.de>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "main.h"

#include <shell/core.h>
#include <shell/shellextension.h>

#include <language/backgroundparser/backgroundparser.h>
#include <language/duchain/definitions.h>
#include <language/duchain/duchain.h>
#include <language/duchain/duchainlock.h>
#include <language/duchain/duchaindumper.h>
#include <language/duchain/dumpdotgraph.h>
#include <language/duchain/problem.h>
#include <language/duchain/persistentsymboltable.h>

#include <interfaces/ilanguagecontroller.h>
#include <tests/autotestshell.h>
#include <tests/testcore.h>

#include <QCoreApplication>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <QDebug>
#include <QDirIterator>
#include <QStringList>
#include <QTimer>

#include <cstdio>

#include <KAboutData>
#include <KLocalizedString>

bool verbose = false, warnings = false;

using namespace KDevelop;

void messageOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{
    Q_UNUSED(context);

    switch (type) {
    case QtInfoMsg:     // fall-through
    case QtDebugMsg:
        if (verbose)
            std::cerr << qPrintable(msg) << std::endl;
        break;
    case QtWarningMsg:
        if (warnings)
            std::cerr << qPrintable(msg) << std::endl;
        break;
    case QtCriticalMsg:
        std::cerr << qPrintable(msg) << std::endl;
        break;
    case QtFatalMsg:
        std::cerr << qPrintable(msg) << std::endl;
        abort();
    }
}

Manager::Manager(QCommandLineParser* args) : m_total(0)
    , m_args(args)
    , m_allFilesAdded(0)
{
}

void Manager::init()
{
    if (m_args->positionalArguments().isEmpty()) {
        std::cerr << "Need file or directory to duchainify" << std::endl;
        QCoreApplication::exit(1);
    }

    TopDUContext::Features features = TopDUContext::VisibleDeclarationsAndContexts;
    if (m_args->isSet(QStringLiteral("features"))) {
        QString featuresStr = m_args->value(QStringLiteral("features"));
        if (featuresStr == QLatin1String("visible-declarations")) {
            features = TopDUContext::VisibleDeclarationsAndContexts;
        } else if (featuresStr == QLatin1String("all-declarations")) {
            features = TopDUContext::AllDeclarationsAndContexts;
        } else if (featuresStr == QLatin1String("all-declarations-and-uses")) {
            features = TopDUContext::AllDeclarationsContextsAndUses;
        } else if (featuresStr == QLatin1String("all-declarations-and-uses-and-AST")) {
            features = TopDUContext::AllDeclarationsContextsAndUses | TopDUContext::AST;
        } else if (featuresStr == QLatin1String("empty")) {
            features = TopDUContext::Empty;
        } else if (featuresStr == QLatin1String("simplified-visible-declarations")) {
            features = TopDUContext::SimplifiedVisibleDeclarationsAndContexts;
        } else {
            std::cerr << "Wrong feature-string given\n";
            QCoreApplication::exit(2);
            return;
        }
    }
    if (m_args->isSet(QStringLiteral("force-update")))
        features |= TopDUContext::ForceUpdate;
    if (m_args->isSet(QStringLiteral("force-update-recursive")))
        features |= TopDUContext::ForceUpdateRecursive;

    if (m_args->isSet(QStringLiteral("threads"))) {
        bool ok = false;
        int count = m_args->value(QStringLiteral("threads")).toInt(&ok);
        ICore::self()->languageController()->backgroundParser()->setThreadCount(count);
        if (!ok) {
            std::cerr << "bad thread count\n";
            QCoreApplication::exit(3);
            return;
        }
    }

    // quit when everything is done
    // background parser emits hideProgress() signal in two situations:
    // when everything is done and when bgparser is suspended
    // later doesn't happen in duchain, so just rely on hideProgress()
    // and quit when it's emitted
    connect(
        ICore::self()->languageController()->backgroundParser(), &BackgroundParser::hideProgress, this,
        [this]() {
            if (ICore::self()->languageController()->backgroundParser()->isIdle())
                QTimer::singleShot(0, this, &Manager::finish);
        });

    const auto files = m_args->positionalArguments();
    for (const auto& file : files) {
        addToBackgroundParser(file, features);
    }

    m_allFilesAdded = 1;

    if (m_total) {
        std::cerr << "Added " << m_total << " files to the background parser" << std::endl;
        const int threads = ICore::self()->languageController()->backgroundParser()->threadCount();
        std::cerr << "parsing with " << threads << " threads" << std::endl;
        ICore::self()->languageController()->backgroundParser()->parseDocuments();
    } else {
        std::cerr << "no files added to the background parser" << std::endl;
        QCoreApplication::exit(0);
        return;
    }
}

void Manager::updateReady(const IndexedString& url, const ReferencedTopDUContext& topContext)
{
    qDebug() << "finished" << url.toUrl().toLocalFile() << "success: " << ( bool )topContext;

    m_waiting.remove(url.toUrl());

    std::cerr << "processed " << (m_total - m_waiting.size()) << " out of " << m_total << "\n";
    dump(topContext);
}

void Manager::dump(const ReferencedTopDUContext& topContext)
{
    if (!topContext) {
        return;
    }

    QTextStream stream(stdout);

    std::cerr << "\n";

    if (m_args->isSet(QStringLiteral("dump-definitions"))) {
        DUChainReadLocker lock;
        std::cerr << "Definitions:" << std::endl;
        DUChain::definitions()->dump(stream);
        std::cerr << std::endl;
    }

    if (m_args->isSet(QStringLiteral("dump-symboltable"))) {
        DUChainReadLocker lock;
        std::cerr << "PersistentSymbolTable:" << std::endl;
        PersistentSymbolTable::self().dump(stream);
        std::cerr << std::endl;
    }

    DUChainDumper::Features features;
    if (m_args->isSet(QStringLiteral("dump-context"))) {
        features |= DUChainDumper::DumpContext;
    }
    if (m_args->isSet(QStringLiteral("dump-errors"))) {
        features |= DUChainDumper::DumpProblems;
    }

    if (auto depth = m_args->value(QStringLiteral("dump-depth")).toInt()) {
        DUChainReadLocker lock;
        std::cerr << "Context:" << std::endl;
        DUChainDumper dumpChain(features);
        dumpChain.dump(topContext, depth);
    }

    if (m_args->isSet(QStringLiteral("dump-graph"))) {
        DUChainReadLocker lock;
        DumpDotGraph dumpGraph;
        const QString dotOutput = dumpGraph.dotGraph(topContext);
        std::cout << qPrintable(dotOutput) << std::endl;
    }

    if (m_args->isSet(QStringLiteral("dump-imported-errors"))) {
        DUChainReadLocker lock;
        const auto imports = topContext->importedParentContexts();
        for (const auto& import : imports) {
            auto top = dynamic_cast<TopDUContext*>(import.indexedContext().context());
            if (top && top != topContext && !top->problems().isEmpty()) {
                DUChainDumper dumpChain(DUChainDumper::DumpProblems);
                dumpChain.dump(top, 0);
            }
        }
    }
}

void Manager::addToBackgroundParser(const QString& path, TopDUContext::Features features)
{
    QFileInfo info(path);

    if (info.isFile()) {
        qDebug() << "adding file" << path;
        QUrl pathUrl = QUrl::fromLocalFile(info.canonicalFilePath());

        m_waiting << pathUrl;
        ++m_total;

        KDevelop::DUChain::self()->updateContextForUrl(KDevelop::IndexedString(pathUrl), features, this);

    } else if (info.isDir()) {
        QDirIterator contents(path);
        while (contents.hasNext()) {
            QString newPath = contents.next();
            if (!newPath.endsWith(QLatin1Char('.')))
                addToBackgroundParser(newPath, features);
        }
    }
}

QSet<QUrl> Manager::waiting()
{
    return m_waiting;
}

void Manager::finish()
{
    std::cerr << "ready" << std::endl;
    QCoreApplication::quit();
}

using namespace KDevelop;

int main(int argc, char** argv)
{
    QCoreApplication app(argc, argv);

    KAboutData aboutData(QStringLiteral("duchainify"), i18n("duchainify"),
        QStringLiteral("1"), i18n("DUChain builder application"), KAboutLicense::GPL,
        i18n("(c) 2009 David Nolden"), QString(), QStringLiteral("https://www.kdevelop.org/"));
    KAboutData::setApplicationData(aboutData);

    QCommandLineParser parser;
    aboutData.setupCommandLine(&parser);

    parser.addPositionalArgument(QStringLiteral("paths"), i18n("file or directory"), QStringLiteral("[PATH...]"));

    parser.addOption(QCommandLineOption{QStringList{QStringLiteral("w"), QStringLiteral("warnings")},
                                        i18n("Show warnings")});
    parser.addOption(QCommandLineOption{QStringList{QStringLiteral("V"), QStringLiteral("verbose")},
                                        i18n("Show warnings and debug output")});
    parser.addOption(QCommandLineOption{QStringList{QStringLiteral("u"), QStringLiteral("force-update")},
                                        i18n("Enforce an update of the top-contexts corresponding to the given files")});
    parser.addOption(QCommandLineOption{QStringList{QStringLiteral("r"), QStringLiteral(
                                                        "force-update-recursive")},
                                        i18n(
                                            "Enforce an update of the top-contexts corresponding to the given files and all included files")});
    parser.addOption(QCommandLineOption{QStringList{QStringLiteral("t"), QStringLiteral("threads")},
                                        i18n("Number of threads to use"), QStringLiteral("count")});
    parser.addOption(QCommandLineOption{QStringList{QStringLiteral("f"), QStringLiteral("features")},
                                        i18n(
                                            "Features to build. Options: empty, simplified-visible-declarations, visible-declarations (default), all-declarations, all-declarations-and-uses, all-declarations-and-uses-and-AST"),
                                        QStringLiteral("features")});

    parser.addOption(QCommandLineOption{QStringList{QStringLiteral("dump-context")},
                                        i18n("Print complete Definition-Use Chain on successful parse")});
    parser.addOption(QCommandLineOption{QStringList{QStringLiteral("dump-definitions")},
                                        i18n("Print complete DUChain Definitions repository on successful parse")});
    parser.addOption(QCommandLineOption{QStringList{QStringLiteral("dump-symboltable")},
                                        i18n(
                                            "Print complete DUChain PersistentSymbolTable repository on successful parse")});
    parser.addOption(
        QCommandLineOption { QStringList { QStringLiteral("dump-depth") },
                             i18n("Number defining the maximum depth where declaration details are printed"),
                             QStringLiteral("depth"), QStringLiteral("100") });
    parser.addOption(QCommandLineOption{QStringList{QStringLiteral("dump-graph")},
                                        i18n("Dump DUChain graph (in .dot format)")});
    parser.addOption(QCommandLineOption{QStringList{QStringLiteral("d"), QStringLiteral("dump-errors")},
                                        i18n("Print problems encountered during parsing")});
    parser.addOption(QCommandLineOption{QStringList{QStringLiteral("dump-imported-errors")},
                                        i18n("Recursively dump errors from imported contexts.")});

    parser.process(app);

    aboutData.processCommandLine(&parser);

    verbose = parser.isSet(QStringLiteral("verbose"));
    warnings = parser.isSet(QStringLiteral("warnings"));
    qInstallMessageHandler(messageOutput);

    AutoTestShell::init();
    TestCore::initialize(Core::NoUi, QStringLiteral("duchainify"));
    Manager manager(&parser);

    QTimer::singleShot(0, &manager, &Manager::init);
    int ret = app.exec();

    TestCore::shutdown();

    return ret;
}