Question

I dont know if name of topic exactly introduces my problem, but thing is: in my company code, there is a function, for example:

float func_x(float a){
      float b
      return b
}

that function occuers in about 1000 places. Now the new function has been added:

void func_x2(void *a, void *b){
     do sth
}

This function should replace all occuerences of func_x in code, so change should happen from (pseudocode):

float p = 123.33;
float x = func_x(p) 
to:
float x;
float p = 123.33;
func_x2((void *)&p, (void*)&x);

My question is: Is it even possible to write some C macro (even very sophisticated) which will replace func_x into func_x2, so the code will not change at all ? Anyone tried to do it ?

regards J

Était-ce utile?

La solution

You are not just replacing the function's implementation but also it's signature. Any macro declaring the extra variable will surely lead to errors because you cannot declare variables at every site where you can call functions.

The best way is to use your editor's find and replace function, fixing this case-by-case.

Autres conseils

I have no idea of the what the functions really do, but in case x2 is just a extension of x, you could do something like that

float func_x(float a){
  float b;
  func_x2((void *)&a, (void *)&b);
  return b;
}

Exactly what you want isn't possible. You could though rename/remove the original func_x and replace it with a function which just calls func_x2.

Not possible since you're trying to modify the function definition and implementation as well.!

One idea is to use a regual expression to replace all occurences of the old function.

This regexp catches the example given in the question

^.*?\s(.*)\s*=\s*func_x\(\s*(.*)\)

and then you can use something like this to replace it with

float \2;\nfunc_x2\(\(void \*\)&\2, \(void\*\)&\1\)

Of course you would probably have to tweak this regexp a bit and there are different flavors of regular expressions but it gives you an idea of what you could do.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top