Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
54 changes: 49 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 as written in 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,39 @@ namespace simplecpp {
return parametertokens;
}

/** Is name one of the <stdint.h> integer constant macros INTn_C / UINTn_C, n = 8, 16, 32 or 64? */
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";
Comment thread
danmar marked this conversation as resolved.
Resolved
}

/**
* The text between the parentheses of the call as written: the tokens joined with a single space,
* comments are skipped. nameTokInst is followed by '('.
*/
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 +2409,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 +3019,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 +4576,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
3 changes: 3 additions & 0 deletions simplecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,9 @@ namespace simplecpp {
Location macroLocation;
Location useLocation;
bool macroValueKnown;
/** The argument of a call of INTn_C / UINTn_C (n = 8, 16, 32, 64) as written in the call: the tokens
* between the parentheses joined with a single space, comments are skipped. Empty for all other macros. */
Comment thread
danmar marked this conversation as resolved.
Resolved
std::string arg;
};

/** Tracking #if/#elif expressions */
Expand Down
77 changes: 77 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4788,6 +4788,78 @@ 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 undef()
{
const char code[] = "#define A\n"
Expand Down Expand Up @@ -5864,6 +5936,11 @@ 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(undef);

Expand Down
Loading