Skip to content

Commit 0ed1480

Browse files
committed
Fix #760 (record argument for integer constant macros)
1 parent 79c4663 commit 0ed1480

3 files changed

Lines changed: 129 additions & 5 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,6 +1872,13 @@ namespace simplecpp {
18721872

18731873
class Macro {
18741874
public:
1875+
/** a usage of this macro: where it is used and, for INTn_C / UINTn_C, the argument as written in the call */
1876+
struct Usage {
1877+
explicit Usage(const Location &location) : location(location) {}
1878+
Location location;
1879+
std::string arg;
1880+
};
1881+
18751882
explicit Macro(std::vector<std::string> &f) : nameTokDef(nullptr), valueToken(nullptr), endToken(nullptr), files(f), tokenListDefine(f), variadic(false), variadicOpt(false), valueDefinedInCode_(false) {}
18761883

18771884
/**
@@ -2068,7 +2075,7 @@ namespace simplecpp {
20682075
}
20692076

20702077
/** how has this macro been used so far */
2071-
const std::list<Location> &usage() const {
2078+
const std::list<Usage> &usage() const {
20722079
return usageList;
20732080
}
20742081

@@ -2264,6 +2271,39 @@ namespace simplecpp {
22642271
return parametertokens;
22652272
}
22662273

2274+
/** Is name one of the <stdint.h> integer constant macros INTn_C / UINTn_C, n = 8, 16, 32 or 64? */
2275+
static bool isIntegerConstantMacro(const std::string &name) {
2276+
const std::string::size_type pos = (name.compare(0, 1, "U") == 0) ? 1 : 0;
2277+
if (name.compare(pos, 3, "INT") != 0)
2278+
return false;
2279+
const std::string bits = name.substr(pos + 3);
2280+
return bits == "8_C" || bits == "16_C" || bits == "32_C" || bits == "64_C";
2281+
}
2282+
2283+
/**
2284+
* The text between the parentheses of the call as written: the tokens joined with a single space,
2285+
* comments are skipped. nameTokInst is followed by '('.
2286+
*/
2287+
std::string writtenArgument(const Token *nameTokInst, bool calledInDefine) const {
2288+
std::string ret;
2289+
unsigned int par = 0U;
2290+
for (const Token *tok = nameTokInst->next->next; calledInDefine ? sameline(tok, nameTokInst) : (tok != nullptr); tok = tok->next) {
2291+
if (tok->comment)
2292+
continue;
2293+
if (tok->op == '(') {
2294+
++par;
2295+
} else if (tok->op == ')') {
2296+
if (par == 0U)
2297+
break;
2298+
--par;
2299+
}
2300+
if (!ret.empty())
2301+
ret += ' ';
2302+
ret += tok->str();
2303+
}
2304+
return ret;
2305+
}
2306+
22672307
/** The '(', each ',' between two arguments and the ')' of the call. nameTokInst is followed by '('. */
22682308
std::vector<const Token *> findArgumentDelimiters(const Token *nameTokInst, bool calledInDefine) const {
22692309
std::vector<const Token *> parametertokens;
@@ -2369,6 +2409,9 @@ namespace simplecpp {
23692409
return nameTokInst->next;
23702410
}
23712411

2412+
if (functionLike() && nameTokInst->next && nameTokInst->next->op == '(' && isIntegerConstantMacro(name()))
2413+
usageList.back().arg = writtenArgument(nameTokInst, calledInDefine);
2414+
23722415
std::vector<const Token*> parametertokens1(getMacroParameters(nameTokInst, calledInDefine));
23732416

23742417
// If macro call uses __COUNTER__ then expand that first
@@ -2976,7 +3019,7 @@ namespace simplecpp {
29763019
TokenList tokenListDefine;
29773020

29783021
/** usage of this macro */
2979-
mutable std::list<Location> usageList;
3022+
mutable std::list<Usage> usageList;
29803023

29813024
/** is macro variadic? */
29823025
bool variadic;
@@ -4533,14 +4576,15 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
45334576
if (macroUsage) {
45344577
for (simplecpp::MacroMap::const_iterator macroIt = macros.begin(); macroIt != macros.end(); ++macroIt) {
45354578
const Macro &macro = macroIt->second;
4536-
std::list<Location> usage = macro.usage();
4579+
std::list<Macro::Usage> usage = macro.usage();
45374580
const std::list<Location>& temp = maybeUsedMacros[macro.name()];
45384581
usage.insert(usage.end(), temp.begin(), temp.end());
4539-
for (std::list<Location>::const_iterator usageIt = usage.begin(); usageIt != usage.end(); ++usageIt) {
4582+
for (std::list<Macro::Usage>::iterator usageIt = usage.begin(); usageIt != usage.end(); ++usageIt) {
45404583
MacroUsage mu(macro.valueDefinedInCode());
45414584
mu.macroName = macro.name();
45424585
mu.macroLocation = macro.defineLocation();
4543-
mu.useLocation = *usageIt;
4586+
mu.useLocation = usageIt->location;
4587+
mu.arg = std::move(usageIt->arg);
45444588
macroUsage->emplace_back(std::move(mu));
45454589
}
45464590
}

‎simplecpp.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,9 @@ namespace simplecpp {
464464
Location macroLocation;
465465
Location useLocation;
466466
bool macroValueKnown;
467+
/** The argument of a call of INTn_C / UINTn_C (n = 8, 16, 32, 64) as written in the call: the tokens
468+
* between the parentheses joined with a single space, comments are skipped. Empty for all other macros. */
469+
std::string arg;
467470
};
468471

469472
/** Tracking #if/#elif expressions */

‎test.cpp‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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> &macroUsage)
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, &macroUsage);
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\nINT8_C(300)\n"));
4818+
ASSERT_EQUALS("INT8_C use 2:1: [1 + 2]\n", macroUsageArg("#define INT8_C(x) x\nINT8_C( 1+2 )\n"));
4819+
ASSERT_EQUALS("INT8_C use 2:1: [1 + 2]\n", macroUsageArg("#define INT8_C(x) x\nINT8_C(1 +\n 2)\n"));
4820+
ASSERT_EQUALS("INT8_C use 2:1: [( 1 , 2 )]\n", macroUsageArg("#define INT8_C(x) x\nINT8_C((1,2))\n"));
4821+
ASSERT_EQUALS("INT8_C use 2:1: []\n", macroUsageArg("#define INT8_C(x) x\nINT8_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\nUINT32_C( 10UL /*x*/)\n"));
4828+
ASSERT_EQUALS("UINT32_C use 2:1: [10UL]\n", macroUsageArg("#define UINT32_C(x) x\nUINT32_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\nUINT16_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__)\nUINT16_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\nINT64_C\n"));
4842+
ASSERT_EQUALS("INT64_C use 2:1: []\n", macroUsageArg("#define INT64_C(x) x\nINT64_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+
47914863
static 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

Comments
 (0)