Index: include/clang/AST/DeclBase.h =================================================================== --- include/clang/AST/DeclBase.h +++ include/clang/AST/DeclBase.h @@ -529,7 +529,7 @@ /// \brief Set whether the declaration is used, in the sense of odr-use. /// /// This should only be used immediately after creating a declaration. - void setIsUsed() { Used = true; } + void setIsUsed() { getCanonicalDecl()->Used = true; } /// \brief Mark the declaration used, in the sense of odr-use. /// Index: lib/AST/DeclBase.cpp =================================================================== --- lib/AST/DeclBase.cpp +++ lib/AST/DeclBase.cpp @@ -341,24 +341,26 @@ } bool Decl::isUsed(bool CheckUsedAttr) const { - if (Used) + const Decl* canonD = getCanonicalDecl(); + if (canonD->Used) return true; // Check for used attribute. - if (CheckUsedAttr && hasAttr()) + if (CheckUsedAttr && canonD->hasAttr()) return true; return false; } void Decl::markUsed(ASTContext &C) { - if (Used) + if (isUsed()) return; +// Q: Do we need to call the callback with the canonical decl? if (C.getASTMutationListener()) C.getASTMutationListener()->DeclarationMarkedUsed(this); - Used = true; + getCanonicalDecl()->Used = true; } bool Decl::isReferenced() const { Index: lib/Sema/SemaExpr.cpp =================================================================== --- lib/Sema/SemaExpr.cpp +++ lib/Sema/SemaExpr.cpp @@ -13011,17 +13011,9 @@ UndefinedButUsed.insert(std::make_pair(Func->getCanonicalDecl(), Loc)); } - // Normally the most current decl is marked used while processing the use and - // any subsequent decls are marked used by decl merging. This fails with - // template instantiation since marking can happen at the end of the file - // and, because of the two phase lookup, this function is called with at - // decl in the middle of a decl chain. We loop to maintain the invariant - // that once a decl is used, all decls after it are also used. - for (FunctionDecl *F = Func->getMostRecentDecl();; F = F->getPreviousDecl()) { - F->markUsed(Context); - if (F == Func) - break; - } + // It is sufficient for the canonical decl to be marked as used. + Func->getMostRecentDecl()->markUsed(Context); + } static void Index: lib/Serialization/ASTReaderDecl.cpp =================================================================== --- lib/Serialization/ASTReaderDecl.cpp +++ lib/Serialization/ASTReaderDecl.cpp @@ -3112,10 +3112,9 @@ Previous->IdentifierNamespace & (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type); + //Q: Redundant check with the new changes? // If the previous declaration is marked as used, then this declaration should // be too. - if (Previous->Used) - D->Used = true; // If the declaration declares a template, it may inherit default arguments // from the previous declaration. @@ -3864,8 +3863,8 @@ // FIXME: This doesn't send the right notifications if there are // ASTMutationListeners other than an ASTWriter. - // Maintain AST consistency: any later redeclarations are used too. - forAllLaterRedecls(D, [](Decl *D) { D->Used = true; }); + // // Maintain AST consistency: any later redeclarations are used too. + D->setIsUsed(); break; }