Skip to content

Commit 6a9acd6

Browse files
dmcppcheckclaude
andcommitted
#: keep the space after an operator of two characters
combineOperators() did not copy whitespaceahead from the second character, so str(a == 0) gave "a ==0" (C11 6.10.3.5 EXAMPLE 4). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 191359d commit 6a9acd6

2 files changed

Lines changed: 27 additions & 33 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1352,10 +1352,11 @@ static bool isAnonymousReferenceParameter(const simplecpp::Token *tok)
13521352
return false;
13531353
}
13541354

1355-
/** Append the next token to tok, and delete the next token */
1355+
/** Append the next token to tok, and delete the next token. tok gets the whitespace after the next token. */
13561356
static void combineWithNext(simplecpp::TokenList &tokens, simplecpp::Token *tok)
13571357
{
13581358
tok->setstr(tok->str() + tok->next->str());
1359+
tok->whitespaceahead = tok->next->whitespaceahead;
13591360
tokens.deleteToken(tok->next);
13601361
}
13611362

‎test.cpp‎

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,6 +1867,14 @@ static void hashMacroTrailingWhitespace()
18671867
"\"a b\"", preprocess(code, dui));
18681868
}
18691869

1870+
static void hashCombinedOperator()
1871+
{
1872+
// the whitespace after an operator of two characters is kept
1873+
const char code[] = "#define str(x) #x\n"
1874+
"str(a == 0) str(a -> b) str(x... y)\n";
1875+
ASSERT_EQUALS("\n\"a == 0\" \"a -> b\" \"x... y\"", preprocess(code));
1876+
}
1877+
18701878
static void hashEscapeTrailingBackslash()
18711879
{
18721880
// an odd number of trailing backslashes would escape the closing quote,
@@ -2383,38 +2391,22 @@ static void macro_c11_example4() // C11 6.10.3.5 EXAMPLE 4
23832391
// the comments are removed before preprocessing, like cppcheck and the simplecpp program do
23842392
simplecpp::DUI dui;
23852393
dui.removeComments = true;
2386-
// TODO: the space before == is lost after the comment and newline in the # argument
2387-
TODO_ASSERT_EQUALS("\n"
2388-
"\n"
2389-
"\n"
2390-
"\n"
2391-
"\n"
2392-
"\n"
2393-
"\n"
2394-
"\n"
2395-
"\n"
2396-
"printf ( \"x\" \"1\" \"= %d, x\" \"2\" \"= %s\" , x1 , x2 ) ;\n"
2397-
"fputs ( \"strncmp(\\\"abc\\\\0d\\\", \\\"abc\\\", '\\\\4') == 0\"\n"
2398-
"\": @\\n\" , s ) ;\n"
2399-
"\"vers2.h\"\n"
2400-
"\"hello\" ;\n"
2401-
"\"hello\" \", world\"",
2402-
"\n"
2403-
"\n"
2404-
"\n"
2405-
"\n"
2406-
"\n"
2407-
"\n"
2408-
"\n"
2409-
"\n"
2410-
"\n"
2411-
"printf ( \"x\" \"1\" \"= %d, x\" \"2\" \"= %s\" , x1 , x2 ) ;\n"
2412-
"fputs ( \"strncmp(\\\"abc\\\\0d\\\", \\\"abc\\\", '\\\\4') ==0\"\n"
2413-
"\": @\\n\" , s ) ;\n"
2414-
"\"vers2.h\"\n"
2415-
"\"hello\" ;\n"
2416-
"\"hello\" \", world\"",
2417-
preprocess(code, dui));
2394+
ASSERT_EQUALS("\n"
2395+
"\n"
2396+
"\n"
2397+
"\n"
2398+
"\n"
2399+
"\n"
2400+
"\n"
2401+
"\n"
2402+
"\n"
2403+
"printf ( \"x\" \"1\" \"= %d, x\" \"2\" \"= %s\" , x1 , x2 ) ;\n"
2404+
"fputs ( \"strncmp(\\\"abc\\\\0d\\\", \\\"abc\\\", '\\\\4') == 0\"\n"
2405+
"\": @\\n\" , s ) ;\n"
2406+
"\"vers2.h\"\n"
2407+
"\"hello\" ;\n"
2408+
"\"hello\" \", world\"",
2409+
preprocess(code, dui));
24182410
}
24192411

24202412
static void macro_c11_example5() // C11 6.10.3.5 EXAMPLE 5
@@ -5625,6 +5617,7 @@ static void runTests(int argc, char **argv, Input input)
56255617
TEST_CASE(hashEscape);
56265618
TEST_CASE(hashComment);
56275619
TEST_CASE(hashMacroTrailingWhitespace);
5620+
TEST_CASE(hashCombinedOperator);
56285621
TEST_CASE(hashEscapeTrailingBackslash);
56295622
TEST_CASE(hashhash1);
56305623
TEST_CASE(hashhash2);

0 commit comments

Comments
 (0)