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

Pedro Delgado Perez pedro.delgadoperez at mail.uca.es
Mon Feb 2 03:02:55 PST 2015


 
Thank you for answering the question. I implemented an ad-hoc solution for this case, got the DeclRefExpr node and obtain the ValueDecl from it. The result was "kapplication" instead of "kapp".

#define kapp KApplication::kApplication()

In any case, as I commented in my message, I need a general solution for this. What if that expression is not a cast of a DeclRefExpr node? Maybe it is a cast of the returning element of the calling to a method. In this sense, what[rsquo]s the reason why I cannot simply retrieve the expression in parentheses as it is explicitly written in the code?


Thanks again.

El día 02 feb 2015 05:46, Nikola Smiljanic <popizdeh at gmail.com> escribió:
> 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 [ mailto: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 [ 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/b29609b8/attachment.html>


More information about the cfe-dev mailing list