[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [kdevplatform/5.0] language/duchain: Reduce duplication
From:       Aleix Pol <aleixpol () kde ! org>
Date:       2016-01-31 23:46:59
Message-ID: E1aQ1hr-0000EE-Va () scm ! kde ! org
[Download RAW message or body]

Git commit 6d835b3aa5be6f9efe3d15487236e0daa6fa5c23 by Aleix Pol.
Committed on 31/01/2016 at 23:37.
Pushed by apol into branch '5.0'.

Reduce duplication

Move some duplicated code blobs into a function.
Share the code that iterates the declarations in a TopDUContext
in DeclarationId.

M  +22   -90   language/duchain/codemodel.cpp
M  +32   -71   language/duchain/declarationid.cpp
M  +3    -0    language/duchain/declarationid.h

http://commits.kde.org/kdevplatform/6d835b3aa5be6f9efe3d15487236e0daa6fa5c23

diff --git a/language/duchain/codemodel.cpp b/language/duchain/codemodel.cpp
index 0cc2bd8..8447987 100644
--- a/language/duchain/codemodel.cpp
+++ b/language/duchain/codemodel.cpp
@@ -72,6 +72,17 @@ class CodeModelItemHandler {
 };
 
 
+static void testReferenceCounting(const KDevelop::IndexedString& file)
+{
+#ifdef TEST_REFERENCE_COUNTING_2
+  uint count = 0;
+  const CodeModelItem* i;
+  this->items(file, count, i);
+  for(int a = 0; a < count; ++a)
+    Q_ASSERT(i[a].id.hasReferenceCount());
+#endif
+}
+
 DEFINE_LIST_MEMBER_HASH(CodeModelRepositoryItem, items, CodeModelItem)
 
 class CodeModelRepositoryItem {
@@ -187,15 +198,7 @@ void CodeModel::addItem(const IndexedString& file, const \
IndexedQualifiedIdentif  newItem.kind = kind;
   newItem.referenceCount = 1;
 
-  #ifdef TEST_REFERENCE_COUNTING_2
-  {
-  uint count = 0;
-  const CodeModelItem* i;
-  this->items(file, count, i);
-  for(int a = 0; a < count; ++a)
-    Q_ASSERT(i[a].id.hasReferenceCount());
-  }
-  #endif
+  testReferenceCounting(file);
 
   if(index) {
     const CodeModelRepositoryItem* oldItem = d->m_repository.itemFromIndex(index);
@@ -226,15 +229,7 @@ void CodeModel::addItem(const IndexedString& file, const \
IndexedQualifiedIdentif  d->m_repository.deleteItem(index);
       }else{
         //We're fine: The item fits into the existing list.
-#ifdef TEST_REFERENCE_COUNTING_2
-        {
-        uint count = 0;
-        const CodeModelItem* i;
-        this->items(file, count, i);
-        for(int a = 0; a < count; ++a)
-          Q_ASSERT(i[a].id.hasReferenceCount());
-        }
-#endif
+        testReferenceCounting(file);
         return;
       }
     }
@@ -243,15 +238,7 @@ void CodeModel::addItem(const IndexedString& file, const \
IndexedQualifiedIdentif  item.itemsList().append(newItem);
   }
 
-#ifdef TEST_REFERENCE_COUNTING_2
-  {
-  uint count = 0;
-  const CodeModelItem* i;
-  this->items(file, count, i);
-  for(int a = 0; a < count; ++a)
-    Q_ASSERT(i[a].id.hasReferenceCount());
-  }
-#endif
+  testReferenceCounting(file);
 
   Q_ASSERT(!d->m_repository.findIndex(request));
 
@@ -260,15 +247,7 @@ void CodeModel::addItem(const IndexedString& file, const \
IndexedQualifiedIdentif  Q_UNUSED(newIndex);
   ifDebug( qCDebug(LANGUAGE) << "new index" << newIndex; )
 
-#ifdef TEST_REFERENCE_COUNTING_2
-  {
-  uint count = 0;
-  const CodeModelItem* i;
-  this->items(file, count, i);
-  for(int a = 0; a < count; ++a)
-    Q_ASSERT(i[a].id.hasReferenceCount());
-  }
-#endif
+  testReferenceCounting(file);
 
   Q_ASSERT(d->m_repository.findIndex(request));
 }
@@ -280,15 +259,7 @@ void CodeModel::updateItem(const IndexedString& file, const \
IndexedQualifiedIden  if(!id.isValid())
     return;
 
-#ifdef TEST_REFERENCE_COUNTING_2
-  {
-  uint count = 0;
-  const CodeModelItem* i;
-  this->items(file, count, i);
-  for(int a = 0; a < count; ++a)
-    Q_ASSERT(i[a].id.hasReferenceCount());
-  }
-#endif
+  testReferenceCounting(file);
 
   CodeModelRepositoryItem item;
   item.file = file;
@@ -315,15 +286,7 @@ void CodeModel::updateItem(const IndexedString& file, const \
IndexedQualifiedIden  Q_ASSERT(items[listIndex].id == id);
     items[listIndex].kind = kind;
 
-#ifdef TEST_REFERENCE_COUNTING_2
-    {
-    uint count = 0;
-    const CodeModelItem* i;
-    this->items(file, count, i);
-    for(int a = 0; a < count; ++a)
-      Q_ASSERT(i[a].id.hasReferenceCount());
-    }
-#endif
+    testReferenceCounting(file);
 
     return;
   }
@@ -336,15 +299,8 @@ void CodeModel::removeItem(const IndexedString& file, const \
IndexedQualifiedIden  {
   if(!id.isValid())
     return;
-#ifdef TEST_REFERENCE_COUNTING_2
-  {
-  uint count = 0;
-  const CodeModelItem* i;
-  this->items(file, count, i);
-  for(int a = 0; a < count; ++a)
-    Q_ASSERT(i[a].id.hasReferenceCount());
-  }
-#endif
+
+  testReferenceCounting(file);
   ifDebug( qCDebug(LANGUAGE) << "removeItem" << file.str() << \
id.identifier().toString(); )  CodeModelRepositoryItem item;
   item.file = file;
@@ -382,15 +338,7 @@ void CodeModel::removeItem(const IndexedString& file, const \
IndexedQualifiedIden  if(newItemCount == 0) {
         //Has become empty, delete the item
         d->m_repository.deleteItem(index);
-#ifdef TEST_REFERENCE_COUNTING_2
-  {
-  uint count = 0;
-  const CodeModelItem* i;
-  this->items(file, count, i);
-  for(int a = 0; a < count; ++a)
-    Q_ASSERT(i[a].id.hasReferenceCount());
-  }
-#endif
+        testReferenceCounting(file);
 
         return;
       }else{
@@ -402,28 +350,12 @@ void CodeModel::removeItem(const IndexedString& file, const \
IndexedQualifiedIden  d->m_repository.deleteItem(index);
         //Add the new list
         d->m_repository.index(request);
-#ifdef TEST_REFERENCE_COUNTING_2
-  {
-  uint count = 0;
-  const CodeModelItem* i;
-  this->items(file, count, i);
-  for(int a = 0; a < count; ++a)
-    Q_ASSERT(i[a].id.hasReferenceCount());
-  }
-#endif
+        testReferenceCounting(file);
         return;
       }
     }
   }
-#ifdef TEST_REFERENCE_COUNTING_2
-  {
-  uint count = 0;
-  const CodeModelItem* i;
-  this->items(file, count, i);
-  for(int a = 0; a < count; ++a)
-    Q_ASSERT(i[a].id.hasReferenceCount());
-  }
-#endif
+  testReferenceCounting(file);
 }
 
 void CodeModel::items(const IndexedString& file, uint& count, const CodeModelItem*& \
                items) const
diff --git a/language/duchain/declarationid.cpp b/language/duchain/declarationid.cpp
index ffd4c9f..0cdde0f 100644
--- a/language/duchain/declarationid.cpp
+++ b/language/duchain/declarationid.cpp
@@ -97,13 +97,11 @@ IndexedInstantiationInformation DeclarationId::specialization() \
const  return m_specialization;
 }
 
-KDevVarLengthArray<Declaration*> DeclarationId::getDeclarations(const TopDUContext* \
top) const +void DeclarationId::iterateDeclarations(const TopDUContext* top, \
std::function<bool(Declaration* d)> func) const  {
-  KDevVarLengthArray<Declaration*> ret;
-
   if(m_isDirect == false) {
     //Find the declaration by its qualified identifier and additionalIdentity
-    QualifiedIdentifier id(m_indirectData.identifier);
+    const QualifiedIdentifier id(m_indirectData.identifier);
 
     if(top) {
       //Do filtering
@@ -112,8 +110,8 @@ KDevVarLengthArray<Declaration*> \
DeclarationId::getDeclarations(const TopDUConte  for(; filter; ++filter) {
           Declaration* decl = filter->data();
           if(decl && m_indirectData.additionalIdentity == \
                decl->additionalIdentity()) {
-            //Hit
-            ret.append(decl);
+            if (func(decl))
+              break;
           }
       }
     }else{
@@ -123,7 +121,7 @@ KDevVarLengthArray<Declaration*> \
DeclarationId::getDeclarations(const TopDUConte  for(; decl; ++decl) {
         const IndexedDeclaration& iDecl(*decl);
 
-          ///@todo think this over once we don't pull in all imported top-context \
any more +        ///@todo think this over once we don't pull in all imported \
                top-context any more
         //Don't trigger loading of top-contexts from here, it will create a lot of \
problems  if((!DUChain::self()->isInMemory(iDecl.topContextIndex())))
           continue;
@@ -132,16 +130,25 @@ KDevVarLengthArray<Declaration*> \
DeclarationId::getDeclarations(const TopDUConte  Declaration* decl = iDecl.data();
           if(decl && m_indirectData.additionalIdentity == \
decl->additionalIdentity()) {  //Hit
-            ret.append(decl);
+            if (func(decl))
+              break;
           }
         }
       }
     }
   }else{
-    Declaration* decl = m_directData.declaration();
-    if(decl)
-      ret.append(decl);
+    func(m_directData.declaration());
   }
+}
+
+KDevVarLengthArray<Declaration*> DeclarationId::getDeclarations(const TopDUContext* \
top) const +{
+  KDevVarLengthArray<Declaration*> ret;
+
+  iterateDeclarations(top, [&ret](Declaration* decl){
+    ret.append(decl);
+    return false;
+  });
 
   if(!ret.isEmpty() && m_specialization.index()) {
     KDevVarLengthArray<Declaration*> newRet;
@@ -157,70 +164,24 @@ KDevVarLengthArray<Declaration*> \
DeclarationId::getDeclarations(const TopDUConte  
 Declaration* DeclarationId::getDeclaration(const TopDUContext* top, bool \
instantiateIfRequired) const  {
-  Declaration* ret = 0;
+  Declaration* ret = nullptr;
 
-  if(m_isDirect == false) {
-    //Find the declaration by its qualified identifier and additionalIdentity
-    QualifiedIdentifier id(m_indirectData.identifier);
+  iterateDeclarations(top, [&ret](Declaration* decl){
+    ret = decl;
+    return !ret->isForwardDeclaration();
+  });
 
-    if(top) {
-      //Do filtering
-      PersistentSymbolTable::FilteredDeclarationIterator filter =
-          PersistentSymbolTable::self().getFilteredDeclarations(id, \
                top->recursiveImportIndices());
-      for(; filter; ++filter) {
-          Declaration* decl = filter->data();
-          if(decl && m_indirectData.additionalIdentity == \
                decl->additionalIdentity()) {
-            //Hit
-            ret = decl;
-            if(!ret->isForwardDeclaration())
-              break;
-          }
-      }
-    }else{
-      //Just accept anything
-      PersistentSymbolTable::Declarations decls = \
                PersistentSymbolTable::self().getDeclarations(id);
-      PersistentSymbolTable::Declarations::Iterator decl = decls.iterator();
-      for(; decl; ++decl) {
-        const IndexedDeclaration& iDecl(*decl);
-
-        ///@todo think this over once we don't pull in all imported top-context any \
                more
-        //Don't trigger loading of top-contexts from here, it will create a lot of \
                problems
-        if((!DUChain::self()->isInMemory(iDecl.topContextIndex())))
-          continue;
+  if(ret && m_specialization.isValid())
+  {
+    const TopDUContext* topContextForSpecialization = top;
+    if(!instantiateIfRequired)
+      topContextForSpecialization = 0; //If we don't want to instantiate new \
declarations, set the top-context to zero, so specialize(..) will only look-up +    \
else if(!topContextForSpecialization) +      topContextForSpecialization = \
ret->topContext();  
-        if(!top) {
-          Declaration* decl = iDecl.data();
-          if(decl && m_indirectData.additionalIdentity == \
                decl->additionalIdentity()) {
-            //Hit
-            ret = decl;
-            if(!ret->isForwardDeclaration())
-              break;
-          }
-        }
-      }
-    }
-  }else{
-    //Find the declaration by m_topContext and m_declaration
-    ret = m_directData.declaration();
+    return ret->specialize(m_specialization, topContextForSpecialization);
   }
-
-
-  if(ret)
-  {
-    if(m_specialization.isValid())
-    {
-      const TopDUContext* topContextForSpecialization = top;
-      if(!instantiateIfRequired)
-        topContextForSpecialization = 0; //If we don't want to instantiate new \
                declarations, set the top-context to zero, so specialize(..) will \
                only look-up
-      else if(!topContextForSpecialization)
-        topContextForSpecialization = ret->topContext();
-
-      return ret->specialize(m_specialization, topContextForSpecialization);
-    }else{
-      return ret;
-    }
-  }else
-    return 0;
+  return ret;
 }
 
 QualifiedIdentifier DeclarationId::qualifiedIdentifier() const
diff --git a/language/duchain/declarationid.h b/language/duchain/declarationid.h
index 775c3cf..0d23e96 100644
--- a/language/duchain/declarationid.h
+++ b/language/duchain/declarationid.h
@@ -23,6 +23,7 @@
 #include "identifier.h"
 #include "instantiationinformation.h"
 #include <language/util/kdevhash.h>
+#include <functional>
 
 //krazy:excludeall=dpointer
 
@@ -177,6 +178,8 @@ class KDEVPLATFORMLANGUAGE_EXPORT DeclarationId {
     QualifiedIdentifier qualifiedIdentifier() const;
 
   private:
+    void iterateDeclarations(const TopDUContext* top, \
std::function<bool(Declaration* d)> func) const; +
     /// An indirect reference to the declaration, which uses the symbol-table for \
lookup. Should be preferred for all  /// declarations that are in the symbol-table
     struct Indirect {


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic