Clang rewriter: what can be effectively removed from the buffer?

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
rewriter.RemoveText(function->getBody()->getSourceRange()); removes only the
body (obviously)

Is there a way to remove the entire declaration/definition?

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

- 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);
...

- 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.

Thank you very much!

That looks like a bug. FunctionDecl::getSourceRange() should return the range for the entire function.

Sebastian

Ok, I'll try to file it as a bug.

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.

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?

The information is in the SourceManager, given a location inside a macro expansion, SourceManager::getExpansionRange() returns the full macro expansion range.