Hi,

I'm trying to remove 'const' qualifiers from function arguments that are
passed by value.

e.g. I want to convert:

void foobar(const int bar, const int *baz);
to
void foobar(int bar, const int *baz);

I came up with this semantic patch:
/* Remove const from arguments passed by value */
@ rule1 @
identifier fi, I;
type T;
@@
 fi ( ...,
-       const T
+       T
        I
        ,...
 )
 { ... }

That seems to work fine on function declarations, but does not cover
function prototypes. Extending the patch to function prototypes I ran
into some trouble. As complete separate patch, I evolved the above to
(just replacing the function body with a ';'):
/* Remove const from arguments passed by value */
@ rule1 @
identifier fi, I;
type T;
@@
 fi ( ...,
-       const T
+       T
        I
        ,...
 ) ;

But that is not parsed by spatch, causing this error:
  init_defs_builtins: /usr/local/lib/coccinelle/standard.h
  minus: parse error:
    File "const_proto.cocci", line 9, column 1, charpos = 115
    around = 'I',
    whole content = '     I'

Also, using the first version of a patch on a c-file with matching function
declarations that also includes function prototypes, results in both,
declarations and prototypes, while using it on a header with prototypes
only doesn't result in any replacements.

I attach the patches and some test files for reference.
const.cocci apparently correctly removes all const keywords from the
test.c file, but does not change the .h at all (which is correct, just
weird that it changes the prototype in the .c).

The const_proto.cocci file causes the above-mentioned error.

Does anybody know what I'm missing here?

        Thanks,
        Sören
/* Remove const from arguments passed by value */
@ rule1 @
identifier fi, I;
type T;
@@
 fi ( ...,
-       const T
+       T
        I
        ,...
 )
 { ... }

/* Remove const from arguments passed by value */
@ rule1 @
identifier fi, I;
type T;
@@
 fi ( ...,
-       const T
+       T
        I
        ,...
 ) ;

static int function1(int foo)
{
	return 0;
}

static int function2(const int foo)
{
	return 0;
}

static int function3(int foo, const int bar);
static int function3(int foo, const int bar)
{
	return 0;
}

static int function4(const int foo, int bar)
{
	return 0;
}

static int function5(int *foo)
{
	return 0;
}

static int function6(const int *foo)
{
	return 0;
}

static int function7(int *foo, const int *bar)
{
	return 0;
}

static int function8(const int *foo, int *bar)
{
	return 0;
}

static int function(int foo,
		    const int bar,
		    const int bar2,
		    int *baz,
		    const int *boo,
		    int dummy)
{
	return 0;
}

static int function3(int foo, const int bar);
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

Reply via email to