Skip to content

Commit 131c265

Browse files
dmcppcheckclaude
andcommitted
Move the ## checks out of Macro::expandHashHash()
checkHashHashOperands(), checkPunctuatorPaste() and checkUniversalCharacterPaste() only depend on the tokens, not on the expansion state, so a new expander can use them too. No behaviour change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent eca9122 commit 131c265

1 file changed

Lines changed: 58 additions & 23 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2679,20 +2679,9 @@ namespace simplecpp {
26792679
throw invalidHashHash::unexpectedNewline(tok->location, name());
26802680

26812681
const bool canBeConcatenatedStringOrChar = isStringLiteral(A->str()) || isCharLiteral(A->str());
2682-
// A is a punctuator; the result is checked when B has been expanded
2683-
const bool punctuatorA = (!A->name && !A->number && !A->str().empty() && !canBeConcatenatedStringOrChar);
26842682

26852683
const Token * const B = tok->next->next;
2686-
if (!punctuatorA) {
2687-
if (!B->name && !B->number && B->op && !B->isOneOf("#="))
2688-
throw invalidHashHash::unexpectedToken(tok->location, name(), B);
2689-
if (B->op == '=')
2690-
throw invalidHashHash::cannotCombine(tok->location, name(), A, B);
2691-
}
2692-
2693-
// Superficial check; more in-depth would in theory be possible _after_ expandArg
2694-
if (canBeConcatenatedStringOrChar && (B->number || !B->name))
2695-
throw invalidHashHash::cannotCombine(tok->location, name(), A, B);
2684+
checkHashHashOperands(tok->location, name(), A, B);
26962685

26972686
TokenList tokensB(files);
26982687
const Token *nextTok = B->next;
@@ -2721,24 +2710,16 @@ namespace simplecpp {
27212710
strAB = ",";
27222711
}
27232712
else {
2724-
if (punctuatorA && !isValidPunctuatorPaste(A->str() + tokensB.cfront()->str()))
2725-
throw invalidHashHash::cannotCombine(tok->location, name(), A, tokensB.cfront());
2713+
checkPunctuatorPaste(tok->location, name(), A, tokensB.cfront());
27262714
strAB = A->str() + tokensB.cfront()->str();
27272715
tokensB.deleteToken(tokensB.front());
27282716
}
27292717
} else {
2730-
if (punctuatorA && !isValidPunctuatorPaste(A->str() + B->str()))
2731-
throw invalidHashHash::cannotCombine(tok->location, name(), A, B);
2718+
checkPunctuatorPaste(tok->location, name(), A, B);
27322719
strAB = A->str() + B->str();
27332720
}
27342721

2735-
// producing universal character is undefined behavior
2736-
if (A->previous && A->previous->str() == "\\") {
2737-
if (strAB[0] == 'u' && strAB.size() == 5)
2738-
throw invalidHashHash::universalCharacterUB(tok->location, name(), A, strAB);
2739-
if (strAB[0] == 'U' && strAB.size() == 9)
2740-
throw invalidHashHash::universalCharacterUB(tok->location, name(), A, strAB);
2741-
}
2722+
checkUniversalCharacterPaste(tok->location, name(), A, strAB);
27422723

27432724
if (varargs && tokensB.empty() && tok->previous->str() == ",") {
27442725
output.deleteToken(A);
@@ -2780,6 +2761,60 @@ namespace simplecpp {
27802761
return nextTok;
27812762
}
27822763

2764+
/** A is a punctuator, not a name, number or literal. Pasting it is checked after B is expanded. */
2765+
static bool isPunctuatorOperand(const Token *A) {
2766+
return !A->name && !A->number && !A->str().empty() && !isStringLiteral(A->str()) && !isCharLiteral(A->str());
2767+
}
2768+
2769+
/**
2770+
* Check the operands of A ## B before B is expanded.
2771+
* @param loc the location of the ##
2772+
* @param macroName the macro that is expanded
2773+
* @param A the token before ##, already expanded
2774+
* @param B the token after ## in the macro definition
2775+
* @throws invalidHashHash if A and B can not be pasted
2776+
*/
2777+
static void checkHashHashOperands(const Location &loc, const std::string &macroName, const Token *A, const Token *B) {
2778+
if (!isPunctuatorOperand(A)) {
2779+
if (!B->name && !B->number && B->op && !B->isOneOf("#="))
2780+
throw invalidHashHash::unexpectedToken(loc, macroName, B);
2781+
if (B->op == '=')
2782+
throw invalidHashHash::cannotCombine(loc, macroName, A, B);
2783+
}
2784+
2785+
// Superficial check; more in-depth would in theory be possible _after_ expandArg
2786+
if ((isStringLiteral(A->str()) || isCharLiteral(A->str())) && (B->number || !B->name))
2787+
throw invalidHashHash::cannotCombine(loc, macroName, A, B);
2788+
}
2789+
2790+
/**
2791+
* When A is a punctuator, check that pasting A and B gives a valid token.
2792+
* @param loc the location of the ##
2793+
* @param macroName the macro that is expanded
2794+
* @param A the token before ##
2795+
* @param B the first token after ##, after expansion
2796+
* @throws invalidHashHash if the result is not a valid token
2797+
*/
2798+
static void checkPunctuatorPaste(const Location &loc, const std::string &macroName, const Token *A, const Token *B) {
2799+
if (isPunctuatorOperand(A) && !isValidPunctuatorPaste(A->str() + B->str()))
2800+
throw invalidHashHash::cannotCombine(loc, macroName, A, B);
2801+
}
2802+
2803+
/**
2804+
* Producing a universal character name with ## is undefined behavior.
2805+
* @param loc the location of the ##
2806+
* @param macroName the macro that is expanded
2807+
* @param A the token before ##
2808+
* @param strAB the pasted text
2809+
* @throws invalidHashHash if A follows a backslash and strAB is a universal character name
2810+
*/
2811+
static void checkUniversalCharacterPaste(const Location &loc, const std::string &macroName, const Token *A, const std::string &strAB) {
2812+
if (!A->previous || A->previous->str() != "\\")
2813+
return;
2814+
if ((strAB[0] == 'u' && strAB.size() == 5) || (strAB[0] == 'U' && strAB.size() == 9))
2815+
throw invalidHashHash::universalCharacterUB(loc, macroName, A, strAB);
2816+
}
2817+
27832818
static bool isReplaced(const std::set<std::string> &expandedmacros) {
27842819
// return true if size > 1
27852820
auto it = expandedmacros.cbegin();

0 commit comments

Comments
 (0)