Failure to rewrite code using clang plugin

Hi, as part of a research project I had to tag certain typedefs with an attribute and then replace those tagged typedefs by some specific code.

I added the attribute to include/clang/Basic/Attr.td, recompiled clang, and then programmed a plugin using ASTMatchers, to detect all the typedefs and do the replacement (using Rewriter) if the attribute is present.

Now, if I use the plugin passing -XClang -plugin as compilation arguments, so that the compilation stops after using the plugin, the plugin works correctly and outputs modified source code. But if I use -XClang -add-plugin, so that the compilation carries on, Rewriter fails and I get a core dump.

Am I doing something wrong in here? Is Rewriter unusable unless it is used as a plugin? If you were to do some transformation similar to this one, would you use a similar strategy or do things differently?

Any advice would be greatly welcomed.

Best regards,

Hi,

Ok, now I see that I didn’t make myself clear.

What I want to do is to modify the source code (using a plugin) during compilation, and then proceed with the compilation of the modified source code. Is this possible?

The tool that you linked me to, while very interesting, is a stand alone tool, and cannot be used (at least, not that I know of) as a compilation stage.

Thanks for your answer.

Regards,

Ah, I see. I've only seen that happening so far only in two passes:
first a rewriter one, then a separate compiler invocation to actually
compile the modified code.

But perhaps others managed to do that in one pass. :slight_smile:

A PluginASTAction can define a getActionType method to say when the plugin
action is used: before, after, or instead of the main AST action. See
clang/examples/AnnotateFunctions for an example.

John