[cfe-dev] Friend Declaration in clang

John McCall rjmccall at apple.com
Tue Jan 22 11:36:19 PST 2013


On Jan 21, 2013, at 4:30 AM, madil90 <madil90 at gmail.com> wrote:
>    My goal is to find out whether every c++ class declaration in a file has
> an overloaded "<<" stream operator. While traversing the AST, whenever I
> encounter a CXXRecordDecl, I want to find out whether that recorddecl has an
> overloaded operator with the spelling "<<". The problem is that the "<<"
> operator is always declared as a friend method. When I traverse frienddecl,
> I am unable to obtain a CXXMethodDecl from it (to check whether it's an
> operator). Can you please tell me how can friend operators be detected?
> consider the following code as an example.
> 
> class A
> {
> friend ostream &operator<<(ostream& out, A A1){}
> };

This is not a CXXMethodDecl because it's not a declaration of a C++
method;  it's a declaration of a namespace-scope function.  You should
just check for a FunctionDecl.

John.



More information about the cfe-dev mailing list