• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdevelop API Reference
  • KDE Home
  • Contact Us
 

kdevplatform/language/duchain

  • sources
  • kfour-appscomplete
  • kdevelop
  • kdevplatform
  • language
  • duchain
duchaindumper.cpp
Go to the documentation of this file.
1 /* This file is part of KDevelop
2  Copyright 2002-2005 Roberto Raggi <[email protected]>
3  Copyright 2006 Hamish Rodda <[email protected]>
4  Copyright 2010 Milian Wolff <[email protected]>
5  Copyright 2014 Kevin Funk <[email protected]>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License version 2 as published by the Free Software Foundation.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20  */
21 
22 #include "duchaindumper.h"
23 
24 #include <QString>
25 #include <QTextStream>
26 
27 #include "definitions.h"
28 #include "ducontext.h"
29 #include "topducontext.h"
30 #include "declaration.h"
31 #include "duchainpointer.h"
32 #include "identifier.h"
33 #include "use.h"
34 #include "problem.h"
35 #include <serialization/indexedstring.h>
36 #include "functiondefinition.h"
37 
38 #include <editor/rangeinrevision.h>
39 #include <editor/documentrange.h>
40 
41 namespace {
42 QDebug fromTextStream(const QTextStream& out)
43 {
44  if (out.device())
45  return {
46  out.device()
47  }; return {
48  out.string()
49  };
50 }
51 
52 }
53 
54 namespace KDevelop {
55 
56 using TextStreamFunction = QTextStream& (*)(QTextStream&);
57 #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
58 constexpr TextStreamFunction endl = Qt::endl;
59 #else
60 constexpr TextStreamFunction endl = ::endl;
61 #endif
62 
63 
64 QString typeToString(DUContext::ContextType type)
65 {
66  switch (type) {
67  case DUContext::Global: return QStringLiteral("Global");
68  case DUContext::Namespace: return QStringLiteral("Namespace");
69  case DUContext::Class: return QStringLiteral("Class");
70  case DUContext::Function: return QStringLiteral("Function");
71  case DUContext::Template: return QStringLiteral("Template");
72  case DUContext::Enum: return QStringLiteral("Enum");
73  case DUContext::Helper: return QStringLiteral("Helper");
74  case DUContext::Other: return QStringLiteral("Other");
75  }
76  Q_ASSERT(false);
77  return QString();
78 }
79 
80 class DUChainDumperPrivate
81 {
82 public:
83  DUChainDumperPrivate()
84  : m_indent(0)
85  {}
86 
87  void dumpProblems(TopDUContext* top, QTextStream& out);
88  void dump(DUContext* context, int allowedDepth, bool isFromImport, QTextStream& out);
89 
90  int m_indent;
91  DUChainDumper::Features m_features;
92  QSet<DUContext*> m_visitedContexts;
93 };
94 
95 DUChainDumper::DUChainDumper(Features features)
96  : d_ptr(new DUChainDumperPrivate)
97 {
98  Q_D(DUChainDumper);
99 
100  d->m_features = features;
101 }
102 
103 DUChainDumper::~DUChainDumper()
104 {
105 }
106 
107 class Indent
108 {
109 public:
110  explicit Indent(int level) : m_level(level) {}
111 
112  friend QDebug& operator<<(QDebug& debug, const Indent& ind)
113  {
114  for (int i = 0; i < ind.m_level; i++) {
115  debug.nospace() << ' ';
116  }
117 
118  return debug.space();
119  }
120 
121 private:
122  int m_level;
123 };
124 
125 void DUChainDumperPrivate::dumpProblems(TopDUContext* top, QTextStream& out)
126 {
127  QDebug qout = fromTextStream(out);
128 
129  if (!top->problems().isEmpty()) {
130  qout << top->problems().size() << "problems encountered:" << endl;
131  const auto problems = top->problems();
132  for (const ProblemPointer& p : problems) {
133  qout << Indent(m_indent * 2) << p->description() << p->explanation() << p->finalLocation() << endl;
134  }
135 
136  qout << endl;
137  }
138 }
139 
140 void DUChainDumperPrivate::dump(DUContext* context, int allowedDepth, bool isFromImport, QTextStream& out)
141 {
142  QDebug qout = fromTextStream(out);
143 
144  qout << Indent(m_indent * 2) << (isFromImport ? " ==import==>" : "")
145  << (dynamic_cast<TopDUContext*>(context) ? "Top-Context" : "Context") << typeToString(context->type())
146  << "(owner: " << context->owner() << ")"
147  << context << context->localScopeIdentifier() << "[" << context->scopeIdentifier(true) << "]"
148  << context->range().castToSimpleRange()
149  << (dynamic_cast<TopDUContext*>(context) ? static_cast<TopDUContext*>(context)->url().byteArray() : "")
150  << endl;
151 
152  if (m_visitedContexts.contains(context)) {
153  qout << Indent((m_indent + 2) * 2) << "(Skipping" << context->scopeIdentifier(true) <<
154  "because it was already printed)" << endl;
155  return;
156  }
157 
158  m_visitedContexts.insert(context);
159 
160  auto top = context->topContext();
161  if (allowedDepth >= 0) {
162  const auto localDeclarations = context->localDeclarations(top);
163  for (Declaration* dec : localDeclarations) {
164  //IdentifiedType* idType = dynamic_cast<IdentifiedType*>(dec->abstractType().data());
165 
166  qout << Indent((m_indent + 2) * 2) << "Declaration:" << dec->toString() << "[" <<
167  dec->qualifiedIdentifier() << "]"
168  << dec << "(internal ctx:" << dec->internalContext() << ")" << dec->range().castToSimpleRange() << ","
169  << (dec->isDefinition() ? "defined, " : (FunctionDefinition::definition(dec) ? "" : "no definition, "))
170  << dec->uses().count() << "use(s)." << endl;
171  if (FunctionDefinition::definition(dec)) {
172  qout << Indent((m_indent + 2) * 2 + 1) << "Definition:" <<
173  FunctionDefinition::definition(dec)->range().castToSimpleRange() << endl;
174  }
175  const auto uses = dec->uses();
176  for (auto it = uses.constBegin(); it != uses.constEnd(); ++it) {
177  qout << Indent((m_indent + 3) * 2) << "File:" << it.key().str() << endl;
178  for (const RangeInRevision range : qAsConst(*it)) {
179  qout << Indent((m_indent + 4) * 2) << "Use:" << range.castToSimpleRange() << endl;
180  }
181  }
182  }
183  } else {
184  qout << Indent((m_indent + 2) * 2) << context->localDeclarations(top).count()
185  << "Declarations," << context->childContexts().size() << "child-contexts" << endl;
186  }
187 
188  ++m_indent;
189  {
190  /*
191  const auto importedParentContexts = context->importedParentContexts();
192  for (const DUContext::Import& parent : importedParentContexts) {
193  DUContext* import = parent.context(top);
194  if(!import) {
195  qout << Indent((m_indent+2) * 2+1) << "Could not get parent, is it registered in the DUChain?" << endl;
196  continue;
197  }
198 
199  dump(import, allowedDepth-1, true, out);
200  }
201  */
202 
203  const auto childContexts = context->childContexts();
204  for (DUContext* child : childContexts) {
205  dump(child, allowedDepth - 1, false, out);
206  }
207  }
208  --m_indent;
209 }
210 
211 void DUChainDumper::dump(DUContext* context, int allowedDepth, QTextStream& out)
212 {
213  Q_D(DUChainDumper);
214 
215  d->m_visitedContexts.clear();
216 
217  if (!context) {
218  out << "Error: Null context" << endl;
219  return;
220  }
221 
222  auto top = context->topContext();
223  if (d->m_features.testFlag(DumpProblems)) {
224  d->dumpProblems(top, out);
225  }
226  if (d->m_features.testFlag(DumpContext)) {
227  d->dump(context, allowedDepth, false, out);
228  }
229 }
230 
231 void DUChainDumper::dump(DUContext* context, int allowedDepth)
232 {
233  QTextStream out(stdout);
234  dump(context, allowedDepth, out);
235 }
236 
237 }
QSet
QTextStream::string
QString * string() const
KDevelop::DUContext::Global
A context that declares functions, namespaces or classes.
Definition: ducontext.h:103
duchainpointer.h
KDevelop::DUChainDumper::DumpContext
Definition: duchaindumper.h:45
KDevelop::TextStreamFunction
QTextStream &(*)(QTextStream &) TextStreamFunction
Definition: definitions.cpp:31
QDebug::nospace
QDebug & nospace()
KDevelop::endl
constexpr TextStreamFunction endl
Definition: definitions.cpp:33
QDebug::space
QDebug & space()
QTextStream::device
QIODevice * device() const
QDebug
KDevelop::DUContext::ContextType
ContextType
Definition: ducontext.h:102
KDevelop::DUChainDumper::DumpProblems
Definition: duchaindumper.h:46
KDevelop::DUContext::Template
A context that declares template-parameters.
Definition: ducontext.h:107
topducontext.h
problem.h
KDevelop::DUContext::topContext
TopDUContext * topContext() const override
Find the top context.
Definition: ducontext.cpp:1499
declaration.h
QString
identifier.h
QTextStream
KDevelop::DUChainBase::range
RangeInRevision range() const
Returns the range assigned to this object, in the document revision when this document was last parse...
Definition: duchainbase.cpp:152
KDevelop::DUChainDumper::DUChainDumper
DUChainDumper(Features features=DumpContext)
Definition: duchaindumper.cpp:95
use.h
KDevelop::DUContext::Helper
A helper context.
Definition: ducontext.h:109
KDevelop::DUContext::Other
Represents executable code, like for example within a compound-statement.
Definition: ducontext.h:113
definitions.h
KDevelop::DUContext::Class
A context that declares class members.
Definition: ducontext.h:105
KDevelop::DUChainDumper
Debugging utility function to dump a DUContext including contained declarations.
Definition: duchaindumper.h:40
duchaindumper.h
KDevelop::ProblemPointer
QExplicitlySharedDataPointer< Problem > ProblemPointer
Definition: problem.h:37
KDevelop::typeToString
QString typeToString(DUContext::ContextType type)
Definition: duchaindumper.cpp:64
functiondefinition.h
KDevelop
Definition: abstractfunctiondeclaration.cpp:27
KDevelop::DUContext
A single context in source code, represented as a node in a directed acyclic graph.
Definition: ducontext.h:72
KDevelop::DUContext::Namespace
A context that declares namespace members.
Definition: ducontext.h:104
KDevelop::FunctionDefinition::definition
static Declaration * definition(const Declaration *decl)
Find the definition for the given declaration, if one exists.
Definition: functiondefinition.cpp:85
KDevelop::DUChainDumper::~DUChainDumper
~DUChainDumper()
Definition: duchaindumper.cpp:103
ducontext.h
KDevelop::DUChainDumper::dump
void dump(DUContext *context, int allowedDepth=0)
Dump DUChain context to stdout.
Definition: duchaindumper.cpp:231
KDevelop::DUContext::Function
A context that declares function-arguments.
Definition: ducontext.h:106
KDevelop::DUContext::Enum
A context that contains a list of enumerators.
Definition: ducontext.h:108
KDevelop::operator<<
QDebug operator<<(QDebug s, const QExplicitlySharedDataPointer< ParsingEnvironmentFile > &p)
Definition: parsingenvironment.h:226
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Wed Mar 3 2021 00:37:28 by doxygen 1.8.16 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kdevplatform/language/duchain

Skip menu "kdevplatform/language/duchain"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdevelop API Reference

Skip menu "kdevelop API Reference"
  • kdevplatform
  •   debugger
  •   documentation
  •   interfaces
  •   language
  •     assistant
  •     backgroundparser
  •     checks
  •     classmodel
  •     codecompletion
  •     codegen
  •     duchain
  •     editor
  •     highlighting
  •     interfaces
  •     util
  •   outputview
  •   project
  •   serialization
  •   shell
  •   sublime
  •   tests
  •   util
  •   vcs

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal