Linux SPARSE checker discussions
 help / color / mirror / Atom feed
From: Josh Poimboeuf <jpoimboe@redhat.com>
To: linux-sparse@vger.kernel.org
Subject: Sparse preprocessing bug with zero-arg variadic macros
Date: Thu, 31 Aug 2017 08:34:00 -0500	[thread overview]
Message-ID: <20170831133400.zjeaxhf25rbdftic@treble> (raw)

Hi,

I think I'm seeing a bug in the sparse preprocessor.  I've reduced it to
the following test case.

----------------------
/* If the macro has arguments, prepend them with a comma. */
#define ARGS_APPEND(...) , ## __VA_ARGS__

#define __ARG17(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12,	\
		_13, _14, _15, _16, _17, ...) _17

/* Return 1 if the macro has arguments, 0 otherwise. */
#define HAS_ARGS(...) __ARG17(0, ## __VA_ARGS__, 1, 1, 1, 1, 1, 1, 1,	\
			      1, 1, 1, 1, 1, 1, 1, 1, 1, 0)

/* Macros for passing inline asm constraints as macro arguments */
#define ARGS(args...) args
#define OUTPUTS ARGS
#define INPUTS ARGS
#define CLOBBERS ARGS

#define __CLOBBERS_APPEND_0(...)
#define __CLOBBERS_APPEND_1(...)  , __VA_ARGS__

#define __CLOBBERS_APPEND(has_args, ...) \
	__CLOBBERS_APPEND_ ## has_args (__VA_ARGS__)

#define _CLOBBERS_APPEND(has_args, ...) \
	__CLOBBERS_APPEND(has_args, __VA_ARGS__)

/* If the macro has arguments, prepend them with a colon. */
#define CLOBBERS_APPEND(...) \
	_CLOBBERS_APPEND(HAS_ARGS(__VA_ARGS__), __VA_ARGS__)


void foo()
{
	ARGS_APPEND()
	CLOBBERS_APPEND()
}
----------------------

  $ gcc -c -o test.o test.c -E; cat test.o
  # 1 "test.c"
  # 1 "<built-in>"
  # 1 "<command-line>"
  # 31 "<command-line>"
  # 1 "/usr/include/stdc-predef.h" 1 3 4
  # 32 "<command-line>" 2
  # 1 "test.c"
  # 31 "test.c"
  void foo()
  {
  
  
  }

  $ sparse -c -o test.o test.c -E
  
  void foo()
  {
  			,
  			,
  }


Notice that sparse incorrectly inserts a comma for both macros when they
have zero arguments.

-- 
Josh

             reply	other threads:[~2017-08-31 13:34 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-31 13:34 Josh Poimboeuf [this message]
2017-08-31 17:19 ` Sparse preprocessing bug with zero-arg variadic macros Linus Torvalds
2017-08-31 20:54   ` Al Viro
2017-08-31 21:09     ` Al Viro
2017-08-31 21:34       ` Josh Triplett
2017-08-31 21:48         ` Al Viro
2017-09-01  0:06           ` Christopher Li

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=20170831133400.zjeaxhf25rbdftic@treble \
    --to=jpoimboe@redhat.com \
    --cc=linux-sparse@vger.kernel.org \
    /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