0

I have a very large old code base I am slowly updating.

Throughout large parts of the code there are for loops preforming basic math routines that I would like to call back to a function ie to change

for(int i=0;i< NUM_EL; i++)
{
   x[i] = a[i] * b[i];
}

to

vector_multiply(a,b,x,NUM_EL);

where the prototype for vector_multiply would be

void vector_multiply(const double *a, const double * b, double *x, int num_el);

Is there anything out there that scans your source code and converts standard math operations to callback routines?

more specifically would make change your code to make blass callbacks in your code automatically?

Im guessing I could use libclang to do this.

Sam P
  • 681
  • 5
  • 19

1 Answers1

0

I don't believe this can be done fully automatic without the possibility to introduce bugs.

However, you could use a perl script and manually review changes.

perl -0777 -pe 's/for\s*\(\s*int\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*0;\s*\1\s*<\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*;\s*\1\s*\+\+\s*\)\s*\{\s*([a-zA-Z_][a-zA-Z0-9_]*)\[\1\]\s*=\s*([a-zA-Z_][a-zA-Z0-9_]*)\[\1\]\s*\*\s*([a-zA-Z_][a-zA-Z0-9_]*)\[\1\]\s*;\s\}/vector_multiply(\4,\5,\3,\2);/igs' test.c
Gamification
  • 787
  • 5
  • 20