Skip to content

Commit e5cb618

Browse files
authored
Fix #760 (record argument for integer constant macros) (#761)
1 parent d4ff563 commit e5cb618

3 files changed

Lines changed: 155 additions & 5 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 50 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 of 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
/**
@@ -2067,7 +2074,7 @@ namespace simplecpp {
20672074
}
20682075

20692076
/** how has this macro been used so far */
2070-
const std::list<Location> &usage() const {
2077+
const std::list<Usage> &usage() const {
20712078
return usageList;
20722079
}
20732080

@@ -2263,6 +2270,40 @@ namespace simplecpp {
22632270
return parametertokens;
22642271
}
22652272

2273+
/** Is name one of the <stdint.h> integer constant macros INTn_C / UINTn_C, n = 8, 16, 32, 64 or MAX? */
2274+
static bool isIntegerConstantMacro(const std::string &name) {
2275+
const std::string::size_type pos = (name.compare(0, 1, "U") == 0) ? 1 : 0;
2276+
if (name.compare(pos, 3, "INT") != 0)
2277+
return false;
2278+
const std::string bits = name.substr(pos + 3);
2279+
return bits == "8_C" || bits == "16_C" || bits == "32_C" || bits == "64_C" || bits == "MAX_C";
2280+
}
2281+
2282+
/**
2283+
* The text between the parentheses of the call: the tokens joined with a single space, comments are
2284+
* skipped. nameTokInst is followed by '('. If the call is in a macro body, the parameters of the
2285+
* enclosing macro have already been substituted and the macros in them expanded.
2286+
*/
2287+
static std::string writtenArgument(const Token *nameTokInst, bool calledInDefine) {
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+
22662307
/** The '(', each ',' between two arguments and the ')' of the call. nameTokInst is followed by '('. */
22672308
std::vector<const Token *> findArgumentDelimiters(const Token *nameTokInst, bool calledInDefine) const {
22682309
std::vector<const Token *> parametertokens;
@@ -2368,6 +2409,9 @@ namespace simplecpp {
23682409
return nameTokInst->next;
23692410
}
23702411

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

23732417
// If macro call uses __COUNTER__ then expand that first
@@ -3142,7 +3186,7 @@ namespace simplecpp {
31423186
TokenList tokenListDefine;
31433187

31443188
/** usage of this macro */
3145-
mutable std::list<Location> usageList;
3189+
mutable std::list<Usage> usageList;
31463190

31473191
/** is macro variadic? */
31483192
bool variadic;
@@ -4699,14 +4743,15 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
46994743
if (macroUsage) {
47004744
for (simplecpp::MacroMap::const_iterator macroIt = macros.begin(); macroIt != macros.end(); ++macroIt) {
47014745
const Macro &macro = macroIt->second;
4702-
std::list<Location> usage = macro.usage();
4746+
std::list<Macro::Usage> usage = macro.usage();
47034747
const std::list<Location>& temp = maybeUsedMacros[macro.name()];
47044748
usage.insert(usage.end(), temp.begin(), temp.end());
4705-
for (std::list<Location>::const_iterator usageIt = usage.begin(); usageIt != usage.end(); ++usageIt) {
4749+
for (std::list<Macro::Usage>::iterator usageIt = usage.begin(); usageIt != usage.end(); ++usageIt) {
47064750
MacroUsage mu(macro.valueDefinedInCode());
47074751
mu.macroName = macro.name();
47084752
mu.macroLocation = macro.defineLocation();
4709-
mu.useLocation = *usageIt;
4753+
mu.useLocation = usageIt->location;
4754+
mu.arg = std::move(usageIt->arg);
47104755
macroUsage->emplace_back(std::move(mu));
47114756
}
47124757
}

‎simplecpp.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,11 @@ namespace simplecpp {
469469
Location macroLocation;
470470
Location useLocation;
471471
bool macroValueKnown;
472+
/** The argument of a call of INTn_C / UINTn_C (n = 8, 16, 32, 64, MAX): the tokens between the parentheses
473+
* joined with a single space, comments are skipped. Empty for all other macros.
474+
* For a call in the code this is the argument as written. For a call in a macro body it is the argument
475+
* after the parameters of the enclosing macro are substituted and the macros in them are expanded. */
476+
std::string arg;
472477
};
473478

474479
/** Tracking #if/#elif expressions */

‎test.cpp‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4976,6 +4976,99 @@ static void tokenMacro15()
49764976
ASSERT_EQUALS("D 1:1 use 1:3 unknown\n", toString(macroUsage));
49774977
}
49784978

4979+
/** The argument of each macro usage as "name use line:col: [arg]", sorted. */
4980+
static std::string argToString(const std::list<simplecpp::MacroUsage> &macroUsage)
4981+
{
4982+
std::vector<std::string> lines;
4983+
for (const simplecpp::MacroUsage &mu : macroUsage)
4984+
lines.push_back(mu.macroName + " use " + std::to_string(mu.useLocation.line) + ':' + std::to_string(mu.useLocation.col) + ": [" + mu.arg + "]\n");
4985+
std::sort(lines.begin(), lines.end());
4986+
std::string ret;
4987+
for (const std::string &line : lines)
4988+
ret += line;
4989+
return ret;
4990+
}
4991+
4992+
static std::string macroUsageArg(const char code[], bool removeComments = true)
4993+
{
4994+
simplecpp::DUI dui;
4995+
dui.removeComments = removeComments;
4996+
std::list<simplecpp::MacroUsage> macroUsage;
4997+
simplecpp::OutputList outputList;
4998+
preprocess(code, dui, &outputList, &macroUsage);
4999+
return argToString(macroUsage);
5000+
}
5001+
5002+
static void macroUsageArg1()
5003+
{
5004+
// the tokens of the argument are joined with one space
5005+
ASSERT_EQUALS("INT8_C use 2:1: [300]\n", macroUsageArg("#define INT8_C(x) x\nINT8_C(300)\n"));
5006+
ASSERT_EQUALS("INT8_C use 2:1: [1 + 2]\n", macroUsageArg("#define INT8_C(x) x\nINT8_C( 1+2 )\n"));
5007+
ASSERT_EQUALS("INT8_C use 2:1: [1 + 2]\n", macroUsageArg("#define INT8_C(x) x\nINT8_C(1 +\n 2)\n"));
5008+
ASSERT_EQUALS("INT8_C use 2:1: [( 1 , 2 )]\n", macroUsageArg("#define INT8_C(x) x\nINT8_C((1,2))\n"));
5009+
ASSERT_EQUALS("INT8_C use 2:1: []\n", macroUsageArg("#define INT8_C(x) x\nINT8_C()\n"));
5010+
}
5011+
5012+
static void macroUsageArg2()
5013+
{
5014+
// comments are skipped, also when they are kept in the output
5015+
ASSERT_EQUALS("UINT32_C use 2:1: [10UL]\n", macroUsageArg("#define UINT32_C(x) x##U\nUINT32_C( 10UL /*x*/)\n"));
5016+
ASSERT_EQUALS("UINT32_C use 2:1: [10UL]\n", macroUsageArg("#define UINT32_C(x) x\nUINT32_C( 10UL /*x*/)\n", false));
5017+
}
5018+
5019+
static void macroUsageArg3()
5020+
{
5021+
// several arguments: all the text between the parentheses
5022+
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"));
5023+
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"));
5024+
}
5025+
5026+
static void macroUsageArg4()
5027+
{
5028+
// object-like macro and function-like macro name without call have no argument
5029+
ASSERT_EQUALS("INT64_C use 2:1: []\n", macroUsageArg("#define INT64_C 1\nINT64_C\n"));
5030+
ASSERT_EQUALS("INT64_C use 2:1: []\n", macroUsageArg("#define INT64_C(x) x\nINT64_C;\n"));
5031+
}
5032+
5033+
static void macroUsageArg5()
5034+
{
5035+
// the argument is only recorded for INTn_C / UINTn_C
5036+
const char code[] = "#define __UINT32_C(c) c ## UL\n"
5037+
"#define UINT32_C(x) __UINT32_C(x)\n"
5038+
"#define F(x) x\n"
5039+
"#define INT128_C(x) x\n"
5040+
"#define XINT8_C(x) x\n"
5041+
"#define INT8_CX(x) x\n"
5042+
"UINT32_C(10UL) F(1) INT128_C(2) XINT8_C(3) INT8_CX(4)\n";
5043+
ASSERT_EQUALS("F use 7:16: []\n"
5044+
"INT128_C use 7:21: []\n"
5045+
"INT8_CX use 7:44: []\n"
5046+
"UINT32_C use 7:1: [10UL]\n"
5047+
"XINT8_C use 7:33: []\n"
5048+
"__UINT32_C use 7:1: []\n", macroUsageArg(code));
5049+
}
5050+
5051+
static void macroUsageArg7()
5052+
{
5053+
// INTMAX_C / UINTMAX_C
5054+
ASSERT_EQUALS("INTMAX_C use 2:1: [300]\n", macroUsageArg("#define INTMAX_C(x) x\nINTMAX_C(300)\n"));
5055+
ASSERT_EQUALS("UINTMAX_C use 2:1: [300]\n", macroUsageArg("#define UINTMAX_C(x) x\nUINTMAX_C(300)\n"));
5056+
ASSERT_EQUALS("INTMAXX_C use 2:1: []\n", macroUsageArg("#define INTMAXX_C(x) x\nINTMAXX_C(300)\n"));
5057+
}
5058+
5059+
static void macroUsageArg6()
5060+
{
5061+
// when the call is in a macro body the argument is recorded after the parameters
5062+
// are substituted and the macros in the arguments are expanded
5063+
const char code[] = "#define UINT32_C(x) x\n"
5064+
"#define X(a) UINT32_C(a)\n"
5065+
"#define R(a) a\n"
5066+
"X(R(7) /* c */ + 8)\n";
5067+
ASSERT_EQUALS("R use 4:1: []\n"
5068+
"UINT32_C use 4:1: [7 + 8]\n"
5069+
"X use 4:1: []\n", macroUsageArg(code));
5070+
}
5071+
49795072
static void undef()
49805073
{
49815074
const char code[] = "#define A\n"
@@ -6053,6 +6146,13 @@ static void runTests(int argc, char **argv, Input input)
60536146
TEST_CASE(tokenMacro13);
60546147
TEST_CASE(tokenMacro14);
60556148
TEST_CASE(tokenMacro15);
6149+
TEST_CASE(macroUsageArg1);
6150+
TEST_CASE(macroUsageArg2);
6151+
TEST_CASE(macroUsageArg3);
6152+
TEST_CASE(macroUsageArg4);
6153+
TEST_CASE(macroUsageArg5);
6154+
TEST_CASE(macroUsageArg6);
6155+
TEST_CASE(macroUsageArg7);
60566156

60576157
TEST_CASE(undef);
60586158

0 commit comments

Comments
 (0)