Julia Lawall írta:
> On Wed, 30 Dec 2009, Németh Márton wrote:
> 
>> Julia Lawall wrote:
>>> I guess you could do something more general, by first finding types that 
>>> have constant fields?
>> Well, maybe. The first thing is that the SmPL below is not the exact
>> one I wanted to create. This is because of my limited knowledge and
>> experience on SmPL. The sd_desc is really a fixed structure name and
>> not an identifier. Anyway, this is not really required for the general
>> solution.
>>
>> The second thing is that most of the time when a driver initializes
>> a constant field then constant content is suitable. There are, however,
>> some drivers which do some tricks by creating or modifying some fields
>> during runtime. In such cases changing the initializer may brake the
>> driver but such cases are really exceptions, this is not the usual way.

First I tried to create a test case without any includes. Here it is:

----8<--- cut here ----8<-----

struct A {
        int aa;
        int ab;
        int ac;
};

struct B {
        int ba;
        int bb;
};

struct C {
        const struct A *pa;
        struct B *pb;
};

static const struct A x[] = {
        {
                0,
                1,
                2
        }
};

static struct A y[] = {
        {
                4,
                5,
                6
        }
};

static struct A y2[] = {
        {
                40,
                50,
                60
        }
};

static struct A y3[] = {
        {
                70,
                80,
                90
        }
};

static struct B z[] = {
        {
                7,
                8,
        }
};

static const struct C q = {
        .pa = x,
        .pb = z,
};

static const struct C r = {
        .pa = y,
        .pb = z,
};

static struct C s;

void foo() {
        s.pa = y2;
        s.pb = z;
}

void bar() {
        y3->aa = 700;
        s.pa = y3;
        s.pb = z;
}
----8<--- cut here ----8<-----

As you can see there is a connection between q, x and z at compile time.
There is a connection between r, y and z also at compile time. The
connection between s, y2 and z is made at runtime in foo(). The same is
true for s, y3 and z with the exception that the content of y3 is also
modified during runtime. What we could change here is the following:

--- test.c      2009-12-30 10:09:14.000000000 +0100
+++ test2.c     2009-12-30 10:10:42.000000000 +0100
@@ -23,7 +23,7 @@
        }
 };

-static struct A y[] = {
+static const struct A y[] = {
        {
                4,
                5,
@@ -31,7 +31,7 @@
        }
 };

-static struct A y2[] = {
+static const struct A y2[] = {
        {
                40,
                50,

The y3 cannot be changed because it is really not constant.

My first try is to change all A to constant and then define rules later
which excludes the not necessary changes. My SmPL (which does not work)
looks like as follows:

// <smpl>
@m@
identifier x;
identifier y;
identifier T;
@@
        struct x {
                ...
                const struct T y;
                ...
        };
@c depends on m@
identifier m.T;
identifier z;
@@
        static const struct T z[] = ...;
@depends on m && !c@
identifier m.T;
identifier z;
@@
        static
+       const
        struct T z[] = ...;
// </smpl>

At this point I'm afraid I depend on your help.

        Márton Németh

>> Thirdly to find structures with constant fields the header files has
>> to be processed. I know that there is the '-local_includes' option of
>> the spatch tool but I am not sure that this would be enough for processing
>> the Linux kernel tree.
> 
> Coccinelle has a default strategy for including some .h files.  For 
> example if the file being processed is foo.c, then it will include any 
> foo.h that it finds.  If you givethe option -all_includes, then it will 
> inlude everything that is explicitly mentioned in the file, as long as it 
> can find it.  You can give a search path with -I, but unfortunately only 
> one.  If you run spatch on a directory, then -I will be implicitly set to 
> directory/include, although if you give a -I option then it will override 
> that.
> 
> julia
> 
>> Regards,
>>
>>      Márton Németh
>>
>>> On Tue, 29 Dec 2009, Németh Márton wrote:
>>>
>>>> From: Márton Németh <[email protected]>
>>>>
>>>> The ctrls field of struct sd_desc is declared as const
>>>> in gspca.h. It is worth to initialize the content also with
>>>> constant values.
>>>>
>>>> The semantic match that finds this kind of pattern is as follows:
>>>> (http://coccinelle.lip6.fr/)
>>>>
>>>> // <smpl>
>>>> @m@
>>>> identifier x;
>>>> identifier y;
>>>> identifier sd_desc;
>>>> @@
>>>>    static struct x sd_desc = {
>>>>            ...
>>>>            .ctrls = y,
>>>>            ...
>>>>    };
>>>> @c depends on m@
>>>> identifier m.y;
>>>> identifier ctrl;
>>>> @@
>>>>    static const struct ctrl y[] = ...;
>>>> @depends on m && !c@
>>>> identifier m.y;
>>>> identifier ctrl;
>>>> @@
>>>>    static
>>>> +  const
>>>>    struct ctrl y[] = ...;
>>>> // </smpl>
>>>>
>>>> Signed-off-by: Márton Németh <[email protected]>
>>>> Cc: [email protected]
_______________________________________________
Cocci mailing list
[email protected]
http://lists.diku.dk/mailman/listinfo/cocci
(Web access from inside DIKUs LAN only)

Reply via email to