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