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