Skip to content

Commit cd9cdef

Browse files
committed
Fix issue with pasting
1 parent 8ea6017 commit cd9cdef

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,7 +2256,9 @@ namespace simplecpp {
22562256
* Parameters are substituted before the rescan, so the parentheses are
22572257
* counted in the substituted tokens: parameters that expand to nothing are
22582258
* skipped, and the '(' and the ')' of the call can each come from the
2259-
* replacement list or from a parameter. Returns the replacement-list token
2259+
* replacement list or from a parameter. A parameter that is followed by
2260+
* ## is not macro-expanded: the search stops at it when it comes before
2261+
* the '('. Returns the replacement-list token
22602262
* that supplied the ')', or nullptr when there is no complete call. When
22612263
* that token is a parameter, its expansion is copied whole since it may
22622264
* hold more than the end of the call. When the first parameter that does
@@ -2276,7 +2278,17 @@ namespace simplecpp {
22762278
continue;
22772279
}
22782280
const Token * const end = callArgs.cback();
2279-
if (!expandArg(callArgs, after, loc, macros, expandedmacros, parametertokens, outputList)) {
2281+
bool isParameter;
2282+
if (sameline(after, after->next) && after->next->op == '#' && sameline(after, after->next->next) && after->next->next->op == '#') {
2283+
// the left operand of ## is substituted without macro expansion
2284+
isParameter = expandArg(callArgs, after, parametertokens);
2285+
// the paste can change what follows the macro name
2286+
if (isParameter && par == 0U)
2287+
return nullptr;
2288+
} else {
2289+
isParameter = expandArg(callArgs, after, loc, macros, expandedmacros, parametertokens, outputList);
2290+
}
2291+
if (!isParameter) {
22802292
// only parameters, which can expand to nothing, can come between
22812293
// the macro name and its '('
22822294
if (par == 0U && after->op != '(')

‎test.cpp‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,22 @@ static void define28()
12111211
"\n"
12121212
"\n"
12131213
"[ F ] F [ 1 ]", preprocess(code9));
1214+
1215+
// a parameter that is followed by ## is not macro-expanded: after a
1216+
// trailing macro name it does not start the call, and inside the call it
1217+
// is substituted as it is
1218+
const char code10[] = "#define F(x) [x]\n"
1219+
"#define E\n"
1220+
"#define X (1)\n"
1221+
"#define A(f, x, y) f x ## y\n"
1222+
"#define C(f, x, y) f (x ## y)\n"
1223+
"A(F, X, ) A(F, X, Z) C(F, 1 E, Z)\n";
1224+
ASSERT_EQUALS("\n"
1225+
"\n"
1226+
"\n"
1227+
"\n"
1228+
"\n"
1229+
"F ( 1 ) F XZ [ 1 EZ ]", preprocess(code10));
12141230
}
12151231

12161232

0 commit comments

Comments
 (0)