Discussion:
[Cocci] Searching for constants between shift operators with SmPL
SF Markus Elfring
2018-06-17 17:09:11 UTC
Permalink
Hello,

The following script for the semantic patch language can mark a bit of source
code also in a corresponding example.


@display@
constant C;
expression A, B;
identifier X;
type T;
@@
T X = A
<<
*C
<<
B;


int main(void)
{
unsigned int a = 2, b = 4;
unsigned long c = a << 2 << b;
}


1. I get the error message “minus: parse error” after the addition of
a SmPL ellipsis behind the metavariable “B”.

2. I have observed that no source code is found if I omit the metavariables “T”
and “X” (omission of the assignment target) in a SmPL script variant.


How do you think about to clarify these software situations any more?

Regards,
Markus
Julia Lawall
2018-06-17 17:51:24 UTC
Permalink
Post by SF Markus Elfring
Hello,
The following script for the semantic patch language can mark a bit of source
code also in a corresponding example.
@display@
constant C;
expression A, B;
identifier X;
type T;
@@
T X = A
<<
*C
<<
B;
int main(void)
{
unsigned int a = 2, b = 4;
unsigned long c = a << 2 << b;
}
1. I get the error message “minus: parse error” after the addition of
a SmPL ellipsis behind the metavariable “B”.
You can't just put ... at random places. If you are in an expression, ...
must replace a single expression.
Post by SF Markus Elfring
2. I have observed that no source code is found if I omit the metavariables “T”
and “X” (omission of the assignment target) in a SmPL script variant.
Since I don't know anythign about the code you are applying it to, and I
don't know the exact definition of the semantic patch, I can't answer the
question.

julia
Post by SF Markus Elfring
How do you think about to clarify these software situations any more?
Regards,
Markus
_______________________________________________
Cocci mailing list
https://systeme.lip6.fr/mailman/listinfo/cocci
Julia Lawall
2018-06-17 18:40:33 UTC
Permalink
Post by Julia Lawall
You can't just put ... at random places.
I hope that this software aspect can be adjusted somehow.
No.
Post by Julia Lawall
If you are in an expression, ... must replace a single expression.
Can it be optional at the end?
I don't know what "it" refers to, but the answer is surely no.
Post by Julia Lawall
Post by SF Markus Elfring
2. I have observed that no source code is found if I omit the metavariables “T”
and “X” (omission of the assignment target) in a SmPL script variant.
Since I don't know anythign about the code you are applying it to,
Please look once more at the constructed example.
int main(void)
{
unsigned int a = 2, b = 4;
unsigned long c = a << 2 << b;
}
Post by Julia Lawall
and I don't know the exact definition of the semantic patch,
Can the following SmPL search approach trigger further software development considerations?
@display@
constant C;
expression A, B;
@@
A
<<
*C
<<
B ...;
parse error”.
That's true. You have an expression followed by ... followed by an empty
statement. It will match things like:

foo = x << 12 << b;
test();
test();
;

It doesn't match your sample program.

julia
Julia Lawall
2018-06-17 19:10:40 UTC
Permalink
The following file combination produces another usable search result.
@display@
constant C;
expression A, B;
identifier X;
type T;
@@
T X = A
<<
*C
<<
B + ...;
Yes, because now ... is replacing an expression.
Should anything be found in the following source code variant then?
int main(void)
{
unsigned int a = 2, b = 4;
unsigned long c = a << 2 << b;
return c;
}
No, because there is no empty statement to match the final ;. The ...
matches a complete statement (in general, 0 or more complete statements).
It doesn't match return c

julia
Julia Lawall
2018-06-17 19:24:38 UTC
Permalink
Post by Julia Lawall
Should anything be found in the following source code variant then?
int main(void)
{
unsigned int a = 2, b = 4;
unsigned long c = a << 2 << b;
return c;
}
No, because there is no empty statement to match the final ;.
Where do you expect that such an empty statement would occur here?
Because your rule contained ...; which is a ... followed by an empty
statement.
Post by Julia Lawall
The ... matches a complete statement (in general, 0 or more complete statements).
It doesn't match return c
Is this line also a complete statement?
No. return c is not a complete statement. return c; is, but then there
is nothing to match the ;
I am struggling still with the presented interpretations if the SmPL ellipsis
should match an expression or/and statements.
If it is in the middle of an expression, it matches a single expression.
If it is elsewhere it matches a sequence of 0 or more statements. It can
also match eg a sequence of 0 or more field declarations in a structure
declaration or 0 or more parameters in a function header.

julia
Julia Lawall
2018-06-17 19:53:32 UTC
Permalink
Post by Julia Lawall
Where do you expect that such an empty statement would occur here?
Because your rule contained ...;
Yes.
Post by Julia Lawall
which is a ... followed by an empty statement.
I hoped that a semicolon could be interpreted as the closing delimiter
for an assignment statement.
No. That would make the parse amiguous. While a ... can match a single
expression, to avoid ambiguity, such a ... cannot appear at the beginning
of the expression. A way around this is to put (...); That could match
an assignment statement, due to the paren isomorphism. But that won#t
match return c; because return c is not an expression either. It's simply
not a complete term.
Post by Julia Lawall
return c; is,
My source code example contained this line.
Yes, but you have to get the ... to match something and the ; to match
something. If the ; matches the ; at the end of the return, then what
does the ... match?

julia
Julia Lawall
2018-06-17 20:25:59 UTC
Permalink
Post by Julia Lawall
I hoped that a semicolon could be interpreted as the closing delimiter
for an assignment statement.
No. That would make the parse amiguous.
I am curious on the clarification of related ambiguity concerns.
...; could match a sequence of statements followed by an empty statement
or a single statement consisting of an expression followed by a ;. The
parser favors the former. The former may seem less useful than the
latter, but the constraints of a yacc like parser mean that the former has
to be the one that is chosen.
Post by Julia Lawall
While a ... can match a single expression,
I hoped that the SmPL ellipsis could handle also an optional continuation
for a computation so that I do not need to express a subsequent operator.
It can't. This is not going to change.

julia

Loading...