Skip to contentSkip to conversation

Adding labels without indentation before specific statements?

Cocci New Markus Elfring 2 participants 3 messages Jul 7, 2020 – Jul 8, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hello,

I have tried another tiny script variant out for the semantic patch language (according to the software combination “Coccinelle 1.0.8-00139-gd0fd4c7d”).

@addition@
identifier work;
expression action;
@@
void work(...)
{
... when any
+last_action:
action(...);
}

Such a simple adjustment approach can work as expected (in principle). It seems that indentation is also applied to the added label according to the function call at the end of found function implementations.

But the Linux coding style prefers to start labels in the first column. How should patches be accordingly generated then?

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

maintainer
#1 lore raw
Quoted 17 lines · Markus Elfring

Hello,

I have tried another tiny script variant out for the semantic patch language (according to the software combination “Coccinelle 1.0.8-00139-gd0fd4c7d”).

@addition@
identifier work;
expression action;
@@
void work(...)
{
... when any
+last_action:
action(...);
}

Quoted 6 lines

Such a simple adjustment approach can work as expected (in principle). It seems that indentation is also applied to the added label according to the function call at the end of found function implementations.

But the Linux coding style prefers to start labels in the first column. How should patches be accordingly generated then?

Probably the pretty printing is not perfect in this case and you are welcome to fix it.

julia

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

#2 lore raw

PATCH 1/1 Coccinelle: Add a SmPL script for the reconsideration of function calls before return statements

In reply to Markus Elfring

From: Markus Elfring <[email protected]>
Date: Wed, 8 Jul 2020 19:09:59 +0200

It can happen that function implementations with the return type “void” call a special function at the end of if branches.
Such calls are occasionally followed by an immediate return. The same function can be called at the end of the function implementation. Thus it can be helpful to replace previous function calls
by goto statements.

Provide design options for the adjustment of affected source code by the means of the semantic patch language (Coccinelle software).

---
 .../misc/goto_last_function_call.cocci        | 89 +++++++++++++++++++
 1 file changed, 89 insertions(+)
 create mode 100644 scripts/coccinelle/misc/goto_last_function_call.cocci
scripts/coccinelle/misc/goto_last_function_call.cocci new file +89 -0
new file mode 100644
index 000000000000..92dcf3626c48
@@ -0,0 +1,89 @@
+// SPDX-License-Identifier: GPL-2.0
+/// Reconsider function calls before a return statement in if branches.
+//
+// Keywords: duplicate function calls common exception handling
+// Confidence: Low
+// See also:
+// Clarification request “Adding labels without indentation before specific statements?”
+// https://lore.kernel.org/cocci/[email protected]/
+
+virtual context, patch, report, org
+
+@display depends on context@
+expression action;
+expression list el;
+identifier work;
+@@
+ void work(...)
+ {
+ <+...
+ if (...)
+ {
+    ...
+*   action(el);
+*   return;
+ }
+ ...+>
+*action(el);
+ }
+
+@replacement depends on patch@
+expression action, condition;
+expression list el;
+identifier work;
+@@
+ void work(...)
+ {
+ <+...
+(
+-if (condition)
+-{
+-   action(el);
+-   return;
+-}
++if (condition)
++   goto last_action;
+|
+ if (...)
+ {
+    ...
+-   action(el);
+-   return;
++   goto last_action;
+ }
+)
+ ...+>
++last_action:
+ action(el);
+ }
+
+@or depends on org || report@
+expression action;
+expression list el;
+identifier work;
+position p;
+@@
+ void work(...)
+ {
+ <+...
+ if (...)
+ {
+    ...
+    action(el);
+    return;
+ }
+ ...+>
+ action@p(el);
+ }
+
+@script:python to_do depends on org@
+p << or.p;
+@@
+coccilib.org.print_todo(p[0],
+                        "WARNING: The same function was called at the end of an if branch before. Would you like to avoid duplicate function calls?")
+
+@script:python reporting depends on report@
+p << or.p;
+@@
+coccilib.report.print_report(p[0],
+                             "WARNING: The same function was called at the end of an if branch before. Would you like to avoid duplicate function calls?")

--
2.27.0