From 4c0557e8261a6753a19de293e8cc7cdd3beb90ca Mon Sep 17 00:00:00 2001 From: Dimitri van Heesch Date: Sun, 1 Nov 2020 13:40:46 +0100 Subject: Refactoring: modernized the getUsedNamespaces() method --- src/doxygen.cpp | 56 ++++++++++++-------------------- src/filedef.cpp | 67 ++++++++++---------------------------- src/filedef.h | 2 +- src/namespacedef.cpp | 59 +++++++++------------------------- src/namespacedef.h | 2 +- src/symbolresolver.cpp | 87 ++++++++++++++++++++------------------------------ 6 files changed, 89 insertions(+), 184 deletions(-) diff --git a/src/doxygen.cpp b/src/doxygen.cpp index 050c9d2..4343851 100644 --- a/src/doxygen.cpp +++ b/src/doxygen.cpp @@ -799,17 +799,13 @@ static Definition *findScopeFromQualifiedName(Definition *startScope,const QCStr //printf(" resultScope=%p\n",resultScope); if (resultScope==0) { - NamespaceSDict *usedNamespaces; - if (orgScope==Doxygen::globalScope && fileScope && - (usedNamespaces = fileScope->getUsedNamespaces())) + if (orgScope==Doxygen::globalScope && fileScope && !fileScope->getUsedNamespaces().empty()) // also search for used namespaces { - NamespaceSDict::Iterator ni(*usedNamespaces); - NamespaceDef *nd; - for (ni.toFirst();((nd=ni.current()) && resultScope==0);++ni) + for (const auto &nd : fileScope->getUsedNamespaces()) { - // restart search within the used namespace - resultScope = findScopeFromQualifiedName(nd,n,fileScope,tagInfo); + resultScope = findScopeFromQualifiedName(const_cast(nd),n,fileScope,tagInfo); + if (resultScope!=0) break; } if (resultScope) { @@ -1593,21 +1589,15 @@ static void buildNamespaceList(const Entry *root) //---------------------------------------------------------------------- -static const NamespaceDef *findUsedNamespace(const NamespaceSDict *unl, +static const NamespaceDef *findUsedNamespace(const LinkedRefMap &unl, const QCString &name) { const NamespaceDef *usingNd =0; - if (unl) + for (const auto &und : unl) { - //printf("Found namespace dict %d\n",unl->count()); - NamespaceSDict::Iterator unli(*unl); - const NamespaceDef *und; - for (unli.toFirst();(und=unli.current());++unli) - { - QCString uScope=und->name()+"::"; - usingNd = getResolvedNamespace(uScope+name); - //printf("Also trying with scope='%s' usingNd=%p\n",(uScope+name).data(),usingNd); - } + QCString uScope=und->name()+"::"; + usingNd = getResolvedNamespace(uScope+name); + if (usingNd!=0) break; } return usingNd; } @@ -5066,18 +5056,19 @@ static bool findGlobalMember(const Entry *root, { nd = md->getNamespaceDef(); } - //const Definition *scope=md->getOuterScope(); - //md = md->resolveAlias(); const FileDef *fd=root->fileDef(); //printf("File %s\n",fd ? fd->name().data() : ""); - NamespaceSDict *nl = fd ? fd->getUsedNamespaces() : 0; - //SDict *cl = fd ? fd->getUsedClasses() : 0; + LinkedRefMap nl; + if (fd) + { + nl = fd->getUsedNamespaces(); + } //printf("NamespaceList %p\n",nl); // search in the list of namespaces that are imported via a // using declaration - bool viaUsingDirective = nl && nd && nl->find(nd->qualifiedName())!=0; + bool viaUsingDirective = nd && nl.find(nd->qualifiedName())!=0; if ((namespaceName.isEmpty() && nd==0) || // not in a namespace (nd && nd->name()==namespaceName) || // or in the same namespace @@ -5985,20 +5976,13 @@ static void findMember(const Entry *root, FileDef *fd=root->fileDef(); if (fd) { - NamespaceSDict *fnl = fd->getUsedNamespaces(); - if (fnl) + for (const auto &fnd : fd->getUsedNamespaces()) { - QCString joinedName; - NamespaceDef *fnd; - NamespaceSDict::Iterator nsdi(*fnl); - for (nsdi.toFirst();(fnd=nsdi.current());++nsdi) + QCString joinedName = fnd->name()+"::"+scopeName; + if (Doxygen::namespaceSDict->find(joinedName)) { - joinedName = fnd->name()+"::"+scopeName; - if (Doxygen::namespaceSDict->find(joinedName)) - { - scopeName=joinedName; - break; - } + scopeName=joinedName; + break; } } } diff --git a/src/filedef.cpp b/src/filedef.cpp index 00255f5..9001f62 100644 --- a/src/filedef.cpp +++ b/src/filedef.cpp @@ -78,7 +78,7 @@ class FileDefImpl : public DefinitionImpl, public FileDef virtual bool isIncluded(const QCString &name) const; virtual PackageDef *packageDef() const { return m_package; } virtual DirDef *getDirDef() const { return m_dir; } - virtual NamespaceSDict *getUsedNamespaces() const; + virtual LinkedRefMap getUsedNamespaces() const; virtual LinkedRefMap getUsedClasses() const { return m_usingDeclList; } virtual QList *includeFileList() const { return m_includeList; } virtual QList *includedByFileList() const { return m_includedByList; } @@ -156,7 +156,7 @@ class FileDefImpl : public DefinitionImpl, public FileDef QList *m_includeList; QDict *m_includedByDict; QList *m_includedByList; - NamespaceSDict *m_usingDirList; + LinkedRefMap m_usingDirList; LinkedRefMap m_usingDeclList; QCString m_path; QCString m_filePath; @@ -240,7 +240,6 @@ FileDefImpl::FileDefImpl(const char *p,const char *nm, m_namespaceSDict = 0; m_srcDefDict = 0; m_srcMemberDict = 0; - m_usingDirList = 0; m_package = 0; m_isSource = guessSection(nm)==Entry::SOURCE_SEC; m_docname = nm; @@ -270,7 +269,6 @@ FileDefImpl::~FileDefImpl() delete m_namespaceSDict; delete m_srcDefDict; delete m_srcMemberDict; - delete m_usingDirList; delete m_memberGroupSDict; } @@ -1488,18 +1486,11 @@ MemberDef *FileDefImpl::getSourceMember(int lineNr) const void FileDefImpl::addUsingDirective(const NamespaceDef *nd) { - if (m_usingDirList==0) - { - m_usingDirList = new NamespaceSDict; - } - if (m_usingDirList->find(nd->qualifiedName())==0) - { - m_usingDirList->append(nd->qualifiedName(),nd); - } + m_usingDirList.add(nd->qualifiedName(),nd); //printf("%p: FileDefImpl::addUsingDirective: %s:%d\n",this,name().data(),usingDirList->count()); } -NamespaceSDict *FileDefImpl::getUsedNamespaces() const +LinkedRefMap FileDefImpl::getUsedNamespaces() const { //printf("%p: FileDefImpl::getUsedNamespace: %s:%d\n",this,name().data(),usingDirList?usingDirList->count():0); return m_usingDirList; @@ -1561,22 +1552,11 @@ void FileDefImpl::addIncludedUsingDirectives() if (ii->fileDef && ii->fileDef!=this) { // add using directives - NamespaceSDict *unl = ii->fileDef->getUsedNamespaces(); - if (unl) + auto unl = ii->fileDef->getUsedNamespaces(); + for (auto it = unl.rbegin(); it!=unl.rend(); ++it) { - NamespaceSDict::Iterator nli(*unl); - NamespaceDef *nd; - for (nli.toLast();(nd=nli.current());--nli) - { - // append each using directive found in a #include file - if (m_usingDirList==0) m_usingDirList = new NamespaceSDict; - //printf("Prepending used namespace %s to the list of file %s\n", - // nd->name().data(),name().data()); - if (m_usingDirList->find(nd->qualifiedName())==0) // not yet added - { - m_usingDirList->prepend(nd->qualifiedName(),nd); - } - } + const auto *nd = *it; + m_usingDirList.prepend(nd->qualifiedName(),nd); } // add using declarations auto udl = ii->fileDef->getUsedClasses(); @@ -1912,32 +1892,19 @@ void FileDefImpl::combineUsingRelations() { if (m_visited) return; // already done m_visited=TRUE; - if (m_usingDirList) + for (auto &nd : m_usingDirList) { - NamespaceSDict::Iterator nli(*m_usingDirList); - NamespaceDef *nd; - for (nli.toFirst();(nd=nli.current());++nli) + const_cast(nd)->combineUsingRelations(); + + // add used namespaces of namespace nd to this namespace + for (const auto &und : nd->getUsedNamespaces()) { - nd->combineUsingRelations(); + addUsingDirective(und); } - for (nli.toFirst();(nd=nli.current());++nli) + // add used classes of namespace nd to this namespace + for (const auto &ucd : nd->getUsedClasses()) { - // add used namespaces of namespace nd to this namespace - if (nd->getUsedNamespaces()) - { - NamespaceSDict::Iterator unli(*nd->getUsedNamespaces()); - NamespaceDef *und; - for (unli.toFirst();(und=unli.current());++unli) - { - //printf("Adding namespace %s to the using list of %s\n",und->qualifiedName().data(),qualifiedName().data()); - addUsingDirective(und); - } - } - // add used classes of namespace nd to this namespace - for (const auto &ucd : nd->getUsedClasses()) - { - addUsingDeclaration(ucd); - } + addUsingDeclaration(ucd); } } } diff --git a/src/filedef.h b/src/filedef.h index 03c6e2f..505625d 100644 --- a/src/filedef.h +++ b/src/filedef.h @@ -117,7 +117,7 @@ class FileDef : virtual public Definition virtual PackageDef *packageDef() const = 0; virtual DirDef *getDirDef() const = 0; - virtual NamespaceSDict *getUsedNamespaces() const = 0; + virtual LinkedRefMap getUsedNamespaces() const = 0; virtual LinkedRefMap getUsedClasses() const = 0; virtual QList *includeFileList() const = 0; virtual QList *includedByFileList() const = 0; diff --git a/src/namespacedef.cpp b/src/namespacedef.cpp index f6d35fb..89ca9de 100644 --- a/src/namespacedef.cpp +++ b/src/namespacedef.cpp @@ -58,7 +58,7 @@ class NamespaceDefImpl : public DefinitionImpl, public NamespaceDef virtual void countMembers(); virtual int numDocMembers() const; virtual void addUsingDirective(const NamespaceDef *nd); - virtual const NamespaceSDict *getUsedNamespaces() const; + virtual LinkedRefMap getUsedNamespaces() const { return m_usingDirList; } virtual void addUsingDeclaration(const ClassDef *cd); virtual LinkedRefMap getUsedClasses() const { return m_usingDeclList; } virtual void combineUsingRelations(); @@ -124,7 +124,7 @@ class NamespaceDefImpl : public DefinitionImpl, public NamespaceDef QCString fileName; FileList files; - NamespaceSDict *usingDirList = 0; + LinkedRefMap m_usingDirList; LinkedRefMap m_usingDeclList; SDict *m_innerCompounds = 0; @@ -170,7 +170,7 @@ class NamespaceDefAliasImpl : public DefinitionAliasImpl, public NamespaceDef virtual int numDocMembers() const { return getNSAlias()->numDocMembers(); } virtual void addUsingDirective(const NamespaceDef *nd) {} - virtual const NamespaceSDict *getUsedNamespaces() const + virtual LinkedRefMap getUsedNamespaces() const { return getNSAlias()->getUsedNamespaces(); } virtual void addUsingDeclaration(const ClassDef *cd) {} virtual LinkedRefMap getUsedClasses() const @@ -284,7 +284,6 @@ NamespaceDefImpl::NamespaceDefImpl(const char *df,int dl,int dc, exceptionSDict = new ClassSDict(17); namespaceSDict = new NamespaceSDict(17); m_innerCompounds = new SDict(17); - usingDirList = 0; m_allMembersDict = 0; setReference(lref); memberGroupSDict = new MemberGroupSDict; @@ -318,7 +317,6 @@ NamespaceDefImpl::~NamespaceDefImpl() delete exceptionSDict; delete namespaceSDict; delete m_innerCompounds; - delete usingDirList; delete memberGroupSDict; delete m_allMembersDict; } @@ -1207,21 +1205,8 @@ int NamespaceDefImpl::numDocMembers() const void NamespaceDefImpl::addUsingDirective(const NamespaceDef *nd) { - if (usingDirList==0) - { - usingDirList = new NamespaceSDict; - } - if (usingDirList->find(nd->qualifiedName())==0) - { - usingDirList->append(nd->qualifiedName(),nd); - } - //printf("%p: NamespaceDefImpl::addUsingDirective: %s:%d\n",this,name().data(),usingDirList->count()); -} - -const NamespaceSDict *NamespaceDefImpl::getUsedNamespaces() const -{ - //printf("%p: NamespaceDefImpl::getUsedNamespace: %s:%d\n",this,name().data(),usingDirList?usingDirList->count():0); - return usingDirList; + m_usingDirList.add(nd->qualifiedName(),nd); + //printf("%p: NamespaceDefImpl::addUsingDirective: %s:%d\n",this,name().data(),m_usingDirList->count()); } void NamespaceDefImpl::addUsingDeclaration(const ClassDef *cd) @@ -1240,9 +1225,9 @@ const Definition *NamespaceDefImpl::findInnerCompound(const char *n) const const Definition *d = m_innerCompounds->find(n); if (d==0) { - if (usingDirList) + if (!m_usingDirList.empty()) { - d = usingDirList->find(n); + d = m_usingDirList.find(n); } if (d==0 && !m_usingDeclList.empty()) { @@ -1312,32 +1297,18 @@ void NamespaceDefImpl::combineUsingRelations() { if (m_visited) return; // already done m_visited=TRUE; - if (usingDirList) + for (auto &nd : m_usingDirList) { - NamespaceSDict::Iterator nli(*usingDirList); - NamespaceDef *nd; - for (nli.toFirst();(nd=nli.current());++nli) + const_cast(nd)->combineUsingRelations(); + // add used namespaces of namespace nd to this namespace + for (const auto &und : nd->getUsedNamespaces()) { - nd->combineUsingRelations(); + addUsingDirective(und); } - for (nli.toFirst();(nd=nli.current());++nli) + // add used classes of namespace nd to this namespace + for (const auto &ucd : nd->getUsedClasses()) { - // add used namespaces of namespace nd to this namespace - if (nd->getUsedNamespaces()) - { - NamespaceSDict::Iterator unli(*nd->getUsedNamespaces()); - NamespaceDef *und; - for (unli.toFirst();(und=unli.current());++unli) - { - //printf("Adding namespace %s to the using list of %s\n",und->qualifiedName().data(),qualifiedName().data()); - addUsingDirective(und); - } - } - // add used classes of namespace nd to this namespace - for (const auto &ucd : nd->getUsedClasses()) - { - addUsingDeclaration(ucd); - } + addUsingDeclaration(ucd); } } } diff --git a/src/namespacedef.h b/src/namespacedef.h index a16301a..16b34a0 100644 --- a/src/namespacedef.h +++ b/src/namespacedef.h @@ -55,7 +55,7 @@ class NamespaceDef : virtual public Definition virtual void countMembers() = 0; virtual int numDocMembers() const = 0; virtual void addUsingDirective(const NamespaceDef *nd) = 0; - virtual const NamespaceSDict *getUsedNamespaces() const = 0; + virtual LinkedRefMap getUsedNamespaces() const = 0; virtual void addUsingDeclaration(const ClassDef *cd) = 0; virtual LinkedRefMap getUsedClasses() const = 0; virtual void combineUsingRelations() = 0; diff --git a/src/symbolresolver.cpp b/src/symbolresolver.cpp index bdc2862..042a832 100644 --- a/src/symbolresolver.cpp +++ b/src/symbolresolver.cpp @@ -146,7 +146,7 @@ struct SymbolResolver::Private const Definition *endOfPathIsUsedClass(LinkedRefMap cl,const QCString &localName); bool accessibleViaUsingNamespace(StringUnorderedSet &visited, - const NamespaceSDict *nl, + const LinkedRefMap &nl, const Definition *item, const QCString &explicitScopePart=""); bool accessibleViaUsingClass(const LinkedRefMap &cl, @@ -217,8 +217,7 @@ const ClassDef *SymbolResolver::Private::getResolvedClassRec( //printf("found symbol!\n"); bool hasUsingStatements = - (m_fileScope && ((m_fileScope->getUsedNamespaces() && - m_fileScope->getUsedNamespaces()->count()>0) || + (m_fileScope && (!m_fileScope->getUsedNamespaces().empty() || !m_fileScope->getUsedClasses().empty()) ); //printf("hasUsingStatements=%d\n",hasUsingStatements); @@ -377,7 +376,7 @@ void SymbolResolver::Private::getResolvedSymbol( } else if (distance==minDistance && m_fileScope && bestMatch && - m_fileScope->getUsedNamespaces() && + !m_fileScope->getUsedNamespaces().empty() && d->getOuterScope()->definitionType()==Definition::TypeNamespace && bestMatch->getOuterScope()==Doxygen::globalScope ) @@ -694,22 +693,16 @@ int SymbolResolver::Private::isAccessibleFromWithExpScope( goto done; } } - const NamespaceSDict *nl = nscope->getUsedNamespaces(); - if (nl) + for (const auto &nd : nscope->getUsedNamespaces()) { - NamespaceSDict::Iterator nli(*nl); - const NamespaceDef *nd; - for (nli.toFirst();(nd=nli.current());++nli) + if (visitedNamespaces.find(nd->name().str())==visitedNamespaces.end()) { - if (visitedNamespaces.find(nd->name().str())==visitedNamespaces.end()) + //printf("Trying for namespace %s\n",nd->name().data()); + i = isAccessibleFromWithExpScope(visitedNamespaces,accessStack,scope,item,nd->name()); + if (i!=-1) { - //printf("Trying for namespace %s\n",nd->name().data()); - i = isAccessibleFromWithExpScope(visitedNamespaces,accessStack,scope,item,nd->name()); - if (i!=-1) - { - //printf("> found via explicit scope of used namespace\n"); - goto done; - } + //printf("> found via explicit scope of used namespace\n"); + goto done; } } } @@ -729,9 +722,8 @@ int SymbolResolver::Private::isAccessibleFromWithExpScope( if (scope->definitionType()==Definition::TypeNamespace) { const NamespaceDef *nscope = dynamic_cast(scope); - const NamespaceSDict *nl = nscope->getUsedNamespaces(); StringUnorderedSet visited; - if (accessibleViaUsingNamespace(visited,nl,item,explicitScopePart)) + if (accessibleViaUsingNamespace(visited,nscope->getUsedNamespaces(),item,explicitScopePart)) { //printf("> found in used namespace\n"); goto done; @@ -741,9 +733,8 @@ int SymbolResolver::Private::isAccessibleFromWithExpScope( { if (m_fileScope) { - const NamespaceSDict *nl = m_fileScope->getUsedNamespaces(); StringUnorderedSet visited; - if (accessibleViaUsingNamespace(visited,nl,item,explicitScopePart)) + if (accessibleViaUsingNamespace(visited,m_fileScope->getUsedNamespaces(),item,explicitScopePart)) { //printf("> found in used namespace\n"); goto done; @@ -835,43 +826,37 @@ const Definition *SymbolResolver::Private::endOfPathIsUsedClass(LinkedRefMap &nl, const Definition *item, const QCString &explicitScopePart) { - if (nl) // check used namespaces for the class + for (const auto &und : nl) // check used namespaces for the class { - NamespaceSDict::Iterator nli(*nl); - NamespaceDef *und; - int count=0; - for (nli.toFirst();(und=nli.current());++nli,count++) + //printf("[Trying via used namespace %s: count=%d/%d\n",und->name().data(), + // count,nl->count()); + const Definition *sc = explicitScopePart.isEmpty() ? und : followPath(und,explicitScopePart); + if (sc && item->getOuterScope()==sc) { - //printf("[Trying via used namespace %s: count=%d/%d\n",und->name().data(), - // count,nl->count()); - const Definition *sc = explicitScopePart.isEmpty() ? und : followPath(und,explicitScopePart); - if (sc && item->getOuterScope()==sc) - { - //printf("] found it\n"); - return true; - } - if (item->getLanguage()==SrcLangExt_Cpp) + //printf("] found it\n"); + return true; + } + if (item->getLanguage()==SrcLangExt_Cpp) + { + QCString key=und->name(); + if (!und->getUsedNamespaces().empty() && visited.find(key.str())==visited.end()) { - QCString key=und->name(); - if (und->getUsedNamespaces() && visited.find(key.str())==visited.end()) - { - visited.insert(key.str()); - - if (accessibleViaUsingNamespace(visited,und->getUsedNamespaces(),item,explicitScopePart)) - { - //printf("] found it via recursion\n"); - return true; - } + visited.insert(key.str()); - visited.erase(key.str()); + if (accessibleViaUsingNamespace(visited,und->getUsedNamespaces(),item,explicitScopePart)) + { + //printf("] found it via recursion\n"); + return true; } + + visited.erase(key.str()); } - //printf("] Try via used namespace done\n"); } + //printf("] Try via used namespace done\n"); } return false; } @@ -937,9 +922,8 @@ int SymbolResolver::Private::isAccessibleFrom(AccessStack &accessStack, //printf("> found via used class\n"); goto done; } - NamespaceSDict *nl = m_fileScope->getUsedNamespaces(); StringUnorderedSet visited; - if (accessibleViaUsingNamespace(visited,nl,item)) + if (accessibleViaUsingNamespace(visited,m_fileScope->getUsedNamespaces(),item)) { //printf("> found via used namespace\n"); goto done; @@ -960,9 +944,8 @@ int SymbolResolver::Private::isAccessibleFrom(AccessStack &accessStack, //printf("> found via used class\n"); goto done; } - const NamespaceSDict *nl = nscope->getUsedNamespaces(); StringUnorderedSet visited; - if (accessibleViaUsingNamespace(visited,nl,item)) + if (accessibleViaUsingNamespace(visited,nscope->getUsedNamespaces(),item)) { //printf("> found via used namespace\n"); goto done; -- cgit v0.12