Skip to contentSkip to conversation

Converting compound statement after if to single statement with SmPL?

cocci Markus Elfring 1 participant 1 message Feb 29, 2024

lore
Share & tools
lore
#0 lore raw

Hello,

I would like to share further test results for two simple source code transformations which could be expressed by the means of the semantic patch language also together with the software combination “Coccinelle 1.1.1-00739-g68c580f1”.

Source file example:
int my_test(void)
{
int rc;
// placeholder 1
rc = my_work(my_input);

 if (rc) {
    last_action(my_data);
    return rc;

}
// placeholder 2
return 0;
}

SmPL script variant 1:
@adjustment1@
@@
if (...)
-{
- last_action(my_data);
- return rc;
-}
+ goto do_last_action;

Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> spatch test_last_action_with_return_in_compound_statement.c convert_compound_statement_after_if_to_single_statement1.cocci …
@@ -3,10 +3,7 @@ int my_test(void)
int rc;
// placeholder 1
rc = my_work(my_input);
- if (rc) {
- last_action(my_data);
- return rc;
- }
+ if (rc) goto do_last_action;
// placeholder 2
return 0;
}

SmPL script variant 2:
@adjustment2@
@@
if (...)
-{
- last_action(my_data);
- return rc;
-}
+
+ goto do_last_action;

Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> spatch test_last_action_with_return_in_compound_statement.c convert_compound_statement_after_if_to_single_statement2.cocci …
@@ -3,10 +3,9 @@ int my_test(void)
int rc;
// placeholder 1
rc = my_work(my_input);
- if (rc) {
- last_action(my_data);
- return rc;
- }
+ if (rc)
+
+ goto do_last_action;
// placeholder 2
return 0;
}

Will such diff outputs support any development considerations for tweaks of code pretty-printing?

Regards,
Markus