chibicc/test/macro.c

330 lines
3.9 KiB
C
Raw Normal View History

#include "test.h"
2020-09-03 13:24:23 +03:00
#include "include1.h"
2020-03-30 03:30:06 +03:00
#
/* */ #
2020-08-18 04:45:03 +03:00
int ret3(void) { return 3; }
int dbl(int x) { return x*x; }
2020-08-18 04:45:03 +03:00
2020-03-30 03:30:06 +03:00
int main() {
ASSERT(5, include1);
ASSERT(7, include2);
2020-09-03 13:24:23 +03:00
2020-08-20 13:36:36 +03:00
#if 0
#include "/no/such/file"
ASSERT(0, 1);
#if nested
#endif
2020-08-20 13:36:36 +03:00
#endif
int m = 0;
#if 1
m = 5;
#endif
ASSERT(5, m);
2020-08-20 13:36:36 +03:00
2020-03-30 03:57:07 +03:00
#if 1
# if 0
# if 1
foo bar
# endif
# endif
m = 3;
#endif
ASSERT(3, m);
2020-03-30 03:57:07 +03:00
#if 1-1
# if 1
# endif
# if 1
# else
# endif
# if 0
# else
# endif
m = 2;
#else
# if 1
m = 3;
# endif
#endif
ASSERT(3, m);
2020-03-30 03:57:07 +03:00
#if 1
m = 2;
#else
m = 3;
#endif
ASSERT(2, m);
2020-03-30 03:57:07 +03:00
2020-03-29 02:51:06 +03:00
#if 1
m = 2;
#else
m = 3;
#endif
ASSERT(2, m);
2020-03-29 02:51:06 +03:00
#if 0
m = 1;
#elif 0
m = 2;
#elif 3+5
m = 3;
#elif 1*5
m = 4;
#endif
ASSERT(3, m);
2020-03-29 02:51:06 +03:00
#if 1+5
m = 1;
#elif 1
m = 2;
#elif 3
m = 2;
#endif
ASSERT(1, m);
2020-03-29 02:51:06 +03:00
#if 0
m = 1;
#elif 1
# if 1
m = 2;
# else
m = 3;
# endif
#else
m = 5;
#endif
ASSERT(2, m);
2020-03-29 02:51:06 +03:00
2020-03-29 07:29:48 +03:00
int M1 = 5;
#define M1 3
ASSERT(3, M1);
2020-03-29 07:29:48 +03:00
#define M1 4
ASSERT(4, M1);
2020-03-29 07:29:48 +03:00
#define M1 3+4+
ASSERT(12, M1 5);
2020-03-29 07:29:48 +03:00
#define M1 3+4
ASSERT(23, M1*5);
2020-03-29 07:29:48 +03:00
#define ASSERT_ assert(
#define if 5
#define five "5"
#define END )
ASSERT_ 5, if, five END;
2020-03-29 12:23:33 +03:00
#undef ASSERT_
#undef if
#undef five
#undef END
if (0);
#define M 5
#if M
m = 5;
#else
m = 6;
#endif
ASSERT(5, m);
#define M 5
#if M-5
m = 6;
#elif M
m = 5;
#endif
ASSERT(5, m);
int M2 = 6;
#define M2 M2 + 3
ASSERT(9, M2);
#define M3 M2 + 3
ASSERT(12, M3);
int M4 = 3;
#define M4 M5 * 5
#define M5 M4 + 2
ASSERT(13, M4);
2020-03-29 11:18:31 +03:00
#ifdef M6
m = 5;
#else
m = 3;
#endif
ASSERT(3, m);
2020-03-29 11:18:31 +03:00
#define M6
#ifdef M6
m = 5;
#else
m = 3;
#endif
ASSERT(5, m);
2020-03-29 11:18:31 +03:00
#ifndef M7
m = 3;
#else
m = 5;
#endif
ASSERT(3, m);
2020-03-29 11:18:31 +03:00
#define M7
#ifndef M7
m = 3;
#else
m = 5;
#endif
ASSERT(5, m);
2020-03-29 11:18:31 +03:00
#if 0
#ifdef NO_SUCH_MACRO
#endif
#ifndef NO_SUCH_MACRO
#endif
#else
#endif
2020-08-18 04:45:03 +03:00
#define M7() 1
int M7 = 5;
ASSERT(1, M7());
ASSERT(5, M7);
2020-08-18 04:45:03 +03:00
#define M7 ()
ASSERT(3, ret3 M7);
2020-08-18 04:45:03 +03:00
2020-03-30 04:37:44 +03:00
#define M8(x,y) x+y
ASSERT(7, M8(3, 4));
2020-03-30 04:37:44 +03:00
#define M8(x,y) x*y
ASSERT(24, M8(3+4, 4+5));
2020-03-30 04:37:44 +03:00
#define M8(x,y) (x)*(y)
ASSERT(63, M8(3+4, 4+5));
2020-03-30 04:37:44 +03:00
2020-03-30 04:39:35 +03:00
#define M8(x,y) x y
ASSERT(9, M8(, 4+5));
2020-03-30 04:39:35 +03:00
#define M8(x,y) x*y
ASSERT(20, M8((2+3), 4));
#define M8(x,y) x*y
ASSERT(12, M8((2,3), 4));
#define dbl(x) M10(x) * x
#define M10(x) dbl(x) + 3
ASSERT(10, dbl(2));
2020-08-29 19:04:23 +03:00
#define M11(x) #x
ASSERT('a', M11( a!b `""c)[0]);
ASSERT('!', M11( a!b `""c)[1]);
ASSERT('b', M11( a!b `""c)[2]);
ASSERT(' ', M11( a!b `""c)[3]);
ASSERT('`', M11( a!b `""c)[4]);
ASSERT('"', M11( a!b `""c)[5]);
ASSERT('"', M11( a!b `""c)[6]);
ASSERT('c', M11( a!b `""c)[7]);
ASSERT(0, M11( a!b `""c)[8]);
2020-08-29 19:04:23 +03:00
2020-08-29 19:05:28 +03:00
#define paste(x,y) x##y
ASSERT(15, paste(1,5));
ASSERT(255, paste(0,xff));
ASSERT(3, ({ int foobar=3; paste(foo,bar); }));
ASSERT(5, paste(5,));
ASSERT(5, paste(,5));
2020-08-29 19:05:28 +03:00
#define i 5
ASSERT(101, ({ int i3=100; paste(1+i,3); }));
2020-08-29 19:05:28 +03:00
#undef i
#define paste2(x) x##5
ASSERT(26, paste2(1+2));
2020-08-29 19:05:28 +03:00
#define paste3(x) 2##x
ASSERT(23, paste3(1+2));
2020-08-29 19:05:28 +03:00
#define paste4(x, y, z) x##y##z
ASSERT(123, paste4(1,2,3));
2020-08-29 19:05:28 +03:00
2020-08-31 10:50:58 +03:00
#define M12
#if defined(M12)
m = 3;
#else
m = 4;
#endif
ASSERT(3, m);
#define M12
#if defined M12
m = 3;
#else
m = 4;
#endif
ASSERT(3, m);
#if defined(M12) - 1
m = 3;
#else
m = 4;
#endif
ASSERT(4, m);
#if defined(NO_SUCH_MACRO)
m = 3;
#else
m = 4;
#endif
ASSERT(4, m);
#if no_such_symbol == 0
m = 5;
#else
m = 6;
#endif
ASSERT(5, m);
#define STR(x) #x
#define M12(x) STR(x)
#define M13(x) M12(foo.x)
ASSERT(0, strcmp(M13(bar), "foo.bar"));
#define M13(x) M12(foo. x)
ASSERT(0, strcmp(M13(bar), "foo. bar"));
#define M12 foo
#define M13(x) STR(x)
#define M14(x) M13(x.M12)
ASSERT(0, strcmp(M14(bar), "bar.foo"));
#define M14(x) M13(x. M12)
ASSERT(0, strcmp(M14(bar), "bar. foo"));
2020-08-30 12:57:20 +03:00
#include "include3.h"
ASSERT(3, foo);
#include "include4.h"
ASSERT(4, foo);
#define M13 "include3.h"
#include M13
ASSERT(3, foo);
#define M13 < include4.h
#include M13 >
ASSERT(4, foo);
#undef foo
2020-04-01 17:54:33 +03:00
ASSERT(1, __STDC__);
2020-03-30 03:30:06 +03:00
printf("OK\n");
return 0;
}