Skip to contentSkip to conversation

matching on previous rule

Cocci Arend Van Spriel 2 participants 5 messages Feb 1, 2017

lore
Share & tools
lore
#0 lore raw

In the cocci script below I want to make sure that attribute_group.group is used for struct class::dev_groups field initializer. So in class_group rule I specified groups_dev identifier. But running the scripts results in failure:

Fatal error: exception Failure("45: unexpected use of a fresh identifier groups_dev")

So I assume the identifier can only be use in the + part. I have been digging in the SmPL grammar pages, but those are tricky to read because it includes C-syntax definitions as well. A hint would be welcome.

Regards,
Arend

-------------------------------
@ attribute_group @
identifier group;
declarer name ATTRIBUTE_GROUPS;
@@

ATTRIBUTE_GROUPS(group);

@ class_group depends on attribute_group @
identifier group_class;
expression groups;
fresh identifier groups_dev = attribute_group.group ## "_groups"; fresh identifier group_dev_attr = attribute_group.group ## "_dev_attrs"; @@

struct class group_class = {
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
.dev_groups = groups_dev,
+#else
+ .dev_attrs = group_dev_attr,
+#endif

#1 lore raw
Quoted 31 lines · Arend Van Spriel

In the cocci script below I want to make sure that attribute_group.group is used for struct class::dev_groups field initializer. So in class_group rule I specified groups_dev identifier. But running the scripts results in failure:

Fatal error: exception Failure("45: unexpected use of a fresh identifier groups_dev")

So I assume the identifier can only be use in the + part. I have been digging in the SmPL grammar pages, but those are tricky to read because it includes C-syntax definitions as well. A hint would be welcome.

Regards,
Arend

-------------------------------
@ attribute_group @
identifier group;
declarer name ATTRIBUTE_GROUPS;
@@

ATTRIBUTE_GROUPS(group);

@ class_group depends on attribute_group @
identifier group_class;
expression groups;
fresh identifier groups_dev = attribute_group.group ## "_groups"; fresh identifier group_dev_attr = attribute_group.group ## "_dev_attrs"; @@

struct class group_class = {

Review

Quoted 5 lines
(quoted diff) +4 -0
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
        .dev_groups = groups_dev,
+#else
+       .dev_attrs = group_dev_attr,
+#endif

Fresh identifiers can indeed only be used in constructed code (+). Could you send an example of the before and after code you expect, so that I can be sure to understand what you are trying to do?

thanks,
julia

author Reviewed-by Guenter Roeck Tested-by Guenter Roeck
#2 lore raw
Quoted 34 lines · Julia Lawall
Quoted 30 lines · Arend Van Spriel

In the cocci script below I want to make sure that attribute_group.group is used for struct class::dev_groups field initializer. So in class_group rule I specified groups_dev identifier. But running the scripts results in failure:

Fatal error: exception Failure("45: unexpected use of a fresh identifier groups_dev")

So I assume the identifier can only be use in the + part. I have been digging in the SmPL grammar pages, but those are tricky to read because it includes C-syntax definitions as well. A hint would be welcome.

Regards,
Arend

-------------------------------
@ attribute_group @
identifier group;
declarer name ATTRIBUTE_GROUPS;
@@

ATTRIBUTE_GROUPS(group);

@ class_group depends on attribute_group @
identifier group_class;
expression groups;
fresh identifier groups_dev = attribute_group.group ## "_groups"; fresh identifier group_dev_attr = attribute_group.group ## "_dev_attrs"; @@

Quoted 10 lines

Review

Quoted 6 lines

struct class group_class = {

(quoted diff) +4 -0
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
        .dev_groups = groups_dev,
+#else
+       .dev_attrs = group_dev_attr,
+#endif

Fresh identifiers can indeed only be used in constructed code (+). Could you send an example of the before and after code you expect, so that I can be sure to understand what you are trying to do?

Attached is the existing script we have in backports. It finds a ATTRIBUTE_GROUPS macro and will make change in the struct class accordingly. This works fine as long as the ATTRIBUTE_GROUPS in the file is indeed used for setting struct class::dev_groups.

Consider example below:

ATTRIBUTE_GROUPS(foo);

struct class bar = {
.dev_groups = foo_groups,
};

The above is properly transformed by the script. However, recent change in struct class introduced a new field .class_groups. So there is another example that does:

ATTRIBUTE_GROUPS(foo_class);

struct class another_bar = {
.class_groups = foo_class_groups,
};

The attached script will also transform this example. I only want it to do that when .dev_groups is initialized with the given attribute group. Below the ATTRIBUTE_GROUPS macro definition for reference.

Regards,
Arend

#define __ATTRIBUTE_GROUPS(_name) \

static const struct attribute_group *_name##_groups[] = {	\
	&_name##_group,						\
	NULL,							\
}

#define ATTRIBUTE_GROUPS(_name) \

static const struct attribute_group _name##_group = {		\
	.attrs = _name##_attrs,					\
};								\

__ATTRIBUTE_GROUPS(_name)

Quoted 3 lines

thanks,
julia

-------------- next part --------------
/*
The new attribute sysfs group was added onto struct class via commit d05a6f96c

mcgrof at ergon ~/linux (git::master)$ git describe --contains d05a6f96c v3.11-rc2~18^2~3

This backpoort makes use of the helpers to backport this more efficiently. Refer to the INFO file for documentation there on that.

commit d05a6f96c76062b5f25858ac02cf677602076f7e

Author: Greg Kroah-Hartman <[email protected]>
Date:   Sun Jul 14 16:05:58 2013 -0700

driver core: add default groups to struct class

We should be using groups, not attribute lists, for classes to allow subdirectories, and soon, binary files. Groups are just more flexible overall, so add them.

The dev_attrs list will go away after all in-kernel users are converted to use dev_groups.

*/

@ attribute_group @
identifier group;
declarer name ATTRIBUTE_GROUPS;
@@

ATTRIBUTE_GROUPS(group);

@ class_group depends on attribute_group @
identifier group_class;
expression groups;
fresh identifier group_dev_attr = attribute_group.group ## "_dev_attrs"; @@

struct class group_class = {
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
.dev_groups = groups,
+#else
+ .dev_attrs = group_dev_attr,
+#endif
};

@ attribute_group_mod depends on attribute_group && class_group @ declarer name ATTRIBUTE_GROUPS_BACKPORT;
identifier group;
@@

+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
ATTRIBUTE_GROUPS(group);
+#else
+#define BP_ATTR_GRP_STRUCT device_attribute
+ATTRIBUTE_GROUPS_BACKPORT(group);
+#endif

@ class_registering depends on class_group && attribute_group_mod @ identifier class_register, ret;
identifier class_group.group_class;
fresh identifier group_class_init = "init_" ## attribute_group_mod.group ## "_attrs"; @@

(

+	group_class_init();
	return class_register(&group_class);
|
+	group_class_init();
	ret = class_register(&group_class);

)

#3 lore raw
Quoted 19 lines

Attached is the existing script we have in backports. It finds a ATTRIBUTE_GROUPS macro and will make change in the struct class accordingly. This works fine as long as the ATTRIBUTE_GROUPS in the file is indeed used for setting struct class::dev_groups.

Consider example below:

ATTRIBUTE_GROUPS(foo);

struct class bar = {
.dev_groups = foo_groups,
};

The above is properly transformed by the script. However, recent change in struct class introduced a new field .class_groups. So there is another example that does:

ATTRIBUTE_GROUPS(foo_class);

Quoted 7 lines

struct class another_bar = {
.class_groups = foo_class_groups,
};

The attached script will also transform this example. I only want it to do that when .dev_groups is initialized with the given attribute group. Below the ATTRIBUTE_GROUPS macro definition for reference.

OK, now I see a bit better what is going on. Actually, fresh identifier is intended for when you bind a metavariable in the same rule, and then want to create something new for it in the generated code. In your case, you can do the following instead:

@ attribute_group @
identifier group;
declarer name ATTRIBUTE_GROUPS;
@@

ATTRIBUTE_GROUPS(group);

@script:python n@
g << attribute_group.group;
groups_var;
dev_attrs_var;
@@

coccinelle.groups_var = concat(g,"_groups")
coccinelle.dev_attrs_var = concat(g,"_dev_attrs")

@ class_group depends on attribute_group @ identifier group_class, n.groups_var, n.dev_attrs_var; @@

struct class group_class = {
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
.dev_groups = groups_var,
+#else
+ .dev_attrs = dev_attrs_var,
+#endif
};

Replace concat by whatever does string concatenation in python.

Feel free to change the metavariable names.

julia

#4 lore raw
Quoted 60 lines · Julia Lawall
Quoted 19 lines

Attached is the existing script we have in backports. It finds a ATTRIBUTE_GROUPS macro and will make change in the struct class accordingly. This works fine as long as the ATTRIBUTE_GROUPS in the file is indeed used for setting struct class::dev_groups.

Consider example below:

ATTRIBUTE_GROUPS(foo);

struct class bar = {
.dev_groups = foo_groups,
};

The above is properly transformed by the script. However, recent change in struct class introduced a new field .class_groups. So there is another example that does:

ATTRIBUTE_GROUPS(foo_class);

Quoted 7 lines

struct class another_bar = {
.class_groups = foo_class_groups,
};

The attached script will also transform this example. I only want it to do that when .dev_groups is initialized with the given attribute group. Below the ATTRIBUTE_GROUPS macro definition for reference.

OK, now I see a bit better what is going on. Actually, fresh identifier is intended for when you bind a metavariable in the same rule, and then want to create something new for it in the generated code. In your case, you can do the following instead:

@ attribute_group @
identifier group;
declarer name ATTRIBUTE_GROUPS;
@@

ATTRIBUTE_GROUPS(group);

@script:python n@
g << attribute_group.group;
groups_var;
dev_attrs_var;
@@

coccinelle.groups_var = concat(g,"_groups")
coccinelle.dev_attrs_var = concat(g,"_dev_attrs")

@ class_group depends on attribute_group @ identifier group_class, n.groups_var, n.dev_attrs_var; @@

struct class group_class = {

(quoted diff) +4 -0
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
        .dev_groups = groups_var,
+#else
+       .dev_attrs = dev_attrs_var,
+#endif

};

Quoted 3 lines

Replace concat by whatever does string concatenation in python.

Feel free to change the metavariable names.

Thanks. One happy customer over here ;-)

Regards,
Arend