Skip to content

Commit 370f18d

Browse files
authored
Expand function macros that have arguments at the end (#695)
1 parent 2567ec4 commit 370f18d

3 files changed

Lines changed: 94 additions & 11 deletions

File tree

‎run-tests.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ def cleanup(out: str) -> str:
8080
'macro_rescan_varargs.c',
8181

8282
# todo, high priority
83-
'c99-6_10_3_4_p5.c',
8483
'c99-6_10_3_4_p6.c',
8584
'expr_usual_conversions.c', # condition is true: 4U - 30 >= 0
8685
'stdint.c',

‎simplecpp.cpp‎

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,26 +2177,37 @@ namespace simplecpp {
21772177
return &it->second;
21782178
}
21792179

2180-
const Token *recursiveExpandToken(TokenList &output, TokenList &temp, const Location &loc, const Token *tok, const MacroMap &macros, const std::set<TokenString> &expandedmacros, const std::vector<const Token*> &parametertokens) const {
2181-
// Expand while the expansion result ends with the name of a function-like
2182-
// macro whose arguments are supplied by the tokens that follow it. Each round
2183-
// consumes that macro call from the token stream, so tok always advances.
2180+
/** Expand while the expansion result in @p temp ends with the name of a
2181+
* function-like macro whose arguments are supplied by the tokens after @p tok.
2182+
* Each round consumes that macro call from the token stream. The @p gatherCall
2183+
* callback copies the "( ... )" tokens that follow @p lpar into its output list
2184+
* and returns the matching ')', or nullptr when no complete call is available.
2185+
* Returns the first token that was not consumed (tok->next when no call was
2186+
* expanded). */
2187+
template<class GatherCall>
2188+
const Token *expandTrailingCalls(TokenList &output, TokenList &temp, const Location &loc, const Token *tok, const MacroMap &macros, const std::set<TokenString> &expandedmacros, GatherCall gatherCall) const {
21842189
while (const Macro * const calledMacro = rescanMacro(temp, tok, macros, expandedmacros)) {
21852190
TokenList temp2(files);
2186-
temp2.push_back(new Token(temp.cback()->str(), tok->location));
2191+
temp2.push_back(new Token(temp.cback()->str(), tok->next->location));
21872192

2188-
const Token * const tok2 = appendTokens(temp2, loc, tok->next, macros, expandedmacros, parametertokens);
2189-
if (!tok2)
2193+
const Token * const closingPar = gatherCall(temp2, tok->next);
2194+
if (!closingPar)
21902195
break;
21912196
output.takeTokens(temp);
21922197
output.deleteToken(output.back());
21932198
calledMacro->expand(temp, loc, temp2.cfront(), macros, expandedmacros);
2194-
tok = tok2;
2199+
tok = closingPar;
21952200
}
21962201
output.takeTokens(temp);
21972202
return tok->next;
21982203
}
21992204

2205+
const Token *recursiveExpandToken(TokenList &output, TokenList &temp, const Location &loc, const Token *tok, const MacroMap &macros, const std::set<TokenString> &expandedmacros, const std::vector<const Token*> &parametertokens) const {
2206+
return expandTrailingCalls(output, temp, loc, tok, macros, expandedmacros, [&](TokenList &temp2, const Token *lpar) {
2207+
return appendTokens(temp2, loc, lpar, macros, expandedmacros, parametertokens);
2208+
});
2209+
}
2210+
22002211
const Token *expandToken(TokenList &output, const Location &loc, const Token *tok, const MacroMap &macros, const std::set<TokenString> &expandedmacros, const std::vector<const Token*> &parametertokens) const {
22012212
// Not name..
22022213
if (!tok->name) {
@@ -2313,12 +2324,35 @@ namespace simplecpp {
23132324
return false;
23142325
if (variadic && argnr + 1U >= parametertokens.size()) // empty variadic parameter
23152326
return true;
2316-
for (const Token *partok = parametertokens[argnr]->next; partok != parametertokens[argnr + 1U];) {
2327+
const Token * const argEnd = parametertokens[argnr + 1U];
2328+
for (const Token *partok = parametertokens[argnr]->next; partok != argEnd;) {
23172329
const MacroMap::const_iterator it = macros.find(partok->str());
23182330
if (it != macros.end() && !partok->isExpandedFrom(&it->second) && (partok->str() == name() || expandedmacros.find(partok->str()) == expandedmacros.end())) {
23192331
std::set<TokenString> expandedmacros2(expandedmacros); // temporary amnesia to allow reexpansion of currently expanding macros during argument evaluation
23202332
expandedmacros2.erase(name());
2321-
partok = it->second.expand(output, loc, partok, macros, std::move(expandedmacros2));
2333+
TokenList temp(files);
2334+
partok = it->second.expand(temp, loc, partok, macros, std::move(expandedmacros2));
2335+
if (partok->op == '(' && temp.cback() && temp.cback()->name) {
2336+
// the expansion result may end with the name of a function-like
2337+
// macro whose arguments are supplied by the remaining argument tokens
2338+
std::set<TokenString> expandedmacros3(expandedmacros);
2339+
expandedmacros3.erase(name());
2340+
partok = expandTrailingCalls(output, temp, loc, partok->previous, macros, expandedmacros3, [&](TokenList &temp2, const Token *lpar) -> const Token * {
2341+
unsigned int par = 0;
2342+
for (const Token *tok2 = lpar; tok2 != argEnd; tok2 = tok2->next) {
2343+
temp2.push_back(new Token(*tok2));
2344+
if (tok2->op == '(') {
2345+
++par;
2346+
} else if (tok2->op == ')') {
2347+
if (--par == 0U)
2348+
return tok2;
2349+
}
2350+
}
2351+
return nullptr;
2352+
});
2353+
} else {
2354+
output.takeTokens(temp);
2355+
}
23222356
} else {
23232357
output.push_back(newMacroToken(partok->str(), loc, isReplaced(expandedmacros), partok));
23242358
output.back()->macro = partok->macro;

‎test.cpp‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,55 @@ static void define26()
989989
"f ( ) ( )", preprocess(code2));
990990
}
991991

992+
static void define27()
993+
{
994+
// an expansion result inside a macro argument that is a function-like macro
995+
// name must be rescanned against the remaining argument tokens
996+
const char code1[] = "#define f(x) [x]\n"
997+
"#define ID(x) x\n"
998+
"#define G() f\n"
999+
"ID(G()(1))\n";
1000+
ASSERT_EQUALS("\n"
1001+
"\n"
1002+
"\n"
1003+
"[ 1 ]", preprocess(code1));
1004+
1005+
// the rescanned call's arguments are themselves rescanned
1006+
const char code2[] = "#define f(x) [x]\n"
1007+
"#define ID(x) x\n"
1008+
"#define G() f\n"
1009+
"ID(G()(G()(2)))\n";
1010+
ASSERT_EQUALS("\n"
1011+
"\n"
1012+
"\n"
1013+
"[ [ 2 ] ]", preprocess(code2));
1014+
1015+
// each rescan round must use the right set of already-expanded macros:
1016+
// A(1)->B, B(2)->A, A(3)->B, B(4)->A
1017+
const char code3[] = "#define ID(x) x\n"
1018+
"#define A(x) B\n"
1019+
"#define B(x) A\n"
1020+
"ID(A(1)(2)(3)(4))\n";
1021+
ASSERT_EQUALS("\n"
1022+
"\n"
1023+
"\n"
1024+
"A", preprocess(code3));
1025+
1026+
// creduce'd fuzzer case: the missed rescan of o1(k) deferred the call to a
1027+
// context where j was wrongly blocked from expanding, so b pasted ')' and
1028+
// '1' into an invalid token that broke the parentheses of the m call
1029+
const char code4[] = "#define o1(a) a()\n"
1030+
"#define b(c, d) c##d\n"
1031+
"#define l(c, d) b(c, 1)\n"
1032+
"#define m(e, f)\n"
1033+
"#define g(h, i) m(h, )\n"
1034+
"#define j(h, i) g(h, )\n"
1035+
"#define k() l(j(, ), )\n"
1036+
"#define n(a) j(l(o, )(a), )\n"
1037+
"n(k)\n";
1038+
ASSERT_EQUALS("", preprocess(code4));
1039+
}
1040+
9921041

9931042
static void define_invalid_1()
9941043
{
@@ -4588,6 +4637,7 @@ static void runTests(int argc, char **argv, Input input)
45884637
TEST_CASE(define24);
45894638
TEST_CASE(define25);
45904639
TEST_CASE(define26);
4640+
TEST_CASE(define27);
45914641
TEST_CASE(define_invalid_1);
45924642
TEST_CASE(define_invalid_2);
45934643
TEST_CASE(define_invalid_3);

0 commit comments

Comments
 (0)