Skip to content

Commit 3dbca5f

Browse files
dmcppcheckclaude
andcommitted
##: pasting a name or number with a string or character literal is an error
Only an encoding prefix such as L, u8 or R gives a valid token, as in L ## "s". Before, cat(a,"s") gave the invalid token a"s". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 131c265 commit 3dbca5f

2 files changed

Lines changed: 34 additions & 7 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2710,12 +2710,12 @@ namespace simplecpp {
27102710
strAB = ",";
27112711
}
27122712
else {
2713-
checkPunctuatorPaste(tok->location, name(), A, tokensB.cfront());
2713+
checkPaste(tok->location, name(), A, tokensB.cfront());
27142714
strAB = A->str() + tokensB.cfront()->str();
27152715
tokensB.deleteToken(tokensB.front());
27162716
}
27172717
} else {
2718-
checkPunctuatorPaste(tok->location, name(), A, B);
2718+
checkPaste(tok->location, name(), A, B);
27192719
strAB = A->str() + B->str();
27202720
}
27212721

@@ -2788,16 +2788,21 @@ namespace simplecpp {
27882788
}
27892789

27902790
/**
2791-
* When A is a punctuator, check that pasting A and B gives a valid token.
2791+
* Check that pasting A and B gives a valid token, when A is a punctuator
2792+
* or B is a string or character literal.
27922793
* @param loc the location of the ##
27932794
* @param macroName the macro that is expanded
27942795
* @param A the token before ##
27952796
* @param B the first token after ##, after expansion
27962797
* @throws invalidHashHash if the result is not a valid token
27972798
*/
2798-
static void checkPunctuatorPaste(const Location &loc, const std::string &macroName, const Token *A, const Token *B) {
2799+
static void checkPaste(const Location &loc, const std::string &macroName, const Token *A, const Token *B) {
27992800
if (isPunctuatorOperand(A) && !isValidPunctuatorPaste(A->str() + B->str()))
28002801
throw invalidHashHash::cannotCombine(loc, macroName, A, B);
2802+
// a name or number followed by a literal: only an encoding prefix such as L or u8 is valid
2803+
if ((A->name || A->number) && (isStringLiteral(B->str()) || isCharLiteral(B->str())) &&
2804+
!(A->name && isStringLiteralPrefix(A->str())))
2805+
throw invalidHashHash::cannotCombine(loc, macroName, A, B);
28012806
}
28022807

28032808
/**

‎test.cpp‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2518,9 +2518,29 @@ static void hashhash_invalid_name_string()
25182518
"#define cat(a,b) a##b\n"
25192519
"cat(a,\"s\")\n";
25202520
simplecpp::OutputList outputList;
2521-
// TODO: a"s" is not a valid token, gcc reports an error
2522-
TODO_ASSERT_EQUALS("", "\na\"s\"", preprocess(code, &outputList));
2523-
TODO_ASSERT_EQUALS("file0,1,syntax_error,failed to expand 'cat', Invalid ## usage when expanding 'cat': Combining 'a' and '\"s\"' yields an invalid token.\n", "", toString(outputList));
2521+
ASSERT_EQUALS("", preprocess(code, &outputList));
2522+
ASSERT_EQUALS("file0,1,syntax_error,failed to expand 'cat', Invalid ## usage when expanding 'cat': Combining 'a' and '\"s\"' yields an invalid token.\n", toString(outputList));
2523+
}
2524+
2525+
static void hashhash_invalid_number_string()
2526+
{
2527+
const char code[] =
2528+
"#define cat(a,b) a##b\n"
2529+
"cat(1,\"s\")\n";
2530+
simplecpp::OutputList outputList;
2531+
ASSERT_EQUALS("", preprocess(code, &outputList));
2532+
ASSERT_EQUALS("file0,1,syntax_error,failed to expand 'cat', Invalid ## usage when expanding 'cat': Combining '1' and '\"s\"' yields an invalid token.\n", toString(outputList));
2533+
}
2534+
2535+
static void hashhash_encoding_prefix()
2536+
{
2537+
const char code[] =
2538+
"#define cat(a,b) a##b\n"
2539+
"#define WIDE(s) L ## s\n"
2540+
"cat(L,\"s\") cat(u8,\"s\") cat(R,\"(x)\") cat(L,'c') WIDE(\"s\")\n";
2541+
simplecpp::OutputList outputList;
2542+
ASSERT_EQUALS("\n\nL\"s\" u8\"s\" R\"(x)\" L'c' L\"s\"", preprocess(code, &outputList));
2543+
ASSERT_EQUALS("", toString(outputList));
25242544
}
25252545

25262546
static void hashhash_va_args_unexpected()
@@ -5641,6 +5661,8 @@ static void runTests(int argc, char **argv, Input input)
56415661
TEST_CASE(hashhash_empty_placemarker);
56425662
TEST_CASE(hashhash_invalid_dotdot);
56435663
TEST_CASE(hashhash_invalid_name_string);
5664+
TEST_CASE(hashhash_invalid_number_string);
5665+
TEST_CASE(hashhash_encoding_prefix);
56445666
TEST_CASE(hashhash_va_args_unexpected);
56455667

56465668
// C standard, 5.1.1.2, paragraph 4:

0 commit comments

Comments
 (0)