Skip to contentSkip to conversation

Checking the influence of an omitted semicolon on a code adjustment

Cocci Markus Elfring 2 participants 16 messages Nov 28, 2020 – Nov 29, 2020 9 messages not in the archive

lore
Share & tools
lore
#0 lore raw

Hello,

The following small SmPL script gets successfully parsed by the Coccinelle software.

@Replacement@
expression call, input, target;
identifier gs;
@@
-\( g_string_assign@gs \| g_string_append@gs \) (target, input);
call (
- target
+ gs (target, input)

      );

But if I would like to omit the semicolon in the last line, I stumble on the error message “incompatible minus and plus code starting on lines 5 and 6”. Can such a software limitation be adjusted?

Regards,
Markus
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#1 lore raw
Quoted 16 lines · Markus Elfring

Hello,

The following small SmPL script gets successfully parsed by the Coccinelle software.

@Replacement@
expression call, input, target;
identifier gs;
@@
-\( g_string_assign@gs \| g_string_append@gs \) (target, input);
call (

(quoted diff) +1 -1
-      target
+      gs (target, input)
      );
Quoted 3 lines

But if I would like to omit the semicolon in the last line, I stumble on the error message “incompatible minus and plus code starting on lines 5 and 6”. Can such a software limitation be adjusted?

Why do you want to remove the semicolon?

If you want to find the call somewhere in the next statement, you can say

(
S
&
call(
...
)
)

where S is a statement metavariable. I'm not sure what will happen if there are two occurrences of call in the statement.

julia

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#2 lore raw
Quoted 1 line

Why do you want to remove the semicolon?

I have shown a transformation example where a function parameter should be replaced by a previous function call.
Thus a semicolon should be intentionally be deleted.

Quoted 11 lines

If you want to find the call somewhere in the next statement, you can say

(
S
&
call(
...
)
)

where S is a statement metavariable.

This SmPL specification variant can be also interesting.

I got just interested in the possibility to omit an extra semicolon in my simple change approach.

Regards,
Markus
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#3 lore raw
Quoted 5 lines · Markus Elfring
Quoted 1 line

Why do you want to remove the semicolon?

I have shown a transformation example where a function parameter should be replaced by a previous function call.
Thus a semicolon should be intentionally be deleted.

That makes no sense. You can't have an expression directly following a statement. Only statements follow other statements.

julia

Quoted 14 lines
Quoted 12 lines

If you want to find the call somewhere in the next statement, you can say

(
S
&
call(
...
)
)

Quoted 1 line

where S is a statement metavariable.

Quoted 8 lines

This SmPL specification variant can be also interesting.

I got just interested in the possibility to omit an extra semicolon in my simple change approach.

Regards,
Markus

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#4 lore raw
Quoted 5 lines
Quoted 3 lines

I have shown a transformation example where a function parameter should be replaced by a previous function call.
Thus a semicolon should be intentionally be deleted.

That makes no sense.

This transformation part is working as expected (under constraints) already.

Quoted 2 lines

You can't have an expression directly following a statement. Only statements follow other statements.

This information is reasonable.

Does the Coccinelle software insist on the specification of another semicolon in the SmPL script for the identification of an adjustable statement?

Regards,
Markus
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#5 lore raw
Quoted 9 lines · Markus Elfring
Quoted 5 lines
Quoted 3 lines

I have shown a transformation example where a function parameter should be replaced by a previous function call.
Thus a semicolon should be intentionally be deleted.

That makes no sense.

This transformation part is working as expected (under constraints) already.

Quoted 7 lines
Quoted 2 lines

You can't have an expression directly following a statement. Only statements follow other statements.

This information is reasonable.

Does the Coccinelle software insist on the specification of another semicolon in the SmPL script for the identification of an adjustable statement?

Yes

Quoted 4 lines

Regards,
Markus

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#6 lore raw
Quoted 9 lines
Quoted 7 lines
Quoted 2 lines

You can't have an expression directly following a statement. Only statements follow other statements.

This information is reasonable.

Does the Coccinelle software insist on the specification of another semicolon in the SmPL script for the identification of an adjustable statement?

Yes

I imagine that another view can become applicable here.

* It is possible to move a function call with a known property into a parameter of a subsequent function/macro call.

* I care more for the implementation detail that a following function/macro call exists and less that this source code needs to be an complete statement (together with the usual semicolon at the end).

I stumble on further software challenges if I would like to make the simple transformation approach a bit more variable on the number of surrounding parameters.

Regards,
Markus
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

A message that is not in the archive (sent off-list or not yet received): [email protected]

#8 lore raw

Re: [Cocci] Adjusting some calls according to pass-through functions

Quoted 17 lines · Markus Elfring
Quoted 2 lines

I stumble on further software challenges if I would like to make the simple transformation approach a bit more variable on the number of surrounding parameters.

It looked promising that another SmPL script variant gets also successfully parsed by the current Coccinelle software.

@Replacement@
expression action, input1, input2, input3, target;
identifier gs;
@@
-\( g_string_assign@gs \| g_string_append@gs \| g_string_append_c@gs \) (target, input1);
action (

(quoted diff) +2 -0
+        gs (
             target
+            , input1)
             ,
Quoted 3 lines

( input2, input3
| input2
) );

I find this way of writing disjunctions really unreadable. You are saving some precious whitespace characters at the cost of making any distinction between what is obligatory and what is optional.

Quoted 40 lines

But another test run shows opportunities for further clarification of the following software behaviour.

elfring@Sonne:~/Projekte/Gnome/Evolution-Data-Server/lokal> spatch --smpl-spacing --max-width 120 src/addressbook/libedata-book/e-book-sqlite.c ~/Projekte/Gnome/use_string_action_return_pointer8.cocci …
previous modification:
MINUS

According to environment 2:

   Replacement.input1 -> ' '

   Replacement.gs -> id g_string_append_c

current modification:

<<< gs(
CONTEXT

Quoted 1 line
Quoted 1 line
Quoted 1 line

, input1)

According to environment 2:

   Replacement.input1 -> info->name

   Replacement.gs -> id g_string_append

Replacement: already tagged token:
C code context
File "src/addressbook/libedata-book/e-book-sqlite.c", line 1909, column 20, charpos = 53094
around = 'string',
whole content = g_string_append_c (string, ' ');

The implementation of a function like “format_column_declaration” contains the following statements.
https://gitlab.gnome.org/GNOME/evolution-data-server/-/blob/3de7ea5fdb5d5db4a097a4d888d77bd2778c1c30/src/addressbook/libedata-book/e-book-sqlite.c#L1898

Quoted 9 lines
	g_string_append (string, info->name);
	g_string_append_c (string, ' ');

	g_string_append (string, info->type);

	if (info->extra) {
		g_string_append_c (string, ' ');
		g_string_append (string, info->extra);
	}

The rule matches for example on the first line and on the second line. The match on the first line makes a change in the second line. The match on the second line removes the second line. This is a conflict, as reported.

The problem would be solved in this case by making two separate rules.

julia

Quoted 10 lines

I imagine that such source code can be transformed into the following variant.

	g_string_append (g_string_append_c (g_string_append (string, info->name), ' '), info->type);

	if (info->extra)
		g_string_append (g_string_append_c (string, ' '), info->extra);
Quoted 5 lines

Do you find any extensions interesting for such a code refactoring?

Regards,
Markus

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

A message that is not in the archive (sent off-list or not yet received): [email protected]

#10 lore raw

Re: [Cocci] Adjusting some calls according to pass-through functions

Quoted 48 lines · Markus Elfring
Quoted 14 lines
Quoted 10 lines

@Replacement@
expression action, input1, input2, input3, target;
identifier gs;
@@
-\( g_string_assign@gs \| g_string_append@gs \| g_string_append_c@gs \) (target, input1);
action (

(quoted diff) +2 -0
+        gs (
             target
+            , input1)
             ,
Quoted 3 lines

( input2, input3
| input2
) );

Quoted 1 line

I find this way of writing disjunctions really unreadable.

I am using different coding styles for SmPL disjunctions because of specific reasons.

Quoted 2 lines

You are saving some precious whitespace characters at the cost of making any distinction between what is obligatory and what is optional.

I tend to increase such distinction possibilities.

Quoted 7 lines
Quoted 7 lines

The implementation of a function like “format_column_declaration” contains the following statements.
https://gitlab.gnome.org/GNOME/evolution-data-server/-/blob/3de7ea5fdb5d5db4a097a4d888d77bd2778c1c30/src/addressbook/libedata-book/e-book-sqlite.c#L1898

	g_string_append (string, info->name);
	g_string_append_c (string, ' ');
Quoted 8 lines
Quoted 6 lines
	g_string_append (string, info->type);

	if (info->extra) {
		g_string_append_c (string, ' ');
		g_string_append (string, info->extra);
	}

The rule matches for example on the first line and on the second line.

Yes. - The transformation approach should work so in principle.

Quoted 3 lines

The match on the first line makes a change in the second line. The match on the second line removes the second line. This is a conflict, as reported.

Quoted 6 lines

I imagine that the error message can be improved somehow.

Quoted 1 line

The problem would be solved in this case by making two separate rules.

I find this suggestion unclear at the moment.

A rule for each of the functions you have in the big disjunction you have on the first line of your rule. But it only helps in your case because in your C code, the first two lines have different function names.

Quoted 5 lines

Two changes should be performed in combination for a code replacement. How should a separation help then?

Can the shown change pattern be applied recursively?

You can follow the example in demos/iteration.cocci.

julia

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

A message that is not in the archive (sent off-list or not yet received): [email protected]

#12 lore raw

Re: [Cocci] Adjusting some calls according to pass-through functions

Quoted 6 lines · Markus Elfring
Quoted 2 lines

A rule for each of the functions you have in the big disjunction you have on the first line of your rule.

I find that the size of such SmPL disjunctions can grow considerably.

Quoted 6 lines
Quoted 2 lines

But it only helps in your case because in your C code, the first two lines have different function names.

This would be usual for the C programming language.
Would you ever like to consider additional development challenges for C++ where distinctions would be more relevant for data types on input parameters?

I don't see any connection between your comment and what I said. I think that in both C and C++, people may call the same function on two subsequent lines.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Two changes should be performed in combination for a code replacement. How should a separation help then?

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Can the shown change pattern be applied recursively?

You can follow the example in demos/iteration.cocci.

Does a corresponding information like “Modification is not allowed when using iteration.” need further clarification for the discussed use case?
https://github.com/coccinelle/coccinelle/blob/730dbb034559b3e549ec0b2973cd0400a3fa072f/docs/manual/cocci_syntax.tex#L1999

If you use the argument --in-place, there is no restriction.

julia

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

A message that is not in the archive (sent off-list or not yet received): [email protected]

#14 lore raw

Re: [Cocci] Adjusting some calls according to pass-through functions

Quoted 18 lines · Markus Elfring
Quoted 10 lines
Quoted 6 lines
Quoted 2 lines

But it only helps in your case because in your C code, the first two lines have different function names.

This would be usual for the C programming language.
Would you ever like to consider additional development challenges for C++ where distinctions would be more relevant for data types on input parameters?

I don't see any connection between your comment and what I said. I think that in both C and C++, people may call the same function on two subsequent lines.

Yes. - This can happen.

The programming language “C++” supports also function overloading. Thus the function (or method) name does not need to change for different data processing effects.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Does a corresponding information like “Modification is not allowed when using iteration.” need further clarification for the discussed use case?
https://github.com/coccinelle/coccinelle/blob/730dbb034559b3e549ec0b2973cd0400a3fa072f/docs/manual/cocci_syntax.tex#L1999

If you use the argument --in-place, there is no restriction.

Would you like to add this detail also to the software documentation?

OK

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

A message that is not in the archive (sent off-list or not yet received): [email protected]

#16 lore raw

Re: [Cocci] Adjusting some calls according to pass-through functions

Quoted 5 lines · Markus Elfring
Quoted 2 lines

But it only helps in your case because in your C code, the first two lines have different function names.

Would any more software extensions be needed for the discussed use case if the same function call would be repeated at subsequent places?

I think it would suffice to iterate over a rule that ensures that the pattern of interest does not occur before the place where it is matched. For a more simple exmple:

... when != A(); B();
- A()
B(
+ A()
)

julia
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

A message that is not in the archive (sent off-list or not yet received): [email protected]

#18 lore raw

Re: [Cocci] Adjusting some calls according to pass-through functions

Quoted 6 lines · Markus Elfring
Quoted 5 lines
Quoted 2 lines

Would any more software extensions be needed for the discussed use case if the same function call would be repeated at subsequent places?

I think it would suffice to iterate over a rule that ensures that the pattern of interest does not occur before the place where it is matched.

Quoted 6 lines

It seems that I stumble still on understanding difficulties for the application of “iteration” according to the SmPL meaning.

Something should provide elements with which an iteration step can work finally. I find it challenging to extract such information from mentioned demo scripts.
https://github.com/coccinelle/coccinelle/blob/730dbb034559b3e549ec0b2973cd0400a3fa072f/docs/manual/cocci_syntax.tex#L1981

When you rule matches, ie you make a change, then you can run Coccinelle again on the result, by calling something like the add_if_not_present function shown in the example.

julia

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

A message that is not in the archive (sent off-list or not yet received): [email protected]

#20 lore raw

Re: [Cocci] Adjusting some calls according to pass-through functions

Quoted 14 lines · Markus Elfring
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

I think it would suffice to iterate over a rule that ensures that the pattern of interest does not occur before the place where it is matched.

It seems that I stumble still on understanding difficulties for the application of “iteration” according to the SmPL meaning.

Quoted 7 lines
Quoted 3 lines

Something should provide elements with which an iteration step can work finally. I find it challenging to extract such information from mentioned demo scripts.
https://github.com/coccinelle/coccinelle/blob/730dbb034559b3e549ec0b2973cd0400a3fa072f/docs/manual/cocci_syntax.tex#L1981

When you rule matches, ie you make a change, then you can run Coccinelle again on the result, by calling something like the add_if_not_present function shown in the example.

Quoted 6 lines

I find this information only partly reasonable.

I guess that I know the set of identifiers already for which a transformation loop could be performed.
I am unsure about the way to integrate such items into a “demonstrated” data processing approach so far.

I think that the only part of the functionality that you need is

it = Iteration()
it.register()

julia

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

A message that is not in the archive (sent off-list or not yet received): [email protected]

#22 lore raw

Re: [Cocci] Adjusting some calls according to pass-through functions

Quoted 5 lines · Markus Elfring
Quoted 5 lines
Quoted 4 lines

I guess that I know the set of identifiers already for which a transformation loop could be performed.
I am unsure about the way to integrate such items into a “demonstrated” data processing approach so far.

Quoted 7 lines
Quoted 4 lines

I think that the only part of the functionality that you need is

it = Iteration()
it.register()

Where should I specify the known function names for which a transformation should be tried?

In your rule. In the place of A, in my example.

I would still suggest to have one rule per function, rather than trying to do everything at once.

julia

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

A message that is not in the archive (sent off-list or not yet received): [email protected]

#24 lore raw

Re: [Cocci] Adjusting some calls according to pass-through functions

Quoted 38 lines · Markus Elfring
Quoted 1 line

In your rule. In the place of A, in my example.

I find such information too terse.

I hope that the software documentation will be improved further for transformation loops.

Quoted 2 lines

I would still suggest to have one rule per function, rather than trying to do everything at once.

I have tried such a SmPL script variant out a moment ago.

elfring@Sonne:~/Projekte/Gnome/Evolution-Data-Server/lokal> spatch --smpl-spacing --max-width 120 src/addressbook/libedata-book/e-book-sqlite.c ~/Projekte/Gnome/use_string_action_return_pointer9.cocci
init_defs_builtins: /usr/local/bin/../lib/coccinelle/standard.h
HANDLING: src/addressbook/libedata-book/e-book-sqlite.c

previous modification:
MINUS

According to environment 2:

   Replacement2.input1 -> field->dbname

   Replacement2.gs -> id g_string_append

current modification:

<<< gs(
CONTEXT

Quoted 1 line
Quoted 1 line
Quoted 1 line

, input1)

According to environment 2:

   Replacement2.input1 -> ", "

   Replacement2.gs -> id g_string_append
Quoted 5 lines

Replacement2: already tagged token:
C code context
File "src/addressbook/libedata-book/e-book-sqlite.c", line 3629, column 21, charpos = 101199
around = 'string',
whole content = g_string_append (string, field->dbname);

Since you have provided neither your semantic patch nor the source code to which it is applied, this information is not useful.

Quoted 2 lines

Do you find the implementation of a function like “ebsql_prepare_insert” more interesting then?
https://gitlab.gnome.org/GNOME/evolution-data-server/-/blob/3de7ea5fdb5d5db4a097a4d888d77bd2778c1c30/src/addressbook/libedata-book/e-book-sqlite.c#L3598

I'm not sure that Coccinelle is well suited for the problem you are trying to address. Normally, one works on code fragments that are more disjoint from each other.

julia

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci