Search and replace in C source code without touching comments

I have a lot of c code in which I would like to replace the old syntax style with a new style. For instance. the following si prefixes:

int siName;

should become "i":

int iName;

But reg-expression or another find / replace tool should not touch source code comments. Any solution?

+5
source share
4 answers

You can try coccinelle . It has a little learning curve, but it analyzes the C code and performs the transformations provided to it in the script. For example, to rename a function from foo to bar, the script would look like this (from here ):

@@

@@

-foo()
+bar()

, , , .

+2

vi

       :%s/\<Oldword\>/Newword/gc

, , , .

+1

Coccinelle, , :

identifier foo =~ "xxx";

foo , xxx. PCRE.

python :

@a@
identifier x;
@@

foo(x)

@script:python b@
x << a.x;
xx;
@@

coccinelle.xx = "%s%s" % (x,x)

@@
identifier a.x;
identifier b.xx;
@@

 foo(
-  x
+  xx
   )

:

int main () {
  foo(one);
}

int main () {
  foo(oneone);
}
0

"int si" → "int i"? - (, ), .

-1
source

All Articles