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

kdevplatform/language/codegen

  • sources
  • kfour-appscomplete
  • kdevelop
  • kdevplatform
  • language
  • codegen
utilities.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2009 Ramón Zarazúa <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17  */
18 
19 #include "utilities.h"
20 #include <debug.h>
21 
22 #include <serialization/indexedstring.h>
23 #include <duchain/identifier.h>
24 #include <duchain/duchainlock.h>
25 #include <duchain/duchain.h>
26 #include <duchain/declaration.h>
27 #include <duchain/forwarddeclaration.h>
28 #include <duchain/functiondefinition.h>
29 #include <duchain/classfunctiondeclaration.h>
30 #include <duchain/types/functiontype.h>
31 
32 namespace KDevelop {
33 namespace CodeGenUtils {
34 IdentifierValidator::IdentifierValidator(DUContext* context) : QValidator(nullptr)
35  , m_context(context)
36 {
37 }
38 
39 IdentifierValidator::~IdentifierValidator()
40 {
41 }
42 
43 QValidator::State IdentifierValidator::validate(QString& input, int&) const
44 {
45  //I can't figure out why it wouldn't compile when I tried to use Identifier identifier();
46  Identifier identifier = Identifier(IndexedString(input));
47 
48  if (identifier.isUnique())
49  return Acceptable;
50 
51  DUChainReadLocker lock(DUChain::lock(), 10);
52  return m_context->findLocalDeclarations(identifier, CursorInRevision::invalid(), nullptr,
53  AbstractType::Ptr(), DUContext::NoFiltering).empty() ? Acceptable : Invalid;
54 }
55 
56 IndexedString fetchImplementationFileForClass(const Declaration& targetClass)
57 {
58  DUChainReadLocker lock(DUChain::lock());
59  qCDebug(LANGUAGE) << "Looking for implementation file for class:" << targetClass.identifier().toString();
60 
61  DUContext* context = targetClass.internalContext();
62 
63  //If this declaration is not a user defined type, then ignore and return empty file
64  if (targetClass.kind() != Declaration::Type)
65  return IndexedString();
66 
67  //If this is a forward declaration attempt to resolve it.
68  const Declaration* realClass = &targetClass;
69  if (const auto* forward = dynamic_cast<const ForwardDeclaration*>(realClass)) {
70  if (!(realClass = forward->resolve(context->topContext())))
71  return IndexedString();
72  context = realClass->internalContext();
73  }
74 
75  const QVector<Declaration*> declarations = context->localDeclarations();
76 
77  QMap<IndexedString, unsigned int> implementationsInFile;
78 
79  for (Declaration* decl : declarations) {
81  if (auto* classFun = dynamic_cast<ClassFunctionDeclaration*>(decl))
82  if (auto* def = FunctionDefinition::definition(classFun)) {
83  qCDebug(LANGUAGE) << "Definition For declaration in:" << def->url().toUrl();
84  ++implementationsInFile[def->url()];
85  }
86  }
87 
88  QMultiMap<unsigned int, IndexedString> sorter;
89  for (auto it = implementationsInFile.constBegin(), end = implementationsInFile.constEnd(); it != end; ++it) {
90  const IndexedString& file = it.key();
91  unsigned int count = it.value();
92  sorter.insert(count, file);
93  }
94  QList<IndexedString> sortedFiles = sorter.values();
95 
96  //If there are no methods, then just return the file the declaration is in
97  if (sortedFiles.empty())
98  return context->url();
99 
100  if (sortedFiles.size() == 1)
101  return sortedFiles[0];
102 
103  const QList<IndexedString> tiedFiles = sorter.values(sorter.end().key());
104  if (tiedFiles.size() > 1) {
105  //Return the file that has the most uses
106  const auto uses = realClass->uses();
107 
108  IndexedString mostUsesFile;
109  unsigned int mostUses = 0;
110  for (const IndexedString& currentFile : tiedFiles) {
111  if (static_cast<unsigned int>(uses[currentFile].size()) > mostUses) {
112  mostUses = uses[currentFile].size();
113  mostUsesFile = currentFile;
114  }
115  }
116 
117  return mostUsesFile;
118  } else
119  return sortedFiles.back();
120 }
121 }
122 }
QMap::constBegin
const_iterator constBegin() const
QMultiMap
QList< IndexedString >
QList::empty
bool empty() const
QList::size
int size() const
QMap::end
iterator end()
KDevelop::CodeGenUtils::IdentifierValidator::validate
State validate(QString &input, int &) const override
Definition: utilities.cpp:43
KDevelop::CodeGenUtils::IdentifierValidator::~IdentifierValidator
~IdentifierValidator() override
Definition: utilities.cpp:39
QMap::constEnd
const_iterator constEnd() const
QString
QValidator
QMap::values
QList< T > values() const
QMultiMap::insert
QMap< Key, T >::iterator insert(const Key &key, const T &value)
utilities.h
QList::back
T & back()
QMap::key
const Key key(const T &value) const
QMap
KDevelop::CodeGenUtils::fetchImplementationFileForClass
IndexedString fetchImplementationFileForClass(const Declaration &targetClass)
Search for the file that contains the implementation of a specified type.
Definition: utilities.cpp:56
KDevelop
NOTE: changes in this file will quite probably also require changes in codedescriptionmetatype....
Definition: applychangeswidget.cpp:42
QVector
KDevelop::CodeGenUtils::IdentifierValidator::IdentifierValidator
IdentifierValidator(DUContext *context)
Definition: utilities.cpp:34
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Jan 16 2021 23:35:01 by doxygen 1.8.16 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kdevplatform/language/codegen

Skip menu "kdevplatform/language/codegen"
  • 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