Hi Julia,

On Sat, 2015-02-14 at 07:56PM +0100, Julia Lawall wrote:
> On Sat, 14 Feb 2015, Sören Brinkmann wrote:
> 
> > Hi,
> >
> > I'm just getting my feet wet with coccinelle, so I'm probably missing
> > something really obvious in my problem.
> >
> > I have a codebase that uses plenty of
> >   typedef struct foo {
> >     ...
> >   } bar;
> >
> > I want to remove all these typedefs and simply use struct foo instead. I
> > came up with this patch:
> >
> >   /// Convert typdefed structs to structs
> >
> >   @ rule1 @
> >   identifier i;
> >   type t;
> >   @@
> >   -typedef struct i {
> >   +struct i {
> >     ...
> >   - }t;
> >   + };
> >
> >   @@
> >   identifier rule1.i;
> >   type rule1.t;
> >   @@
> >   -t
> >   +struct i
> >   // end of patch
> >
> > This seems to work for most occurrences, but there are a couple of
> > variables defined like this:
> >
> >   static const t *const baz[MAX] = {
> >     ...
> >   };
> >
> > And also function declarations like:
> >   u32 fn(const t* const baz);
> >
> > In such cases, t isn't replaces as I would expect, but those lines
> > aren't changed at all.
> >
> > Other variables and non-const function parameters seem to be replaced as
> > expected.
> > Does anybody have an idea what I'm missing?
> 
> In principle, it should be OK.  Could you send a concrete example of C
> conde on which it does not work?  It is possible that there is some
> problem in parsing baz, and it is just skipping over it.  For this, you
> can run spatch --parse-c on your file.  If there is a BAD or bad in front
> of the code that you expect to have changed, then there may be a problem.

When I run with --parse-c, all I get is this:
  init_defs_builtins: /usr/share/coccinelle/standard.h
  
  PARSING: testcase.c
  -----------------------------------------------------------------------
  maybe 10 most problematic tokens
  -----------------------------------------------------------------------
  -----------------------------------------------------------------------
  NB total files = 1; perfect = 1; pbs = 0; timeout = 0; =========> 100%
  nb good = 10,  nb passed = 0 =========> 0.000000% passed
  nb good = 10,  nb bad = 0 =========> 100.000000% good

It doesn't show any code (not sure whether that is correct or not), but no 
'BAD' either.
Running that file through gcc -c -Wall works fine as well. So, it should be 
legal c (just
a warning regarding an unused variable).

I attached the testcase.c and my cocci patch. This is the diff I get:
  init_defs_builtins: /usr/share/coccinelle/standard.h
  HANDLING: testcase.c
  diff =
  --- testcase.c
  +++ /tmp/cocci-output-17008-c37c42-testcase.c
  @@ -1,11 +1,11 @@
  -typedef struct a_struct {
  +struct a_struct {
          int a_member;
  -} a_struct;
  +};
  
   static const a_struct *const a_struct_array[3] = {
   };
  
   unsigned int function(const a_struct* const a_struct_arg);
  
  -static a_struct a_struct_instance;
  +static struct a_struct a_struct_instance;
   static const a_struct a_const_struct_instance;

        Thanks,
        Soren
typedef struct a_struct {
	int a_member;
} a_struct;

static const a_struct *const a_struct_array[3] = {
};

unsigned int function(const a_struct* const a_struct_arg);

static a_struct a_struct_instance;
static const a_struct a_const_struct_instance;
/// Convert typdefed structs to structs

@ rule1 @
identifier i;
type t;
@@
-typedef struct i {
+struct i {
        ...
- }t;
+ };

@ rule2 @
identifier rule1.i;
type rule1.t;
@@
-t
+struct i

@ rule3 @
identifier i;
type t;
@@

-typedef struct i t;
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

Reply via email to