[cfe-dev] Retrieving the element to be casted in "ExplicitCastExpr

Nikola Smiljanic popizdeh at gmail.com
Sun Feb 1 20:46:14 PST 2015


I'm pretty sure you need to get the actual DeclRefExpr node and obtain the
ValueDecl from it.

On Tue, Jan 27, 2015 at 10:52 PM, Pedro Delgado Perez <
pedro.delgadoperez at mail.uca.es> wrote:

>  Hi, I've been trying lots of things to solve this problem, but I have
> not found the solution yet.
> Nobody has encountered this problem before?
>
>
> ------------------------------------------------------------------------------------------
> In the program I am developing, I search for "ExplicitCastExpr" because I
> need to print the elements which are being casted. If the program retrieves
> this expression:
>
> static_cast<KWriteApp *>(kapp);
>
> what I want to print is "kapp".
>
> However, I have found a problem in that situation:
> - kapp is defined as:
> #define kapp KApplication::kApplication()
>
> - This is the AST of that expression:
>
> -CXXStaticCastExpr 0x4fcc118 <col:10, col:39> 'class KWriteApp *'
> static_cast<class KWriteApp *> <BaseToDerived (KApplication)>
> |       `-CallExpr 0x4fcc0e0 </usr/include/KDE/../kapplication.h:56:14,
> col:41> 'class KApplication *'
> |         `-ImplicitCastExpr 0x4fcc0c8 <col:14, col:28> 'class
> KApplication *(*)(void)' <FunctionToPointerDecay>
> |           `-DeclRefExpr 0x4fcc068 <col:14, col:28> 'class KApplication
> *(void)' lvalue CXXMethod 0x7f4c9d3c2fe0 'kApplication' 'class KApplication
> *(void)'
>
> As you can observe, "kapp" does not appear explicitly in the tree.
> When in my program I invoke "getSubExpr()" (from the object containing the
> "ExplicitCastExpr") to obtain and print "kapp":
>
>       SourceRange r = SourceRange(
>
> FS2->getSubExpr()->getSourceRange().getBegin(),
>
> FS2->getSubExpr()->getSourceRange().getEnd()
>                                 );
>       string element_casted = Rewrite.getRewrittenText(r);
>
> However, the range of the expression is empty. I have also tried with
> "getSubExprAsWritten()" but it makes no difference. This is the first time
> I have found a problem with the solution that I had implemented.
>
> The question is:
> How could I simply obtain what is being casted as it is written in the
> code? I do not refer only to this specific case, but a way to achieve my
> goal in general because I cannot understand this situation.
>
> Thanks in advance.
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150202/3da5b475/attachment.html>


More information about the cfe-dev mailing list