git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] blame.c: replace instance of !oidcmp for oideq

From
Jeff King <peff@peff.net>
Date
Sep 9, 2020, 19:13 UTC
Message-ID
<20200909191345.GA2511547@coredump.intra.peff.net>
In-Reply-To
<CAOc6etZS3mGxsPPh25XFi2-qR0TNzq0Gx1NrydgQwmHbsjxejA@mail.gmail.com>
On Wed, Sep 09, 2020 at 08:00:57AM -0600, Edmundo Carmona Antoranz wrote:
Show 17 quoted lines
> On Wed, Sep 9, 2020 at 3:11 AM Jeff King <peff@peff.net> wrote:
> >
> > Yeah, it looks obviously correct. I am puzzled why "make coccicheck"
> > doesn't find this, though. +cc René, as my favorite target for
> > coccinelle nerd-snipes. :)
> >
> 
> I added this to contrib/coccinelle/object_id.cocci in v2.27.0
> 
> @@
> identifier f != oideq;
> expression E1, E2;
> @@
> - !oidcmp(E1, E2)
> + oideq(E1, E2)
> 
> And it found it:
Interesting. The existing rule is:
  struct object_id *OIDPTR1;
  struct object_id *OIDPTR2;
  @@
  - oidcmp(OIDPTR1, OIDPTR2) == 0
  + oideq(OIDPTR1, OIDPTR2)

The "== 0" part looks like it might be significant, but it's not. Coccinelle knows that "!foo" is the same as "foo == 0" (and you can confirm by tweaking it).

The addition of "identifer f != oideq" here isn't necessary (we don't even define an "f" in the semantic patch part). And anyway, we use hasheq() inside oideq(), so no need to override the rule there.

So the relevant part is probably that our existing rule specifies the exact type, whereas your rule allows any expression.

And indeed, if I do this, it works:
diff --git a/contrib/coccinelle/object_id.cocci b/contrib/coccinelle/object_id.cocci
index ddf4f22bd7..62a6cee0eb 100644
--- a/contrib/coccinelle/object_id.cocci
+++ b/contrib/coccinelle/object_id.cocci
@@ -55,8 +55,8 @@ struct object_id OID;
 + oidcmp(&OID, OIDPTR)
 
 @@
-struct object_id *OIDPTR1;
-struct object_id *OIDPTR2;
+expression OIDPTR1;
+expression OIDPTR2;
 @@
 - oidcmp(OIDPTR1, OIDPTR2) == 0
 + oideq(OIDPTR1, OIDPTR2)

Which really _seems_ like a bug in coccinelle, unless I am missing
something. Because both of those parameters look like object_id pointers
(and the compiler would be complaining if it were not the case).  But I
also wonder if giving the specific types in the coccinelle rule is
buying us anything. If you passed two void pointers or ints or whatever
to !oidcmp(), we'd still want to rewrite it as oideq().

-Peff
Previous: Jeff SmithNext: Jeff King
Message 8 of 14 in “blame.c: replace instance of !oidcmp for oideq”
  1. blame.c: replace instance of !oidcmp for oideqEdmundo Carmona Antoranz, Sep 7, 2020
  2. Edmundo Carmona AntoranzSep 7, 2020
  3. Edmundo Carmona AntoranzSep 8, 2020
  4. Derrick StoleeSep 8, 2020
  5. Jeff KingSep 9, 2020
  6. Edmundo Carmona AntoranzSep 9, 2020
  7. Jeff SmithSep 9, 2020
  8. Jeff KingSep 9, 2020
  9. Jeff KingSep 9, 2020
  10. René ScharfeSep 9, 2020
  11. Jeff KingSep 9, 2020
  12. Junio C HamanoSep 9, 2020
  13. Junio C HamanoSep 9, 2020
  14. René ScharfeSep 9, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.