Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #292 (Fail to expand macro, comma in inner macro) #326

Merged
merged 1 commit into from
Oct 5, 2023
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
11 changes: 11 additions & 0 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ static const simplecpp::TokenString ONCE("once");

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

static const simplecpp::TokenString INNER_COMMA(",,");

template<class T> static std::string toString(T t)
{
// NOLINTNEXTLINE(misc-const-correctness) - false positive
Expand Down Expand Up @@ -1559,6 +1561,10 @@ namespace simplecpp {
rawtok = rawtok2->next;
}
output->takeTokens(output2);
for (Token* tok = output->front(); tok; tok = tok->next) {
if (tok->str() == INNER_COMMA)
tok->setstr(",");
}
return rawtok;
}

Expand Down Expand Up @@ -1733,7 +1739,12 @@ namespace simplecpp {
if (it != macros.end() && expandedmacros.find(tok->str()) == expandedmacros.end()) {
const Macro &m = it->second;
if (!m.functionLike()) {
Token* mtok = tokens->back();
m.expand(tokens, rawloc, tok, macros, expandedmacros);
for (mtok = mtok->next; mtok; mtok = mtok->next) {
if (mtok->op == ',')
mtok->setstr(INNER_COMMA);
}
expanded = true;
}
}
Expand Down
12 changes: 11 additions & 1 deletion test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,15 @@ static void define_define_18()
ASSERT_EQUALS("\n\n\n( ( p -> var ) ) ;", preprocess(code));
}

static void define_define_19() // #292
{
const char code[] = "#define X 1,2,3\n"
"#define Foo(A, B) A\n"
"#define Bar Foo(X, 0)\n"
"Bar\n";
ASSERT_EQUALS("\n\n\n1 , 2 , 3", preprocess(code));
}

static void define_va_args_1()
{
const char code[] = "#define A(fmt...) dostuff(fmt)\n"
Expand Down Expand Up @@ -1294,7 +1303,7 @@ static void has_include_1()
static void has_include_2()
{
const char code[] = "#if defined( __has_include)\n"
" #if /*commant*/ __has_include /*comment*/(\"simplecpp.h\") // comment\n"
" #if /*comment*/ __has_include /*comment*/(\"simplecpp.h\") // comment\n"
" A\n"
" #else\n"
" B\n"
Expand Down Expand Up @@ -2680,6 +2689,7 @@ int main(int argc, char **argv)
TEST_CASE(define_define_16);
TEST_CASE(define_define_17);
TEST_CASE(define_define_18);
TEST_CASE(define_define_19);
TEST_CASE(define_va_args_1);
TEST_CASE(define_va_args_2);
TEST_CASE(define_va_args_3);
Expand Down