From 7b1fdbda2757cc4a7f25664475be44119d7f8e59 Mon Sep 17 00:00:00 2001 From: Ryan Flynn Date: Fri, 31 Jul 2009 02:52:19 +0000 Subject: [PATCH] PR3679 - enable #pragma weak aliasing. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@77660 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/ParseAST.cpp | 6 ++++++ lib/Sema/Sema.h | 13 ++++++++++++- lib/Sema/SemaDecl.cpp | 2 +- lib/Sema/SemaDeclAttr.cpp | 18 ++++++++++++------ 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/lib/Sema/ParseAST.cpp b/lib/Sema/ParseAST.cpp index 7fa8c595ee..a731401dcd 100644 --- a/lib/Sema/ParseAST.cpp +++ b/lib/Sema/ParseAST.cpp @@ -68,6 +68,12 @@ void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, Consumer->HandleTopLevelDecl(ADecl.getAsVal()); }; + // process any TopLevelDecls generated by #pragma weak + for (llvm::SmallVector::iterator + I = S.WeakTopLevelDecls().begin(), + E = S.WeakTopLevelDecls().end(); I != E; ++I) + Consumer->HandleTopLevelDecl(DeclGroupRef(*I)); + Consumer->HandleTranslationUnit(Ctx); if (PrintStats) { diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h index e41172a0d5..dbf65782fb 100644 --- a/lib/Sema/Sema.h +++ b/lib/Sema/Sema.h @@ -254,6 +254,13 @@ public: }; llvm::DenseMap WeakUndeclaredIdentifiers; + /// WeakTopLevelDecl - Translation-unit scoped declarations generated by + /// #pragma weak during processing of other Decls. + /// I couldn't figure out a clean way to generate these in-line, so + /// we store them here and handle separately -- which is a hack. + /// It would be best to refactor this. + llvm::SmallVector WeakTopLevelDecl; + IdentifierResolver IdResolver; /// Translation Unit Scope - useful to Objective-C actions that need @@ -381,6 +388,9 @@ public: llvm::SmallVector &getSwitchStack() { return CurBlock ? CurBlock->SwitchStack : FunctionSwitchStack; } + + /// WeakTopLevelDeclDecls - access to #pragma weak-generated Decls + llvm::SmallVector &WeakTopLevelDecls() { return WeakTopLevelDecl; } virtual void ActOnComment(SourceRange Comment); @@ -2948,7 +2958,8 @@ public: SourceLocation LParenLoc, SourceLocation RParenLoc); - void DeclApplyPragmaWeak(NamedDecl *D, WeakInfo &W); + NamedDecl *DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II); + void DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W); /// ActOnPragmaWeakID - Called on well formed #pragma weak ident. virtual void ActOnPragmaWeakID(IdentifierInfo* WeakName, diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 58df38b645..8ee798aaf7 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -5220,7 +5220,7 @@ void Sema::ActOnPragmaWeakAlias(IdentifierInfo* Name, if (PrevDecl) { if (!PrevDecl->hasAttr()) if (NamedDecl *ND = dyn_cast(PrevDecl)) - DeclApplyPragmaWeak(ND, W); + DeclApplyPragmaWeak(TUScope, ND, W); } else { (void)WeakUndeclaredIdentifiers.insert( std::pair(AliasName, W)); diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index a8efc2497a..08b4910a4f 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -1820,8 +1820,9 @@ void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *Attr /// DeclClonePragmaWeak - clone existing decl (maybe definition), /// #pragma weak needs a non-definition decl and source may not have one -static NamedDecl * DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) +NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) { + assert(isa(ND) || isa(ND)); NamedDecl *NewD = 0; if (FunctionDecl *FD = dyn_cast(ND)) { NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), @@ -1837,8 +1838,7 @@ static NamedDecl * DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) /// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak /// applied to it, possibly with an alias. -void Sema::DeclApplyPragmaWeak(NamedDecl *ND, WeakInfo &W) { - assert(isa(ND) || isa(ND)); +void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { if (!W.getUsed()) { // only do this once W.setUsed(true); if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) @@ -1846,7 +1846,13 @@ void Sema::DeclApplyPragmaWeak(NamedDecl *ND, WeakInfo &W) { NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias()); NewD->addAttr(::new (Context) AliasAttr(NDId->getName())); NewD->addAttr(::new (Context) WeakAttr()); - ND->getDeclContext()->addDecl(NewD); + WeakTopLevelDecl.push_back(NewD); + // FIXME: "hideous" code from Sema::LazilyCreateBuiltin + // to insert Decl at TU scope, sorry. + DeclContext *SavedContext = CurContext; + CurContext = Context.getTranslationUnitDecl(); + PushOnScopeChains(NewD, S); + CurContext = SavedContext; } else { // just add weak to existing ND->addAttr(::new (Context) WeakAttr()); } @@ -1862,8 +1868,8 @@ void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { if (ND->hasLinkage()) { WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier()); if (W != WeakInfo()) { - // Declaration referenced by #pragma weak before it was declared - DeclApplyPragmaWeak(ND, W); + // Identifier referenced by #pragma weak before it was declared + DeclApplyPragmaWeak(S, ND, W); WeakUndeclaredIdentifiers[ND->getIdentifier()] = W; } } -- 2.40.0