[cfe-dev] CXXMethodDecl body is empty !

Ismail Pazarbasi ismail.pazarbasi at gmail.com
Sat Nov 1 06:40:35 PDT 2014


On Fri, Oct 31, 2014 at 5:22 PM, zaki <z.ahadri at myappconverter.com> wrote:
> Hi,
>
> my cpp class
>
> class Rectangle{
>
> public :
>               void add(int a,int b){
>                             retrun a+b;
>                     }
>
> }
> i'm trying to get my c++ method body, by calling CXXMethodDecl->getBody()
> but i get no body , is it the right method to call
>
> Thank you
>
>
>
>
>
> --
> View this message in context: http://clang-developers.42468.n3.nabble.com/CXXMethodDecl-body-is-empty-tp4042306.html
> Sent from the Clang Developers mailing list archive at Nabble.com.
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev

Presumably, this is not the exact copy of the code you have tested.
Because your example is ill-formed; there is a typo in `return`
keyword, function return type is `void`, but it's trying to return an
`int`. Finally, there is no semi after class body. Clang won't create
an AST for ill-formed code.

Whether you can get the body depends on when you call `getBody`. For
that to work, it must first parse the body. I would suggest you to
start working with AST when it's fully parsed, as this will cause less
confusion; do the work inside your implementation of
ASTConsumer::HandleTranslationUnitDecl, where you will have
translation unit completely parsed for a well-formed code.

Ismail



More information about the cfe-dev mailing list