[cfe-dev] Tokens for rewriting C++ constructor initializers

Matthieu Monrocq matthieu.monrocq at gmail.com
Tue May 15 11:58:48 PDT 2012


On Tue, May 15, 2012 at 5:49 AM, Douglas Gregor <dgregor at apple.com> wrote:

>
> On May 14, 2012, at 5:52 PM, Lukhnos Liu wrote:
>
> > Hi,
> >
> > 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?
> >
> > 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).
> >
> > 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?
>
> 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.
>
>        - Doug
>


A less complicated idea would be to always infer them from the information
you have:
- put the colon right after the closing paren
- put the comma right after the closing paren

It may change the file slightly, but that will harmonize the style and
won't cause any major issue.

-- Matthieu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120515/86b90414/attachment.html>


More information about the cfe-dev mailing list