Is there a way to run ASTConsumer plugin inspite of compile errors?

Hi All,

I developed a clang plugin which uses AST matchers to find interesting record declarations and automatically inserts a function (lets say newfn()) along with some boiler plate code.

//original code

class A{


//modified code after plugin is run
class A{
void newfn() {…}

This works fine as long as there are no compile errors. But there are cases when the absence of the
function is causing the compilation to fail. E.g., the following code which expects class A to contain the function.

class B{
T t;
B() {

Due to the compilation errors, my plugin is not executed.

So the compilation wont proceed unless the function is already present and the function can’t be added unless compilation succeeds.

Is there a way to solve this?


In general clang produces ast only for valid code. I don’t think you’ll be able to get this working if your starting point is code that doesn’t compile. In the example you gave you might be able to get away with it so long as you don’t compile the instantiation of B as that’s what produces the error. But this essentially means having code that compiles…

The only other thing that comes to my mind is having multiple passes in the hope that each insertion you perform removes errors and allows you to continue. I have no idea how feasible this would be.

Thanks Nikola. I don’t mind doing multiple passes but yes, I don’t know if even that is possible.

Is there any other way to solve this problem using plugins (maybe insert code as soon as a class definition is found during parsing stage itself)?