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

Douglas Gregor dgregor at apple.com
Mon May 14 20:49:53 PDT 2012


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



More information about the cfe-dev mailing list