[cfe-dev] SorceLocation of end of iclude list and of main

Douglas Gregor dgregor at apple.com
Wed Aug 18 07:47:22 PDT 2010


On Aug 18, 2010, at 3:15 AM, ilya.mirsky at gmail.com wrote:

> Hi dear list,
> 
> I have 2 questions please:
> 
> 1. I want to insert some global data right after the includes list using the Rewriter. How can I find out the right SourceLocation?

Lexer::ComputePreamble will give you the source location just after the initial set of comments, #includes, and other preprocessor directives. You can turn that offset into a source location using the SourceManager.

> 2. I want to insert some code just before the main function. I'm able to find the SourceLocation of main FunctionDecl, and the method getLocStart gives me the location between the int and the main (int ^main()...). How can get the location of the line just above the main?


You can get the beginning of the declaration of main() using FunctionDecl::getTypeSourceInfo() and then getting the beginning of the TypeLoc inside of it. To actually get to the start of a line, you'll have to adjust that location by looking at the contents of the file/buffer or just guessing that it makes sense to start at the first column on the previous line.

	- Doug



More information about the cfe-dev mailing list