Clang C interface `clang_visitChildren` problems

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

Clang C interface `clang_visitChildren` problems

Boris Kolpackov via cfe-dev


hi,

I’m using a Clang C interface clang_visitChildren to traverse the abstract syntax tree in Clang. But I get following problems :

  1. Get operators in the expression :
    For expressions like a+b, I can get the current cursor information (name, kind, type) and its child node info, but I don’t know how can I get the operator ‘+’ ? Should I use token to get the operators in expression? If so, please tell me how can I use token to get operators.
  2. Get the value of constants :
    For expressions like int a =10, for the node 10, I can get the result by using clang_Cursor_Evaluate interface, and then call the function clang_EvalResult_getAsInt to get the integer value 10. But, for expressions like int a = 2+8, I got 2+8 and keep traverse down, using the same approach, then I got two 0. Did get the value by a wrong way?What’s the correct way to get the value?
  3. Location order of the child node in the abstract syntax tree :
    If I have two statement for (; ; a) {...} and for (; a; ) {...}, I use clang_visitChildren to traverse them, then I got two subtree: UnexposedExpr and CompoundStmt, How can I separate the two statements? And why did I get UnexposedExpr, what does it means?
I don't know how to solve these problems, I hope that someone can help. 
Thank you in advance, Freya

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Clang C interface `clang_visitChildren` problems

Boris Kolpackov via cfe-dev
Hi,


On 15 March 2017 at 03:24, freya zhou via cfe-dev <[hidden email]> wrote:


hi,

I’m using a Clang C interface clang_visitChildren to traverse the abstract syntax tree in Clang. But I get following problems :

  1. Get operators in the expression :
    For expressions like a+b, I can get the current cursor information (name, kind, type) and its child node info, but I don’t know how can I get the operator ‘+’ ? Should I use token to get the operators in expression? If so, please tell me how can I use token to get operators.
I scanned the API but it seems like there's no way to get the operator kind right now. I might be wrong though.

You could probably try to get the operator kind by looking at the tokens, yeah. For binary operators you could try tokenizing the range from the end of LHS to the start of RHS, and look at the first token, e.g:

CXSourceRange LHS_Range = clang_getCursorExtent(BinOp_LHS);
CXSourceRange RHS_Range = clang_getCursorExtent(BinOp_RHS);
CXToken *Tokens; unsigned NumTokens;
clang_tokenize(TU, clang_getRange(clang_getRangeEnd(BinOp_LHS), clang_getRangeStart(BinOp_RHS)),
                                   &Tokens, &NumTokens);
clang_getTokenSpelling(Tokens[0]); // Should be '+'

Note that this will only work for simple expressions: it will not work correctly when the operator is in a macro. It might not work in some C++ cases as well.
  1. Get the value of constants :
    For expressions like int a =10, for the node 10, I can get the result by using clang_Cursor_Evaluate interface, and then call the function clang_EvalResult_getAsInt to get the integer value 10. But, for expressions like int a = 2+8, I got 2+8 and keep traverse down, using the same approach, then I got two 0. Did get the value by a wrong way?What’s the correct way to get the value?
Libclang should be able to evaluate 2+8. Did you try stopping at 2+8 and then evaluating that cursor?
  1. Location order of the child node in the abstract syntax tree :
    If I have two statement for (; ; a) {...} and for (; a; ) {...}, I use clang_visitChildren to traverse them, then I got two subtree: UnexposedExpr and CompoundStmt, How can I separate the two statements? And why did I get UnexposedExpr, what does it means?
Unexposed expression is a special type of expression that is not currently supported by the C API. In other words, while the actual AST might be a valid expression like an Objective-C array literal, libclang's C API can't represent that expression type in terms of CXCursor, so it falls back to using CXCursor_UnexposedExpr. 

In your particular example I think the unexposed expression corresponds to the 'a'. What do you mean by separating the two statements?

 
I don't know how to solve these problems, I hope that someone can help. 
Thank you in advance, Freya

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev