<br><br><div class="gmail_quote">On Tue, May 15, 2012 at 5:49 AM, Douglas Gregor <span dir="ltr"><<a href="mailto:dgregor@apple.com" target="_blank">dgregor@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im"><br>
On May 14, 2012, at 5:52 PM, Lukhnos Liu wrote:<br>
<br>
> Hi,<br>
><br>
> I'm writing a refactoring tool as a way to learn Clang internals. A problem I run into is: how do you get the locations of the tokens that are not present in the AST?<br>
><br>
> What I'm doing is like this: I have an ASTConsumer, which I call ParseAST to run. In the consumer, I look for CXXConstructorDecl with the TraverseDecl method, then I want to rewrite the method (for example, to move the function body from the header to an implementation file).<br>

><br>
> Getting the function body is easy, but I'm not sure what to do with the initializers. For each item in the initializer list that I get from init_begin() and init_end(), the source range encompasses the item itself, but not the preceding colon or the in-between commas. Is there a way to get those parts from the AST, or do I need to get those lex info elsewhere?<br>

<br>
</div>The typical way to handle this is to  point the lexer to the position just before where you expect to see the token, the lex until you find the token you're looking for. It doesn't work in the cases of insane macro hackery, but it works fairly well in general.<br>

<br>
        - Doug<br></blockquote></div><br><br>A less complicated idea would be to always infer them from the information you have:<br>- put the colon right after the closing paren<br>- put the comma right after the closing paren<br>
<br>It may change the file slightly, but that will harmonize the style and won't cause any major issue.<br><br>-- Matthieu<br>