Skip to content

Commit 2e16061

Browse files
committed
Fix #718 (# operator drops the space that a comment stands for)
'str(a/**/b)' gave "ab" instead of "a b". When comments are removed, mark the previous token as followed by whitespace.
1 parent 9fedb7d commit 2e16061

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

‎simplecpp.cpp‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1490,8 +1490,12 @@ void simplecpp::TokenList::removeComments()
14901490
while (tok) {
14911491
Token * const tok1 = tok;
14921492
tok = tok->next;
1493-
if (tok1->comment)
1493+
if (tok1->comment) {
1494+
// a comment is replaced by one space character
1495+
if (tok1->previous)
1496+
tok1->previous->whitespaceahead = true;
14941497
deleteToken(tok1);
1498+
}
14951499
}
14961500
}
14971501

‎test.cpp‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,6 +1731,31 @@ static void hashEscape()
17311731
"\"L\\\"a\\\\\\\"b\\\"\"", preprocess(code));
17321732
}
17331733

1734+
static void hashComment()
1735+
{
1736+
// a comment is replaced by one space character
1737+
simplecpp::DUI dui;
1738+
dui.removeComments = true;
1739+
const char code[] = "#define str(x) #x\n"
1740+
"#define xstr(x) str(x)\n"
1741+
"str(a/**/b)\n"
1742+
"str(a/**/ /**/b)\n"
1743+
"str(a/**/)\n"
1744+
"xstr(a/**/b)";
1745+
ASSERT_EQUALS("\n\n"
1746+
"\"a b\"\n"
1747+
"\"a b\"\n"
1748+
"\"a\"\n"
1749+
"\"a b\"", preprocess(code, dui));
1750+
1751+
// comments are kept in the string when they are not removed, like gcc -C
1752+
ASSERT_EQUALS("\n\n"
1753+
"\"a/**/b\"\n"
1754+
"\"a/**/ /**/b\"\n"
1755+
"\"a/**/\"\n"
1756+
"\"a/**/b\"", preprocess(code));
1757+
}
1758+
17341759
static void hashEscapeTrailingBackslash()
17351760
{
17361761
// an odd number of trailing backslashes would escape the closing quote,
@@ -4836,6 +4861,7 @@ static void runTests(int argc, char **argv, Input input)
48364861

48374862
TEST_CASE(hash);
48384863
TEST_CASE(hashEscape);
4864+
TEST_CASE(hashComment);
48394865
TEST_CASE(hashEscapeTrailingBackslash);
48404866
TEST_CASE(hashhash1);
48414867
TEST_CASE(hashhash2);

0 commit comments

Comments
 (0)