@@ -4976,6 +4976,99 @@ static void tokenMacro15()
49764976 ASSERT_EQUALS (" D 1:1 use 1:3 unknown\n " , toString (macroUsage));
49774977}
49784978
4979+ /* * The argument of each macro usage as "name use line:col: [arg]", sorted. */
4980+ static std::string argToString (const std::list<simplecpp::MacroUsage> ¯oUsage)
4981+ {
4982+ std::vector<std::string> lines;
4983+ for (const simplecpp::MacroUsage &mu : macroUsage)
4984+ lines.push_back (mu.macroName + " use " + std::to_string (mu.useLocation .line ) + ' :' + std::to_string (mu.useLocation .col ) + " : [" + mu.arg + " ]\n " );
4985+ std::sort (lines.begin (), lines.end ());
4986+ std::string ret;
4987+ for (const std::string &line : lines)
4988+ ret += line;
4989+ return ret;
4990+ }
4991+
4992+ static std::string macroUsageArg (const char code[], bool removeComments = true )
4993+ {
4994+ simplecpp::DUI dui;
4995+ dui.removeComments = removeComments;
4996+ std::list<simplecpp::MacroUsage> macroUsage;
4997+ simplecpp::OutputList outputList;
4998+ preprocess (code, dui, &outputList, ¯oUsage);
4999+ return argToString (macroUsage);
5000+ }
5001+
5002+ static void macroUsageArg1 ()
5003+ {
5004+ // the tokens of the argument are joined with one space
5005+ ASSERT_EQUALS (" INT8_C use 2:1: [300]\n " , macroUsageArg (" #define INT8_C(x) x\n INT8_C(300)\n " ));
5006+ ASSERT_EQUALS (" INT8_C use 2:1: [1 + 2]\n " , macroUsageArg (" #define INT8_C(x) x\n INT8_C( 1+2 )\n " ));
5007+ ASSERT_EQUALS (" INT8_C use 2:1: [1 + 2]\n " , macroUsageArg (" #define INT8_C(x) x\n INT8_C(1 +\n 2)\n " ));
5008+ ASSERT_EQUALS (" INT8_C use 2:1: [( 1 , 2 )]\n " , macroUsageArg (" #define INT8_C(x) x\n INT8_C((1,2))\n " ));
5009+ ASSERT_EQUALS (" INT8_C use 2:1: []\n " , macroUsageArg (" #define INT8_C(x) x\n INT8_C()\n " ));
5010+ }
5011+
5012+ static void macroUsageArg2 ()
5013+ {
5014+ // comments are skipped, also when they are kept in the output
5015+ ASSERT_EQUALS (" UINT32_C use 2:1: [10UL]\n " , macroUsageArg (" #define UINT32_C(x) x##U\n UINT32_C( 10UL /*x*/)\n " ));
5016+ ASSERT_EQUALS (" UINT32_C use 2:1: [10UL]\n " , macroUsageArg (" #define UINT32_C(x) x\n UINT32_C( 10UL /*x*/)\n " , false ));
5017+ }
5018+
5019+ static void macroUsageArg3 ()
5020+ {
5021+ // several arguments: all the text between the parentheses
5022+ ASSERT_EQUALS (" UINT16_C use 2:1: [1 , , 3]\n " , macroUsageArg (" #define UINT16_C(a,b,c) a b c\n UINT16_C(1,,3)\n " ));
5023+ ASSERT_EQUALS (" UINT16_C use 2:1: [\" %d\" , a , b]\n " , macroUsageArg (" #define UINT16_C(fmt, ...) f(fmt, __VA_ARGS__)\n UINT16_C(\" %d\" , a, b)\n " ));
5024+ }
5025+
5026+ static void macroUsageArg4 ()
5027+ {
5028+ // object-like macro and function-like macro name without call have no argument
5029+ ASSERT_EQUALS (" INT64_C use 2:1: []\n " , macroUsageArg (" #define INT64_C 1\n INT64_C\n " ));
5030+ ASSERT_EQUALS (" INT64_C use 2:1: []\n " , macroUsageArg (" #define INT64_C(x) x\n INT64_C;\n " ));
5031+ }
5032+
5033+ static void macroUsageArg5 ()
5034+ {
5035+ // the argument is only recorded for INTn_C / UINTn_C
5036+ const char code[] = " #define __UINT32_C(c) c ## UL\n "
5037+ " #define UINT32_C(x) __UINT32_C(x)\n "
5038+ " #define F(x) x\n "
5039+ " #define INT128_C(x) x\n "
5040+ " #define XINT8_C(x) x\n "
5041+ " #define INT8_CX(x) x\n "
5042+ " UINT32_C(10UL) F(1) INT128_C(2) XINT8_C(3) INT8_CX(4)\n " ;
5043+ ASSERT_EQUALS (" F use 7:16: []\n "
5044+ " INT128_C use 7:21: []\n "
5045+ " INT8_CX use 7:44: []\n "
5046+ " UINT32_C use 7:1: [10UL]\n "
5047+ " XINT8_C use 7:33: []\n "
5048+ " __UINT32_C use 7:1: []\n " , macroUsageArg (code));
5049+ }
5050+
5051+ static void macroUsageArg7 ()
5052+ {
5053+ // INTMAX_C / UINTMAX_C
5054+ ASSERT_EQUALS (" INTMAX_C use 2:1: [300]\n " , macroUsageArg (" #define INTMAX_C(x) x\n INTMAX_C(300)\n " ));
5055+ ASSERT_EQUALS (" UINTMAX_C use 2:1: [300]\n " , macroUsageArg (" #define UINTMAX_C(x) x\n UINTMAX_C(300)\n " ));
5056+ ASSERT_EQUALS (" INTMAXX_C use 2:1: []\n " , macroUsageArg (" #define INTMAXX_C(x) x\n INTMAXX_C(300)\n " ));
5057+ }
5058+
5059+ static void macroUsageArg6 ()
5060+ {
5061+ // when the call is in a macro body the argument is recorded after the parameters
5062+ // are substituted and the macros in the arguments are expanded
5063+ const char code[] = " #define UINT32_C(x) x\n "
5064+ " #define X(a) UINT32_C(a)\n "
5065+ " #define R(a) a\n "
5066+ " X(R(7) /* c */ + 8)\n " ;
5067+ ASSERT_EQUALS (" R use 4:1: []\n "
5068+ " UINT32_C use 4:1: [7 + 8]\n "
5069+ " X use 4:1: []\n " , macroUsageArg (code));
5070+ }
5071+
49795072static void undef ()
49805073{
49815074 const char code[] = " #define A\n "
@@ -6053,6 +6146,13 @@ static void runTests(int argc, char **argv, Input input)
60536146 TEST_CASE (tokenMacro13);
60546147 TEST_CASE (tokenMacro14);
60556148 TEST_CASE (tokenMacro15);
6149+ TEST_CASE (macroUsageArg1);
6150+ TEST_CASE (macroUsageArg2);
6151+ TEST_CASE (macroUsageArg3);
6152+ TEST_CASE (macroUsageArg4);
6153+ TEST_CASE (macroUsageArg5);
6154+ TEST_CASE (macroUsageArg6);
6155+ TEST_CASE (macroUsageArg7);
60566156
60576157 TEST_CASE (undef);
60586158
0 commit comments