Skip to content

Commit 2f44b77

Browse files
authored
Fix #292 (Fail to expand macro, comma in inner macro) (#326)
1 parent b9bfbaa commit 2f44b77

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

simplecpp.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ static const simplecpp::TokenString ONCE("once");
114114

115115
static const simplecpp::TokenString HAS_INCLUDE("__has_include");
116116

117+
static const simplecpp::TokenString INNER_COMMA(",,");
118+
117119
template<class T> static std::string toString(T t)
118120
{
119121
// NOLINTNEXTLINE(misc-const-correctness) - false positive
@@ -1559,6 +1561,10 @@ namespace simplecpp {
15591561
rawtok = rawtok2->next;
15601562
}
15611563
output->takeTokens(output2);
1564+
for (Token* tok = output->front(); tok; tok = tok->next) {
1565+
if (tok->str() == INNER_COMMA)
1566+
tok->setstr(",");
1567+
}
15621568
return rawtok;
15631569
}
15641570

@@ -1733,7 +1739,12 @@ namespace simplecpp {
17331739
if (it != macros.end() && expandedmacros.find(tok->str()) == expandedmacros.end()) {
17341740
const Macro &m = it->second;
17351741
if (!m.functionLike()) {
1742+
Token* mtok = tokens->back();
17361743
m.expand(tokens, rawloc, tok, macros, expandedmacros);
1744+
for (mtok = mtok->next; mtok; mtok = mtok->next) {
1745+
if (mtok->op == ',')
1746+
mtok->setstr(INNER_COMMA);
1747+
}
17371748
expanded = true;
17381749
}
17391750
}

test.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,15 @@ static void define_define_18()
783783
ASSERT_EQUALS("\n\n\n( ( p -> var ) ) ;", preprocess(code));
784784
}
785785

786+
static void define_define_19() // #292
787+
{
788+
const char code[] = "#define X 1,2,3\n"
789+
"#define Foo(A, B) A\n"
790+
"#define Bar Foo(X, 0)\n"
791+
"Bar\n";
792+
ASSERT_EQUALS("\n\n\n1 , 2 , 3", preprocess(code));
793+
}
794+
786795
static void define_va_args_1()
787796
{
788797
const char code[] = "#define A(fmt...) dostuff(fmt)\n"
@@ -1294,7 +1303,7 @@ static void has_include_1()
12941303
static void has_include_2()
12951304
{
12961305
const char code[] = "#if defined( __has_include)\n"
1297-
" #if /*commant*/ __has_include /*comment*/(\"simplecpp.h\") // comment\n"
1306+
" #if /*comment*/ __has_include /*comment*/(\"simplecpp.h\") // comment\n"
12981307
" A\n"
12991308
" #else\n"
13001309
" B\n"
@@ -2680,6 +2689,7 @@ int main(int argc, char **argv)
26802689
TEST_CASE(define_define_16);
26812690
TEST_CASE(define_define_17);
26822691
TEST_CASE(define_define_18);
2692+
TEST_CASE(define_define_19);
26832693
TEST_CASE(define_va_args_1);
26842694
TEST_CASE(define_va_args_2);
26852695
TEST_CASE(define_va_args_3);

0 commit comments

Comments
 (0)