<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<p><font size="+1">I've found some functions in the
tools/libclang/CXCursor.h file, and they are:</font></p>
<p><tt><font size="+1">std::pair<OverloadedDeclRefStorage,
SourceLocation><br>
getCursorOverloadedDeclRef(CXCursor C);<br>
<br>
const Decl *getCursorDecl(CXCursor Cursor);<br>
const Expr *getCursorExpr(CXCursor Cursor);<br>
const Stmt *getCursorStmt(CXCursor Cursor);<br>
const Attr *getCursorAttr(CXCursor Cursor);<br>
const Decl *getCursorParentDecl(CXCursor Cursor);<br>
<br>
ASTContext &getCursorContext(CXCursor Cursor);<br>
ASTUnit *getCursorASTUnit(CXCursor Cursor);<br>
CXTranslationUnit getCursorTU(CXCursor Cursor);<br>
<br>
void getOverriddenCursors(CXCursor cursor,<br>
SmallVectorImpl<CXCursor>
&overridden);<br>
</font></tt></p>
<p><font size="+1"><br>
</font></p>
<p><font size="+1">Some of them are implemented as follows:</font></p>
<p><tt><font size="+1">const Decl *cxcursor::getCursorDecl(CXCursor
Cursor) {<br>
return static_cast<const Decl *>(Cursor.data[0]);<br>
}<br>
</font></tt></p>
<p><font size="+1"><tt>const Stmt *cxcursor::getCursorStmt(CXCursor
Cursor) {</tt><tt><br>
</tt><tt> if (Cursor.kind == CXCursor_ObjCSuperClassRef ||</tt><tt><br>
</tt><tt> Cursor.kind == CXCursor_ObjCProtocolRef ||</tt><tt><br>
</tt><tt> Cursor.kind == CXCursor_ObjCClassRef)</tt><tt><br>
</tt><tt> return nullptr;</tt><tt><br>
</tt><tt><br>
</tt><tt> return static_cast<const Stmt
*>(Cursor.data[1]);</tt><tt><br>
</tt><tt>}</tt><tt><br>
</tt><tt><br>
</tt><tt>const Attr *cxcursor::getCursorAttr(CXCursor Cursor) {</tt><tt><br>
</tt><tt> return static_cast<const Attr
*>(Cursor.data[1]);</tt><tt><br>
</tt><tt>}</tt><tt><br>
</tt><tt><br>
</tt><tt>const Decl *cxcursor::getCursorParentDecl(CXCursor
Cursor) {</tt><tt><br>
</tt><tt> return static_cast<const Decl
*>(Cursor.data[0]);</tt><tt><br>
</tt><tt>}</tt></font><font size="+1"><br>
</font></p>
<p><font size="+1"><br>
</font></p>
<p><font size="+1">But they are not exported in the libClang. I'm
wondering if I can use them in my code, and how? Can anyone give
me any hints on when to use data[i], where i = 0, 1, 2? This
would be the best way for me to get the contents of a node and
do source-to-source translation, if it works.</font></p>
<pre class="moz-signature" cols="72">Love,
Lou
</pre>
<div class="moz-cite-prefix">On 10/25/18 5:31 PM, Jacob Carlborg via
cfe-dev wrote:<br>
</div>
<blockquote type="cite" cite="mid:pqs2cn$2jp$1@blaine.gmane.org">On
2018-10-25 10:02, Lou Wynn via cfe-dev wrote:
<br>
<blockquote type="cite">Hi,
<br>
<br>
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.
<br>
</blockquote>
<br>
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:
<br>
<br>
1. Get the translation unit using clang_Cursor_getTranslationUnit
<br>
2. Get the source extent of the cursor using clang_getCursorExtent
<br>
3. Tokenize the source extent using clang_tokenize, this will give
you an array of tokens
<br>
4. For each token:
<br>
1. Get the token kind using clang_getTokenKind
<br>
2. Get the token spelling using clang_getTokenSpelling
<br>
5. Find the token with the kind CXToken_Punctuation
<br>
6. The spelling for this token will contain the binary operator as
a string
<br>
<br>
</blockquote>
</body>
</html>