Skip to content

Commit 768e6c3

Browse files
authored
Fix #718 (# operator drops the space that a comment stands for) (#719)
1 parent 9fedb7d commit 768e6c3

2 files changed

Lines changed: 64 additions & 2 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 13 additions & 2 deletions
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

@@ -2187,7 +2191,14 @@ namespace simplecpp {
21872191
if (!parametertokens1.empty())
21882192
parametertokens1.swap(parametertokens2);
21892193

2190-
return functionLike() ? parametertokens2.back()->next : nameTokInst->next;
2194+
const Token * const callEnd = functionLike() ? parametertokens2.back() : nameTokInst;
2195+
2196+
// Whitespace at the end of the replacement list does not count.
2197+
// The expansion is followed by the whitespace after the macro call.
2198+
if (output.back() && output.back() != output_end_1)
2199+
output.back()->whitespaceahead = callEnd->whitespaceahead;
2200+
2201+
return callEnd->next;
21912202
}
21922203

21932204
/** Returns the macro to expand when the last token of @p temp is the name of a

‎test.cpp‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,6 +1731,55 @@ 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+
1759+
static void hashMacroTrailingWhitespace()
1760+
{
1761+
// whitespace at the end of a replacement list does not count
1762+
simplecpp::DUI dui;
1763+
dui.removeComments = true;
1764+
const char code[] = "#define str(x) #x\n"
1765+
"#define xstr(x) str(x)\n"
1766+
"#define MAJOR 1/*major*/\n"
1767+
"#define MINOR 2 \n"
1768+
"#define F(x) x\n"
1769+
"#define G F(a)\n"
1770+
"xstr(MAJOR.MINOR)\n"
1771+
"xstr(F(a)b)\n"
1772+
"xstr(F(a) b)\n"
1773+
"xstr(G.b)\n"
1774+
"xstr(G b)";
1775+
ASSERT_EQUALS("\n\n\n\n\n\n"
1776+
"\"1.2\"\n"
1777+
"\"ab\"\n"
1778+
"\"a b\"\n"
1779+
"\"a.b\"\n"
1780+
"\"a b\"", preprocess(code, dui));
1781+
}
1782+
17341783
static void hashEscapeTrailingBackslash()
17351784
{
17361785
// an odd number of trailing backslashes would escape the closing quote,
@@ -4836,6 +4885,8 @@ static void runTests(int argc, char **argv, Input input)
48364885

48374886
TEST_CASE(hash);
48384887
TEST_CASE(hashEscape);
4888+
TEST_CASE(hashComment);
4889+
TEST_CASE(hashMacroTrailingWhitespace);
48394890
TEST_CASE(hashEscapeTrailingBackslash);
48404891
TEST_CASE(hashhash1);
48414892
TEST_CASE(hashhash2);

0 commit comments

Comments
 (0)