File: dumpdotgraph.cpp

package info (click to toggle)
kdevelop 4%3A24.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 71,888 kB
  • sloc: cpp: 290,869; python: 3,626; javascript: 3,518; sh: 1,316; ansic: 703; xml: 401; php: 95; lisp: 66; makefile: 31; sed: 12
file content (212 lines) | stat: -rw-r--r-- 7,667 bytes parent folder | download | duplicates (3)
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
/*
    SPDX-FileCopyrightText: 2007 David Nolden <david.nolden.kdevelop@art-master.de>

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

#include "dumpdotgraph.h"

#include "ducontext.h"
#include "topducontext.h"
#include "declaration.h"
#include "duchainpointer.h"
#include "parsingenvironment.h"
#include "identifier.h"
#include "functiondefinition.h"

namespace KDevelop {
QString shortLabel(KDevelop::DUContext* context)
{
    return QStringLiteral("q%1").arg(( quint64 )context);
}

QString shortLabel(KDevelop::Declaration* declaration)
{
    return QStringLiteral("q%1").arg(( quint64 )declaration);
}

QString rangeToString(const KTextEditor::Range& r)
{
    return QStringLiteral("%1:%2->%3:%4").arg(r.start().line()).arg(r.start().column()).arg(r.end().line()).arg(
        r.end().column());
}

class DumpDotGraphPrivate
{
public:

    QString dotGraphInternal(KDevelop::DUContext* contex, bool isMaster, bool shortened);

    void addDeclaration(QTextStream& stream, Declaration* decl);

    QMap<QUrl, QString> m_hadVersions;
    QMap<KDevelop::DUChainBase*, bool> m_hadObjects;
    TopDUContext* m_topContext;
};

QString DumpDotGraph::dotGraph(KDevelop::DUContext* context, bool shortened)
{
    Q_D(DumpDotGraph);

    d->m_hadObjects.clear();
    d->m_hadVersions.clear();
    d->m_topContext = context->topContext(); ///@todo maybe get this as a parameter
    return d->dotGraphInternal(context, true, shortened);
}

DumpDotGraph::DumpDotGraph()
    : d_ptr(new DumpDotGraphPrivate())
{
}

DumpDotGraph::~DumpDotGraph() = default;

void DumpDotGraphPrivate::addDeclaration(QTextStream& stream, Declaration* dec)
{
    if (m_hadObjects.contains(dec))
        return;

    m_hadObjects[dec] = true;

    Declaration* declarationForDefinition = nullptr;
    if (dynamic_cast<FunctionDefinition*>(dec))
        declarationForDefinition = static_cast<FunctionDefinition*>(dec)->declaration(m_topContext);

    if (!declarationForDefinition) {
        //Declaration
        stream << shortLabel(dec) <<
            "[shape=box, label=\"" <<
            dec->toString() << " [" <<
            dec->qualifiedIdentifier().toString() << "]" << " " <<
            rangeToString(dec->range().castToSimpleRange()) << "\"];\n";
        stream << shortLabel(dec->context()) << " -> " << shortLabel(dec) << "[color=green];\n";
        if (dec->internalContext())
            stream << shortLabel(dec) << " -> " << shortLabel(dec->internalContext()) <<
                "[label=\"internal\", color=blue];\n";
    } else {
        //Definition
        stream << shortLabel(dec) <<  "[shape=regular,color=yellow,label=\"" << declarationForDefinition->toString() <<
            " " << rangeToString(dec->range().castToSimpleRange()) <<  "\"];\n";
        stream << shortLabel(dec->context()) << " -> " << shortLabel(dec) << ";\n";

        stream << shortLabel(dec) << " -> " << shortLabel(declarationForDefinition) <<
            "[label=\"defines\",color=green];\n";
        addDeclaration(stream, declarationForDefinition);

        if (dec->internalContext())
            stream << shortLabel(dec) << " -> " << shortLabel(dec->internalContext()) <<
                "[label=\"internal\", color=blue];\n";
    }
}

QString DumpDotGraphPrivate::dotGraphInternal(KDevelop::DUContext* context, bool isMaster, bool shortened)
{
    if (m_hadObjects.contains(context))
        return QString();

    m_hadObjects[context] = true;

    QTextStream stream;
    QString ret;
    stream.setString(&ret, QIODevice::WriteOnly);

    if (isMaster)
        stream << "Digraph chain {\n";

    QString shape = QStringLiteral("parallelogram");
    //QString shape = "box";
    QString label = QStringLiteral("unknown");

    if (dynamic_cast<TopDUContext*>(context)) {
        auto* topCtx = static_cast<TopDUContext*>(context);
        if (topCtx->parsingEnvironmentFile()) {
            QUrl url = topCtx->parsingEnvironmentFile()->url().toUrl();
            const QString fileName = url.fileName();
            QString file = url.toDisplayString(QUrl::PreferLocalFile);
            if (topCtx->parsingEnvironmentFile() && topCtx->parsingEnvironmentFile()->isProxyContext())
                url.setPath(url.path() + QLatin1String("/_[proxy]_"));

            //Find the context this one is derived from. If there is one, connect it with a line, and shorten the url.
            if (m_hadVersions.contains(url)) {
                stream << shortLabel(context) << " -> " << m_hadVersions[url] << "[color=blue,label=\"version\"];\n";
                file = fileName;
            } else {
                m_hadVersions[url] = shortLabel(context);
            }

            label = file;

            if (topCtx->importers().count() != 0)
                label += QStringLiteral(" imported by %1").arg(topCtx->importers().count());
        } else {
            label = QStringLiteral("unknown file");
        }
        if (topCtx->parsingEnvironmentFile() && topCtx->parsingEnvironmentFile()->isProxyContext())
            label = QLatin1String("Proxy-context ") + label;
    } else {
        label = /*"context " + */ context->localScopeIdentifier().toString();
        label += QLatin1Char(' ') + rangeToString(context->range().castToSimpleRange());
    }

    //label = QStringLiteral("%1 ").arg((size_t)context) + label;

    if (isMaster && !dynamic_cast<TopDUContext*>(context)) {
        //Also draw contexts that import this one
        const auto importers = context->importers();
        for (DUContext* ctx : importers) {
            stream << dotGraphInternal(ctx, false, true);
        }
    }
    const auto importedParentContexts = context->importedParentContexts();
    for (const DUContext::Import& parent : importedParentContexts) {
        if (parent.context(m_topContext)) {
            stream << dotGraphInternal(parent.context(m_topContext), false, true);
            QString label = QStringLiteral("imports");
            if ((!dynamic_cast<TopDUContext*>(parent.context(m_topContext)) || !dynamic_cast<TopDUContext*>(context)) &&
                !(parent.context(m_topContext)->url() == context->url())) {
                label += QLatin1String(" from ") + parent.context(m_topContext)->url().toUrl().fileName()
                         + QLatin1String(" to ") + context->url().toUrl().fileName();
            }

            stream << shortLabel(context) << QLatin1String(" -> ") << shortLabel(parent.context(m_topContext)) <<
                QLatin1String("[style=dotted,label=\"") << label  << QLatin1String("\"];\n");
        }
    }

    const auto childContexts = context->childContexts();
    if (!childContexts.isEmpty()) {
        label += QStringLiteral(", %1 C.").arg(childContexts.count());
    }

    if (!shortened) {
        for (DUContext* child : childContexts) {
            stream << dotGraphInternal(child, false, false);
            stream << shortLabel(context) << QLatin1String(" -> ") << shortLabel(child) << QLatin1String(
                "[style=dotted,color=green];\n");
        }
    }

    const auto localDeclarations = context->localDeclarations();
    if (!localDeclarations.isEmpty()) {
        label += QStringLiteral(", %1 D.").arg(localDeclarations.count());
    }

    if (!shortened) {
        for (Declaration* dec : localDeclarations) {
            addDeclaration(stream, dec);
        }
    }

    if (context->owner()) {
        addDeclaration(stream, context->owner());
    }

    stream << shortLabel(context) << "[shape=" << shape << ",label=\"" << label << "\"" <<
    (isMaster ? QLatin1String("color=red") : QLatin1String("color=blue")) << "];\n";

    if (isMaster)
        stream << "}\n";

    return ret;
}
}