File: duchaindumper.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 (215 lines) | stat: -rw-r--r-- 6,745 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
/*
    SPDX-FileCopyrightText: 2002-2005 Roberto Raggi <roberto@kdevelop.org>
    SPDX-FileCopyrightText: 2006 Hamish Rodda <rodda@kde.org>
    SPDX-FileCopyrightText: 2010 Milian Wolff <mail@milianw.de>
    SPDX-FileCopyrightText: 2014 Kevin Funk <kfunk@kde.org>

    SPDX-License-Identifier: LGPL-2.0-only
*/

#include "duchaindumper.h"

#include <QString>
#include <QTextStream>

#include "definitions.h"
#include "ducontext.h"
#include "topducontext.h"
#include "declaration.h"
#include "duchainpointer.h"
#include "identifier.h"
#include "use.h"
#include "problem.h"
#include <serialization/indexedstring.h>
#include "functiondefinition.h"

#include <editor/rangeinrevision.h>
#include <editor/documentrange.h>

namespace {
QDebug fromTextStream(const QTextStream& out)
{
    if (out.device())
        return {
                   out.device()
        }; return {
               out.string()
    };
}

}

namespace KDevelop {
QString typeToString(DUContext::ContextType type)
{
    switch (type) {
    case DUContext::Global: return QStringLiteral("Global");
    case DUContext::Namespace: return QStringLiteral("Namespace");
    case DUContext::Class: return QStringLiteral("Class");
    case DUContext::Function: return QStringLiteral("Function");
    case DUContext::Template: return QStringLiteral("Template");
    case DUContext::Enum: return QStringLiteral("Enum");
    case DUContext::Helper: return QStringLiteral("Helper");
    case DUContext::Other: return QStringLiteral("Other");
    }
    Q_ASSERT(false);
    return QString();
}

class DUChainDumperPrivate
{
public:
    DUChainDumperPrivate()
        : m_indent(0)
    {}

    void dumpProblems(TopDUContext* top, QTextStream& out);
    void dump(DUContext* context, int allowedDepth, bool isFromImport, QTextStream& out);

    int m_indent;
    DUChainDumper::Features m_features;
    QSet<DUContext*> m_visitedContexts;
};

DUChainDumper::DUChainDumper(Features features)
    : d_ptr(new DUChainDumperPrivate)
{
    Q_D(DUChainDumper);

    d->m_features = features;
}

DUChainDumper::~DUChainDumper()
{
}

class Indent
{
public:
    explicit Indent(int level) : m_level(level) {}

    friend QDebug& operator<<(QDebug& debug, const Indent& ind)
    {
        for (int i = 0; i < ind.m_level; i++) {
            debug.nospace() << ' ';
        }

        return debug.space();
    }

private:
    int m_level;
};

void DUChainDumperPrivate::dumpProblems(TopDUContext* top, QTextStream& out)
{
    QDebug qout = fromTextStream(out);

    if (!top->problems().isEmpty()) {
        qout << top->problems().size() << "problems encountered:" << Qt::endl;
        const auto problems = top->problems();
        for (const ProblemPointer& p : problems) {
            qout << Indent(m_indent * 2) << p->description() << p->explanation() << p->finalLocation() << Qt::endl;
        }

        qout << Qt::endl;
    }
}

void DUChainDumperPrivate::dump(DUContext* context, int allowedDepth, bool isFromImport, QTextStream& out)
{
    QDebug qout = fromTextStream(out);

    qout << Indent(m_indent * 2) << (isFromImport ? " ==import==>" : "")
         << (dynamic_cast<TopDUContext*>(context) ? "Top-Context" : "Context") << typeToString(context->type())
         << "(owner: " << context->owner() << ")" << context << context->localScopeIdentifier() << "["
         << context->scopeIdentifier(true) << "]" << context->range().castToSimpleRange()
         << (dynamic_cast<TopDUContext*>(context) ? static_cast<TopDUContext*>(context)->url().byteArray() : "")
         << Qt::endl;

    if (m_visitedContexts.contains(context)) {
        qout << Indent((m_indent + 2) * 2) << "(Skipping" << context->scopeIdentifier(true)
             << "because it was already printed)" << Qt::endl;
        return;
    }

    m_visitedContexts.insert(context);

    auto top = context->topContext();
    if (allowedDepth >= 0) {
        const auto localDeclarations = context->localDeclarations(top);
        for (Declaration* dec : localDeclarations) {
            //IdentifiedType* idType = dynamic_cast<IdentifiedType*>(dec->abstractType().data());

            qout << Indent((m_indent + 2) * 2) << "Declaration:" << dec->toString() << "[" << dec->qualifiedIdentifier()
                 << "]" << dec << "(internal ctx:" << dec->internalContext() << ")" << dec->range().castToSimpleRange()
                 << ","
                 << (dec->isDefinition() ? "defined, " : (FunctionDefinition::definition(dec) ? "" : "no definition, "))
                 << dec->uses().count() << "use(s)." << Qt::endl;
            if (FunctionDefinition::definition(dec)) {
                qout << Indent((m_indent + 2) * 2 + 1)
                     << "Definition:" << FunctionDefinition::definition(dec)->range().castToSimpleRange() << Qt::endl;
            }
            const auto uses = dec->uses();
            for (auto it = uses.constBegin(); it != uses.constEnd(); ++it) {
                qout << Indent((m_indent + 3) * 2) << "File:" << it.key().str() << Qt::endl;
                for (const RangeInRevision range : qAsConst(*it)) {
                    qout << Indent((m_indent + 4) * 2) << "Use:" << range.castToSimpleRange() << Qt::endl;
                }
            }
        }
    } else {
        qout << Indent((m_indent + 2) * 2) << context->localDeclarations(top).count() << "Declarations,"
             << context->childContexts().size() << "child-contexts" << Qt::endl;
    }

    ++m_indent;
    {
        /*
           const auto importedParentContexts = context->importedParentContexts();
           for (const DUContext::Import& parent : importedParentContexts) {
           DUContext* import = parent.context(top);
           if(!import) {
              qout << Indent((m_indent+2) * 2+1) << "Could not get parent, is it registered in the DUChain?" << Qt::endl;
              continue;
           }

           dump(import, allowedDepth-1, true, out);
           }
         */

        const auto childContexts = context->childContexts();
        for (DUContext* child : childContexts) {
            dump(child, allowedDepth - 1, false, out);
        }
    }
    --m_indent;
}

void DUChainDumper::dump(DUContext* context, int allowedDepth, QTextStream& out)
{
    Q_D(DUChainDumper);

    d->m_visitedContexts.clear();

    if (!context) {
        out << "Error: Null context" << Qt::endl;
        return;
    }

    auto top = context->topContext();
    if (d->m_features.testFlag(DumpProblems)) {
        d->dumpProblems(top, out);
    }
    if (d->m_features.testFlag(DumpContext)) {
        d->dump(context, allowedDepth, false, out);
    }
}

void DUChainDumper::dump(DUContext* context, int allowedDepth)
{
    QTextStream out(stdout);
    dump(context, allowedDepth, out);
}

}