VLA code: minor fix
[tinycc.git] / tccpp.c
Commit [+]AuthorDateLineData
a93bcdff grischka2009-05-05 20:17:11 +02001/*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
88a3ccab grischka2009-12-20 01:53:49 +010021#include "tcc.h"
22
23/********************************************************/
24/* global variables */
25
26ST_DATA int tok_flags;
88a3ccab grischka2009-12-20 01:53:49 +010027ST_DATA int parse_flags;
88a3ccab grischka2009-12-20 01:53:49 +010028
29ST_DATA struct BufferedFile *file;
30ST_DATA int ch, tok;
31ST_DATA CValue tokc;
4e5170d4 grischka2010-01-14 20:58:03 +010032ST_DATA const int *macro_ptr;
88a3ccab grischka2009-12-20 01:53:49 +010033ST_DATA CString tokcstr; /* current parsed string, if any */
34
35/* display benchmark infos */
36ST_DATA int total_lines;
37ST_DATA int total_bytes;
38ST_DATA int tok_ident;
39ST_DATA TokenSym **table_ident;
40
41/* ------------------------------------------------------------------------- */
42
43static int *macro_ptr_allocated;
4e5170d4 grischka2010-01-14 20:58:03 +010044static const int *unget_saved_macro_ptr;
88a3ccab grischka2009-12-20 01:53:49 +010045static int unget_saved_buffer[TOK_MAX_SIZE + 1];
46static int unget_buffer_enabled;
47static TokenSym *hash_ident[TOK_HASH_SIZE];
48static char token_buf[STRING_MAX_SIZE + 1];
49/* true if isid(c) || isnum(c) */
50static unsigned char isidnum_table[256-CH_EOF];
b8f6e1ae grischka2009-05-05 20:30:39 +020051
52static const char tcc_keywords[] =
53#define DEF(id, str) str "\0"
54#include "tcctok.h"
55#undef DEF
56;
57
58/* WARNING: the content of this string encodes token numbers */
c998985c grischka2009-07-18 22:07:42 +020059static const unsigned char tok_two_chars[] =
0ac8aaab grischka2014-03-29 16:40:54 +010060/* outdated -- gr
c998985c grischka2009-07-18 22:07:42 +020061 "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
62 "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
0ac8aaab grischka2014-03-29 16:40:54 +010063*/{
64 '<','=', TOK_LE,
65 '>','=', TOK_GE,
66 '!','=', TOK_NE,
67 '&','&', TOK_LAND,
68 '|','|', TOK_LOR,
69 '+','+', TOK_INC,
70 '-','-', TOK_DEC,
71 '=','=', TOK_EQ,
72 '<','<', TOK_SHL,
a620b12d Vincent Lefevre2014-03-31 15:24:32 +020073 '>','>', TOK_SAR,
0ac8aaab grischka2014-03-29 16:40:54 +010074 '+','=', TOK_A_ADD,
75 '-','=', TOK_A_SUB,
76 '*','=', TOK_A_MUL,
77 '/','=', TOK_A_DIV,
78 '%','=', TOK_A_MOD,
79 '&','=', TOK_A_AND,
80 '^','=', TOK_A_XOR,
81 '|','=', TOK_A_OR,
82 '-','>', TOK_ARROW,
83 '.','.', 0xa8, // C++ token ?
84 '#','#', TOK_TWOSHARPS,
85 0
86};
b8f6e1ae grischka2009-05-05 20:30:39 +020087
0a35f9d6 grischka2009-05-11 18:45:56 +020088struct macro_level {
89 struct macro_level *prev;
4e5170d4 grischka2010-01-14 20:58:03 +010090 const int *p;
0a35f9d6 grischka2009-05-11 18:45:56 +020091};
92
0a35f9d6 grischka2009-05-11 18:45:56 +020093static void next_nomacro_spc(void);
88a3ccab grischka2009-12-20 01:53:49 +010094static void macro_subst(
95 TokenString *tok_str,
96 Sym **nested_list,
97 const int *macro_str,
98 struct macro_level **can_read_stream
99 );
100
101ST_FUNC void skip(int c)
102{
103 if (tok != c)
bf374a5f grischka2011-08-11 17:07:56 +0200104 tcc_error("'%c' expected (got \"%s\")", c, get_tok_str(tok, &tokc));
88a3ccab grischka2009-12-20 01:53:49 +0100105 next();
106}
0a35f9d6 grischka2009-05-11 18:45:56 +0200107
74a24d77 grischka2011-08-11 16:55:30 +0200108ST_FUNC void expect(const char *msg)
109{
bf374a5f grischka2011-08-11 17:07:56 +0200110 tcc_error("%s expected", msg);
74a24d77 grischka2011-08-11 16:55:30 +0200111}
112
88a3ccab grischka2009-12-20 01:53:49 +0100113/* ------------------------------------------------------------------------- */
114/* CString handling */
115static void cstr_realloc(CString *cstr, int new_size)
116{
117 int size;
118 void *data;
119
120 size = cstr->size_allocated;
121 if (size == 0)
122 size = 8; /* no need to allocate a too small first string */
123 while (size < new_size)
124 size = size * 2;
125 data = tcc_realloc(cstr->data_allocated, size);
88a3ccab grischka2009-12-20 01:53:49 +0100126 cstr->data_allocated = data;
127 cstr->size_allocated = size;
128 cstr->data = data;
129}
130
131/* add a byte */
05108a3b grischka2013-02-12 19:13:28 +0100132ST_FUNC void cstr_ccat(CString *cstr, int ch)
88a3ccab grischka2009-12-20 01:53:49 +0100133{
134 int size;
135 size = cstr->size + 1;
136 if (size > cstr->size_allocated)
137 cstr_realloc(cstr, size);
138 ((unsigned char *)cstr->data)[size - 1] = ch;
139 cstr->size = size;
140}
141
05108a3b grischka2013-02-12 19:13:28 +0100142ST_FUNC void cstr_cat(CString *cstr, const char *str)
88a3ccab grischka2009-12-20 01:53:49 +0100143{
144 int c;
145 for(;;) {
146 c = *str;
147 if (c == '\0')
148 break;
149 cstr_ccat(cstr, c);
150 str++;
151 }
152}
0a35f9d6 grischka2009-05-11 18:45:56 +0200153
88a3ccab grischka2009-12-20 01:53:49 +0100154/* add a wide char */
05108a3b grischka2013-02-12 19:13:28 +0100155ST_FUNC void cstr_wccat(CString *cstr, int ch)
88a3ccab grischka2009-12-20 01:53:49 +0100156{
157 int size;
158 size = cstr->size + sizeof(nwchar_t);
159 if (size > cstr->size_allocated)
160 cstr_realloc(cstr, size);
161 *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
162 cstr->size = size;
163}
164
05108a3b grischka2013-02-12 19:13:28 +0100165ST_FUNC void cstr_new(CString *cstr)
88a3ccab grischka2009-12-20 01:53:49 +0100166{
167 memset(cstr, 0, sizeof(CString));
168}
169
170/* free string and reset it to NULL */
05108a3b grischka2013-02-12 19:13:28 +0100171ST_FUNC void cstr_free(CString *cstr)
88a3ccab grischka2009-12-20 01:53:49 +0100172{
173 tcc_free(cstr->data_allocated);
174 cstr_new(cstr);
175}
176
8eb92e60 Kirill Smelkov2012-12-21 12:21:59 +0400177/* reset string to empty */
05108a3b grischka2013-02-12 19:13:28 +0100178ST_FUNC void cstr_reset(CString *cstr)
8eb92e60
KS
Kirill Smelkov2012-12-21 12:21:59 +0400179{
180 cstr->size = 0;
181}
182
88a3ccab grischka2009-12-20 01:53:49 +0100183/* XXX: unicode ? */
4274c44d grischka2012-04-18 18:32:37 +0200184static void add_char(CString *cstr, int c)
88a3ccab grischka2009-12-20 01:53:49 +0100185{
186 if (c == '\'' || c == '\"' || c == '\\') {
187 /* XXX: could be more precise if char or string */
188 cstr_ccat(cstr, '\\');
189 }
190 if (c >= 32 && c <= 126) {
191 cstr_ccat(cstr, c);
192 } else {
193 cstr_ccat(cstr, '\\');
194 if (c == '\n') {
195 cstr_ccat(cstr, 'n');
196 } else {
197 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
198 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
199 cstr_ccat(cstr, '0' + (c & 7));
200 }
201 }
202}
203
204/* ------------------------------------------------------------------------- */
b8f6e1ae grischka2009-05-05 20:30:39 +0200205/* allocate a new token */
206static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
207{
208 TokenSym *ts, **ptable;
209 int i;
210
211 if (tok_ident >= SYM_FIRST_ANOM)
2bd0daab grischka2014-01-06 19:56:26 +0100212 tcc_error("memory full (symbols)");
b8f6e1ae grischka2009-05-05 20:30:39 +0200213
214 /* expand token table if needed */
215 i = tok_ident - TOK_IDENT;
216 if ((i % TOK_ALLOC_INCR) == 0) {
217 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
b8f6e1ae grischka2009-05-05 20:30:39 +0200218 table_ident = ptable;
219 }
220
221 ts = tcc_malloc(sizeof(TokenSym) + len);
222 table_ident[i] = ts;
223 ts->tok = tok_ident++;
224 ts->sym_define = NULL;
225 ts->sym_label = NULL;
226 ts->sym_struct = NULL;
227 ts->sym_identifier = NULL;
228 ts->len = len;
229 ts->hash_next = NULL;
230 memcpy(ts->str, str, len);
231 ts->str[len] = '\0';
232 *pts = ts;
233 return ts;
234}
235
236#define TOK_HASH_INIT 1
237#define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
238
239/* find a token and add it if not found */
88a3ccab grischka2009-12-20 01:53:49 +0100240ST_FUNC TokenSym *tok_alloc(const char *str, int len)
b8f6e1ae grischka2009-05-05 20:30:39 +0200241{
242 TokenSym *ts, **pts;
243 int i;
244 unsigned int h;
245
246 h = TOK_HASH_INIT;
247 for(i=0;i<len;i++)
248 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
249 h &= (TOK_HASH_SIZE - 1);
250
251 pts = &hash_ident[h];
252 for(;;) {
253 ts = *pts;
254 if (!ts)
255 break;
256 if (ts->len == len && !memcmp(ts->str, str, len))
257 return ts;
258 pts = &(ts->hash_next);
259 }
260 return tok_alloc_new(pts, str, len);
261}
262
263/* XXX: buffer overflow */
264/* XXX: float tokens */
88a3ccab grischka2009-12-20 01:53:49 +0100265ST_FUNC char *get_tok_str(int v, CValue *cv)
b8f6e1ae grischka2009-05-05 20:30:39 +0200266{
267 static char buf[STRING_MAX_SIZE + 1];
268 static CString cstr_buf;
269 CString *cstr;
b8f6e1ae grischka2009-05-05 20:30:39 +0200270 char *p;
271 int i, len;
272
273 /* NOTE: to go faster, we give a fixed buffer for small strings */
274 cstr_reset(&cstr_buf);
275 cstr_buf.data = buf;
276 cstr_buf.size_allocated = sizeof(buf);
277 p = buf;
278
0ac8aaab grischka2014-03-29 16:40:54 +0100279/* just an explanation, should never happen:
280 if (v <= TOK_LINENUM && v >= TOK_CINT && cv == NULL)
281 tcc_error("internal error: get_tok_str"); */
282
b8f6e1ae grischka2009-05-05 20:30:39 +0200283 switch(v) {
284 case TOK_CINT:
285 case TOK_CUINT:
286 /* XXX: not quite exact, but only useful for testing */
287 sprintf(p, "%u", cv->ui);
288 break;
289 case TOK_CLLONG:
290 case TOK_CULLONG:
291 /* XXX: not quite exact, but only useful for testing */
035918ef grischka2009-07-18 22:05:58 +0200292#ifdef _WIN32
293 sprintf(p, "%u", (unsigned)cv->ull);
294#else
fdb3b10d Thomas Preud'homme2014-03-08 18:36:02 +0800295 sprintf(p, "%llu", cv->ull);
035918ef grischka2009-07-18 22:05:58 +0200296#endif
b8f6e1ae grischka2009-05-05 20:30:39 +0200297 break;
298 case TOK_LCHAR:
299 cstr_ccat(&cstr_buf, 'L');
300 case TOK_CCHAR:
301 cstr_ccat(&cstr_buf, '\'');
302 add_char(&cstr_buf, cv->i);
303 cstr_ccat(&cstr_buf, '\'');
304 cstr_ccat(&cstr_buf, '\0');
305 break;
306 case TOK_PPNUM:
307 cstr = cv->cstr;
308 len = cstr->size - 1;
309 for(i=0;i<len;i++)
310 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
311 cstr_ccat(&cstr_buf, '\0');
312 break;
313 case TOK_LSTR:
314 cstr_ccat(&cstr_buf, 'L');
315 case TOK_STR:
316 cstr = cv->cstr;
317 cstr_ccat(&cstr_buf, '\"');
318 if (v == TOK_STR) {
319 len = cstr->size - 1;
320 for(i=0;i<len;i++)
321 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
322 } else {
323 len = (cstr->size / sizeof(nwchar_t)) - 1;
324 for(i=0;i<len;i++)
325 add_char(&cstr_buf, ((nwchar_t *)cstr->data)[i]);
326 }
327 cstr_ccat(&cstr_buf, '\"');
328 cstr_ccat(&cstr_buf, '\0');
329 break;
0ac8aaab grischka2014-03-29 16:40:54 +0100330
331 case TOK_CFLOAT:
332 case TOK_CDOUBLE:
333 case TOK_CLDOUBLE:
334 case TOK_LINENUM:
335 return NULL; /* should not happen */
336
337 /* above tokens have value, the ones below don't */
338
b8f6e1ae grischka2009-05-05 20:30:39 +0200339 case TOK_LT:
340 v = '<';
341 goto addv;
342 case TOK_GT:
343 v = '>';
344 goto addv;
345 case TOK_DOTS:
346 return strcpy(p, "...");
347 case TOK_A_SHL:
348 return strcpy(p, "<<=");
349 case TOK_A_SAR:
350 return strcpy(p, ">>=");
351 default:
352 if (v < TOK_IDENT) {
353 /* search in two bytes table */
c998985c grischka2009-07-18 22:07:42 +0200354 const unsigned char *q = tok_two_chars;
b8f6e1ae grischka2009-05-05 20:30:39 +0200355 while (*q) {
356 if (q[2] == v) {
357 *p++ = q[0];
358 *p++ = q[1];
359 *p = '\0';
360 return buf;
361 }
362 q += 3;
363 }
364 addv:
365 *p++ = v;
366 *p = '\0';
367 } else if (v < tok_ident) {
368 return table_ident[v - TOK_IDENT]->str;
369 } else if (v >= SYM_FIRST_ANOM) {
370 /* special name for anonymous symbol */
371 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
372 } else {
373 /* should never happen */
374 return NULL;
375 }
376 break;
377 }
378 return cstr_buf.data;
379}
380
805990b9 grischka2009-05-05 20:17:49 +0200381/* fill input buffer and peek next char */
382static int tcc_peekc_slow(BufferedFile *bf)
383{
384 int len;
385 /* only tries to read if really end of buffer */
386 if (bf->buf_ptr >= bf->buf_end) {
8c56b0cf Thomas Preud'homme2013-01-14 17:34:07 +0100387 if (bf->fd != -1) {
805990b9 grischka2009-05-05 20:17:49 +0200388#if defined(PARSE_DEBUG)
951a43ea Philip2015-04-30 21:31:32 +0000389 len = 1;
805990b9 grischka2009-05-05 20:17:49 +0200390#else
391 len = IO_BUF_SIZE;
392#endif
8c56b0cf Thomas Preud'homme2013-01-14 17:34:07 +0100393 len = read(bf->fd, bf->buffer, len);
805990b9 grischka2009-05-05 20:17:49 +0200394 if (len < 0)
395 len = 0;
396 } else {
397 len = 0;
398 }
399 total_bytes += len;
400 bf->buf_ptr = bf->buffer;
401 bf->buf_end = bf->buffer + len;
402 *bf->buf_end = CH_EOB;
403 }
404 if (bf->buf_ptr < bf->buf_end) {
405 return bf->buf_ptr[0];
406 } else {
407 bf->buf_ptr = bf->buf_end;
408 return CH_EOF;
409 }
410}
411
412/* return the current character, handling end of block if necessary
413 (but not stray) */
88a3ccab grischka2009-12-20 01:53:49 +0100414ST_FUNC int handle_eob(void)
805990b9 grischka2009-05-05 20:17:49 +0200415{
416 return tcc_peekc_slow(file);
417}
418
419/* read next char from current input file and handle end of input buffer */
88a3ccab grischka2009-12-20 01:53:49 +0100420ST_INLN void inp(void)
805990b9 grischka2009-05-05 20:17:49 +0200421{
422 ch = *(++(file->buf_ptr));
423 /* end of buffer/file handling */
424 if (ch == CH_EOB)
425 ch = handle_eob();
426}
427
428/* handle '\[\r]\n' */
429static int handle_stray_noerror(void)
430{
431 while (ch == '\\') {
432 inp();
433 if (ch == '\n') {
434 file->line_num++;
435 inp();
436 } else if (ch == '\r') {
437 inp();
438 if (ch != '\n')
439 goto fail;
440 file->line_num++;
441 inp();
442 } else {
443 fail:
444 return 1;
445 }
446 }
447 return 0;
448}
449
450static void handle_stray(void)
451{
452 if (handle_stray_noerror())
bf374a5f grischka2011-08-11 17:07:56 +0200453 tcc_error("stray '\\' in program");
805990b9 grischka2009-05-05 20:17:49 +0200454}
455
456/* skip the stray and handle the \\n case. Output an error if
457 incorrect char after the stray */
458static int handle_stray1(uint8_t *p)
459{
460 int c;
461
462 if (p >= file->buf_end) {
463 file->buf_ptr = p;
464 c = handle_eob();
465 p = file->buf_ptr;
466 if (c == '\\')
467 goto parse_stray;
468 } else {
469 parse_stray:
470 file->buf_ptr = p;
471 ch = *p;
472 handle_stray();
473 p = file->buf_ptr;
474 c = *p;
475 }
476 return c;
477}
478
479/* handle just the EOB case, but not stray */
480#define PEEKC_EOB(c, p)\
481{\
482 p++;\
483 c = *p;\
484 if (c == '\\') {\
485 file->buf_ptr = p;\
486 c = handle_eob();\
487 p = file->buf_ptr;\
488 }\
489}
490
491/* handle the complicated stray case */
492#define PEEKC(c, p)\
493{\
494 p++;\
495 c = *p;\
496 if (c == '\\') {\
497 c = handle_stray1(p);\
498 p = file->buf_ptr;\
499 }\
500}
501
502/* input with '\[\r]\n' handling. Note that this function cannot
503 handle other characters after '\', so you cannot call it inside
504 strings or comments */
88a3ccab grischka2009-12-20 01:53:49 +0100505ST_FUNC void minp(void)
805990b9 grischka2009-05-05 20:17:49 +0200506{
507 inp();
508 if (ch == '\\')
509 handle_stray();
510}
511
512
513/* single line C++ comments */
514static uint8_t *parse_line_comment(uint8_t *p)
515{
516 int c;
517
518 p++;
519 for(;;) {
520 c = *p;
521 redo:
522 if (c == '\n' || c == CH_EOF) {
523 break;
524 } else if (c == '\\') {
525 file->buf_ptr = p;
526 c = handle_eob();
527 p = file->buf_ptr;
528 if (c == '\\') {
529 PEEKC_EOB(c, p);
530 if (c == '\n') {
531 file->line_num++;
532 PEEKC_EOB(c, p);
533 } else if (c == '\r') {
534 PEEKC_EOB(c, p);
535 if (c == '\n') {
536 file->line_num++;
537 PEEKC_EOB(c, p);
538 }
539 }
540 } else {
541 goto redo;
542 }
543 } else {
544 p++;
545 }
546 }
547 return p;
548}
549
550/* C comments */
88a3ccab grischka2009-12-20 01:53:49 +0100551ST_FUNC uint8_t *parse_comment(uint8_t *p)
805990b9 grischka2009-05-05 20:17:49 +0200552{
553 int c;
554
555 p++;
556 for(;;) {
557 /* fast skip loop */
558 for(;;) {
559 c = *p;
560 if (c == '\n' || c == '*' || c == '\\')
561 break;
562 p++;
563 c = *p;
564 if (c == '\n' || c == '*' || c == '\\')
565 break;
566 p++;
567 }
568 /* now we can handle all the cases */
569 if (c == '\n') {
570 file->line_num++;
571 p++;
572 } else if (c == '*') {
573 p++;
574 for(;;) {
575 c = *p;
576 if (c == '*') {
577 p++;
578 } else if (c == '/') {
579 goto end_of_comment;
580 } else if (c == '\\') {
581 file->buf_ptr = p;
582 c = handle_eob();
583 p = file->buf_ptr;
584 if (c == '\\') {
585 /* skip '\[\r]\n', otherwise just skip the stray */
586 while (c == '\\') {
587 PEEKC_EOB(c, p);
588 if (c == '\n') {
589 file->line_num++;
590 PEEKC_EOB(c, p);
591 } else if (c == '\r') {
592 PEEKC_EOB(c, p);
593 if (c == '\n') {
594 file->line_num++;
595 PEEKC_EOB(c, p);
596 }
597 } else {
598 goto after_star;
599 }
600 }
601 }
602 } else {
603 break;
604 }
605 }
606 after_star: ;
607 } else {
608 /* stray, eob or eof */
609 file->buf_ptr = p;
610 c = handle_eob();
611 p = file->buf_ptr;
612 if (c == CH_EOF) {
bf374a5f grischka2011-08-11 17:07:56 +0200613 tcc_error("unexpected end of file in comment");
805990b9 grischka2009-05-05 20:17:49 +0200614 } else if (c == '\\') {
615 p++;
616 }
617 }
618 }
619 end_of_comment:
620 p++;
621 return p;
622}
623
624#define cinp minp
625
805990b9 grischka2009-05-05 20:17:49 +0200626static inline void skip_spaces(void)
627{
628 while (is_space(ch))
629 cinp();
630}
631
632static inline int check_space(int t, int *spc)
633{
634 if (is_space(t)) {
635 if (*spc)
636 return 1;
637 *spc = 1;
638 } else
639 *spc = 0;
640 return 0;
641}
642
643/* parse a string without interpreting escapes */
644static uint8_t *parse_pp_string(uint8_t *p,
645 int sep, CString *str)
646{
647 int c;
648 p++;
649 for(;;) {
650 c = *p;
651 if (c == sep) {
652 break;
653 } else if (c == '\\') {
654 file->buf_ptr = p;
655 c = handle_eob();
656 p = file->buf_ptr;
657 if (c == CH_EOF) {
658 unterminated_string:
659 /* XXX: indicate line number of start of string */
bf374a5f grischka2011-08-11 17:07:56 +0200660 tcc_error("missing terminating %c character", sep);
805990b9 grischka2009-05-05 20:17:49 +0200661 } else if (c == '\\') {
662 /* escape : just skip \[\r]\n */
663 PEEKC_EOB(c, p);
664 if (c == '\n') {
665 file->line_num++;
666 p++;
667 } else if (c == '\r') {
668 PEEKC_EOB(c, p);
669 if (c != '\n')
670 expect("'\n' after '\r'");
671 file->line_num++;
672 p++;
673 } else if (c == CH_EOF) {
674 goto unterminated_string;
675 } else {
676 if (str) {
677 cstr_ccat(str, '\\');
678 cstr_ccat(str, c);
679 }
680 p++;
681 }
682 }
683 } else if (c == '\n') {
684 file->line_num++;
685 goto add_char;
686 } else if (c == '\r') {
687 PEEKC_EOB(c, p);
688 if (c != '\n') {
689 if (str)
690 cstr_ccat(str, '\r');
691 } else {
692 file->line_num++;
693 goto add_char;
694 }
695 } else {
696 add_char:
697 if (str)
698 cstr_ccat(str, c);
699 p++;
700 }
701 }
702 p++;
703 return p;
704}
705
706/* skip block of text until #else, #elif or #endif. skip also pairs of
707 #if/#endif */
88a3ccab grischka2009-12-20 01:53:49 +0100708static void preprocess_skip(void)
805990b9 grischka2009-05-05 20:17:49 +0200709{
710 int a, start_of_line, c, in_warn_or_error;
711 uint8_t *p;
712
713 p = file->buf_ptr;
714 a = 0;
715redo_start:
716 start_of_line = 1;
717 in_warn_or_error = 0;
718 for(;;) {
719 redo_no_start:
720 c = *p;
721 switch(c) {
722 case ' ':
723 case '\t':
724 case '\f':
725 case '\v':
726 case '\r':
727 p++;
728 goto redo_no_start;
729 case '\n':
730 file->line_num++;
731 p++;
732 goto redo_start;
733 case '\\':
734 file->buf_ptr = p;
735 c = handle_eob();
736 if (c == CH_EOF) {
737 expect("#endif");
738 } else if (c == '\\') {
739 ch = file->buf_ptr[0];
740 handle_stray_noerror();
741 }
742 p = file->buf_ptr;
743 goto redo_no_start;
744 /* skip strings */
745 case '\"':
746 case '\'':
747 if (in_warn_or_error)
748 goto _default;
749 p = parse_pp_string(p, c, NULL);
750 break;
751 /* skip comments */
752 case '/':
753 if (in_warn_or_error)
754 goto _default;
755 file->buf_ptr = p;
756 ch = *p;
757 minp();
758 p = file->buf_ptr;
759 if (ch == '*') {
760 p = parse_comment(p);
761 } else if (ch == '/') {
762 p = parse_line_comment(p);
763 }
764 break;
765 case '#':
766 p++;
767 if (start_of_line) {
768 file->buf_ptr = p;
769 next_nomacro();
770 p = file->buf_ptr;
771 if (a == 0 &&
772 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
773 goto the_end;
774 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
775 a++;
776 else if (tok == TOK_ENDIF)
777 a--;
778 else if( tok == TOK_ERROR || tok == TOK_WARNING)
779 in_warn_or_error = 1;
5d55647a grischka2011-03-06 19:13:12 +0100780 else if (tok == TOK_LINEFEED)
781 goto redo_start;
bbcb54a1 seyko2015-04-27 16:36:58 +0300782 else if (parse_flags & PARSE_FLAG_ASM_FILE)
70dbe169 seyko2015-04-10 16:31:12 +0300783 p = parse_line_comment(p);
805990b9 grischka2009-05-05 20:17:49 +0200784 }
d81611b6 seyko2015-04-10 16:53:29 +0300785 else if (parse_flags & PARSE_FLAG_ASM_FILE)
786 p = parse_line_comment(p);
805990b9 grischka2009-05-05 20:17:49 +0200787 break;
788_default:
789 default:
790 p++;
791 break;
792 }
793 start_of_line = 0;
794 }
795 the_end: ;
796 file->buf_ptr = p;
797}
798
799/* ParseState handling */
800
801/* XXX: currently, no include file info is stored. Thus, we cannot display
802 accurate messages if the function or data definition spans multiple
803 files */
804
805/* save current parse state in 's' */
88a3ccab grischka2009-12-20 01:53:49 +0100806ST_FUNC void save_parse_state(ParseState *s)
805990b9 grischka2009-05-05 20:17:49 +0200807{
808 s->line_num = file->line_num;
809 s->macro_ptr = macro_ptr;
810 s->tok = tok;
811 s->tokc = tokc;
812}
813
814/* restore parse state from 's' */
88a3ccab grischka2009-12-20 01:53:49 +0100815ST_FUNC void restore_parse_state(ParseState *s)
805990b9 grischka2009-05-05 20:17:49 +0200816{
817 file->line_num = s->line_num;
818 macro_ptr = s->macro_ptr;
819 tok = s->tok;
820 tokc = s->tokc;
821}
822
823/* return the number of additional 'ints' necessary to store the
824 token */
825static inline int tok_ext_size(int t)
826{
827 switch(t) {
828 /* 4 bytes */
829 case TOK_CINT:
830 case TOK_CUINT:
831 case TOK_CCHAR:
832 case TOK_LCHAR:
833 case TOK_CFLOAT:
834 case TOK_LINENUM:
835 return 1;
836 case TOK_STR:
837 case TOK_LSTR:
838 case TOK_PPNUM:
bf374a5f grischka2011-08-11 17:07:56 +0200839 tcc_error("unsupported token");
805990b9 grischka2009-05-05 20:17:49 +0200840 return 1;
841 case TOK_CDOUBLE:
842 case TOK_CLLONG:
843 case TOK_CULLONG:
844 return 2;
845 case TOK_CLDOUBLE:
846 return LDOUBLE_SIZE / 4;
847 default:
848 return 0;
849 }
850}
851
852/* token string handling */
853
88a3ccab grischka2009-12-20 01:53:49 +0100854ST_INLN void tok_str_new(TokenString *s)
805990b9 grischka2009-05-05 20:17:49 +0200855{
856 s->str = NULL;
857 s->len = 0;
858 s->allocated_len = 0;
859 s->last_line_num = -1;
860}
861
88a3ccab grischka2009-12-20 01:53:49 +0100862ST_FUNC void tok_str_free(int *str)
805990b9 grischka2009-05-05 20:17:49 +0200863{
864 tcc_free(str);
865}
866
867static int *tok_str_realloc(TokenString *s)
868{
869 int *str, len;
870
871 if (s->allocated_len == 0) {
872 len = 8;
873 } else {
874 len = s->allocated_len * 2;
875 }
876 str = tcc_realloc(s->str, len * sizeof(int));
805990b9 grischka2009-05-05 20:17:49 +0200877 s->allocated_len = len;
878 s->str = str;
879 return str;
880}
881
88a3ccab grischka2009-12-20 01:53:49 +0100882ST_FUNC void tok_str_add(TokenString *s, int t)
805990b9 grischka2009-05-05 20:17:49 +0200883{
884 int len, *str;
885
886 len = s->len;
887 str = s->str;
888 if (len >= s->allocated_len)
889 str = tok_str_realloc(s);
890 str[len++] = t;
891 s->len = len;
892}
893
894static void tok_str_add2(TokenString *s, int t, CValue *cv)
895{
896 int len, *str;
897
898 len = s->len;
899 str = s->str;
900
901 /* allocate space for worst case */
902 if (len + TOK_MAX_SIZE > s->allocated_len)
903 str = tok_str_realloc(s);
904 str[len++] = t;
905 switch(t) {
906 case TOK_CINT:
907 case TOK_CUINT:
908 case TOK_CCHAR:
909 case TOK_LCHAR:
910 case TOK_CFLOAT:
911 case TOK_LINENUM:
912 str[len++] = cv->tab[0];
913 break;
914 case TOK_PPNUM:
915 case TOK_STR:
916 case TOK_LSTR:
917 {
918 int nb_words;
919 CString *cstr;
920
921 nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
922 while ((len + nb_words) > s->allocated_len)
923 str = tok_str_realloc(s);
924 cstr = (CString *)(str + len);
925 cstr->data = NULL;
926 cstr->size = cv->cstr->size;
927 cstr->data_allocated = NULL;
928 cstr->size_allocated = cstr->size;
929 memcpy((char *)cstr + sizeof(CString),
930 cv->cstr->data, cstr->size);
931 len += nb_words;
932 }
933 break;
934 case TOK_CDOUBLE:
935 case TOK_CLLONG:
936 case TOK_CULLONG:
937#if LDOUBLE_SIZE == 8
938 case TOK_CLDOUBLE:
939#endif
940 str[len++] = cv->tab[0];
941 str[len++] = cv->tab[1];
942 break;
943#if LDOUBLE_SIZE == 12
944 case TOK_CLDOUBLE:
945 str[len++] = cv->tab[0];
946 str[len++] = cv->tab[1];
947 str[len++] = cv->tab[2];
948#elif LDOUBLE_SIZE == 16
949 case TOK_CLDOUBLE:
950 str[len++] = cv->tab[0];
951 str[len++] = cv->tab[1];
952 str[len++] = cv->tab[2];
953 str[len++] = cv->tab[3];
954#elif LDOUBLE_SIZE != 8
955#error add long double size support
956#endif
957 break;
958 default:
959 break;
960 }
961 s->len = len;
962}
963
964/* add the current parse token in token string 's' */
88a3ccab grischka2009-12-20 01:53:49 +0100965ST_FUNC void tok_str_add_tok(TokenString *s)
805990b9 grischka2009-05-05 20:17:49 +0200966{
967 CValue cval;
968
969 /* save line number info */
970 if (file->line_num != s->last_line_num) {
971 s->last_line_num = file->line_num;
972 cval.i = s->last_line_num;
973 tok_str_add2(s, TOK_LINENUM, &cval);
974 }
975 tok_str_add2(s, tok, &tokc);
976}
977
4e5170d4 grischka2010-01-14 20:58:03 +0100978/* get a token from an integer array and increment pointer
979 accordingly. we code it as a macro to avoid pointer aliasing. */
980static inline void TOK_GET(int *t, const int **pp, CValue *cv)
981{
982 const int *p = *pp;
983 int n, *tab;
984
985 tab = cv->tab;
986 switch(*t = *p++) {
987 case TOK_CINT:
988 case TOK_CUINT:
989 case TOK_CCHAR:
990 case TOK_LCHAR:
991 case TOK_CFLOAT:
992 case TOK_LINENUM:
993 tab[0] = *p++;
994 break;
995 case TOK_STR:
996 case TOK_LSTR:
997 case TOK_PPNUM:
998 cv->cstr = (CString *)p;
999 cv->cstr->data = (char *)p + sizeof(CString);
1000 p += (sizeof(CString) + cv->cstr->size + 3) >> 2;
1001 break;
1002 case TOK_CDOUBLE:
1003 case TOK_CLLONG:
1004 case TOK_CULLONG:
1005 n = 2;
1006 goto copy;
1007 case TOK_CLDOUBLE:
805990b9 grischka2009-05-05 20:17:49 +02001008#if LDOUBLE_SIZE == 16
4e5170d4 grischka2010-01-14 20:58:03 +01001009 n = 4;
805990b9 grischka2009-05-05 20:17:49 +02001010#elif LDOUBLE_SIZE == 12
4e5170d4 grischka2010-01-14 20:58:03 +01001011 n = 3;
805990b9 grischka2009-05-05 20:17:49 +02001012#elif LDOUBLE_SIZE == 8
4e5170d4 grischka2010-01-14 20:58:03 +01001013 n = 2;
805990b9 grischka2009-05-05 20:17:49 +02001014#else
4e5170d4 grischka2010-01-14 20:58:03 +01001015# error add long double size support
805990b9 grischka2009-05-05 20:17:49 +02001016#endif
4e5170d4 grischka2010-01-14 20:58:03 +01001017 copy:
1018 do
1019 *tab++ = *p++;
1020 while (--n);
1021 break;
1022 default:
1023 break;
1024 }
1025 *pp = p;
805990b9 grischka2009-05-05 20:17:49 +02001026}
1027
280e20b1 grischka2010-01-14 20:57:50 +01001028static int macro_is_equal(const int *a, const int *b)
1029{
1030 char buf[STRING_MAX_SIZE + 1];
4bc83ac3 mingodad2014-03-26 20:14:39 +00001031 CValue cv;
5879c854 grischka2014-04-04 20:18:39 +02001032 int t;
280e20b1 grischka2010-01-14 20:57:50 +01001033 while (*a && *b) {
4e5170d4 grischka2010-01-14 20:58:03 +01001034 TOK_GET(&t, &a, &cv);
280e20b1 grischka2010-01-14 20:57:50 +01001035 pstrcpy(buf, sizeof buf, get_tok_str(t, &cv));
4e5170d4 grischka2010-01-14 20:58:03 +01001036 TOK_GET(&t, &b, &cv);
280e20b1 grischka2010-01-14 20:57:50 +01001037 if (strcmp(buf, get_tok_str(t, &cv)))
1038 return 0;
1039 }
1040 return !(*a || *b);
1041}
1042
b7b9f9f5 seyko2015-03-03 14:25:57 +03001043static void define_print(Sym *s, int is_undef)
1044{
1045 int c, t;
1046 CValue cval;
1047 const int *str;
1048 Sym *arg;
1049
1050 if (tcc_state->dflag == 0 || !s || !tcc_state->ppfp)
1051 return;
1052
1053 if (file) {
1054 c = file->line_num - file->line_ref - 1;
1055 if (c > 0) {
1056 while (c--)
1057 fputs("\n", tcc_state->ppfp);
1058 file->line_ref = file->line_num;
1059 }
1060 }
1061
1062 if (is_undef) {
1351de6a seyko2015-04-27 16:04:54 +03001063 fprintf(tcc_state->ppfp, "// #undef %s", get_tok_str(s->v, NULL));
b7b9f9f5 seyko2015-03-03 14:25:57 +03001064 return;
1065 }
1066
1067 fprintf(tcc_state->ppfp, "// #define %s", get_tok_str(s->v, NULL));
1068 arg = s->next;
1069 if (arg) {
1070 char *sep = "(";
1071 while (arg) {
1072 fprintf(tcc_state->ppfp, "%s%s", sep, get_tok_str(arg->v & ~SYM_FIELD, NULL));
1073 sep = ",";
1074 arg = arg->next;
1075 }
1076 fprintf(tcc_state->ppfp, ")");
1077 }
1078
1079 str = s->d;
1080 if (str)
1081 fprintf(tcc_state->ppfp, " ");
1082
1083 while (str) {
1084 TOK_GET(&t, &str, &cval);
1085 if (!t)
1086 break;
1087 fprintf(tcc_state->ppfp, "%s", get_tok_str(t, &cval));
1088 }
b7b9f9f5 seyko2015-03-03 14:25:57 +03001089}
1090
805990b9 grischka2009-05-05 20:17:49 +02001091/* defines handling */
88a3ccab grischka2009-12-20 01:53:49 +01001092ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)
805990b9 grischka2009-05-05 20:17:49 +02001093{
1094 Sym *s;
1095
280e20b1 grischka2010-01-14 20:57:50 +01001096 s = define_find(v);
1097 if (s && !macro_is_equal(s->d, str))
bf374a5f grischka2011-08-11 17:07:56 +02001098 tcc_warning("%s redefined", get_tok_str(v, NULL));
280e20b1 grischka2010-01-14 20:57:50 +01001099
bed17847 grischka2009-07-06 21:16:41 +02001100 s = sym_push2(&define_stack, v, macro_type, 0);
1101 s->d = str;
805990b9 grischka2009-05-05 20:17:49 +02001102 s->next = first_arg;
1103 table_ident[v - TOK_IDENT]->sym_define = s;
b7b9f9f5 seyko2015-03-03 14:25:57 +03001104 define_print(s, 0);
805990b9 grischka2009-05-05 20:17:49 +02001105}
1106
1107/* undefined a define symbol. Its name is just set to zero */
88a3ccab grischka2009-12-20 01:53:49 +01001108ST_FUNC void define_undef(Sym *s)
805990b9 grischka2009-05-05 20:17:49 +02001109{
1110 int v;
1111 v = s->v;
b7b9f9f5 seyko2015-03-03 14:25:57 +03001112 if (v >= TOK_IDENT && v < tok_ident) {
1113 define_print(s, 1);
805990b9 grischka2009-05-05 20:17:49 +02001114 table_ident[v - TOK_IDENT]->sym_define = NULL;
b7b9f9f5 seyko2015-03-03 14:25:57 +03001115 }
805990b9 grischka2009-05-05 20:17:49 +02001116}
1117
88a3ccab grischka2009-12-20 01:53:49 +01001118ST_INLN Sym *define_find(int v)
805990b9 grischka2009-05-05 20:17:49 +02001119{
1120 v -= TOK_IDENT;
1121 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1122 return NULL;
1123 return table_ident[v]->sym_define;
1124}
1125
1126/* free define stack until top reaches 'b' */
88a3ccab grischka2009-12-20 01:53:49 +01001127ST_FUNC void free_defines(Sym *b)
805990b9 grischka2009-05-05 20:17:49 +02001128{
1129 Sym *top, *top1;
1130 int v;
1131
1132 top = define_stack;
1133 while (top != b) {
1134 top1 = top->prev;
1135 /* do not free args or predefined defines */
bed17847 grischka2009-07-06 21:16:41 +02001136 if (top->d)
1137 tok_str_free(top->d);
805990b9 grischka2009-05-05 20:17:49 +02001138 v = top->v;
1139 if (v >= TOK_IDENT && v < tok_ident)
1140 table_ident[v - TOK_IDENT]->sym_define = NULL;
1141 sym_free(top);
1142 top = top1;
1143 }
1144 define_stack = b;
1145}
1146
e2650608 seyko2015-03-19 08:07:35 +03001147ST_FUNC void print_defines(void)
b7b9f9f5 seyko2015-03-03 14:25:57 +03001148{
1149 Sym *top, *s;
1150 int v;
1151
1152 top = define_stack;
1153 while (top) {
1154 v = top->v;
1155 if (v >= TOK_IDENT && v < tok_ident) {
1156 s = table_ident[v - TOK_IDENT]->sym_define;
1157 define_print(s, 0);
1158 }
1159 top = top->prev;
1160 }
1161}
1162
805990b9 grischka2009-05-05 20:17:49 +02001163/* label lookup */
88a3ccab grischka2009-12-20 01:53:49 +01001164ST_FUNC Sym *label_find(int v)
805990b9 grischka2009-05-05 20:17:49 +02001165{
1166 v -= TOK_IDENT;
1167 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1168 return NULL;
1169 return table_ident[v]->sym_label;
1170}
1171
88a3ccab grischka2009-12-20 01:53:49 +01001172ST_FUNC Sym *label_push(Sym **ptop, int v, int flags)
805990b9 grischka2009-05-05 20:17:49 +02001173{
1174 Sym *s, **ps;
1175 s = sym_push2(ptop, v, 0, 0);
1176 s->r = flags;
1177 ps = &table_ident[v - TOK_IDENT]->sym_label;
1178 if (ptop == &global_label_stack) {
1179 /* modify the top most local identifier, so that
1180 sym_identifier will point to 's' when popped */
1181 while (*ps != NULL)
1182 ps = &(*ps)->prev_tok;
1183 }
1184 s->prev_tok = *ps;
1185 *ps = s;
1186 return s;
1187}
1188
1189/* pop labels until element last is reached. Look if any labels are
1190 undefined. Define symbols if '&&label' was used. */
88a3ccab grischka2009-12-20 01:53:49 +01001191ST_FUNC void label_pop(Sym **ptop, Sym *slast)
805990b9 grischka2009-05-05 20:17:49 +02001192{
1193 Sym *s, *s1;
1194 for(s = *ptop; s != slast; s = s1) {
1195 s1 = s->prev;
1196 if (s->r == LABEL_DECLARED) {
bf374a5f grischka2011-08-11 17:07:56 +02001197 tcc_warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
805990b9 grischka2009-05-05 20:17:49 +02001198 } else if (s->r == LABEL_FORWARD) {
bf374a5f grischka2011-08-11 17:07:56 +02001199 tcc_error("label '%s' used but not defined",
805990b9 grischka2009-05-05 20:17:49 +02001200 get_tok_str(s->v, NULL));
1201 } else {
1202 if (s->c) {
1203 /* define corresponding symbol. A size of
1204 1 is put. */
035918ef grischka2009-07-18 22:05:58 +02001205 put_extern_sym(s, cur_text_section, s->jnext, 1);
805990b9 grischka2009-05-05 20:17:49 +02001206 }
1207 }
1208 /* remove label */
1209 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
1210 sym_free(s);
1211 }
1212 *ptop = slast;
1213}
1214
1215/* eval an expression for #if/#elif */
1216static int expr_preprocess(void)
1217{
1218 int c, t;
1219 TokenString str;
1220
1221 tok_str_new(&str);
1222 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1223 next(); /* do macro subst */
1224 if (tok == TOK_DEFINED) {
1225 next_nomacro();
1226 t = tok;
1227 if (t == '(')
1228 next_nomacro();
1229 c = define_find(tok) != 0;
1230 if (t == '(')
1231 next_nomacro();
1232 tok = TOK_CINT;
1233 tokc.i = c;
1234 } else if (tok >= TOK_IDENT) {
1235 /* if undefined macro */
1236 tok = TOK_CINT;
1237 tokc.i = 0;
1238 }
1239 tok_str_add_tok(&str);
1240 }
1241 tok_str_add(&str, -1); /* simulate end of file */
1242 tok_str_add(&str, 0);
1243 /* now evaluate C constant expression */
1244 macro_ptr = str.str;
1245 next();
1246 c = expr_const();
1247 macro_ptr = NULL;
1248 tok_str_free(str.str);
1249 return c != 0;
1250}
1251
805990b9 grischka2009-05-05 20:17:49 +02001252/* parse after #define */
88a3ccab grischka2009-12-20 01:53:49 +01001253ST_FUNC void parse_define(void)
805990b9 grischka2009-05-05 20:17:49 +02001254{
1255 Sym *s, *first, **ps;
a715d714 Thomas Preud'homme2014-04-08 22:19:48 +08001256 int v, t, varg, is_vaargs, spc, ptok, macro_list_start;
a6e6a954 Philip2015-05-02 13:19:14 +00001257 int saved_parse_flags;
805990b9 grischka2009-05-05 20:17:49 +02001258 TokenString str;
a715d714 Thomas Preud'homme2014-04-08 22:19:48 +08001259
805990b9 grischka2009-05-05 20:17:49 +02001260 v = tok;
1261 if (v < TOK_IDENT)
bf374a5f grischka2011-08-11 17:07:56 +02001262 tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc));
805990b9 grischka2009-05-05 20:17:49 +02001263 /* XXX: should check if same macro (ANSI) */
1264 first = NULL;
1265 t = MACRO_OBJ;
1266 /* '(' must be just after macro definition for MACRO_FUNC */
1267 next_nomacro_spc();
1268 if (tok == '(') {
1269 next_nomacro();
1270 ps = &first;
1271 while (tok != ')') {
1272 varg = tok;
1273 next_nomacro();
1274 is_vaargs = 0;
1275 if (varg == TOK_DOTS) {
1276 varg = TOK___VA_ARGS__;
1277 is_vaargs = 1;
1278 } else if (tok == TOK_DOTS && gnu_ext) {
1279 is_vaargs = 1;
1280 next_nomacro();
1281 }
1282 if (varg < TOK_IDENT)
a3fc5434 jiang2014-06-29 20:35:57 +08001283 tcc_error( "\'%s\' may not appear in parameter list", get_tok_str(varg, NULL));
805990b9 grischka2009-05-05 20:17:49 +02001284 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
1285 *ps = s;
1286 ps = &s->next;
1287 if (tok != ',')
a3fc5434 jiang2014-06-29 20:35:57 +08001288 continue;
805990b9 grischka2009-05-05 20:17:49 +02001289 next_nomacro();
1290 }
a3fc5434 jiang2014-06-29 20:35:57 +08001291 next_nomacro_spc();
805990b9 grischka2009-05-05 20:17:49 +02001292 t = MACRO_FUNC;
1293 }
1294 tok_str_new(&str);
1295 spc = 2;
1296 /* EOF testing necessary for '-D' handling */
a715d714
TP
Thomas Preud'homme2014-04-08 22:19:48 +08001297 ptok = 0;
1298 macro_list_start = 1;
a6e6a954
P
Philip2015-05-02 13:19:14 +00001299 saved_parse_flags = parse_flags;
1300 parse_flags |= PARSE_FLAG_ACCEPT_STRAYS | PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED;
805990b9 grischka2009-05-05 20:17:49 +02001301 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
3a922ad2 Philip2015-05-02 12:14:14 +00001302 if (macro_list_start && spc == 2 && tok == TOK_TWOSHARPS)
a715d714
TP
Thomas Preud'homme2014-04-08 22:19:48 +08001303 tcc_error("'##' invalid at start of macro");
1304 ptok = tok;
1305 /* remove spaces around ## and after '#' */
805990b9 grischka2009-05-05 20:17:49 +02001306 if (TOK_TWOSHARPS == tok) {
1307 if (1 == spc)
1308 --str.len;
1309 spc = 2;
1310 } else if ('#' == tok) {
1311 spc = 2;
1312 } else if (check_space(tok, &spc)) {
1313 goto skip;
1314 }
1315 tok_str_add2(&str, tok, &tokc);
3a922ad2 Philip2015-05-02 12:14:14 +00001316 macro_list_start = 0;
805990b9 grischka2009-05-05 20:17:49 +02001317 skip:
1318 next_nomacro_spc();
1319 }
a6e6a954 Philip2015-05-02 13:19:14 +00001320 parse_flags = saved_parse_flags;
a715d714
TP
Thomas Preud'homme2014-04-08 22:19:48 +08001321 if (ptok == TOK_TWOSHARPS)
1322 tcc_error("'##' invalid at end of macro");
805990b9 grischka2009-05-05 20:17:49 +02001323 if (spc == 1)
1324 --str.len; /* remove trailing space */
1325 tok_str_add(&str, 0);
805990b9 grischka2009-05-05 20:17:49 +02001326 define_push(v, t, str.str, first);
1327}
1328
2358b378 grischka2013-01-06 17:20:44 +01001329static inline int hash_cached_include(const char *filename)
805990b9 grischka2009-05-05 20:17:49 +02001330{
1331 const unsigned char *s;
1332 unsigned int h;
1333
1334 h = TOK_HASH_INIT;
62d1da1b Thomas Preud'homme2014-03-09 22:52:31 +08001335 s = (unsigned char *) filename;
805990b9 grischka2009-05-05 20:17:49 +02001336 while (*s) {
1337 h = TOK_HASH_FUNC(h, *s);
1338 s++;
1339 }
1340 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1341 return h;
1342}
1343
2358b378 grischka2013-01-06 17:20:44 +01001344static CachedInclude *search_cached_include(TCCState *s1, const char *filename)
805990b9 grischka2009-05-05 20:17:49 +02001345{
1346 CachedInclude *e;
1347 int i, h;
2358b378 grischka2013-01-06 17:20:44 +01001348 h = hash_cached_include(filename);
805990b9 grischka2009-05-05 20:17:49 +02001349 i = s1->cached_includes_hash[h];
1350 for(;;) {
1351 if (i == 0)
1352 break;
1353 e = s1->cached_includes[i - 1];
2358b378 grischka2013-01-06 17:20:44 +01001354 if (0 == PATHCMP(e->filename, filename))
805990b9 grischka2009-05-05 20:17:49 +02001355 return e;
1356 i = e->hash_next;
1357 }
1358 return NULL;
1359}
1360
2358b378 grischka2013-01-06 17:20:44 +01001361static inline void add_cached_include(TCCState *s1, const char *filename, int ifndef_macro)
805990b9 grischka2009-05-05 20:17:49 +02001362{
1363 CachedInclude *e;
1364 int h;
1365
2358b378 grischka2013-01-06 17:20:44 +01001366 if (search_cached_include(s1, filename))
805990b9 grischka2009-05-05 20:17:49 +02001367 return;
1368#ifdef INC_DEBUG
1369 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
1370#endif
1371 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
805990b9 grischka2009-05-05 20:17:49 +02001372 strcpy(e->filename, filename);
1373 e->ifndef_macro = ifndef_macro;
1374 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
1375 /* add in hash table */
2358b378 grischka2013-01-06 17:20:44 +01001376 h = hash_cached_include(filename);
805990b9 grischka2009-05-05 20:17:49 +02001377 e->hash_next = s1->cached_includes_hash[h];
1378 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1379}
1380
1381static void pragma_parse(TCCState *s1)
1382{
72e8ff11 grischka2015-04-23 23:27:36 +02001383 next_nomacro();
805990b9 grischka2009-05-05 20:17:49 +02001384 if (tok == TOK_pack) {
1385 /*
1386 This may be:
1387 #pragma pack(1) // set
1388 #pragma pack() // reset to default
1389 #pragma pack(push,1) // push & set
1390 #pragma pack(pop) // restore previous
1391 */
1392 next();
1393 skip('(');
1394 if (tok == TOK_ASM_pop) {
1395 next();
1396 if (s1->pack_stack_ptr <= s1->pack_stack) {
1397 stk_error:
bf374a5f grischka2011-08-11 17:07:56 +02001398 tcc_error("out of pack stack");
805990b9 grischka2009-05-05 20:17:49 +02001399 }
1400 s1->pack_stack_ptr--;
1401 } else {
72e8ff11 grischka2015-04-23 23:27:36 +02001402 int val = 0;
805990b9 grischka2009-05-05 20:17:49 +02001403 if (tok != ')') {
1404 if (tok == TOK_ASM_push) {
1405 next();
1406 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
1407 goto stk_error;
1408 s1->pack_stack_ptr++;
1409 skip(',');
1410 }
72e8ff11 grischka2015-04-23 23:27:36 +02001411 if (tok != TOK_CINT)
1412 goto pragma_err;
805990b9 grischka2009-05-05 20:17:49 +02001413 val = tokc.i;
1414 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
72e8ff11 grischka2015-04-23 23:27:36 +02001415 goto pragma_err;
805990b9 grischka2009-05-05 20:17:49 +02001416 next();
1417 }
1418 *s1->pack_stack_ptr = val;
805990b9 grischka2009-05-05 20:17:49 +02001419 }
72e8ff11 grischka2015-04-23 23:27:36 +02001420 if (tok != ')')
1421 goto pragma_err;
aeaff94e
SM
Steven G. Messervey2015-04-15 22:56:21 -04001422 } else if (tok == TOK_comment) {
1423 if (s1->ms_extensions) {
1424 next();
1425 skip('(');
1426 if (tok == TOK_lib) {
1427 next();
1428 skip(',');
1429 if (tok != TOK_STR) {
1430 tcc_error("invalid library specification");
1431 } else {
1432 int len = strlen((char *)tokc.cstr->data);
1433 char *file = tcc_malloc(len + 4); /* filetype, "-l", and \0 at the end */
1434 file[0] = TCC_FILETYPE_BINARY;
1435 file[1] = '-';
1436 file[2] = 'l';
1437 strcpy(&file[3],(char *)tokc.cstr->data);
1438 dynarray_add((void ***)&s1->files, &s1->nb_files, file);
1439 }
1440 next();
1441 tok = TOK_LINEFEED;
1442 } else {
b472d536 seyko2015-04-16 07:30:24 +03001443 tcc_warning("unknown specifier '%s' in #pragma comment", get_tok_str(tok, &tokc));
aeaff94e
SM
Steven G. Messervey2015-04-15 22:56:21 -04001444 }
1445 } else {
1446 tcc_warning("#pragma comment(lib) is ignored");
1447 }
72e8ff11 grischka2015-04-23 23:27:36 +02001448 } else if (tok == TOK_push_macro || tok == TOK_pop_macro) {
1449 int t = tok, v;
1450 Sym *s;
1451
1452 if (next_nomacro(), tok != '(')
1453 goto pragma_err;
1454 if (next_nomacro(), tok != TOK_STR)
1455 goto pragma_err;
1456 v = tok_alloc(tokc.cstr->data, tokc.cstr->size - 1)->tok;
1457 if (next_nomacro(), tok != ')')
1458 goto pragma_err;
1459 if (t == TOK_push_macro) {
1460 while (NULL == (s = define_find(v)))
1461 define_push(v, 0, NULL, NULL);
1462 s->type.ref = s; /* set push boundary */
1463 } else {
1464 for (s = define_stack; s; s = s->prev)
1465 if (s->v == v && s->type.ref == s) {
1466 s->type.ref = NULL;
1467 break;
1468 }
1469 }
1470 if (s)
1471 table_ident[v - TOK_IDENT]->sym_define = s->d ? s : NULL;
1472 else
1473 tcc_warning("unbalanced #pragma pop_macro");
1351de6a seyko2015-04-27 16:04:54 +03001474
1475 /* print info when tcc is called with "-E -dD" switches */
1476 if (s1->dflag && s1->ppfp) {
1477 if (file) {
1478 int c = file->line_num - file->line_ref - 1;
1479 if (c > 0) {
1480 while (c--)
1481 fputs("\n", tcc_state->ppfp);
1482 file->line_ref = file->line_num;
1483 }
1484 }
1485 fprintf(s1->ppfp, "// #pragma %s_macro(\"%s\")",
1486 (t == TOK_push_macro) ? "push" : "pop",
1487 get_tok_str(v, NULL));
1488 }
b08ce880 seyko2015-04-21 15:46:29 +03001489 } else if (tok == TOK_once) {
1490 add_cached_include(s1, file->filename, TOK_once);
0c8447db seyko2015-04-21 06:34:35 +03001491 } else {
1492 tcc_warning("unknown #pragma %s", get_tok_str(tok, &tokc));
805990b9 grischka2009-05-05 20:17:49 +02001493 }
72e8ff11 grischka2015-04-23 23:27:36 +02001494 return;
1495pragma_err:
1496 tcc_error("malformed #pragma directive");
805990b9 grischka2009-05-05 20:17:49 +02001497}
1498
1499/* is_bof is true if first non space token at beginning of file */
88a3ccab grischka2009-12-20 01:53:49 +01001500ST_FUNC void preprocess(int is_bof)
805990b9 grischka2009-05-05 20:17:49 +02001501{
1502 TCCState *s1 = tcc_state;
aed6a7cb grischka2009-05-11 18:55:16 +02001503 int i, c, n, saved_parse_flags;
805990b9 grischka2009-05-05 20:17:49 +02001504 char buf[1024], *q;
805990b9 grischka2009-05-05 20:17:49 +02001505 Sym *s;
aed6a7cb grischka2009-05-11 18:55:16 +02001506
805990b9 grischka2009-05-05 20:17:49 +02001507 saved_parse_flags = parse_flags;
1508 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
1509 PARSE_FLAG_LINEFEED;
bbcb54a1 seyko2015-04-27 16:36:58 +03001510 parse_flags |= (saved_parse_flags & PARSE_FLAG_ASM_FILE);
805990b9 grischka2009-05-05 20:17:49 +02001511 next_nomacro();
1512 redo:
1513 switch(tok) {
1514 case TOK_DEFINE:
1515 next_nomacro();
1516 parse_define();
1517 break;
1518 case TOK_UNDEF:
1519 next_nomacro();
1520 s = define_find(tok);
1521 /* undefine symbol by putting an invalid name */
1522 if (s)
1523 define_undef(s);
1524 break;
1525 case TOK_INCLUDE:
1526 case TOK_INCLUDE_NEXT:
1527 ch = file->buf_ptr[0];
1528 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1529 skip_spaces();
1530 if (ch == '<') {
1531 c = '>';
1532 goto read_name;
1533 } else if (ch == '\"') {
1534 c = ch;
1535 read_name:
1536 inp();
1537 q = buf;
1538 while (ch != c && ch != '\n' && ch != CH_EOF) {
1539 if ((q - buf) < sizeof(buf) - 1)
1540 *q++ = ch;
1541 if (ch == '\\') {
1542 if (handle_stray_noerror() == 0)
1543 --q;
1544 } else
1545 inp();
1546 }
1547 *q = '\0';
1548 minp();
1549#if 0
1550 /* eat all spaces and comments after include */
1551 /* XXX: slightly incorrect */
1552 while (ch1 != '\n' && ch1 != CH_EOF)
1553 inp();
1554#endif
1555 } else {
1556 /* computed #include : either we have only strings or
1557 we have anything enclosed in '<>' */
1558 next();
1559 buf[0] = '\0';
1560 if (tok == TOK_STR) {
1561 while (tok != TOK_LINEFEED) {
1562 if (tok != TOK_STR) {
1563 include_syntax:
bf374a5f grischka2011-08-11 17:07:56 +02001564 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
805990b9 grischka2009-05-05 20:17:49 +02001565 }
1566 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
1567 next();
1568 }
1569 c = '\"';
1570 } else {
1571 int len;
1572 while (tok != TOK_LINEFEED) {
1573 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1574 next();
1575 }
1576 len = strlen(buf);
1577 /* check syntax and remove '<>' */
1578 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
1579 goto include_syntax;
1580 memmove(buf, buf + 1, len - 2);
1581 buf[len - 2] = '\0';
1582 c = '>';
1583 }
1584 }
1585
aed6a7cb grischka2009-05-11 18:55:16 +02001586 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
bf374a5f grischka2011-08-11 17:07:56 +02001587 tcc_error("#include recursion too deep");
2358b378 grischka2013-01-06 17:20:44 +01001588 /* store current file in stack, but increment stack later below */
1589 *s1->include_stack_ptr = file;
aed6a7cb grischka2009-05-11 18:55:16 +02001590
1591 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
1592 for (i = -2; i < n; ++i) {
1593 char buf1[sizeof file->filename];
aed6a7cb grischka2009-05-11 18:55:16 +02001594 CachedInclude *e;
2358b378 grischka2013-01-06 17:20:44 +01001595 BufferedFile **f;
aed6a7cb grischka2009-05-11 18:55:16 +02001596 const char *path;
aed6a7cb grischka2009-05-11 18:55:16 +02001597
1598 if (i == -2) {
1599 /* check absolute include path */
1600 if (!IS_ABSPATH(buf))
1601 continue;
1602 buf1[0] = 0;
2358b378 grischka2013-01-06 17:20:44 +01001603 i = n; /* force end loop */
aed6a7cb grischka2009-05-11 18:55:16 +02001604
1605 } else if (i == -1) {
1606 /* search in current dir if "header.h" */
1607 if (c != '\"')
1608 continue;
8c56b0cf
TP
Thomas Preud'homme2013-01-14 17:34:07 +01001609 path = file->filename;
1610 pstrncpy(buf1, path, tcc_basename(path) - path);
aed6a7cb grischka2009-05-11 18:55:16 +02001611
1612 } else {
1613 /* search in all the include paths */
805990b9 grischka2009-05-05 20:17:49 +02001614 if (i < s1->nb_include_paths)
1615 path = s1->include_paths[i];
1616 else
1617 path = s1->sysinclude_paths[i - s1->nb_include_paths];
1618 pstrcpy(buf1, sizeof(buf1), path);
1619 pstrcat(buf1, sizeof(buf1), "/");
aed6a7cb grischka2009-05-11 18:55:16 +02001620 }
1621
1622 pstrcat(buf1, sizeof(buf1), buf);
1623
2358b378 grischka2013-01-06 17:20:44 +01001624 if (tok == TOK_INCLUDE_NEXT)
1625 for (f = s1->include_stack_ptr; f >= s1->include_stack; --f)
1626 if (0 == PATHCMP((*f)->filename, buf1)) {
1627#ifdef INC_DEBUG
1628 printf("%s: #include_next skipping %s\n", file->filename, buf1);
1629#endif
1630 goto include_trynext;
1631 }
1632
1633 e = search_cached_include(s1, buf1);
b08ce880 seyko2015-04-21 15:46:29 +03001634 if (e && (define_find(e->ifndef_macro) || e->ifndef_macro == TOK_once)) {
aed6a7cb grischka2009-05-11 18:55:16 +02001635 /* no need to parse the include because the 'ifndef macro'
1636 is defined */
1637#ifdef INC_DEBUG
2358b378 grischka2013-01-06 17:20:44 +01001638 printf("%s: skipping cached %s\n", file->filename, buf1);
aed6a7cb grischka2009-05-11 18:55:16 +02001639#endif
2358b378 grischka2013-01-06 17:20:44 +01001640 goto include_done;
805990b9 grischka2009-05-05 20:17:49 +02001641 }
aed6a7cb grischka2009-05-11 18:55:16 +02001642
8c56b0cf Thomas Preud'homme2013-01-14 17:34:07 +01001643 if (tcc_open(s1, buf1) < 0)
2358b378 grischka2013-01-06 17:20:44 +01001644include_trynext:
aed6a7cb grischka2009-05-11 18:55:16 +02001645 continue;
aed6a7cb grischka2009-05-11 18:55:16 +02001646
805990b9 grischka2009-05-05 20:17:49 +02001647#ifdef INC_DEBUG
2358b378 grischka2013-01-06 17:20:44 +01001648 printf("%s: including %s\n", file->prev->filename, file->filename);
805990b9 grischka2009-05-05 20:17:49 +02001649#endif
0c928da9
KS
Kirill Smelkov2010-06-20 20:08:12 +04001650 /* update target deps */
1651 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
1652 tcc_strdup(buf1));
2358b378 grischka2013-01-06 17:20:44 +01001653 /* push current file in stack */
1654 ++s1->include_stack_ptr;
805990b9 grischka2009-05-05 20:17:49 +02001655 /* add include file debug info */
e97bf88b grischka2010-11-25 13:29:15 +01001656 if (s1->do_debug)
805990b9 grischka2009-05-05 20:17:49 +02001657 put_stabs(file->filename, N_BINCL, 0, 0, 0);
805990b9 grischka2009-05-05 20:17:49 +02001658 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1659 ch = file->buf_ptr[0];
1660 goto the_end;
1661 }
bf374a5f grischka2011-08-11 17:07:56 +02001662 tcc_error("include file '%s' not found", buf);
aed6a7cb grischka2009-05-11 18:55:16 +02001663include_done:
805990b9 grischka2009-05-05 20:17:49 +02001664 break;
1665 case TOK_IFNDEF:
1666 c = 1;
1667 goto do_ifdef;
1668 case TOK_IF:
1669 c = expr_preprocess();
1670 goto do_if;
1671 case TOK_IFDEF:
1672 c = 0;
1673 do_ifdef:
1674 next_nomacro();
1675 if (tok < TOK_IDENT)
bf374a5f grischka2011-08-11 17:07:56 +02001676 tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
805990b9 grischka2009-05-05 20:17:49 +02001677 if (is_bof) {
1678 if (c) {
1679#ifdef INC_DEBUG
1680 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1681#endif
1682 file->ifndef_macro = tok;
1683 }
1684 }
1685 c = (define_find(tok) != 0) ^ c;
1686 do_if:
1687 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
2bd0daab grischka2014-01-06 19:56:26 +01001688 tcc_error("memory full (ifdef)");
805990b9 grischka2009-05-05 20:17:49 +02001689 *s1->ifdef_stack_ptr++ = c;
1690 goto test_skip;
1691 case TOK_ELSE:
1692 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
bf374a5f grischka2011-08-11 17:07:56 +02001693 tcc_error("#else without matching #if");
805990b9 grischka2009-05-05 20:17:49 +02001694 if (s1->ifdef_stack_ptr[-1] & 2)
bf374a5f grischka2011-08-11 17:07:56 +02001695 tcc_error("#else after #else");
805990b9 grischka2009-05-05 20:17:49 +02001696 c = (s1->ifdef_stack_ptr[-1] ^= 3);
569c20f1 grischka2009-07-23 19:21:14 +02001697 goto test_else;
805990b9 grischka2009-05-05 20:17:49 +02001698 case TOK_ELIF:
1699 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
bf374a5f grischka2011-08-11 17:07:56 +02001700 tcc_error("#elif without matching #if");
805990b9 grischka2009-05-05 20:17:49 +02001701 c = s1->ifdef_stack_ptr[-1];
1702 if (c > 1)
bf374a5f grischka2011-08-11 17:07:56 +02001703 tcc_error("#elif after #else");
805990b9 grischka2009-05-05 20:17:49 +02001704 /* last #if/#elif expression was true: we skip */
1705 if (c == 1)
1706 goto skip;
1707 c = expr_preprocess();
1708 s1->ifdef_stack_ptr[-1] = c;
569c20f1 grischka2009-07-23 19:21:14 +02001709 test_else:
1710 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1711 file->ifndef_macro = 0;
805990b9 grischka2009-05-05 20:17:49 +02001712 test_skip:
1713 if (!(c & 1)) {
1714 skip:
1715 preprocess_skip();
1716 is_bof = 0;
1717 goto redo;
1718 }
1719 break;
1720 case TOK_ENDIF:
1721 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
bf374a5f grischka2011-08-11 17:07:56 +02001722 tcc_error("#endif without matching #if");
805990b9 grischka2009-05-05 20:17:49 +02001723 s1->ifdef_stack_ptr--;
1724 /* '#ifndef macro' was at the start of file. Now we check if
1725 an '#endif' is exactly at the end of file */
1726 if (file->ifndef_macro &&
1727 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1728 file->ifndef_macro_saved = file->ifndef_macro;
1729 /* need to set to zero to avoid false matches if another
1730 #ifndef at middle of file */
1731 file->ifndef_macro = 0;
1732 while (tok != TOK_LINEFEED)
1733 next_nomacro();
1734 tok_flags |= TOK_FLAG_ENDIF;
1735 goto the_end;
1736 }
1737 break;
1738 case TOK_LINE:
1739 next();
1740 if (tok != TOK_CINT)
5e3e3214 seyko2015-03-03 14:06:05 +03001741 tcc_error("A #line format is wrong");
1742 case TOK_PPNUM:
1743 if (tok != TOK_CINT) {
1744 char *p = tokc.cstr->data;
1745 tokc.i = strtoul(p, (char **)&p, 10);
1746 }
70dbe169 seyko2015-04-10 16:31:12 +03001747 i = file->line_num;
1748 file->line_num = tokc.i - 1;
805990b9 grischka2009-05-05 20:17:49 +02001749 next();
1750 if (tok != TOK_LINEFEED) {
70dbe169 seyko2015-04-10 16:31:12 +03001751 if (tok != TOK_STR) {
bbcb54a1 seyko2015-04-27 16:36:58 +03001752 if ((parse_flags & PARSE_FLAG_ASM_FILE) == 0) {
70dbe169 seyko2015-04-10 16:31:12 +03001753 file->line_num = i;
1754 tcc_error("#line format is wrong");
1755 }
1756 break;
1757 }
805990b9 grischka2009-05-05 20:17:49 +02001758 pstrcpy(file->filename, sizeof(file->filename),
1759 (char *)tokc.cstr->data);
1760 }
8d10c578 seyko2015-03-03 14:46:44 +03001761 if (s1->do_debug)
1762 put_stabs(file->filename, N_BINCL, 0, 0, 0);
805990b9 grischka2009-05-05 20:17:49 +02001763 break;
1764 case TOK_ERROR:
1765 case TOK_WARNING:
1766 c = tok;
1767 ch = file->buf_ptr[0];
1768 skip_spaces();
1769 q = buf;
1770 while (ch != '\n' && ch != CH_EOF) {
1771 if ((q - buf) < sizeof(buf) - 1)
1772 *q++ = ch;
1773 if (ch == '\\') {
1774 if (handle_stray_noerror() == 0)
1775 --q;
1776 } else
1777 inp();
1778 }
1779 *q = '\0';
1780 if (c == TOK_ERROR)
bf374a5f grischka2011-08-11 17:07:56 +02001781 tcc_error("#error %s", buf);
805990b9 grischka2009-05-05 20:17:49 +02001782 else
bf374a5f grischka2011-08-11 17:07:56 +02001783 tcc_warning("#warning %s", buf);
805990b9 grischka2009-05-05 20:17:49 +02001784 break;
1785 case TOK_PRAGMA:
1786 pragma_parse(s1);
1787 break;
1788 default:
b9aeac0a Alexandre Becoulet2010-02-01 18:08:51 +01001789 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_PPNUM) {
805990b9 grischka2009-05-05 20:17:49 +02001790 /* '!' is ignored to allow C scripts. numbers are ignored
1791 to emulate cpp behaviour */
1792 } else {
bbcb54a1 seyko2015-04-27 16:36:58 +03001793 if (!(parse_flags & PARSE_FLAG_ASM_FILE))
bf374a5f grischka2011-08-11 17:07:56 +02001794 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
15b8a570
JS
Joe Soroka2011-02-23 15:13:08 -08001795 else {
1796 /* this is a gas line comment in an 'S' file. */
1797 file->buf_ptr = parse_line_comment(file->buf_ptr);
1798 goto the_end;
1799 }
805990b9 grischka2009-05-05 20:17:49 +02001800 }
1801 break;
1802 }
1803 /* ignore other preprocess commands or #! for C scripts */
1804 while (tok != TOK_LINEFEED)
1805 next_nomacro();
1806 the_end:
1807 parse_flags = saved_parse_flags;
1808}
1809
1810/* evaluate escape codes in a string. */
1811static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1812{
1813 int c, n;
1814 const uint8_t *p;
1815
1816 p = buf;
1817 for(;;) {
1818 c = *p;
1819 if (c == '\0')
1820 break;
1821 if (c == '\\') {
1822 p++;
1823 /* escape */
1824 c = *p;
1825 switch(c) {
1826 case '0': case '1': case '2': case '3':
1827 case '4': case '5': case '6': case '7':
1828 /* at most three octal digits */
1829 n = c - '0';
1830 p++;
1831 c = *p;
1832 if (isoct(c)) {
1833 n = n * 8 + c - '0';
1834 p++;
1835 c = *p;
1836 if (isoct(c)) {
1837 n = n * 8 + c - '0';
1838 p++;
1839 }
1840 }
1841 c = n;
1842 goto add_char_nonext;
1843 case 'x':
1844 case 'u':
1845 case 'U':
1846 p++;
1847 n = 0;
1848 for(;;) {
1849 c = *p;
1850 if (c >= 'a' && c <= 'f')
1851 c = c - 'a' + 10;
1852 else if (c >= 'A' && c <= 'F')
1853 c = c - 'A' + 10;
1854 else if (isnum(c))
1855 c = c - '0';
1856 else
1857 break;
1858 n = n * 16 + c;
1859 p++;
1860 }
1861 c = n;
1862 goto add_char_nonext;
1863 case 'a':
1864 c = '\a';
1865 break;
1866 case 'b':
1867 c = '\b';
1868 break;
1869 case 'f':
1870 c = '\f';
1871 break;
1872 case 'n':
1873 c = '\n';
1874 break;
1875 case 'r':
1876 c = '\r';
1877 break;
1878 case 't':
1879 c = '\t';
1880 break;
1881 case 'v':
1882 c = '\v';
1883 break;
1884 case 'e':
1885 if (!gnu_ext)
1886 goto invalid_escape;
1887 c = 27;
1888 break;
1889 case '\'':
1890 case '\"':
1891 case '\\':
1892 case '?':
1893 break;
1894 default:
1895 invalid_escape:
1896 if (c >= '!' && c <= '~')
bf374a5f grischka2011-08-11 17:07:56 +02001897 tcc_warning("unknown escape sequence: \'\\%c\'", c);
805990b9 grischka2009-05-05 20:17:49 +02001898 else
bf374a5f grischka2011-08-11 17:07:56 +02001899 tcc_warning("unknown escape sequence: \'\\x%x\'", c);
805990b9 grischka2009-05-05 20:17:49 +02001900 break;
1901 }
1902 }
1903 p++;
1904 add_char_nonext:
1905 if (!is_long)
1906 cstr_ccat(outstr, c);
1907 else
1908 cstr_wccat(outstr, c);
1909 }
1910 /* add a trailing '\0' */
1911 if (!is_long)
1912 cstr_ccat(outstr, '\0');
1913 else
1914 cstr_wccat(outstr, '\0');
1915}
1916
1917/* we use 64 bit numbers */
1918#define BN_SIZE 2
1919
1920/* bn = (bn << shift) | or_val */
88a3ccab grischka2009-12-20 01:53:49 +01001921static void bn_lshift(unsigned int *bn, int shift, int or_val)
805990b9 grischka2009-05-05 20:17:49 +02001922{
1923 int i;
1924 unsigned int v;
1925 for(i=0;i<BN_SIZE;i++) {
1926 v = bn[i];
1927 bn[i] = (v << shift) | or_val;
1928 or_val = v >> (32 - shift);
1929 }
1930}
1931
88a3ccab grischka2009-12-20 01:53:49 +01001932static void bn_zero(unsigned int *bn)
805990b9 grischka2009-05-05 20:17:49 +02001933{
1934 int i;
1935 for(i=0;i<BN_SIZE;i++) {
1936 bn[i] = 0;
1937 }
1938}
1939
1940/* parse number in null terminated string 'p' and return it in the
1941 current token */
88a3ccab grischka2009-12-20 01:53:49 +01001942static void parse_number(const char *p)
805990b9 grischka2009-05-05 20:17:49 +02001943{
1944 int b, t, shift, frac_bits, s, exp_val, ch;
1945 char *q;
1946 unsigned int bn[BN_SIZE];
1947 double d;
1948
1949 /* number */
1950 q = token_buf;
1951 ch = *p++;
1952 t = ch;
1953 ch = *p++;
1954 *q++ = t;
1955 b = 10;
1956 if (t == '.') {
1957 goto float_frac_parse;
1958 } else if (t == '0') {
1959 if (ch == 'x' || ch == 'X') {
1960 q--;
1961 ch = *p++;
1962 b = 16;
1963 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
1964 q--;
1965 ch = *p++;
1966 b = 2;
1967 }
1968 }
1969 /* parse all digits. cannot check octal numbers at this stage
1970 because of floating point constants */
1971 while (1) {
1972 if (ch >= 'a' && ch <= 'f')
1973 t = ch - 'a' + 10;
1974 else if (ch >= 'A' && ch <= 'F')
1975 t = ch - 'A' + 10;
1976 else if (isnum(ch))
1977 t = ch - '0';
1978 else
1979 break;
1980 if (t >= b)
1981 break;
1982 if (q >= token_buf + STRING_MAX_SIZE) {
1983 num_too_long:
bf374a5f grischka2011-08-11 17:07:56 +02001984 tcc_error("number too long");
805990b9 grischka2009-05-05 20:17:49 +02001985 }
1986 *q++ = ch;
1987 ch = *p++;
1988 }
1989 if (ch == '.' ||
1990 ((ch == 'e' || ch == 'E') && b == 10) ||
1991 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
1992 if (b != 10) {
1993 /* NOTE: strtox should support that for hexa numbers, but
1994 non ISOC99 libcs do not support it, so we prefer to do
1995 it by hand */
1996 /* hexadecimal or binary floats */
1997 /* XXX: handle overflows */
1998 *q = '\0';
1999 if (b == 16)
2000 shift = 4;
2001 else
5a76c5d2 Lee Duhem2014-12-15 16:32:08 +08002002 shift = 1;
805990b9 grischka2009-05-05 20:17:49 +02002003 bn_zero(bn);
2004 q = token_buf;
2005 while (1) {
2006 t = *q++;
2007 if (t == '\0') {
2008 break;
2009 } else if (t >= 'a') {
2010 t = t - 'a' + 10;
2011 } else if (t >= 'A') {
2012 t = t - 'A' + 10;
2013 } else {
2014 t = t - '0';
2015 }
2016 bn_lshift(bn, shift, t);
2017 }
2018 frac_bits = 0;
2019 if (ch == '.') {
2020 ch = *p++;
2021 while (1) {
2022 t = ch;
2023 if (t >= 'a' && t <= 'f') {
2024 t = t - 'a' + 10;
2025 } else if (t >= 'A' && t <= 'F') {
2026 t = t - 'A' + 10;
2027 } else if (t >= '0' && t <= '9') {
2028 t = t - '0';
2029 } else {
2030 break;
2031 }
2032 if (t >= b)
bf374a5f grischka2011-08-11 17:07:56 +02002033 tcc_error("invalid digit");
805990b9 grischka2009-05-05 20:17:49 +02002034 bn_lshift(bn, shift, t);
2035 frac_bits += shift;
2036 ch = *p++;
2037 }
2038 }
2039 if (ch != 'p' && ch != 'P')
2040 expect("exponent");
2041 ch = *p++;
2042 s = 1;
2043 exp_val = 0;
2044 if (ch == '+') {
2045 ch = *p++;
2046 } else if (ch == '-') {
2047 s = -1;
2048 ch = *p++;
2049 }
2050 if (ch < '0' || ch > '9')
2051 expect("exponent digits");
2052 while (ch >= '0' && ch <= '9') {
2053 exp_val = exp_val * 10 + ch - '0';
2054 ch = *p++;
2055 }
2056 exp_val = exp_val * s;
2057
2058 /* now we can generate the number */
2059 /* XXX: should patch directly float number */
2060 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
2061 d = ldexp(d, exp_val - frac_bits);
2062 t = toup(ch);
2063 if (t == 'F') {
2064 ch = *p++;
2065 tok = TOK_CFLOAT;
2066 /* float : should handle overflow */
2067 tokc.f = (float)d;
2068 } else if (t == 'L') {
2069 ch = *p++;
9fda4f42 grischka2009-07-18 22:07:17 +02002070#ifdef TCC_TARGET_PE
2071 tok = TOK_CDOUBLE;
2072 tokc.d = d;
2073#else
805990b9 grischka2009-05-05 20:17:49 +02002074 tok = TOK_CLDOUBLE;
2075 /* XXX: not large enough */
2076 tokc.ld = (long double)d;
9fda4f42 grischka2009-07-18 22:07:17 +02002077#endif
805990b9 grischka2009-05-05 20:17:49 +02002078 } else {
2079 tok = TOK_CDOUBLE;
2080 tokc.d = d;
2081 }
2082 } else {
2083 /* decimal floats */
2084 if (ch == '.') {
2085 if (q >= token_buf + STRING_MAX_SIZE)
2086 goto num_too_long;
2087 *q++ = ch;
2088 ch = *p++;
2089 float_frac_parse:
2090 while (ch >= '0' && ch <= '9') {
2091 if (q >= token_buf + STRING_MAX_SIZE)
2092 goto num_too_long;
2093 *q++ = ch;
2094 ch = *p++;
2095 }
2096 }
2097 if (ch == 'e' || ch == 'E') {
2098 if (q >= token_buf + STRING_MAX_SIZE)
2099 goto num_too_long;
2100 *q++ = ch;
2101 ch = *p++;
2102 if (ch == '-' || ch == '+') {
2103 if (q >= token_buf + STRING_MAX_SIZE)
2104 goto num_too_long;
2105 *q++ = ch;
2106 ch = *p++;
2107 }
2108 if (ch < '0' || ch > '9')
2109 expect("exponent digits");
2110 while (ch >= '0' && ch <= '9') {
2111 if (q >= token_buf + STRING_MAX_SIZE)
2112 goto num_too_long;
2113 *q++ = ch;
2114 ch = *p++;
2115 }
2116 }
2117 *q = '\0';
2118 t = toup(ch);
2119 errno = 0;
2120 if (t == 'F') {
2121 ch = *p++;
2122 tok = TOK_CFLOAT;
2123 tokc.f = strtof(token_buf, NULL);
2124 } else if (t == 'L') {
2125 ch = *p++;
9fda4f42 grischka2009-07-18 22:07:17 +02002126#ifdef TCC_TARGET_PE
2127 tok = TOK_CDOUBLE;
2128 tokc.d = strtod(token_buf, NULL);
2129#else
805990b9 grischka2009-05-05 20:17:49 +02002130 tok = TOK_CLDOUBLE;
2131 tokc.ld = strtold(token_buf, NULL);
9fda4f42 grischka2009-07-18 22:07:17 +02002132#endif
805990b9 grischka2009-05-05 20:17:49 +02002133 } else {
2134 tok = TOK_CDOUBLE;
2135 tokc.d = strtod(token_buf, NULL);
2136 }
2137 }
2138 } else {
2139 unsigned long long n, n1;
a6c3ce6e
TP
Thomas Preud'homme2015-02-18 04:22:25 +00002140 int lcount, ucount, must_64bit;
2141 const char *p1;
805990b9 grischka2009-05-05 20:17:49 +02002142
2143 /* integer number */
2144 *q = '\0';
2145 q = token_buf;
2146 if (b == 10 && *q == '0') {
2147 b = 8;
2148 q++;
2149 }
2150 n = 0;
2151 while(1) {
2152 t = *q++;
2153 /* no need for checks except for base 10 / 8 errors */
a6c3ce6e Thomas Preud'homme2015-02-18 04:22:25 +00002154 if (t == '\0')
805990b9 grischka2009-05-05 20:17:49 +02002155 break;
a6c3ce6e Thomas Preud'homme2015-02-18 04:22:25 +00002156 else if (t >= 'a')
805990b9 grischka2009-05-05 20:17:49 +02002157 t = t - 'a' + 10;
a6c3ce6e Thomas Preud'homme2015-02-18 04:22:25 +00002158 else if (t >= 'A')
805990b9 grischka2009-05-05 20:17:49 +02002159 t = t - 'A' + 10;
a6c3ce6e Thomas Preud'homme2015-02-18 04:22:25 +00002160 else
805990b9 grischka2009-05-05 20:17:49 +02002161 t = t - '0';
a6c3ce6e
TP
Thomas Preud'homme2015-02-18 04:22:25 +00002162 if (t >= b)
2163 tcc_error("invalid digit");
805990b9 grischka2009-05-05 20:17:49 +02002164 n1 = n;
2165 n = n * b + t;
2166 /* detect overflow */
2167 /* XXX: this test is not reliable */
2168 if (n < n1)
bf374a5f grischka2011-08-11 17:07:56 +02002169 tcc_error("integer constant overflow");
805990b9 grischka2009-05-05 20:17:49 +02002170 }
a6c3ce6e
TP
Thomas Preud'homme2015-02-18 04:22:25 +00002171
2172 /* Determine the characteristics (unsigned and/or 64bit) the type of
2173 the constant must have according to the constant suffix(es) */
2174 lcount = ucount = must_64bit = 0;
2175 p1 = p;
805990b9 grischka2009-05-05 20:17:49 +02002176 for(;;) {
2177 t = toup(ch);
2178 if (t == 'L') {
2179 if (lcount >= 2)
bf374a5f grischka2011-08-11 17:07:56 +02002180 tcc_error("three 'l's in integer constant");
a6c3ce6e
TP
Thomas Preud'homme2015-02-18 04:22:25 +00002181 if (lcount && *(p - 1) != ch)
2182 tcc_error("incorrect integer suffix: %s", p1);
805990b9 grischka2009-05-05 20:17:49 +02002183 lcount++;
1736a71b Thomas Preud'homme2012-03-14 15:47:42 +01002184#if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
a6c3ce6e Thomas Preud'homme2015-02-18 04:22:25 +00002185 if (lcount == 2)
1736a71b Thomas Preud'homme2012-03-14 15:47:42 +01002186#endif
a6c3ce6e Thomas Preud'homme2015-02-18 04:22:25 +00002187 must_64bit = 1;
805990b9 grischka2009-05-05 20:17:49 +02002188 ch = *p++;
2189 } else if (t == 'U') {
2190 if (ucount >= 1)
bf374a5f grischka2011-08-11 17:07:56 +02002191 tcc_error("two 'u's in integer constant");
805990b9 grischka2009-05-05 20:17:49 +02002192 ucount++;
805990b9 grischka2009-05-05 20:17:49 +02002193 ch = *p++;
2194 } else {
2195 break;
2196 }
2197 }
a6c3ce6e
TP
Thomas Preud'homme2015-02-18 04:22:25 +00002198
2199 /* Whether 64 bits are needed to hold the constant's value */
2200 if (n & 0xffffffff00000000LL || must_64bit) {
2201 tok = TOK_CLLONG;
2202 n1 = n >> 32;
2203 } else {
2204 tok = TOK_CINT;
2205 n1 = n;
2206 }
2207
2208 /* Whether type must be unsigned to hold the constant's value */
2209 if (ucount || ((n1 >> 31) && (b != 10))) {
2210 if (tok == TOK_CLLONG)
2211 tok = TOK_CULLONG;
2212 else
2213 tok = TOK_CUINT;
2214 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2215 } else if (n1 >> 31) {
2216 if (tok == TOK_CINT)
2217 tok = TOK_CLLONG;
2218 else
2219 tcc_error("integer constant overflow");
2220 }
2221
805990b9 grischka2009-05-05 20:17:49 +02002222 if (tok == TOK_CINT || tok == TOK_CUINT)
2223 tokc.ui = n;
2224 else
2225 tokc.ull = n;
2226 }
2227 if (ch)
bf374a5f grischka2011-08-11 17:07:56 +02002228 tcc_error("invalid number\n");
805990b9 grischka2009-05-05 20:17:49 +02002229}
2230
2231
2232#define PARSE2(c1, tok1, c2, tok2) \
2233 case c1: \
2234 PEEKC(c, p); \
2235 if (c == c2) { \
2236 p++; \
2237 tok = tok2; \
2238 } else { \
2239 tok = tok1; \
2240 } \
2241 break;
2242
2243/* return next token without macro substitution */
2244static inline void next_nomacro1(void)
2245{
2246 int t, c, is_long;
2247 TokenSym *ts;
2248 uint8_t *p, *p1;
2249 unsigned int h;
2250
2251 p = file->buf_ptr;
2252 redo_no_start:
2253 c = *p;
2254 switch(c) {
2255 case ' ':
2256 case '\t':
2257 tok = c;
2258 p++;
2259 goto keep_tok_flags;
2260 case '\f':
2261 case '\v':
2262 case '\r':
2263 p++;
2264 goto redo_no_start;
2265 case '\\':
2266 /* first look if it is in fact an end of buffer */
2267 if (p >= file->buf_end) {
2268 file->buf_ptr = p;
2269 handle_eob();
2270 p = file->buf_ptr;
2271 if (p >= file->buf_end)
2272 goto parse_eof;
2273 else
2274 goto redo_no_start;
2275 } else {
2276 file->buf_ptr = p;
2277 ch = *p;
0877ba7c
P
Philip2015-05-02 12:58:37 +00002278 if (parse_flags & PARSE_FLAG_ACCEPT_STRAYS) {
2279 if (handle_stray_noerror() != 0) {
2280 goto parse_simple;
2281 }
2282 } else {
2283 handle_stray();
2284 }
805990b9 grischka2009-05-05 20:17:49 +02002285 p = file->buf_ptr;
2286 goto redo_no_start;
2287 }
2288 parse_eof:
2289 {
2290 TCCState *s1 = tcc_state;
2291 if ((parse_flags & PARSE_FLAG_LINEFEED)
2292 && !(tok_flags & TOK_FLAG_EOF)) {
2293 tok_flags |= TOK_FLAG_EOF;
2294 tok = TOK_LINEFEED;
2295 goto keep_tok_flags;
a40814cc grischka2010-01-14 21:00:05 +01002296 } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
2297 tok = TOK_EOF;
2298 } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
bf374a5f grischka2011-08-11 17:07:56 +02002299 tcc_error("missing #endif");
a40814cc grischka2010-01-14 21:00:05 +01002300 } else if (s1->include_stack_ptr == s1->include_stack) {
805990b9 grischka2009-05-05 20:17:49 +02002301 /* no include left : end of file. */
2302 tok = TOK_EOF;
2303 } else {
2304 tok_flags &= ~TOK_FLAG_EOF;
2305 /* pop include file */
2306
2307 /* test if previous '#endif' was after a #ifdef at
2308 start of file */
2309 if (tok_flags & TOK_FLAG_ENDIF) {
2310#ifdef INC_DEBUG
2311 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2312#endif
2358b378 grischka2013-01-06 17:20:44 +01002313 add_cached_include(s1, file->filename, file->ifndef_macro_saved);
2314 tok_flags &= ~TOK_FLAG_ENDIF;
805990b9 grischka2009-05-05 20:17:49 +02002315 }
2316
2317 /* add end of include file debug info */
f9181416 grischka2009-05-11 18:45:44 +02002318 if (tcc_state->do_debug) {
805990b9 grischka2009-05-05 20:17:49 +02002319 put_stabd(N_EINCL, 0, 0);
2320 }
2321 /* pop include stack */
e97bf88b grischka2010-11-25 13:29:15 +01002322 tcc_close();
805990b9 grischka2009-05-05 20:17:49 +02002323 s1->include_stack_ptr--;
805990b9 grischka2009-05-05 20:17:49 +02002324 p = file->buf_ptr;
2325 goto redo_no_start;
2326 }
2327 }
2328 break;
2329
2330 case '\n':
2331 file->line_num++;
2332 tok_flags |= TOK_FLAG_BOL;
2333 p++;
85e481b6 grischka2009-07-06 21:12:45 +02002334maybe_newline:
805990b9 grischka2009-05-05 20:17:49 +02002335 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2336 goto redo_no_start;
2337 tok = TOK_LINEFEED;
2338 goto keep_tok_flags;
2339
2340 case '#':
2341 /* XXX: simplify */
2342 PEEKC(c, p);
d81611b6 seyko2015-04-10 16:53:29 +03002343 if (is_space(c) && (parse_flags & PARSE_FLAG_ASM_FILE)) {
2344 p = parse_line_comment(p);
2345 goto redo_no_start;
2346 }
2347 else
805990b9 grischka2009-05-05 20:17:49 +02002348 if ((tok_flags & TOK_FLAG_BOL) &&
2349 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2350 file->buf_ptr = p;
2351 preprocess(tok_flags & TOK_FLAG_BOF);
2352 p = file->buf_ptr;
85e481b6 grischka2009-07-06 21:12:45 +02002353 goto maybe_newline;
805990b9 grischka2009-05-05 20:17:49 +02002354 } else {
2355 if (c == '#') {
2356 p++;
2357 tok = TOK_TWOSHARPS;
2358 } else {
bbcb54a1 seyko2015-04-27 16:36:58 +03002359 if (parse_flags & PARSE_FLAG_ASM_FILE) {
805990b9 grischka2009-05-05 20:17:49 +02002360 p = parse_line_comment(p - 1);
2361 goto redo_no_start;
2362 } else {
2363 tok = '#';
2364 }
2365 }
2366 }
2367 break;
dcb36587 seyko2015-04-12 15:32:03 +03002368
5ce2154c seyko2015-04-20 03:44:08 +03002369 /* dollar is allowed to start identifiers when not parsing asm */
2370 case '$':
2371 if (!tcc_state->dollars_in_identifiers
2372 || (parse_flags & PARSE_FLAG_ASM_FILE)) goto parse_simple;
2373
805990b9 grischka2009-05-05 20:17:49 +02002374 case 'a': case 'b': case 'c': case 'd':
2375 case 'e': case 'f': case 'g': case 'h':
2376 case 'i': case 'j': case 'k': case 'l':
2377 case 'm': case 'n': case 'o': case 'p':
2378 case 'q': case 'r': case 's': case 't':
2379 case 'u': case 'v': case 'w': case 'x':
2380 case 'y': case 'z':
2381 case 'A': case 'B': case 'C': case 'D':
2382 case 'E': case 'F': case 'G': case 'H':
2383 case 'I': case 'J': case 'K':
2384 case 'M': case 'N': case 'O': case 'P':
2385 case 'Q': case 'R': case 'S': case 'T':
2386 case 'U': case 'V': case 'W': case 'X':
2387 case 'Y': case 'Z':
2388 case '_':
2389 parse_ident_fast:
2390 p1 = p;
2391 h = TOK_HASH_INIT;
2392 h = TOK_HASH_FUNC(h, c);
2393 p++;
2394 for(;;) {
2395 c = *p;
5ce2154c seyko2015-04-20 03:44:08 +03002396 if (!isidnum_table[c-CH_EOF]
2397 && (tcc_state->dollars_in_identifiers ? (c != '$') : 1))
805990b9 grischka2009-05-05 20:17:49 +02002398 break;
2399 h = TOK_HASH_FUNC(h, c);
2400 p++;
2401 }
2402 if (c != '\\') {
2403 TokenSym **pts;
2404 int len;
2405
2406 /* fast case : no stray found, so we have the full token
2407 and we have already hashed it */
2408 len = p - p1;
2409 h &= (TOK_HASH_SIZE - 1);
2410 pts = &hash_ident[h];
2411 for(;;) {
2412 ts = *pts;
2413 if (!ts)
2414 break;
2415 if (ts->len == len && !memcmp(ts->str, p1, len))
2416 goto token_found;
2417 pts = &(ts->hash_next);
2418 }
62d1da1b Thomas Preud'homme2014-03-09 22:52:31 +08002419 ts = tok_alloc_new(pts, (char *) p1, len);
805990b9 grischka2009-05-05 20:17:49 +02002420 token_found: ;
2421 } else {
2422 /* slower case */
2423 cstr_reset(&tokcstr);
2424
2425 while (p1 < p) {
2426 cstr_ccat(&tokcstr, *p1);
2427 p1++;
2428 }
2429 p--;
2430 PEEKC(c, p);
2431 parse_ident_slow:
5ce2154c seyko2015-04-20 03:44:08 +03002432 while (isidnum_table[c-CH_EOF]
2433 || (tcc_state->dollars_in_identifiers ? (c == '$') : 0)) {
805990b9 grischka2009-05-05 20:17:49 +02002434 cstr_ccat(&tokcstr, c);
2435 PEEKC(c, p);
2436 }
2437 ts = tok_alloc(tokcstr.data, tokcstr.size);
2438 }
2439 tok = ts->tok;
2440 break;
2441 case 'L':
2442 t = p[1];
2443 if (t != '\\' && t != '\'' && t != '\"') {
2444 /* fast case */
2445 goto parse_ident_fast;
2446 } else {
2447 PEEKC(c, p);
2448 if (c == '\'' || c == '\"') {
2449 is_long = 1;
2450 goto str_const;
2451 } else {
2452 cstr_reset(&tokcstr);
2453 cstr_ccat(&tokcstr, 'L');
2454 goto parse_ident_slow;
2455 }
2456 }
2457 break;
2458 case '0': case '1': case '2': case '3':
2459 case '4': case '5': case '6': case '7':
2460 case '8': case '9':
2461
2462 cstr_reset(&tokcstr);
2463 /* after the first digit, accept digits, alpha, '.' or sign if
2464 prefixed by 'eEpP' */
2465 parse_num:
2466 for(;;) {
2467 t = c;
2468 cstr_ccat(&tokcstr, c);
2469 PEEKC(c, p);
2470 if (!(isnum(c) || isid(c) || c == '.' ||
2471 ((c == '+' || c == '-') &&
2472 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
2473 break;
2474 }
2475 /* We add a trailing '\0' to ease parsing */
2476 cstr_ccat(&tokcstr, '\0');
2477 tokc.cstr = &tokcstr;
2478 tok = TOK_PPNUM;
2479 break;
2480 case '.':
2481 /* special dot handling because it can also start a number */
2482 PEEKC(c, p);
2483 if (isnum(c)) {
2484 cstr_reset(&tokcstr);
2485 cstr_ccat(&tokcstr, '.');
2486 goto parse_num;
2487 } else if (c == '.') {
2488 PEEKC(c, p);
8037a1ce seyko2015-04-10 16:40:30 +03002489 if (c != '.') {
bbcb54a1 seyko2015-04-27 16:36:58 +03002490 if ((parse_flags & PARSE_FLAG_ASM_FILE) == 0)
8037a1ce seyko2015-04-10 16:40:30 +03002491 expect("'.'");
2492 tok = '.';
2493 break;
2494 }
805990b9 grischka2009-05-05 20:17:49 +02002495 PEEKC(c, p);
2496 tok = TOK_DOTS;
2497 } else {
2498 tok = '.';
2499 }
2500 break;
2501 case '\'':
2502 case '\"':
2503 is_long = 0;
2504 str_const:
2505 {
2506 CString str;
2507 int sep;
2508
2509 sep = c;
2510
2511 /* parse the string */
2512 cstr_new(&str);
2513 p = parse_pp_string(p, sep, &str);
2514 cstr_ccat(&str, '\0');
2515
2516 /* eval the escape (should be done as TOK_PPNUM) */
2517 cstr_reset(&tokcstr);
2518 parse_escape_string(&tokcstr, str.data, is_long);
2519 cstr_free(&str);
2520
2521 if (sep == '\'') {
2522 int char_size;
2523 /* XXX: make it portable */
2524 if (!is_long)
2525 char_size = 1;
2526 else
2527 char_size = sizeof(nwchar_t);
2528 if (tokcstr.size <= char_size)
bf374a5f grischka2011-08-11 17:07:56 +02002529 tcc_error("empty character constant");
805990b9 grischka2009-05-05 20:17:49 +02002530 if (tokcstr.size > 2 * char_size)
bf374a5f grischka2011-08-11 17:07:56 +02002531 tcc_warning("multi-character character constant");
805990b9 grischka2009-05-05 20:17:49 +02002532 if (!is_long) {
2533 tokc.i = *(int8_t *)tokcstr.data;
2534 tok = TOK_CCHAR;
2535 } else {
2536 tokc.i = *(nwchar_t *)tokcstr.data;
2537 tok = TOK_LCHAR;
2538 }
2539 } else {
2540 tokc.cstr = &tokcstr;
2541 if (!is_long)
2542 tok = TOK_STR;
2543 else
2544 tok = TOK_LSTR;
2545 }
2546 }
2547 break;
2548
2549 case '<':
2550 PEEKC(c, p);
2551 if (c == '=') {
2552 p++;
2553 tok = TOK_LE;
2554 } else if (c == '<') {
2555 PEEKC(c, p);
2556 if (c == '=') {
2557 p++;
2558 tok = TOK_A_SHL;
2559 } else {
2560 tok = TOK_SHL;
2561 }
2562 } else {
2563 tok = TOK_LT;
2564 }
2565 break;
2566
2567 case '>':
2568 PEEKC(c, p);
2569 if (c == '=') {
2570 p++;
2571 tok = TOK_GE;
2572 } else if (c == '>') {
2573 PEEKC(c, p);
2574 if (c == '=') {
2575 p++;
2576 tok = TOK_A_SAR;
2577 } else {
2578 tok = TOK_SAR;
2579 }
2580 } else {
2581 tok = TOK_GT;
2582 }
2583 break;
2584
2585 case '&':
2586 PEEKC(c, p);
2587 if (c == '&') {
2588 p++;
2589 tok = TOK_LAND;
2590 } else if (c == '=') {
2591 p++;
2592 tok = TOK_A_AND;
2593 } else {
2594 tok = '&';
2595 }
2596 break;
2597
2598 case '|':
2599 PEEKC(c, p);
2600 if (c == '|') {
2601 p++;
2602 tok = TOK_LOR;
2603 } else if (c == '=') {
2604 p++;
2605 tok = TOK_A_OR;
2606 } else {
2607 tok = '|';
2608 }
2609 break;
2610
2611 case '+':
2612 PEEKC(c, p);
2613 if (c == '+') {
2614 p++;
2615 tok = TOK_INC;
2616 } else if (c == '=') {
2617 p++;
2618 tok = TOK_A_ADD;
2619 } else {
2620 tok = '+';
2621 }
2622 break;
2623
2624 case '-':
2625 PEEKC(c, p);
2626 if (c == '-') {
2627 p++;
2628 tok = TOK_DEC;
2629 } else if (c == '=') {
2630 p++;
2631 tok = TOK_A_SUB;
2632 } else if (c == '>') {
2633 p++;
2634 tok = TOK_ARROW;
2635 } else {
2636 tok = '-';
2637 }
2638 break;
2639
2640 PARSE2('!', '!', '=', TOK_NE)
2641 PARSE2('=', '=', '=', TOK_EQ)
2642 PARSE2('*', '*', '=', TOK_A_MUL)
2643 PARSE2('%', '%', '=', TOK_A_MOD)
2644 PARSE2('^', '^', '=', TOK_A_XOR)
2645
2646 /* comments or operator */
2647 case '/':
2648 PEEKC(c, p);
2649 if (c == '*') {
2650 p = parse_comment(p);
68472348
CX
Changming Xu2011-03-01 09:19:43 +08002651 /* comments replaced by a blank */
2652 tok = ' ';
2653 goto keep_tok_flags;
805990b9 grischka2009-05-05 20:17:49 +02002654 } else if (c == '/') {
2655 p = parse_line_comment(p);
68472348
CX
Changming Xu2011-03-01 09:19:43 +08002656 tok = ' ';
2657 goto keep_tok_flags;
805990b9 grischka2009-05-05 20:17:49 +02002658 } else if (c == '=') {
2659 p++;
2660 tok = TOK_A_DIV;
2661 } else {
2662 tok = '/';
2663 }
2664 break;
2665
2666 /* simple tokens */
2667 case '(':
2668 case ')':
2669 case '[':
2670 case ']':
2671 case '{':
2672 case '}':
2673 case ',':
2674 case ';':
2675 case ':':
2676 case '?':
2677 case '~':
dcb36587 seyko2015-04-12 15:32:03 +03002678 case '@': /* only used in assembler */
2679 parse_simple:
805990b9 grischka2009-05-05 20:17:49 +02002680 tok = c;
2681 p++;
2682 break;
2683 default:
d81611b6 seyko2015-04-10 16:53:29 +03002684 if ((parse_flags & PARSE_FLAG_ASM_FILE) == 0)
2685 tcc_error("unrecognized character \\x%02x", c);
2686 else {
2687 tok = ' ';
2688 p++;
2689 }
805990b9 grischka2009-05-05 20:17:49 +02002690 break;
2691 }
2692 tok_flags = 0;
2693keep_tok_flags:
2694 file->buf_ptr = p;
2695#if defined(PARSE_DEBUG)
2696 printf("token = %s\n", get_tok_str(tok, &tokc));
2697#endif
2698}
2699
2700/* return next token without macro substitution. Can read input from
2701 macro_ptr buffer */
2702static void next_nomacro_spc(void)
2703{
2704 if (macro_ptr) {
2705 redo:
2706 tok = *macro_ptr;
2707 if (tok) {
4e5170d4 grischka2010-01-14 20:58:03 +01002708 TOK_GET(&tok, &macro_ptr, &tokc);
805990b9 grischka2009-05-05 20:17:49 +02002709 if (tok == TOK_LINENUM) {
2710 file->line_num = tokc.i;
2711 goto redo;
2712 }
2713 }
2714 } else {
2715 next_nomacro1();
2716 }
2717}
2718
88a3ccab grischka2009-12-20 01:53:49 +01002719ST_FUNC void next_nomacro(void)
805990b9 grischka2009-05-05 20:17:49 +02002720{
2721 do {
2722 next_nomacro_spc();
2723 } while (is_space(tok));
2724}
2725
6e56bb38
TP
Thomas Preud'homme2014-04-12 12:00:13 +08002726/* substitute arguments in replacement lists in macro_str by the values in
2727 args (field d) and return allocated string */
4e5170d4 grischka2010-01-14 20:58:03 +01002728static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
805990b9 grischka2009-05-05 20:17:49 +02002729{
4e5170d4 grischka2010-01-14 20:58:03 +01002730 int last_tok, t, spc;
2731 const int *st;
805990b9 grischka2009-05-05 20:17:49 +02002732 Sym *s;
5879c854 grischka2014-04-04 20:18:39 +02002733 CValue cval;
805990b9 grischka2009-05-05 20:17:49 +02002734 TokenString str;
2735 CString cstr;
a6e6a954 Philip2015-05-02 13:19:14 +00002736 CString cstr2;
805990b9 grischka2009-05-05 20:17:49 +02002737
2738 tok_str_new(&str);
2739 last_tok = 0;
2740 while(1) {
4e5170d4 grischka2010-01-14 20:58:03 +01002741 TOK_GET(&t, &macro_str, &cval);
805990b9 grischka2009-05-05 20:17:49 +02002742 if (!t)
2743 break;
2744 if (t == '#') {
2745 /* stringize */
4e5170d4 grischka2010-01-14 20:58:03 +01002746 TOK_GET(&t, &macro_str, &cval);
805990b9 grischka2009-05-05 20:17:49 +02002747 if (!t)
2748 break;
2749 s = sym_find2(args, t);
2750 if (s) {
2751 cstr_new(&cstr);
bed17847 grischka2009-07-06 21:16:41 +02002752 st = s->d;
805990b9 grischka2009-05-05 20:17:49 +02002753 spc = 0;
2754 while (*st) {
4e5170d4 grischka2010-01-14 20:58:03 +01002755 TOK_GET(&t, &st, &cval);
a6b94eff Philip2015-05-01 14:48:25 +00002756 if (t != TOK_PLCHLDR && !check_space(t, &spc))
805990b9 grischka2009-05-05 20:17:49 +02002757 cstr_cat(&cstr, get_tok_str(t, &cval));
2758 }
2759 cstr.size -= spc;
2760 cstr_ccat(&cstr, '\0');
2761#ifdef PP_DEBUG
2762 printf("stringize: %s\n", (char *)cstr.data);
2763#endif
2764 /* add string */
a6e6a954
P
Philip2015-05-02 13:19:14 +00002765 cstr_new(&cstr2);
2766 /* emulate GCC behaviour and parse escapes in the token string */
2767 parse_escape_string(&cstr2, cstr.data, 0);
805990b9 grischka2009-05-05 20:17:49 +02002768 cstr_free(&cstr);
a6e6a954
P
Philip2015-05-02 13:19:14 +00002769 cval.cstr = &cstr2;
2770 tok_str_add2(&str, TOK_STR, &cval);
2771 cstr_free(cval.cstr);
805990b9 grischka2009-05-05 20:17:49 +02002772 } else {
2773 tok_str_add2(&str, t, &cval);
2774 }
2775 } else if (t >= TOK_IDENT) {
2776 s = sym_find2(args, t);
2777 if (s) {
bed17847 grischka2009-07-06 21:16:41 +02002778 st = s->d;
805990b9 grischka2009-05-05 20:17:49 +02002779 /* if '##' is present before or after, no arg substitution */
2780 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
2781 /* special case for var arg macros : ## eats the
2782 ',' if empty VA_ARGS variable. */
2783 /* XXX: test of the ',' is not 100%
2784 reliable. should fix it to avoid security
2785 problems */
2786 if (gnu_ext && s->type.t &&
2787 last_tok == TOK_TWOSHARPS &&
2788 str.len >= 2 && str.str[str.len - 2] == ',') {
2f90db43
P
Philip2015-05-02 14:27:49 +00002789 str.len -= 2;
2790 tok_str_add(&str, TOK_GNUCOMMA);
2791 }
2792
2793 for(;;) {
805990b9 grischka2009-05-05 20:17:49 +02002794 int t1;
2f90db43
P
Philip2015-05-02 14:27:49 +00002795 TOK_GET(&t1, &st, &cval);
2796 if (!t1)
2797 break;
2798 tok_str_add2(&str, t1, &cval);
805990b9 grischka2009-05-05 20:17:49 +02002799 }
2f90db43 Philip2015-05-02 14:27:49 +00002800 } else {
805990b9 grischka2009-05-05 20:17:49 +02002801 /* NOTE: the stream cannot be read when macro
2802 substituing an argument */
2803 macro_subst(&str, nested_list, st, NULL);
2804 }
2805 } else {
2806 tok_str_add(&str, t);
2807 }
2808 } else {
2809 tok_str_add2(&str, t, &cval);
2810 }
2811 last_tok = t;
2812 }
2f90db43
P
Philip2015-05-02 14:27:49 +00002813 if (str.len == 0)
2814 tok_str_add(&str, TOK_PLCHLDR);
805990b9 grischka2009-05-05 20:17:49 +02002815 tok_str_add(&str, 0);
2816 return str.str;
2817}
2818
2819static char const ab_month_name[12][4] =
2820{
2821 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2822 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2823};
2824
2825/* do macro substitution of current token with macro 's' and add
2826 result to (tok_str,tok_len). 'nested_list' is the list of all
2827 macros we got inside to avoid recursing. Return non zero if no
2828 substitution needs to be done */
2829static int macro_subst_tok(TokenString *tok_str,
2830 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
2831{
2832 Sym *args, *sa, *sa1;
4e5170d4 grischka2010-01-14 20:58:03 +01002833 int mstr_allocated, parlevel, *mstr, t, t1, spc;
2834 const int *p;
805990b9 grischka2009-05-05 20:17:49 +02002835 TokenString str;
2836 char *cstrval;
5879c854 grischka2014-04-04 20:18:39 +02002837 CValue cval;
805990b9 grischka2009-05-05 20:17:49 +02002838 CString cstr;
2839 char buf[32];
2840
2841 /* if symbol is a macro, prepare substitution */
2842 /* special macros */
2843 if (tok == TOK___LINE__) {
2844 snprintf(buf, sizeof(buf), "%d", file->line_num);
2845 cstrval = buf;
2846 t1 = TOK_PPNUM;
2847 goto add_cstr1;
2848 } else if (tok == TOK___FILE__) {
2849 cstrval = file->filename;
2850 goto add_cstr;
2851 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
2852 time_t ti;
2853 struct tm *tm;
2854
2855 time(&ti);
2856 tm = localtime(&ti);
2857 if (tok == TOK___DATE__) {
2858 snprintf(buf, sizeof(buf), "%s %2d %d",
2859 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
2860 } else {
2861 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
2862 tm->tm_hour, tm->tm_min, tm->tm_sec);
2863 }
2864 cstrval = buf;
2865 add_cstr:
2866 t1 = TOK_STR;
2867 add_cstr1:
2868 cstr_new(&cstr);
2869 cstr_cat(&cstr, cstrval);
2870 cstr_ccat(&cstr, '\0');
2871 cval.cstr = &cstr;
2872 tok_str_add2(tok_str, t1, &cval);
2873 cstr_free(&cstr);
2874 } else {
2f50cefb Philip2015-05-02 13:55:42 +00002875 int mtok = tok;
a6e6a954
P
Philip2015-05-02 13:19:14 +00002876 int saved_parse_flags = parse_flags;
2877 parse_flags |= PARSE_FLAG_ACCEPT_STRAYS | PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED;
2878
bed17847 grischka2009-07-06 21:16:41 +02002879 mstr = s->d;
805990b9 grischka2009-05-05 20:17:49 +02002880 mstr_allocated = 0;
2881 if (s->type.t == MACRO_FUNC) {
2f50cefb
P
Philip2015-05-02 13:55:42 +00002882 TokenString ws_str; /* whitespace between macro name and
2883 * argument list */
2884 tok_str_new(&ws_str);
2885 spc = 0;
805990b9 grischka2009-05-05 20:17:49 +02002886 /* NOTE: we do not use next_nomacro to avoid eating the
2887 next token. XXX: find better solution */
2888 redo:
2889 if (macro_ptr) {
2890 p = macro_ptr;
2f50cefb Philip2015-05-02 13:55:42 +00002891 while (is_space(t = *p) || TOK_LINEFEED == t) {
3b4c42c3 Philip2015-05-02 16:49:12 +00002892 if (saved_parse_flags & PARSE_FLAG_SPACES)
27ec4f67 Philip2015-05-02 17:14:07 +00002893 tok_str_add(&ws_str, t);
805990b9 grischka2009-05-05 20:17:49 +02002894 ++p;
2f50cefb Philip2015-05-02 13:55:42 +00002895 }
805990b9 grischka2009-05-05 20:17:49 +02002896 if (t == 0 && can_read_stream) {
2897 /* end of macro stream: we must look at the token
2898 after in the file */
2899 struct macro_level *ml = *can_read_stream;
2900 macro_ptr = NULL;
2901 if (ml)
2902 {
2903 macro_ptr = ml->p;
2904 ml->p = NULL;
2905 *can_read_stream = ml -> prev;
2906 }
d01f65ef
JS
Joe Soroka2011-07-08 00:55:34 -07002907 /* also, end of scope for nested defined symbol */
2908 (*nested_list)->v = -1;
805990b9 grischka2009-05-05 20:17:49 +02002909 goto redo;
2910 }
2911 } else {
2e04fa88 Philip2015-04-30 19:07:44 +00002912 ch = tcc_peekc_slow(file);
4c0d70ab
MM
Michael Matz2012-04-16 00:21:40 +02002913 while (is_space(ch) || ch == '\n' || ch == '/')
2914 {
2915 if (ch == '/')
2f50cefb Philip2015-05-02 13:55:42 +00002916 {
4c0d70ab
MM
Michael Matz2012-04-16 00:21:40 +02002917 int c;
2918 uint8_t *p = file->buf_ptr;
2919 PEEKC(c, p);
2920 if (c == '*') {
2921 p = parse_comment(p);
2922 file->buf_ptr = p - 1;
2923 } else if (c == '/') {
2924 p = parse_line_comment(p);
2925 file->buf_ptr = p - 1;
2926 } else
2f50cefb
P
Philip2015-05-02 13:55:42 +00002927 break;
2928 ch = ' ';
2929 }
27ec4f67
P
Philip2015-05-02 17:14:07 +00002930 if (saved_parse_flags & PARSE_FLAG_SPACES)
2931 tok_str_add(&ws_str, ch);
2f50cefb
P
Philip2015-05-02 13:55:42 +00002932 cinp();
2933 }
805990b9 grischka2009-05-05 20:17:49 +02002934 t = ch;
2935 }
a6e6a954 Philip2015-05-02 13:19:14 +00002936 if (t != '(') {
2f50cefb
P
Philip2015-05-02 13:55:42 +00002937 /* not a macro substitution after all, restore the
2938 * macro token plus all whitespace we've read.
2939 * whitespace is intentionally not merged to preserve
2940 * newlines. */
2941 int i;
2942 tok_str_add(tok_str, mtok);
2943 for(i=0; i<ws_str.len; i++)
2944 tok_str_add(tok_str, ws_str.str[i]);
2945 tok_str_free(ws_str.str);
a6e6a954 Philip2015-05-02 13:19:14 +00002946 parse_flags = saved_parse_flags;
805990b9 grischka2009-05-05 20:17:49 +02002947 return -1;
2f50cefb
P
Philip2015-05-02 13:55:42 +00002948 } else {
2949 tok_str_free(ws_str.str);
a6e6a954 Philip2015-05-02 13:19:14 +00002950 }
805990b9 grischka2009-05-05 20:17:49 +02002951 /* argument macro */
2952 next_nomacro();
2953 next_nomacro();
2954 args = NULL;
2955 sa = s->next;
2956 /* NOTE: empty args are allowed, except if no args */
2957 for(;;) {
2958 /* handle '()' case */
2959 if (!args && !sa && tok == ')')
2960 break;
2961 if (!sa)
bf374a5f grischka2011-08-11 17:07:56 +02002962 tcc_error("macro '%s' used with too many args",
805990b9 grischka2009-05-05 20:17:49 +02002963 get_tok_str(s->v, 0));
2964 tok_str_new(&str);
2965 parlevel = spc = 0;
2966 /* NOTE: non zero sa->t indicates VA_ARGS */
2967 while ((parlevel > 0 ||
2968 (tok != ')' &&
a3d78b95
P
Philip2015-05-02 12:33:45 +00002969 (tok != ',' || sa->type.t)))) {
2970 if (tok == TOK_EOF || tok == 0)
2971 break;
805990b9 grischka2009-05-05 20:17:49 +02002972 if (tok == '(')
2973 parlevel++;
2974 else if (tok == ')')
2975 parlevel--;
2976 if (tok == TOK_LINEFEED)
2977 tok = ' ';
2978 if (!check_space(tok, &spc))
2979 tok_str_add2(&str, tok, &tokc);
2980 next_nomacro_spc();
2981 }
823d0583
P
Philip2015-05-02 14:47:11 +00002982 if (parlevel)
2983 expect(")");
805990b9 grischka2009-05-05 20:17:49 +02002984 str.len -= spc;
2f90db43
P
Philip2015-05-02 14:27:49 +00002985 if (str.len == 0)
2986 tok_str_add(&str, TOK_PLCHLDR);
805990b9 grischka2009-05-05 20:17:49 +02002987 tok_str_add(&str, 0);
bed17847 grischka2009-07-06 21:16:41 +02002988 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
2989 sa1->d = str.str;
805990b9 grischka2009-05-05 20:17:49 +02002990 sa = sa->next;
2991 if (tok == ')') {
2992 /* special case for gcc var args: add an empty
2993 var arg argument if it is omitted */
2994 if (sa && sa->type.t && gnu_ext)
2995 continue;
2996 else
2997 break;
2998 }
929d171f
P
Philip2015-05-02 20:30:07 +00002999 if (tok != ',')
3000 expect(",");
805990b9 grischka2009-05-05 20:17:49 +02003001 next_nomacro();
3002 }
3003 if (sa) {
bf374a5f grischka2011-08-11 17:07:56 +02003004 tcc_error("macro '%s' used with too few args",
805990b9 grischka2009-05-05 20:17:49 +02003005 get_tok_str(s->v, 0));
3006 }
3007
3008 /* now subst each arg */
3009 mstr = macro_arg_subst(nested_list, mstr, args);
3010 /* free memory */
3011 sa = args;
3012 while (sa) {
3013 sa1 = sa->prev;
bed17847 grischka2009-07-06 21:16:41 +02003014 tok_str_free(sa->d);
805990b9 grischka2009-05-05 20:17:49 +02003015 sym_free(sa);
3016 sa = sa1;
3017 }
3018 mstr_allocated = 1;
3019 }
3020 sym_push2(nested_list, s->v, 0, 0);
a6e6a954 Philip2015-05-02 13:19:14 +00003021 parse_flags = saved_parse_flags;
805990b9 grischka2009-05-05 20:17:49 +02003022 macro_subst(tok_str, nested_list, mstr, can_read_stream);
3023 /* pop nested defined symbol */
3024 sa1 = *nested_list;
3025 *nested_list = sa1->prev;
3026 sym_free(sa1);
3027 if (mstr_allocated)
3028 tok_str_free(mstr);
3029 }
3030 return 0;
3031}
3032
3033/* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3034 return the resulting string (which must be freed). */
3035static inline int *macro_twosharps(const int *macro_str)
3036{
fcdb663d grischka2009-07-06 21:12:18 +02003037 const int *ptr;
805990b9 grischka2009-05-05 20:17:49 +02003038 int t;
805990b9 grischka2009-05-05 20:17:49 +02003039 TokenString macro_str1;
3040 CString cstr;
38756b50 Joe Soroka2011-07-08 02:51:06 -07003041 int n, start_of_nosubsts;
805990b9 grischka2009-05-05 20:17:49 +02003042
3043 /* we search the first '##' */
3044 for(ptr = macro_str;;) {
944627c4 grischka2013-02-14 06:53:07 +01003045 CValue cval;
4e5170d4 grischka2010-01-14 20:58:03 +01003046 TOK_GET(&t, &ptr, &cval);
805990b9 grischka2009-05-05 20:17:49 +02003047 if (t == TOK_TWOSHARPS)
3048 break;
3049 /* nothing more to do if end of string */
3050 if (t == 0)
3051 return NULL;
3052 }
3053
3054 /* we saw '##', so we need more processing to handle it */
38756b50 Joe Soroka2011-07-08 02:51:06 -07003055 start_of_nosubsts = -1;
805990b9 grischka2009-05-05 20:17:49 +02003056 tok_str_new(&macro_str1);
fcdb663d grischka2009-07-06 21:12:18 +02003057 for(ptr = macro_str;;) {
4e5170d4 grischka2010-01-14 20:58:03 +01003058 TOK_GET(&tok, &ptr, &tokc);
805990b9 grischka2009-05-05 20:17:49 +02003059 if (tok == 0)
3060 break;
929d171f
P
Philip2015-05-02 20:30:07 +00003061 if (tok == TOK_TWOSHARPS)
3062 continue;
38756b50
JS
Joe Soroka2011-07-08 02:51:06 -07003063 if (tok == TOK_NOSUBST && start_of_nosubsts < 0)
3064 start_of_nosubsts = macro_str1.len;
fcdb663d grischka2009-07-06 21:12:18 +02003065 while (*ptr == TOK_TWOSHARPS) {
38756b50
JS
Joe Soroka2011-07-08 02:51:06 -07003066 /* given 'a##b', remove nosubsts preceding 'a' */
3067 if (start_of_nosubsts >= 0)
3068 macro_str1.len = start_of_nosubsts;
3069 /* given 'a##b', skip '##' */
3070 t = *++ptr;
3071 /* given 'a##b', remove nosubsts preceding 'b' */
3072 while (t == TOK_NOSUBST)
3073 t = *++ptr;
805990b9 grischka2009-05-05 20:17:49 +02003074 if (t && t != TOK_TWOSHARPS) {
944627c4 grischka2013-02-14 06:53:07 +01003075 CValue cval;
4e5170d4 grischka2010-01-14 20:58:03 +01003076 TOK_GET(&t, &ptr, &cval);
fcdb663d grischka2009-07-06 21:12:18 +02003077 /* We concatenate the two tokens */
3078 cstr_new(&cstr);
6e56bb38
TP
Thomas Preud'homme2014-04-12 12:00:13 +08003079 if (tok != TOK_PLCHLDR)
3080 cstr_cat(&cstr, get_tok_str(tok, &tokc));
fcdb663d grischka2009-07-06 21:12:18 +02003081 n = cstr.size;
929d171f Philip2015-05-02 20:30:07 +00003082 if (t != TOK_PLCHLDR || tok == TOK_PLCHLDR)
6e56bb38 Thomas Preud'homme2014-04-12 12:00:13 +08003083 cstr_cat(&cstr, get_tok_str(t, &cval));
805990b9 grischka2009-05-05 20:17:49 +02003084 cstr_ccat(&cstr, '\0');
3085
944627c4 grischka2013-02-14 06:53:07 +01003086 tcc_open_bf(tcc_state, ":paste:", cstr.size);
44e84bb2 grischka2010-11-25 13:30:31 +01003087 memcpy(file->buffer, cstr.data, cstr.size);
fcdb663d grischka2009-07-06 21:12:18 +02003088 for (;;) {
3089 next_nomacro1();
3090 if (0 == *file->buf_ptr)
3091 break;
3092 tok_str_add2(&macro_str1, tok, &tokc);
bf374a5f grischka2011-08-11 17:07:56 +02003093 tcc_warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
fcdb663d grischka2009-07-06 21:12:18 +02003094 n, cstr.data, (char*)cstr.data + n);
805990b9 grischka2009-05-05 20:17:49 +02003095 }
44e84bb2 grischka2010-11-25 13:30:31 +01003096 tcc_close();
0a8c7d14 grischka2013-01-06 17:21:33 +01003097 cstr_free(&cstr);
805990b9 grischka2009-05-05 20:17:49 +02003098 }
3099 }
6e56bb38 Thomas Preud'homme2014-04-12 12:00:13 +08003100 if (tok != TOK_NOSUBST) {
6b7a6fcb
TP
Thomas Preud'homme2014-04-14 20:45:10 +08003101 tok_str_add2(&macro_str1, tok, &tokc);
3102 tok = ' ';
38756b50 Joe Soroka2011-07-08 02:51:06 -07003103 start_of_nosubsts = -1;
929d171f
P
Philip2015-05-02 20:30:07 +00003104 }
3105 tok_str_add2(&macro_str1, tok, &tokc);
805990b9 grischka2009-05-05 20:17:49 +02003106 }
805990b9 grischka2009-05-05 20:17:49 +02003107 tok_str_add(&macro_str1, 0);
3108 return macro_str1.str;
3109}
3110
3111
3112/* do macro substitution of macro_str and add result to
3113 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3114 inside to avoid recursing. */
3115static void macro_subst(TokenString *tok_str, Sym **nested_list,
3116 const int *macro_str, struct macro_level ** can_read_stream)
3117{
3118 Sym *s;
3119 int *macro_str1;
3120 const int *ptr;
2f50cefb Philip2015-05-02 13:55:42 +00003121 int t, spc;
5879c854 grischka2014-04-04 20:18:39 +02003122 CValue cval;
805990b9 grischka2009-05-05 20:17:49 +02003123 struct macro_level ml;
929d171f Philip2015-05-02 20:30:07 +00003124 int force_blank;
2f90db43 Philip2015-05-02 14:27:49 +00003125 int gnucomma_index = -1;
805990b9 grischka2009-05-05 20:17:49 +02003126
3127 /* first scan for '##' operator handling */
3128 ptr = macro_str;
3129 macro_str1 = macro_twosharps(ptr);
185fba41 Changming Xu2011-02-27 10:15:15 +08003130
805990b9 grischka2009-05-05 20:17:49 +02003131 if (macro_str1)
3132 ptr = macro_str1;
3133 spc = 0;
929d171f Philip2015-05-02 20:30:07 +00003134 force_blank = 0;
68472348 Changming Xu2011-03-01 09:19:43 +08003135
805990b9 grischka2009-05-05 20:17:49 +02003136 while (1) {
3137 /* NOTE: ptr == NULL can only happen if tokens are read from
3138 file stream due to a macro function call */
3139 if (ptr == NULL)
3140 break;
4e5170d4 grischka2010-01-14 20:58:03 +01003141 TOK_GET(&t, &ptr, &cval);
805990b9 grischka2009-05-05 20:17:49 +02003142 if (t == 0)
3143 break;
a6e6a954
P
Philip2015-05-02 13:19:14 +00003144 if (t == '\\' && !(parse_flags & PARSE_FLAG_ACCEPT_STRAYS)) {
3145 tcc_error("stray '\\' in program");
3146 }
75c66959
JS
Joe Soroka2011-02-01 13:23:40 -08003147 if (t == TOK_NOSUBST) {
3148 /* following token has already been subst'd. just copy it on */
3149 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3150 TOK_GET(&t, &ptr, &cval);
3151 goto no_subst;
3152 }
2f90db43
P
Philip2015-05-02 14:27:49 +00003153 if (t == TOK_GNUCOMMA) {
3154 if (gnucomma_index != -1)
3155 tcc_error("two GNU commas in the same macro");
3156 gnucomma_index = tok_str->len;
3157 tok_str_add(tok_str, ',');
3158 TOK_GET(&t, &ptr, &cval);
3159 }
805990b9 grischka2009-05-05 20:17:49 +02003160 s = define_find(t);
3161 if (s != NULL) {
2f90db43 Philip2015-05-02 14:27:49 +00003162 int old_len = tok_str->len;
805990b9 grischka2009-05-05 20:17:49 +02003163 /* if nested substitution, do nothing */
75c66959
JS
Joe Soroka2011-02-01 13:23:40 -08003164 if (sym_find2(*nested_list, t)) {
3165 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3166 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
805990b9 grischka2009-05-05 20:17:49 +02003167 goto no_subst;
75c66959 Joe Soroka2011-02-01 13:23:40 -08003168 }
805990b9 grischka2009-05-05 20:17:49 +02003169 ml.p = macro_ptr;
3170 if (can_read_stream)
3171 ml.prev = *can_read_stream, *can_read_stream = &ml;
3172 macro_ptr = (int *)ptr;
3173 tok = t;
2f50cefb Philip2015-05-02 13:55:42 +00003174 macro_subst_tok(tok_str, nested_list, s, can_read_stream);
1e878200 Philip2015-05-02 12:26:10 +00003175 spc = tok_str->len && is_space(tok_str->str[tok_str->len-1]);
805990b9 grischka2009-05-05 20:17:49 +02003176 ptr = (int *)macro_ptr;
3177 macro_ptr = ml.p;
3178 if (can_read_stream && *can_read_stream == &ml)
3179 *can_read_stream = ml.prev;
929d171f
P
Philip2015-05-02 20:30:07 +00003180 if (parse_flags & PARSE_FLAG_SPACES)
3181 force_blank = 1;
2f90db43
P
Philip2015-05-02 14:27:49 +00003182 if (old_len == tok_str->len)
3183 tok_str_add(tok_str, TOK_PLCHLDR);
805990b9 grischka2009-05-05 20:17:49 +02003184 } else {
3185 no_subst:
929d171f
P
Philip2015-05-02 20:30:07 +00003186 if (force_blank) {
3187 tok_str_add(tok_str, ' ');
3188 spc = 1;
3189 force_blank = 0;
3190 }
3191 if (!check_space(t, &spc))
805990b9 grischka2009-05-05 20:17:49 +02003192 tok_str_add2(tok_str, t, &cval);
3193 }
2f90db43
P
Philip2015-05-02 14:27:49 +00003194 if (gnucomma_index != -1) {
3195 if (tok_str->len >= gnucomma_index+2) {
3196 if (tok_str->str[gnucomma_index+1] == TOK_PLCHLDR)
3197 tok_str->len -= 2;
3198 gnucomma_index = -1;
3199 }
3200 }
3201 if (tok_str->len && tok_str->str[tok_str->len-1] == TOK_PLCHLDR)
3202 tok_str->len--;
805990b9 grischka2009-05-05 20:17:49 +02003203 }
3204 if (macro_str1)
3205 tok_str_free(macro_str1);
3206}
3207
3208/* return next token with macro substitution */
88a3ccab grischka2009-12-20 01:53:49 +01003209ST_FUNC void next(void)
805990b9 grischka2009-05-05 20:17:49 +02003210{
3211 Sym *nested_list, *s;
3212 TokenString str;
3213 struct macro_level *ml;
3214
3215 redo:
3216 if (parse_flags & PARSE_FLAG_SPACES)
3217 next_nomacro_spc();
3218 else
3219 next_nomacro();
3220 if (!macro_ptr) {
3221 /* if not reading from macro substituted string, then try
3222 to substitute macros */
3223 if (tok >= TOK_IDENT &&
3224 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3225 s = define_find(tok);
3226 if (s) {
3227 /* we have a macro: we try to substitute */
3228 tok_str_new(&str);
3229 nested_list = NULL;
3230 ml = NULL;
3231 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
3232 /* substitution done, NOTE: maybe empty */
3233 tok_str_add(&str, 0);
3234 macro_ptr = str.str;
3235 macro_ptr_allocated = str.str;
3236 goto redo;
2f50cefb
P
Philip2015-05-02 13:55:42 +00003237 } else {
3238 tok_str_add(&str, 0);
3239 if (str.len > 1) {
3240 macro_ptr = str.str + 1;
3241 macro_ptr_allocated = str.str;
3242 }
3243 tok = str.str[0];
805990b9 grischka2009-05-05 20:17:49 +02003244 }
3245 }
3246 }
3247 } else {
3248 if (tok == 0) {
3249 /* end of macro or end of unget buffer */
3250 if (unget_buffer_enabled) {
3251 macro_ptr = unget_saved_macro_ptr;
3252 unget_buffer_enabled = 0;
3253 } else {
3254 /* end of macro string: free it */
3255 tok_str_free(macro_ptr_allocated);
d923e652 grischka2009-06-19 15:12:58 +02003256 macro_ptr_allocated = NULL;
805990b9 grischka2009-05-05 20:17:49 +02003257 macro_ptr = NULL;
3258 }
3259 goto redo;
75c66959
JS
Joe Soroka2011-02-01 13:23:40 -08003260 } else if (tok == TOK_NOSUBST) {
3261 /* discard preprocessor's nosubst markers */
3262 goto redo;
805990b9 grischka2009-05-05 20:17:49 +02003263 }
3264 }
3265
3266 /* convert preprocessor tokens into C tokens */
3267 if (tok == TOK_PPNUM &&
3268 (parse_flags & PARSE_FLAG_TOK_NUM)) {
3269 parse_number((char *)tokc.cstr->data);
3270 }
3271}
3272
3273/* push back current token and set current token to 'last_tok'. Only
3274 identifier case handled for labels. */
88a3ccab grischka2009-12-20 01:53:49 +01003275ST_INLN void unget_tok(int last_tok)
805990b9 grischka2009-05-05 20:17:49 +02003276{
3277 int i, n;
3278 int *q;
15f4ac2b
MM
Michael Matz2012-04-15 22:17:51 +02003279 if (unget_buffer_enabled)
3280 {
3281 /* assert(macro_ptr == unget_saved_buffer + 1);
3282 assert(*macro_ptr == 0); */
3283 }
3284 else
3285 {
3286 unget_saved_macro_ptr = macro_ptr;
3287 unget_buffer_enabled = 1;
3288 }
805990b9 grischka2009-05-05 20:17:49 +02003289 q = unget_saved_buffer;
3290 macro_ptr = q;
3291 *q++ = tok;
3292 n = tok_ext_size(tok) - 1;
3293 for(i=0;i<n;i++)
3294 *q++ = tokc.tab[i];
3295 *q = 0; /* end of token string */
3296 tok = last_tok;
3297}
3298
3299
b8f6e1ae grischka2009-05-05 20:30:39 +02003300/* better than nothing, but needs extension to handle '-E' option
3301 correctly too */
88a3ccab grischka2009-12-20 01:53:49 +01003302ST_FUNC void preprocess_init(TCCState *s1)
b8f6e1ae grischka2009-05-05 20:30:39 +02003303{
3304 s1->include_stack_ptr = s1->include_stack;
3305 /* XXX: move that before to avoid having to initialize
3306 file->ifdef_stack_ptr ? */
3307 s1->ifdef_stack_ptr = s1->ifdef_stack;
3308 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
3309
b8f6e1ae grischka2009-05-05 20:30:39 +02003310 vtop = vstack - 1;
3311 s1->pack_stack[0] = 0;
3312 s1->pack_stack_ptr = s1->pack_stack;
3313}
3314
cec76c8b Urs Janssen2013-02-15 12:48:33 +01003315ST_FUNC void preprocess_new(void)
b8f6e1ae grischka2009-05-05 20:30:39 +02003316{
3317 int i, c;
3318 const char *p, *r;
b8f6e1ae grischka2009-05-05 20:30:39 +02003319
3320 /* init isid table */
5ce2154c seyko2015-04-20 03:44:08 +03003321
b8f6e1ae grischka2009-05-05 20:30:39 +02003322 for(i=CH_EOF;i<256;i++)
5ce2154c seyko2015-04-20 03:44:08 +03003323 isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
b8f6e1ae grischka2009-05-05 20:30:39 +02003324
3325 /* add all tokens */
a429d40f seyko2015-03-03 14:54:46 +03003326 if (table_ident) {
3327 tcc_free (table_ident);
3328 table_ident = NULL;
3329 }
b8f6e1ae grischka2009-05-05 20:30:39 +02003330 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
3331
3332 tok_ident = TOK_IDENT;
3333 p = tcc_keywords;
3334 while (*p) {
3335 r = p;
3336 for(;;) {
3337 c = *r++;
3338 if (c == '\0')
3339 break;
3340 }
ee06ef9d Thomas Preud'homme2011-05-16 14:15:32 +02003341 tok_alloc(p, r - p - 1);
b8f6e1ae grischka2009-05-05 20:30:39 +02003342 p = r;
3343 }
3344}
3345
50cdccf3 seyko2015-03-03 14:19:14 +03003346static void line_macro_output(BufferedFile *f, const char *s, TCCState *s1)
3347{
3348 switch (s1->Pflag) {
3349 case LINE_MACRO_OUTPUT_FORMAT_STD:
3350 /* "tcc -E -P1" case */
3351 fprintf(s1->ppfp, "# line %d \"%s\"\n", f->line_num, f->filename);
3352 break;
3353
3354 case LINE_MACRO_OUTPUT_FORMAT_NONE:
3355 /* "tcc -E -P" case: don't output a line directive */
3356 break;
3357
3358 case LINE_MACRO_OUTPUT_FORMAT_GCC:
3359 default:
3360 /* "tcc -E" case: a gcc standard by default */
3361 fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename, s);
3362 break;
3363 }
3364}
3365
b8f6e1ae grischka2009-05-05 20:30:39 +02003366/* Preprocess the current file */
88a3ccab grischka2009-12-20 01:53:49 +01003367ST_FUNC int tcc_preprocess(TCCState *s1)
b8f6e1ae grischka2009-05-05 20:30:39 +02003368{
e4ae77c7 grischka2009-06-17 02:10:42 +02003369 BufferedFile *file_ref, **iptr, **iptr_new;
40418f87 seyko2015-03-03 14:15:28 +03003370 int token_seen, d;
e4ae77c7 grischka2009-06-17 02:10:42 +02003371 const char *s;
b8f6e1ae grischka2009-05-05 20:30:39 +02003372
3373 preprocess_init(s1);
b8f6e1ae grischka2009-05-05 20:30:39 +02003374 ch = file->buf_ptr[0];
3375 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
bbcb54a1 seyko2015-04-27 16:36:58 +03003376 parse_flags &= PARSE_FLAG_ASM_FILE;
0877ba7c Philip2015-05-02 12:58:37 +00003377 parse_flags |= PARSE_FLAG_PREPROCESS | PARSE_FLAG_LINEFEED | PARSE_FLAG_SPACES | PARSE_FLAG_ACCEPT_STRAYS;
b8f6e1ae grischka2009-05-05 20:30:39 +02003378 token_seen = 0;
40418f87 seyko2015-03-03 14:15:28 +03003379 file->line_ref = 0;
b8f6e1ae grischka2009-05-05 20:30:39 +02003380 file_ref = NULL;
e4ae77c7 grischka2009-06-17 02:10:42 +02003381 iptr = s1->include_stack_ptr;
88a3ccab grischka2009-12-20 01:53:49 +01003382
b8f6e1ae grischka2009-05-05 20:30:39 +02003383 for (;;) {
3384 next();
3385 if (tok == TOK_EOF) {
3386 break;
e4ae77c7 grischka2009-06-17 02:10:42 +02003387 } else if (file != file_ref) {
50cdccf3 seyko2015-03-03 14:19:14 +03003388 if (file_ref)
3389 line_macro_output(file_ref, "", s1);
e4ae77c7 grischka2009-06-17 02:10:42 +02003390 goto print_line;
b8f6e1ae grischka2009-05-05 20:30:39 +02003391 } else if (tok == TOK_LINEFEED) {
3392 if (!token_seen)
3393 continue;
40418f87 seyko2015-03-03 14:15:28 +03003394 file->line_ref++;
b8f6e1ae grischka2009-05-05 20:30:39 +02003395 token_seen = 0;
3396 } else if (!token_seen) {
40418f87 seyko2015-03-03 14:15:28 +03003397 d = file->line_num - file->line_ref;
50cdccf3 seyko2015-03-03 14:19:14 +03003398 if (file != file_ref || d >= 8) {
e4ae77c7 grischka2009-06-17 02:10:42 +02003399print_line:
50cdccf3 seyko2015-03-03 14:19:14 +03003400 s = "";
3401 if (tcc_state->Pflag == LINE_MACRO_OUTPUT_FORMAT_GCC) {
3402 iptr_new = s1->include_stack_ptr;
3403 s = iptr_new > iptr ? " 1"
3404 : iptr_new < iptr ? " 2"
3405 : iptr_new > s1->include_stack ? " 3"
3406 : ""
3407 ;
3408 }
3409 line_macro_output(file, s, s1);
e4ae77c7 grischka2009-06-17 02:10:42 +02003410 } else {
50cdccf3 seyko2015-03-03 14:19:14 +03003411 while (d > 0)
05108a3b grischka2013-02-12 19:13:28 +01003412 fputs("\n", s1->ppfp), --d;
e4ae77c7 grischka2009-06-17 02:10:42 +02003413 }
40418f87 seyko2015-03-03 14:15:28 +03003414 file->line_ref = (file_ref = file)->line_num;
e4ae77c7 grischka2009-06-17 02:10:42 +02003415 token_seen = tok != TOK_LINEFEED;
3416 if (!token_seen)
3417 continue;
b8f6e1ae grischka2009-05-05 20:30:39 +02003418 }
05108a3b grischka2013-02-12 19:13:28 +01003419 fputs(get_tok_str(tok, &tokc), s1->ppfp);
b8f6e1ae grischka2009-05-05 20:30:39 +02003420 }
b8f6e1ae grischka2009-05-05 20:30:39 +02003421 return 0;
3422}