From kde-commits Sun Jan 31 23:46:59 2016 From: Aleix Pol Date: Sun, 31 Jan 2016 23:46:59 +0000 To: kde-commits Subject: [kdevplatform/5.0] language/duchain: Reduce duplication Message-Id: X-MARC-Message: https://marc.info/?l=kde-commits&m=145428403330663 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 =3D 0; + const CodeModelItem* i; + this->items(file, count, i); + for(int a =3D 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, con= st IndexedQualifiedIdentif newItem.kind =3D kind; newItem.referenceCount =3D 1; = - #ifdef TEST_REFERENCE_COUNTING_2 - { - uint count =3D 0; - const CodeModelItem* i; - this->items(file, count, i); - for(int a =3D 0; a < count; ++a) - Q_ASSERT(i[a].id.hasReferenceCount()); - } - #endif + testReferenceCounting(file); = if(index) { const CodeModelRepositoryItem* oldItem =3D d->m_repository.itemFromInd= ex(index); @@ -226,15 +229,7 @@ void CodeModel::addItem(const IndexedString& file, con= st IndexedQualifiedIdentif d->m_repository.deleteItem(index); }else{ //We're fine: The item fits into the existing list. -#ifdef TEST_REFERENCE_COUNTING_2 - { - uint count =3D 0; - const CodeModelItem* i; - this->items(file, count, i); - for(int a =3D 0; a < count; ++a) - Q_ASSERT(i[a].id.hasReferenceCount()); - } -#endif + testReferenceCounting(file); return; } } @@ -243,15 +238,7 @@ void CodeModel::addItem(const IndexedString& file, con= st IndexedQualifiedIdentif item.itemsList().append(newItem); } = -#ifdef TEST_REFERENCE_COUNTING_2 - { - uint count =3D 0; - const CodeModelItem* i; - this->items(file, count, i); - for(int a =3D 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, con= st IndexedQualifiedIdentif Q_UNUSED(newIndex); ifDebug( qCDebug(LANGUAGE) << "new index" << newIndex; ) = -#ifdef TEST_REFERENCE_COUNTING_2 - { - uint count =3D 0; - const CodeModelItem* i; - this->items(file, count, i); - for(int a =3D 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 =3D 0; - const CodeModelItem* i; - this->items(file, count, i); - for(int a =3D 0; a < count; ++a) - Q_ASSERT(i[a].id.hasReferenceCount()); - } -#endif + testReferenceCounting(file); = CodeModelRepositoryItem item; item.file =3D file; @@ -315,15 +286,7 @@ void CodeModel::updateItem(const IndexedString& file, = const IndexedQualifiedIden Q_ASSERT(items[listIndex].id =3D=3D id); items[listIndex].kind =3D kind; = -#ifdef TEST_REFERENCE_COUNTING_2 - { - uint count =3D 0; - const CodeModelItem* i; - this->items(file, count, i); - for(int a =3D 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 =3D 0; - const CodeModelItem* i; - this->items(file, count, i); - for(int a =3D 0; a < count; ++a) - Q_ASSERT(i[a].id.hasReferenceCount()); - } -#endif + + testReferenceCounting(file); ifDebug( qCDebug(LANGUAGE) << "removeItem" << file.str() << id.identifie= r().toString(); ) CodeModelRepositoryItem item; item.file =3D file; @@ -382,15 +338,7 @@ void CodeModel::removeItem(const IndexedString& file, = const IndexedQualifiedIden if(newItemCount =3D=3D 0) { //Has become empty, delete the item d->m_repository.deleteItem(index); -#ifdef TEST_REFERENCE_COUNTING_2 - { - uint count =3D 0; - const CodeModelItem* i; - this->items(file, count, i); - for(int a =3D 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 =3D 0; - const CodeModelItem* i; - this->items(file, count, i); - for(int a =3D 0; a < count; ++a) - Q_ASSERT(i[a].id.hasReferenceCount()); - } -#endif + testReferenceCounting(file); return; } } } -#ifdef TEST_REFERENCE_COUNTING_2 - { - uint count =3D 0; - const CodeModelItem* i; - this->items(file, count, i); - for(int a =3D 0; a < count; ++a) - Q_ASSERT(i[a].id.hasReferenceCount()); - } -#endif + testReferenceCounting(file); } = void CodeModel::items(const IndexedString& file, uint& count, const CodeMo= delItem*& items) const diff --git a/language/duchain/declarationid.cpp b/language/duchain/declarat= ionid.cpp index ffd4c9f..0cdde0f 100644 --- a/language/duchain/declarationid.cpp +++ b/language/duchain/declarationid.cpp @@ -97,13 +97,11 @@ IndexedInstantiationInformation DeclarationId::speciali= zation() const return m_specialization; } = -KDevVarLengthArray DeclarationId::getDeclarations(const TopD= UContext* top) const +void DeclarationId::iterateDeclarations(const TopDUContext* top, std::func= tion func) const { - KDevVarLengthArray ret; - if(m_isDirect =3D=3D false) { //Find the declaration by its qualified identifier and additionalIdent= ity - QualifiedIdentifier id(m_indirectData.identifier); + const QualifiedIdentifier id(m_indirectData.identifier); = if(top) { //Do filtering @@ -112,8 +110,8 @@ KDevVarLengthArray DeclarationId::getDecl= arations(const TopDUConte for(; filter; ++filter) { Declaration* decl =3D filter->data(); if(decl && m_indirectData.additionalIdentity =3D=3D decl->additi= onalIdentity()) { - //Hit - ret.append(decl); + if (func(decl)) + break; } } }else{ @@ -123,7 +121,7 @@ KDevVarLengthArray DeclarationId::getDecl= arations(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-co= ntext 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 DeclarationId::getDe= clarations(const TopDUConte Declaration* decl =3D iDecl.data(); if(decl && m_indirectData.additionalIdentity =3D=3D decl->additi= onalIdentity()) { //Hit - ret.append(decl); + if (func(decl)) + break; } } } } }else{ - Declaration* decl =3D m_directData.declaration(); - if(decl) - ret.append(decl); + func(m_directData.declaration()); } +} + +KDevVarLengthArray DeclarationId::getDeclarations(const TopD= UContext* top) const +{ + KDevVarLengthArray ret; + + iterateDeclarations(top, [&ret](Declaration* decl){ + ret.append(decl); + return false; + }); = if(!ret.isEmpty() && m_specialization.index()) { KDevVarLengthArray newRet; @@ -157,70 +164,24 @@ KDevVarLengthArray DeclarationId::getDe= clarations(const TopDUConte = Declaration* DeclarationId::getDeclaration(const TopDUContext* top, bool i= nstantiateIfRequired) const { - Declaration* ret =3D 0; + Declaration* ret =3D nullptr; = - if(m_isDirect =3D=3D false) { - //Find the declaration by its qualified identifier and additionalIdent= ity - QualifiedIdentifier id(m_indirectData.identifier); + iterateDeclarations(top, [&ret](Declaration* decl){ + ret =3D decl; + return !ret->isForwardDeclaration(); + }); = - if(top) { - //Do filtering - PersistentSymbolTable::FilteredDeclarationIterator filter =3D - PersistentSymbolTable::self().getFilteredDeclarations(id, top->r= ecursiveImportIndices()); - for(; filter; ++filter) { - Declaration* decl =3D filter->data(); - if(decl && m_indirectData.additionalIdentity =3D=3D decl->additi= onalIdentity()) { - //Hit - ret =3D decl; - if(!ret->isForwardDeclaration()) - break; - } - } - }else{ - //Just accept anything - PersistentSymbolTable::Declarations decls =3D PersistentSymbolTable:= :self().getDeclarations(id); - PersistentSymbolTable::Declarations::Iterator decl =3D decls.iterato= r(); - for(; decl; ++decl) { - const IndexedDeclaration& iDecl(*decl); - - ///@todo think this over once we don't pull in all imported top-co= ntext 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 =3D top; + if(!instantiateIfRequired) + topContextForSpecialization =3D 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 =3D ret->topContext(); = - if(!top) { - Declaration* decl =3D iDecl.data(); - if(decl && m_indirectData.additionalIdentity =3D=3D decl->additi= onalIdentity()) { - //Hit - ret =3D decl; - if(!ret->isForwardDeclaration()) - break; - } - } - } - } - }else{ - //Find the declaration by m_topContext and m_declaration - ret =3D m_directData.declaration(); + return ret->specialize(m_specialization, topContextForSpecialization); } - - - if(ret) - { - if(m_specialization.isValid()) - { - const TopDUContext* topContextForSpecialization =3D top; - if(!instantiateIfRequired) - topContextForSpecialization =3D 0; //If we don't want to instantia= te new declarations, set the top-context to zero, so specialize(..) will on= ly look-up - else if(!topContextForSpecialization) - topContextForSpecialization =3D 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/declaratio= nid.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 +#include = //krazy:excludeall=3Ddpointer = @@ -177,6 +178,8 @@ class KDEVPLATFORMLANGUAGE_EXPORT DeclarationId { QualifiedIdentifier qualifiedIdentifier() const; = private: + void iterateDeclarations(const TopDUContext* top, std::function func) const; + /// An indirect reference to the declaration, which uses the symbol-ta= ble for lookup. Should be preferred for all /// declarations that are in the symbol-table struct Indirect {