move grow_symbol function into unicode.h
[cparser] / preprocessor.c
1 #include <config.h>
2
3 #include <assert.h>
4 #include <errno.h>
5 #include <string.h>
6 #include <stdbool.h>
7 #include <ctype.h>
8
9 #include "token_t.h"
10 #include "symbol_t.h"
11 #include "adt/util.h"
12 #include "adt/error.h"
13 #include "adt/strutil.h"
14 #include "adt/strset.h"
15 #include "lang_features.h"
16 #include "diagnostic.h"
17 #include "string_rep.h"
18 #include "input.h"
19
20 #define MAX_PUTBACK 3
21 #define INCLUDE_LIMIT 199  /* 199 is for gcc "compatibility" */
22
23 struct pp_argument_t {
24         size_t   list_len;
25         token_t *token_list;
26 };
27
28 struct pp_definition_t {
29         symbol_t          *symbol;
30         source_position_t  source_position;
31         pp_definition_t   *parent_expansion;
32         size_t             expand_pos;
33         bool               is_variadic    : 1;
34         bool               is_expanding   : 1;
35         bool               has_parameters : 1;
36         size_t             n_parameters;
37         symbol_t          *parameters;
38
39         /* replacement */
40         size_t             list_len;
41         token_t           *token_list;
42
43 };
44
45 typedef struct pp_conditional_t pp_conditional_t;
46 struct pp_conditional_t {
47         source_position_t  source_position;
48         bool               condition;
49         bool               in_else;
50         bool               skip; /**< conditional in skip mode (then+else gets skipped) */
51         pp_conditional_t  *parent;
52 };
53
54 typedef struct pp_input_t pp_input_t;
55 struct pp_input_t {
56         FILE              *file;
57         input_t           *input;
58         utf32              c;
59         utf32              buf[1024+MAX_PUTBACK];
60         const utf32       *bufend;
61         const utf32       *bufpos;
62         source_position_t  position;
63         pp_input_t        *parent;
64         unsigned           output_line;
65 };
66
67 /** additional info about the current token */
68 typedef struct add_token_info_t {
69         /** whitespace from beginning of line to the token */
70         unsigned whitespace;
71         /** there has been any whitespace before the token */
72         bool     had_whitespace;
73         /** the token is at the beginning of the line */
74         bool     at_line_begin;
75 } add_token_info_t;
76
77 typedef struct searchpath_entry_t searchpath_entry_t;
78 struct searchpath_entry_t {
79         const char         *path;
80         searchpath_entry_t *next;
81 };
82
83 static pp_input_t      input;
84
85 static pp_input_t     *input_stack;
86 static unsigned        n_inputs;
87 static struct obstack  input_obstack;
88
89 static pp_conditional_t *conditional_stack;
90
91 static token_t           pp_token;
92 static bool              resolve_escape_sequences = false;
93 static bool              ignore_unknown_chars     = true;
94 static bool              in_pp_directive;
95 static bool              skip_mode;
96 static FILE             *out;
97 static struct obstack    pp_obstack;
98 static struct obstack    config_obstack;
99 static const char       *printed_input_name = NULL;
100 static source_position_t expansion_pos;
101 static pp_definition_t  *current_expansion  = NULL;
102 static strset_t          stringset;
103 static preprocessor_token_kind_t last_token = TP_ERROR;
104
105 static searchpath_entry_t *searchpath;
106
107 static add_token_info_t  info;
108
109 static inline void next_char(void);
110 static void next_preprocessing_token(void);
111 static void print_line_directive(const source_position_t *pos, const char *add);
112
113 static void switch_input(FILE *file, const char *filename)
114 {
115         input.file                = file;
116         input.input               = input_from_stream(file, NULL);
117         input.bufend              = NULL;
118         input.bufpos              = NULL;
119         input.output_line         = 0;
120         input.position.input_name = filename;
121         input.position.lineno     = 1;
122
123         /* indicate that we're at a new input */
124         print_line_directive(&input.position, input_stack != NULL ? "1" : NULL);
125
126         /* place a virtual '\n' so we realize we're at line begin */
127         input.position.lineno = 0;
128         input.c               = '\n';
129         next_preprocessing_token();
130 }
131
132 static void close_input(void)
133 {
134         input_free(input.input);
135         assert(input.file != NULL);
136
137         fclose(input.file);
138         input.input  = NULL;
139         input.file   = NULL;
140         input.bufend = NULL;
141         input.bufpos = NULL;
142         input.c      = EOF;
143 }
144
145 static void push_input(void)
146 {
147         pp_input_t *saved_input
148                 = obstack_alloc(&input_obstack, sizeof(*saved_input));
149
150         memcpy(saved_input, &input, sizeof(*saved_input));
151
152         /* adjust buffer positions */
153         if (input.bufpos != NULL)
154                 saved_input->bufpos = saved_input->buf + (input.bufpos - input.buf);
155         if (input.bufend != NULL)
156                 saved_input->bufend = saved_input->buf + (input.bufend - input.buf);
157
158         saved_input->parent = input_stack;
159         input_stack         = saved_input;
160         ++n_inputs;
161 }
162
163 static void pop_restore_input(void)
164 {
165         assert(n_inputs > 0);
166         assert(input_stack != NULL);
167
168         pp_input_t *saved_input = input_stack;
169
170         memcpy(&input, saved_input, sizeof(input));
171         input.parent = NULL;
172
173         /* adjust buffer positions */
174         if (saved_input->bufpos != NULL)
175                 input.bufpos = input.buf + (saved_input->bufpos - saved_input->buf);
176         if (saved_input->bufend != NULL)
177                 input.bufend = input.buf + (saved_input->bufend - saved_input->buf);
178
179         input_stack = saved_input->parent;
180         obstack_free(&input_obstack, saved_input);
181         --n_inputs;
182 }
183
184 /**
185  * Prints a parse error message at the current token.
186  *
187  * @param msg   the error message
188  */
189 static void parse_error(const char *msg)
190 {
191         errorf(&pp_token.base.source_position,  "%s", msg);
192 }
193
194 static inline void next_real_char(void)
195 {
196         assert(input.bufpos <= input.bufend);
197         if (input.bufpos >= input.bufend) {
198                 size_t n = decode(input.input, input.buf + MAX_PUTBACK,
199                                   sizeof(input.buf)/sizeof(input.buf[0]) - MAX_PUTBACK);
200                 if (n == 0) {
201                         input.c = EOF;
202                         return;
203                 }
204                 input.bufpos = input.buf + MAX_PUTBACK;
205                 input.bufend = input.bufpos + n;
206         }
207         input.c = *input.bufpos++;
208         ++input.position.colno;
209 }
210
211 /**
212  * Put a character back into the buffer.
213  *
214  * @param pc  the character to put back
215  */
216 static inline void put_back(utf32 const pc)
217 {
218         assert(input.bufpos > input.buf);
219         *(--input.bufpos - input.buf + input.buf) = (char) pc;
220         --input.position.colno;
221 }
222
223 #define MATCH_NEWLINE(code)                   \
224         case '\r':                                \
225                 next_char();                          \
226                 if (input.c == '\n') {                \
227         case '\n':                                \
228                         next_char();                      \
229                 }                                     \
230                 info.whitespace = 0;                  \
231                 ++input.position.lineno;              \
232                 input.position.colno = 1;             \
233                 code
234
235 #define eat(c_type) (assert(input.c == c_type), next_char())
236
237 static void maybe_concat_lines(void)
238 {
239         eat('\\');
240
241         switch (input.c) {
242         MATCH_NEWLINE(
243                 return;
244         )
245
246         default:
247                 break;
248         }
249
250         put_back(input.c);
251         input.c = '\\';
252 }
253
254 /**
255  * Set c to the next input character, ie.
256  * after expanding trigraphs.
257  */
258 static inline void next_char(void)
259 {
260         next_real_char();
261
262         /* filter trigraphs and concatenated lines */
263         if (UNLIKELY(input.c == '\\')) {
264                 maybe_concat_lines();
265                 goto end_of_next_char;
266         }
267
268         if (LIKELY(input.c != '?'))
269                 goto end_of_next_char;
270
271         next_real_char();
272         if (LIKELY(input.c != '?')) {
273                 put_back(input.c);
274                 input.c = '?';
275                 goto end_of_next_char;
276         }
277
278         next_real_char();
279         switch (input.c) {
280         case '=': input.c = '#'; break;
281         case '(': input.c = '['; break;
282         case '/': input.c = '\\'; maybe_concat_lines(); break;
283         case ')': input.c = ']'; break;
284         case '\'': input.c = '^'; break;
285         case '<': input.c = '{'; break;
286         case '!': input.c = '|'; break;
287         case '>': input.c = '}'; break;
288         case '-': input.c = '~'; break;
289         default:
290                 put_back(input.c);
291                 put_back('?');
292                 input.c = '?';
293                 break;
294         }
295
296 end_of_next_char:;
297 #ifdef DEBUG_CHARS
298         printf("nchar '%c'\n", input.c);
299 #endif
300 }
301
302
303
304 /**
305  * Returns true if the given char is a octal digit.
306  *
307  * @param char  the character to check
308  */
309 static inline bool is_octal_digit(int chr)
310 {
311         switch (chr) {
312         case '0':
313         case '1':
314         case '2':
315         case '3':
316         case '4':
317         case '5':
318         case '6':
319         case '7':
320                 return true;
321         default:
322                 return false;
323         }
324 }
325
326 /**
327  * Returns the value of a digit.
328  * The only portable way to do it ...
329  */
330 static int digit_value(int digit)
331 {
332         switch (digit) {
333         case '0': return 0;
334         case '1': return 1;
335         case '2': return 2;
336         case '3': return 3;
337         case '4': return 4;
338         case '5': return 5;
339         case '6': return 6;
340         case '7': return 7;
341         case '8': return 8;
342         case '9': return 9;
343         case 'a':
344         case 'A': return 10;
345         case 'b':
346         case 'B': return 11;
347         case 'c':
348         case 'C': return 12;
349         case 'd':
350         case 'D': return 13;
351         case 'e':
352         case 'E': return 14;
353         case 'f':
354         case 'F': return 15;
355         default:
356                 panic("wrong character given");
357         }
358 }
359
360 /**
361  * Parses an octal character sequence.
362  *
363  * @param first_digit  the already read first digit
364  */
365 static int parse_octal_sequence(const int first_digit)
366 {
367         assert(is_octal_digit(first_digit));
368         int value = digit_value(first_digit);
369         if (!is_octal_digit(input.c)) return value;
370         value = 8 * value + digit_value(input.c);
371         next_char();
372         if (!is_octal_digit(input.c)) return value;
373         value = 8 * value + digit_value(input.c);
374         next_char();
375
376         if (char_is_signed) {
377                 return (signed char) value;
378         } else {
379                 return (unsigned char) value;
380         }
381 }
382
383 /**
384  * Parses a hex character sequence.
385  */
386 static int parse_hex_sequence(void)
387 {
388         int value = 0;
389         while (isxdigit(input.c)) {
390                 value = 16 * value + digit_value(input.c);
391                 next_char();
392         }
393
394         if (char_is_signed) {
395                 return (signed char) value;
396         } else {
397                 return (unsigned char) value;
398         }
399 }
400
401 /**
402  * Parse an escape sequence.
403  */
404 static int parse_escape_sequence(void)
405 {
406         eat('\\');
407
408         int ec = input.c;
409         next_char();
410
411         switch (ec) {
412         case '"':  return '"';
413         case '\'': return '\'';
414         case '\\': return '\\';
415         case '?': return '\?';
416         case 'a': return '\a';
417         case 'b': return '\b';
418         case 'f': return '\f';
419         case 'n': return '\n';
420         case 'r': return '\r';
421         case 't': return '\t';
422         case 'v': return '\v';
423         case 'x':
424                 return parse_hex_sequence();
425         case '0':
426         case '1':
427         case '2':
428         case '3':
429         case '4':
430         case '5':
431         case '6':
432         case '7':
433                 return parse_octal_sequence(ec);
434         case EOF:
435                 parse_error("reached end of file while parsing escape sequence");
436                 return EOF;
437         default:
438                 parse_error("unknown escape sequence");
439                 return EOF;
440         }
441 }
442
443 static const char *identify_string(char *string)
444 {
445         const char *result = strset_insert(&stringset, string);
446         if (result != string) {
447                 obstack_free(&symbol_obstack, string);
448         }
449         return result;
450 }
451
452 static string_t make_string(char *string, size_t len)
453 {
454         const char *result = identify_string(string);
455         return (string_t) {result, len};
456 }
457
458 static void parse_string_literal(void)
459 {
460         const unsigned start_linenr = input.position.lineno;
461
462         eat('"');
463
464         while (true) {
465                 switch (input.c) {
466                 case '\\': {
467                         utf32 tc;
468                         if (resolve_escape_sequences) {
469                                 tc = parse_escape_sequence();
470                                 obstack_1grow(&symbol_obstack, (char) tc);
471                         } else {
472                                 obstack_1grow(&symbol_obstack, (char) input.c);
473                                 next_char();
474                                 obstack_1grow(&symbol_obstack, (char) input.c);
475                                 next_char();
476                         }
477                         break;
478                 }
479
480                 case EOF: {
481                         source_position_t source_position;
482                         source_position.input_name = pp_token.base.source_position.input_name;
483                         source_position.lineno     = start_linenr;
484                         errorf(&source_position, "string has no end");
485                         pp_token.kind = TP_ERROR;
486                         return;
487                 }
488
489                 case '"':
490                         next_char();
491                         goto end_of_string;
492
493                 default:
494                         obstack_grow_symbol(&symbol_obstack, input.c);
495                         next_char();
496                         break;
497                 }
498         }
499
500 end_of_string:
501         /* add finishing 0 to the string */
502         obstack_1grow(&symbol_obstack, '\0');
503         const size_t size   = (size_t)obstack_object_size(&symbol_obstack);
504         char *const  string = obstack_finish(&symbol_obstack);
505
506         pp_token.kind          = TP_STRING_LITERAL;
507         pp_token.string.string = make_string(string, size);
508 }
509
510 /**
511  * Parse a wide string literal and set lexer_token.
512  */
513 static void parse_wide_string_literal(void)
514 {
515         parse_string_literal();
516         if (pp_token.kind == TP_STRING_LITERAL)
517                 pp_token.kind = TP_WIDE_STRING_LITERAL;
518 }
519
520 static void parse_wide_character_constant(void)
521 {
522         eat('\'');
523
524         while (true) {
525                 switch (input.c) {
526                 case '\\': {
527                         const utf32 tc = parse_escape_sequence();
528                         obstack_grow_symbol(&symbol_obstack, tc);
529                         break;
530                 }
531
532                 MATCH_NEWLINE(
533                         parse_error("newline while parsing character constant");
534                         break;
535                 )
536
537                 case '\'':
538                         next_char();
539                         goto end_of_wide_char_constant;
540
541                 case EOF:
542                         parse_error("EOF while parsing character constant");
543                         pp_token.kind = TP_ERROR;
544                         return;
545
546                 default:
547                         obstack_grow_symbol(&symbol_obstack, input.c);
548                         next_char();
549                         break;
550                 }
551         }
552
553 end_of_wide_char_constant:
554         obstack_1grow(&symbol_obstack, '\0');
555         size_t  size = (size_t) obstack_object_size(&symbol_obstack)-1;
556         char   *string = obstack_finish(&symbol_obstack);
557         pp_token.kind          = TP_WIDE_CHARACTER_CONSTANT;
558         pp_token.string.string = make_string(string, size);
559
560         if (size == 0) {
561                 parse_error("empty character constant");
562         }
563 }
564
565 static void parse_character_constant(void)
566 {
567         const unsigned start_linenr = input.position.lineno;
568
569         eat('\'');
570
571         int tc;
572         while (true) {
573                 switch (input.c) {
574                 case '\\':
575                         tc = parse_escape_sequence();
576                         obstack_1grow(&symbol_obstack, (char) tc);
577                         break;
578
579                 MATCH_NEWLINE(
580                         parse_error("newline while parsing character constant");
581                         break;
582                 )
583
584                 case EOF: {
585                         source_position_t source_position;
586                         source_position.input_name = pp_token.base.source_position.input_name;
587                         source_position.lineno     = start_linenr;
588                         errorf(&source_position, "EOF while parsing character constant");
589                         pp_token.kind = TP_ERROR;
590                         return;
591                 }
592
593                 case '\'':
594                         next_char();
595                         goto end_of_char_constant;
596
597                 default:
598                         obstack_1grow(&symbol_obstack, (char) input.c);
599                         next_char();
600                         break;
601
602                 }
603         }
604
605 end_of_char_constant:;
606         obstack_1grow(&symbol_obstack, '\0');
607         const size_t size   = (size_t)obstack_object_size(&symbol_obstack);
608         char *const  string = obstack_finish(&symbol_obstack);
609
610         pp_token.kind          = TP_CHARACTER_CONSTANT;
611         pp_token.string.string = make_string(string, size);
612
613         if (size == 0) {
614                 parse_error("empty character constant");
615         }
616 }
617
618 #define SYMBOL_CHARS_WITHOUT_E_P \
619         case 'a': \
620         case 'b': \
621         case 'c': \
622         case 'd': \
623         case 'f': \
624         case 'g': \
625         case 'h': \
626         case 'i': \
627         case 'j': \
628         case 'k': \
629         case 'l': \
630         case 'm': \
631         case 'n': \
632         case 'o': \
633         case 'q': \
634         case 'r': \
635         case 's': \
636         case 't': \
637         case 'u': \
638         case 'v': \
639         case 'w': \
640         case 'x': \
641         case 'y': \
642         case 'z': \
643         case 'A': \
644         case 'B': \
645         case 'C': \
646         case 'D': \
647         case 'F': \
648         case 'G': \
649         case 'H': \
650         case 'I': \
651         case 'J': \
652         case 'K': \
653         case 'L': \
654         case 'M': \
655         case 'N': \
656         case 'O': \
657         case 'Q': \
658         case 'R': \
659         case 'S': \
660         case 'T': \
661         case 'U': \
662         case 'V': \
663         case 'W': \
664         case 'X': \
665         case 'Y': \
666         case 'Z': \
667         case '_':
668
669 #define SYMBOL_CHARS \
670         SYMBOL_CHARS_WITHOUT_E_P \
671         case 'e': \
672         case 'p': \
673         case 'E': \
674         case 'P':
675
676 #define DIGITS \
677         case '0':  \
678         case '1':  \
679         case '2':  \
680         case '3':  \
681         case '4':  \
682         case '5':  \
683         case '6':  \
684         case '7':  \
685         case '8':  \
686         case '9':
687
688 /**
689  * returns next final token from a preprocessor macro expansion
690  */
691 static void expand_next(void)
692 {
693         assert(current_expansion != NULL);
694
695         pp_definition_t *definition = current_expansion;
696
697 restart:
698         if (definition->list_len == 0
699                         || definition->expand_pos >= definition->list_len) {
700                 /* we're finished with the current macro, move up 1 level in the
701                  * expansion stack */
702                 pp_definition_t *parent = definition->parent_expansion;
703                 definition->parent_expansion = NULL;
704                 definition->is_expanding     = false;
705
706                 /* it was the outermost expansion, parse normal pptoken */
707                 if (parent == NULL) {
708                         current_expansion = NULL;
709                         next_preprocessing_token();
710                         return;
711                 }
712                 definition        = parent;
713                 current_expansion = definition;
714                 goto restart;
715         }
716         pp_token = definition->token_list[definition->expand_pos];
717         pp_token.base.source_position = expansion_pos;
718         ++definition->expand_pos;
719
720         if (pp_token.kind != TP_IDENTIFIER)
721                 return;
722
723         /* if it was an identifier then we might need to expand again */
724         pp_definition_t *symbol_definition = pp_token.identifier.symbol->pp_definition;
725         if (symbol_definition != NULL && !symbol_definition->is_expanding) {
726                 symbol_definition->parent_expansion = definition;
727                 symbol_definition->expand_pos       = 0;
728                 symbol_definition->is_expanding     = true;
729                 definition                          = symbol_definition;
730                 current_expansion                   = definition;
731                 goto restart;
732         }
733 }
734
735 static void skip_line_comment(void)
736 {
737         while (true) {
738                 switch (input.c) {
739                 case EOF:
740                         return;
741
742                 case '\r':
743                 case '\n':
744                         return;
745
746                 default:
747                         next_char();
748                         break;
749                 }
750         }
751 }
752
753 static void skip_multiline_comment(void)
754 {
755         unsigned start_linenr = input.position.lineno;
756         while (true) {
757                 switch (input.c) {
758                 case '/':
759                         next_char();
760                         if (input.c == '*') {
761                                 /* TODO: nested comment, warn here */
762                         }
763                         break;
764                 case '*':
765                         next_char();
766                         if (input.c == '/') {
767                                 next_char();
768                                 info.whitespace += input.position.colno-1;
769                                 return;
770                         }
771                         break;
772
773                 MATCH_NEWLINE(
774                         info.at_line_begin |= !in_pp_directive;
775                         break;
776                 )
777
778                 case EOF: {
779                         source_position_t source_position;
780                         source_position.input_name = pp_token.base.source_position.input_name;
781                         source_position.lineno     = start_linenr;
782                         errorf(&source_position, "at end of file while looking for comment end");
783                         return;
784                 }
785
786                 default:
787                         next_char();
788                         break;
789                 }
790         }
791 }
792
793 static void skip_whitespace(void)
794 {
795         while (true) {
796                 switch (input.c) {
797                 case ' ':
798                 case '\t':
799                         next_char();
800                         continue;
801
802                 MATCH_NEWLINE(
803                         info.at_line_begin = true;
804                         return;
805                 )
806
807                 case '/':
808                         next_char();
809                         if (input.c == '/') {
810                                 next_char();
811                                 skip_line_comment();
812                                 continue;
813                         } else if (input.c == '*') {
814                                 next_char();
815                                 skip_multiline_comment();
816                                 continue;
817                         } else {
818                                 put_back(input.c);
819                                 input.c = '/';
820                         }
821                         return;
822                 default:
823                         return;
824                 }
825         }
826 }
827
828 static void eat_pp(int type)
829 {
830         (void) type;
831         assert(pp_token.kind == type);
832         next_preprocessing_token();
833 }
834
835 static void parse_symbol(void)
836 {
837         obstack_1grow(&symbol_obstack, (char) input.c);
838         next_char();
839
840         while (true) {
841                 switch (input.c) {
842                 DIGITS
843                 SYMBOL_CHARS
844                         obstack_1grow(&symbol_obstack, (char) input.c);
845                         next_char();
846                         break;
847
848                 default:
849                         goto end_symbol;
850                 }
851         }
852
853 end_symbol:
854         obstack_1grow(&symbol_obstack, '\0');
855         char *string = obstack_finish(&symbol_obstack);
856
857         /* might be a wide string or character constant ( L"string"/L'c' ) */
858         if (input.c == '"' && string[0] == 'L' && string[1] == '\0') {
859                 obstack_free(&symbol_obstack, string);
860                 parse_wide_string_literal();
861                 return;
862         } else if (input.c == '\'' && string[0] == 'L' && string[1] == '\0') {
863                 obstack_free(&symbol_obstack, string);
864                 parse_wide_character_constant();
865                 return;
866         }
867
868         symbol_t *symbol = symbol_table_insert(string);
869
870         pp_token.kind              = symbol->pp_ID;
871         pp_token.identifier.symbol = symbol;
872
873         /* we can free the memory from symbol obstack if we already had an entry in
874          * the symbol table */
875         if (symbol->string != string) {
876                 obstack_free(&symbol_obstack, string);
877         }
878 }
879
880 static void parse_number(void)
881 {
882         obstack_1grow(&symbol_obstack, (char) input.c);
883         next_char();
884
885         while (true) {
886                 switch (input.c) {
887                 case '.':
888                 DIGITS
889                 SYMBOL_CHARS_WITHOUT_E_P
890                         obstack_1grow(&symbol_obstack, (char) input.c);
891                         next_char();
892                         break;
893
894                 case 'e':
895                 case 'p':
896                 case 'E':
897                 case 'P':
898                         obstack_1grow(&symbol_obstack, (char) input.c);
899                         next_char();
900                         if (input.c == '+' || input.c == '-') {
901                                 obstack_1grow(&symbol_obstack, (char) input.c);
902                                 next_char();
903                         }
904                         break;
905
906                 default:
907                         goto end_number;
908                 }
909         }
910
911 end_number:
912         obstack_1grow(&symbol_obstack, '\0');
913         size_t  size   = obstack_object_size(&symbol_obstack);
914         char   *string = obstack_finish(&symbol_obstack);
915
916         pp_token.kind          = TP_NUMBER;
917         pp_token.number.number = make_string(string, size);
918 }
919
920
921 #define MAYBE_PROLOG                                       \
922                         next_char();                                   \
923                         while (true) {                                 \
924                                 switch (input.c) {
925
926 #define MAYBE(ch, set_type)                                \
927                                 case ch:                                   \
928                                         next_char();                           \
929                                         pp_token.kind = set_type;              \
930                                         return;
931
932 #define ELSE_CODE(code)                                    \
933                                 default:                                   \
934                                         code                                   \
935                                         return;                                \
936                                 }                                          \
937                         }
938
939 #define ELSE(set_type)                                     \
940                 ELSE_CODE(                                         \
941                         pp_token.kind = set_type;                      \
942                 )
943
944 static void next_preprocessing_token(void)
945 {
946         if (current_expansion != NULL) {
947                 expand_next();
948                 return;
949         }
950
951         info.at_line_begin  = false;
952         info.had_whitespace = false;
953 restart:
954         pp_token.base.source_position = input.position;
955         switch (input.c) {
956         case ' ':
957         case '\t':
958                 ++info.whitespace;
959                 info.had_whitespace = true;
960                 next_char();
961                 goto restart;
962
963         MATCH_NEWLINE(
964                 info.at_line_begin = true;
965                 info.had_whitespace = true;
966                 goto restart;
967         )
968
969         SYMBOL_CHARS
970                 parse_symbol();
971                 return;
972
973         DIGITS
974                 parse_number();
975                 return;
976
977         case '"':
978                 parse_string_literal();
979                 return;
980
981         case '\'':
982                 parse_character_constant();
983                 return;
984
985         case '.':
986                 MAYBE_PROLOG
987                         case '0':
988                         case '1':
989                         case '2':
990                         case '3':
991                         case '4':
992                         case '5':
993                         case '6':
994                         case '7':
995                         case '8':
996                         case '9':
997                                 put_back(input.c);
998                                 input.c = '.';
999                                 parse_number();
1000                                 return;
1001
1002                         case '.':
1003                                 MAYBE_PROLOG
1004                                 MAYBE('.', TP_DOTDOTDOT)
1005                                 ELSE_CODE(
1006                                         put_back(input.c);
1007                                         input.c = '.';
1008                                         pp_token.kind = '.';
1009                                 )
1010                 ELSE('.')
1011         case '&':
1012                 MAYBE_PROLOG
1013                 MAYBE('&', TP_ANDAND)
1014                 MAYBE('=', TP_ANDEQUAL)
1015                 ELSE('&')
1016         case '*':
1017                 MAYBE_PROLOG
1018                 MAYBE('=', TP_ASTERISKEQUAL)
1019                 ELSE('*')
1020         case '+':
1021                 MAYBE_PROLOG
1022                 MAYBE('+', TP_PLUSPLUS)
1023                 MAYBE('=', TP_PLUSEQUAL)
1024                 ELSE('+')
1025         case '-':
1026                 MAYBE_PROLOG
1027                 MAYBE('>', TP_MINUSGREATER)
1028                 MAYBE('-', TP_MINUSMINUS)
1029                 MAYBE('=', TP_MINUSEQUAL)
1030                 ELSE('-')
1031         case '!':
1032                 MAYBE_PROLOG
1033                 MAYBE('=', TP_EXCLAMATIONMARKEQUAL)
1034                 ELSE('!')
1035         case '/':
1036                 MAYBE_PROLOG
1037                 MAYBE('=', TP_SLASHEQUAL)
1038                         case '*':
1039                                 next_char();
1040                                 info.had_whitespace = true;
1041                                 skip_multiline_comment();
1042                                 goto restart;
1043                         case '/':
1044                                 next_char();
1045                                 info.had_whitespace = true;
1046                                 skip_line_comment();
1047                                 goto restart;
1048                 ELSE('/')
1049         case '%':
1050                 MAYBE_PROLOG
1051                 MAYBE('>', '}')
1052                 MAYBE('=', TP_PERCENTEQUAL)
1053                         case ':':
1054                                 MAYBE_PROLOG
1055                                         case '%':
1056                                                 MAYBE_PROLOG
1057                                                 MAYBE(':', TP_HASHHASH)
1058                                                 ELSE_CODE(
1059                                                         put_back(input.c);
1060                                                         input.c = '%';
1061                                                         pp_token.kind = '#';
1062                                                 )
1063                                 ELSE('#')
1064                 ELSE('%')
1065         case '<':
1066                 MAYBE_PROLOG
1067                 MAYBE(':', '[')
1068                 MAYBE('%', '{')
1069                 MAYBE('=', TP_LESSEQUAL)
1070                         case '<':
1071                                 MAYBE_PROLOG
1072                                 MAYBE('=', TP_LESSLESSEQUAL)
1073                                 ELSE(TP_LESSLESS)
1074                 ELSE('<')
1075         case '>':
1076                 MAYBE_PROLOG
1077                 MAYBE('=', TP_GREATEREQUAL)
1078                         case '>':
1079                                 MAYBE_PROLOG
1080                                 MAYBE('=', TP_GREATERGREATEREQUAL)
1081                                 ELSE(TP_GREATERGREATER)
1082                 ELSE('>')
1083         case '^':
1084                 MAYBE_PROLOG
1085                 MAYBE('=', TP_CARETEQUAL)
1086                 ELSE('^')
1087         case '|':
1088                 MAYBE_PROLOG
1089                 MAYBE('=', TP_PIPEEQUAL)
1090                 MAYBE('|', TP_PIPEPIPE)
1091                 ELSE('|')
1092         case ':':
1093                 MAYBE_PROLOG
1094                 MAYBE('>', ']')
1095                 ELSE(':')
1096         case '=':
1097                 MAYBE_PROLOG
1098                 MAYBE('=', TP_EQUALEQUAL)
1099                 ELSE('=')
1100         case '#':
1101                 MAYBE_PROLOG
1102                 MAYBE('#', TP_HASHHASH)
1103                 ELSE_CODE(
1104                         pp_token.kind = '#';
1105                 )
1106
1107         case '?':
1108         case '[':
1109         case ']':
1110         case '(':
1111         case ')':
1112         case '{':
1113         case '}':
1114         case '~':
1115         case ';':
1116         case ',':
1117         case '\\':
1118                 pp_token.kind = input.c;
1119                 next_char();
1120                 return;
1121
1122         case EOF:
1123                 if (input_stack != NULL) {
1124                         close_input();
1125                         pop_restore_input();
1126                         fputc('\n', out);
1127                         print_line_directive(&input.position, "2");
1128                         goto restart;
1129                 } else {
1130                         pp_token.base.source_position.lineno++;
1131                         info.at_line_begin = true;
1132                         pp_token.kind = TP_EOF;
1133                 }
1134                 return;
1135
1136         default:
1137                 next_char();
1138                 if (!ignore_unknown_chars) {
1139                         errorf(&pp_token.base.source_position,
1140                                "unknown character '%c' found\n", input.c);
1141                         pp_token.kind = TP_ERROR;
1142                 } else {
1143                         pp_token.kind = input.c;
1144                 }
1145                 return;
1146         }
1147 }
1148
1149 static void print_quoted_string(const char *const string)
1150 {
1151         fputc('"', out);
1152         for (const char *c = string; *c != 0; ++c) {
1153                 switch (*c) {
1154                 case '"': fputs("\\\"", out); break;
1155                 case '\\':  fputs("\\\\", out); break;
1156                 case '\a':  fputs("\\a", out); break;
1157                 case '\b':  fputs("\\b", out); break;
1158                 case '\f':  fputs("\\f", out); break;
1159                 case '\n':  fputs("\\n", out); break;
1160                 case '\r':  fputs("\\r", out); break;
1161                 case '\t':  fputs("\\t", out); break;
1162                 case '\v':  fputs("\\v", out); break;
1163                 case '\?':  fputs("\\?", out); break;
1164                 default:
1165                         if (!isprint(*c)) {
1166                                 fprintf(out, "\\%03o", (unsigned)*c);
1167                                 break;
1168                         }
1169                         fputc(*c, out);
1170                         break;
1171                 }
1172         }
1173         fputc('"', out);
1174 }
1175
1176 static void print_line_directive(const source_position_t *pos, const char *add)
1177 {
1178         fprintf(out, "# %u ", pos->lineno);
1179         print_quoted_string(pos->input_name);
1180         if (add != NULL) {
1181                 fputc(' ', out);
1182                 fputs(add, out);
1183         }
1184
1185         printed_input_name = pos->input_name;
1186         input.output_line  = pos->lineno-1;
1187 }
1188
1189 static void emit_newlines(void)
1190 {
1191         unsigned delta = pp_token.base.source_position.lineno - input.output_line;
1192
1193         if (delta >= 9) {
1194                 fputc('\n', out);
1195                 print_line_directive(&pp_token.base.source_position, NULL);
1196                 fputc('\n', out);
1197         } else {
1198                 for (unsigned i = 0; i < delta; ++i) {
1199                         fputc('\n', out);
1200                 }
1201         }
1202         input.output_line = pp_token.base.source_position.lineno;
1203 }
1204
1205 static void emit_pp_token(void)
1206 {
1207         if (skip_mode)
1208                 return;
1209
1210         if (info.at_line_begin) {
1211                 emit_newlines();
1212
1213                 for (unsigned i = 0; i < info.whitespace; ++i)
1214                         fputc(' ', out);
1215
1216         } else if (info.had_whitespace ||
1217                            tokens_would_paste(last_token, pp_token.kind)) {
1218                 fputc(' ', out);
1219         }
1220
1221         switch (pp_token.kind) {
1222         case TP_IDENTIFIER:
1223                 fputs(pp_token.identifier.symbol->string, out);
1224                 break;
1225         case TP_NUMBER:
1226                 fputs(pp_token.number.number.begin, out);
1227                 break;
1228         case TP_WIDE_STRING_LITERAL:
1229                 fputc('L', out);
1230         case TP_STRING_LITERAL:
1231                 fputc('"', out);
1232                 fputs(pp_token.string.string.begin, out);
1233                 fputc('"', out);
1234                 break;
1235         case TP_WIDE_CHARACTER_CONSTANT:
1236                 fputc('L', out);
1237         case TP_CHARACTER_CONSTANT:
1238                 fputc('\'', out);
1239                 fputs(pp_token.string.string.begin, out);
1240                 fputc('\'', out);
1241                 break;
1242         default:
1243                 print_pp_token_kind(out, pp_token.kind);
1244                 break;
1245         }
1246         last_token = pp_token.kind;
1247 }
1248
1249 static void eat_pp_directive(void)
1250 {
1251         while (!info.at_line_begin) {
1252                 next_preprocessing_token();
1253         }
1254 }
1255
1256 static bool strings_equal(const string_t *string1, const string_t *string2)
1257 {
1258         size_t size = string1->size;
1259         if (size != string2->size)
1260                 return false;
1261
1262         const char *c1 = string1->begin;
1263         const char *c2 = string2->begin;
1264         for (size_t i = 0; i < size; ++i, ++c1, ++c2) {
1265                 if (*c1 != *c2)
1266                         return false;
1267         }
1268         return true;
1269 }
1270
1271 static bool pp_tokens_equal(const token_t *token1, const token_t *token2)
1272 {
1273         if (token1->kind != token2->kind)
1274                 return false;
1275
1276         switch (token1->kind) {
1277         case TP_IDENTIFIER:
1278                 return token1->identifier.symbol == token2->identifier.symbol;
1279         case TP_NUMBER:
1280         case TP_CHARACTER_CONSTANT:
1281         case TP_STRING_LITERAL:
1282                 return strings_equal(&token1->string.string, &token2->string.string);
1283
1284         default:
1285                 return true;
1286         }
1287 }
1288
1289 static bool pp_definitions_equal(const pp_definition_t *definition1,
1290                                  const pp_definition_t *definition2)
1291 {
1292         if (definition1->list_len != definition2->list_len)
1293                 return false;
1294
1295         size_t         len = definition1->list_len;
1296         const token_t *t1  = definition1->token_list;
1297         const token_t *t2  = definition2->token_list;
1298         for (size_t i = 0; i < len; ++i, ++t1, ++t2) {
1299                 if (!pp_tokens_equal(t1, t2))
1300                         return false;
1301         }
1302         return true;
1303 }
1304
1305 static void parse_define_directive(void)
1306 {
1307         eat_pp(TP_define);
1308         assert(obstack_object_size(&pp_obstack) == 0);
1309
1310         if (pp_token.kind != TP_IDENTIFIER || info.at_line_begin) {
1311                 errorf(&pp_token.base.source_position,
1312                        "expected identifier after #define, got '%t'", &pp_token);
1313                 goto error_out;
1314         }
1315         symbol_t *symbol = pp_token.identifier.symbol;
1316
1317         pp_definition_t *new_definition
1318                 = obstack_alloc(&pp_obstack, sizeof(new_definition[0]));
1319         memset(new_definition, 0, sizeof(new_definition[0]));
1320         new_definition->source_position = input.position;
1321
1322         /* this is probably the only place where spaces are significant in the
1323          * lexer (except for the fact that they separate tokens). #define b(x)
1324          * is something else than #define b (x) */
1325         if (input.c == '(') {
1326                 /* eat the '(' */
1327                 next_preprocessing_token();
1328                 /* get next token after '(' */
1329                 next_preprocessing_token();
1330
1331                 while (true) {
1332                         switch (pp_token.kind) {
1333                         case TP_DOTDOTDOT:
1334                                 new_definition->is_variadic = true;
1335                                 next_preprocessing_token();
1336                                 if (pp_token.kind != ')') {
1337                                         errorf(&input.position,
1338                                                         "'...' not at end of macro argument list");
1339                                         goto error_out;
1340                                 }
1341                                 break;
1342                         case TP_IDENTIFIER:
1343                                 obstack_ptr_grow(&pp_obstack, pp_token.identifier.symbol);
1344                                 next_preprocessing_token();
1345
1346                                 if (pp_token.kind == ',') {
1347                                         next_preprocessing_token();
1348                                         break;
1349                                 }
1350
1351                                 if (pp_token.kind != ')') {
1352                                         errorf(&pp_token.base.source_position,
1353                                                "expected ',' or ')' after identifier, got '%t'",
1354                                                &pp_token);
1355                                         goto error_out;
1356                                 }
1357                                 break;
1358                         case ')':
1359                                 next_preprocessing_token();
1360                                 goto finish_argument_list;
1361                         default:
1362                                 errorf(&pp_token.base.source_position,
1363                                        "expected identifier, '...' or ')' in #define argument list, got '%t'",
1364                                        &pp_token);
1365                                 goto error_out;
1366                         }
1367                 }
1368
1369         finish_argument_list:
1370                 new_definition->has_parameters = true;
1371                 new_definition->n_parameters
1372                         = obstack_object_size(&pp_obstack) / sizeof(new_definition->parameters[0]);
1373                 new_definition->parameters = obstack_finish(&pp_obstack);
1374         } else {
1375                 next_preprocessing_token();
1376         }
1377
1378         /* construct a new pp_definition on the obstack */
1379         assert(obstack_object_size(&pp_obstack) == 0);
1380         size_t list_len = 0;
1381         while (!info.at_line_begin) {
1382                 obstack_grow(&pp_obstack, &pp_token, sizeof(pp_token));
1383                 ++list_len;
1384                 next_preprocessing_token();
1385         }
1386
1387         new_definition->list_len   = list_len;
1388         new_definition->token_list = obstack_finish(&pp_obstack);
1389
1390         pp_definition_t *old_definition = symbol->pp_definition;
1391         if (old_definition != NULL) {
1392                 if (!pp_definitions_equal(old_definition, new_definition)) {
1393                         warningf(WARN_OTHER, &input.position, "multiple definition of macro '%Y' (first defined %P)", symbol, &old_definition->source_position);
1394                 } else {
1395                         /* reuse the old definition */
1396                         obstack_free(&pp_obstack, new_definition);
1397                         new_definition = old_definition;
1398                 }
1399         }
1400
1401         symbol->pp_definition = new_definition;
1402         return;
1403
1404 error_out:
1405         if (obstack_object_size(&pp_obstack) > 0) {
1406                 char *ptr = obstack_finish(&pp_obstack);
1407                 obstack_free(&pp_obstack, ptr);
1408         }
1409         eat_pp_directive();
1410 }
1411
1412 static void parse_undef_directive(void)
1413 {
1414         eat_pp(TP_undef);
1415
1416         if (pp_token.kind != TP_IDENTIFIER) {
1417                 errorf(&input.position,
1418                        "expected identifier after #undef, got '%t'", &pp_token);
1419                 eat_pp_directive();
1420                 return;
1421         }
1422
1423         symbol_t *symbol = pp_token.identifier.symbol;
1424         symbol->pp_definition = NULL;
1425         next_preprocessing_token();
1426
1427         if (!info.at_line_begin) {
1428                 warningf(WARN_OTHER, &input.position, "extra tokens at end of #undef directive");
1429         }
1430         eat_pp_directive();
1431 }
1432
1433 static const char *parse_headername(void)
1434 {
1435         /* behind an #include we can have the special headername lexems.
1436          * They're only allowed behind an #include so they're not recognized
1437          * by the normal next_preprocessing_token. We handle them as a special
1438          * exception here */
1439         if (info.at_line_begin) {
1440                 parse_error("expected headername after #include");
1441                 return NULL;
1442         }
1443
1444         assert(obstack_object_size(&symbol_obstack) == 0);
1445
1446         /* check wether we have a "... or <... headername */
1447         switch (input.c) {
1448         case '<':
1449                 next_char();
1450                 while (true) {
1451                         switch (input.c) {
1452                         case EOF:
1453                                 /* fallthrough */
1454                         MATCH_NEWLINE(
1455                                 parse_error("header name without closing '>'");
1456                                 return NULL;
1457                         )
1458                         case '>':
1459                                 next_char();
1460                                 goto finished_headername;
1461                         }
1462                         obstack_1grow(&symbol_obstack, (char) input.c);
1463                         next_char();
1464                 }
1465                 /* we should never be here */
1466
1467         case '"':
1468                 next_char();
1469                 while (true) {
1470                         switch (input.c) {
1471                         case EOF:
1472                                 /* fallthrough */
1473                         MATCH_NEWLINE(
1474                                 parse_error("header name without closing '>'");
1475                                 return NULL;
1476                         )
1477                         case '"':
1478                                 next_char();
1479                                 goto finished_headername;
1480                         }
1481                         obstack_1grow(&symbol_obstack, (char) input.c);
1482                         next_char();
1483                 }
1484                 /* we should never be here */
1485
1486         default:
1487                 /* TODO: do normal pp_token parsing and concatenate results */
1488                 panic("pp_token concat include not implemented yet");
1489         }
1490
1491 finished_headername:
1492         obstack_1grow(&symbol_obstack, '\0');
1493         char *headername = obstack_finish(&symbol_obstack);
1494
1495         /* TODO: iterate search-path to find the file */
1496
1497         skip_whitespace();
1498
1499         return identify_string(headername);
1500 }
1501
1502 static bool do_include(bool system_include, const char *headername)
1503 {
1504         if (!system_include) {
1505                 /* for "bla" includes first try current dir
1506                  * TODO: this isn't correct, should be the directory of the source file
1507                  */
1508                 FILE *file = fopen(headername, "r");
1509                 if (file != NULL) {
1510                         switch_input(file, headername);
1511                         return true;
1512                 }
1513         }
1514
1515         size_t headername_len = strlen(headername);
1516         assert(obstack_object_size(&pp_obstack) == 0);
1517         /* check searchpath */
1518         for (searchpath_entry_t *entry = searchpath; entry != NULL;
1519              entry = entry->next) {
1520             const char *path = entry->path;
1521             size_t      len  = strlen(path);
1522                 obstack_grow(&pp_obstack, path, len);
1523                 if (path[len-1] != '/')
1524                         obstack_1grow(&pp_obstack, '/');
1525                 obstack_grow(&pp_obstack, headername, headername_len+1);
1526
1527                 char *complete_path = obstack_finish(&pp_obstack);
1528                 FILE *file          = fopen(complete_path, "r");
1529                 if (file != NULL) {
1530                         const char *filename = identify_string(complete_path);
1531                         switch_input(file, filename);
1532                         return true;
1533                 }
1534                 obstack_free(&pp_obstack, complete_path);
1535         }
1536
1537         return false;
1538 }
1539
1540 static bool parse_include_directive(void)
1541 {
1542         /* don't eat the TP_include here!
1543          * we need an alternative parsing for the next token */
1544         skip_whitespace();
1545         bool system_include = input.c == '<';
1546         const char *headername = parse_headername();
1547         if (headername == NULL) {
1548                 eat_pp_directive();
1549                 return false;
1550         }
1551
1552         if (!info.at_line_begin) {
1553                 warningf(WARN_OTHER, &pp_token.base.source_position,
1554                          "extra tokens at end of #include directive");
1555                 eat_pp_directive();
1556         }
1557
1558         if (n_inputs > INCLUDE_LIMIT) {
1559                 errorf(&pp_token.base.source_position, "#include nested too deeply");
1560                 /* eat \n or EOF */
1561                 next_preprocessing_token();
1562                 return false;
1563         }
1564
1565         /* we have to reenable space counting and macro expansion here,
1566          * because it is still disabled in directive parsing,
1567          * but we will trigger a preprocessing token reading of the new file
1568          * now and need expansions/space counting */
1569         in_pp_directive = false;
1570
1571         /* switch inputs */
1572         emit_newlines();
1573         push_input();
1574         bool res = do_include(system_include, headername);
1575         if (!res) {
1576                 errorf(&pp_token.base.source_position,
1577                        "failed including '%s': %s", headername, strerror(errno));
1578                 pop_restore_input();
1579                 return false;
1580         }
1581
1582         return true;
1583 }
1584
1585 static pp_conditional_t *push_conditional(void)
1586 {
1587         pp_conditional_t *conditional
1588                 = obstack_alloc(&pp_obstack, sizeof(*conditional));
1589         memset(conditional, 0, sizeof(*conditional));
1590
1591         conditional->parent = conditional_stack;
1592         conditional_stack   = conditional;
1593
1594         return conditional;
1595 }
1596
1597 static void pop_conditional(void)
1598 {
1599         assert(conditional_stack != NULL);
1600         conditional_stack = conditional_stack->parent;
1601 }
1602
1603 static void check_unclosed_conditionals(void)
1604 {
1605         while (conditional_stack != NULL) {
1606                 pp_conditional_t *conditional = conditional_stack;
1607
1608                 if (conditional->in_else) {
1609                         errorf(&conditional->source_position, "unterminated #else");
1610                 } else {
1611                         errorf(&conditional->source_position, "unterminated condition");
1612                 }
1613                 pop_conditional();
1614         }
1615 }
1616
1617 static void parse_ifdef_ifndef_directive(void)
1618 {
1619         bool is_ifndef = (pp_token.kind == TP_ifndef);
1620         bool condition;
1621         next_preprocessing_token();
1622
1623         if (skip_mode) {
1624                 eat_pp_directive();
1625                 pp_conditional_t *conditional = push_conditional();
1626                 conditional->source_position  = pp_token.base.source_position;
1627                 conditional->skip             = true;
1628                 return;
1629         }
1630
1631         if (pp_token.kind != TP_IDENTIFIER || info.at_line_begin) {
1632                 errorf(&pp_token.base.source_position,
1633                        "expected identifier after #%s, got '%t'",
1634                        is_ifndef ? "ifndef" : "ifdef", &pp_token);
1635                 eat_pp_directive();
1636
1637                 /* just take the true case in the hope to avoid further errors */
1638                 condition = true;
1639         } else {
1640                 symbol_t        *symbol        = pp_token.identifier.symbol;
1641                 pp_definition_t *pp_definition = symbol->pp_definition;
1642                 next_preprocessing_token();
1643
1644                 if (!info.at_line_begin) {
1645                         errorf(&pp_token.base.source_position,
1646                                "extra tokens at end of #%s",
1647                                is_ifndef ? "ifndef" : "ifdef");
1648                         eat_pp_directive();
1649                 }
1650
1651                 /* evaluate wether we are in true or false case */
1652                 condition = is_ifndef ? pp_definition == NULL : pp_definition != NULL;
1653         }
1654
1655         pp_conditional_t *conditional = push_conditional();
1656         conditional->source_position  = pp_token.base.source_position;
1657         conditional->condition        = condition;
1658
1659         if (!condition) {
1660                 skip_mode = true;
1661         }
1662 }
1663
1664 static void parse_else_directive(void)
1665 {
1666         eat_pp(TP_else);
1667
1668         if (!info.at_line_begin) {
1669                 if (!skip_mode) {
1670                         warningf(WARN_OTHER, &pp_token.base.source_position, "extra tokens at end of #else");
1671                 }
1672                 eat_pp_directive();
1673         }
1674
1675         pp_conditional_t *conditional = conditional_stack;
1676         if (conditional == NULL) {
1677                 errorf(&pp_token.base.source_position, "#else without prior #if");
1678                 return;
1679         }
1680
1681         if (conditional->in_else) {
1682                 errorf(&pp_token.base.source_position,
1683                        "#else after #else (condition started %P)",
1684                        conditional->source_position);
1685                 skip_mode = true;
1686                 return;
1687         }
1688
1689         conditional->in_else = true;
1690         if (!conditional->skip) {
1691                 skip_mode = conditional->condition;
1692         }
1693         conditional->source_position = pp_token.base.source_position;
1694 }
1695
1696 static void parse_endif_directive(void)
1697 {
1698         eat_pp(TP_endif);
1699
1700         if (!info.at_line_begin) {
1701                 if (!skip_mode) {
1702                         warningf(WARN_OTHER, &pp_token.base.source_position, "extra tokens at end of #endif");
1703                 }
1704                 eat_pp_directive();
1705         }
1706
1707         pp_conditional_t *conditional = conditional_stack;
1708         if (conditional == NULL) {
1709                 errorf(&pp_token.base.source_position, "#endif without prior #if");
1710                 return;
1711         }
1712
1713         if (!conditional->skip) {
1714                 skip_mode = false;
1715         }
1716         pop_conditional();
1717 }
1718
1719 static void parse_preprocessing_directive(void)
1720 {
1721         in_pp_directive = true;
1722         eat_pp('#');
1723
1724         if (skip_mode) {
1725                 switch (pp_token.kind) {
1726                 case TP_ifdef:
1727                 case TP_ifndef:
1728                         parse_ifdef_ifndef_directive();
1729                         break;
1730                 case TP_else:
1731                         parse_else_directive();
1732                         break;
1733                 case TP_endif:
1734                         parse_endif_directive();
1735                         break;
1736                 default:
1737                         eat_pp_directive();
1738                         break;
1739                 }
1740         } else {
1741                 switch (pp_token.kind) {
1742                 case TP_define:
1743                         parse_define_directive();
1744                         break;
1745                 case TP_undef:
1746                         parse_undef_directive();
1747                         break;
1748                 case TP_ifdef:
1749                 case TP_ifndef:
1750                         parse_ifdef_ifndef_directive();
1751                         break;
1752                 case TP_else:
1753                         parse_else_directive();
1754                         break;
1755                 case TP_endif:
1756                         parse_endif_directive();
1757                         break;
1758                 case TP_include:
1759                         parse_include_directive();
1760                         break;
1761                 default:
1762                         if (info.at_line_begin) {
1763                                 /* the nop directive "#" */
1764                                 break;
1765                         }
1766                         errorf(&pp_token.base.source_position,
1767                                    "invalid preprocessing directive #%t", &pp_token);
1768                         eat_pp_directive();
1769                         break;
1770                 }
1771         }
1772
1773         in_pp_directive = false;
1774         assert(info.at_line_begin);
1775 }
1776
1777 static void prepend_include_path(const char *path)
1778 {
1779         searchpath_entry_t *entry = OALLOCZ(&config_obstack, searchpath_entry_t);
1780         entry->path = path;
1781         entry->next = searchpath;
1782         searchpath  = entry;
1783 }
1784
1785 static void setup_include_path(void)
1786 {
1787         /* built-in paths */
1788         prepend_include_path("/usr/include");
1789
1790         /* parse environment variable */
1791         const char *cpath = getenv("CPATH");
1792         if (cpath != NULL && *cpath != '\0') {
1793                 const char *begin = cpath;
1794                 const char *c;
1795                 do {
1796                         c = begin;
1797                         while (*c != '\0' && *c != ':')
1798                                 ++c;
1799
1800                         size_t len = c-begin;
1801                         if (len == 0) {
1802                                 /* for gcc compatibility (Matze: I would expect that
1803                                  * nothing happens for an empty entry...) */
1804                                 prepend_include_path(".");
1805                         } else {
1806                                 char *string = obstack_alloc(&config_obstack, len+1);
1807                                 memcpy(string, begin, len);
1808                                 string[len] = '\0';
1809
1810                                 prepend_include_path(string);
1811                         }
1812
1813                         begin = c+1;
1814                         /* skip : */
1815                         if (*begin == ':')
1816                                 ++begin;
1817                 } while(*c != '\0');
1818         }
1819 }
1820
1821 int pptest_main(int argc, char **argv);
1822 int pptest_main(int argc, char **argv)
1823 {
1824         init_symbol_table();
1825         init_tokens();
1826
1827         obstack_init(&config_obstack);
1828         obstack_init(&pp_obstack);
1829         obstack_init(&input_obstack);
1830         strset_init(&stringset);
1831
1832         setup_include_path();
1833
1834         /* simplistic commandline parser */
1835         const char *filename = NULL;
1836         for (int i = 1; i < argc; ++i) {
1837                 const char *opt = argv[i];
1838                 if (streq(opt, "-I")) {
1839                         prepend_include_path(argv[++i]);
1840                         continue;
1841                 } else if (streq(opt, "-E")) {
1842                         /* ignore */
1843                 } else if (opt[0] == '-') {
1844                         fprintf(stderr, "Unknown option '%s'\n", opt);
1845                 } else {
1846                         if (filename != NULL)
1847                                 fprintf(stderr, "Multiple inputs not supported\n");
1848                         filename = argv[i];
1849                 }
1850         }
1851         if (filename == NULL) {
1852                 fprintf(stderr, "No input specified\n");
1853                 return 1;
1854         }
1855
1856         out = stdout;
1857
1858         /* just here for gcc compatibility */
1859         fprintf(out, "# 1 \"%s\"\n", filename);
1860         fprintf(out, "# 1 \"<built-in>\"\n");
1861         fprintf(out, "# 1 \"<command-line>\"\n");
1862
1863         FILE *file = fopen(filename, "r");
1864         if (file == NULL) {
1865                 fprintf(stderr, "Couldn't open input '%s'\n", filename);
1866                 return 1;
1867         }
1868         switch_input(file, filename);
1869
1870         while (true) {
1871                 if (pp_token.kind == '#' && info.at_line_begin) {
1872                         parse_preprocessing_directive();
1873                         continue;
1874                 } else if (pp_token.kind == TP_EOF) {
1875                         goto end_of_main_loop;
1876                 } else if (pp_token.kind == TP_IDENTIFIER && !in_pp_directive) {
1877                         symbol_t *symbol = pp_token.identifier.symbol;
1878                         pp_definition_t *pp_definition = symbol->pp_definition;
1879                         if (pp_definition != NULL && !pp_definition->is_expanding) {
1880                                 expansion_pos = pp_token.base.source_position;
1881                                 if (pp_definition->has_parameters) {
1882                                         source_position_t position = pp_token.base.source_position;
1883                                         add_token_info_t old_info = info;
1884                                         next_preprocessing_token();
1885                                         add_token_info_t new_info = info;
1886
1887                                         /* no opening brace -> no expansion */
1888                                         if (pp_token.kind == '(') {
1889                                                 eat_pp('(');
1890
1891                                                 /* parse arguments (TODO) */
1892                                                 while (pp_token.kind != TP_EOF && pp_token.kind != ')')
1893                                                         next_preprocessing_token();
1894                                         } else {
1895                                                 token_t next_token = pp_token;
1896                                                 /* restore identifier token */
1897                                                 pp_token.kind                 = TP_IDENTIFIER;
1898                                                 pp_token.identifier.symbol    = symbol;
1899                                                 pp_token.base.source_position = position;
1900                                                 info = old_info;
1901                                                 emit_pp_token();
1902
1903                                                 info = new_info;
1904                                                 pp_token = next_token;
1905                                                 continue;
1906                                         }
1907                                         info = old_info;
1908                                 }
1909                                 pp_definition->expand_pos   = 0;
1910                                 pp_definition->is_expanding = true;
1911                                 current_expansion           = pp_definition;
1912                                 expand_next();
1913                                 continue;
1914                         }
1915                 }
1916
1917                 emit_pp_token();
1918                 next_preprocessing_token();
1919         }
1920 end_of_main_loop:
1921
1922         fputc('\n', out);
1923         check_unclosed_conditionals();
1924         close_input();
1925
1926         obstack_free(&input_obstack, NULL);
1927         obstack_free(&pp_obstack, NULL);
1928         obstack_free(&config_obstack, NULL);
1929
1930         strset_destroy(&stringset);
1931
1932         exit_tokens();
1933         exit_symbol_table();
1934
1935         return 0;
1936 }