Skip to contentSkip to conversation

Negated Positions and Modifications

Cocci Michael Stefaniuc 3 participants 11 messages Nov 22, 2016 – Dec 9, 2016

lore
Share & tools
lore
#0 lore raw

Hello,

I have to have 3 rules to remove a cast. The rules are basically identical, they just differ by the type of an expression. That type is inherited from different rules that aren't matching at the same time.

Now I wanted to use positions to eliminate false positives rather than duplicating that code, but that doesn't work. Using the position in the second rule will generate an error:
meta: semantic error: position cannot be inherited over modifications: p

Though that doesn't makes sense, as the position is never modified, only all the other positions not matching are modified.
Here are the relevant chunks:

@ disable drop_cast @
type find.To;
type T;
To *obj;
position p != cast_good.p;
@@
- (T)
(obj at p)

@ disable drop_cast @
type find2.To;
type T;
To *obj;
position p != cast_good.p;
@@
- (T)
(obj at p)

thanks
bye
michael

#1 lore raw
Quoted 25 lines · Michael Stefaniuc

Hello,

I have to have 3 rules to remove a cast. The rules are basically identical, they just differ by the type of an expression. That type is inherited from different rules that aren't matching at the same time.

Now I wanted to use positions to eliminate false positives rather than duplicating that code, but that doesn't work. Using the position in the second rule will generate an error:
meta: semantic error: position cannot be inherited over modifications: p

Though that doesn't makes sense, as the position is never modified, only all the other positions not matching are modified.
Here are the relevant chunks:

@ disable drop_cast @
type find.To;
type T;
To *obj;
position p != cast_good.p;
@@
- (T)
(obj at p)

Quoted 8 lines

@ disable drop_cast @
type find2.To;
type T;
To *obj;
position p != cast_good.p;
@@
- (T)
(obj at p)

What gets modified is the file, or at least the function, so you can't rely on the positions being meaningful once the code has been changed.

I think you could use a disjunction to make just one rule, to avoid the problem.

You can also put a set of possible types for obj ({type1, type2, ...}) and put the whole thing into one line.

julia

#2 lore raw
Quoted 38 lines · Julia Lawall
Quoted 25 lines · Michael Stefaniuc

Hello,

I have to have 3 rules to remove a cast. The rules are basically identical, they just differ by the type of an expression. That type is inherited from different rules that aren't matching at the same time.

Now I wanted to use positions to eliminate false positives rather than duplicating that code, but that doesn't work. Using the position in the second rule will generate an error:
meta: semantic error: position cannot be inherited over modifications: p

Though that doesn't makes sense, as the position is never modified, only all the other positions not matching are modified.
Here are the relevant chunks:

@ disable drop_cast @
type find.To;
type T;
To *obj;
position p != cast_good.p;
@@
- (T)
(obj at p)

Quoted 8 lines

@ disable drop_cast @
type find2.To;
type T;
To *obj;
position p != cast_good.p;
@@
- (T)
(obj at p)

Quoted 2 lines

What gets modified is the file, or at least the function, so you can't rely on the positions being meaningful once the code has been changed.

Ok, I have seen the positions just as an anchor and as that respective code is never modified the position shouldn't change.

Quoted 2 lines

I think you could use a disjunction to make just one rule, to avoid the problem.

Yeah, but I have to add the disjunction to all 3 functions. That's what I did initially but that doesn't feels right. But if that's the only workable solution I'll go back to that.

Quoted 2 lines

You can also put a set of possible types for obj ({type1, type2, ...}) and put the whole thing into one line.

That doesn't work, I have tried it before:

@@
{r1.T, r2.T} obj;
@@

That only matches when both r1.T and r2.T are defined. That kinda feels odd but probably is easier to implement as all inherited variables need to be bound. I have quiet a few duplicated rules across multiple scripts due to that.

thanks
bye
michael

#3 lore raw
Quoted 54 lines · Michael Stefaniuc
Quoted 38 lines · Julia Lawall
Quoted 25 lines · Michael Stefaniuc

Hello,

I have to have 3 rules to remove a cast. The rules are basically identical, they just differ by the type of an expression. That type is inherited from different rules that aren't matching at the same time.

Now I wanted to use positions to eliminate false positives rather than duplicating that code, but that doesn't work. Using the position in the second rule will generate an error:
meta: semantic error: position cannot be inherited over modifications: p

Though that doesn't makes sense, as the position is never modified, only all the other positions not matching are modified.
Here are the relevant chunks:

@ disable drop_cast @
type find.To;
type T;
To *obj;
position p != cast_good.p;
@@
- (T)
(obj at p)

Quoted 8 lines

@ disable drop_cast @
type find2.To;
type T;
To *obj;
position p != cast_good.p;
@@
- (T)
(obj at p)

Quoted 2 lines

What gets modified is the file, or at least the function, so you can't rely on the positions being meaningful once the code has been changed.

Ok, I have seen the positions just as an anchor and as that respective code is never modified the position shouldn't change.

Quoted 2 lines

I think you could use a disjunction to make just one rule, to avoid the problem.

Yeah, but I have to add the disjunction to all 3 functions. That's what I did initially but that doesn't feels right. But if that's the only workable solution I'll go back to that.

Quoted 2 lines

You can also put a set of possible types for obj ({type1, type2, ...}) and put the whole thing into one line.

That doesn't work, I have tried it before:

Quoted 8 lines

@@
{r1.T, r2.T} obj;
@@

That only matches when both r1.T and r2.T are defined. That kinda feels odd but probably is easier to implement as all inherited variables need to be bound. I have quiet a few duplicated rules across multiple scripts due to that.

OK. It should not require that all of the variables are bound, but it is possible that it does...

julia

#4 lore raw
In reply to Michael Stefaniuc
Quoted 8 lines

@@
{r1.T, r2.T} obj;
@@

That only matches when both r1.T and r2.T are defined. That kinda feels odd but probably is easier to implement as all inherited variables need to be bound. I have quiet a few duplicated rules across multiple scripts due to that.

This problem is fixed by the following patch. Will appear in github shortly, but not immediately.

julia

---

parsing_cocci/free_vars.ml +6 -1
index 6eea167..9dfcf1f 100644
@@ -86,7 +86,12 @@ let collect_refs include_constraints =
     bind (k e)
       (match Ast.unwrap e with
 	Ast.MetaExpr(name,constraints,_,Some type_list,_,_) ->
-	  let types = List.fold_left type_collect option_default type_list in
+	  let types =
+	    (* problem: if there are multiple types, then none in particular
+	       is needed *)
+	    if include_constraints || List.length type_list = 1
+	    then List.fold_left type_collect option_default type_list
+	    else [] in
 	  let extra =
 	    if include_constraints
 	    then
#5 lore raw
Quoted 11 lines · Julia Lawall
Quoted 8 lines

@@
{r1.T, r2.T} obj;
@@

That only matches when both r1.T and r2.T are defined. That kinda feels odd but probably is easier to implement as all inherited variables need to be bound. I have quiet a few duplicated rules across multiple scripts due to that.

This problem is fixed by the following patch. Will appear in github shortly, but not immediately.

The change is now available in the github version. It is also now possible, thanks to Thierry, to put negated constraints on type variables, as illustrated by the following semantic patch (tests/type_constraints.cocci):

@@
type T != { int, char };
identifier x;
@@
- T x;
+ T *x;

julia

#6 lore raw
Quoted 13 lines · Julia Lawall
Quoted 9 lines · Julia Lawall
Quoted 8 lines

@@
{r1.T, r2.T} obj;
@@

That only matches when both r1.T and r2.T are defined. That kinda feels odd but probably is easier to implement as all inherited variables need to be bound. I have quiet a few duplicated rules across multiple scripts due to that.

Quoted 4 lines
Quoted 2 lines

This problem is fixed by the following patch. Will appear in github shortly, but not immediately.

The change is now available in the github version. It is also now

Thanks Julia, I have tested the change and it seems to work.

Quoted 1 line

possible, thanks to Thierry, to put negated constraints on type variables,

Is there a way to have the positive constraint variant too? That doesn't seem that useful for static types like int and char but can avoid rule duplication when the type is inherited from 2 or more rules that don't match at the same time. E.g.:
@@
type T = {r1.T, r2.T};
@@
T *foo =
- xyz
+ abc
;

Now in my specific case I'm getting the error:
meta: semantic error: incompatible inheritance declaration T

Review

Quoted 9 lines

as illustrated by the following semantic patch
(tests/type_constraints.cocci):

@@
type T != { int, char };
identifier x;
@@

(quoted diff) +1 -1
-   T x;
+   T *x;

thanks
bye
michael

#7 lore raw
Quoted 33 lines · Michael Stefaniuc
Quoted 13 lines · Julia Lawall
Quoted 9 lines · Julia Lawall
Quoted 8 lines

@@
{r1.T, r2.T} obj;
@@

That only matches when both r1.T and r2.T are defined. That kinda feels odd but probably is easier to implement as all inherited variables need to be bound. I have quiet a few duplicated rules across multiple scripts due to that.

Quoted 4 lines
Quoted 2 lines

This problem is fixed by the following patch. Will appear in github shortly, but not immediately.

The change is now available in the github version. It is also now

Thanks Julia, I have tested the change and it seems to work.

Quoted 1 line

possible, thanks to Thierry, to put negated constraints on type variables,

Is there a way to have the positive constraint variant too? That doesn't seem that useful for static types like int and char but can avoid rule duplication when the type is inherited from 2 or more rules that don't match at the same time. E.g.:
@@
type T = {r1.T, r2.T};
@@
T *foo =

(quoted diff) +1 -1
-          xyz
+          abc
 ;
Quoted 2 lines

Now in my specific case I'm getting the error:
meta: semantic error: incompatible inheritance declaration T

It should be possible. I'll check with Thierry about it.

Too much mixing of metavariables from different sources tends to cause performance problems. This is just my experience; perhapsitis ok in this case.

julia

Quoted 15 lines

Review

Quoted 10 lines

as illustrated by the following semantic patch
(tests/type_constraints.cocci):

@@
type T != { int, char };
identifier x;
@@

(quoted diff) +1 -1
-   T x;
+   T *x;

thanks
bye
michael

#8 lore raw

Julia?:

Quoted 1 line

It should be possible. I'll check with Thierry about it.

It is now possible (with the version pushed to Github master).

Michael's example is now included in tests/type_constraints.cocci:

| /*Rule given by Michael Stefaniuc*/
| @depends on r2@
| type T = {r1.T, r2.T};
| @@
|  T *foo =
| -          xyz
| +          abc
|  ;

Best regards.

Signature · 3 lines
Thierry.

Julia Lawall (2016/12/08, 13:21 UTC)?:
Quoted 65 lines · Michael Stefaniuc
Quoted 34 lines · Julia Lawall
Quoted 13 lines · Julia Lawall
Quoted 10 lines
Quoted 9 lines

@@
{r1.T, r2.T} obj;
@@

That only matches when both r1.T and r2.T are defined. That kinda feels odd but probably is easier to implement as all inherited variables need to be bound. I have quiet a few duplicated rules across multiple scripts due to that.

Quoted 4 lines
Quoted 2 lines

This problem is fixed by the following patch. Will appear in github shortly, but not immediately.

The change is now available in the github version. It is also now

Thanks Julia, I have tested the change and it seems to work.

Quoted 1 line

possible, thanks to Thierry, to put negated constraints on type variables,

Is there a way to have the positive constraint variant too? That doesn't seem that useful for static types like int and char but can avoid rule duplication when the type is inherited from 2 or more rules that don't match at the same time. E.g.:
@@
type T = {r1.T, r2.T};
@@
T *foo =

(quoted diff) +1 -1
-          xyz
+          abc
 ;
Quoted 2 lines

Now in my specific case I'm getting the error:
meta: semantic error: incompatible inheritance declaration T

It should be possible. I'll check with Thierry about it.

Too much mixing of metavariables from different sources tends to cause performance problems. This is just my experience; perhapsitis ok in this case.

julia

Quoted 11 lines

Review

Quoted 10 lines

as illustrated by the following semantic patch
(tests/type_constraints.cocci):

@@
type T != { int, char };
identifier x;
@@

(quoted diff) +1 -1
-   T x;
+   T *x;
Quoted 4 lines

thanks
bye
michael

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

#9 lore raw
Quoted 4 lines · Thierry Martinez

Julia?:

Quoted 1 line

It should be possible. I'll check with Thierry about it.

It is now possible (with the version pushed to Github master).

Thanks!

julia

Quoted 83 lines

Michael's example is now included in tests/type_constraints.cocci:

| /*Rule given by Michael Stefaniuc*/
| @depends on r2@
| type T = {r1.T, r2.T};
| @@
|  T *foo =
| -          xyz
| +          abc
|  ;

Best regards.
--
Thierry.

Julia Lawall (2016/12/08, 13:21 UTC)?:

Quoted 65 lines · Michael Stefaniuc
Quoted 34 lines · Julia Lawall
Quoted 13 lines · Julia Lawall
Quoted 10 lines
Quoted 9 lines

@@
{r1.T, r2.T} obj;
@@

That only matches when both r1.T and r2.T are defined. That kinda feels odd but probably is easier to implement as all inherited variables need to be bound. I have quiet a few duplicated rules across multiple scripts due to that.

Quoted 4 lines
Quoted 2 lines

This problem is fixed by the following patch. Will appear in github shortly, but not immediately.

The change is now available in the github version. It is also now

Thanks Julia, I have tested the change and it seems to work.

Quoted 1 line

possible, thanks to Thierry, to put negated constraints on type variables,

Is there a way to have the positive constraint variant too? That doesn't seem that useful for static types like int and char but can avoid rule duplication when the type is inherited from 2 or more rules that don't match at the same time. E.g.:
@@
type T = {r1.T, r2.T};
@@
T *foo =

(quoted diff) +1 -1
-          xyz
+          abc
 ;
Quoted 2 lines

Now in my specific case I'm getting the error:
meta: semantic error: incompatible inheritance declaration T

It should be possible. I'll check with Thierry about it.

Too much mixing of metavariables from different sources tends to cause performance problems. This is just my experience; perhapsitis ok in this case.

julia

Quoted 11 lines

Review

Quoted 10 lines

as illustrated by the following semantic patch
(tests/type_constraints.cocci):

@@
type T != { int, char };
identifier x;
@@

(quoted diff) +1 -1
-   T x;
+   T *x;
Quoted 4 lines

thanks
bye
michael

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

#10 lore raw
In reply to Thierry Martinez
Quoted 4 lines · Thierry Martinez

Julia :

Quoted 1 line

It should be possible. I'll check with Thierry about it.

It is now possible (with the version pushed to Github master).

Thanks Thierry,

it works nicely and I've deduplicated some more rules.

But I'm getting a new warning that I wasn't getting before:
warning: r3: metavariable T not used in the - or context code

The exact same rule r4 that just uses type r.T does not produce that warning. Simplified test rules attached.

I don't mind too much as the code works but I thought it is worth mentioning.

bye
michael
-------------- next part --------------
@r1@
type T;
@@
T foo;

@r2@
type T;
@@
T bar;

@r3@
type T = {r1.T, r2.T};
identifier fn;
@@
fn(...) {
+ T foobar;
...
}

@r4@
type r1.T;
identifier fn;
@@
fn(...) {
+ T foo;
...
}