cocci.inria.fr archive mirror
 help / color / mirror / Atom feed
* [cocci] How do I disable formatting for the "+"-side of a rule?
@ 2022-07-14  9:31 Ævar Arnfjörð Bjarmason
  2022-07-14  9:39 ` Julia Lawall
  0 siblings, 1 reply; 5+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-07-14  9:31 UTC (permalink / raw)
  To: Coccinelle ML

How do I stop coccinelle from turning my "{ 0 };\n" into
"{\n\t\t0\n\t};\n"?

I haven't been able to find in the docs how to disable formatting on the
"+"-side of a rule. I have a rule like:
	
	@@
	type T;
	identifier I;
	@@
	- T I;
	+ T I = { 0 };
	... when != \( I \| &I \)
	- memset(&I, 0, ...);

Which produces the expected results as far as the patch is concerned:
	
	diff -u -p a/builtin/for-each-ref.c b/builtin/for-each-ref.c
	--- a/builtin/for-each-ref.c
	+++ b/builtin/for-each-ref.c
	@@ -20,7 +20,9 @@ int cmd_for_each_ref(int argc, const cha
	 	struct ref_sorting *sorting;
	 	struct string_list sorting_options = STRING_LIST_INIT_DUP;
	 	int maxcount = 0, icase = 0;
	-	struct ref_array array;
	+	struct ref_array array = {
	+		0
	+	};
	 	struct ref_filter filter;
	 	struct ref_format format = REF_FORMAT_INIT;
	 	struct strbuf output = STRBUF_INIT;
	@@ -52,7 +54,6 @@ int cmd_for_each_ref(int argc, const cha
	 		OPT_END(),
	 	};
	 
	-	memset(&array, 0, sizeof(array));
	 	memset(&filter, 0, sizeof(filter));
	 
	 	format.format = "%(objectname) %(objecttype)\t%(refname)";
	
But I'd like for that to be e.g.:

	struct ref_array array = { 0 };

Which is a common idiom both in this codebase (git) and elsewhere.

Usually I can find an answer in one of the coccinelle.git's *.cocci
files, the man page, or the grammar PDF, but in this case I've come up
blank...

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

* Re: [cocci] How do I disable formatting for the "+"-side of a rule?
  2022-07-14  9:31 [cocci] How do I disable formatting for the "+"-side of a rule? Ævar Arnfjörð Bjarmason
@ 2022-07-14  9:39 ` Julia Lawall
  2022-07-14  9:52   ` Ævar Arnfjörð Bjarmason
  0 siblings, 1 reply; 5+ messages in thread
From: Julia Lawall @ 2022-07-14  9:39 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason; +Cc: Coccinelle ML

[-- Attachment #1: Type: text/plain, Size: 1590 bytes --]



On Thu, 14 Jul 2022, Ævar Arnfjörð Bjarmason wrote:

> How do I stop coccinelle from turning my "{ 0 };\n" into
> "{\n\t\t0\n\t};\n"?
>
> I haven't been able to find in the docs how to disable formatting on the
> "+"-side of a rule. I have a rule like:
>
> 	@@
> 	type T;
> 	identifier I;
> 	@@
> 	- T I;
> 	+ T I = { 0 };
> 	... when != \( I \| &I \)
> 	- memset(&I, 0, ...);
>
> Which produces the expected results as far as the patch is concerned:
>
> 	diff -u -p a/builtin/for-each-ref.c b/builtin/for-each-ref.c
> 	--- a/builtin/for-each-ref.c
> 	+++ b/builtin/for-each-ref.c
> 	@@ -20,7 +20,9 @@ int cmd_for_each_ref(int argc, const cha
> 	 	struct ref_sorting *sorting;
> 	 	struct string_list sorting_options = STRING_LIST_INIT_DUP;
> 	 	int maxcount = 0, icase = 0;
> 	-	struct ref_array array;
> 	+	struct ref_array array = {
> 	+		0
> 	+	};
> 	 	struct ref_filter filter;
> 	 	struct ref_format format = REF_FORMAT_INIT;
> 	 	struct strbuf output = STRBUF_INIT;
> 	@@ -52,7 +54,6 @@ int cmd_for_each_ref(int argc, const cha
> 	 		OPT_END(),
> 	 	};
>
> 	-	memset(&array, 0, sizeof(array));
> 	 	memset(&filter, 0, sizeof(filter));
>
> 	 	format.format = "%(objectname) %(objecttype)\t%(refname)";
>
> But I'd like for that to be e.g.:
>
> 	struct ref_array array = { 0 };
>
> Which is a common idiom both in this codebase (git) and elsewhere.
>
> Usually I can find an answer in one of the coccinelle.git's *.cocci
> files, the man page, or the grammar PDF, but in this case I've come up
> blank...

Try --smpl-spacing.  I'm not sure whether it affects newlines, though.

julia

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

* Re: [cocci] How do I disable formatting for the "+"-side of a rule?
  2022-07-14  9:39 ` Julia Lawall
@ 2022-07-14  9:52   ` Ævar Arnfjörð Bjarmason
  2022-07-14 10:47     ` Julia Lawall
  0 siblings, 1 reply; 5+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-07-14  9:52 UTC (permalink / raw)
  To: Julia Lawall; +Cc: Coccinelle ML


On Thu, Jul 14 2022, Julia Lawall wrote:

> On Thu, 14 Jul 2022, Ævar Arnfjörð Bjarmason wrote:
>
>> How do I stop coccinelle from turning my "{ 0 };\n" into
>> "{\n\t\t0\n\t};\n"?
>>
>> I haven't been able to find in the docs how to disable formatting on the
>> "+"-side of a rule. I have a rule like:
>>
>> 	@@
>> 	type T;
>> 	identifier I;
>> 	@@
>> 	- T I;
>> 	+ T I = { 0 };
>> 	... when != \( I \| &I \)
>> 	- memset(&I, 0, ...);
>>
>> Which produces the expected results as far as the patch is concerned:
>>
>> 	diff -u -p a/builtin/for-each-ref.c b/builtin/for-each-ref.c
>> 	--- a/builtin/for-each-ref.c
>> 	+++ b/builtin/for-each-ref.c
>> 	@@ -20,7 +20,9 @@ int cmd_for_each_ref(int argc, const cha
>> 	 	struct ref_sorting *sorting;
>> 	 	struct string_list sorting_options = STRING_LIST_INIT_DUP;
>> 	 	int maxcount = 0, icase = 0;
>> 	-	struct ref_array array;
>> 	+	struct ref_array array = {
>> 	+		0
>> 	+	};
>> 	 	struct ref_filter filter;
>> 	 	struct ref_format format = REF_FORMAT_INIT;
>> 	 	struct strbuf output = STRBUF_INIT;
>> 	@@ -52,7 +54,6 @@ int cmd_for_each_ref(int argc, const cha
>> 	 		OPT_END(),
>> 	 	};
>>
>> 	-	memset(&array, 0, sizeof(array));
>> 	 	memset(&filter, 0, sizeof(filter));
>>
>> 	 	format.format = "%(objectname) %(objecttype)\t%(refname)";
>>
>> But I'd like for that to be e.g.:
>>
>> 	struct ref_array array = { 0 };
>>
>> Which is a common idiom both in this codebase (git) and elsewhere.
>>
>> Usually I can find an answer in one of the coccinelle.git's *.cocci
>> files, the man page, or the grammar PDF, but in this case I've come up
>> blank...
>
> Try --smpl-spacing.  I'm not sure whether it affects newlines, though.

Thanks, no it just changes the spacing of the "=", i.e.:

	+       struct ref_array array = {

v.s. with --smpl-spacing:

	+       struct ref_array array<TAB>= {

(Or similar, I think it's aligning to the "=" above it?)

But the rest is the same, i.e. a \n after the "{", the "0" on its own
line etc.



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

* Re: [cocci] How do I disable formatting for the "+"-side of a rule?
  2022-07-14  9:52   ` Ævar Arnfjörð Bjarmason
@ 2022-07-14 10:47     ` Julia Lawall
  2022-07-15  9:50       ` Ævar Arnfjörð Bjarmason
  0 siblings, 1 reply; 5+ messages in thread
From: Julia Lawall @ 2022-07-14 10:47 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason; +Cc: Coccinelle ML

> Thanks, no it just changes the spacing of the "=", i.e.:
>
> 	+       struct ref_array array = {
>
> v.s. with --smpl-spacing:
>
> 	+       struct ref_array array<TAB>= {

Strange about the tab.  There was no intent to have alignment.

> (Or similar, I think it's aligning to the "=" above it?)
>
> But the rest is the same, i.e. a \n after the "{", the "0" on its own
> line etc.

Coccinelle now just does what you asked for.  If there is one element in
the initializer, it doesn't add newlines.  So no need for the
--smpl-spacing argument.

julia

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

* Re: [cocci] How do I disable formatting for the "+"-side of a rule?
  2022-07-14 10:47     ` Julia Lawall
@ 2022-07-15  9:50       ` Ævar Arnfjörð Bjarmason
  0 siblings, 0 replies; 5+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-07-15  9:50 UTC (permalink / raw)
  To: Julia Lawall; +Cc: Coccinelle ML


On Thu, Jul 14 2022, Julia Lawall wrote:

>> Thanks, no it just changes the spacing of the "=", i.e.:
>>
>> 	+       struct ref_array array = {
>>
>> v.s. with --smpl-spacing:
>>
>> 	+       struct ref_array array<TAB>= {
>
> Strange about the tab.  There was no intent to have alignment.
>
>> (Or similar, I think it's aligning to the "=" above it?)
>>
>> But the rest is the same, i.e. a \n after the "{", the "0" on its own
>> line etc.
>
> Coccinelle now just does what you asked for.  If there is one element in
> the initializer, it doesn't add newlines.  So no need for the
> --smpl-spacing argument.

Fantastic! I just tried with your
https://github.com/coccinelle/coccinelle/commit/bcc39cff36dcc8bbad3e2131fd92addc3ffa4ee5
and it formats it as expected, thanks a lot for the help & quick fix!

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

end of thread, other threads:[~2022-07-15  9:51 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-14  9:31 [cocci] How do I disable formatting for the "+"-side of a rule? Ævar Arnfjörð Bjarmason
2022-07-14  9:39 ` Julia Lawall
2022-07-14  9:52   ` Ævar Arnfjörð Bjarmason
2022-07-14 10:47     ` Julia Lawall
2022-07-15  9:50       ` Ævar Arnfjörð Bjarmason

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).