Expr walk up? Looking for Enum name not value?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Expr walk up? Looking for Enum name not value?

steveneliuk
This post was updated on .
Hi there,

There are various implemented visit*() statements in myRecursiveASTVisitor() and I am looking for a way to obtain the actual enum name before it is converted to a value. The value works fine for Source-to-Source translation but some have complained because it is not as easy to read. I guess it is a MacroID and therefore is not contained in the original source. I can think of many laborious ways to obtain this but I know there has to be a built in function to obtain the name,

ie)
cl_device_type CL_DEVICE_TYPE_ALL has a value of 4294967295U, but I need CL_DEVICE_TYPE_ALL to keep the translation easy to read.

Any advice would be appreciated!!!

I think this may be referred to as enumConstDecl, but not sure...
Reply | Threaded
Open this post in threaded view
|

Re: Expr walk up? Looking for Enum name not value?

Gabor Kozar-2
Hi,

If I understand your problem correctly, what you need is to look for typecasts (e.g. ImplicitCast) where the source type is an enum. I actually do not know how enum values appear in the AST, i.e. whether they are expanded to their values, but I'm assuming that they are not: in this case the typecast AST node probably has an EnumConstantDecl as a child (somewhere below an Expr), which you should be able to find without too much of an issue, and it will give you the information you need. Try dumping some example code (using enums) AST to figure out what is it you exactly need to do.

Also consult the documentation for EnumDecl and EnumConstantDecl types:

Hope this helps!

Gabor


2013/4/18 steveneliuk <[hidden email]>
Hi there,

There are various implemented visit*() statements in myRecursiveASTVisitor()
and I am looking for a way to obtain the actual enum name before it is
converted to a value. The value works fine for Source-to-Source translation
but some have complained because it is not as easy to read. I guess it is a
MacroID and therefore is not contained in the original source. I can think
of many laborious ways to obtain this but I know there has to be a built in
function to obtain the name,

ie)
cl_device_type CL_DEVICE_TYPE_ALL has a value of 4294967295U, but I need
CL_DEVICE_TYPE_ALL to keep the translation easy to read.

Any advice would be appreciated!!!



--
View this message in context: http://clang-developers.42468.n3.nabble.com/Expr-walk-up-Looking-for-Enum-name-not-value-tp4031582.html
Sent from the Clang Developers mailing list archive at Nabble.com.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Expr walk up? Looking for Enum name not value?

Lukas Vogel
Hi,

If you use VisitEnumDecl(ed) you can use an iterator to loop over all EnumConstantDecl and from those you can get the name with getNameAsString()
I implemented this myself just few days ago (if you want to take a look: https://github.com/lukedirtwalker/Envision/blob/cppimport/CppImport/src/ClangAstVisitor.cpp#L321 )

Cheers


2013/4/20 Gábor Kozár <[hidden email]>
Hi,

If I understand your problem correctly, what you need is to look for typecasts (e.g. ImplicitCast) where the source type is an enum. I actually do not know how enum values appear in the AST, i.e. whether they are expanded to their values, but I'm assuming that they are not: in this case the typecast AST node probably has an EnumConstantDecl as a child (somewhere below an Expr), which you should be able to find without too much of an issue, and it will give you the information you need. Try dumping some example code (using enums) AST to figure out what is it you exactly need to do.

Also consult the documentation for EnumDecl and EnumConstantDecl types:

Hope this helps!

Gabor


2013/4/18 steveneliuk <[hidden email]>
Hi there,

There are various implemented visit*() statements in myRecursiveASTVisitor()
and I am looking for a way to obtain the actual enum name before it is
converted to a value. The value works fine for Source-to-Source translation
but some have complained because it is not as easy to read. I guess it is a
MacroID and therefore is not contained in the original source. I can think
of many laborious ways to obtain this but I know there has to be a built in
function to obtain the name,

ie)
cl_device_type CL_DEVICE_TYPE_ALL has a value of 4294967295U, but I need
CL_DEVICE_TYPE_ALL to keep the translation easy to read.

Any advice would be appreciated!!!



--
View this message in context: http://clang-developers.42468.n3.nabble.com/Expr-walk-up-Looking-for-Enum-name-not-value-tp4031582.html
Sent from the Clang Developers mailing list archive at Nabble.com.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev



_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Expr walk up? Looking for Enum name not value?

steveneliuk
Not using VisitEnumDecl(), i am using VisitCallExpr() and rewriting the called expression and all subexpressions in the param list.

Your advice is appreciated and I shall update with my solution shortly,

Kindest Regards,
Reply | Threaded
Open this post in threaded view
|

Re: Expr walk up? Looking for Enum name not value?

steveneliuk
hmm, not much luck there.

From an expression I can traverse the subexpressions/args with clang::CallExpr::arg_iterator argIt = CE->arg_begin(). As mentioned in the first post, this gives the value of the enum and I need the name as included in the original source code,

I see no good method looking at the Expr class for Clang,

If someone has anymore directional advice, or a solution, I would greatly appreciate it... burning too many cycles on this.

Kindest Regards,
Reply | Threaded
Open this post in threaded view
|

Re: Expr walk up? Looking for Enum name not value?

steveneliuk
Arggg... should have been more careful. Turns out these are defines and not typedef enums in OpenCL.

Foolish, guess Im back to square one. I figure I should be able to query the preprocessor and possibly get the info.

In the worst case, since I know the location in the source for the start position, would it be a bad idea just to read until an escape character is seen?
Reply | Threaded
Open this post in threaded view
|

Re: Expr walk up? Looking for Enum name not value?

steveneliuk
Here is some example code if say you have a VisitCallExpr,

Reminder, this doesnt answer my original question but if your looking for MacroName this will do it.

bool VisitCallExpr(CallExpr *CE){
//traverse the subexpressions/args
for(clang::CallExpr::arg_iterator argHead = CE->arg_begin(),
     argTail = CE->arg_end();
     argHead != argTail;
     ++argHead){
SourceRange range = argHead->getSourceRange();
if(range.getBegin().isMacroID() || range.getEnd().isMacroID()){
    std::string macroName(_preprocessor->getImmediateMacroName(range.getBegin()).str());
}

}
}