Match macro argument names with their expansions.

This commit is contained in:
Andrius Štikonas 2021-11-18 22:16:25 +00:00
parent 2ee908ec82
commit 04906fd5b0
2 changed files with 34 additions and 2 deletions

1
cc.h
View File

@ -60,6 +60,7 @@ struct token_list
union
{
struct token_list* arguments;
struct token_list* expansion;
int depth;
int linenumber;
};

View File

@ -754,6 +754,8 @@ struct token_list* maybe_expand(struct token_list* token)
struct macro_list* hold = lookup_macro(token);
struct token_list* hold2;
struct token_list* hold3;
struct token_list* hold4;
if(NULL == token->next)
{
line_error_token(macro_token);
@ -774,9 +776,38 @@ struct token_list* maybe_expand(struct token_list* token)
{
return token->next;
}
hold2 = insert_tokens(token, hold->expansion);
return hold2->next;
/* Match macro arguments with stored names */
hold3 = hold->arguments;
if(NULL != hold3)
{
require('(' == token->s[0], "missing open parenthesis for macro function\n");
token = eat_token(token);
require(NULL != token, "got an EOF terminated macro function\n");
do
{
hold2 = calloc(1, sizeof(struct token_list));
hold2->s = token->s;
hold2->next = hold->arguments->expansion;
hold->arguments->expansion = hold2;
token = eat_token(token);
require(NULL != token, "incomplete macro call\n");
if(token->s[0] == ',')
{
hold->arguments->expansion = reverse_list(hold->arguments->expansion);
hold->arguments = hold->arguments->next;
require(NULL != hold->arguments, "too many arguments in macro call\n");
token = eat_token(token);
require(NULL != token, "incomplete macro call\n");
}
} while(token->s[0] != ')');
hold->arguments->expansion = reverse_list(hold->arguments->expansion);
hold->arguments = hold3;
token = eat_token(token);
}
hold4 = insert_tokens(token, hold->expansion);
return hold4->next;
}
void preprocess()