History

fixed -D option

bellard committed 24 years ago

Commit 0b8f3521 parent 9c065951 file · +73 −70Browse files

Unified · Split

tcc.c+73−70
@@ -42,7 +42,7 @@ · 41 unmodified lines
4242 /* preprocessor debug */
4343 //#define PP_DEBUG
4444
45−/* amount of virtual memory associate to a section (currently, we do
45+/* amount of virtual memory associated to a section (currently, we do
4646 not realloc them) */
4747 #define SECTION_VSIZE (1024 * 1024)
4848
@@ -1306,57 +1306,66 @@ void tok_print(int *str) · 1257 unmodified lines
13061306 }
13071307 #endif
13081308
1309+/* parse after #define */
1310+void parse_define(void)
1311+{
1312+ Sym *s, *first, **ps;
1313+ int v, t, *str, len;
1314+
1315+ v = tok;
1316+ /* XXX: should check if same macro (ANSI) */
1317+ first = NULL;
1318+ t = MACRO_OBJ;
1319+ /* '(' must be just after macro definition for MACRO_FUNC */
1320+ if (ch == '(') {
1321+ next_nomacro();
1322+ next_nomacro();
1323+ ps = &first;
1324+ while (tok != ')') {
1325+ if (tok == TOK_DOTS)
1326+ tok = TOK___VA_ARGS__;
1327+ s = sym_push1(&define_stack, tok | SYM_FIELD, 0, 0);
1328+ *ps = s;
1329+ ps = &s->next;
1330+ next_nomacro();
1331+ if (tok != ',')
1332+ break;
1333+ next_nomacro();
1334+ }
1335+ t = MACRO_FUNC;
1336+ }
1337+ str = NULL;
1338+ len = 0;
1339+ while (1) {
1340+ skip_spaces();
1341+ if (ch == '\n' || ch == -1)
1342+ break;
1343+ next_nomacro();
1344+ tok_add2(&str, &len, tok, &tokc);
1345+ }
1346+ tok_add(&str, &len, 0);
1347+#ifdef PP_DEBUG
1348+ printf("define %s %d: ", get_tok_str(v, NULL), t);
1349+ tok_print(str);
1350+#endif
1351+ s = sym_push1(&define_stack, v, t, (int)str);
1352+ s->next = first;
1353+}
1354+
13091355 void preprocess(void)
13101356 {
1311− int size, i, c, v, t, *str, len;
1357+ int size, i, c;
13121358 char buf[1024], *q, *p;
13131359 char buf1[1024];
13141360 BufferedFile *f;
1315− Sym **ps, *first, *s;
1361+ Sym *s;
13161362
13171363 cinp();
13181364 next_nomacro();
13191365 redo:
13201366 if (tok == TOK_DEFINE) {
13211367 next_nomacro();
1322− v = tok;
1323− /* XXX: should check if same macro (ANSI) */
1324− first = NULL;
1325− t = MACRO_OBJ;
1326− /* '(' must be just after macro definition for MACRO_FUNC */
1327− if (ch == '(') {
1328− next_nomacro();
1329− next_nomacro();
1330− ps = &first;
1331− while (tok != ')') {
1332− if (tok == TOK_DOTS)
1333− tok = TOK___VA_ARGS__;
1334− s = sym_push1(&define_stack, tok | SYM_FIELD, 0, 0);
1335− *ps = s;
1336− ps = &s->next;
1337− next_nomacro();
1338− if (tok != ',')
1339− break;
1340− next_nomacro();
1341− }
1342− t = MACRO_FUNC;
1343− }
1344− str = NULL;
1345− len = 0;
1346− while (1) {
1347− skip_spaces();
1348− if (ch == '\n' || ch == -1)
1349− break;
1350− next_nomacro();
1351− tok_add2(&str, &len, tok, &tokc);
1352− }
1353− tok_add(&str, &len, 0);
1354−#ifdef PP_DEBUG
1355− printf("define %s %d: ", get_tok_str(v, NULL), t);
1356− tok_print(str);
1357−#endif
1358− s = sym_push1(&define_stack, v, t, (int)str);
1359− s->next = first;
1368+ parse_define();
13601369 } else if (tok == TOK_UNDEF) {
13611370 next_nomacro();
13621371 s = sym_find1(&define_stack, tok);
@@ -5675,46 +5684,40 @@ int tcc_compile_file(const char *filename1) · 4312 unmodified lines
56755684 tcc parser, but would need a custom 'FILE *' */
56765685 void define_symbol(const char *sym)
56775686 {
5678− TokenSym *ts;
5679− int *str, len;
5680− CValue cval;
5681− const char *p;
5682− char buf[256];
5687+ char *p;
5688+ BufferedFile bf1, *bf = &bf1;
56835689
5684− p = strchr(sym, '=');
5690+ pstrcpy(bf->buffer, IO_BUF_SIZE, sym);
5691+ p = strchr(bf->buffer, '=');
56855692 if (!p) {
5686− pstrcpy(buf, sizeof(buf), sym);
5687− p = "1";
5693+ /* default value */
5694+ pstrcat(bf->buffer, IO_BUF_SIZE, " 1");
56885695 } else {
5689− len = p - sym;
5690− if (len > sizeof(buf) - 1)
5691− len = sizeof(buf) - 1;
5692− memcpy(buf, sym, len);
5693− buf[len] = '\0';
5694− p++;
5696+ *p = ' ';
56955697 }
56965698
5697− ts = tok_alloc(buf, 0);
5698− str = NULL;
5699− len = 0;
5700− if (isnum(*p)) {
5701− /* integer case */
5702− cval.i = atoi(p);
5703− tok_add2(&str, &len, TOK_CINT, &cval);
5704− } else {
5705− /* string case */
5706− cval.ts = tok_alloc(p, 0);
5707− tok_add2(&str, &len, TOK_STR, &cval);
5708− }
5709− tok_add(&str, &len, 0);
5710− sym_push1(&define_stack, ts->tok, MACRO_OBJ, (int)str);
5699+ /* init file structure */
5700+ bf->fd = -1;
5701+ bf->buf_ptr = bf->buffer;
5702+ bf->buf_end = bf->buffer + strlen(bf->buffer);
5703+ bf->filename[0] = '\0';
5704+ bf->line_num = 1;
5705+ file = bf;
5706+
5707+ include_stack_ptr = include_stack;
5708+
5709+ /* parse with define parser */
5710+ inp();
5711+ ch = '\n'; /* needed to parse correctly first preprocessor command */
5712+ next_nomacro();
5713+ parse_define();
5714+ file = NULL;
57115715 }
57125716
57135717 void undef_symbol(const char *sym)
57145718 {
57155719 TokenSym *ts;
57165720 Sym *s;
5717− printf("undef %s\n", sym);
57185721 ts = tok_alloc(sym, 0);
57195722 s = sym_find1(&define_stack, tok);
57205723 /* undefine symbol by putting an invalid name */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t