Skip to content

Commit 12c7dd3

Browse files
dmcppcheckclaude
andcommitted
HideSetExpander: handle ##
As in C11 6.10.3.3: an argument that is an operand of ## is not macro expanded, an empty argument is a placemarker, and the pasted token is rescanned. The ## checks of the old expansion are used. Like the old expansion, ", ## __VA_ARGS__" removes the comma when there are no variable arguments, and a string literal followed by a name is not pasted. With the hide-set expansion, 141 of the 155 clang/gcc testsuite tests pass (140 with the old expansion). The new test macro TODO_OLD_EXPANSION_ASSERT_EQUALS() is for known bugs of the old expansion. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6d16edf commit 12c7dd3

2 files changed

Lines changed: 161 additions & 151 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2952,7 +2952,7 @@ namespace simplecpp {
29522952
* The result of an expansion is pushed back onto the input and scanned again, so a
29532953
* function-like macro whose arguments follow the expansion in the source is found by
29542954
* normal rescanning.
2955-
* Work in progress: ##, __VA_OPT__ and "defined" in a replacement list are not handled yet.
2955+
* Work in progress: __VA_OPT__ and "defined" in a replacement list are not handled yet.
29562956
*/
29572957
class HideSetExpander {
29582958
public:
@@ -3126,6 +3126,57 @@ namespace simplecpp {
31263126
return (tok && tok != macro.endToken && tok->name) ? macro.getArgNum(tok->str()) : ~0U;
31273127
}
31283128

3129+
/** tok and the token after it are ## in the replacement list of macro */
3130+
static bool isHashHash(const Macro &macro, const Token *tok) {
3131+
if (!tok || tok == macro.endToken || tok->op != '#')
3132+
return false;
3133+
const Token * const next = tok->next;
3134+
return next && next != macro.endToken && next->op == '#' &&
3135+
sameline(tok, next) && next->location.col == tok->location.col + 1U;
3136+
}
3137+
3138+
/**
3139+
* A ## B: paste the last token of result with the first token of operand.
3140+
* An empty operand is a placemarker: pasting it gives the other operand.
3141+
* @param result the replacement so far, it ends with A
3142+
* @param operand B, its tokens are moved to result
3143+
* @param previousEmpty A is a placemarker
3144+
* @param macro the macro
3145+
* @param hashHash the ## in the replacement list
3146+
* @param operandTok the token after ## in the replacement list
3147+
* @throws invalidHashHash if the result is not a valid token
3148+
*/
3149+
static void paste(TokenList &result, TokenList &operand, bool previousEmpty, const Macro &macro, const Token *hashHash, const Token *operandTok) {
3150+
Token * const A = result.back();
3151+
const bool varargs = macro.variadic && !macro.args.empty() && operandTok->str() == macro.args.back();
3152+
if (operand.empty()) {
3153+
// gcc extension: in ", ## __VA_ARGS__" the comma is removed when there are no variable arguments
3154+
if (varargs && A && !previousEmpty && A->op == ',')
3155+
result.deleteToken(A);
3156+
return;
3157+
}
3158+
if (previousEmpty || !A) {
3159+
result.takeTokens(operand);
3160+
return;
3161+
}
3162+
Macro::checkHashHashOperands(hashHash->location, macro.name(), A, operandTok);
3163+
// ", ## __VA_ARGS__" with variable arguments: nothing is pasted
3164+
// a string or character literal followed by a name (user-defined literal) is not pasted either
3165+
if ((varargs && A->op == ',') || isStringLiteral(A->str()) || isCharLiteral(A->str())) {
3166+
result.takeTokens(operand);
3167+
return;
3168+
}
3169+
const Token * const B = operand.cfront();
3170+
Macro::checkPaste(hashHash->location, macro.name(), A, B);
3171+
const std::string strAB = A->str() + B->str();
3172+
Macro::checkUniversalCharacterPaste(hashHash->location, macro.name(), A, strAB);
3173+
A->setstr(strAB);
3174+
A->whitespaceahead = B->whitespaceahead;
3175+
A->macro = macro.name();
3176+
operand.deleteToken(operand.front());
3177+
result.takeTokens(operand);
3178+
}
3179+
31293180
/**
31303181
* The tokens that a token in the replacement list is replaced by: a parameter by its
31313182
* argument, # and a parameter by a string literal, and other tokens by a copy.
@@ -3169,20 +3220,37 @@ namespace simplecpp {
31693220
void substitute(TokenList &result, const Macro &macro, const std::vector<TokenList> &arguments, const std::vector<TokenList> &expandedArguments, const std::set<TokenString> &hideSet, bool whitespaceAfter) const {
31703221
if (macro.variadicOpt)
31713222
throw unsupported(loc, "__VA_OPT__");
3223+
const Token *hashHash = nullptr; // the ## before the current operand
3224+
bool previousEmpty = false; // the previous operand was empty, it is a placemarker
31723225
for (const Token *tok = macro.valueToken; tok != macro.endToken; tok = tok->next) {
3173-
const Token * const next = (tok->next != macro.endToken) ? tok->next : nullptr;
3174-
if (tok->op == '#' && next && next->op == '#')
3175-
throw unsupported(loc, "##");
3226+
if (isHashHash(macro, tok)) {
3227+
if (!result.back() && !previousEmpty)
3228+
throw Macro::invalidHashHash(tok->location, macro.name(), "Missing first argument");
3229+
hashHash = tok;
3230+
tok = tok->next;
3231+
continue;
3232+
}
31763233
if (tok->str() == DEFINED)
31773234
throw unsupported(loc, "defined");
3235+
const Token * const operandTok = tok;
31783236
const bool parameter = getParameter(macro, tok) < arguments.size();
3237+
// an argument is not expanded when it is an operand of ##
3238+
const bool expand = !hashHash && !isHashHash(macro, tok->next);
31793239
TokenList operand(files);
3180-
getOperand(operand, macro, tok, arguments, expandedArguments, true);
3181-
result.takeTokens(operand);
3240+
getOperand(operand, macro, tok, arguments, expandedArguments, expand);
3241+
const bool empty = operand.empty();
3242+
if (hashHash)
3243+
paste(result, operand, previousEmpty, macro, hashHash, operandTok);
3244+
else
3245+
result.takeTokens(operand);
31823246
// the whitespace after a parameter comes after its argument
31833247
if (parameter && tok->whitespaceahead && result.back())
31843248
result.back()->whitespaceahead = true;
3249+
previousEmpty = empty && (hashHash ? previousEmpty : true);
3250+
hashHash = nullptr;
31853251
}
3252+
if (hashHash)
3253+
throw Macro::invalidHashHash::unexpectedNewline(hashHash->location, macro.name());
31863254
for (Token *tok = result.front(); tok; tok = tok->next) {
31873255
tok->location = loc;
31883256
for (const TokenString &name : hideSet)

0 commit comments

Comments
 (0)