preprocessor: rework headername parsing
[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_literal(void)
461 {
462         const unsigned start_linenr = input.position.lineno;
463
464         eat('"');
465
466         while (true) {
467                 switch (input.c) {
468                 case '\\': {
469                         utf32 tc;
470                         if (resolve_escape_sequences) {
471                                 tc = parse_escape_sequence();
472                                 obstack_1grow(&symbol_obstack, (char) tc);
473                         } else {
474                                 obstack_1grow(&symbol_obstack, (char) input.c);
475                                 next_char();
476                                 obstack_1grow(&symbol_obstack, (char) input.c);
477                                 next_char();
478                         }
479                         break;
480                 }
481
482                 case EOF: {
483                         source_position_t source_position;
484                         source_position.input_name = pp_token.base.source_position.input_name;
485                         source_position.lineno     = start_linenr;
486                         errorf(&source_position, "string has no end");
487                         goto end_of_string;
488                 }
489
490                 case '"':
491                         next_char();
492                         goto end_of_string;
493
494                 default:
495                         obstack_grow_symbol(&symbol_obstack, input.c);
496                         next_char();
497                         break;
498                 }
499         }
500
501 end_of_string:
502         /* add finishing 0 to the string */
503         obstack_1grow(&symbol_obstack, '\0');
504         const size_t size   = (size_t)obstack_object_size(&symbol_obstack);
505         char *const  string = obstack_finish(&symbol_obstack);
506
507         pp_token.kind          = TP_STRING_LITERAL;
508         pp_token.string.string = make_string(string, size);
509 }
510
511 /**
512  * Parse a wide string literal and set lexer_token.
513  */
514 static void parse_wide_string_literal(void)
515 {
516         parse_string_literal();
517         if (pp_token.kind == TP_STRING_LITERAL)
518                 pp_token.kind = TP_WIDE_STRING_LITERAL;
519 }
520
521 static void parse_wide_character_constant(void)
522 {
523         eat('\'');
524
525         while (true) {
526                 switch (input.c) {
527                 case '\\': {
528                         const utf32 tc = parse_escape_sequence();
529                         obstack_grow_symbol(&symbol_obstack, tc);
530                         break;
531                 }
532
533                 MATCH_NEWLINE(
534                         parse_error("newline while parsing character constant");
535                         break;
536                 )
537
538                 case '\'':
539                         next_char();
540                         goto end_of_wide_char_constant;
541
542                 case EOF:
543                         parse_error("EOF while parsing character constant");
544                         goto end_of_wide_char_constant;
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                         goto end_of_char_constant;
590                 }
591
592                 case '\'':
593                         next_char();
594                         goto end_of_char_constant;
595
596                 default:
597                         obstack_1grow(&symbol_obstack, (char) input.c);
598                         next_char();
599                         break;
600
601                 }
602         }
603
604 end_of_char_constant:;
605         obstack_1grow(&symbol_obstack, '\0');
606         const size_t size   = (size_t)obstack_object_size(&symbol_obstack);
607         char *const  string = obstack_finish(&symbol_obstack);
608
609         pp_token.kind          = TP_CHARACTER_CONSTANT;
610         pp_token.string.string = make_string(string, size);
611
612         if (size == 0) {
613                 parse_error("empty character constant");
614         }
615 }
616
617 #define SYMBOL_CHARS_WITHOUT_E_P \
618         case 'a': \
619         case 'b': \
620         case 'c': \
621         case 'd': \
622         case 'f': \
623         case 'g': \
624         case 'h': \
625         case 'i': \
626         case 'j': \
627         case 'k': \
628         case 'l': \
629         case 'm': \
630         case 'n': \
631         case 'o': \
632         case 'q': \
633         case 'r': \
634         case 's': \
635         case 't': \
636         case 'u': \
637         case 'v': \
638         case 'w': \
639         case 'x': \
640         case 'y': \
641         case 'z': \
642         case 'A': \
643         case 'B': \
644         case 'C': \
645         case 'D': \
646         case 'F': \
647         case 'G': \
648         case 'H': \
649         case 'I': \
650         case 'J': \
651         case 'K': \
652         case 'L': \
653         case 'M': \
654         case 'N': \
655         case 'O': \
656         case 'Q': \
657         case 'R': \
658         case 'S': \
659         case 'T': \
660         case 'U': \
661         case 'V': \
662         case 'W': \
663         case 'X': \
664         case 'Y': \
665         case 'Z': \
666         case '_':
667
668 #define SYMBOL_CHARS \
669         SYMBOL_CHARS_WITHOUT_E_P \
670         case 'e': \
671         case 'p': \
672         case 'E': \
673         case 'P':
674
675 #define DIGITS \
676         case '0':  \
677         case '1':  \
678         case '2':  \
679         case '3':  \
680         case '4':  \
681         case '5':  \
682         case '6':  \
683         case '7':  \
684         case '8':  \
685         case '9':
686
687 /**
688  * returns next final token from a preprocessor macro expansion
689  */
690 static void expand_next(void)
691 {
692         assert(current_expansion != NULL);
693
694         pp_definition_t *definition = current_expansion;
695
696 restart:
697         if (definition->list_len == 0
698                         || definition->expand_pos >= definition->list_len) {
699                 /* we're finished with the current macro, move up 1 level in the
700                  * expansion stack */
701                 pp_definition_t *parent = definition->parent_expansion;
702                 definition->parent_expansion = NULL;
703                 definition->is_expanding     = false;
704
705                 /* it was the outermost expansion, parse normal pptoken */
706                 if (parent == NULL) {
707                         current_expansion = NULL;
708                         next_preprocessing_token();
709                         return;
710                 }
711                 definition        = parent;
712                 current_expansion = definition;
713                 goto restart;
714         }
715         pp_token = definition->token_list[definition->expand_pos];
716         pp_token.base.source_position = expansion_pos;
717         ++definition->expand_pos;
718
719         if (pp_token.kind != TP_IDENTIFIER)
720                 return;
721
722         /* if it was an identifier then we might need to expand again */
723         pp_definition_t *const symbol_definition = pp_token.base.symbol->pp_definition;
724         if (symbol_definition != NULL && !symbol_definition->is_expanding) {
725                 symbol_definition->parent_expansion = definition;
726                 symbol_definition->expand_pos       = 0;
727                 symbol_definition->is_expanding     = true;
728                 definition                          = symbol_definition;
729                 current_expansion                   = definition;
730                 goto restart;
731         }
732 }
733
734 static void skip_line_comment(void)
735 {
736         while (true) {
737                 switch (input.c) {
738                 case EOF:
739                         return;
740
741                 case '\r':
742                 case '\n':
743                         return;
744
745                 default:
746                         next_char();
747                         break;
748                 }
749         }
750 }
751
752 static void skip_multiline_comment(void)
753 {
754         unsigned start_linenr = input.position.lineno;
755         while (true) {
756                 switch (input.c) {
757                 case '/':
758                         next_char();
759                         if (input.c == '*') {
760                                 /* TODO: nested comment, warn here */
761                         }
762                         break;
763                 case '*':
764                         next_char();
765                         if (input.c == '/') {
766                                 if (input.position.lineno != input.output_line)
767                                         info.whitespace = input.position.colno;
768                                 next_char();
769                                 return;
770                         }
771                         break;
772
773                 MATCH_NEWLINE(
774                         break;
775                 )
776
777                 case EOF: {
778                         source_position_t source_position;
779                         source_position.input_name = pp_token.base.source_position.input_name;
780                         source_position.lineno     = start_linenr;
781                         errorf(&source_position, "at end of file while looking for comment end");
782                         return;
783                 }
784
785                 default:
786                         next_char();
787                         break;
788                 }
789         }
790 }
791
792 static void skip_whitespace(void)
793 {
794         while (true) {
795                 switch (input.c) {
796                 case ' ':
797                 case '\t':
798                         next_char();
799                         continue;
800
801                 MATCH_NEWLINE(
802                         info.at_line_begin = true;
803                         return;
804                 )
805
806                 case '/':
807                         next_char();
808                         if (input.c == '/') {
809                                 next_char();
810                                 skip_line_comment();
811                                 continue;
812                         } else if (input.c == '*') {
813                                 next_char();
814                                 skip_multiline_comment();
815                                 continue;
816                         } else {
817                                 put_back(input.c);
818                                 input.c = '/';
819                         }
820                         return;
821                 default:
822                         return;
823                 }
824         }
825 }
826
827 static void eat_pp(preprocessor_token_kind_t const type)
828 {
829         (void) type;
830         assert(pp_token.kind == type);
831         next_preprocessing_token();
832 }
833
834 static void parse_symbol(void)
835 {
836         obstack_1grow(&symbol_obstack, (char) input.c);
837         next_char();
838
839         while (true) {
840                 switch (input.c) {
841                 DIGITS
842                 SYMBOL_CHARS
843                         obstack_1grow(&symbol_obstack, (char) input.c);
844                         next_char();
845                         break;
846
847                 default:
848                         goto end_symbol;
849                 }
850         }
851
852 end_symbol:
853         obstack_1grow(&symbol_obstack, '\0');
854         char *string = obstack_finish(&symbol_obstack);
855
856         /* might be a wide string or character constant ( L"string"/L'c' ) */
857         if (input.c == '"' && string[0] == 'L' && string[1] == '\0') {
858                 obstack_free(&symbol_obstack, string);
859                 parse_wide_string_literal();
860                 return;
861         } else if (input.c == '\'' && string[0] == 'L' && string[1] == '\0') {
862                 obstack_free(&symbol_obstack, string);
863                 parse_wide_character_constant();
864                 return;
865         }
866
867         symbol_t *symbol = symbol_table_insert(string);
868
869         pp_token.kind        = symbol->pp_ID;
870         pp_token.base.symbol = symbol;
871
872         /* we can free the memory from symbol obstack if we already had an entry in
873          * the symbol table */
874         if (symbol->string != string) {
875                 obstack_free(&symbol_obstack, string);
876         }
877 }
878
879 static void parse_number(void)
880 {
881         obstack_1grow(&symbol_obstack, (char) input.c);
882         next_char();
883
884         while (true) {
885                 switch (input.c) {
886                 case '.':
887                 DIGITS
888                 SYMBOL_CHARS_WITHOUT_E_P
889                         obstack_1grow(&symbol_obstack, (char) input.c);
890                         next_char();
891                         break;
892
893                 case 'e':
894                 case 'p':
895                 case 'E':
896                 case 'P':
897                         obstack_1grow(&symbol_obstack, (char) input.c);
898                         next_char();
899                         if (input.c == '+' || input.c == '-') {
900                                 obstack_1grow(&symbol_obstack, (char) input.c);
901                                 next_char();
902                         }
903                         break;
904
905                 default:
906                         goto end_number;
907                 }
908         }
909
910 end_number:
911         obstack_1grow(&symbol_obstack, '\0');
912         size_t  size   = obstack_object_size(&symbol_obstack);
913         char   *string = obstack_finish(&symbol_obstack);
914
915         pp_token.kind          = TP_NUMBER;
916         pp_token.number.number = make_string(string, size);
917 }
918
919
920 #define MAYBE_PROLOG                                       \
921                         next_char();                                   \
922                         while (true) {                                 \
923                                 switch (input.c) {
924
925 #define MAYBE(ch, set_type)                                \
926                                 case ch:                                   \
927                                         next_char();                           \
928                                         pp_token.kind = set_type;              \
929                                         return;
930
931 #define ELSE_CODE(code)                                    \
932                                 default:                                   \
933                                         code                                   \
934                                         return;                                \
935                                 }                                          \
936                         }
937
938 #define ELSE(set_type)                                     \
939                 ELSE_CODE(                                         \
940                         pp_token.kind = set_type;                      \
941                 )
942
943 static void next_preprocessing_token(void)
944 {
945         if (current_expansion != NULL) {
946                 expand_next();
947                 return;
948         }
949
950         info.at_line_begin  = false;
951         info.had_whitespace = false;
952 restart:
953         pp_token.base.source_position = input.position;
954         pp_token.base.symbol          = NULL;
955
956         switch (input.c) {
957         case ' ':
958         case '\t':
959                 ++info.whitespace;
960                 info.had_whitespace = true;
961                 next_char();
962                 goto restart;
963
964         MATCH_NEWLINE(
965                 info.at_line_begin = true;
966                 info.had_whitespace = true;
967                 goto restart;
968         )
969
970         SYMBOL_CHARS
971                 parse_symbol();
972                 return;
973
974         DIGITS
975                 parse_number();
976                 return;
977
978         case '"':
979                 parse_string_literal();
980                 return;
981
982         case '\'':
983                 parse_character_constant();
984                 return;
985
986         case '.':
987                 MAYBE_PROLOG
988                         case '0':
989                         case '1':
990                         case '2':
991                         case '3':
992                         case '4':
993                         case '5':
994                         case '6':
995                         case '7':
996                         case '8':
997                         case '9':
998                                 put_back(input.c);
999                                 input.c = '.';
1000                                 parse_number();
1001                                 return;
1002
1003                         case '.':
1004                                 MAYBE_PROLOG
1005                                 MAYBE('.', TP_DOTDOTDOT)
1006                                 ELSE_CODE(
1007                                         put_back(input.c);
1008                                         input.c = '.';
1009                                         pp_token.kind = '.';
1010                                 )
1011                 ELSE('.')
1012         case '&':
1013                 MAYBE_PROLOG
1014                 MAYBE('&', TP_ANDAND)
1015                 MAYBE('=', TP_ANDEQUAL)
1016                 ELSE('&')
1017         case '*':
1018                 MAYBE_PROLOG
1019                 MAYBE('=', TP_ASTERISKEQUAL)
1020                 ELSE('*')
1021         case '+':
1022                 MAYBE_PROLOG
1023                 MAYBE('+', TP_PLUSPLUS)
1024                 MAYBE('=', TP_PLUSEQUAL)
1025                 ELSE('+')
1026         case '-':
1027                 MAYBE_PROLOG
1028                 MAYBE('>', TP_MINUSGREATER)
1029                 MAYBE('-', TP_MINUSMINUS)
1030                 MAYBE('=', TP_MINUSEQUAL)
1031                 ELSE('-')
1032         case '!':
1033                 MAYBE_PROLOG
1034                 MAYBE('=', TP_EXCLAMATIONMARKEQUAL)
1035                 ELSE('!')
1036         case '/':
1037                 MAYBE_PROLOG
1038                 MAYBE('=', TP_SLASHEQUAL)
1039                         case '*':
1040                                 next_char();
1041                                 info.had_whitespace = true;
1042                                 skip_multiline_comment();
1043                                 goto restart;
1044                         case '/':
1045                                 next_char();
1046                                 info.had_whitespace = true;
1047                                 skip_line_comment();
1048                                 goto restart;
1049                 ELSE('/')
1050         case '%':
1051                 MAYBE_PROLOG
1052                 MAYBE('>', '}')
1053                 MAYBE('=', TP_PERCENTEQUAL)
1054                         case ':':
1055                                 MAYBE_PROLOG
1056                                         case '%':
1057                                                 MAYBE_PROLOG
1058                                                 MAYBE(':', TP_HASHHASH)
1059                                                 ELSE_CODE(
1060                                                         put_back(input.c);
1061                                                         input.c = '%';
1062                                                         pp_token.kind = '#';
1063                                                 )
1064                                 ELSE('#')
1065                 ELSE('%')
1066         case '<':
1067                 MAYBE_PROLOG
1068                 MAYBE(':', '[')
1069                 MAYBE('%', '{')
1070                 MAYBE('=', TP_LESSEQUAL)
1071                         case '<':
1072                                 MAYBE_PROLOG
1073                                 MAYBE('=', TP_LESSLESSEQUAL)
1074                                 ELSE(TP_LESSLESS)
1075                 ELSE('<')
1076         case '>':
1077                 MAYBE_PROLOG
1078                 MAYBE('=', TP_GREATEREQUAL)
1079                         case '>':
1080                                 MAYBE_PROLOG
1081                                 MAYBE('=', TP_GREATERGREATEREQUAL)
1082                                 ELSE(TP_GREATERGREATER)
1083                 ELSE('>')
1084         case '^':
1085                 MAYBE_PROLOG
1086                 MAYBE('=', TP_CARETEQUAL)
1087                 ELSE('^')
1088         case '|':
1089                 MAYBE_PROLOG
1090                 MAYBE('=', TP_PIPEEQUAL)
1091                 MAYBE('|', TP_PIPEPIPE)
1092                 ELSE('|')
1093         case ':':
1094                 MAYBE_PROLOG
1095                 MAYBE('>', ']')
1096                 ELSE(':')
1097         case '=':
1098                 MAYBE_PROLOG
1099                 MAYBE('=', TP_EQUALEQUAL)
1100                 ELSE('=')
1101         case '#':
1102                 MAYBE_PROLOG
1103                 MAYBE('#', TP_HASHHASH)
1104                 ELSE_CODE(
1105                         pp_token.kind = '#';
1106                 )
1107
1108         case '?':
1109         case '[':
1110         case ']':
1111         case '(':
1112         case ')':
1113         case '{':
1114         case '}':
1115         case '~':
1116         case ';':
1117         case ',':
1118         case '\\':
1119                 pp_token.kind = input.c;
1120                 next_char();
1121                 return;
1122
1123         case EOF:
1124                 if (input_stack != NULL) {
1125                         close_input();
1126                         pop_restore_input();
1127                         fputc('\n', out);
1128                         print_line_directive(&input.position, "2");
1129                         goto restart;
1130                 } else {
1131                         pp_token.base.source_position.lineno++;
1132                         info.at_line_begin = true;
1133                         pp_token.kind = TP_EOF;
1134                 }
1135                 return;
1136
1137         default:
1138                 next_char();
1139                 if (!ignore_unknown_chars) {
1140                         errorf(&pp_token.base.source_position,
1141                                "unknown character '%c' found\n", input.c);
1142                         goto restart;
1143                 } else {
1144                         pp_token.kind = input.c;
1145                         return;
1146                 }
1147         }
1148 }
1149
1150 static void print_quoted_string(const char *const string)
1151 {
1152         fputc('"', out);
1153         for (const char *c = string; *c != 0; ++c) {
1154                 switch (*c) {
1155                 case '"': fputs("\\\"", out); break;
1156                 case '\\':  fputs("\\\\", out); break;
1157                 case '\a':  fputs("\\a", out); break;
1158                 case '\b':  fputs("\\b", out); break;
1159                 case '\f':  fputs("\\f", out); break;
1160                 case '\n':  fputs("\\n", out); break;
1161                 case '\r':  fputs("\\r", out); break;
1162                 case '\t':  fputs("\\t", out); break;
1163                 case '\v':  fputs("\\v", out); break;
1164                 case '\?':  fputs("\\?", out); break;
1165                 default:
1166                         if (!isprint(*c)) {
1167                                 fprintf(out, "\\%03o", (unsigned)*c);
1168                                 break;
1169                         }
1170                         fputc(*c, out);
1171                         break;
1172                 }
1173         }
1174         fputc('"', out);
1175 }
1176
1177 static void print_line_directive(const source_position_t *pos, const char *add)
1178 {
1179         fprintf(out, "# %u ", pos->lineno);
1180         print_quoted_string(pos->input_name);
1181         if (add != NULL) {
1182                 fputc(' ', out);
1183                 fputs(add, out);
1184         }
1185
1186         printed_input_name = pos->input_name;
1187         input.output_line  = pos->lineno-1;
1188 }
1189
1190 static bool emit_newlines(void)
1191 {
1192         unsigned delta = pp_token.base.source_position.lineno - input.output_line;
1193         if (delta == 0)
1194                 return false;
1195
1196         if (delta >= 9) {
1197                 fputc('\n', out);
1198                 print_line_directive(&pp_token.base.source_position, NULL);
1199                 fputc('\n', out);
1200         } else {
1201                 for (unsigned i = 0; i < delta; ++i) {
1202                         fputc('\n', out);
1203                 }
1204         }
1205         input.output_line = pp_token.base.source_position.lineno;
1206
1207         for (unsigned i = 0; i < info.whitespace; ++i)
1208                 fputc(' ', out);
1209
1210         return true;
1211 }
1212
1213 static void emit_pp_token(void)
1214 {
1215         if (skip_mode)
1216                 return;
1217
1218         if (!emit_newlines() &&
1219             (info.had_whitespace || tokens_would_paste(last_token, pp_token.kind)))
1220                 fputc(' ', out);
1221
1222         switch (pp_token.kind) {
1223         case TP_IDENTIFIER:
1224                 fputs(pp_token.base.symbol->string, out);
1225                 break;
1226         case TP_NUMBER:
1227                 fputs(pp_token.number.number.begin, out);
1228                 break;
1229         case TP_WIDE_STRING_LITERAL:
1230                 fputc('L', out);
1231         case TP_STRING_LITERAL:
1232                 fputc('"', out);
1233                 fputs(pp_token.string.string.begin, out);
1234                 fputc('"', out);
1235                 break;
1236         case TP_WIDE_CHARACTER_CONSTANT:
1237                 fputc('L', out);
1238         case TP_CHARACTER_CONSTANT:
1239                 fputc('\'', out);
1240                 fputs(pp_token.string.string.begin, out);
1241                 fputc('\'', out);
1242                 break;
1243         default:
1244                 print_pp_token_kind(out, pp_token.kind);
1245                 break;
1246         }
1247         last_token = pp_token.kind;
1248 }
1249
1250 static void eat_pp_directive(void)
1251 {
1252         while (!info.at_line_begin) {
1253                 next_preprocessing_token();
1254         }
1255 }
1256
1257 static bool strings_equal(const string_t *string1, const string_t *string2)
1258 {
1259         size_t size = string1->size;
1260         if (size != string2->size)
1261                 return false;
1262
1263         const char *c1 = string1->begin;
1264         const char *c2 = string2->begin;
1265         for (size_t i = 0; i < size; ++i, ++c1, ++c2) {
1266                 if (*c1 != *c2)
1267                         return false;
1268         }
1269         return true;
1270 }
1271
1272 static bool pp_tokens_equal(const token_t *token1, const token_t *token2)
1273 {
1274         if (token1->kind != token2->kind)
1275                 return false;
1276
1277         switch (token1->kind) {
1278         case TP_IDENTIFIER:
1279                 return token1->base.symbol == token2->base.symbol;
1280
1281         case TP_NUMBER:
1282         case TP_CHARACTER_CONSTANT:
1283         case TP_STRING_LITERAL:
1284                 return strings_equal(&token1->string.string, &token2->string.string);
1285
1286         default:
1287                 return true;
1288         }
1289 }
1290
1291 static bool pp_definitions_equal(const pp_definition_t *definition1,
1292                                  const pp_definition_t *definition2)
1293 {
1294         if (definition1->list_len != definition2->list_len)
1295                 return false;
1296
1297         size_t         len = definition1->list_len;
1298         const token_t *t1  = definition1->token_list;
1299         const token_t *t2  = definition2->token_list;
1300         for (size_t i = 0; i < len; ++i, ++t1, ++t2) {
1301                 if (!pp_tokens_equal(t1, t2))
1302                         return false;
1303         }
1304         return true;
1305 }
1306
1307 static void parse_define_directive(void)
1308 {
1309         eat_pp(TP_define);
1310         assert(obstack_object_size(&pp_obstack) == 0);
1311
1312         if (pp_token.kind != TP_IDENTIFIER || info.at_line_begin) {
1313                 errorf(&pp_token.base.source_position,
1314                        "expected identifier after #define, got '%t'", &pp_token);
1315                 goto error_out;
1316         }
1317         symbol_t *const symbol = pp_token.base.symbol;
1318
1319         pp_definition_t *new_definition
1320                 = obstack_alloc(&pp_obstack, sizeof(new_definition[0]));
1321         memset(new_definition, 0, sizeof(new_definition[0]));
1322         new_definition->source_position = input.position;
1323
1324         /* this is probably the only place where spaces are significant in the
1325          * lexer (except for the fact that they separate tokens). #define b(x)
1326          * is something else than #define b (x) */
1327         if (input.c == '(') {
1328                 /* eat the '(' */
1329                 next_preprocessing_token();
1330                 /* get next token after '(' */
1331                 next_preprocessing_token();
1332
1333                 while (true) {
1334                         switch (pp_token.kind) {
1335                         case TP_DOTDOTDOT:
1336                                 new_definition->is_variadic = true;
1337                                 next_preprocessing_token();
1338                                 if (pp_token.kind != ')') {
1339                                         errorf(&input.position,
1340                                                         "'...' not at end of macro argument list");
1341                                         goto error_out;
1342                                 }
1343                                 break;
1344                         case TP_IDENTIFIER:
1345                                 obstack_ptr_grow(&pp_obstack, pp_token.base.symbol);
1346                                 next_preprocessing_token();
1347
1348                                 if (pp_token.kind == ',') {
1349                                         next_preprocessing_token();
1350                                         break;
1351                                 }
1352
1353                                 if (pp_token.kind != ')') {
1354                                         errorf(&pp_token.base.source_position,
1355                                                "expected ',' or ')' after identifier, got '%t'",
1356                                                &pp_token);
1357                                         goto error_out;
1358                                 }
1359                                 break;
1360                         case ')':
1361                                 next_preprocessing_token();
1362                                 goto finish_argument_list;
1363                         default:
1364                                 errorf(&pp_token.base.source_position,
1365                                        "expected identifier, '...' or ')' in #define argument list, got '%t'",
1366                                        &pp_token);
1367                                 goto error_out;
1368                         }
1369                 }
1370
1371         finish_argument_list:
1372                 new_definition->has_parameters = true;
1373                 new_definition->n_parameters
1374                         = obstack_object_size(&pp_obstack) / sizeof(new_definition->parameters[0]);
1375                 new_definition->parameters = obstack_finish(&pp_obstack);
1376         } else {
1377                 next_preprocessing_token();
1378         }
1379
1380         /* construct a new pp_definition on the obstack */
1381         assert(obstack_object_size(&pp_obstack) == 0);
1382         size_t list_len = 0;
1383         while (!info.at_line_begin) {
1384                 obstack_grow(&pp_obstack, &pp_token, sizeof(pp_token));
1385                 ++list_len;
1386                 next_preprocessing_token();
1387         }
1388
1389         new_definition->list_len   = list_len;
1390         new_definition->token_list = obstack_finish(&pp_obstack);
1391
1392         pp_definition_t *old_definition = symbol->pp_definition;
1393         if (old_definition != NULL) {
1394                 if (!pp_definitions_equal(old_definition, new_definition)) {
1395                         warningf(WARN_OTHER, &input.position, "multiple definition of macro '%Y' (first defined %P)", symbol, &old_definition->source_position);
1396                 } else {
1397                         /* reuse the old definition */
1398                         obstack_free(&pp_obstack, new_definition);
1399                         new_definition = old_definition;
1400                 }
1401         }
1402
1403         symbol->pp_definition = new_definition;
1404         return;
1405
1406 error_out:
1407         if (obstack_object_size(&pp_obstack) > 0) {
1408                 char *ptr = obstack_finish(&pp_obstack);
1409                 obstack_free(&pp_obstack, ptr);
1410         }
1411         eat_pp_directive();
1412 }
1413
1414 static void parse_undef_directive(void)
1415 {
1416         eat_pp(TP_undef);
1417
1418         if (pp_token.kind != TP_IDENTIFIER) {
1419                 errorf(&input.position,
1420                        "expected identifier after #undef, got '%t'", &pp_token);
1421                 eat_pp_directive();
1422                 return;
1423         }
1424
1425         pp_token.base.symbol->pp_definition = NULL;
1426         next_preprocessing_token();
1427
1428         if (!info.at_line_begin) {
1429                 warningf(WARN_OTHER, &input.position, "extra tokens at end of #undef directive");
1430         }
1431         eat_pp_directive();
1432 }
1433
1434 static void parse_headername(void)
1435 {
1436         const source_position_t start_position = input.position;
1437         string_t                string         = {NULL, 0};
1438         assert(obstack_object_size(&symbol_obstack) == 0);
1439
1440         /* behind an #include we can have the special headername lexems.
1441          * They're only allowed behind an #include so they're not recognized
1442          * by the normal next_preprocessing_token. We handle them as a special
1443          * exception here */
1444         if (info.at_line_begin) {
1445                 parse_error("expected headername after #include");
1446                 goto finish_error;
1447         }
1448
1449         /* check wether we have a "... or <... headername */
1450         switch (input.c) {
1451         case '<':
1452                 next_char();
1453                 while (true) {
1454                         switch (input.c) {
1455                         case EOF:
1456                                 /* fallthrough */
1457                         MATCH_NEWLINE(
1458                                 parse_error("header name without closing '>'");
1459                                 goto finish_error;
1460                         )
1461                         case '>':
1462                                 next_char();
1463                                 goto finished_headername;
1464                         }
1465                         obstack_1grow(&symbol_obstack, (char) input.c);
1466                         next_char();
1467                 }
1468                 /* we should never be here */
1469
1470         case '"':
1471                 next_char();
1472                 while (true) {
1473                         switch (input.c) {
1474                         case EOF:
1475                                 /* fallthrough */
1476                         MATCH_NEWLINE(
1477                                 parse_error("header name without closing '>'");
1478                                 goto finish_error;
1479                         )
1480                         case '"':
1481                                 next_char();
1482                                 goto finished_headername;
1483                         }
1484                         obstack_1grow(&symbol_obstack, (char) input.c);
1485                         next_char();
1486                 }
1487                 /* we should never be here */
1488
1489         default:
1490                 /* TODO: do normal pp_token parsing and concatenate results */
1491                 panic("pp_token concat include not implemented yet");
1492         }
1493
1494 finished_headername:
1495         obstack_1grow(&symbol_obstack, '\0');
1496         const size_t size       = (size_t)obstack_object_size(&symbol_obstack);
1497         char *const  headername = obstack_finish(&symbol_obstack);
1498         string                  = make_string(headername, size);
1499
1500 finish_error:
1501         pp_token.base.source_position = start_position;
1502         pp_token.kind                 = TP_HEADERNAME;
1503         pp_token.string.string        = string;
1504 }
1505
1506 static bool do_include(bool system_include, const char *headername)
1507 {
1508         size_t headername_len = strlen(headername);
1509         if (!system_include) {
1510                 /* put dirname of current input on obstack */
1511                 const char *filename   = input.position.input_name;
1512                 const char *last_slash = strrchr(filename, '/');
1513                 if (last_slash != NULL) {
1514                         size_t len = last_slash - filename;
1515                         obstack_grow(&symbol_obstack, filename, len + 1);
1516                         obstack_grow0(&symbol_obstack, headername, headername_len);
1517                         char *complete_path = obstack_finish(&symbol_obstack);
1518                         headername = identify_string(complete_path);
1519                 }
1520
1521                 FILE *file = fopen(headername, "r");
1522                 if (file != NULL) {
1523                         switch_input(file, headername);
1524                         return true;
1525                 }
1526         }
1527
1528         assert(obstack_object_size(&symbol_obstack) == 0);
1529         /* check searchpath */
1530         for (searchpath_entry_t *entry = searchpath; entry != NULL;
1531              entry = entry->next) {
1532             const char *path = entry->path;
1533             size_t      len  = strlen(path);
1534                 obstack_grow(&symbol_obstack, path, len);
1535                 if (path[len-1] != '/')
1536                         obstack_1grow(&symbol_obstack, '/');
1537                 obstack_grow(&symbol_obstack, headername, headername_len+1);
1538
1539                 char *complete_path = obstack_finish(&symbol_obstack);
1540                 FILE *file          = fopen(complete_path, "r");
1541                 if (file != NULL) {
1542                         const char *filename = identify_string(complete_path);
1543                         switch_input(file, filename);
1544                         return true;
1545                 } else {
1546                         obstack_free(&symbol_obstack, complete_path);
1547                 }
1548         }
1549
1550         return false;
1551 }
1552
1553 /* read till next newline character, only for parse_include_directive(),
1554  * use eat_pp_directive() in all other cases */
1555 static void skip_till_newline(void)
1556 {
1557         /* skip till newline */
1558         while (true) {
1559                 switch (input.c) {
1560                 MATCH_NEWLINE(
1561                         return;
1562                 )
1563                 case EOF:
1564                         return;
1565                 }
1566                 next_char();
1567         }
1568 }
1569
1570 static bool parse_include_directive(void)
1571 {
1572         /* don't eat the TP_include here!
1573          * we need an alternative parsing for the next token */
1574         skip_whitespace();
1575         bool system_include = input.c == '<';
1576         parse_headername();
1577         string_t headername = pp_token.string.string;
1578         if (headername.begin == NULL) {
1579                 eat_pp_directive();
1580                 return false;
1581         }
1582
1583         skip_whitespace();
1584         if (!info.at_line_begin) {
1585                 warningf(WARN_OTHER, &pp_token.base.source_position,
1586                          "extra tokens at end of #include directive");
1587                 skip_till_newline();
1588         }
1589
1590         if (n_inputs > INCLUDE_LIMIT) {
1591                 errorf(&pp_token.base.source_position, "#include nested too deeply");
1592                 /* eat \n or EOF */
1593                 next_preprocessing_token();
1594                 return false;
1595         }
1596
1597         /* switch inputs */
1598         emit_newlines();
1599         push_input();
1600         bool res = do_include(system_include, pp_token.string.string.begin);
1601         if (!res) {
1602                 errorf(&pp_token.base.source_position,
1603                        "failed including '%S': %s", pp_token.string, strerror(errno));
1604                 pop_restore_input();
1605                 return false;
1606         }
1607
1608         return true;
1609 }
1610
1611 static pp_conditional_t *push_conditional(void)
1612 {
1613         pp_conditional_t *conditional
1614                 = obstack_alloc(&pp_obstack, sizeof(*conditional));
1615         memset(conditional, 0, sizeof(*conditional));
1616
1617         conditional->parent = conditional_stack;
1618         conditional_stack   = conditional;
1619
1620         return conditional;
1621 }
1622
1623 static void pop_conditional(void)
1624 {
1625         assert(conditional_stack != NULL);
1626         conditional_stack = conditional_stack->parent;
1627 }
1628
1629 static void check_unclosed_conditionals(void)
1630 {
1631         while (conditional_stack != NULL) {
1632                 pp_conditional_t *conditional = conditional_stack;
1633
1634                 if (conditional->in_else) {
1635                         errorf(&conditional->source_position, "unterminated #else");
1636                 } else {
1637                         errorf(&conditional->source_position, "unterminated condition");
1638                 }
1639                 pop_conditional();
1640         }
1641 }
1642
1643 static void parse_ifdef_ifndef_directive(void)
1644 {
1645         bool is_ifndef = (pp_token.kind == TP_ifndef);
1646         bool condition;
1647         next_preprocessing_token();
1648
1649         if (skip_mode) {
1650                 eat_pp_directive();
1651                 pp_conditional_t *conditional = push_conditional();
1652                 conditional->source_position  = pp_token.base.source_position;
1653                 conditional->skip             = true;
1654                 return;
1655         }
1656
1657         if (pp_token.kind != TP_IDENTIFIER || info.at_line_begin) {
1658                 errorf(&pp_token.base.source_position,
1659                        "expected identifier after #%s, got '%t'",
1660                        is_ifndef ? "ifndef" : "ifdef", &pp_token);
1661                 eat_pp_directive();
1662
1663                 /* just take the true case in the hope to avoid further errors */
1664                 condition = true;
1665         } else {
1666                 /* evaluate wether we are in true or false case */
1667                 condition = !pp_token.base.symbol->pp_definition == is_ifndef;
1668
1669                 next_preprocessing_token();
1670
1671                 if (!info.at_line_begin) {
1672                         errorf(&pp_token.base.source_position,
1673                                "extra tokens at end of #%s",
1674                                is_ifndef ? "ifndef" : "ifdef");
1675                         eat_pp_directive();
1676                 }
1677         }
1678
1679         pp_conditional_t *conditional = push_conditional();
1680         conditional->source_position  = pp_token.base.source_position;
1681         conditional->condition        = condition;
1682
1683         if (!condition) {
1684                 skip_mode = true;
1685         }
1686 }
1687
1688 static void parse_else_directive(void)
1689 {
1690         eat_pp(TP_else);
1691
1692         if (!info.at_line_begin) {
1693                 if (!skip_mode) {
1694                         warningf(WARN_OTHER, &pp_token.base.source_position, "extra tokens at end of #else");
1695                 }
1696                 eat_pp_directive();
1697         }
1698
1699         pp_conditional_t *conditional = conditional_stack;
1700         if (conditional == NULL) {
1701                 errorf(&pp_token.base.source_position, "#else without prior #if");
1702                 return;
1703         }
1704
1705         if (conditional->in_else) {
1706                 errorf(&pp_token.base.source_position,
1707                        "#else after #else (condition started %P)",
1708                        conditional->source_position);
1709                 skip_mode = true;
1710                 return;
1711         }
1712
1713         conditional->in_else = true;
1714         if (!conditional->skip) {
1715                 skip_mode = conditional->condition;
1716         }
1717         conditional->source_position = pp_token.base.source_position;
1718 }
1719
1720 static void parse_endif_directive(void)
1721 {
1722         eat_pp(TP_endif);
1723
1724         if (!info.at_line_begin) {
1725                 if (!skip_mode) {
1726                         warningf(WARN_OTHER, &pp_token.base.source_position, "extra tokens at end of #endif");
1727                 }
1728                 eat_pp_directive();
1729         }
1730
1731         pp_conditional_t *conditional = conditional_stack;
1732         if (conditional == NULL) {
1733                 errorf(&pp_token.base.source_position, "#endif without prior #if");
1734                 return;
1735         }
1736
1737         if (!conditional->skip) {
1738                 skip_mode = false;
1739         }
1740         pop_conditional();
1741 }
1742
1743 static void parse_preprocessing_directive(void)
1744 {
1745         eat_pp('#');
1746
1747         if (info.at_line_begin) {
1748                 /* empty directive */
1749                 return;
1750         }
1751
1752         if (skip_mode) {
1753                 switch (pp_token.kind) {
1754                 case TP_ifdef:
1755                 case TP_ifndef:
1756                         parse_ifdef_ifndef_directive();
1757                         break;
1758                 case TP_else:
1759                         parse_else_directive();
1760                         break;
1761                 case TP_endif:
1762                         parse_endif_directive();
1763                         break;
1764                 default:
1765                         eat_pp_directive();
1766                         break;
1767                 }
1768         } else {
1769                 switch (pp_token.kind) {
1770                 case TP_define:
1771                         parse_define_directive();
1772                         break;
1773                 case TP_undef:
1774                         parse_undef_directive();
1775                         break;
1776                 case TP_ifdef:
1777                 case TP_ifndef:
1778                         parse_ifdef_ifndef_directive();
1779                         break;
1780                 case TP_else:
1781                         parse_else_directive();
1782                         break;
1783                 case TP_endif:
1784                         parse_endif_directive();
1785                         break;
1786                 case TP_include:
1787                         parse_include_directive();
1788                         break;
1789                 default:
1790                         if (info.at_line_begin) {
1791                                 /* the nop directive "#" */
1792                                 break;
1793                         }
1794                         errorf(&pp_token.base.source_position,
1795                                    "invalid preprocessing directive #%t", &pp_token);
1796                         eat_pp_directive();
1797                         break;
1798                 }
1799         }
1800
1801         assert(info.at_line_begin);
1802 }
1803
1804 static void prepend_include_path(const char *path)
1805 {
1806         searchpath_entry_t *entry = OALLOCZ(&config_obstack, searchpath_entry_t);
1807         entry->path = path;
1808         entry->next = searchpath;
1809         searchpath  = entry;
1810 }
1811
1812 static void setup_include_path(void)
1813 {
1814         /* built-in paths */
1815         prepend_include_path("/usr/include");
1816
1817         /* parse environment variable */
1818         const char *cpath = getenv("CPATH");
1819         if (cpath != NULL && *cpath != '\0') {
1820                 const char *begin = cpath;
1821                 const char *c;
1822                 do {
1823                         c = begin;
1824                         while (*c != '\0' && *c != ':')
1825                                 ++c;
1826
1827                         size_t len = c-begin;
1828                         if (len == 0) {
1829                                 /* for gcc compatibility (Matze: I would expect that
1830                                  * nothing happens for an empty entry...) */
1831                                 prepend_include_path(".");
1832                         } else {
1833                                 char *string = obstack_alloc(&config_obstack, len+1);
1834                                 memcpy(string, begin, len);
1835                                 string[len] = '\0';
1836
1837                                 prepend_include_path(string);
1838                         }
1839
1840                         begin = c+1;
1841                         /* skip : */
1842                         if (*begin == ':')
1843                                 ++begin;
1844                 } while(*c != '\0');
1845         }
1846 }
1847
1848 int pptest_main(int argc, char **argv);
1849 int pptest_main(int argc, char **argv)
1850 {
1851         init_symbol_table();
1852         init_tokens();
1853
1854         obstack_init(&config_obstack);
1855         obstack_init(&pp_obstack);
1856         obstack_init(&input_obstack);
1857         strset_init(&stringset);
1858
1859         setup_include_path();
1860
1861         /* simplistic commandline parser */
1862         const char *filename = NULL;
1863         const char *output = NULL;
1864         for (int i = 1; i < argc; ++i) {
1865                 const char *opt = argv[i];
1866                 if (streq(opt, "-I")) {
1867                         prepend_include_path(argv[++i]);
1868                         continue;
1869                 } else if (streq(opt, "-E")) {
1870                         /* ignore */
1871                 } else if (streq(opt, "-o")) {
1872                         output = argv[++i];
1873                         continue;
1874                 } else if (opt[0] == '-') {
1875                         fprintf(stderr, "Unknown option '%s'\n", opt);
1876                 } else {
1877                         if (filename != NULL)
1878                                 fprintf(stderr, "Multiple inputs not supported\n");
1879                         filename = argv[i];
1880                 }
1881         }
1882         if (filename == NULL) {
1883                 fprintf(stderr, "No input specified\n");
1884                 return 1;
1885         }
1886
1887         if (output == NULL) {
1888                 out = stdout;
1889         } else {
1890                 out = fopen(output, "w");
1891                 if (out == NULL) {
1892                         fprintf(stderr, "Couldn't open output '%s'\n", output);
1893                         return 1;
1894                 }
1895         }
1896
1897         /* just here for gcc compatibility */
1898         fprintf(out, "# 1 \"%s\"\n", filename);
1899         fprintf(out, "# 1 \"<built-in>\"\n");
1900         fprintf(out, "# 1 \"<command-line>\"\n");
1901
1902         FILE *file = fopen(filename, "r");
1903         if (file == NULL) {
1904                 fprintf(stderr, "Couldn't open input '%s'\n", filename);
1905                 return 1;
1906         }
1907         switch_input(file, filename);
1908
1909         while (true) {
1910                 if (pp_token.kind == '#' && info.at_line_begin) {
1911                         parse_preprocessing_directive();
1912                         continue;
1913                 } else if (pp_token.kind == TP_EOF) {
1914                         goto end_of_main_loop;
1915                 } else if (pp_token.kind == TP_IDENTIFIER) {
1916                         symbol_t        *const symbol        = pp_token.base.symbol;
1917                         pp_definition_t *const pp_definition = symbol->pp_definition;
1918                         if (pp_definition != NULL && !pp_definition->is_expanding) {
1919                                 expansion_pos = pp_token.base.source_position;
1920                                 if (pp_definition->has_parameters) {
1921                                         source_position_t position = pp_token.base.source_position;
1922                                         add_token_info_t old_info = info;
1923                                         next_preprocessing_token();
1924                                         add_token_info_t new_info = info;
1925
1926                                         /* no opening brace -> no expansion */
1927                                         if (pp_token.kind == '(') {
1928                                                 eat_pp('(');
1929
1930                                                 /* parse arguments (TODO) */
1931                                                 while (pp_token.kind != TP_EOF && pp_token.kind != ')')
1932                                                         next_preprocessing_token();
1933                                         } else {
1934                                                 token_t next_token = pp_token;
1935                                                 /* restore identifier token */
1936                                                 pp_token.kind                 = TP_IDENTIFIER;
1937                                                 pp_token.base.symbol          = symbol;
1938                                                 pp_token.base.source_position = position;
1939                                                 info = old_info;
1940                                                 emit_pp_token();
1941
1942                                                 info = new_info;
1943                                                 pp_token = next_token;
1944                                                 continue;
1945                                         }
1946                                         info = old_info;
1947                                 }
1948                                 pp_definition->expand_pos   = 0;
1949                                 pp_definition->is_expanding = true;
1950                                 current_expansion           = pp_definition;
1951                                 expand_next();
1952                                 continue;
1953                         }
1954                 }
1955
1956                 emit_pp_token();
1957                 next_preprocessing_token();
1958         }
1959 end_of_main_loop:
1960
1961         fputc('\n', out);
1962         check_unclosed_conditionals();
1963         close_input();
1964         if (out != stdout)
1965                 fclose(out);
1966
1967         obstack_free(&input_obstack, NULL);
1968         obstack_free(&pp_obstack, NULL);
1969         obstack_free(&config_obstack, NULL);
1970
1971         strset_destroy(&stringset);
1972
1973         exit_tokens();
1974         exit_symbol_table();
1975
1976         return 0;
1977 }