kconfig: introduce KCONFIG_* symbols for .c files
Quoted 31 lines · Jeremy Fitzhardinge
Quoted 10 lines
Would
#define KCONFIG(x) (CONFIG_##x - 0)
if (KCONFIG(PREEMPT)) {
...
}work?
$ cat test.c #define KCONFIG(x) (CONFIG_##x - 0)int
main()
{
if (KCONFIG(PREEMPT))
;return 0;
}$ gcc -O2 -Wall test.c test.c: In function âmainâ: test.c:5: error: âCONFIG_PREEMPTâ undeclared (first use in this function) test.c:5: error: (Each undeclared identifier is reported only once test.c:5: error: for each function it appears in.) $ gcc --version gcc (Debian 4.3.0-5) 4.3.1 20080523 (prerelease)
Quoted 4 lines
Quoted 1 line
J
cu
Adrian
How about this?
#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.
Steven
2008/5/25 Steven Fuerst <[email protected]>:
Quoted 66 lines
Quoted 31 lines · Jeremy Fitzhardinge
Quoted 10 lines
Would
#define KCONFIG(x) (CONFIG_##x - 0)
if (KCONFIG(PREEMPT)) {
...
}work?
$ cat test.c #define KCONFIG(x) (CONFIG_##x - 0)int
main()
{
if (KCONFIG(PREEMPT))
;return 0;
}$ gcc -O2 -Wall test.c test.c: In function âmainâ: test.c:5: error: âCONFIG_PREEMPTâ undeclared (first use in this function) test.c:5: error: (Each undeclared identifier is reported only once test.c:5: error: for each function it appears in.) $ gcc --version gcc (Debian 4.3.0-5) 4.3.1 20080523 (prerelease)Quoted 4 lines
Quoted 1 line
J
cu
AdrianHow about this?
#include <
stdio.h>#define
CONFIG_FOO1
/* #defineCONFIG_BAR1 */#define STR2(x) #x
#define STR(x) STR2(x)#define
PASTE2(x, y)x##y
#definePASTE(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; }
Quoted 5 lines
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 toKCONFIG()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]. ;-)
Quoted 1 line
Steven
Signature · 3 lines
Tom Spink [1] http://www.ioccc.org/
<snip>
Quoted 29 lines
Quoted 29 lines
#include <
stdio.h>#define
CONFIG_FOO1
/* #defineCONFIG_BAR1 */#define STR2(x) #x
#define STR(x) STR2(x)#define
PASTE2(x, y)x##y
#definePASTE(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; }
Quoted 7 lines
Quoted 5 lines
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 toKCONFIG()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