diff options
author | Douglas Gregor <dgregor@apple.com> | 2009-02-24 04:26:15 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2009-02-24 04:26:15 +0000 |
commit | 25d944af5d8d665611e09956954f10896c1071f6 (patch) | |
tree | 7d5409c1bf505495c11a54ea5f26d4c8a9858963 /lib/Sema/SemaDecl.cpp | |
parent | 8a219ceda2b5afd447e7199b9c53079bead31b89 (diff) |
In C, when we see a function declaration within a local scope, export
that declaration to global scope so that it can be found from other
scopes. This allows us to diagnose redeclaration errors for external
declarations across scopes. We also warn when name lookup finds such
an out-of-scope declaration. This is part of <rdar://problem/6127293>;
we'll also need to do the same thing for variables.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@65373 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema/SemaDecl.cpp')
-rw-r--r-- | lib/Sema/SemaDecl.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 634017330d..00136badfe 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -2033,6 +2033,58 @@ Sema::ActOnFunctionDeclarator(Scope* S, Declarator& D, DeclContext* DC, InvalidDecl = true; } } + + if (!getLangOptions().CPlusPlus && CurContext->isFunctionOrMethod()) { + // If this is a function declaration in local scope, inject its + // name into the top-level scope so that it will be visible to + // later uses and declarations of the same function, since the + // function is external. + // FIXME: We don't do this in C++ because, although we would like + // to get the extra checking that this operation implies, + // the declaration itself is not visible according to C++'s rules. + IdentifierResolver::iterator I = IdResolver.begin(Name), + IEnd = IdResolver.end(); + NamedDecl *PrevIdDecl = 0; + while (I != IEnd && !isa<TranslationUnitDecl>((*I)->getDeclContext())) { + PrevIdDecl = *I; + ++I; + } + + if (I == IEnd) { + // No name with this identifier has been declared at translation + // unit scope. Add this name into the appropriate scope. + if (PrevIdDecl) + IdResolver.AddShadowedDecl(NewFD, PrevIdDecl); + else + IdResolver.AddDecl(NewFD); + TUScope->AddDecl(NewFD); + return NewFD; + } + + if (isa<TagDecl>(*I)) { + // The first thing we found was a tag declaration, so insert + // this function so that it will be found before the tag + // declaration. + if (PrevIdDecl) + IdResolver.AddShadowedDecl(NewFD, PrevIdDecl); + else + IdResolver.AddDecl(NewFD); + TUScope->AddDecl(NewFD); + } else if (isa<FunctionDecl>(*I) && NewFD->declarationReplaces(*I)) { + // We found a previous declaration of the same function. Replace + // that declaration with this one. + TUScope->RemoveDecl(*I); + TUScope->AddDecl(NewFD); + IdResolver.RemoveDecl(*I); + if (PrevIdDecl) + IdResolver.AddShadowedDecl(NewFD, PrevIdDecl); + else + IdResolver.AddDecl(NewFD); + } + + return NewFD; + } + return NewFD; } |