[cfe-dev] [PATCH] Friend class cannot access protected field of parent

Olivier Goffart ogoffart at kde.org
Mon Jun 7 08:36:46 PDT 2010


Hi,

So I spend my week end, trying to get into the clang code. I chosen to try to 
fix Bug 6840 [1] which is one of the last issue preventing Qt to compile.


So the problem is with code like this:

class N { protected: static int m; };
class P : public N { friend class R; };
class R { int foo() { return N::n; } };

The C++ specification says that [class.access.base]
A member m is accessible at the point R when named in class N if [...]
 m as a member of N is protected, and R occurs in a [...] friend of a class P
 derived from N, where m as a member of P is public, private, or protected

So this code should compile, because in R::foo() we can access N::m though P's 
friendship.

But it does not with clang currently.

The problem is that when checking the access of N::n within the R::foo() 
context, we do not have information about P. We know which class are friend of 
N (that's enough for private:), but we don't know which class R is friend of.

I called that the reverseFriend relationship, and I added this information 
onto the CXXRecordDecl and the FunctionDecl.

You can find the patch attached.

It is not complete, as it does not work with template friends yet. (see FIXME 
in the test). But it works with the common case.

How do you find it?

Regards
-- 
Olivier


[1] http://llvm.org/bugs/show_bug.cgi?id=6840
-------------- next part --------------
A non-text attachment was scrubbed...
Name: clang_6840.diff
Type: text/x-patch
Size: 17582 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20100607/a9a5e985/attachment.bin>


More information about the cfe-dev mailing list