fixed -D option

This commit is contained in:
bellard 2002-01-13 11:07:19 +00:00
parent 9c06595a1e
commit 0b8f352cb7

143
tcc.c
View File

@ -42,7 +42,7 @@
/* preprocessor debug */ /* preprocessor debug */
//#define PP_DEBUG //#define PP_DEBUG
/* amount of virtual memory associate to a section (currently, we do /* amount of virtual memory associated to a section (currently, we do
not realloc them) */ not realloc them) */
#define SECTION_VSIZE (1024 * 1024) #define SECTION_VSIZE (1024 * 1024)
@ -1306,57 +1306,66 @@ void tok_print(int *str)
} }
#endif #endif
/* parse after #define */
void parse_define(void)
{
Sym *s, *first, **ps;
int v, t, *str, len;
v = tok;
/* XXX: should check if same macro (ANSI) */
first = NULL;
t = MACRO_OBJ;
/* '(' must be just after macro definition for MACRO_FUNC */
if (ch == '(') {
next_nomacro();
next_nomacro();
ps = &first;
while (tok != ')') {
if (tok == TOK_DOTS)
tok = TOK___VA_ARGS__;
s = sym_push1(&define_stack, tok | SYM_FIELD, 0, 0);
*ps = s;
ps = &s->next;
next_nomacro();
if (tok != ',')
break;
next_nomacro();
}
t = MACRO_FUNC;
}
str = NULL;
len = 0;
while (1) {
skip_spaces();
if (ch == '\n' || ch == -1)
break;
next_nomacro();
tok_add2(&str, &len, tok, &tokc);
}
tok_add(&str, &len, 0);
#ifdef PP_DEBUG
printf("define %s %d: ", get_tok_str(v, NULL), t);
tok_print(str);
#endif
s = sym_push1(&define_stack, v, t, (int)str);
s->next = first;
}
void preprocess(void) void preprocess(void)
{ {
int size, i, c, v, t, *str, len; int size, i, c;
char buf[1024], *q, *p; char buf[1024], *q, *p;
char buf1[1024]; char buf1[1024];
BufferedFile *f; BufferedFile *f;
Sym **ps, *first, *s; Sym *s;
cinp(); cinp();
next_nomacro(); next_nomacro();
redo: redo:
if (tok == TOK_DEFINE) { if (tok == TOK_DEFINE) {
next_nomacro(); next_nomacro();
v = tok; parse_define();
/* XXX: should check if same macro (ANSI) */
first = NULL;
t = MACRO_OBJ;
/* '(' must be just after macro definition for MACRO_FUNC */
if (ch == '(') {
next_nomacro();
next_nomacro();
ps = &first;
while (tok != ')') {
if (tok == TOK_DOTS)
tok = TOK___VA_ARGS__;
s = sym_push1(&define_stack, tok | SYM_FIELD, 0, 0);
*ps = s;
ps = &s->next;
next_nomacro();
if (tok != ',')
break;
next_nomacro();
}
t = MACRO_FUNC;
}
str = NULL;
len = 0;
while (1) {
skip_spaces();
if (ch == '\n' || ch == -1)
break;
next_nomacro();
tok_add2(&str, &len, tok, &tokc);
}
tok_add(&str, &len, 0);
#ifdef PP_DEBUG
printf("define %s %d: ", get_tok_str(v, NULL), t);
tok_print(str);
#endif
s = sym_push1(&define_stack, v, t, (int)str);
s->next = first;
} else if (tok == TOK_UNDEF) { } else if (tok == TOK_UNDEF) {
next_nomacro(); next_nomacro();
s = sym_find1(&define_stack, tok); s = sym_find1(&define_stack, tok);
@ -5675,46 +5684,40 @@ int tcc_compile_file(const char *filename1)
tcc parser, but would need a custom 'FILE *' */ tcc parser, but would need a custom 'FILE *' */
void define_symbol(const char *sym) void define_symbol(const char *sym)
{ {
TokenSym *ts; char *p;
int *str, len; BufferedFile bf1, *bf = &bf1;
CValue cval;
const char *p;
char buf[256];
p = strchr(sym, '='); pstrcpy(bf->buffer, IO_BUF_SIZE, sym);
p = strchr(bf->buffer, '=');
if (!p) { if (!p) {
pstrcpy(buf, sizeof(buf), sym); /* default value */
p = "1"; pstrcat(bf->buffer, IO_BUF_SIZE, " 1");
} else { } else {
len = p - sym; *p = ' ';
if (len > sizeof(buf) - 1)
len = sizeof(buf) - 1;
memcpy(buf, sym, len);
buf[len] = '\0';
p++;
} }
ts = tok_alloc(buf, 0); /* init file structure */
str = NULL; bf->fd = -1;
len = 0; bf->buf_ptr = bf->buffer;
if (isnum(*p)) { bf->buf_end = bf->buffer + strlen(bf->buffer);
/* integer case */ bf->filename[0] = '\0';
cval.i = atoi(p); bf->line_num = 1;
tok_add2(&str, &len, TOK_CINT, &cval); file = bf;
} else {
/* string case */ include_stack_ptr = include_stack;
cval.ts = tok_alloc(p, 0);
tok_add2(&str, &len, TOK_STR, &cval); /* parse with define parser */
} inp();
tok_add(&str, &len, 0); ch = '\n'; /* needed to parse correctly first preprocessor command */
sym_push1(&define_stack, ts->tok, MACRO_OBJ, (int)str); next_nomacro();
parse_define();
file = NULL;
} }
void undef_symbol(const char *sym) void undef_symbol(const char *sym)
{ {
TokenSym *ts; TokenSym *ts;
Sym *s; Sym *s;
printf("undef %s\n", sym);
ts = tok_alloc(sym, 0); ts = tok_alloc(sym, 0);
s = sym_find1(&define_stack, tok); s = sym_find1(&define_stack, tok);
/* undefine symbol by putting an invalid name */ /* undefine symbol by putting an invalid name */