@@ -710,6 +710,181 @@ static void define13()
710710 " }" , preprocess (code));
711711}
712712
713+ static void define14 () // #296
714+ {
715+ const char code[] = " #define bar(x) x % 2\n "
716+ " #define foo(x) printf(#x \"\\ n\" )\n "
717+ " \n "
718+ " foo(bar(3));\n " ;
719+ ASSERT_EQUALS (" \n "
720+ " \n "
721+ " \n "
722+ " printf ( \" bar(3)\" \"\\ n\" ) ;" , preprocess (code));
723+ }
724+
725+ static void define15 () // #231
726+ {
727+ const char code[] = " #define CAT(a, b) CAT2(a, b)\n "
728+ " #define CAT2(a, b) a ## b\n "
729+ " #define FOO x\n "
730+ " #define BAR() CAT(F, OO)\n "
731+ " #define BAZ CAT(B, AR)()\n "
732+ " BAZ\n " ;
733+ ASSERT_EQUALS (" \n "
734+ " \n "
735+ " \n "
736+ " \n "
737+ " \n "
738+ " x" , preprocess (code));
739+ }
740+
741+ static void define16 () // #201
742+ {
743+ const char code[] = " #define ALL_COLORS(warm_colors) \\\n "
744+ " X(Blue) \\\n "
745+ " X(Green) \\\n "
746+ " X(Purple) \\\n "
747+ " warm_colors\n "
748+ " \n "
749+ " #define WARM_COLORS \\\n "
750+ " X(Red) \\\n "
751+ " X(Yellow) \\\n "
752+ " X(Orange)\n "
753+ " \n "
754+ " #define COLOR_SET ALL_COLORS(WARM_COLORS)\n "
755+ " \n "
756+ " #define X(color) #color,\n "
757+ " \n "
758+ " COLOR_SET\n " ;
759+ ASSERT_EQUALS (" \n "
760+ " \n "
761+ " \n "
762+ " \n "
763+ " \n "
764+ " \n "
765+ " \n "
766+ " \n "
767+ " \n "
768+ " \n "
769+ " \n "
770+ " \n "
771+ " \n "
772+ " \n "
773+ " \n "
774+ " \" Blue\" , \" Green\" , \" Purple\" , \" Red\" , \" Yellow\" , \" Orange\" ," , preprocess (code));
775+ }
776+
777+ static void define17 () // #185
778+ {
779+ const char code[] = " #define at(x, y) x##y\n "
780+ " #define b(...) \\\n "
781+ " aa(__VA_ARGS__, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , \\\n "
782+ " , , , , , , , , 2)\n "
783+ " #define aa(c, d, a, b, e, f, g, h, ab, ac, i, ad, j, k, l, m, n, o, p, ae, q, \\\n "
784+ " r, s, t, u, v, w, x, y, z, af, ag, ah, ai, aj, ak, al, am, an, ao, \\\n "
785+ " ap) \\\n "
786+ " ap\n "
787+ " #define aq(...) ar(b(__VA_ARGS__), __VA_ARGS__) static_assert(true, \" \" )\n "
788+ " #define ar(ap, ...) at(I_, ap)(__VA_ARGS__)\n "
789+ " #define I_2(as, a)\n "
790+ " aq(a, array);\n " ;
791+ ASSERT_EQUALS (" \n "
792+ " \n "
793+ " \n "
794+ " \n "
795+ " \n "
796+ " \n "
797+ " \n "
798+ " \n "
799+ " \n "
800+ " \n "
801+ " \n "
802+ " static_assert ( true , \" \" ) ;" , preprocess (code));
803+ }
804+
805+ static void define18 () // #130
806+ {
807+ const char code[] = " #define MAC2STR(x) x[0],x[1],x[2],x[3],x[4],x[5]\n "
808+ " #define FT_DEBUG(fmt, args...) if(pGlobalCtx && pGlobalCtx->debug_level>=2) printf(\" FT-dbg: \" fmt, ##args)\n "
809+ " \n "
810+ " FT_DEBUG(\" %02x:%02x:%02x:%02x:%02x:%02x\\ n\" , MAC2STR(pCtx->wlan_intf_addr[i]));\n " ;
811+ ASSERT_EQUALS (" \n "
812+ " \n "
813+ " \n "
814+ " if ( pGlobalCtx && pGlobalCtx -> debug_level >= 2 ) printf ( \" FT-dbg: \" \" %02x:%02x:%02x:%02x:%02x:%02x\\ n\" , pCtx -> wlan_intf_addr [ i ] [ 0 ] , pCtx -> wlan_intf_addr [ i ] [ 1 ] , pCtx -> wlan_intf_addr [ i ] [ 2 ] , pCtx -> wlan_intf_addr [ i ] [ 3 ] , pCtx -> wlan_intf_addr [ i ] [ 4 ] , pCtx -> wlan_intf_addr [ i ] [ 5 ] ) ;" , preprocess (code));
815+ }
816+
817+ static void define19 () // #124
818+ {
819+ const char code[] = " #define CONCAT(tok) tok##suffix\n "
820+ " \n "
821+ " CONCAT(Test);\n "
822+ " CONCAT(const Test);\n " ;
823+ ASSERT_EQUALS (" \n "
824+ " \n "
825+ " Testsuffix ;\n "
826+ " const Testsuffix ;" , preprocess (code));
827+ }
828+
829+ static void define20 () // #113
830+ {
831+ const char code[] = " #define TARGS4 T1,T2,T3,T4\n "
832+ " #define FOOIMPL(T__CLASS, TARGS) void foo(const T__CLASS<TARGS>& x) { }\n "
833+ " #define FOOIMPL_4(T__CLASS) FOOIMPL(T__CLASS, TARGS4)\n "
834+ " FOOIMPL_4(y)\n " ;
835+ ASSERT_EQUALS (" \n "
836+ " \n "
837+ " \n "
838+ " void foo ( const y < T1 , T2 , T3 , T4 > & x ) { }" , preprocess (code));
839+ }
840+
841+ static void define21 () // #66
842+ {
843+ const char code[] = " #define GETMYID(a) ((a))+1\n "
844+ " #define FIGHT_FOO(c, ...) foo(c, ##__VA_ARGS__)\n "
845+ " FIGHT_FOO(1, GETMYID(a));\n " ;
846+ ASSERT_EQUALS (" \n "
847+ " \n "
848+ " foo ( 1 , ( ( a ) ) + 1 ) ;" , preprocess (code));
849+ }
850+
851+ static void define22 () // #40
852+ {
853+ const char code[] = " #define COUNTER_NAME(NAME, ...) NAME##Count\n "
854+ " #define COMMA ,\n "
855+ " \n "
856+ " #define DECLARE_COUNTERS(LIST) unsigned long LIST(COUNTER_NAME, COMMA);\n "
857+ " \n "
858+ " #define ACTUAL_LIST(FUNCTION, SEPARATOR) \\\n "
859+ " FUNCTION(event1, int, foo) SEPARATOR \\\n "
860+ " FUNCTION(event2, char, bar)\n "
861+ " \n "
862+ " DECLARE_COUNTERS(ACTUAL_LIST)\n " ;
863+ ASSERT_EQUALS (" \n "
864+ " \n "
865+ " \n "
866+ " \n "
867+ " \n "
868+ " \n "
869+ " \n "
870+ " \n "
871+ " \n "
872+ " unsigned long event1Count , event2Count ;" , preprocess (code));
873+ }
874+
875+ static void define23 () // #40
876+ {
877+ const char code[] = " #define COMMA ,\n "
878+ " #define MULTI(SEPARATOR) A SEPARATOR B\n "
879+ " \n "
880+ " #define VARS MULTI(COMMA)\n "
881+ " unsigned VARS;\n " ;
882+ ASSERT_EQUALS (" \n "
883+ " \n "
884+ " \n "
885+ " \n "
886+ " unsigned A , B ;" , preprocess (code));
887+ }
713888
714889
715890static void define_invalid_1 ()
@@ -1172,6 +1347,15 @@ static void pragma_backslash()
11721347 ASSERT_EQUALS (" " , preprocess (code, &outputList));
11731348}
11741349
1350+ static void pragma_backslash_2 () // #217
1351+ {
1352+ const char code[] = " #pragma comment(linker, \" foo \\\n "
1353+ " bar\" )\n " ;
1354+
1355+ simplecpp::OutputList outputList;
1356+ ASSERT_EQUALS (" " , preprocess (code, &outputList));
1357+ }
1358+
11751359static void dollar ()
11761360{
11771361 ASSERT_EQUALS (" $ab" , readfile (" $ab" ));
@@ -3720,6 +3904,16 @@ int main(int argc, char **argv)
37203904 TEST_CASE (define11);
37213905 TEST_CASE (define12);
37223906 TEST_CASE (define13);
3907+ TEST_CASE (define14); // #296
3908+ TEST_CASE (define15); // #231
3909+ TEST_CASE (define16); // #201
3910+ TEST_CASE (define17); // #185
3911+ TEST_CASE (define18); // #130
3912+ TEST_CASE (define19); // #124
3913+ TEST_CASE (define20); // #113
3914+ TEST_CASE (define21); // #66
3915+ TEST_CASE (define22); // #40
3916+ TEST_CASE (define23); // #40
37233917 TEST_CASE (define_invalid_1);
37243918 TEST_CASE (define_invalid_2);
37253919 TEST_CASE (define_define_1);
@@ -3762,6 +3956,7 @@ int main(int argc, char **argv)
37623956 TEST_CASE (define_va_opt_9); // #632
37633957
37643958 TEST_CASE (pragma_backslash); // multiline pragma directive
3959+ TEST_CASE (pragma_backslash_2); // #217
37653960
37663961 // UB: #ifdef as macro parameter
37673962 TEST_CASE (define_ifdef);
0 commit comments