Skip to content

Commit 05d017c

Browse files
committed
Fix another test case
1 parent e1fe1ab commit 05d017c

2 files changed

Lines changed: 64 additions & 25 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2252,35 +2252,53 @@ namespace simplecpp {
22522252

22532253
/** Gather the call of the function-like macro named by @p nameTok whose
22542254
* arguments follow the replacement-list token @p tok into @p call.
2255-
* Parameters are substituted before the rescan, so parameters that expand
2256-
* to nothing are skipped and the arguments can come from a parameter whose
2257-
* expansion starts with '('; that expansion is copied whole since it may
2258-
* hold more than the arguments. Returns the ')' from the replacement list,
2259-
* the parameter that supplied the arguments, or nullptr when there is no
2260-
* call. */
2255+
* Parameters are substituted before the rescan, so the parentheses are
2256+
* counted in the substituted tokens: parameters that expand to nothing are
2257+
* skipped, and the '(' and the ')' of the call can each come from the
2258+
* replacement list or from a parameter. Returns the replacement-list token
2259+
* that supplied the ')', or nullptr when there is no complete call. When
2260+
* that token is a parameter, its expansion is copied whole since it may
2261+
* hold more than the end of the call. */
22612262
const Token *gatherCallAfter(TokenList &call, const Token *nameTok, const Location &loc, const Token *tok, const MacroMap &macros, const std::set<TokenString> &expandedmacros, const std::vector<const Token*> &parametertokens, OutputList *outputList) const {
22622263
TokenList callArgs(files);
2263-
const Token *last = nullptr;
2264-
for (const Token *after = tok->next; sameline(tok, after); after = after->next) {
2265-
if (after->op == '(') {
2266-
last = appendTokens(callArgs, loc, after, macros, expandedmacros, parametertokens, outputList);
2267-
break;
2264+
unsigned int par = 0;
2265+
for (const Token *after = tok->next; sameline(tok, after);) {
2266+
if (par > 0U && after->op == '#' && sameline(after, after->next) && (after->next->op != '#' || sameline(after, after->next->next))) {
2267+
// # and ## keep the parentheses balanced
2268+
if (after->next->op == '#')
2269+
after = expandHashHash(callArgs, loc, after, macros, expandedmacros, parametertokens, outputList, false);
2270+
else
2271+
after = expandHash(callArgs, loc, after, expandedmacros, parametertokens, outputList);
2272+
continue;
22682273
}
2269-
if (!expandArg(callArgs, after, loc, macros, expandedmacros, parametertokens, outputList))
2270-
break;
2271-
// a parameter that expands to nothing is substituted before the rescan,
2272-
// so it does not separate the macro name from its '('
2273-
if (callArgs.cfront()) {
2274-
if (callArgs.cfront()->op == '(')
2275-
last = after;
2276-
break;
2274+
const Token * const end = callArgs.cback();
2275+
if (!expandArg(callArgs, after, loc, macros, expandedmacros, parametertokens, outputList)) {
2276+
// only parameters, which can expand to nothing, can come between
2277+
// the macro name and its '('
2278+
if (par == 0U && after->op != '(')
2279+
return nullptr;
2280+
callArgs.push_back(new Token(*after));
2281+
if (after->macro.empty() && par > 0U)
2282+
callArgs.back()->macro = name();
22772283
}
2284+
for (const Token *tok2 = end ? end->next : callArgs.cfront(); tok2; tok2 = tok2->next) {
2285+
if (tok2->op == '(') {
2286+
++par;
2287+
} else if (par == 0U) {
2288+
// the parameter does not start with '('
2289+
return nullptr;
2290+
} else if (tok2->op == ')' && --par == 0U) {
2291+
// the tokens of the call must be on the same line
2292+
for (Token *tok3 = callArgs.front(); tok3 != tok2->next; tok3 = tok3->next)
2293+
tok3->location = callArgs.cfront()->location;
2294+
call.push_back(new Token(nameTok->str(), callArgs.cfront()->location));
2295+
call.takeTokens(callArgs);
2296+
return after;
2297+
}
2298+
}
2299+
after = after->next;
22782300
}
2279-
if (!last)
2280-
return nullptr;
2281-
call.push_back(new Token(nameTok->str(), callArgs.cfront()->location));
2282-
call.takeTokens(callArgs);
2283-
return last;
2301+
return nullptr;
22842302
}
22852303

22862304
/** Expand while the expansion result in @p temp ends with the name of a
@@ -2300,7 +2318,7 @@ namespace simplecpp {
23002318
output.takeTokens(temp);
23012319
output.deleteToken(output.back());
23022320
if (last->name) {
2303-
// the arguments came from a parameter that may hold more than the call
2321+
// the call ended in a parameter that may hold more than the call
23042322
temp.takeTokens(call);
23052323
rescanArg(temp, loc, macros, expandedmacros, outputList);
23062324
} else {

‎test.cpp‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,27 @@ static void define28()
11581158
ASSERT_EQUALS(std::string(32, '\n') +
11591159
"REPEAT_INDIRECT ( ) ( 1 , M , ~ ) M ( 1 , ~ )\n"
11601160
"0 1 2", preprocess(code6));
1161+
1162+
// the parentheses of a call are counted after the parameters are
1163+
// substituted, so its '(' and its ')' can each come from a parameter. The
1164+
// tokens that a parameter holds after the ')' are rescanned with the
1165+
// tokens that follow.
1166+
const char code7[] = "#define LP (\n"
1167+
"#define RP )\n"
1168+
"#define F(x) [x]\n"
1169+
"#define G(x) <x>\n"
1170+
"#define A(f, x) f x 1)\n"
1171+
"#define B(x) F(1 x 2)\n"
1172+
"#define C(x) F(1 x (2))\n"
1173+
"A(F, (LP)) B(RP) B(RP LP) C(RP G)\n";
1174+
ASSERT_EQUALS("\n"
1175+
"\n"
1176+
"\n"
1177+
"\n"
1178+
"\n"
1179+
"\n"
1180+
"\n"
1181+
"[ ( ) 1 ] [ 1 ] 2 ) [ 1 ] ( 2 ) [ 1 ] < 2 > )", preprocess(code7));
11611182
}
11621183

11631184

0 commit comments

Comments
 (0)