Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 50 additions & 5 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> &f) : nameTokDef(nullptr), valueToken(nullptr), endToken(nullptr), files(f), tokenListDefine(f), variadic(false), variadicOpt(false), valueDefinedInCode_(false) {}

/**
Expand Down Expand Up @@ -2068,7 +2075,7 @@ namespace simplecpp {
}

/** how has this macro been used so far */
const std::list<Location> &usage() const {
const std::list<Usage> &usage() const {
return usageList;
}

Expand Down Expand Up @@ -2264,6 +2271,40 @@ namespace simplecpp {
return parametertokens;
}

/** Is name one of the <stdint.h> 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<const Token *> findArgumentDelimiters(const Token *nameTokInst, bool calledInDefine) const {
std::vector<const Token *> parametertokens;
Expand Down Expand Up @@ -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<const Token*> parametertokens1(getMacroParameters(nameTokInst, calledInDefine));

// If macro call uses __COUNTER__ then expand that first
Expand Down Expand Up @@ -2976,7 +3020,7 @@ namespace simplecpp {
TokenList tokenListDefine;

/** usage of this macro */
mutable std::list<Location> usageList;
mutable std::list<Usage> usageList;

/** is macro variadic? */
bool variadic;
Expand Down Expand Up @@ -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 &macro = macroIt->second;
std::list<Location> usage = macro.usage();
std::list<Macro::Usage> usage = macro.usage();
const std::list<Location>& temp = maybeUsedMacros[macro.name()];
usage.insert(usage.end(), temp.begin(), temp.end());
for (std::list<Location>::const_iterator usageIt = usage.begin(); usageIt != usage.end(); ++usageIt) {
for (std::list<Macro::Usage>::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));
}
}
Expand Down
5 changes: 5 additions & 0 deletions simplecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
100 changes: 100 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<simplecpp::MacroUsage> &macroUsage)
{
std::vector<std::string> 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<simplecpp::MacroUsage> macroUsage;
simplecpp::OutputList outputList;
preprocess(code, dui, &outputList, &macroUsage);
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"
Expand Down Expand Up @@ -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);

Expand Down
Loading