Coccinelle archive on lore.kernel.org
 help / color / Atom feed
* [Cocci] Pretty-printing of code for ternary operators?
@ 2019-07-04  7:33 Markus Elfring
       [not found] ` <alpine.DEB.2.20.1907041233010.3517@hadrien>
  0 siblings, 1 reply; 3+ messages in thread
From: Markus Elfring @ 2019-07-04  7:33 UTC (permalink / raw)
  To: Coccinelle

Hello,

The semantic patch language supports also to convert conditional statements
into usages of the ternary operator.
How much can the Coccinelle software help with pretty-printing of generated
source code in this area?
Would you like to achieve any improvements for automatic code beautification?
https://github.com/coccinelle/coccinelle/issues/37

Regards,
Markus
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Cocci] Pretty-printing of code for ternary operators?
       [not found] ` <alpine.DEB.2.20.1907041233010.3517@hadrien>
@ 2019-07-04 11:40   ` Markus Elfring
       [not found]     ` <alpine.DEB.2.20.1907041343250.3517@hadrien>
  0 siblings, 1 reply; 3+ messages in thread
From: Markus Elfring @ 2019-07-04 11:40 UTC (permalink / raw)
  To: Julia Lawall; +Cc: Coccinelle

>> Would you like to achieve any improvements for automatic code beautification?
>> https://github.com/coccinelle/coccinelle/issues/37
>
> If you want to report a problem,

I became curious again if more contributors would become interested
to influence a possibly known software situation a bit more.


> you must include a semantic patch

@replacement@
expression check, context, x1, x2;
identifier action;
@@
-if (check)
-   action(context, x1);
-else
-   action(context, x2);
+action(context, (check) ? x1 : x2);


> and C code that gives an unsatisfactory result.

Source file example:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/mm/ksm.c?id=550d1f5bda33fa3b203d8cf8df1396825dbfd213#n2500
https://elixir.bootlin.com/linux/v5.2-rc7/source/mm/ksm.c#L2500


// SPDX-License-Identifier: GPL-2.0-only

// Deleted part

int __ksm_enter(struct mm_struct *mm)
{
	struct mm_slot *mm_slot;
	int needs_wakeup;

	mm_slot = alloc_mm_slot();
	if (!mm_slot)
		return -ENOMEM;

	/* Check ksm_run too?  Would need tighter locking */
	needs_wakeup = list_empty(&ksm_mm_head.mm_list);

	spin_lock(&ksm_mmlist_lock);
	insert_to_mm_slots_hash(mm, mm_slot);

// Deleted part

	if (ksm_run & KSM_RUN_UNMERGE)
		list_add_tail(&mm_slot->mm_list, &ksm_mm_head.mm_list);
	else
		list_add_tail(&mm_slot->mm_list, &ksm_scan.mm_slot->mm_list);
	spin_unlock(&ksm_mmlist_lock);

// Deleted part

	return 0;
}

// Deleted part


Another test result:

elfring@Sonne:~/Projekte/Coccinelle/Probe> spatch ../janitor/use_call_with_ternary_operator1.cocci ksm-excerpt1.c
…
+	list_add_tail(&mm_slot->mm_list,
+		      (ksm_run & KSM_RUN_UNMERGE) ? &ksm_mm_head.mm_list : &ksm_scan.mm_slot->mm_list);
…



How likely is it that the combined source code for a single function parameter
will fit into known line length limitations?

Regards,
Markus
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Cocci] Pretty-printing of code for ternary operators?
       [not found]     ` <alpine.DEB.2.20.1907041343250.3517@hadrien>
@ 2019-07-04 12:05       ` Markus Elfring
  0 siblings, 0 replies; 3+ messages in thread
From: Markus Elfring @ 2019-07-04 12:05 UTC (permalink / raw)
  To: Julia Lawall; +Cc: Coccinelle

>> elfring@Sonne:~/Projekte/Coccinelle/Probe> spatch ../janitor/use_call_with_ternary_operator1.cocci ksm-excerpt1.c
>> …
>> +	list_add_tail(&mm_slot->mm_list,
>> +		      (ksm_run & KSM_RUN_UNMERGE) ? &ksm_mm_head.mm_list : &ksm_scan.mm_slot->mm_list);
>
> And this is unsatisfactory because why?

Such a transformation result could be acceptable if the generated line length
would be tolerated.
But the second function parameter should be reformatted if we would like to care
for the Linux coding style in such an use case finally.


> I don't think ? should be used if it is going to end up over multiple lines.

I got an other software development opinion.


> In any case, I already commented that you proposed code was unreadable.

I am also curious on how the clarification for a patch on a topic like
“NFS: Less function calls in show_pnfs()” will evolve further.
https://lore.kernel.org/patchwork/patch/1096614/#1293018
https://lore.kernel.org/lkml/ed599239-a6a2-1cc6-14ce-d5bb41e9170f@web.de/
https://lkml.org/lkml/2019/7/3/173

Regards,
Markus
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, back to index

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-04  7:33 [Cocci] Pretty-printing of code for ternary operators? Markus Elfring
     [not found] ` <alpine.DEB.2.20.1907041233010.3517@hadrien>
2019-07-04 11:40   ` Markus Elfring
     [not found]     ` <alpine.DEB.2.20.1907041343250.3517@hadrien>
2019-07-04 12:05       ` Markus Elfring

Coccinelle archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/cocci/0 cocci/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 cocci cocci/ https://lore.kernel.org/cocci \
		cocci@systeme.lip6.fr cocci@archiver.kernel.org
	public-inbox-index cocci


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/fr.lip6.systeme.cocci


AGPL code for this site: git clone https://public-inbox.org/ public-inbox