[cfe-dev] Clang rewriter: what can be effectively removed from the buffer?

Ted Kremenek kremenek at apple.com
Tue Oct 30 11:48:32 PDT 2012


On Oct 30, 2012, at 7:00 AM, GabrieleCocco <cocco at di.unipi.it> wrote:

> Hi to everybody.
> I'm finding problems in removing some elements inside a translation unit
> using Rewriter.
> The interesting elements are:
> 
> - Function declaration/definition: 
> rewriter.RemoveText(function->getLocation()); removes only the function
> name.
> rewriter.RemoveText(function->getSourceRange()); doesn't remove anything

This could be a bug.  What is the range returned for the entire function?

> rewriter.RemoveText(function->getBody()->getSourceRange()); removes only the
> body (obviously)
> 
> Is there a way to remove the entire declaration/definition?

The rewriter just operates on source ranges.  It has no notion of the semantics of the code itself.  If you cannot insert/remove text, it should (generally speaking) be an issue with the ranges.

> 
> - Attributes
> Can be removed from the rewriter buffer? It seems it doesn't work.

There might not be source ranges for attributes, which would prevent them from being removed.

> 
> - Macro expanded to function calls
> Is there any way to remove macros that get expanded to function calls?
> Example:
> 
> #define this_opencl_device(p, d) call_this_opencl_device(p, d);
> 
> ...
> int* a,*b,*c;
> 
> this_opencl_device(0, 1)  // <- remove this
> foo(a, b, c, 1024);
>
I don't recall what the current state of what information from the preprocessor is preserved in the AST, but certainly the SourceLocations for an expressions allow you to walk the instantiation chain (the SourceLocation will say that it is from a macro instantiation).

What I don't know is if the complete range for the macro instantiation is always available.  CIndex.cpp, which implements syntax coloring APIs, uses the preprocessing record for syntax coloring macro-related code.

Argyrios: do you know what information is available here for getting the entire range of the macro instantiation?

> 
> - Include directives
> Is there a way to insert or remove include directives? I would like the code
> processed by my clang tool to be transformed into something that requires to
> import a local header, and I would like to produce the appropriate include
> directive.

The rewriter is just inserting *text*.  You can insert arbitrary text.

> 
> Thank you very much!
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20121030/b632d5a5/attachment.html>


More information about the cfe-dev mailing list