Skip to contentSkip to conversation

matching a modified identifier?

DISCUSSION Johannes Berg 2 participants 4 messages Feb 6, 2017

lore
Share & tools
lore
#0 lore raw

Hi,

In backports, we have the following spatch:

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

ATTRIBUTE_GROUPS(group);

@ class_group depends on attribute_group @
identifier group_class;
identifier 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
};

[...]

But this isn't sufficient and falls over if there are multiple instances of ATTRIBUTE_GROUPS, it's essentially undefined which one gets picked.

I tried to match a modified identifier like this:

@ attribute_group @
identifier group;
fresh identifier groups = group ## "_groups";
declarer name ATTRIBUTE_GROUPS;
@@

ATTRIBUTE_GROUPS(group);

@ class_group depends on attribute_group @
identifier group_class;
identifier attribute_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,
+#else
+ .dev_attrs = group_dev_attr,
+#endif
};

[...]

but that doesn't work at all.

Any thoughts?

johannes

#1 lore raw
Quoted 8 lines

@ attribute_group @
identifier group;
fresh identifier groups = group ## "_groups";
declarer name ATTRIBUTE_GROUPS;
@@

ATTRIBUTE_GROUPS(group);

If I add a dummy here:
+ATTRIBUTE_GROUPS_NAME(groups);

Quoted 15 lines

@ class_group depends on attribute_group @ identifier group_class;
identifier attribute_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,

(quoted diff) +3 -0
+#else
+???????.dev_attrs = group_dev_attr,
+#endif

};

Quoted 1 line

[...]

Then the whole thing works - I guess I can live with that.

johannes

#2 lore raw
In reply to Johannes Berg
Quoted 57 lines · Johannes Berg

Hi,

In backports, we have the following spatch:

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

ATTRIBUTE_GROUPS(group);

@ class_group depends on attribute_group @
identifier group_class;
identifier 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,

(quoted diff) +3 -0
+#else
+???????.dev_attrs = group_dev_attr,
+#endif

};

[...]

But this isn't sufficient and falls over if there are multiple instances of ATTRIBUTE_GROUPS, it's essentially undefined which one gets picked.

I tried to match a modified identifier like this:

@ attribute_group @
identifier group;
fresh identifier groups = group ## "_groups";
declarer name ATTRIBUTE_GROUPS;
@@

ATTRIBUTE_GROUPS(group);

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

struct class group_class = {

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

};

Quoted 4 lines

[...]

but that doesn't work at all.

Use python between the two rules to construct the identifier you want. You can see examples in coccinelle/demos/pythontococci.cocci

julia

#3 lore raw
Quoted 3 lines

Use python between the two rules to construct the identifier you want.

Indeed, that works and is simpler than I thought (had briefly considered, but then found the workaround)

Thanks :)

johannes