mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Steven Fuerst <sfuerst@stanford.edu>
To: linux-kernel@vger.kernel.org
Cc: "Tom Spink" <tspink@gmail.com>
Subject: Re: [RFC PATCH] kconfig: introduce KCONFIG_* symbols for .c files
Date: Sat, 24 May 2008 17:20:51 -0700	[thread overview]
Message-ID: <200805241720.51669.sfuerst@stanford.edu> (raw)
In-Reply-To: <7b9198260805241700o2bc38848m60d5853b63e6ff26@mail.gmail.com>

<snip>
> >
> > #include <stdio.h>
> >
> > #define CONFIG_FOO      1
> > /* #define CONFIG_BAR   1 */
> >
> > #define STR2(x) #x
> > #define STR(x) STR2(x)
> >
> > #define PASTE2(x, y) x##y
> > #define PASTE(x, y) PASTE2(x, y)
> >
> > #define KCONFIG2(x, y) STR(PASTE(CONFIG_, x))[y]
> > #define KCONFIG(x) (!((KCONFIG2(x, 0) == 'C') && \
> >                                        (KCONFIG2(x, 1) == 'O') && \
> >                                        (KCONFIG2(x, 2) == 'N') && \
> >                                        (KCONFIG2(x, 3) == 'F') && \
> >                                        (KCONFIG2(x, 4) == 'I') && \
> >                                        (KCONFIG2(x, 5) == 'G') && \
> >                                        (KCONFIG2(x, 6) == '_')))
> >
> > int main()
> > {
> >        if (KCONFIG(FOO)) printf("FOO\n");
> >        if (KCONFIG(BAR)) printf("BAR\n");
> >        return 0;
> > }
> >
> >
> > It of course assumes that the result of all defined macros you want to
> > test for do not start with "CONFIG_", so this doesn't work generally. 
> > However, nearly all of the time the arguement to KCONFIG() will be
> > defined to be "1" an integer, or some other known string that doesn't
> > begin with "CONFIG_", so this trick is fairly robust.
>
> I've seen worse abuse of the preprocessor[1]. ;-)

True...  Here's a "nicer" version, that even gets empty defines right.  I've 
checked that gcc is smart enough to compile away the constant string 
comparison.  The only thing wrong with this now is that it'll fail to get
#define CONFIG_BLAH	CONFIG_BLAH
correct, but that's a rather silly corner case.

#include <stdio.h>

#define CONFIG_FOO	1
/* #define CONFIG_BAR	1 */
#define CONFIG_BAZ

#define STR2(x) #x
#define STR(x) STR2(x)

#define PASTE(x, y) x##y

#define KCONFIG(x) \
	(__builtin_strcmp("Z" STR(PASTE(CONFIG_, x)), "ZCONFIG_" #x))

int main()
{
	if (KCONFIG(FOO)) printf("FOO\n");
	if (KCONFIG(BAR)) printf("BAR\n");
	if (KCONFIG(BAZ)) printf("BAZ\n");
	
	return 0;
}

Steven


  reply	other threads:[~2008-05-25  0:21 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-24 23:36 Steven Fuerst
2008-05-25  0:00 ` Tom Spink
2008-05-25  0:20   ` Steven Fuerst [this message]
  -- strict thread matches above, loose matches on Subject: below --
2008-05-24 19:25 Sam Ravnborg
2008-05-24 19:53 ` Andrew Morton
2008-05-24 20:14   ` Jeremy Fitzhardinge
2008-05-24 20:46     ` Sam Ravnborg
2008-05-24 20:56       ` Jeremy Fitzhardinge
2008-05-24 21:03         ` Sam Ravnborg
2008-05-24 20:24   ` Sam Ravnborg
2008-05-24 20:48     ` Andrew Morton
2008-05-24 21:00       ` Sam Ravnborg
2008-05-24 20:05 ` Adrian Bunk
2008-05-24 20:44   ` Sam Ravnborg
2008-05-24 20:57     ` Adrian Bunk
2008-05-24 20:20 ` Linus Torvalds
2008-05-24 20:48 ` Jeremy Fitzhardinge
2008-05-24 20:58   ` Jeremy Fitzhardinge
2008-05-24 21:03     ` Adrian Bunk
2008-05-24 21:13       ` Jeremy Fitzhardinge
2008-05-24 21:26 ` Pavel Machek

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=200805241720.51669.sfuerst@stanford.edu \
    --to=sfuerst@stanford.edu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tspink@gmail.com \
    /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

all inboxes | Powered by JetHome®