Skip to content

Commit 8f6fc00

Browse files
dmcppcheckclaude
andcommitted
hash: keep the space after an operator of two characters
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 99e5a99 commit 8f6fc00

2 files changed

Lines changed: 38 additions & 32 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,7 +1364,9 @@ void simplecpp::TokenList::combineOperators()
13641364
if (tok->next && tok->next->op == '.' && tok->next->location.col == (tok->location.col + 1) &&
13651365
tok->next->next && tok->next->next->op == '.' && tok->next->next->location.col == (tok->location.col + 2)) {
13661366
tok->setstr("...");
1367+
tok->whitespaceahead = tok->next->whitespaceahead;
13671368
deleteToken(tok->next);
1369+
tok->whitespaceahead = tok->next->whitespaceahead;
13681370
deleteToken(tok->next);
13691371
continue;
13701372
}
@@ -1374,11 +1376,13 @@ void simplecpp::TokenList::combineOperators()
13741376
deleteToken(tok->previous);
13751377
if (sameline(tok, tok->next) && (isFloatSuffix(tok->next) || (tok->next && tok->next->startsWithOneOf("AaBbCcDdEeFfPp") && !isAlternativeAndBitandBitor(tok->next)))) {
13761378
tok->setstr(tok->str() + tok->next->str());
1379+
tok->whitespaceahead = tok->next->whitespaceahead;
13771380
deleteToken(tok->next);
13781381
}
13791382
}
13801383
if (tok->next && tok->next->number) {
13811384
tok->setstr(tok->str() + tok->next->str());
1385+
tok->whitespaceahead = tok->next->whitespaceahead;
13821386
deleteToken(tok->next);
13831387
}
13841388
}
@@ -1389,7 +1393,9 @@ void simplecpp::TokenList::combineOperators()
13891393
(isHex(tok->str()) && (lastChar == 'P' || lastChar == 'p'))) &&
13901394
tok->next && tok->next->isOneOf("+-") && tok->next->next && tok->next->next->number) {
13911395
tok->setstr(tok->str() + tok->next->op + tok->next->next->str());
1396+
tok->whitespaceahead = tok->next->whitespaceahead;
13921397
deleteToken(tok->next);
1398+
tok->whitespaceahead = tok->next->whitespaceahead;
13931399
deleteToken(tok->next);
13941400
}
13951401

@@ -1406,21 +1412,27 @@ void simplecpp::TokenList::combineOperators()
14061412
if (tok->op == '&' && !executableScope.top() && isAnonymousReferenceParameter(tok))
14071413
continue;
14081414
tok->setstr(tok->str() + "=");
1415+
tok->whitespaceahead = tok->next->whitespaceahead;
14091416
deleteToken(tok->next);
14101417
} else if ((tok->op == '|' || tok->op == '&') && tok->op == tok->next->op) {
14111418
tok->setstr(tok->str() + tok->next->str());
1419+
tok->whitespaceahead = tok->next->whitespaceahead;
14121420
deleteToken(tok->next);
14131421
} else if (tok->op == ':' && tok->next->op == ':') {
14141422
tok->setstr(tok->str() + tok->next->str());
1423+
tok->whitespaceahead = tok->next->whitespaceahead;
14151424
deleteToken(tok->next);
14161425
} else if (tok->op == '-' && tok->next->op == '>') {
14171426
tok->setstr(tok->str() + tok->next->str());
1427+
tok->whitespaceahead = tok->next->whitespaceahead;
14181428
deleteToken(tok->next);
14191429
} else if ((tok->op == '<' || tok->op == '>') && tok->op == tok->next->op) {
14201430
tok->setstr(tok->str() + tok->next->str());
1431+
tok->whitespaceahead = tok->next->whitespaceahead;
14211432
deleteToken(tok->next);
14221433
if (tok->next && tok->next->op == '=' && tok->next->next && tok->next->next->op != '=') {
14231434
tok->setstr(tok->str() + tok->next->str());
1435+
tok->whitespaceahead = tok->next->whitespaceahead;
14241436
deleteToken(tok->next);
14251437
}
14261438
} else if ((tok->op == '+' || tok->op == '-') && tok->op == tok->next->op) {
@@ -1431,6 +1443,7 @@ void simplecpp::TokenList::combineOperators()
14311443
if (tok->next->next && tok->next->next->number)
14321444
continue;
14331445
tok->setstr(tok->str() + tok->next->str());
1446+
tok->whitespaceahead = tok->next->whitespaceahead;
14341447
deleteToken(tok->next);
14351448
}
14361449
}

‎test.cpp‎

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

1914+
static void hashCombinedOperator()
1915+
{
1916+
// the whitespace after an operator of two characters is kept
1917+
const char code[] = "#define str(x) #x\n"
1918+
"str(a == 0) str(a -> b) str(x... y)\n";
1919+
ASSERT_EQUALS("\n\"a == 0\" \"a -> b\" \"x... y\"", preprocess(code));
1920+
}
1921+
19141922
static void hashEscapeTrailingBackslash()
19151923
{
19161924
// an odd number of trailing backslashes would escape the closing quote,
@@ -2427,38 +2435,22 @@ static void macro_c11_example4() // C11 6.10.3.5 EXAMPLE 4
24272435
// the comments are removed before preprocessing, like cppcheck and the simplecpp program do
24282436
simplecpp::DUI dui;
24292437
dui.removeComments = true;
2430-
// TODO: the space before == is lost after the comment and newline in the # argument
2431-
TODO_ASSERT_EQUALS("\n"
2432-
"\n"
2433-
"\n"
2434-
"\n"
2435-
"\n"
2436-
"\n"
2437-
"\n"
2438-
"\n"
2439-
"\n"
2440-
"printf ( \"x\" \"1\" \"= %d, x\" \"2\" \"= %s\" , x1 , x2 ) ;\n"
2441-
"fputs ( \"strncmp(\\\"abc\\\\0d\\\", \\\"abc\\\", '\\\\4') == 0\"\n"
2442-
"\": @\\n\" , s ) ;\n"
2443-
"\"vers2.h\"\n"
2444-
"\"hello\" ;\n"
2445-
"\"hello\" \", world\"",
2446-
"\n"
2447-
"\n"
2448-
"\n"
2449-
"\n"
2450-
"\n"
2451-
"\n"
2452-
"\n"
2453-
"\n"
2454-
"\n"
2455-
"printf ( \"x\" \"1\" \"= %d, x\" \"2\" \"= %s\" , x1 , x2 ) ;\n"
2456-
"fputs ( \"strncmp(\\\"abc\\\\0d\\\", \\\"abc\\\", '\\\\4') ==0\"\n"
2457-
"\": @\\n\" , s ) ;\n"
2458-
"\"vers2.h\"\n"
2459-
"\"hello\" ;\n"
2460-
"\"hello\" \", world\"",
2461-
preprocess(code, dui));
2438+
ASSERT_EQUALS("\n"
2439+
"\n"
2440+
"\n"
2441+
"\n"
2442+
"\n"
2443+
"\n"
2444+
"\n"
2445+
"\n"
2446+
"\n"
2447+
"printf ( \"x\" \"1\" \"= %d, x\" \"2\" \"= %s\" , x1 , x2 ) ;\n"
2448+
"fputs ( \"strncmp(\\\"abc\\\\0d\\\", \\\"abc\\\", '\\\\4') == 0\"\n"
2449+
"\": @\\n\" , s ) ;\n"
2450+
"\"vers2.h\"\n"
2451+
"\"hello\" ;\n"
2452+
"\"hello\" \", world\"",
2453+
preprocess(code, dui));
24622454
}
24632455

24642456
static void macro_c11_example5() // C11 6.10.3.5 EXAMPLE 5
@@ -5660,6 +5652,7 @@ static void runTests(int argc, char **argv, Input input)
56605652
TEST_CASE(hashEscape);
56615653
TEST_CASE(hashComment);
56625654
TEST_CASE(hashMacroTrailingWhitespace);
5655+
TEST_CASE(hashCombinedOperator);
56635656
TEST_CASE(hashEscapeTrailingBackslash);
56645657
TEST_CASE(hashhash1);
56655658
TEST_CASE(hashhash2);

0 commit comments

Comments
 (0)