[cfe-dev] Function Rewriting

Makslane Araújo Rodrigues makslane at hotmail.com
Wed Aug 12 05:58:47 PDT 2009


Ok, I've found a solution for now (is not neateful, but works for me :-) ).Thank you all for the tips!
void RewriteFunctiont(FunctionDecl *FD) 	{		//Get the start location of the function name		SourceLocation loc = FD->getLocStart();
		// Decompose the location into a FID/Offset pair.		std::pair<FileID, unsigned> LocInfo = Context->getSourceManager().getDecomposedLoc(loc);		FileID FID = LocInfo.first;		unsigned FileOffset = LocInfo.second, offset = 0;
		// Get information about the buffer it points into.		std::pair<const char*, const char*> BufferInfo = Context->getSourceManager().getBufferData(FID);		const char *BufStart = BufferInfo.first;
		// Pointer to the buffer		const char *TokPtr = BufStart+FileOffset;						//Advance to the parameter list		while(*TokPtr != '(' && *TokPtr != NULL) 		{			TokPtr++;			offset++;		}
		//Insert as first parameters (listDeclaration contains the list of declarations to insert)
		int i = 0;		for(std::list<std::string>::iterator it = listDeclaration.begin(); it != listDeclaration.end(); it++)		{				if(i > 0) Rewrite.InsertTextAfter(loc.getFileLocWithOffset(offset+1), ", ", 2);

			Rewrite.InsertTextAfter(loc.getFileLocWithOffset(offset+1), (*it).c_str(), (*it).length());			i++;		}				if(i && FD->param_size() > 0)		{			Rewrite.InsertTextAfter(loc.getFileLocWithOffset(offset+1), ", ", 2);		}	}

> Date: Tue, 11 Aug 2009 09:45:15 -0700
> From: rjmccall at apple.com
> To: cfe-dev at cs.uiuc.edu
> Subject: Re: [cfe-dev] Function Rewriting
> 
> Cédric Venet wrote:
> > I didn't respond earlier because I do not remember exactly the function 
> > to use. I do not think this is a bug, but a design decision. it is 
> > always only the start which is in the AST (for single token construct at 
> > least), to get the end of the token, use a Lex function lextoken or 
> > something like this. However, this is just my understanding. you can 
> > check how the caret diagnostic are generated, it will show you the good 
> > way to do what you want.
> > 
> 
> No, it's a bug. I'm not sure how that function works well enough to 
> tell you if it works after the parse phase, but regardless, in C/C++, 
> knowing the endpoint of the identifier token does not actually tell you 
> where the end of the parameter declaration is.
> 
> void apply(int count, int /*unused */, int array[], int 
> (*function)(void ()) = &standard_callback);
> 
> John.
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev

_________________________________________________________________
Novo Internet Explorer 8. Baixe agora, é grátis!
http://brasil.microsoft.com.br/IE8/mergulhe/?utm_source=MSN%3BHotmail&utm_medium=Tagline&utm_campaign=IE8
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20090812/990649f4/attachment.html>


More information about the cfe-dev mailing list