Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1490,8 +1490,12 @@ void simplecpp::TokenList::removeComments()
while (tok) {
Token * const tok1 = tok;
tok = tok->next;
if (tok1->comment)
if (tok1->comment) {
// a comment is replaced by one space character
if (tok1->previous)
tok1->previous->whitespaceahead = true;
Comment thread
danmar marked this conversation as resolved.
Resolved
deleteToken(tok1);
}
}
}

Expand Down Expand Up @@ -2187,7 +2191,14 @@ namespace simplecpp {
if (!parametertokens1.empty())
parametertokens1.swap(parametertokens2);

return functionLike() ? parametertokens2.back()->next : nameTokInst->next;
const Token * const callEnd = functionLike() ? parametertokens2.back() : nameTokInst;

// Whitespace at the end of the replacement list does not count.
// The expansion is followed by the whitespace after the macro call.
if (output.back() && output.back() != output_end_1)
output.back()->whitespaceahead = callEnd->whitespaceahead;

return callEnd->next;
}

/** Returns the macro to expand when the last token of @p temp is the name of a
Expand Down
51 changes: 51 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1731,6 +1731,55 @@ static void hashEscape()
"\"L\\\"a\\\\\\\"b\\\"\"", preprocess(code));
}

static void hashComment()
{
// a comment is replaced by one space character
simplecpp::DUI dui;
dui.removeComments = true;
const char code[] = "#define str(x) #x\n"
"#define xstr(x) str(x)\n"
"str(a/**/b)\n"
"str(a/**/ /**/b)\n"
"str(a/**/)\n"
"xstr(a/**/b)";
ASSERT_EQUALS("\n\n"
"\"a b\"\n"
"\"a b\"\n"
"\"a\"\n"
"\"a b\"", preprocess(code, dui));

// comments are kept in the string when they are not removed, like gcc -C
ASSERT_EQUALS("\n\n"
"\"a/**/b\"\n"
"\"a/**/ /**/b\"\n"
"\"a/**/\"\n"
"\"a/**/b\"", preprocess(code));
}

static void hashMacroTrailingWhitespace()
{
// whitespace at the end of a replacement list does not count
simplecpp::DUI dui;
dui.removeComments = true;
const char code[] = "#define str(x) #x\n"
"#define xstr(x) str(x)\n"
"#define MAJOR 1/*major*/\n"
"#define MINOR 2 \n"
"#define F(x) x\n"
"#define G F(a)\n"
"xstr(MAJOR.MINOR)\n"
"xstr(F(a)b)\n"
"xstr(F(a) b)\n"
"xstr(G.b)\n"
"xstr(G b)";
ASSERT_EQUALS("\n\n\n\n\n\n"
"\"1.2\"\n"
"\"ab\"\n"
"\"a b\"\n"
"\"a.b\"\n"
"\"a b\"", preprocess(code, dui));
}

static void hashEscapeTrailingBackslash()
{
// an odd number of trailing backslashes would escape the closing quote,
Expand Down Expand Up @@ -4836,6 +4885,8 @@ static void runTests(int argc, char **argv, Input input)

TEST_CASE(hash);
TEST_CASE(hashEscape);
TEST_CASE(hashComment);
TEST_CASE(hashMacroTrailingWhitespace);
TEST_CASE(hashEscapeTrailingBackslash);
TEST_CASE(hashhash1);
TEST_CASE(hashhash2);
Expand Down
Loading