From 5407920f82618114a66e92bf5dad280f42a4075c Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Fri, 6 Jan 2012 22:05:37 +0000 Subject: [PATCH] During name lookup, use redecl_iterator to walk over the redeclaration chain to determine whether any declaration of the given entity is visible, eliminating the redundant (and less efficient) getPreviousDeclaration() implementation. This tweak uncovered an omission in the handling of RedeclarableTemplateDecl, where we weren't making sure to search for additional redeclarations of a template in other module files. Things would be cleaner if RedeclarableTemplateDecl actually used Redeclarable. llvm-svn: 147687 --- clang/lib/Sema/SemaLookup.cpp | 29 ++++++----------------------- clang/lib/Serialization/ASTReaderDecl.cpp | 7 +++++-- 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp index 7ee50e3..984184b 100644 --- a/clang/lib/Sema/SemaLookup.cpp +++ b/clang/lib/Sema/SemaLookup.cpp @@ -1053,26 +1053,6 @@ bool Sema::CppLookupName(LookupResult &R, Scope *S) { return !R.empty(); } -/// \brief Retrieve the previous declaration of D. -static NamedDecl *getPreviousDeclaration(NamedDecl *D) { - if (TagDecl *TD = dyn_cast(D)) - return TD->getPreviousDeclaration(); - if (VarDecl *VD = dyn_cast(D)) - return VD->getPreviousDeclaration(); - if (FunctionDecl *FD = dyn_cast(D)) - return FD->getPreviousDeclaration(); - if (RedeclarableTemplateDecl *RTD = dyn_cast(D)) - return RTD->getPreviousDeclaration(); - if (TypedefNameDecl *TD = dyn_cast(D)) - return TD->getPreviousDeclaration(); - if (ObjCInterfaceDecl *ID = dyn_cast(D)) - return ID->getPreviousDeclaration(); - if (ObjCProtocolDecl *PD = dyn_cast(D)) - return PD->getPreviousDeclaration(); - - return 0; -} - /// \brief Retrieve the visible declaration corresponding to D, if any. /// /// This routine determines whether the declaration D is visible in the current @@ -1085,9 +1065,12 @@ static NamedDecl *getVisibleDecl(NamedDecl *D) { if (LookupResult::isVisible(D)) return D; - while ((D = getPreviousDeclaration(D))) { - if (LookupResult::isVisible(D)) - return D; + for (Decl::redecl_iterator RD = D->redecls_begin(), RDEnd = D->redecls_end(); + RD != RDEnd; ++RD) { + if (NamedDecl *ND = dyn_cast(*RD)) { + if (LookupResult::isVisible(ND)) + return ND; + } } return 0; diff --git a/clang/lib/Serialization/ASTReaderDecl.cpp b/clang/lib/Serialization/ASTReaderDecl.cpp index 51a275a..24ac976 100644 --- a/clang/lib/Serialization/ASTReaderDecl.cpp +++ b/clang/lib/Serialization/ASTReaderDecl.cpp @@ -255,7 +255,7 @@ namespace clang { void VisitParmVarDecl(ParmVarDecl *PD); void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); void VisitTemplateDecl(TemplateDecl *D); - void VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D); + RedeclarableResult VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D); void VisitClassTemplateDecl(ClassTemplateDecl *D); void VisitFunctionTemplateDecl(FunctionTemplateDecl *D); void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); @@ -1219,7 +1219,8 @@ void ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) { D->init(TemplatedDecl, TemplateParams); } -void ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) { +ASTDeclReader::RedeclarableResult +ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) { // Initialize CommonOrPrev before VisitTemplateDecl so that getCommonPtr() // can be used while this is still initializing. enum RedeclKind { FirstDeclaration, FirstInFile, PointsToPrevious }; @@ -1278,6 +1279,8 @@ void ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) { VisitTemplateDecl(D); D->IdentifierNamespace = Record[Idx++]; + + return RedeclarableResult(Reader, FirstDeclID); } void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) { -- 2.7.4