cocci.inria.fr archive mirror
 help / color / mirror / Atom feed
From: "Jürgen Groß" <jgross@suse.com>
To: Julia Lawall <julia.lawall@lip6.fr>
Cc: cocci@systeme.lip6.fr
Subject: Re: [Cocci] macro parameters and expressions?
Date: Mon, 14 Oct 2019 15:33:04 +0200	[thread overview]
Message-ID: <c03fd3e3-685b-1305-54c0-e3063201cd31@suse.com> (raw)
In-Reply-To: <alpine.DEB.2.21.1910141309080.2330@hadrien>

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

On 14.10.19 13:14, Julia Lawall wrote:
> 
> 
> On Mon, 14 Oct 2019, Jürgen Groß wrote:
> 
>> On 11.10.19 22:00, Julia Lawall wrote:
>>>
>>>
>>> On Fri, 11 Oct 2019, Jürgen Groß wrote:
>>>
>>>> Hi,
>>>>
>>>> I have a simple semantic patch:
>>>>
>>>>     virtual patch
>>>>
>>>>     @@
>>>>     expression buf, val;
>>>>     @@
>>>>     - snprintf(buf, PAGE_SIZE, "%d\n", val)
>>>>     + spgprintf_d(buf, val)
>>>>
>>>> This works nearly always as expected, but not in some macros. The cases
>>>> where it is not working are those when "val" is using a parameter of the
>>>> macro, e.g.:
>>>>
>>>>     #define MACRO(name)    snprintf(pg, PAGE_SIZE, %u\n", ptr->name)
>>>>
>>>> Is that on purpose? If yes, how can this be avoided?
>>>
>>> I don't think it should be on purpose.  Could you send some C code that
>>> illustrates the problem?
>>
>> I have attached a little C file and a semantic patch, which I had
>> located in a local directory.
>>
>> I invoke spatch via: "spatch --cocci-file patch.cocci --patch . --dir ."
>>
>> The result shows that the problem is a little bit different from my
>> first analysis: whether a pattern is recognized or not seems to depend
>> on macro parameter usage, which is not limited to the pattern itself.
> 
> Thanks for the examples.  The problem in these examples actually has
> nothing to with parameter usage, but with the ability to parse the macro
> definition.  I get:
> 
>   #define macro1a(par)            \
> -        func(buf, 1, par)
> +        func2(buf, par)
> 
> Tha above mentions a parameter but the code gets changed.  This case is no
> problem because the body of the macro is an expression.
> 
> 
>   #define macro1b(par)            \
>           func(buf, 1, par)       \
>           func(buf, 1, par)
> 
> Nothing happens here.  It is not possible in C to have one function call
> after another like this.

Oh sorry, typo by me.

> 
> 
>   #define macro2a(par)            \
>           par++;                  \
>           func(buf, 1, 17)
> 
> Nothing happens here either.  Because the final ; is omitted the body of
> the macro is not valid C.  But it doesn't work if you add a semicolon at
> the end either.  It seems that a sequence of statements is not supported
> either.  OOn the other hand, if you transform it into a do while(0), even
> with no trailing semicolon after the while (0) all is fine.

This is not always possible. See the new example as attached.

This is more like the original problem (stripped down a lot).


Juergen

[-- Attachment #2: tst.c --]
[-- Type: text/x-csrc, Size: 881 bytes --]

#include <stdio.h>

#define macro1(prefix, name)                              \
static int func##prefix##_##name##_show(char *page)       \
{                                                         \
        return snprintf(page, 1, "%d\n", name);           \
}

#define macro2(name)                                      \
static int func##name##_show(char *page)                  \
{                                                         \
        int rb;                                           \
                                                          \
        rb = snprintf(page, 1, "%d\n", name);             \
                                                          \
        return rb;                                        \
}                                                         \
                                                          \
macro1(pre, name)

[-- Attachment #3: patch.cocci --]
[-- Type: text/plain, Size: 81 bytes --]

@@
expression buf, val;
@@
- snprintf(buf, 1, "%d\n", val)
+ spgprintf(buf, val)

[-- Attachment #4: Type: text/plain, Size: 136 bytes --]

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

  parent reply	other threads:[~2019-10-14 13:33 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-11  7:42 [Cocci] macro parameters and expressions? Jürgen Groß
2019-10-11 20:00 ` Julia Lawall
2019-10-13 13:37   ` Markus Elfring
     [not found]     ` <alpine.DEB.2.21.1910131806400.2565@hadrien>
2019-10-13 16:33       ` [Cocci] Adjusting macros with SmPL? Markus Elfring
2019-10-13 16:37         ` Julia Lawall
2019-10-13 16:50           ` Markus Elfring
2019-10-14  9:08           ` Markus Elfring
2019-10-14 10:24             ` Julia Lawall
2019-10-14 11:00               ` Markus Elfring
2019-10-15  8:20               ` Markus Elfring
2019-10-16 14:16               ` [Cocci] Adjustments with SmPL after macros? Markus Elfring
2019-10-16 15:23                 ` Julia Lawall
2019-10-16 16:48                   ` Markus Elfring
     [not found]                     ` <alpine.DEB.2.21.1910161850250.3539@hadrien>
2019-10-16 16:55                       ` Markus Elfring
2019-10-17  9:33                   ` Markus Elfring
     [not found]   ` <fb924870-4bc2-1ca0-4fa4-0ddbf7ebb9d2@suse.com>
2019-10-14 11:14     ` [Cocci] macro parameters and expressions? Julia Lawall
2019-10-14 11:50       ` Markus Elfring
2019-10-14 13:33       ` Jürgen Groß [this message]
2019-10-14 14:03         ` Julia Lawall
2019-10-13 13:28 ` Markus Elfring
2019-10-13 13:36   ` Julia Lawall
2019-10-13 14:22     ` [Cocci] Checking the influence of “virtual” SmPL variables Markus Elfring
2019-10-13 16:09       ` Julia Lawall
2019-10-13 16:39         ` Markus Elfring
2019-10-14 15:33 ` [Cocci] macro parameters and expressions? Markus Elfring
     [not found]   ` <alpine.DEB.2.21.1910141740580.2330@hadrien>
2019-10-14 16:05     ` Markus Elfring
2019-10-15  6:44     ` Markus Elfring
2019-10-15  6:54       ` Markus Elfring
2019-10-15  7:40       ` Markus Elfring
2019-10-15  8:05       ` Julia Lawall
2019-10-15  8:34         ` Markus Elfring
2019-10-15  9:07         ` Markus Elfring
2019-10-15  9:28           ` 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=c03fd3e3-685b-1305-54c0-e3063201cd31@suse.com \
    --to=jgross@suse.com \
    --cc=cocci@systeme.lip6.fr \
    --cc=julia.lawall@lip6.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 NNTP newsgroup(s).