diff --git a/clang/lib/Sema/SemaAccess.cpp b/clang/lib/Sema/SemaAccess.cpp index 3103255d2001..4f9bf1c5edd7 100644 --- a/clang/lib/Sema/SemaAccess.cpp +++ b/clang/lib/Sema/SemaAccess.cpp @@ -1286,16 +1286,20 @@ static Sema::AccessResult CheckAccess(Sema &S, SourceLocation Loc, return Sema::AR_accessible; } -void Sema::HandleDelayedAccessCheck(DelayedDiagnostic &DD, Decl *Ctx) { - // Pretend we did this from the context of the newly-parsed - // declaration. If that declaration itself forms a declaration context, - // include it in the effective context so that parameters and return types of - // befriended functions have that function's access priveledges. - DeclContext *DC = Ctx->getDeclContext(); - if (isa(Ctx)) - DC = cast(Ctx); - else if (FunctionTemplateDecl *FnTpl = dyn_cast(Ctx)) - DC = cast(FnTpl->getTemplatedDecl()); +void Sema::HandleDelayedAccessCheck(DelayedDiagnostic &DD, Decl *decl) { + // Access control for names used in the declarations of functions + // and function templates should normally be evaluated in the context + // of the declaration, just in case it's a friend of something. + // However, this does not apply to local extern declarations. + + DeclContext *DC = decl->getDeclContext(); + if (FunctionDecl *fn = dyn_cast(decl)) { + if (!DC->isFunctionOrMethod()) DC = fn; + } else if (FunctionTemplateDecl *fnt = dyn_cast(decl)) { + // Never a local declaration. + DC = fnt->getTemplatedDecl(); + } + EffectiveContext EC(DC); AccessTarget Target(DD.getAccessData()); diff --git a/clang/test/CXX/class.access/p6.cpp b/clang/test/CXX/class.access/p6.cpp index 1112699c8b9a..fe3304a22253 100644 --- a/clang/test/CXX/class.access/p6.cpp +++ b/clang/test/CXX/class.access/p6.cpp @@ -153,3 +153,18 @@ namespace test6 { private_inner c; // expected-error {{ 'private_inner' is a private member of 'test6::A'}} }; } + +// PR9229 +namespace test7 { + void foo(int arg[1]); + class A { + void check(); + }; + class B { + friend class A; + A ins; + }; + void A::check() { + void foo(int arg[__builtin_offsetof(B, ins)]); + } +}