@@ -4788,6 +4788,78 @@ static void tokenMacro15()
47884788 ASSERT_EQUALS (" D 1:1 use 1:3 unknown\n " , toString (macroUsage));
47894789}
47904790
4791+ /* * The argument of each macro usage as "name use line:col: [arg]", sorted. */
4792+ static std::string argToString (const std::list<simplecpp::MacroUsage> ¯oUsage)
4793+ {
4794+ std::vector<std::string> lines;
4795+ for (const simplecpp::MacroUsage &mu : macroUsage)
4796+ lines.push_back (mu.macroName + " use " + std::to_string (mu.useLocation .line ) + ' :' + std::to_string (mu.useLocation .col ) + " : [" + mu.arg + " ]\n " );
4797+ std::sort (lines.begin (), lines.end ());
4798+ std::string ret;
4799+ for (const std::string &line : lines)
4800+ ret += line;
4801+ return ret;
4802+ }
4803+
4804+ static std::string macroUsageArg (const char code[], bool removeComments = true )
4805+ {
4806+ simplecpp::DUI dui;
4807+ dui.removeComments = removeComments;
4808+ std::list<simplecpp::MacroUsage> macroUsage;
4809+ simplecpp::OutputList outputList;
4810+ preprocess (code, dui, &outputList, ¯oUsage);
4811+ return argToString (macroUsage);
4812+ }
4813+
4814+ static void macroUsageArg1 ()
4815+ {
4816+ // the tokens of the argument are joined with one space
4817+ ASSERT_EQUALS (" INT8_C use 2:1: [300]\n " , macroUsageArg (" #define INT8_C(x) x\n INT8_C(300)\n " ));
4818+ ASSERT_EQUALS (" INT8_C use 2:1: [1 + 2]\n " , macroUsageArg (" #define INT8_C(x) x\n INT8_C( 1+2 )\n " ));
4819+ ASSERT_EQUALS (" INT8_C use 2:1: [1 + 2]\n " , macroUsageArg (" #define INT8_C(x) x\n INT8_C(1 +\n 2)\n " ));
4820+ ASSERT_EQUALS (" INT8_C use 2:1: [( 1 , 2 )]\n " , macroUsageArg (" #define INT8_C(x) x\n INT8_C((1,2))\n " ));
4821+ ASSERT_EQUALS (" INT8_C use 2:1: []\n " , macroUsageArg (" #define INT8_C(x) x\n INT8_C()\n " ));
4822+ }
4823+
4824+ static void macroUsageArg2 ()
4825+ {
4826+ // comments are skipped, also when they are kept in the output
4827+ ASSERT_EQUALS (" UINT32_C use 2:1: [10UL]\n " , macroUsageArg (" #define UINT32_C(x) x##U\n UINT32_C( 10UL /*x*/)\n " ));
4828+ ASSERT_EQUALS (" UINT32_C use 2:1: [10UL]\n " , macroUsageArg (" #define UINT32_C(x) x\n UINT32_C( 10UL /*x*/)\n " , false ));
4829+ }
4830+
4831+ static void macroUsageArg3 ()
4832+ {
4833+ // several arguments: all the text between the parentheses
4834+ 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 " ));
4835+ 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 " ));
4836+ }
4837+
4838+ static void macroUsageArg4 ()
4839+ {
4840+ // object-like macro and function-like macro name without call have no argument
4841+ ASSERT_EQUALS (" INT64_C use 2:1: []\n " , macroUsageArg (" #define INT64_C 1\n INT64_C\n " ));
4842+ ASSERT_EQUALS (" INT64_C use 2:1: []\n " , macroUsageArg (" #define INT64_C(x) x\n INT64_C;\n " ));
4843+ }
4844+
4845+ static void macroUsageArg5 ()
4846+ {
4847+ // the argument is only recorded for INTn_C / UINTn_C
4848+ const char code[] = " #define __UINT32_C(c) c ## UL\n "
4849+ " #define UINT32_C(x) __UINT32_C(x)\n "
4850+ " #define F(x) x\n "
4851+ " #define INT128_C(x) x\n "
4852+ " #define XINT8_C(x) x\n "
4853+ " #define INT8_CX(x) x\n "
4854+ " UINT32_C(10UL) F(1) INT128_C(2) XINT8_C(3) INT8_CX(4)\n " ;
4855+ ASSERT_EQUALS (" F use 7:16: []\n "
4856+ " INT128_C use 7:21: []\n "
4857+ " INT8_CX use 7:44: []\n "
4858+ " UINT32_C use 7:1: [10UL]\n "
4859+ " XINT8_C use 7:33: []\n "
4860+ " __UINT32_C use 7:1: []\n " , macroUsageArg (code));
4861+ }
4862+
47914863static void undef ()
47924864{
47934865 const char code[] = " #define A\n "
@@ -5864,6 +5936,11 @@ static void runTests(int argc, char **argv, Input input)
58645936 TEST_CASE (tokenMacro13);
58655937 TEST_CASE (tokenMacro14);
58665938 TEST_CASE (tokenMacro15);
5939+ TEST_CASE (macroUsageArg1);
5940+ TEST_CASE (macroUsageArg2);
5941+ TEST_CASE (macroUsageArg3);
5942+ TEST_CASE (macroUsageArg4);
5943+ TEST_CASE (macroUsageArg5);
58675944
58685945 TEST_CASE (undef);
58695946
0 commit comments