Skip to contentSkip to conversation

Searching for constants between shift operators with SmPL

Cocci SF Markus Elfring 2 participants 7 messages Jun 17, 2018 5 messages not in the archive

lore
Share & tools
lore
#0 lore raw

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

#1 lore raw
Quoted 26 lines · 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;
}

Quoted 2 lines

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.

Quoted 2 lines

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

Quoted 10 lines

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

Regards,
Markus
_______________________________________________
Cocci mailing list
Cocci at systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

#3 lore raw
In reply to a message not in the archive
Quoted 3 lines · SF Markus Elfring
Quoted 1 line

You can't just put ... at random places.

I hope that this software aspect can be adjusted somehow.

No.

Quoted 4 lines
Quoted 2 lines

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.

Quoted 30 lines
Quoted 5 lines
Quoted 3 lines

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;
}

Quoted 1 line

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 ...;

Quoted 2 lines

This SmPL script variant does not present the error message ?minus:
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

#5 lore raw
In reply to a message not in the archive
Quoted 13 lines

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.

Quoted 8 lines

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

#7 lore raw
In reply to a message not in the archive
Quoted 9 lines · SF Markus Elfring
Quoted 9 lines
Quoted 8 lines

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;
}

Quoted 3 lines
Quoted 1 line

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.

Quoted 4 lines
Quoted 2 lines

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 ;

Quoted 3 lines

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

#9 lore raw
In reply to a message not in the archive
Quoted 11 lines · SF Markus Elfring
Quoted 3 lines
Quoted 1 line

Where do you expect that such an empty statement would occur here?

Because your rule contained ...;

Yes.

Quoted 1 line

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.

Quoted 3 lines
Quoted 1 line

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

#11 lore raw
In reply to a message not in the archive
Quoted 6 lines · SF Markus Elfring
Quoted 4 lines
Quoted 2 lines

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.

Quoted 4 lines
Quoted 1 line

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