diff --git a/simplecpp.cpp b/simplecpp.cpp index 6fd26ddc..cd49e269 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1872,6 +1872,13 @@ namespace simplecpp { class Macro { public: + /** a usage of this macro: where it is used and, for INTn_C / UINTn_C, the argument of the call */ + struct Usage { + explicit Usage(const Location &location) : location(location) {} + Location location; + std::string arg; + }; + explicit Macro(std::vector &f) : nameTokDef(nullptr), valueToken(nullptr), endToken(nullptr), files(f), tokenListDefine(f), variadic(false), variadicOpt(false), valueDefinedInCode_(false) {} /** @@ -2068,7 +2075,7 @@ namespace simplecpp { } /** how has this macro been used so far */ - const std::list &usage() const { + const std::list &usage() const { return usageList; } @@ -2264,6 +2271,40 @@ namespace simplecpp { return parametertokens; } + /** Is name one of the integer constant macros INTn_C / UINTn_C, n = 8, 16, 32, 64 or MAX? */ + static bool isIntegerConstantMacro(const std::string &name) { + const std::string::size_type pos = (name.compare(0, 1, "U") == 0) ? 1 : 0; + if (name.compare(pos, 3, "INT") != 0) + return false; + const std::string bits = name.substr(pos + 3); + return bits == "8_C" || bits == "16_C" || bits == "32_C" || bits == "64_C" || bits == "MAX_C"; + } + + /** + * The text between the parentheses of the call: the tokens joined with a single space, comments are + * skipped. nameTokInst is followed by '('. If the call is in a macro body, the parameters of the + * enclosing macro have already been substituted and the macros in them expanded. + */ + static std::string writtenArgument(const Token *nameTokInst, bool calledInDefine) { + std::string ret; + unsigned int par = 0U; + for (const Token *tok = nameTokInst->next->next; calledInDefine ? sameline(tok, nameTokInst) : (tok != nullptr); tok = tok->next) { + if (tok->comment) + continue; + if (tok->op == '(') { + ++par; + } else if (tok->op == ')') { + if (par == 0U) + break; + --par; + } + if (!ret.empty()) + ret += ' '; + ret += tok->str(); + } + return ret; + } + /** The '(', each ',' between two arguments and the ')' of the call. nameTokInst is followed by '('. */ std::vector findArgumentDelimiters(const Token *nameTokInst, bool calledInDefine) const { std::vector parametertokens; @@ -2369,6 +2410,9 @@ namespace simplecpp { return nameTokInst->next; } + if (functionLike() && nameTokInst->next && nameTokInst->next->op == '(' && isIntegerConstantMacro(name())) + usageList.back().arg = writtenArgument(nameTokInst, calledInDefine); + std::vector parametertokens1(getMacroParameters(nameTokInst, calledInDefine)); // If macro call uses __COUNTER__ then expand that first @@ -2976,7 +3020,7 @@ namespace simplecpp { TokenList tokenListDefine; /** usage of this macro */ - mutable std::list usageList; + mutable std::list usageList; /** is macro variadic? */ bool variadic; @@ -4533,14 +4577,15 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL if (macroUsage) { for (simplecpp::MacroMap::const_iterator macroIt = macros.begin(); macroIt != macros.end(); ++macroIt) { const Macro ¯o = macroIt->second; - std::list usage = macro.usage(); + std::list usage = macro.usage(); const std::list& temp = maybeUsedMacros[macro.name()]; usage.insert(usage.end(), temp.begin(), temp.end()); - for (std::list::const_iterator usageIt = usage.begin(); usageIt != usage.end(); ++usageIt) { + for (std::list::iterator usageIt = usage.begin(); usageIt != usage.end(); ++usageIt) { MacroUsage mu(macro.valueDefinedInCode()); mu.macroName = macro.name(); mu.macroLocation = macro.defineLocation(); - mu.useLocation = *usageIt; + mu.useLocation = usageIt->location; + mu.arg = std::move(usageIt->arg); macroUsage->emplace_back(std::move(mu)); } } diff --git a/simplecpp.h b/simplecpp.h index 2b560d27..a34b7eca 100644 --- a/simplecpp.h +++ b/simplecpp.h @@ -464,6 +464,11 @@ namespace simplecpp { Location macroLocation; Location useLocation; bool macroValueKnown; + /** The argument of a call of INTn_C / UINTn_C (n = 8, 16, 32, 64, MAX): the tokens between the parentheses + * joined with a single space, comments are skipped. Empty for all other macros. + * For a call in the code this is the argument as written. For a call in a macro body it is the argument + * after the parameters of the enclosing macro are substituted and the macros in them are expanded. */ + std::string arg; }; /** Tracking #if/#elif expressions */ diff --git a/test.cpp b/test.cpp index 6a9cefa9..047dce38 100644 --- a/test.cpp +++ b/test.cpp @@ -4788,6 +4788,99 @@ static void tokenMacro15() ASSERT_EQUALS("D 1:1 use 1:3 unknown\n", toString(macroUsage)); } +/** The argument of each macro usage as "name use line:col: [arg]", sorted. */ +static std::string argToString(const std::list ¯oUsage) +{ + std::vector lines; + for (const simplecpp::MacroUsage &mu : macroUsage) + lines.push_back(mu.macroName + " use " + std::to_string(mu.useLocation.line) + ':' + std::to_string(mu.useLocation.col) + ": [" + mu.arg + "]\n"); + std::sort(lines.begin(), lines.end()); + std::string ret; + for (const std::string &line : lines) + ret += line; + return ret; +} + +static std::string macroUsageArg(const char code[], bool removeComments = true) +{ + simplecpp::DUI dui; + dui.removeComments = removeComments; + std::list macroUsage; + simplecpp::OutputList outputList; + preprocess(code, dui, &outputList, ¯oUsage); + return argToString(macroUsage); +} + +static void macroUsageArg1() +{ + // the tokens of the argument are joined with one space + ASSERT_EQUALS("INT8_C use 2:1: [300]\n", macroUsageArg("#define INT8_C(x) x\nINT8_C(300)\n")); + ASSERT_EQUALS("INT8_C use 2:1: [1 + 2]\n", macroUsageArg("#define INT8_C(x) x\nINT8_C( 1+2 )\n")); + ASSERT_EQUALS("INT8_C use 2:1: [1 + 2]\n", macroUsageArg("#define INT8_C(x) x\nINT8_C(1 +\n 2)\n")); + ASSERT_EQUALS("INT8_C use 2:1: [( 1 , 2 )]\n", macroUsageArg("#define INT8_C(x) x\nINT8_C((1,2))\n")); + ASSERT_EQUALS("INT8_C use 2:1: []\n", macroUsageArg("#define INT8_C(x) x\nINT8_C()\n")); +} + +static void macroUsageArg2() +{ + // comments are skipped, also when they are kept in the output + ASSERT_EQUALS("UINT32_C use 2:1: [10UL]\n", macroUsageArg("#define UINT32_C(x) x##U\nUINT32_C( 10UL /*x*/)\n")); + ASSERT_EQUALS("UINT32_C use 2:1: [10UL]\n", macroUsageArg("#define UINT32_C(x) x\nUINT32_C( 10UL /*x*/)\n", false)); +} + +static void macroUsageArg3() +{ + // several arguments: all the text between the parentheses + ASSERT_EQUALS("UINT16_C use 2:1: [1 , , 3]\n", macroUsageArg("#define UINT16_C(a,b,c) a b c\nUINT16_C(1,,3)\n")); + ASSERT_EQUALS("UINT16_C use 2:1: [\"%d\" , a , b]\n", macroUsageArg("#define UINT16_C(fmt, ...) f(fmt, __VA_ARGS__)\nUINT16_C(\"%d\", a, b)\n")); +} + +static void macroUsageArg4() +{ + // object-like macro and function-like macro name without call have no argument + ASSERT_EQUALS("INT64_C use 2:1: []\n", macroUsageArg("#define INT64_C 1\nINT64_C\n")); + ASSERT_EQUALS("INT64_C use 2:1: []\n", macroUsageArg("#define INT64_C(x) x\nINT64_C;\n")); +} + +static void macroUsageArg5() +{ + // the argument is only recorded for INTn_C / UINTn_C + const char code[] = "#define __UINT32_C(c) c ## UL\n" + "#define UINT32_C(x) __UINT32_C(x)\n" + "#define F(x) x\n" + "#define INT128_C(x) x\n" + "#define XINT8_C(x) x\n" + "#define INT8_CX(x) x\n" + "UINT32_C(10UL) F(1) INT128_C(2) XINT8_C(3) INT8_CX(4)\n"; + ASSERT_EQUALS("F use 7:16: []\n" + "INT128_C use 7:21: []\n" + "INT8_CX use 7:44: []\n" + "UINT32_C use 7:1: [10UL]\n" + "XINT8_C use 7:33: []\n" + "__UINT32_C use 7:1: []\n", macroUsageArg(code)); +} + +static void macroUsageArg7() +{ + // INTMAX_C / UINTMAX_C + ASSERT_EQUALS("INTMAX_C use 2:1: [300]\n", macroUsageArg("#define INTMAX_C(x) x\nINTMAX_C(300)\n")); + ASSERT_EQUALS("UINTMAX_C use 2:1: [300]\n", macroUsageArg("#define UINTMAX_C(x) x\nUINTMAX_C(300)\n")); + ASSERT_EQUALS("INTMAXX_C use 2:1: []\n", macroUsageArg("#define INTMAXX_C(x) x\nINTMAXX_C(300)\n")); +} + +static void macroUsageArg6() +{ + // when the call is in a macro body the argument is recorded after the parameters + // are substituted and the macros in the arguments are expanded + const char code[] = "#define UINT32_C(x) x\n" + "#define X(a) UINT32_C(a)\n" + "#define R(a) a\n" + "X(R(7) /* c */ + 8)\n"; + ASSERT_EQUALS("R use 4:1: []\n" + "UINT32_C use 4:1: [7 + 8]\n" + "X use 4:1: []\n", macroUsageArg(code)); +} + static void undef() { const char code[] = "#define A\n" @@ -5864,6 +5957,13 @@ static void runTests(int argc, char **argv, Input input) TEST_CASE(tokenMacro13); TEST_CASE(tokenMacro14); TEST_CASE(tokenMacro15); + TEST_CASE(macroUsageArg1); + TEST_CASE(macroUsageArg2); + TEST_CASE(macroUsageArg3); + TEST_CASE(macroUsageArg4); + TEST_CASE(macroUsageArg5); + TEST_CASE(macroUsageArg6); + TEST_CASE(macroUsageArg7); TEST_CASE(undef);