<div dir="ltr">Hi,<div><br></div><div class="gmail_extra"><br><div class="gmail_quote">On 15 March 2017 at 03:24, freya zhou via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><p style="margin:0px 0px 1.2em"><br class="gmail-m_2233291301977104631gmail-Apple-interchange-newline">hi,<br></p><p style="margin:0px 0px 1.2em">I’m using a Clang C interface <code style="font-size:0.85em;font-family:consolas,inconsolata,courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline">clang_visitChildren</code> <wbr>to traverse the abstract syntax tree in Clang. But I get following problems :</p><ol style="margin:1.2em 0px;padding-left:2em"><li style="margin:0.5em 0px">Get operators in the expression :<br>For expressions like <code style="font-size:0.85em;font-family:consolas,inconsolata,courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline">a+b</code>, 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.</li></ol></div></blockquote><div>I scanned the API but it seems like there's no way to get the operator kind right now. I might be wrong though.<br></div><div><br></div><div>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:</div><div><br></div><div>CXSourceRange LHS_Range = clang_getCursorExtent(BinOp_LHS);<br></div><div>CXSourceRange RHS_Range = clang_getCursorExtent(BinOp_RHS);<br></div><div>CXToken *Tokens; unsigned NumTokens;</div><div><div>clang_tokenize(TU, clang_getRange(<span style="color:rgb(0,0,0)">clang_getRangeEnd(</span>BinOp_LHS<span style="color:rgb(0,0,0)">), </span><span style="color:rgb(0,0,0)">clang_getRangeStart(</span>BinOp_RHS<span style="color:rgb(0,0,0)">)</span>),</div>

<div>                                   &Tokens, &NumTokens);</div></div><div><span style="color:rgb(0,0,0)">clang_getTokenSpelling(Tokens[0]); // Should be '+'</span></div><div><span style="color:rgb(0,0,0)"><br></span></div><div><span style="color:rgb(0,0,0)">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.</span></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><ol style="margin:1.2em 0px;padding-left:2em"><li style="margin:0.5em 0px">Get the value of constants :<br>For expressions like <code style="font-size:0.85em;font-family:consolas,inconsolata,courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline">int a =10</code>, for the node <code style="font-size:0.85em;font-family:consolas,inconsolata,courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline">10</code>, I can get the result by using <code style="font-size:0.85em;font-family:consolas,inconsolata,courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline">clang_Cursor_Evaluate</code> <wbr>interface, and then call the function <code style="font-size:0.85em;font-family:consolas,inconsolata,courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline">clang_EvalResult_<wbr>getAsInt</code> to get the integer value 10. But, for expressions like <code style="font-size:0.85em;font-family:consolas,inconsolata,courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline">int a = 2+8</code>, I got <code style="font-size:0.85em;font-family:consolas,inconsolata,courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline">2+8</code> 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?</li></ol></div></blockquote><div>Libclang should be able to evaluate 2+8. Did you try stopping at 2+8 and then evaluating that cursor?</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><ol style="margin:1.2em 0px;padding-left:2em"><li style="margin:0.5em 0px">Location order of the child node in the abstract syntax tree :<br>If I have two statement <code style="font-size:0.85em;font-family:consolas,inconsolata,courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline">for (; ; a) {...}</code> and <code style="font-size:0.85em;font-family:consolas,inconsolata,courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline">for (; a; ) {...}</code>, I use <code style="font-size:0.85em;font-family:consolas,inconsolata,courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline">clang_visitChildren</code> to traverse them, then I got two subtree: <code style="font-size:0.85em;font-family:consolas,inconsolata,courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline">UnexposedExpr</code> and <code style="font-size:0.85em;font-family:consolas,inconsolata,courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline">Com<wbr>poundStmt</code>, How can I separate the two statements? And why did I get <code style="font-size:0.85em;font-family:consolas,inconsolata,courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline">UnexposedExpr</code>, what does it means?</li></ol></div></blockquote><div>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. </div><div><br></div><div>In your particular example I think the unexposed expression corresponds to the 'a'. What do you mean by separating the two statements?</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>I don't know how to solve these problems, I hope that someone <span style="color:rgb(51,51,51);font-family:arial,helvetica,verdana;font-size:12px">can help. </span></div><div>Thank you in advance, Freya</div></div>
<br>______________________________<wbr>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div></div>