more precise switch/case matching
Hi,
So I'm trying to do something like this:
@@
expression E;
identifier OTHER =~ "NL80211_CHAN_WIDTH_(?!320)";
@@
switch (E) {
case OTHER:
+#if CFG80211_VERSION >= KERNEL_VERSION(5,18,0)
case NL80211_CHAN_WIDTH_320:
+#endif
}
which I _wanted_ to add both the + lines only in case there was something like
switch (chandef->width) {
case NL80211_CHAN_WIDTH_20_NOHT:
case NL80211_CHAN_WIDTH_20:
case NL80211_CHAN_WIDTH_40:
case NL80211_CHAN_WIDTH_80:
case NL80211_CHAN_WIDTH_80P80:
case NL80211_CHAN_WIDTH_160:
case NL80211_CHAN_WIDTH_320:
break;so what I wanted was
switch (chandef->width) {
case NL80211_CHAN_WIDTH_20_NOHT:
case NL80211_CHAN_WIDTH_20:
case NL80211_CHAN_WIDTH_40:
case NL80211_CHAN_WIDTH_80:
case NL80211_CHAN_WIDTH_80P80:
case NL80211_CHAN_WIDTH_160:
+#if CFG80211_VERSION >= KERNEL_VERSION(5,18,0)
case NL80211_CHAN_WIDTH_320:
+#endif
break;but what I got was
switch (chandef->width) {
case NL80211_CHAN_WIDTH_20_NOHT:
+#if CFG80211_VERSION >= KERNEL_VERSION(5,18,0)
case NL80211_CHAN_WIDTH_20:
+#if CFG80211_VERSION >= KERNEL_VERSION(5,18,0)
case NL80211_CHAN_WIDTH_40:
+#if CFG80211_VERSION >= KERNEL_VERSION(5,18,0)
case NL80211_CHAN_WIDTH_80:
+#if CFG80211_VERSION >= KERNEL_VERSION(5,18,0)
case NL80211_CHAN_WIDTH_80P80:
+#if CFG80211_VERSION >= KERNEL_VERSION(5,18,0)
case NL80211_CHAN_WIDTH_160:
+#if CFG80211_VERSION >= KERNEL_VERSION(5,18,0)
case NL80211_CHAN_WIDTH_320:
+#endif
break;because it's apparently treating each case kind of separately.
Is there any way to not do that?
Similarly, I don't see a way to make something like this work:
@@
expression E, value;
@@
switch (E) {
+#if CFG80211_VERSION >= KERNEL_VERSION(5,18,0)
case NL80211_CHAN_WIDTH_320:
...
(
break;
|
fallthrough;
|
return;
|
return value;
)
+#endif
}
it actually results in adding only the +#if line, never the +#endif line, which is a bit surprising?
Is there any way to turn off some of the magic in switch/case handling so this might work?
johannes
Quoted 4 lines · Johannes Berg
Hi,
So I'm trying to do something like this:
Quoted 10 lines
@@
expression E;
identifier OTHER =~ "NL80211_CHAN_WIDTH_(?!320)";
@@
switch (E) {
case OTHER:
+#ifCFG80211_VERSION>=KERNEL_VERSION(5,18,0)
caseNL80211_CHAN_WIDTH_320:
+#endif
}
This one might work if you make the rule in two steps:
@@
expression E;
position p;
identifier OTHER =~ "NL80211_CHAN_WIDTH_(?!320)";
@@
switch (E) {
case OTHER:
case NL80211_CHAN_WIDTH_320@p:
}
@@
expression E;
position r.p;
@@
switch (E) {
+#if CFG80211_VERSION >= KERNEL_VERSION(5,18,0)
case NL80211_CHAN_WIDTH_320@p:
+#endif
}
A ... before the +if might be helpful if the above isn't sufficient.
Quoted 76 lines
which I _wanted_ to add both the + lines only in case there was something like
switch (chandef->width) { case NL80211_CHAN_WIDTH_20_NOHT: case NL80211_CHAN_WIDTH_20: case NL80211_CHAN_WIDTH_40: case NL80211_CHAN_WIDTH_80: case NL80211_CHAN_WIDTH_80P80: case NL80211_CHAN_WIDTH_160: case NL80211_CHAN_WIDTH_320: break;so what I wanted was
switch (chandef->width) { case NL80211_CHAN_WIDTH_20_NOHT: case NL80211_CHAN_WIDTH_20: case NL80211_CHAN_WIDTH_40: case NL80211_CHAN_WIDTH_80: case NL80211_CHAN_WIDTH_80P80: case NL80211_CHAN_WIDTH_160:(quoted diff) +2
-0+#if CFG80211_VERSION >= KERNEL_VERSION(5,18,0) case NL80211_CHAN_WIDTH_320: +#endif break;but what I got was
switch (chandef->width) { case NL80211_CHAN_WIDTH_20_NOHT:(quoted diff) +7
-0+#if CFG80211_VERSION >= KERNEL_VERSION(5,18,0) case NL80211_CHAN_WIDTH_20: +#if CFG80211_VERSION >= KERNEL_VERSION(5,18,0) case NL80211_CHAN_WIDTH_40: +#if CFG80211_VERSION >= KERNEL_VERSION(5,18,0) case NL80211_CHAN_WIDTH_80: +#if CFG80211_VERSION >= KERNEL_VERSION(5,18,0) case NL80211_CHAN_WIDTH_80P80: +#if CFG80211_VERSION >= KERNEL_VERSION(5,18,0) case NL80211_CHAN_WIDTH_160: +#if CFG80211_VERSION >= KERNEL_VERSION(5,18,0) case NL80211_CHAN_WIDTH_320: +#endif break;because it's apparently treating each case kind of separately.
Is there any way to not do that?
Similarly, I don't see a way to make something like this work:
@@
expression E, value;
@@
switch (E) {
+#ifCFG80211_VERSION>=KERNEL_VERSION(5,18,0)
caseNL80211_CHAN_WIDTH_320:
...
(
break;
|
fallthrough;
|
return;
|
return value;
)
+#endif
}
Quoted 2 lines
it actually results in adding only the +#if line, never the +#endif line, which is a bit surprising?
Maybe it will work if you duplicate the + line in each branch of the disjunction. Actually, it should have complained about that...
julia
Hi Julia,
Thanks for your answer!
Quoted 11 lines
This one might work if you make the rule in two steps:
@@
expression E;
position p;
identifier OTHER =~ "NL80211_CHAN_WIDTH_(?!320)";
@@switch (E) {
case OTHER:
case NL80211_CHAN_WIDTH_320@p:
Interestingly, I had actually played with that too, but didn't want to make my email overly long. However, somehow I had not gotten it to work with position matches either.
Your version does work, mostly (see below).
Quoted 5 lines
@@
expression E;
position r.p;
@@
Quoted 7 lines
switch (E) {
+#ifCFG80211_VERSION>=KERNEL_VERSION(5,18,0)
case NL80211_CHAN_WIDTH_320@p:
+#endif
}A ... before the +if might be helpful if the above isn't sufficient.
That doesn't parse.
But I put a 'break' after the +#endif to catch the case I wanted.
Quoted 19 lines
Quoted 19 lines
@@
expression E, value;
@@
switch (E) {
+#ifCFG80211_VERSION>=KERNEL_VERSION(5,18,0)
caseNL80211_CHAN_WIDTH_320:
...
(
break;Quoted 1 line
fallthrough;
Quoted 1 line
return;
Quoted 1 line
return value;
)
+#endif
}
Quoted 6 lines
Quoted 2 lines
it actually results in adding only the +#if line, never the +#endif line, which is a bit surprising?
Maybe it will work if you duplicate the + line in each branch of the disjunction. Actually, it should have complained about that...
Indeed that works, but no complaints.
Now, I don't need this to deal with everything - we check this spatch in our CI pipelines and compile afterwards so that we know it did the right thing, so really we just use spatch because patches break all the time when things just change slightly (context).
What ended up working for me is the below spatch. One thing I don't understand there is the second @p match, it feels like in that case it shouldn't match if there's no OTHER ahead of it, but it still does ... I first thought I wanted "position q != r.p" but that had the opposite effect, making both spatches apply in the same place, rather than in mutually exclusive places ...
Anyway, I think it works now, thanks for the ideas!
johannes
@r@
expression E;
position p;
identifier OTHER =~ "NL80211_CHAN_WIDTH_(?!320)";
@@
switch (E) {
case OTHER:
case NL80211_CHAN_WIDTH_320@p:
}
@@
expression E;
position r.p;
@@
switch (E) {
+#if CFG80211_VERSION >= KERNEL_VERSION(5,18,0)
case NL80211_CHAN_WIDTH_320@p:
+#endif
break;
}
@@
expression E, value;
position r.p;
@@
switch (E) {
+#if CFG80211_VERSION >= KERNEL_VERSION(5,18,0)
case NL80211_CHAN_WIDTH_320@p:
(
...
break;
+#endif
|
...
fallthrough;
+#endif
|
...
return;
+#endif
|
...
return value;
+#endif
)
}
Quoted 33 lines · Johannes Berg
Hi Julia,
Thanks for your answer!
Quoted 11 lines
This one might work if you make the rule in two steps:
@@
expression E;
position p;
identifier OTHER =~ "NL80211_CHAN_WIDTH_(?!320)";
@@switch (E) {
case OTHER:
case NL80211_CHAN_WIDTH_320@p:Interestingly, I had actually played with that too, but didn't want to make my email overly long. However, somehow I had not gotten it to work with position matches either.
Your version does work, mostly (see below).
Quoted 11 lines
@@
expression E;
position r.p;
@@switch (E) {
+#ifCFG80211_VERSION>=KERNEL_VERSION(5,18,0)
case NL80211_CHAN_WIDTH_320@p:
+#endif
}
Quoted 5 lines
Quoted 1 line
A ... before the +if might be helpful if the above isn't sufficient.
That doesn't parse.
But I put a 'break' after the +#endif to catch the case I wanted.
OK, I also considered that.
Quoted 35 lines
Quoted 20 lines
Quoted 20 lines
@@
expression E, value;
@@
switch (E) {
+#ifCFG80211_VERSION>=KERNEL_VERSION(5,18,0)
caseNL80211_CHAN_WIDTH_320:
...
(
break;Quoted 1 line
fallthrough;
Quoted 1 line
return;
Quoted 1 line
return value;
)
+#endif
}Quoted 6 lines
Quoted 2 lines
it actually results in adding only the +#if line, never the +#endif line, which is a bit surprising?
Maybe it will work if you duplicate the + line in each branch of the disjunction. Actually, it should have complained about that...
Indeed that works, but no complaints.
Now, I don't need this to deal with everything - we check this spatch in our CI pipelines and compile afterwards so that we know it did the right thing, so really we just use spatch because patches break all the time when things just change slightly (context).
Quoted 6 lines
What ended up working for me is the below spatch. One thing I don't understand there is the second @p match, it feels like in that case it shouldn't match if there's no OTHER ahead of it, but it still does ... I first thought I wanted "position q != r.p" but that had the opposite effect, making both spatches apply in the same place, rather than in mutually exclusive places ...
I believe that for switch there is a direct link from the switch header to each of the cases, and another that falls through from one case to the next. It's very complex...
julia
Quoted 54 lines
Anyway, I think it works now, thanks for the ideas!
johannes
@r@
expression E;
position p;
identifier OTHER =~ "NL80211_CHAN_WIDTH_(?!320)";
@@switch (E) {
case OTHER:
case NL80211_CHAN_WIDTH_320@p:
}@@
expression E;
position r.p;
@@
switch (E) {
+#ifCFG80211_VERSION>=KERNEL_VERSION(5,18,0)
case NL80211_CHAN_WIDTH_320@p:
+#endif
break;
}@@
expression E, value;
position r.p;
@@
switch (E) {
+#ifCFG80211_VERSION>=KERNEL_VERSION(5,18,0)
case NL80211_CHAN_WIDTH_320@p:
(
...
break;
+#endif
|
...
fallthrough;
+#endif
|
...
return;
+#endif
|
...
return value;
+#endif
)
}