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
30 changes: 30 additions & 0 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -2671,6 +2671,36 @@ basic_block_t *read_body_statement(block_t *parent, basic_block_t *bb)
if (!lex_peek(T_identifier, token))
error("Unexpected token");

/* handle macro parameter substitution for statements */
int macro_param_idx = find_macro_param_src_idx(token, parent);
if (macro_param_idx && parent->macro) {
/* save current state */
int saved_size = SOURCE->size;
char saved_char = next_char;
int saved_token = next_token;

/* jump to parameter value */
SOURCE->size = macro_param_idx;
next_char = SOURCE->elements[SOURCE->size];
next_token = lex_token();

/* extract the parameter value as identifier token */
if (lex_peek(T_identifier, token)) {
lex_expect(T_identifier);
} else {
/* parameter is not a simple identifier, restore state and continue
*/
SOURCE->size = saved_size;
next_char = saved_char;
next_token = saved_token;
}

/* restore source position */
SOURCE->size = saved_size;
next_char = saved_char;
next_token = saved_token;
}

/* is it a variable declaration? */
int find_type_flag = lex_accept(T_struct) ? 2 : 1;
type = find_type(token, find_type_flag);
Expand Down
43 changes: 43 additions & 0 deletions tests/driver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,49 @@ int main()
}
EOF

# macro parameter substitution works in expression contexts
try_ 15 << EOF
#define ADD_PARAMS(a, b) ((a) + (b))
int main()
{
int x = 5, y = 10;
return ADD_PARAMS(x, y);
}
EOF

# macro with assignment operators
try_ 18 << EOF
#define ASSIGN_MACRO(variable, val) \
variable = variable + val + 10
int main()
{
int x = 5;
ASSIGN_MACRO(x, 3);
return x;
}
EOF

try_ 27 << EOF
#define COMPOUND_ASSIGN(variable, val) \
variable += val + 10
int main()
{
int y = 10;
COMPOUND_ASSIGN(y, 7);
return y;
}
EOF

try_ 42 << EOF
#define SET_VAR(var, value) var = value
int main()
{
int z = 0;
SET_VAR(z, 42);
return z;
}
EOF

try_output 0 "Wrapper: Hello World!" << EOF
#define WRAPPER(...) \
do { \
Expand Down