Merge the implementations of parse_character_constant() and parse_string_literal().
[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         case '<':
1371                 next_char();
1372                 while (true) {
1373                         switch (input.c) {
1374                         case EOF:
1375                                 /* fallthrough */
1376                         MATCH_NEWLINE(
1377                                 parse_error("header name without closing '>'");
1378                                 goto finish_error;
1379                         )
1380                         case '>':
1381                                 next_char();
1382                                 goto finished_headername;
1383                         }
1384                         obstack_1grow(&symbol_obstack, (char) input.c);
1385                         next_char();
1386                 }
1387                 /* we should never be here */
1388
1389         case '"':
1390                 next_char();
1391                 while (true) {
1392                         switch (input.c) {
1393                         case EOF:
1394                                 /* fallthrough */
1395                         MATCH_NEWLINE(
1396                                 parse_error("header name without closing '>'");
1397                                 goto finish_error;
1398                         )
1399                         case '"':
1400                                 next_char();
1401                                 goto finished_headername;
1402                         }
1403                         obstack_1grow(&symbol_obstack, (char) input.c);
1404                         next_char();
1405                 }
1406                 /* we should never be here */
1407
1408         default:
1409                 /* TODO: do normal pp_token parsing and concatenate results */
1410                 panic("pp_token concat include not implemented yet");
1411         }
1412
1413 finished_headername:
1414         obstack_1grow(&symbol_obstack, '\0');
1415         const size_t size       = (size_t)obstack_object_size(&symbol_obstack);
1416         char *const  headername = obstack_finish(&symbol_obstack);
1417         string                  = make_string(headername, size);
1418
1419 finish_error:
1420         pp_token.base.source_position = start_position;
1421         pp_token.kind                 = TP_HEADERNAME;
1422         pp_token.string.string        = string;
1423 }
1424
1425 static bool do_include(bool system_include, const char *headername)
1426 {
1427         size_t headername_len = strlen(headername);
1428         if (!system_include) {
1429                 /* put dirname of current input on obstack */
1430                 const char *filename   = input.position.input_name;
1431                 const char *last_slash = strrchr(filename, '/');
1432                 if (last_slash != NULL) {
1433                         size_t len = last_slash - filename;
1434                         obstack_grow(&symbol_obstack, filename, len + 1);
1435                         obstack_grow0(&symbol_obstack, headername, headername_len);
1436                         char *complete_path = obstack_finish(&symbol_obstack);
1437                         headername = identify_string(complete_path);
1438                 }
1439
1440                 FILE *file = fopen(headername, "r");
1441                 if (file != NULL) {
1442                         switch_input(file, headername);
1443                         return true;
1444                 }
1445         }
1446
1447         assert(obstack_object_size(&symbol_obstack) == 0);
1448         /* check searchpath */
1449         for (searchpath_entry_t *entry = searchpath; entry != NULL;
1450              entry = entry->next) {
1451             const char *path = entry->path;
1452             size_t      len  = strlen(path);
1453                 obstack_grow(&symbol_obstack, path, len);
1454                 if (path[len-1] != '/')
1455                         obstack_1grow(&symbol_obstack, '/');
1456                 obstack_grow(&symbol_obstack, headername, headername_len+1);
1457
1458                 char *complete_path = obstack_finish(&symbol_obstack);
1459                 FILE *file          = fopen(complete_path, "r");
1460                 if (file != NULL) {
1461                         const char *filename = identify_string(complete_path);
1462                         switch_input(file, filename);
1463                         return true;
1464                 } else {
1465                         obstack_free(&symbol_obstack, complete_path);
1466                 }
1467         }
1468
1469         return false;
1470 }
1471
1472 /* read till next newline character, only for parse_include_directive(),
1473  * use eat_pp_directive() in all other cases */
1474 static void skip_till_newline(void)
1475 {
1476         /* skip till newline */
1477         while (true) {
1478                 switch (input.c) {
1479                 MATCH_NEWLINE(
1480                         return;
1481                 )
1482                 case EOF:
1483                         return;
1484                 }
1485                 next_char();
1486         }
1487 }
1488
1489 static bool parse_include_directive(void)
1490 {
1491         /* don't eat the TP_include here!
1492          * we need an alternative parsing for the next token */
1493         skip_whitespace();
1494         bool system_include = input.c == '<';
1495         parse_headername();
1496         string_t headername = pp_token.string.string;
1497         if (headername.begin == NULL) {
1498                 eat_pp_directive();
1499                 return false;
1500         }
1501
1502         skip_whitespace();
1503         if (!info.at_line_begin) {
1504                 warningf(WARN_OTHER, &pp_token.base.source_position,
1505                          "extra tokens at end of #include directive");
1506                 skip_till_newline();
1507         }
1508
1509         if (n_inputs > INCLUDE_LIMIT) {
1510                 errorf(&pp_token.base.source_position, "#include nested too deeply");
1511                 /* eat \n or EOF */
1512                 next_preprocessing_token();
1513                 return false;
1514         }
1515
1516         /* switch inputs */
1517         emit_newlines();
1518         push_input();
1519         bool res = do_include(system_include, pp_token.string.string.begin);
1520         if (!res) {
1521                 errorf(&pp_token.base.source_position,
1522                        "failed including '%S': %s", pp_token.string, strerror(errno));
1523                 pop_restore_input();
1524                 return false;
1525         }
1526
1527         return true;
1528 }
1529
1530 static pp_conditional_t *push_conditional(void)
1531 {
1532         pp_conditional_t *conditional
1533                 = obstack_alloc(&pp_obstack, sizeof(*conditional));
1534         memset(conditional, 0, sizeof(*conditional));
1535
1536         conditional->parent = conditional_stack;
1537         conditional_stack   = conditional;
1538
1539         return conditional;
1540 }
1541
1542 static void pop_conditional(void)
1543 {
1544         assert(conditional_stack != NULL);
1545         conditional_stack = conditional_stack->parent;
1546 }
1547
1548 static void check_unclosed_conditionals(void)
1549 {
1550         while (conditional_stack != NULL) {
1551                 pp_conditional_t *conditional = conditional_stack;
1552
1553                 if (conditional->in_else) {
1554                         errorf(&conditional->source_position, "unterminated #else");
1555                 } else {
1556                         errorf(&conditional->source_position, "unterminated condition");
1557                 }
1558                 pop_conditional();
1559         }
1560 }
1561
1562 static void parse_ifdef_ifndef_directive(void)
1563 {
1564         bool is_ifndef = (pp_token.kind == TP_ifndef);
1565         bool condition;
1566         next_preprocessing_token();
1567
1568         if (skip_mode) {
1569                 eat_pp_directive();
1570                 pp_conditional_t *conditional = push_conditional();
1571                 conditional->source_position  = pp_token.base.source_position;
1572                 conditional->skip             = true;
1573                 return;
1574         }
1575
1576         if (pp_token.kind != TP_IDENTIFIER || info.at_line_begin) {
1577                 errorf(&pp_token.base.source_position,
1578                        "expected identifier after #%s, got '%t'",
1579                        is_ifndef ? "ifndef" : "ifdef", &pp_token);
1580                 eat_pp_directive();
1581
1582                 /* just take the true case in the hope to avoid further errors */
1583                 condition = true;
1584         } else {
1585                 /* evaluate wether we are in true or false case */
1586                 condition = !pp_token.base.symbol->pp_definition == is_ifndef;
1587
1588                 next_preprocessing_token();
1589
1590                 if (!info.at_line_begin) {
1591                         errorf(&pp_token.base.source_position,
1592                                "extra tokens at end of #%s",
1593                                is_ifndef ? "ifndef" : "ifdef");
1594                         eat_pp_directive();
1595                 }
1596         }
1597
1598         pp_conditional_t *conditional = push_conditional();
1599         conditional->source_position  = pp_token.base.source_position;
1600         conditional->condition        = condition;
1601
1602         if (!condition) {
1603                 skip_mode = true;
1604         }
1605 }
1606
1607 static void parse_else_directive(void)
1608 {
1609         eat_pp(TP_else);
1610
1611         if (!info.at_line_begin) {
1612                 if (!skip_mode) {
1613                         warningf(WARN_OTHER, &pp_token.base.source_position, "extra tokens at end of #else");
1614                 }
1615                 eat_pp_directive();
1616         }
1617
1618         pp_conditional_t *conditional = conditional_stack;
1619         if (conditional == NULL) {
1620                 errorf(&pp_token.base.source_position, "#else without prior #if");
1621                 return;
1622         }
1623
1624         if (conditional->in_else) {
1625                 errorf(&pp_token.base.source_position,
1626                        "#else after #else (condition started %P)",
1627                        conditional->source_position);
1628                 skip_mode = true;
1629                 return;
1630         }
1631
1632         conditional->in_else = true;
1633         if (!conditional->skip) {
1634                 skip_mode = conditional->condition;
1635         }
1636         conditional->source_position = pp_token.base.source_position;
1637 }
1638
1639 static void parse_endif_directive(void)
1640 {
1641         eat_pp(TP_endif);
1642
1643         if (!info.at_line_begin) {
1644                 if (!skip_mode) {
1645                         warningf(WARN_OTHER, &pp_token.base.source_position, "extra tokens at end of #endif");
1646                 }
1647                 eat_pp_directive();
1648         }
1649
1650         pp_conditional_t *conditional = conditional_stack;
1651         if (conditional == NULL) {
1652                 errorf(&pp_token.base.source_position, "#endif without prior #if");
1653                 return;
1654         }
1655
1656         if (!conditional->skip) {
1657                 skip_mode = false;
1658         }
1659         pop_conditional();
1660 }
1661
1662 static void parse_preprocessing_directive(void)
1663 {
1664         eat_pp('#');
1665
1666         if (info.at_line_begin) {
1667                 /* empty directive */
1668                 return;
1669         }
1670
1671         if (skip_mode) {
1672                 switch (pp_token.kind) {
1673                 case TP_ifdef:
1674                 case TP_ifndef:
1675                         parse_ifdef_ifndef_directive();
1676                         break;
1677                 case TP_else:
1678                         parse_else_directive();
1679                         break;
1680                 case TP_endif:
1681                         parse_endif_directive();
1682                         break;
1683                 default:
1684                         eat_pp_directive();
1685                         break;
1686                 }
1687         } else {
1688                 switch (pp_token.kind) {
1689                 case TP_define:
1690                         parse_define_directive();
1691                         break;
1692                 case TP_undef:
1693                         parse_undef_directive();
1694                         break;
1695                 case TP_ifdef:
1696                 case TP_ifndef:
1697                         parse_ifdef_ifndef_directive();
1698                         break;
1699                 case TP_else:
1700                         parse_else_directive();
1701                         break;
1702                 case TP_endif:
1703                         parse_endif_directive();
1704                         break;
1705                 case TP_include:
1706                         parse_include_directive();
1707                         break;
1708                 default:
1709                         if (info.at_line_begin) {
1710                                 /* the nop directive "#" */
1711                                 break;
1712                         }
1713                         errorf(&pp_token.base.source_position,
1714                                    "invalid preprocessing directive #%t", &pp_token);
1715                         eat_pp_directive();
1716                         break;
1717                 }
1718         }
1719
1720         assert(info.at_line_begin);
1721 }
1722
1723 static void prepend_include_path(const char *path)
1724 {
1725         searchpath_entry_t *entry = OALLOCZ(&config_obstack, searchpath_entry_t);
1726         entry->path = path;
1727         entry->next = searchpath;
1728         searchpath  = entry;
1729 }
1730
1731 static void setup_include_path(void)
1732 {
1733         /* built-in paths */
1734         prepend_include_path("/usr/include");
1735
1736         /* parse environment variable */
1737         const char *cpath = getenv("CPATH");
1738         if (cpath != NULL && *cpath != '\0') {
1739                 const char *begin = cpath;
1740                 const char *c;
1741                 do {
1742                         c = begin;
1743                         while (*c != '\0' && *c != ':')
1744                                 ++c;
1745
1746                         size_t len = c-begin;
1747                         if (len == 0) {
1748                                 /* for gcc compatibility (Matze: I would expect that
1749                                  * nothing happens for an empty entry...) */
1750                                 prepend_include_path(".");
1751                         } else {
1752                                 char *string = obstack_alloc(&config_obstack, len+1);
1753                                 memcpy(string, begin, len);
1754                                 string[len] = '\0';
1755
1756                                 prepend_include_path(string);
1757                         }
1758
1759                         begin = c+1;
1760                         /* skip : */
1761                         if (*begin == ':')
1762                                 ++begin;
1763                 } while(*c != '\0');
1764         }
1765 }
1766
1767 int pptest_main(int argc, char **argv);
1768 int pptest_main(int argc, char **argv)
1769 {
1770         init_symbol_table();
1771         init_tokens();
1772
1773         obstack_init(&config_obstack);
1774         obstack_init(&pp_obstack);
1775         obstack_init(&input_obstack);
1776         strset_init(&stringset);
1777
1778         setup_include_path();
1779
1780         /* simplistic commandline parser */
1781         const char *filename = NULL;
1782         const char *output = NULL;
1783         for (int i = 1; i < argc; ++i) {
1784                 const char *opt = argv[i];
1785                 if (streq(opt, "-I")) {
1786                         prepend_include_path(argv[++i]);
1787                         continue;
1788                 } else if (streq(opt, "-E")) {
1789                         /* ignore */
1790                 } else if (streq(opt, "-o")) {
1791                         output = argv[++i];
1792                         continue;
1793                 } else if (opt[0] == '-') {
1794                         fprintf(stderr, "Unknown option '%s'\n", opt);
1795                 } else {
1796                         if (filename != NULL)
1797                                 fprintf(stderr, "Multiple inputs not supported\n");
1798                         filename = argv[i];
1799                 }
1800         }
1801         if (filename == NULL) {
1802                 fprintf(stderr, "No input specified\n");
1803                 return 1;
1804         }
1805
1806         if (output == NULL) {
1807                 out = stdout;
1808         } else {
1809                 out = fopen(output, "w");
1810                 if (out == NULL) {
1811                         fprintf(stderr, "Couldn't open output '%s'\n", output);
1812                         return 1;
1813                 }
1814         }
1815
1816         /* just here for gcc compatibility */
1817         fprintf(out, "# 1 \"%s\"\n", filename);
1818         fprintf(out, "# 1 \"<built-in>\"\n");
1819         fprintf(out, "# 1 \"<command-line>\"\n");
1820
1821         FILE *file = fopen(filename, "r");
1822         if (file == NULL) {
1823                 fprintf(stderr, "Couldn't open input '%s'\n", filename);
1824                 return 1;
1825         }
1826         switch_input(file, filename);
1827
1828         while (true) {
1829                 if (pp_token.kind == '#' && info.at_line_begin) {
1830                         parse_preprocessing_directive();
1831                         continue;
1832                 } else if (pp_token.kind == TP_EOF) {
1833                         goto end_of_main_loop;
1834                 } else if (pp_token.kind == TP_IDENTIFIER) {
1835                         symbol_t        *const symbol        = pp_token.base.symbol;
1836                         pp_definition_t *const pp_definition = symbol->pp_definition;
1837                         if (pp_definition != NULL && !pp_definition->is_expanding) {
1838                                 expansion_pos = pp_token.base.source_position;
1839                                 if (pp_definition->has_parameters) {
1840                                         source_position_t position = pp_token.base.source_position;
1841                                         add_token_info_t old_info = info;
1842                                         next_preprocessing_token();
1843                                         add_token_info_t new_info = info;
1844
1845                                         /* no opening brace -> no expansion */
1846                                         if (pp_token.kind == '(') {
1847                                                 eat_pp('(');
1848
1849                                                 /* parse arguments (TODO) */
1850                                                 while (pp_token.kind != TP_EOF && pp_token.kind != ')')
1851                                                         next_preprocessing_token();
1852                                         } else {
1853                                                 token_t next_token = pp_token;
1854                                                 /* restore identifier token */
1855                                                 pp_token.kind                 = TP_IDENTIFIER;
1856                                                 pp_token.base.symbol          = symbol;
1857                                                 pp_token.base.source_position = position;
1858                                                 info = old_info;
1859                                                 emit_pp_token();
1860
1861                                                 info = new_info;
1862                                                 pp_token = next_token;
1863                                                 continue;
1864                                         }
1865                                         info = old_info;
1866                                 }
1867                                 pp_definition->expand_pos   = 0;
1868                                 pp_definition->is_expanding = true;
1869                                 current_expansion           = pp_definition;
1870                                 expand_next();
1871                                 continue;
1872                         }
1873                 }
1874
1875                 emit_pp_token();
1876                 next_preprocessing_token();
1877         }
1878 end_of_main_loop:
1879
1880         fputc('\n', out);
1881         check_unclosed_conditionals();
1882         close_input();
1883         if (out != stdout)
1884                 fclose(out);
1885
1886         obstack_free(&input_obstack, NULL);
1887         obstack_free(&pp_obstack, NULL);
1888         obstack_free(&config_obstack, NULL);
1889
1890         strset_destroy(&stringset);
1891
1892         exit_tokens();
1893         exit_symbol_table();
1894
1895         return 0;
1896 }