[cfe-dev] how to get AST content from CXCursor?

Jacob Carlborg via cfe-dev cfe-dev at lists.llvm.org
Thu Oct 25 02:31:05 PDT 2018


On 2018-10-25 10:02, Lou Wynn via cfe-dev wrote:
> Hi,
> 
> I'm traversing an AST by using the CXCursorVisitor. How can I get the 
> node content from the cursor? For example, for a CXCursor_BinaryOperator 
> cursor, I'd like to get the binary operator itself. I guess that the 
> information is included in the CXCursor struct, but I haven't found 
> examples of how to use it? Any help is appreciated.

I'm not sure if there's a better way but you can get the tokens of the 
expression. Example, assuming the current cursor is the binary operator:

1. Get the translation unit using clang_Cursor_getTranslationUnit
2. Get the source extent of the cursor using clang_getCursorExtent
3. Tokenize the source extent using clang_tokenize, this will give you 
an array of tokens
4. For each token:
   1. Get the token kind using clang_getTokenKind
   2. Get the token spelling using clang_getTokenSpelling
5. Find the token with the kind CXToken_Punctuation
6. The spelling for this token will contain the binary operator as a string

-- 
/Jacob Carlborg




More information about the cfe-dev mailing list