Coccinelle archive mirror
 help / color / mirror / Atom feed
From: Eric Wheeler <cocci@lists.ewheeler.net>
To: cocci@inria.fr
Subject: [cocci] Strange spatch behavior while patching an array of structs
Date: Mon, 19 Jun 2023 17:19:53 -0700 (PDT)	[thread overview]
Message-ID: <b28e953-86e3-a6bb-fc84-1d2794a81135@ewheeler.net> (raw)

Hello all,

I ran the following .cocci spatch on the code below (which is a minimum 
excerpt from [1] for the project [2]) and was surprised by the results. If 
I understand .cocci (and perhaps this is my misunderstanding) then I would 
expect the patch _not_ to apply here at all.

Here is my spatch version:
	spatch version 1.1.1-00106-ge65a6dd compiled with OCaml version 4.05.0
	Flags passed to the configure script: --prefix=/usr/local/coccinelle-git/
	OCaml scripting support: yes
	Python scripting support: yes
	Syntax of regular expressions: PCRE

Please see my question at the bottom.  Here are the relevant bits of code:

-- t.c ------------------------------------------------------------------------------
#include <stdio.h>

struct rotor {
	struct motor {
		char *name;
	} motor;

	struct cal {
		float deg, v;
	} cal[90];

} rotors[4] =
	{
		{ .motor = { .name = "foo1" }, .cal = {{ .deg = 0, .v = 1 }, { .deg = 360, .v = 2 }} },
		{ .motor = { .name = "foo2" }, .cal = {{ .deg = 0, .v = 2 }, { .deg = 360, .v = 5 }} },
		{ .motor = { .name = "foo3" }, .cal = {{ .deg = 0, .v = 4 }, { .deg = 360, .v = 9 }} },
		{ .motor = { .name = "foo4" }, .cal = {{ .deg = 0, .v = 6 }, { .deg = 360, .v = 12 }} },
	};

int main()
{
	int i;
	for (i = 0; i < 4; i++)
	{
		printf("%s cal: [%.2f, %.2f] deg = [%.2f, %.2f] volts, %.4f mV/deg\r\n", 
			rotors[i].motor.name,
			rotors[i].cal[0].deg, rotors[i].cal[1].deg,
			rotors[i].cal[0].v, rotors[i].cal[1].v,
			(rotors[i].cal[1].v - rotors[i].cal[0].v) / (rotors[i].cal[1].deg - rotors[i].cal[0].deg) * 1000
			);
	}
}
-------------------------------------------------------------------------------------

-- cal.spatch -----------------------------------------------------------------------
@@
identifier R, X;
@@

-R->cal[0].X
+rotor_cal_min(R)->X
-------------------------------------------------------------------------------------

-- output ---------------------------------------------------------------------------
]$ spatch --sp-file cal.spatch t.c
init_defs_builtins: /usr/lib64/coccinelle/standard.h
HANDLING: t.c
diff = 
--- t.c
+++ /tmp/cocci-output-452476-33c7f6-t.c
@@ -24,9 +24,9 @@ int main()
 	{
 		printf("%s cal: [%.2f, %.2f] deg = [%.2f, %.2f] volts, %.4f mV/deg\r\n", 
 			rotors[i].motor.name,
-			rotors[i].cal[0].deg, rotors[i].cal[1].deg,
-			rotors[i].cal[0].v, rotors[i].cal[1].v,
-			(rotors[i].cal[1].v - rotors[i].cal[0].v) / (rotors[i].cal[1].deg - rotors[i].cal[0].deg) * 1000
+			rotor_cal_min(rotors)->deg, rotors[i].cal[1].deg,
+			rotor_cal_min(rotors)->v, rotors[i].cal[1].v,
+			(rotors[i].cal[1].v - rotor_cal_min(rotors)->v) / (rotors[i].cal[1].deg - rotor_cal_min(rotors)->deg) * 1000
 			);
 	}
 }
-------------------------------------------------------------------------------------


Question:

Why did spatch match R in "R->cal[0].X" as "rotors" and drop the array 
index "[i]" all together to replace as follows?

	-rotors[i].cal[0].deg
	+rotor_cal_min(rotors)->deg

Note that the spatch specifies "R->" , not "R." nor even "R[n]." ; it is 
as though spatch interpreted it as follows:
	-R[n].cal[0].X
	+rotor_cal_min(R)->X

...so for some reason it picked up "rotors[i]" as "R" and replaced it with 
just "rotors"...which, is indeed a pointer, but looses the meaning as 
being a pointer to the beginning of the array indexed by "[i]".

I am confused as to why it matched "rotors[i].cal" at all. It should not 
have matched "rotors[i]" at all, right?

Is this a bug, or an error in my understanding of SML?

References: 

[1] https://github.com/KJ7NLL/space-ham/blob/master/main.c
[2] "Track the Space Station and OreSat Satellites with a LEGO Rotor..."
	https://www.youtube.com/watch?v=vrlw4QPKMRY

--
Eric Wheeler

             reply	other threads:[~2023-06-20  0:20 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-20  0:19 Eric Wheeler [this message]
2023-06-20  7:01 ` [cocci] Strange spatch behavior while patching an array of structs Julia Lawall
2023-06-20 21:51   ` Eric Wheeler
2023-06-20  8:22 ` Markus Elfring
2023-06-20 12:15 ` Markus Elfring
2023-06-20 21:52   ` Eric Wheeler
2023-06-20 21:56     ` Julia Lawall

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b28e953-86e3-a6bb-fc84-1d2794a81135@ewheeler.net \
    --to=cocci@lists.ewheeler.net \
    --cc=cocci@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 read-only IMAP folder(s) and NNTP newsgroup(s).