@@ -2115,6 +2115,14 @@ static void hashhash10()
21152115 ASSERT_EQUALS (" # #" , preprocess (code));
21162116}
21172117
2118+ static void hashFromMacroAtLineStart ()
2119+ {
2120+ // clang testsuite hash_space.c: a space before the # so that the output is not read as a directive
2121+ const char code[] = " #define HASH #\n "
2122+ " HASH define foo bar\n " ;
2123+ TODO_ASSERT_EQUALS (" \n # define foo bar" , " #\n define foo bar" , preprocess (code));
2124+ }
2125+
21182126static void hashhash11 ()
21192127{
21202128 const char code[] = " #define x # # #\n "
@@ -4788,6 +4796,269 @@ static void tokenMacro15()
47884796 ASSERT_EQUALS (" D 1:1 use 1:3 unknown\n " , toString (macroUsage));
47894797}
47904798
4799+ // Rescanning with disabling contexts. The results are the same as gcc gives.
4800+ static void tokenStreamExpansion_recursive ()
4801+ {
4802+ const char code[] = " #define f f\n "
4803+ " f\n " ;
4804+ ASSERT_EQUALS (" \n "
4805+ " f" , preprocess (code));
4806+ }
4807+
4808+ // A macro is only disabled while its replacement is rescanned (pfultz2's recursion idiom)
4809+ static void tokenStreamExpansion_defer ()
4810+ {
4811+ const char code[] = " #define EMPTY()\n "
4812+ " #define DEFER(id) id EMPTY()\n "
4813+ " #define EXPAND(...) __VA_ARGS__\n "
4814+ " #define R_I() R\n "
4815+ " #define R(n) n DEFER(R_I)()(x)\n "
4816+ " EXPAND(R(1))\n " ;
4817+ TODO_ASSERT_EQUALS (" \n\n\n\n\n 1 x R_I ( ) ( x )" , " \n\n\n\n\n 1 R ( x )" , preprocess (code));
4818+ }
4819+
4820+ static void tokenStreamExpansion_mutual ()
4821+ {
4822+ const char code[] = " #define a b\n "
4823+ " #define b a\n "
4824+ " a b\n " ;
4825+ ASSERT_EQUALS (" \n "
4826+ " \n "
4827+ " a b" , preprocess (code));
4828+ }
4829+
4830+ static void tokenStreamExpansion_std_example ()
4831+ {
4832+ const char code[] = " #define f(a) a*g\n "
4833+ " #define g(a) f(a)\n "
4834+ " f(2)(9)\n " ;
4835+ ASSERT_EQUALS (" \n "
4836+ " \n "
4837+ " 2 * 9 * g" , preprocess (code));
4838+ }
4839+
4840+ static void tokenStreamExpansion_trailing ()
4841+ {
4842+ const char code[] = " #define f g\n "
4843+ " #define g(x) x+1\n "
4844+ " f(2)\n " ;
4845+ ASSERT_EQUALS (" \n "
4846+ " \n "
4847+ " 2 + 1" , preprocess (code));
4848+ }
4849+
4850+ static void tokenStreamExpansion_trailing_args ()
4851+ {
4852+ const char code[] = " #define g(x) x+1\n "
4853+ " #define f g(\n "
4854+ " f 2)\n " ;
4855+ ASSERT_EQUALS (" \n "
4856+ " \n "
4857+ " 2 + 1" , preprocess (code));
4858+ }
4859+
4860+ static void tokenStreamExpansion_args_lines ()
4861+ {
4862+ const char code[] = " #define ADD(X,Y) X+Y\n "
4863+ " ADD(1,\n "
4864+ " 2)\n "
4865+ " z\n " ;
4866+ ASSERT_EQUALS (" \n "
4867+ " 1 + 2\n "
4868+ " \n "
4869+ " z" , preprocess (code));
4870+ }
4871+
4872+ static void tokenStreamExpansion_nested ()
4873+ {
4874+ const char code[] = " #define A(x) [x]\n "
4875+ " #define B(x) A(x) A(A(x))\n "
4876+ " B(1)\n " ;
4877+ ASSERT_EQUALS (" \n "
4878+ " \n "
4879+ " [ 1 ] [ [ 1 ] ]" , preprocess (code));
4880+ }
4881+
4882+ static void tokenStreamExpansion_arg_self ()
4883+ {
4884+ const char code[] = " #define f(x) x\n "
4885+ " f(f)(1)\n " ;
4886+ TODO_ASSERT_EQUALS (" \n "
4887+ " f ( 1 )" ,
4888+ " \n "
4889+ " 1" ,
4890+ preprocess (code));
4891+ }
4892+
4893+ static void tokenStreamExpansion_blue_in_arg ()
4894+ {
4895+ const char code[] = " #define f(x) g(x)\n "
4896+ " #define g(x) f(x)\n "
4897+ " f(f(1))\n " ;
4898+ ASSERT_EQUALS (" \n "
4899+ " \n "
4900+ " f ( f ( 1 ) )" , preprocess (code));
4901+ }
4902+
4903+ static void tokenStreamExpansion_paren_in_arg ()
4904+ {
4905+ const char code[] = " #define f(x,y) (x)(y)\n "
4906+ " f((1,2),3)\n " ;
4907+ ASSERT_EQUALS (" \n "
4908+ " ( ( 1 , 2 ) ) ( 3 )" , preprocess (code));
4909+ }
4910+
4911+ static void tokenStreamExpansion_not_call ()
4912+ {
4913+ const char code[] = " #define f(x) x\n "
4914+ " f ; f\n "
4915+ " (1)\n " ;
4916+ ASSERT_EQUALS (" \n "
4917+ " f ; 1" , preprocess (code));
4918+ }
4919+
4920+ static void tokenStreamExpansion_obj_to_fn ()
4921+ {
4922+ const char code[] = " #define LPAREN (\n "
4923+ " #define f(x) [x]\n "
4924+ " f LPAREN 1)\n " ;
4925+ ASSERT_EQUALS (" \n "
4926+ " \n "
4927+ " f ( 1 )" , preprocess (code));
4928+ }
4929+
4930+ static void tokenStreamExpansion_va_args ()
4931+ {
4932+ const char code[] = " #define F(...) f(__VA_ARGS__)\n "
4933+ " F(1,2) F()\n " ;
4934+ ASSERT_EQUALS (" \n "
4935+ " f ( 1 , 2 ) f ( )" , preprocess (code));
4936+ }
4937+
4938+ static void tokenStreamExpansion_counter ()
4939+ {
4940+ const char code[] = " #define C __COUNTER__\n "
4941+ " C C __COUNTER__\n " ;
4942+ ASSERT_EQUALS (" \n "
4943+ " 0 1 2" , preprocess (code));
4944+ }
4945+
4946+ static void tokenStreamExpansion_line ()
4947+ {
4948+ const char code[] = " #define L __LINE__\n "
4949+ " L\n " ;
4950+ ASSERT_EQUALS (" \n "
4951+ " 2" , preprocess (code));
4952+ }
4953+
4954+ static void tokenStreamExpansion_deep ()
4955+ {
4956+ const char code[] = " #define a(x) b(x)\n "
4957+ " #define b(x) c(x)\n "
4958+ " #define c(x) x x\n "
4959+ " a(a(1))\n " ;
4960+ ASSERT_EQUALS (" \n "
4961+ " \n "
4962+ " \n "
4963+ " 1 1 1 1" , preprocess (code));
4964+ }
4965+
4966+ static void tokenStreamExpansion_q_example ()
4967+ {
4968+ const char code[] = " #define q(x) x\n "
4969+ " #define r(x,y) x y\n "
4970+ " q(r)(q,1)\n " ;
4971+ ASSERT_EQUALS (" \n "
4972+ " \n "
4973+ " q 1" , preprocess (code));
4974+ }
4975+
4976+ static void tokenStreamExpansion_empty ()
4977+ {
4978+ const char code[] = " #define E\n "
4979+ " #define f(x) <x>\n "
4980+ " f(E) f()\n " ;
4981+ ASSERT_EQUALS (" \n "
4982+ " \n "
4983+ " < > < >" , preprocess (code));
4984+ }
4985+
4986+ // __VA_OPT__ (C23 6.10.5.1). The results are the same as gcc gives.
4987+ static void tokenStreamExpansion_va_opt_empty_after_expansion ()
4988+ {
4989+ const char code[] = " #define F(...) <__VA_OPT__(x)>\n "
4990+ " #define E\n "
4991+ " F() F(E) F(1)\n " ;
4992+ TODO_ASSERT_EQUALS (" \n "
4993+ " \n "
4994+ " < > < > < x >" ,
4995+ " \n "
4996+ " \n "
4997+ " < > < x > < x >" ,
4998+ preprocess (code));
4999+ }
5000+
5001+ static void tokenStreamExpansion_va_opt_hash ()
5002+ {
5003+ const char code[] = " #define X 1\n "
5004+ " #define F(a,...) #__VA_OPT__(a b)\n "
5005+ " F(X,2) F(X)\n " ;
5006+ TODO_ASSERT_EQUALS (" \n "
5007+ " \n "
5008+ " \" 1 b\" \"\" " ,
5009+ " \n "
5010+ " \n "
5011+ " \" X\" b \"\" " ,
5012+ preprocess (code));
5013+ }
5014+
5015+ static void tokenStreamExpansion_va_opt_hashhash ()
5016+ {
5017+ const char code[] = " #define F(a,...) a ## __VA_OPT__(b c) ## d\n "
5018+ " F(x,1) F(x)\n " ;
5019+ TODO_ASSERT_EQUALS (" \n "
5020+ " xb cd xd" ,
5021+ " \n "
5022+ " xb cd x# \" d\" " ,
5023+ preprocess (code));
5024+ }
5025+
5026+ static void tokenStreamExpansion_va_opt_hash_in_content ()
5027+ {
5028+ const char code[] = " #define F(a,...) __VA_OPT__(a##a) __VA_OPT__(#a)\n "
5029+ " F(x,1)\n " ;
5030+ ASSERT_EQUALS (" \n "
5031+ " xx \" x\" " , preprocess (code));
5032+ }
5033+
5034+ static void tokenStreamExpansion_va_opt_rescan ()
5035+ {
5036+ const char code[] = " #define LP (\n "
5037+ " #define F(...) __VA_OPT__(LP)\n "
5038+ " F(1)\n " ;
5039+ ASSERT_EQUALS (" \n "
5040+ " \n "
5041+ " (" , preprocess (code));
5042+ }
5043+
5044+ static void tokenStreamExpansion_va_opt_comma ()
5045+ {
5046+ const char code[] = " #define F(a,...) f(a __VA_OPT__(,) __VA_ARGS__)\n "
5047+ " #define X 1\n "
5048+ " F(X) F(X,X)\n " ;
5049+ ASSERT_EQUALS (" \n "
5050+ " \n "
5051+ " f ( 1 ) f ( 1 , 1 )" , preprocess (code));
5052+ }
5053+
5054+ static void tokenStreamExpansion_va_opt_parentheses ()
5055+ {
5056+ const char code[] = " #define F(...) __VA_OPT__((__VA_ARGS__))\n "
5057+ " F(a,(b)) F()\n " ;
5058+ ASSERT_EQUALS (" \n "
5059+ " ( a , ( b ) )" , preprocess (code));
5060+ }
5061+
47915062static void undef ()
47925063{
47935064 const char code[] = " #define A\n "
@@ -5697,6 +5968,7 @@ static void runTests(int argc, char **argv, Input input)
56975968 TEST_CASE (hashhash9);
56985969 TEST_CASE (hashhash10); // #108 : #define x # #
56995970 TEST_CASE (hashhash11); // #60: #define x # # #
5971+ TEST_CASE (hashFromMacroAtLineStart);
57005972 TEST_CASE (hashhash12);
57015973 TEST_CASE (hashhash13);
57025974 TEST_CASE (hashhash_string_literal);
@@ -5864,6 +6136,32 @@ static void runTests(int argc, char **argv, Input input)
58646136 TEST_CASE (tokenMacro13);
58656137 TEST_CASE (tokenMacro14);
58666138 TEST_CASE (tokenMacro15);
6139+ TEST_CASE (tokenStreamExpansion_recursive);
6140+ TEST_CASE (tokenStreamExpansion_defer);
6141+ TEST_CASE (tokenStreamExpansion_mutual);
6142+ TEST_CASE (tokenStreamExpansion_std_example);
6143+ TEST_CASE (tokenStreamExpansion_trailing);
6144+ TEST_CASE (tokenStreamExpansion_trailing_args);
6145+ TEST_CASE (tokenStreamExpansion_args_lines);
6146+ TEST_CASE (tokenStreamExpansion_nested);
6147+ TEST_CASE (tokenStreamExpansion_arg_self);
6148+ TEST_CASE (tokenStreamExpansion_blue_in_arg);
6149+ TEST_CASE (tokenStreamExpansion_paren_in_arg);
6150+ TEST_CASE (tokenStreamExpansion_not_call);
6151+ TEST_CASE (tokenStreamExpansion_obj_to_fn);
6152+ TEST_CASE (tokenStreamExpansion_va_args);
6153+ TEST_CASE (tokenStreamExpansion_counter);
6154+ TEST_CASE (tokenStreamExpansion_line);
6155+ TEST_CASE (tokenStreamExpansion_deep);
6156+ TEST_CASE (tokenStreamExpansion_q_example);
6157+ TEST_CASE (tokenStreamExpansion_empty);
6158+ TEST_CASE (tokenStreamExpansion_va_opt_empty_after_expansion);
6159+ TEST_CASE (tokenStreamExpansion_va_opt_hash);
6160+ TEST_CASE (tokenStreamExpansion_va_opt_hashhash);
6161+ TEST_CASE (tokenStreamExpansion_va_opt_hash_in_content);
6162+ TEST_CASE (tokenStreamExpansion_va_opt_rescan);
6163+ TEST_CASE (tokenStreamExpansion_va_opt_comma);
6164+ TEST_CASE (tokenStreamExpansion_va_opt_parentheses);
58676165
58686166 TEST_CASE (undef);
58696167
0 commit comments