Skip to content

Commit e6d9457

Browse files
committed
Better handling of # in define (danmar#60)
1 parent 546d085 commit e6d9457

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

simplecpp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,14 +1612,14 @@ namespace simplecpp {
16121612
hashToken = hashToken->next;
16131613
++numberOfHash;
16141614
}
1615-
if (numberOfHash == 4) {
1615+
if (numberOfHash == 4 && tok->next->location.col + 1 == tok->next->next->location.col) {
16161616
// # ## # => ##
16171617
output->push_back(newMacroToken("##", loc, isReplaced(expandedmacros)));
16181618
tok = hashToken;
16191619
continue;
16201620
}
16211621

1622-
if (numberOfHash == 2 && tok->location.col + 1 < tok->next->location.col) {
1622+
if (numberOfHash >= 2 && tok->location.col + 1 < tok->next->location.col) {
16231623
output->push_back(new Token(*tok));
16241624
tok = tok->next;
16251625
continue;

test.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,13 @@ static void hashhash10()
766766
ASSERT_EQUALS("# #", preprocess(code));
767767
}
768768

769+
static void hashhash11()
770+
{
771+
const char code[] = "#define x # # #\n"
772+
"x";
773+
ASSERT_EQUALS("# # #", preprocess(code));
774+
}
775+
769776
static void hashhash_invalid_1()
770777
{
771778
std::istringstream istr("#define f(a) (##x)\nf(1)");
@@ -1930,7 +1937,8 @@ int main(int argc, char **argv)
19301937
TEST_CASE(hashhash7); // # ## # (C standard; 6.10.3.3.p4)
19311938
TEST_CASE(hashhash8);
19321939
TEST_CASE(hashhash9);
1933-
TEST_CASE(hashhash10);
1940+
TEST_CASE(hashhash10); // #108 : #define x # #
1941+
TEST_CASE(hashhash11); // #60: #define x # # #
19341942
TEST_CASE(hashhash_invalid_1);
19351943
TEST_CASE(hashhash_invalid_2);
19361944

0 commit comments

Comments
 (0)