view tests/mpp/misc.c @ 15:f5acaf0c8a29

Don't cast through "volatile int". Causes a gcc warning nowadays. XXX: should put something else back here to frighten the optimizer
author David A. Holland
date Tue, 31 May 2022 01:00:55 -0400
parents 13d2b8934445
children
line wrap: on
line source

////////////////////////////////////////////////////////////
... line splicing

he\
llo

/\
/ comment

//\
comment

#define SPLIT splat
SPL\
IT

int c = '\0\
12';
const char *x = "blah \0\
12";

////////////////////////////////////////////////////////////
... digraphs

#define DI_STR(x) %:x
#define DI_CAT(x,y) x %:%: y

DI_STR(hi)
DI_CAT(fu,bar)

DI_STR(<:)
DI_STR(:>)
DI_STR(<%)
DI_STR(%>)
DI_STR(%:)

#define HASH %:
#define HASHHASH %:%:
HASH
HASHHASH

%:define FOO 123
#ifdef FOO
right
#else
wrong
#endif
%:undef FOO
#ifdef FOO
wrong
#else
right
#endif

////////////////////////////////////////////////////////////
... comments

a /**// b;

a = b//*q*/ blah
- c;

#define CAT(x,y) x ## y
#define CAT2(x,y) CAT(x,y)

CAT(/,*) not comment CAT(*,/)
CAT(/,/) not comment

<a/*comment*/b>

/* comment /* comment */
not comment

#define CM stuff // comment
CM staff
(should be stuff staff on one line)

#define CMM stuff /* comment */
CMM staff
(should be stuff staff on one line)

////////////////////////////////////////////////////////////
... conditionals

#if 0
wrong
#elif 1
right
#else
wrong
#endif

#if NOTDEF
wrong
#else
right
#endif

#if NOTDEF
wrong
#elif NOTDEF
wrong
#elif NOTDEF2
wrong
#elif 1
right
#endif

#if 0
#if 1
wrong
#else
wrong
#endif
#if 0
wrong
#else
wrong
#endif
#elif 1
right
#if 1
right
#else
wrong
#endif
#if 0
wrong
#else
right
#endif
#elif 1
wrong
#else
wrong
#endif

#if /*blah*/ 1 /*boo*/
right
#else /* bar */
wrong
#endif /* baz */

#ifdef NOTDEF
wrong
#else
right
#endif

#ifndef NOTDEF
right
#else
wrong
#endif

#define TWO 2
#if TWO > 1
right
#else
wrong
#endif

#if defined(TWO)
right
#else
wrong
#endif

#if defined TWO 
right
#else
wrong
#endif

#if defined(THREE)
wrong
#else
right
#endif

#if defined THREE 
wrong
#else
right
#endif

#if defined(defined)
wrong
#else
right
#endif

defined(TWO) should have a 2 in it
defined(THREE) should not be changed

#define SUCC(x) x+1

#if SUCC(1) == 2
right
#else
wrong
#endif

#if TWO == 2
right
#else
wrong
#endif

#if TWO + NOTDEF == 2
right
#else
wrong
#endif

#if SUCC(TWO + NOTDEF * 12574) == 3
right
#else
wrong
#endif

#if SUCC(TWO + '0') == '3'
right
#else
wrong
#endif

#if 'A'
right
#else
wrong
#endif

#if '\0'
wrong
#else
right
#endif

#if '\007' == 7
right
#else
wrong
#endif

#if defined TWO + 3 == 4
right
#else
wrong
#endif

#if defined NOTDEF * 5
wrong
#else
right
#endif

#undef TWO
#if defined(TWO)
wrong
#else
right
#endif

#if 1
#elif NOTDEF(1)
#endif

#if 0
#if NOTDEF(1)
#endif
#endif

////////////////////////////////////////////////////////////
... definitions

#define AA aa
#define BB() bb
#define CC(x) x cc
/* put these back in when/if varargs macros are supported */
/*#define DD(...) dd*/
/*#define EE(x, ...) x ee*/

AA
/*BB*/
/*CC*/
DD
EE

AA()
BB()
/*CC()*/
DD()
DD(,)
EE(,)
DD(,,)
EE(,,)

#undef AA
#undef BB
#undef CC
#undef DD
#undef EE

/*
#define FF(x, ...) __VA_ARGS__ x
FF(ff, )
FF(ff, a)
FF(ff, a, b)
FF(ff, a, b, c)
FF(ff, a, b, c, d)
*/

#undef FF

#define define fedine
#define undef dunef
#define ifndef difnef
#define GG gg
GG should be gg
#ifdef GG
right
#else
wrong
#endif
define should be fedine
undef should be dunef
ifndef should be difnef

#ifdef define
right
#else
wrong
#endif

#ifdef undef
right
#else
wrong
#endif

#ifdef ifndef
right
#else
wrong
#endif

#ifndef ifndef
wrong
#else
right
#endif

#undef define
#undef undef
#undef ifndef

#ifdef define
wrong
#else
right
#endif

#ifdef undef
wrong
#else
right
#endif

#ifdef ifndef
wrong
#else
right
#endif

#define HH(a,b,c) a b c
HH((1,2),(3,4),(5,6))
/*
HH(1,
2,
3
)
HH(4,
     5,
       6)
*/
#undef HH

#define SUM(a,b) a + b
#define CALL(x) x(1,2)
CALL(SUM) should be 1+2

#if CALL(SUM) == 3
right
#else
wrong
#endif

#define ADD SUM
ADD(1,ADD(2,3))  should be 1+2+3

/*
#define RECURSE RECURSE
RECURSE
*/

/*
#define CROSSRECURSE1(x) x CROSSRECURSE2(1)
#define CROSSRECURSE2(x) x CROSSRECURSE1(2)
CROSSRECURSE1(0)
CROSSRECURSE2(0)
*/

#define TWO two
#define ADDTWO(x) TWO + x
#undef TWO
ADDTWO(1) should be two + 1

... legal redefinitions
#define ZERO (1-1)
#define ZERO /* foo */ (1-1) /* bar */
#define IDENTITY(x)  ( x )
#define IDENTITY(x)  ( /* foo */ x )


////////////////////////////////////////////////////////////
... stringize

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

#define STRY(x, y) #y
/* these should be using STRY not STR, but that doesn't work */
#if 0
STRY(, a b c)  should be "a b c"
STRY(,   a   b   c)  should be "a b c"
STRY(, "stry\n") should be "\"stry\\n\""
STRY(, )  should be ""
STRY(,)  should be ""
STRY(, /*woodle*/ b)  should be "b"
STRY(, a/*noodle*/b)  should be "a b"
STRY(, a /*boodle*/ b) should be "a b"
#endif
STR( a b c)  should be "a b c"
STR(   a   b   c)  should be "a b c"
STR( "stry\n") should be "\"stry\\n\""
/*
STR( )  should be ""
STR()  should be ""
*/
STR( /*woodle*/ b)  should be "b"
STR( a/*noodle*/b)  should be "a b"
STR( a /*boodle*/ b) should be "a b"

#define GRACKLE(x) STR(x(1,2))
GRACKLE(SUM)  should give "SUM(1,2)", not "1+2"
STR2(SUM)(3,4) should give "SUM"(3,4), not "3+4"

#if 0 /* broken */
STR(/*comment*/) should be ""
#endif

////////////////////////////////////////////////////////////
... concat

#define HASHES # ## #
#define YY(x,y) STR(x H y)
#define ZZ(x,y) STR2(x H y)

YY(1,2)  should be "1 H 2"
ZZ(1,2)  should be "1 ## 2"

... pasting of punctuation is not allowed
CAT(+,+)
CAT(-,-)
CAT(<<,=)
CAT(>>,=)
CAT(<,<=)
CAT(>,>=)
CAT(<,>=)
CAT(<,CAT(<,=))
CAT(>,CAT(>,=))

CAT2(+,+)
CAT2(-,-)
CAT2(<<,=)
CAT2(>>,=)
CAT2(<,<=)
CAT2(>,>=)
CAT2(<,>=)
CAT2(<,CAT2(<,=))
CAT2(>,CAT2(>,=))

#define ONEHUNDRED 100
CAT(ONE, HUNDRED) should be 100

#define BASTET "bast"
#define ET ET "et"

CAT(BAST,ET)  ---> "bast"
/*
CAT2(BAST,ET)  ---> "bast" "et"
*/

#undef BASTET
#undef ET

#define KLUDGE KLU
CAT2(KLUDGE, DGE) should be "KLU"
#undef KLUDGE
#define KLUDGE HACK KLUDGE
/*
CAT2(KLUDGE, ) should be "HACK KLUDGE" not "HACK HACK KLUDGE"
*/

#define CATE(x) x ## // comment
CATE(10)
#if 0
#define ECAT(x) /* comment */ ## x
ECAT(20)
#endif

/*
#define CATVA(x, ...) x ## __VA_ARGS__
CATVA(z,y,z)  should be zy,x
*/

CAT(IDENTITY,IDENTITY(z))  should be IDENTITYIDENTITY(z)
CAT2(IDENTITY,IDENTITY(z))  should be IDENTITYz

CAT(1, /*comment*/ 2)  should be 12

////////////////////////////////////////////////////////////
... directive recognition

#define EMPTY

/*
EMPTY #error wrong
EMPTY #define SHOULDNT bad
#ifdef SHOULDNT
wrong
#else
wrong
#endif
*/

#   define SHOULD1 good
#ifdef SHOULD1
right
#else
wrong
#endif

   #define SHOULD2 good
#ifdef SHOULD2
right
#else
wrong
#endif

   #   define SHOULD3 good
#ifdef SHOULD3
right
#else
wrong
#endif

/*comment*/ #define SHOULD4 good
#ifdef SHOULD4
right
#else
wrong
#endif

/*
#define DEFINE #define FOO foo
DEFINE
#ifdef FOO
wrong
#else
right
#endif
*/

/*
#define INCLUDE #include "nosuchfile.h"
INCLUDE
*/

/*
#define ERROR #error wrong
ERROR
*/

////////////////////////////////////////////////////////////
... keywords

#define register 1
#define int +
#define e 3
#if register int e == 4
right
#else
wrong
#endif

#undef register
#undef int
#undef e

////////////////////////////////////////////////////////////
... builtin macros

__STDC__   should be 1
__STDC_VERSION__  should be a date