@@ -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+
17341783static 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