Expr walk up? Looking for Enum name not value?

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,

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!!!


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!



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 )


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,

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,

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

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;
SourceRange range = argHead->getSourceRange();
if(range.getBegin().isMacroID() || range.getEnd().isMacroID()){