Skip to contentSkip to conversation

spatch adds zero or two lines after if statement

cocci Andrzej Hajda 3 participants 8 messages Oct 14, 2022 – Oct 18, 2022

lore
Share & tools
lore
#0 lore raw

Adding 'if' statement to the code does not add empty line after it, as expected, but if I explicitly add empty line spatch adds two lines, and this is unexpected. How can I convince spatch to add exactly one line?

Following script:
@@
@@
+ if (1)
+ printf("a\n");
return 0;

@@
@@
+ if (1)
+ printf("b\n");
+
return 1;

##### EOF
used on file:
int a()
{
return 0;
}

int b()
{
return 1;
}
##### EOF
produces:
@@ -1,9 +1,15 @@
int a()
{
+ if (1)
+ printf("a\n");
return 0;
}

int b()
{
+ if (1)
+ printf("b\n");
+
+
return 0;
}
##### EOF

Regards
Andrzej

#1 lore raw
Quoted 3 lines · Andrzej Hajda

Adding 'if' statement to the code does not add empty line after it, as expected, but if I explicitly add empty line spatch adds two lines, and this is unexpected. How can I convince spatch to add exactly one line?

Coccinelle is actually not intended to add any blank lines...

I will try to see why this adds two, though.

thanks,
julia

Quoted 50 lines

Following script:
@@
@@
+ if (1)
+ printf("a\n");
return 0;

@@
@@
+ if (1)
+ printf("b\n");
+
return 1;

##### EOF
used on file:
int a()
{
return 0;
}

int b()
{
return 1;
}
##### EOF
produces:

(quoted diff) +6 -0
@@ -1,9 +1,15 @@
 int a()
 {
+ if (1)
+ printf("a\n");
 return 0;
 }
 
 int b()
 {
+ if (1)
+ printf("b\n");
+
+
 return 0;
 }

##### EOF

Regards
Andrzej

#2 lore raw
Quoted 3 lines
Quoted 3 lines

Adding 'if' statement to the code does not add empty line after it, as expected, but if I explicitly add empty line spatch adds two lines, and this is unexpected.

I find these use cases also interesting.

Quoted 1 line
Quoted 1 line

How can I convince spatch to add exactly one line?

I suggest to take another look at some software development possibilities.

2020-11-06
Checking the handling of line breaks for source code transformations
https://github.com/coccinelle/coccinelle/issues/234

Quoted 1 line

Coccinelle is actually not intended to add any blank lines...

I proprose to reconsider such information in more detail.

* Some cases should be taken into account where the original source code should be preserved as much as possible despite of modification attempts.

2018-05-23
Add a metavariable for the handling of source code
https://github.com/coccinelle/coccinelle/issues/140

* The semantic patch language (Coccinelle software) can eventually help further to achieve a consistent coding style, can't it?

2015-03-10
Make change influence configurable for coding style rules
https://github.com/coccinelle/coccinelle/issues/37

Regards,
Markus

#3 lore raw

Re: [cocci] How to fix diff generation for adding if statements before return statements?

In reply to Julia Lawall
Quoted 1 line

I will try to see why this adds two, though.

Would you like to reconsider also the following implementation details for the generated diff according to the presented transformation test case?

Quoted 1 line
Quoted 1 line

Following script:

…

Quoted 6 lines
Quoted 6 lines

@@
@@
+ if (1)
+ printf("b\n");
+
return 1;

…

Quoted 2 lines
Quoted 2 lines

produces:

(quoted diff) +0 -0
@@ -1,9 +1,15 @@

…

Quoted 8 lines
Quoted 8 lines

int b()
{
+ if (1)
+ printf("b\n");

(quoted diff) +2 -0
+
+

return 0;
}

…

Open issues:
1. Indentation for added if branches
2. Number of blank lines
3. Return values

Regards,
Markus

#4 lore raw

Re: [cocci] How to fix diff generation for adding if statements before return statements?

Quoted 28 lines · Markus Elfring
Quoted 1 line

I will try to see why this adds two, though.

Would you like to reconsider also the following implementation details for the generated diff according to the presented transformation test case?

Quoted 1 line
Quoted 1 line

Following script:

…

Quoted 6 lines
Quoted 6 lines

@@
@@
+ if (1)
+ printf("b\n");
+
return 1;

…

Quoted 2 lines
Quoted 2 lines

produces:

(quoted diff) +0 -0
@@ -1,9 +1,15 @@

…

Quoted 8 lines
Quoted 8 lines

int b()
{
+ if (1)
+ printf("b\n");

(quoted diff) +2 -0
+
+

return 0;
}

…

Quoted 4 lines

Open issues:
1. Indentation for added if branches
2. Number of blank lines
3. Return values

If the return in the original code is not indented, then the if branch is not indented. This is not a bug but a feature. If the return is not indented, Coccinelle assumes that indentation is not wanted.

Coccinelle doesn't change the return value.

So the only open issue is the number of blank lines.

julia

#5 lore raw

Re: [cocci] How to fix diff generation for adding if statements before return statements?

Quoted 22 lines
Quoted 22 lines
Quoted 1 line
Quoted 1 line

Following script:

…

Quoted 6 lines
Quoted 6 lines

@@
@@
+ if (1)
+ printf("b\n");
+
return 1;

…

Quoted 2 lines
Quoted 2 lines

produces:

(quoted diff) +0 -0
@@ -1,9 +1,15 @@

…

Quoted 8 lines
Quoted 8 lines

int b()
{
+ if (1)
+ printf("b\n");

(quoted diff) +2 -0
+
+

return 0;
}

…

Quoted 8 lines
Quoted 4 lines

Open issues:
1. Indentation for added if branches
2. Number of blank lines
3. Return values

If the return in the original code is not indented, then the if branch is not indented. This is not a bug but a feature. If the return is not indented, Coccinelle assumes that indentation is not wanted.

I suggest to reconsider such a development view further.
I imagine that other coding style variants would occasionally be preferred.

Quoted 1 line

Coccinelle doesn't change the return value.

Do you care for this software tool if the value “1” should be found and displayed from the implementation of the function “b” according to the discussed transformation test case instead of “0”?

Quoted 1 line

So the only open issue is the number of blank lines.

I got an other impression.

Regards,
Markus

#6 lore raw

Re: [cocci] How to fix diff generation for adding if statements before return statements?

In reply to Julia Lawall

…

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

int b()
{
+ if (1)
+ printf("b\n");

(quoted diff) +2 -0
+
+

return 0;
}

…

Quoted 2 lines

If the return is not indented,
Coccinelle assumes that indentation is not wanted.

I would like to point another diff format detail out for further considerations according to such information.
I see a space character between a plus character (in the first text column) and C source code.

* Would this usually be interpreted as “indentation”?

* How do you think about variations for possible code beautifications?

Regards,
Markus

#7 lore raw
In reply to Andrzej Hajda
Quoted 3 lines · Andrzej Hajda

Adding 'if' statement to the code does not add empty line after it, as expected, but if I explicitly add empty line spatch adds two lines, and this is unexpected. How can I convince spatch to add exactly one line?

If you explicitly add an empty line, you should now (github version) get exactly one line.

Thanks for the report.

julia

Quoted 50 lines

Following script:
@@
@@
+ if (1)
+ printf("a\n");
return 0;

@@
@@
+ if (1)
+ printf("b\n");
+
return 1;

##### EOF
used on file:
int a()
{
return 0;
}

int b()
{
return 1;
}
##### EOF
produces:

(quoted diff) +6 -0
@@ -1,9 +1,15 @@
 int a()
 {
+ if (1)
+ printf("a\n");
 return 0;
 }
 
 int b()
 {
+ if (1)
+ printf("b\n");
+
+
 return 0;
 }

##### EOF

Regards
Andrzej