9 #include "preprocessor.h"
13 #include "adt/error.h"
14 #include "adt/strutil.h"
15 #include "adt/strset.h"
16 #include "lang_features.h"
17 #include "diagnostic.h"
18 #include "string_rep.h"
22 #define INCLUDE_LIMIT 199 /* 199 is for gcc "compatibility" */
24 typedef struct saved_token_t {
29 typedef struct whitespace_info_t {
30 /** current token had whitespace in front of it */
32 /** current token is at the beginning of a line.
33 * => a "#" at line begin starts a preprocessing directive. */
35 /** number of spaces before the first token in a line */
36 unsigned whitespace_at_line_begin;
39 struct pp_definition_t {
41 source_position_t source_position;
42 pp_definition_t *parent_expansion;
44 whitespace_info_t expand_info;
46 bool is_expanding : 1;
47 bool has_parameters : 1;
48 bool is_parameter : 1;
49 pp_definition_t *function_definition;
51 pp_definition_t *parameters;
55 saved_token_t *token_list;
58 typedef struct pp_conditional_t pp_conditional_t;
59 struct pp_conditional_t {
60 source_position_t source_position;
63 /** conditional in skip mode (then+else gets skipped) */
65 pp_conditional_t *parent;
68 typedef struct pp_input_t pp_input_t;
73 utf32 buf[1024+MAX_PUTBACK];
76 source_position_t position;
79 searchpath_entry_t *path;
82 struct searchpath_entry_t {
84 searchpath_entry_t *next;
87 static pp_input_t input;
89 static pp_input_t *input_stack;
90 static unsigned n_inputs;
91 static struct obstack input_obstack;
93 static pp_conditional_t *conditional_stack;
96 bool allow_dollar_in_symbol = true;
97 static bool resolve_escape_sequences = true;
98 static bool error_on_unknown_chars = true;
99 static bool skip_mode;
101 static struct obstack pp_obstack;
102 static struct obstack config_obstack;
103 static const char *printed_input_name = NULL;
104 static source_position_t expansion_pos;
105 static pp_definition_t *current_expansion = NULL;
106 static pp_definition_t *current_call = NULL;
107 static pp_definition_t *current_argument = NULL;
108 static pp_definition_t *argument_expanding = NULL;
109 static unsigned argument_brace_count;
110 static strset_t stringset;
111 static token_kind_t last_token;
113 static searchpath_entry_t *searchpath;
115 static whitespace_info_t next_info; /* valid if had_whitespace is true */
116 static whitespace_info_t info;
118 static inline void next_char(void);
119 static void next_input_token(void);
120 static void print_line_directive(const source_position_t *pos, const char *add);
122 static symbol_t *symbol_colongreater;
123 static symbol_t *symbol_lesscolon;
124 static symbol_t *symbol_lesspercent;
125 static symbol_t *symbol_percentcolon;
126 static symbol_t *symbol_percentcolonpercentcolon;
127 static symbol_t *symbol_percentgreater;
129 static void init_symbols(void)
131 symbol_colongreater = symbol_table_insert(":>");
132 symbol_lesscolon = symbol_table_insert("<:");
133 symbol_lesspercent = symbol_table_insert("<%");
134 symbol_percentcolon = symbol_table_insert("%:");
135 symbol_percentcolonpercentcolon = symbol_table_insert("%:%:");
136 symbol_percentgreater = symbol_table_insert("%>");
139 void switch_pp_input(FILE *const file, char const *const filename, searchpath_entry_t *const path)
142 input.input = input_from_stream(file, NULL);
145 input.output_line = 0;
146 input.position.input_name = filename;
147 input.position.lineno = 1;
150 /* indicate that we're at a new input */
151 print_line_directive(&input.position, input_stack != NULL ? "1" : NULL);
153 /* place a virtual '\n' so we realize we're at line begin */
154 input.position.lineno = 0;
158 FILE *close_pp_input(void)
160 input_free(input.input);
162 FILE* const file = input.file;
174 static void push_input(void)
176 pp_input_t *const saved_input = obstack_copy(&input_obstack, &input, sizeof(input));
178 /* adjust buffer positions */
179 if (input.bufpos != NULL)
180 saved_input->bufpos = saved_input->buf + (input.bufpos - input.buf);
181 if (input.bufend != NULL)
182 saved_input->bufend = saved_input->buf + (input.bufend - input.buf);
184 saved_input->parent = input_stack;
185 input_stack = saved_input;
189 static void pop_restore_input(void)
191 assert(n_inputs > 0);
192 assert(input_stack != NULL);
194 pp_input_t *saved_input = input_stack;
196 memcpy(&input, saved_input, sizeof(input));
199 /* adjust buffer positions */
200 if (saved_input->bufpos != NULL)
201 input.bufpos = input.buf + (saved_input->bufpos - saved_input->buf);
202 if (saved_input->bufend != NULL)
203 input.bufend = input.buf + (saved_input->bufend - saved_input->buf);
205 input_stack = saved_input->parent;
206 obstack_free(&input_obstack, saved_input);
211 * Prints a parse error message at the current token.
213 * @param msg the error message
215 static void parse_error(const char *msg)
217 errorf(&pp_token.base.source_position, "%s", msg);
220 static inline void next_real_char(void)
222 assert(input.bufpos <= input.bufend);
223 if (input.bufpos >= input.bufend) {
224 size_t const n = decode(input.input, input.buf + MAX_PUTBACK, lengthof(input.buf) - MAX_PUTBACK);
229 input.bufpos = input.buf + MAX_PUTBACK;
230 input.bufend = input.bufpos + n;
232 input.c = *input.bufpos++;
233 ++input.position.colno;
237 * Put a character back into the buffer.
239 * @param pc the character to put back
241 static inline void put_back(utf32 const pc)
243 assert(input.bufpos > input.buf);
244 *(--input.bufpos - input.buf + input.buf) = (char) pc;
245 --input.position.colno;
251 if (input.c == '\n') { \
255 ++input.position.lineno; \
256 input.position.colno = 1; \
258 newline // Let it look like an ordinary case label.
260 #define eat(c_type) (assert(input.c == c_type), next_char())
262 static void maybe_concat_lines(void)
268 info.whitespace_at_line_begin = 0;
280 * Set c to the next input character, ie.
281 * after expanding trigraphs.
283 static inline void next_char(void)
287 /* filter trigraphs and concatenated lines */
288 if (UNLIKELY(input.c == '\\')) {
289 maybe_concat_lines();
290 goto end_of_next_char;
293 if (LIKELY(input.c != '?'))
294 goto end_of_next_char;
297 if (LIKELY(input.c != '?')) {
300 goto end_of_next_char;
305 case '=': input.c = '#'; break;
306 case '(': input.c = '['; break;
307 case '/': input.c = '\\'; maybe_concat_lines(); break;
308 case ')': input.c = ']'; break;
309 case '\'': input.c = '^'; break;
310 case '<': input.c = '{'; break;
311 case '!': input.c = '|'; break;
312 case '>': input.c = '}'; break;
313 case '-': input.c = '~'; break;
323 printf("nchar '%c'\n", input.c);
330 * Returns true if the given char is a octal digit.
332 * @param char the character to check
334 static inline bool is_octal_digit(int chr)
352 * Returns the value of a digit.
353 * The only portable way to do it ...
355 static int digit_value(int digit)
381 panic("wrong character given");
386 * Parses an octal character sequence.
388 * @param first_digit the already read first digit
390 static utf32 parse_octal_sequence(const utf32 first_digit)
392 assert(is_octal_digit(first_digit));
393 utf32 value = digit_value(first_digit);
394 if (!is_octal_digit(input.c)) return value;
395 value = 8 * value + digit_value(input.c);
397 if (!is_octal_digit(input.c)) return value;
398 value = 8 * value + digit_value(input.c);
405 * Parses a hex character sequence.
407 static utf32 parse_hex_sequence(void)
410 while (isxdigit(input.c)) {
411 value = 16 * value + digit_value(input.c);
417 static bool is_universal_char_valid(utf32 const v)
420 if (v < 0xA0U && v != 0x24 && v != 0x40 && v != 0x60)
422 if (0xD800 <= v && v <= 0xDFFF)
427 static utf32 parse_universal_char(unsigned const n_digits)
430 for (unsigned k = n_digits; k != 0; --k) {
431 if (isxdigit(input.c)) {
432 v = 16 * v + digit_value(input.c);
433 if (!resolve_escape_sequences)
434 obstack_1grow(&symbol_obstack, input.c);
437 errorf(&input.position,
438 "short universal character name, expected %u more digits",
443 if (!is_universal_char_valid(v)) {
444 errorf(&input.position,
445 "\\%c%0*X is not a valid universal character name",
446 n_digits == 4 ? 'u' : 'U', (int)n_digits, v);
451 static bool is_universal_char_valid_identifier(utf32 const v)
454 if ( v == 0x000A8) return true;
455 if ( v == 0x000AA) return true;
456 if ( v == 0x000AD) return true;
457 if ( v == 0x000AF) return true;
458 if (0x000B2 <= v && v <= 0x000B5) return true;
459 if (0x000B7 <= v && v <= 0x000BA) return true;
460 if (0x000BC <= v && v <= 0x000BE) return true;
461 if (0x000C0 <= v && v <= 0x000D6) return true;
462 if (0x000D8 <= v && v <= 0x000F6) return true;
463 if (0x000F8 <= v && v <= 0x000FF) return true;
464 if (0x00100 <= v && v <= 0x0167F) return true;
465 if (0x01681 <= v && v <= 0x0180D) return true;
466 if (0x0180F <= v && v <= 0x01FFF) return true;
467 if (0x0200B <= v && v <= 0x0200D) return true;
468 if (0x0202A <= v && v <= 0x0202E) return true;
469 if (0x0203F <= v && v <= 0x02040) return true;
470 if ( v == 0x02054) return true;
471 if (0x02060 <= v && v <= 0x0206F) return true;
472 if (0x02070 <= v && v <= 0x0218F) return true;
473 if (0x02460 <= v && v <= 0x024FF) return true;
474 if (0x02776 <= v && v <= 0x02793) return true;
475 if (0x02C00 <= v && v <= 0x02DFF) return true;
476 if (0x02E80 <= v && v <= 0x02FFF) return true;
477 if (0x03004 <= v && v <= 0x03007) return true;
478 if (0x03021 <= v && v <= 0x0302F) return true;
479 if (0x03031 <= v && v <= 0x0303F) return true;
480 if (0x03040 <= v && v <= 0x0D7FF) return true;
481 if (0x0F900 <= v && v <= 0x0FD3D) return true;
482 if (0x0FD40 <= v && v <= 0x0FDCF) return true;
483 if (0x0FDF0 <= v && v <= 0x0FE44) return true;
484 if (0x0FE47 <= v && v <= 0x0FFFD) return true;
485 if (0x10000 <= v && v <= 0x1FFFD) return true;
486 if (0x20000 <= v && v <= 0x2FFFD) return true;
487 if (0x30000 <= v && v <= 0x3FFFD) return true;
488 if (0x40000 <= v && v <= 0x4FFFD) return true;
489 if (0x50000 <= v && v <= 0x5FFFD) return true;
490 if (0x60000 <= v && v <= 0x6FFFD) return true;
491 if (0x70000 <= v && v <= 0x7FFFD) return true;
492 if (0x80000 <= v && v <= 0x8FFFD) return true;
493 if (0x90000 <= v && v <= 0x9FFFD) return true;
494 if (0xA0000 <= v && v <= 0xAFFFD) return true;
495 if (0xB0000 <= v && v <= 0xBFFFD) return true;
496 if (0xC0000 <= v && v <= 0xCFFFD) return true;
497 if (0xD0000 <= v && v <= 0xDFFFD) return true;
498 if (0xE0000 <= v && v <= 0xEFFFD) return true;
502 static bool is_universal_char_valid_identifier_start(utf32 const v)
505 if (0x0300 <= v && v <= 0x036F) return false;
506 if (0x1DC0 <= v && v <= 0x1DFF) return false;
507 if (0x20D0 <= v && v <= 0x20FF) return false;
508 if (0xFE20 <= v && v <= 0xFE2F) return false;
513 * Parse an escape sequence.
515 static utf32 parse_escape_sequence(void)
519 utf32 const ec = input.c;
523 case '"': return '"';
524 case '\'': return '\'';
525 case '\\': return '\\';
526 case '?': return '\?';
527 case 'a': return '\a';
528 case 'b': return '\b';
529 case 'f': return '\f';
530 case 'n': return '\n';
531 case 'r': return '\r';
532 case 't': return '\t';
533 case 'v': return '\v';
535 return parse_hex_sequence();
544 return parse_octal_sequence(ec);
546 parse_error("reached end of file while parsing escape sequence");
548 /* \E is not documented, but handled, by GCC. It is acceptable according
549 * to §6.11.4, whereas \e is not. */
553 return 27; /* hopefully 27 is ALWAYS the code for ESCAPE */
556 case 'U': return parse_universal_char(8);
557 case 'u': return parse_universal_char(4);
562 /* §6.4.4.4:8 footnote 64 */
563 parse_error("unknown escape sequence");
567 static const char *identify_string(char *string)
569 const char *result = strset_insert(&stringset, string);
570 if (result != string) {
571 obstack_free(&symbol_obstack, string);
576 static string_t sym_make_string(string_encoding_t const enc)
578 obstack_1grow(&symbol_obstack, '\0');
579 size_t const len = obstack_object_size(&symbol_obstack) - 1;
580 char *const string = obstack_finish(&symbol_obstack);
581 char const *const result = identify_string(string);
582 return (string_t){ result, len, enc };
585 string_t make_string(char const *const string)
587 obstack_grow(&symbol_obstack, string, strlen(string));
588 return sym_make_string(STRING_ENCODING_CHAR);
591 static void parse_string(utf32 const delimiter, token_kind_t const kind,
592 string_encoding_t const enc,
593 char const *const context)
595 const unsigned start_linenr = input.position.lineno;
602 if (resolve_escape_sequences) {
603 utf32 const tc = parse_escape_sequence();
604 if (enc == STRING_ENCODING_CHAR) {
606 warningf(WARN_OTHER, &pp_token.base.source_position, "escape sequence out of range");
608 obstack_1grow(&symbol_obstack, tc);
610 obstack_grow_utf8(&symbol_obstack, tc);
613 obstack_1grow(&symbol_obstack, (char)input.c);
615 obstack_1grow(&symbol_obstack, (char)input.c);
622 errorf(&pp_token.base.source_position, "newline while parsing %s", context);
626 source_position_t source_position;
627 source_position.input_name = pp_token.base.source_position.input_name;
628 source_position.lineno = start_linenr;
629 errorf(&source_position, "EOF while parsing %s", context);
634 if (input.c == delimiter) {
638 obstack_grow_utf8(&symbol_obstack, input.c);
646 pp_token.kind = kind;
647 pp_token.literal.string = sym_make_string(enc);
650 static void parse_string_literal(string_encoding_t const enc)
652 parse_string('"', T_STRING_LITERAL, enc, "string literal");
655 static void parse_character_constant(string_encoding_t const enc)
657 parse_string('\'', T_CHARACTER_CONSTANT, enc, "character constant");
658 if (pp_token.literal.string.size == 0) {
659 parse_error("empty character constant");
663 #define SYMBOL_CASES_WITHOUT_E_P \
664 '$': if (!allow_dollar_in_symbol) goto dollar_sign; \
715 #define SYMBOL_CASES \
716 SYMBOL_CASES_WITHOUT_E_P: \
722 #define DIGIT_CASES \
734 static void start_expanding(pp_definition_t *definition)
736 definition->parent_expansion = current_expansion;
737 definition->expand_pos = 0;
738 definition->is_expanding = true;
739 if (definition->list_len > 0) {
740 definition->token_list[0].had_whitespace
741 = info.had_whitespace;
743 current_expansion = definition;
746 static void finished_expanding(pp_definition_t *definition)
748 assert(definition->is_expanding);
749 pp_definition_t *parent = definition->parent_expansion;
750 definition->parent_expansion = NULL;
751 definition->is_expanding = false;
753 /* stop further expanding once we expanded a parameter used in a
755 if (definition == argument_expanding)
756 argument_expanding = NULL;
758 assert(current_expansion == definition);
759 current_expansion = parent;
762 static void grow_string_escaped(struct obstack *obst, const string_t *string, char const *delimiter)
764 char const *prefix = get_string_encoding_prefix(string->encoding);
765 obstack_printf(obst, "%s%s", prefix, delimiter);
766 size_t size = string->size;
767 const char *str = string->begin;
768 if (resolve_escape_sequences) {
769 obstack_grow(obst, str, size);
771 for (size_t i = 0; i < size; ++i) {
772 const char c = str[i];
773 if (c == '\\' || c == '"')
774 obstack_1grow(obst, '\\');
775 obstack_1grow(obst, c);
778 obstack_printf(obst, "%s", delimiter);
781 static void grow_token(struct obstack *obst, const token_t *token)
783 switch (token->kind) {
785 obstack_grow(obst, token->literal.string.begin, token->literal.string.size);
788 case T_STRING_LITERAL: {
789 char const *const delimiter = resolve_escape_sequences ? "\"" : "\\\"";
790 grow_string_escaped(obst, &token->literal.string, delimiter);
794 case T_CHARACTER_CONSTANT:
795 grow_string_escaped(obst, &token->literal.string, "'");
800 const char *str = token->base.symbol->string;
801 size_t len = strlen(str);
802 obstack_grow(obst, str, len);
808 static void stringify(const pp_definition_t *definition)
810 assert(obstack_object_size(&symbol_obstack) == 0);
812 size_t list_len = definition->list_len;
813 for (size_t p = 0; p < list_len; ++p) {
814 const saved_token_t *saved = &definition->token_list[p];
815 if (p > 0 && saved->had_whitespace)
816 obstack_1grow(&symbol_obstack, ' ');
817 grow_token(&symbol_obstack, &saved->token);
819 pp_token.kind = T_STRING_LITERAL;
820 pp_token.literal.string = sym_make_string(STRING_ENCODING_CHAR);
823 static inline void set_punctuator(token_kind_t const kind)
825 pp_token.kind = kind;
826 pp_token.base.symbol = token_symbols[kind];
829 static inline void set_digraph(token_kind_t const kind, symbol_t *const symbol)
831 pp_token.kind = kind;
832 pp_token.base.symbol = symbol;
836 * returns next final token from a preprocessor macro expansion
838 static bool expand_next(void)
840 if (current_expansion == NULL)
844 size_t pos = current_expansion->expand_pos;
845 if (pos >= current_expansion->list_len) {
846 finished_expanding(current_expansion);
847 /* it was the outermost expansion, parse pptoken normally */
848 if (current_expansion == NULL) {
853 const saved_token_t *saved = ¤t_expansion->token_list[pos++];
854 pp_token = saved->token;
855 if (pp_token.kind == '#') {
856 if (pos < current_expansion->list_len) {
857 const saved_token_t *next = ¤t_expansion->token_list[pos];
858 if (next->token.kind == T_MACRO_PARAMETER) {
859 pp_definition_t *def = next->token.macro_parameter.def;
860 assert(def != NULL && def->is_parameter);
867 if (current_expansion->expand_pos > 0)
868 info.had_whitespace = saved->had_whitespace;
869 current_expansion->expand_pos = pos;
870 pp_token.base.source_position = expansion_pos;
876 * Returns the next token kind found when continuing the current expansions
877 * without starting new sub-expansions.
879 static token_kind_t peek_expansion(void)
881 for (pp_definition_t *e = current_expansion; e; e = e->parent_expansion) {
882 if (e->expand_pos < e->list_len)
883 return e->token_list[e->expand_pos].token.kind;
888 static void skip_line_comment(void)
890 info.had_whitespace = true;
907 static void skip_multiline_comment(void)
909 info.had_whitespace = true;
911 unsigned start_linenr = input.position.lineno;
916 if (input.c == '*') {
917 /* TODO: nested comment, warn here */
922 if (input.c == '/') {
923 if (input.position.lineno != input.output_line)
924 info.whitespace_at_line_begin = input.position.colno;
934 source_position_t source_position;
935 source_position.input_name = pp_token.base.source_position.input_name;
936 source_position.lineno = start_linenr;
937 errorf(&source_position, "at end of file while looking for comment end");
948 static bool skip_till_newline(bool stop_at_non_whitespace)
960 if (input.c == '/') {
964 } else if (input.c == '*') {
966 skip_multiline_comment();
978 if (stop_at_non_whitespace)
987 static void skip_whitespace(void)
993 ++info.whitespace_at_line_begin;
994 info.had_whitespace = true;
999 info.at_line_begin = true;
1000 info.had_whitespace = true;
1001 info.whitespace_at_line_begin = 0;
1006 if (input.c == '/') {
1008 skip_line_comment();
1010 } else if (input.c == '*') {
1012 skip_multiline_comment();
1026 static inline void eat_pp(pp_token_kind_t const kind)
1028 assert(pp_token.base.symbol->pp_ID == kind);
1033 static inline void eat_token(token_kind_t const kind)
1035 assert(pp_token.kind == kind);
1040 static void parse_symbol(void)
1042 assert(obstack_object_size(&symbol_obstack) == 0);
1047 obstack_1grow(&symbol_obstack, (char) input.c);
1056 case 'U': n = 8; goto universal;
1057 case 'u': n = 4; goto universal;
1059 if (!resolve_escape_sequences) {
1060 obstack_1grow(&symbol_obstack, '\\');
1061 obstack_1grow(&symbol_obstack, input.c);
1064 utf32 const v = parse_universal_char(n);
1065 if (!is_universal_char_valid_identifier(v)) {
1066 if (is_universal_char_valid(v)) {
1067 errorf(&input.position,
1068 "universal character \\%c%0*X is not valid in an identifier",
1069 n == 4 ? 'u' : 'U', (int)n, v);
1071 } else if (obstack_object_size(&symbol_obstack) == 0 && !is_universal_char_valid_identifier_start(v)) {
1072 errorf(&input.position,
1073 "universal character \\%c%0*X is not valid as start of an identifier",
1074 n == 4 ? 'u' : 'U', (int)n, v);
1075 } else if (resolve_escape_sequences) {
1076 obstack_grow_utf8(&symbol_obstack, v);
1094 obstack_1grow(&symbol_obstack, '\0');
1095 char *string = obstack_finish(&symbol_obstack);
1097 /* might be a wide string or character constant ( L"string"/L'c' ) */
1098 if (input.c == '"' && string[0] == 'L' && string[1] == '\0') {
1099 obstack_free(&symbol_obstack, string);
1100 parse_string_literal(STRING_ENCODING_WIDE);
1102 } else if (input.c == '\'' && string[0] == 'L' && string[1] == '\0') {
1103 obstack_free(&symbol_obstack, string);
1104 parse_character_constant(STRING_ENCODING_WIDE);
1108 symbol_t *symbol = symbol_table_insert(string);
1110 pp_token.kind = symbol->ID;
1111 pp_token.base.symbol = symbol;
1113 /* we can free the memory from symbol obstack if we already had an entry in
1114 * the symbol table */
1115 if (symbol->string != string) {
1116 obstack_free(&symbol_obstack, string);
1120 static void parse_number(void)
1122 obstack_1grow(&symbol_obstack, (char) input.c);
1129 case SYMBOL_CASES_WITHOUT_E_P:
1130 obstack_1grow(&symbol_obstack, (char) input.c);
1138 obstack_1grow(&symbol_obstack, (char) input.c);
1140 if (input.c == '+' || input.c == '-') {
1141 obstack_1grow(&symbol_obstack, (char) input.c);
1153 pp_token.kind = T_NUMBER;
1154 pp_token.literal.string = sym_make_string(STRING_ENCODING_CHAR);
1157 #define MAYBE_PROLOG \
1161 #define MAYBE(ch, kind) \
1164 set_punctuator(kind); \
1167 #define MAYBE_DIGRAPH(ch, kind, symbol) \
1170 set_digraph(kind, symbol); \
1173 #define ELSE_CODE(code) \
1178 #define ELSE(kind) ELSE_CODE(set_punctuator(kind); return;)
1180 /** identifies and returns the next preprocessing token contained in the
1181 * input stream. No macro expansion is performed. */
1182 static void next_input_token(void)
1184 if (next_info.had_whitespace) {
1186 next_info.had_whitespace = false;
1188 info.at_line_begin = false;
1189 info.had_whitespace = false;
1192 pp_token.base.source_position = input.position;
1193 pp_token.base.symbol = NULL;
1198 info.whitespace_at_line_begin++;
1199 info.had_whitespace = true;
1204 info.at_line_begin = true;
1205 info.had_whitespace = true;
1206 info.whitespace_at_line_begin = 0;
1218 parse_string_literal(STRING_ENCODING_CHAR);
1222 parse_character_constant(STRING_ENCODING_CHAR);
1244 MAYBE('.', T_DOTDOTDOT)
1248 set_punctuator('.');
1254 MAYBE('&', T_ANDAND)
1255 MAYBE('=', T_ANDEQUAL)
1259 MAYBE('=', T_ASTERISKEQUAL)
1263 MAYBE('+', T_PLUSPLUS)
1264 MAYBE('=', T_PLUSEQUAL)
1268 MAYBE('>', T_MINUSGREATER)
1269 MAYBE('-', T_MINUSMINUS)
1270 MAYBE('=', T_MINUSEQUAL)
1274 MAYBE('=', T_EXCLAMATIONMARKEQUAL)
1278 MAYBE('=', T_SLASHEQUAL)
1281 skip_multiline_comment();
1285 skip_line_comment();
1290 MAYBE_DIGRAPH('>', '}', symbol_percentgreater)
1291 MAYBE('=', T_PERCENTEQUAL)
1296 MAYBE_DIGRAPH(':', T_HASHHASH, symbol_percentcolonpercentcolon)
1300 goto digraph_percentcolon;
1303 digraph_percentcolon:
1304 set_digraph('#', symbol_percentcolon);
1310 MAYBE_DIGRAPH(':', '[', symbol_lesscolon)
1311 MAYBE_DIGRAPH('%', '{', symbol_lesspercent)
1312 MAYBE('=', T_LESSEQUAL)
1315 MAYBE('=', T_LESSLESSEQUAL)
1320 MAYBE('=', T_GREATEREQUAL)
1323 MAYBE('=', T_GREATERGREATEREQUAL)
1324 ELSE(T_GREATERGREATER)
1328 MAYBE('=', T_CARETEQUAL)
1332 MAYBE('=', T_PIPEEQUAL)
1333 MAYBE('|', T_PIPEPIPE)
1337 MAYBE_DIGRAPH('>', ']', symbol_colongreater)
1339 if (c_mode & _CXX) {
1341 set_punctuator(T_COLONCOLON);
1348 MAYBE('=', T_EQUALEQUAL)
1352 MAYBE('#', T_HASHHASH)
1365 set_punctuator(input.c);
1370 if (input_stack != NULL) {
1371 fclose(close_pp_input());
1372 pop_restore_input();
1374 if (input.c == (utf32)EOF)
1375 --input.position.lineno;
1376 print_line_directive(&input.position, "2");
1379 info.at_line_begin = true;
1380 set_punctuator(T_EOF);
1386 int next_c = input.c;
1389 if (next_c == 'U' || next_c == 'u') {
1396 if (error_on_unknown_chars) {
1397 errorf(&pp_token.base.source_position,
1398 "unknown character '%lc' found\n", input.c);
1402 assert(obstack_object_size(&symbol_obstack) == 0);
1403 obstack_grow_utf8(&symbol_obstack, input.c);
1404 obstack_1grow(&symbol_obstack, '\0');
1405 char *const string = obstack_finish(&symbol_obstack);
1406 symbol_t *const symbol = symbol_table_insert(string);
1407 if (symbol->string != string)
1408 obstack_free(&symbol_obstack, string);
1410 pp_token.kind = T_UNKNOWN_CHAR;
1411 pp_token.base.symbol = symbol;
1418 static void print_quoted_string(const char *const string)
1421 for (const char *c = string; *c != 0; ++c) {
1423 case '"': fputs("\\\"", out); break;
1424 case '\\': fputs("\\\\", out); break;
1425 case '\a': fputs("\\a", out); break;
1426 case '\b': fputs("\\b", out); break;
1427 case '\f': fputs("\\f", out); break;
1428 case '\n': fputs("\\n", out); break;
1429 case '\r': fputs("\\r", out); break;
1430 case '\t': fputs("\\t", out); break;
1431 case '\v': fputs("\\v", out); break;
1432 case '\?': fputs("\\?", out); break;
1435 fprintf(out, "\\%03o", (unsigned)*c);
1445 static void print_line_directive(const source_position_t *pos, const char *add)
1450 fprintf(out, "# %u ", pos->lineno);
1451 print_quoted_string(pos->input_name);
1456 if (pos->is_system_header) {
1460 printed_input_name = pos->input_name;
1461 input.output_line = pos->lineno-1;
1464 static bool emit_newlines(void)
1466 unsigned delta = pp_token.base.source_position.lineno - input.output_line;
1472 print_line_directive(&pp_token.base.source_position, NULL);
1475 for (unsigned i = 0; i < delta; ++i) {
1479 input.output_line = pp_token.base.source_position.lineno;
1481 unsigned whitespace = info.whitespace_at_line_begin;
1482 /* make sure there is at least 1 whitespace before a (macro-expanded)
1483 * '#' at line begin. I'm not sure why this is good, but gcc does it. */
1484 if (pp_token.kind == '#' && whitespace == 0)
1486 for (unsigned i = 0; i < whitespace; ++i)
1492 void set_preprocessor_output(FILE *output)
1496 error_on_unknown_chars = false;
1497 resolve_escape_sequences = false;
1499 error_on_unknown_chars = true;
1500 resolve_escape_sequences = true;
1504 void emit_pp_token(void)
1506 if (!emit_newlines() &&
1507 (info.had_whitespace || tokens_would_paste(last_token, pp_token.kind)))
1510 switch (pp_token.kind) {
1512 fputs(pp_token.literal.string.begin, out);
1515 case T_STRING_LITERAL:
1516 fputs(get_string_encoding_prefix(pp_token.literal.string.encoding), out);
1518 fputs(pp_token.literal.string.begin, out);
1522 case T_CHARACTER_CONSTANT:
1523 fputs(get_string_encoding_prefix(pp_token.literal.string.encoding), out);
1525 fputs(pp_token.literal.string.begin, out);
1529 case T_MACRO_PARAMETER:
1530 panic("macro parameter not expanded");
1533 fputs(pp_token.base.symbol->string, out);
1536 last_token = pp_token.kind;
1539 static void eat_pp_directive(void)
1541 while (!info.at_line_begin) {
1546 static bool strings_equal(const string_t *string1, const string_t *string2)
1548 size_t size = string1->size;
1549 if (size != string2->size)
1552 const char *c1 = string1->begin;
1553 const char *c2 = string2->begin;
1554 for (size_t i = 0; i < size; ++i, ++c1, ++c2) {
1561 static bool pp_tokens_equal(const token_t *token1, const token_t *token2)
1563 if (token1->kind != token2->kind)
1566 switch (token1->kind) {
1568 case T_CHARACTER_CONSTANT:
1569 case T_STRING_LITERAL:
1570 return strings_equal(&token1->literal.string, &token2->literal.string);
1572 case T_MACRO_PARAMETER:
1573 return token1->macro_parameter.def->symbol
1574 == token2->macro_parameter.def->symbol;
1577 return token1->base.symbol == token2->base.symbol;
1581 static bool pp_definitions_equal(const pp_definition_t *definition1,
1582 const pp_definition_t *definition2)
1584 if (definition1->list_len != definition2->list_len)
1587 size_t len = definition1->list_len;
1588 const saved_token_t *t1 = definition1->token_list;
1589 const saved_token_t *t2 = definition2->token_list;
1590 for (size_t i = 0; i < len; ++i, ++t1, ++t2) {
1591 if (!pp_tokens_equal(&t1->token, &t2->token))
1593 if (t1->had_whitespace != t2->had_whitespace)
1599 static void missing_macro_param_error(void)
1601 errorf(&pp_token.base.source_position,
1602 "'#' is not followed by a macro parameter");
1605 static bool is_defineable_token(char const *const context)
1607 if (info.at_line_begin) {
1608 errorf(&pp_token.base.source_position, "unexpected end of line after %s", context);
1611 symbol_t *const symbol = pp_token.base.symbol;
1615 if (pp_token.kind != T_IDENTIFIER) {
1616 switch (symbol->string[0]) {
1623 errorf(&pp_token.base.source_position, "expected identifier after %s, got %K", context, &pp_token);
1628 /* TODO turn this into a flag in pp_def. */
1629 switch (symbol->pp_ID) {
1632 errorf(&pp_token.base.source_position, "%K cannot be used as macro name in %s", &pp_token, context);
1640 static void parse_define_directive(void)
1648 assert(obstack_object_size(&pp_obstack) == 0);
1650 if (!is_defineable_token("#define"))
1652 symbol_t *const symbol = pp_token.base.symbol;
1654 pp_definition_t *new_definition
1655 = obstack_alloc(&pp_obstack, sizeof(new_definition[0]));
1656 memset(new_definition, 0, sizeof(new_definition[0]));
1657 new_definition->symbol = symbol;
1658 new_definition->source_position = input.position;
1660 /* this is probably the only place where spaces are significant in the
1661 * lexer (except for the fact that they separate tokens). #define b(x)
1662 * is something else than #define b (x) */
1663 if (input.c == '(') {
1668 switch (pp_token.kind) {
1670 new_definition->is_variadic = true;
1671 eat_token(T_DOTDOTDOT);
1672 if (pp_token.kind != ')') {
1673 errorf(&input.position,
1674 "'...' not at end of macro argument list");
1679 case T_IDENTIFIER: {
1680 pp_definition_t parameter;
1681 memset(¶meter, 0, sizeof(parameter));
1682 parameter.source_position = pp_token.base.source_position;
1683 parameter.symbol = pp_token.base.symbol;
1684 parameter.is_parameter = true;
1685 obstack_grow(&pp_obstack, ¶meter, sizeof(parameter));
1686 eat_token(T_IDENTIFIER);
1688 if (pp_token.kind == ',') {
1693 if (pp_token.kind != ')') {
1694 errorf(&pp_token.base.source_position,
1695 "expected ',' or ')' after identifier, got %K",
1704 goto finish_argument_list;
1707 errorf(&pp_token.base.source_position,
1708 "expected identifier, '...' or ')' in #define argument list, got %K",
1714 finish_argument_list:
1715 new_definition->has_parameters = true;
1716 size_t size = obstack_object_size(&pp_obstack);
1717 new_definition->n_parameters
1718 = size / sizeof(new_definition->parameters[0]);
1719 new_definition->parameters = obstack_finish(&pp_obstack);
1720 for (size_t i = 0; i < new_definition->n_parameters; ++i) {
1721 pp_definition_t *param = &new_definition->parameters[i];
1722 symbol_t *symbol = param->symbol;
1723 pp_definition_t *previous = symbol->pp_definition;
1724 if (previous != NULL
1725 && previous->function_definition == new_definition) {
1726 errorf(¶m->source_position,
1727 "duplicate macro parameter '%Y'", symbol);
1728 param->symbol = sym_anonymous;
1731 param->parent_expansion = previous;
1732 param->function_definition = new_definition;
1733 symbol->pp_definition = param;
1739 /* construct token list */
1740 assert(obstack_object_size(&pp_obstack) == 0);
1741 bool next_must_be_param = false;
1742 while (!info.at_line_begin) {
1743 if (pp_token.kind == T_IDENTIFIER) {
1744 const symbol_t *symbol = pp_token.base.symbol;
1745 pp_definition_t *definition = symbol->pp_definition;
1746 if (definition != NULL
1747 && definition->function_definition == new_definition) {
1748 pp_token.kind = T_MACRO_PARAMETER;
1749 pp_token.macro_parameter.def = definition;
1752 if (next_must_be_param && pp_token.kind != T_MACRO_PARAMETER) {
1753 missing_macro_param_error();
1755 saved_token_t saved_token;
1756 saved_token.token = pp_token;
1757 saved_token.had_whitespace = info.had_whitespace;
1758 obstack_grow(&pp_obstack, &saved_token, sizeof(saved_token));
1760 = new_definition->has_parameters && pp_token.kind == '#';
1763 if (next_must_be_param)
1764 missing_macro_param_error();
1766 new_definition->list_len = obstack_object_size(&pp_obstack)
1767 / sizeof(new_definition->token_list[0]);
1768 new_definition->token_list = obstack_finish(&pp_obstack);
1770 if (new_definition->has_parameters) {
1771 for (size_t i = 0; i < new_definition->n_parameters; ++i) {
1772 pp_definition_t *param = &new_definition->parameters[i];
1773 symbol_t *symbol = param->symbol;
1774 if (symbol == sym_anonymous)
1776 assert(symbol->pp_definition == param);
1777 assert(param->function_definition == new_definition);
1778 symbol->pp_definition = param->parent_expansion;
1779 param->parent_expansion = NULL;
1783 pp_definition_t *old_definition = symbol->pp_definition;
1784 if (old_definition != NULL) {
1785 if (!pp_definitions_equal(old_definition, new_definition)) {
1786 warningf(WARN_OTHER, &input.position, "multiple definition of macro '%Y' (first defined %P)", symbol, &old_definition->source_position);
1788 /* reuse the old definition */
1789 obstack_free(&pp_obstack, new_definition);
1790 new_definition = old_definition;
1794 symbol->pp_definition = new_definition;
1798 if (obstack_object_size(&pp_obstack) > 0) {
1799 char *ptr = obstack_finish(&pp_obstack);
1800 obstack_free(&pp_obstack, ptr);
1805 static void parse_undef_directive(void)
1813 if (!is_defineable_token("#undef")) {
1818 pp_token.base.symbol->pp_definition = NULL;
1821 if (!info.at_line_begin) {
1822 warningf(WARN_OTHER, &input.position, "extra tokens at end of #undef directive");
1827 /** behind an #include we can have the special headername lexems.
1828 * They're only allowed behind an #include so they're not recognized
1829 * by the normal next_preprocessing_token. We handle them as a special
1831 static const char *parse_headername(bool *system_include)
1833 if (info.at_line_begin) {
1834 parse_error("expected headername after #include");
1838 /* check wether we have a "... or <... headername */
1839 source_position_t position = input.position;
1843 case '<': delimiter = '>'; *system_include = true; goto parse_name;
1844 case '"': delimiter = '"'; *system_include = false; goto parse_name;
1846 assert(obstack_object_size(&symbol_obstack) == 0);
1853 char *dummy = obstack_finish(&symbol_obstack);
1854 obstack_free(&symbol_obstack, dummy);
1856 errorf(&pp_token.base.source_position,
1857 "header name without closing '%c'", (char)delimiter);
1861 if (input.c == delimiter) {
1863 goto finish_headername;
1865 obstack_1grow(&symbol_obstack, (char)input.c);
1871 /* we should never be here */
1875 next_preprocessing_token();
1876 if (info.at_line_begin) {
1877 /* TODO: if we are already in the new line then we parsed more than
1878 * wanted. We reuse the token, but could produce following errors
1879 * misbehaviours... */
1880 goto error_invalid_input;
1882 if (pp_token.kind == T_STRING_LITERAL) {
1883 *system_include = false;
1884 return pp_token.literal.string.begin;
1885 } else if (pp_token.kind == '<') {
1886 *system_include = true;
1887 assert(obstack_object_size(&pp_obstack) == 0);
1889 next_preprocessing_token();
1890 if (info.at_line_begin) {
1891 /* TODO: we shouldn't have parsed/expanded something on the
1892 * next line yet... */
1893 char *dummy = obstack_finish(&pp_obstack);
1894 obstack_free(&pp_obstack, dummy);
1895 goto error_invalid_input;
1897 if (pp_token.kind == '>')
1900 saved_token_t saved;
1901 saved.token = pp_token;
1902 saved.had_whitespace = info.had_whitespace;
1903 obstack_grow(&pp_obstack, &saved, sizeof(saved));
1905 size_t size = obstack_object_size(&pp_obstack);
1906 assert(size % sizeof(saved_token_t) == 0);
1907 size_t n_tokens = size / sizeof(saved_token_t);
1908 saved_token_t *tokens = obstack_finish(&pp_obstack);
1909 assert(obstack_object_size(&symbol_obstack) == 0);
1910 for (size_t i = 0; i < n_tokens; ++i) {
1911 const saved_token_t *saved = &tokens[i];
1912 if (i > 0 && saved->had_whitespace)
1913 obstack_1grow(&symbol_obstack, ' ');
1914 grow_token(&symbol_obstack, &saved->token);
1916 obstack_free(&pp_obstack, tokens);
1917 goto finish_headername;
1919 error_invalid_input:
1921 char *dummy = obstack_finish(&symbol_obstack);
1922 obstack_free(&symbol_obstack, dummy);
1925 errorf(&pp_token.base.source_position,
1926 "expected \"FILENAME\" or <FILENAME> after #include");
1932 obstack_1grow(&symbol_obstack, '\0');
1933 char *const headername = obstack_finish(&symbol_obstack);
1934 const char *identified = identify_string(headername);
1935 pp_token.base.source_position = position;
1939 static bool do_include(bool const system_include, bool const include_next, char const *const headername)
1941 size_t const headername_len = strlen(headername);
1942 searchpath_entry_t *entry;
1944 entry = input.path ? input.path->next : searchpath;
1946 if (!system_include) {
1947 /* put dirname of current input on obstack */
1948 const char *filename = input.position.input_name;
1949 const char *last_slash = strrchr(filename, '/');
1950 const char *full_name;
1951 if (last_slash != NULL) {
1952 size_t len = last_slash - filename;
1953 obstack_grow(&symbol_obstack, filename, len + 1);
1954 obstack_grow0(&symbol_obstack, headername, headername_len);
1955 char *complete_path = obstack_finish(&symbol_obstack);
1956 full_name = identify_string(complete_path);
1958 full_name = headername;
1961 FILE *file = fopen(full_name, "r");
1963 switch_pp_input(file, full_name, NULL);
1971 assert(obstack_object_size(&symbol_obstack) == 0);
1972 /* check searchpath */
1973 for (; entry; entry = entry->next) {
1974 const char *path = entry->path;
1975 size_t len = strlen(path);
1976 obstack_grow(&symbol_obstack, path, len);
1977 if (path[len-1] != '/')
1978 obstack_1grow(&symbol_obstack, '/');
1979 obstack_grow(&symbol_obstack, headername, headername_len+1);
1981 char *complete_path = obstack_finish(&symbol_obstack);
1982 FILE *file = fopen(complete_path, "r");
1984 const char *filename = identify_string(complete_path);
1985 switch_pp_input(file, filename, entry);
1988 obstack_free(&symbol_obstack, complete_path);
1995 static void parse_include_directive(bool const include_next)
2002 /* do not eat the TP_include, since it would already parse the next token
2003 * which needs special handling here. */
2004 skip_till_newline(true);
2005 bool system_include;
2006 const char *headername = parse_headername(&system_include);
2007 if (headername == NULL) {
2012 bool had_nonwhitespace = skip_till_newline(false);
2013 if (had_nonwhitespace) {
2014 warningf(WARN_OTHER, &input.position,
2015 "extra tokens at end of #include directive");
2018 if (n_inputs > INCLUDE_LIMIT) {
2019 errorf(&pp_token.base.source_position, "#include nested too deeply");
2026 info.whitespace_at_line_begin = 0;
2027 info.had_whitespace = false;
2028 info.at_line_begin = true;
2031 bool res = do_include(system_include, include_next, headername);
2035 errorf(&pp_token.base.source_position, "failed including '%s': %s", headername, strerror(errno));
2036 pop_restore_input();
2040 static pp_conditional_t *push_conditional(void)
2042 pp_conditional_t *conditional
2043 = obstack_alloc(&pp_obstack, sizeof(*conditional));
2044 memset(conditional, 0, sizeof(*conditional));
2046 conditional->parent = conditional_stack;
2047 conditional_stack = conditional;
2052 static void pop_conditional(void)
2054 assert(conditional_stack != NULL);
2055 conditional_stack = conditional_stack->parent;
2058 void check_unclosed_conditionals(void)
2060 while (conditional_stack != NULL) {
2061 pp_conditional_t *conditional = conditional_stack;
2063 if (conditional->in_else) {
2064 errorf(&conditional->source_position, "unterminated #else");
2066 errorf(&conditional->source_position, "unterminated condition");
2072 static void parse_ifdef_ifndef_directive(bool const is_ifdef)
2075 eat_pp(is_ifdef ? TP_ifdef : TP_ifndef);
2079 pp_conditional_t *conditional = push_conditional();
2080 conditional->source_position = pp_token.base.source_position;
2081 conditional->skip = true;
2085 if (pp_token.kind != T_IDENTIFIER || info.at_line_begin) {
2086 errorf(&pp_token.base.source_position,
2087 "expected identifier after #%s, got %K",
2088 is_ifdef ? "ifdef" : "ifndef", &pp_token);
2091 /* just take the true case in the hope to avoid further errors */
2094 /* evaluate wether we are in true or false case */
2095 condition = (bool)pp_token.base.symbol->pp_definition == is_ifdef;
2096 eat_token(T_IDENTIFIER);
2098 if (!info.at_line_begin) {
2099 errorf(&pp_token.base.source_position,
2100 "extra tokens at end of #%s",
2101 is_ifdef ? "ifdef" : "ifndef");
2106 pp_conditional_t *conditional = push_conditional();
2107 conditional->source_position = pp_token.base.source_position;
2108 conditional->condition = condition;
2115 static void parse_else_directive(void)
2119 if (!info.at_line_begin) {
2121 warningf(WARN_OTHER, &pp_token.base.source_position, "extra tokens at end of #else");
2126 pp_conditional_t *conditional = conditional_stack;
2127 if (conditional == NULL) {
2128 errorf(&pp_token.base.source_position, "#else without prior #if");
2132 if (conditional->in_else) {
2133 errorf(&pp_token.base.source_position,
2134 "#else after #else (condition started %P)",
2135 &conditional->source_position);
2140 conditional->in_else = true;
2141 if (!conditional->skip) {
2142 skip_mode = conditional->condition;
2144 conditional->source_position = pp_token.base.source_position;
2147 static void parse_endif_directive(void)
2151 if (!info.at_line_begin) {
2153 warningf(WARN_OTHER, &pp_token.base.source_position, "extra tokens at end of #endif");
2158 pp_conditional_t *conditional = conditional_stack;
2159 if (conditional == NULL) {
2160 errorf(&pp_token.base.source_position, "#endif without prior #if");
2164 if (!conditional->skip) {
2170 typedef enum stdc_pragma_kind_t {
2174 STDC_CX_LIMITED_RANGE
2175 } stdc_pragma_kind_t;
2177 typedef enum stdc_pragma_value_kind_t {
2182 } stdc_pragma_value_kind_t;
2184 static void parse_pragma_directive(void)
2192 if (pp_token.kind != T_IDENTIFIER) {
2193 warningf(WARN_UNKNOWN_PRAGMAS, &pp_token.base.source_position,
2194 "expected identifier after #pragma");
2199 stdc_pragma_kind_t kind = STDC_UNKNOWN;
2200 if (pp_token.base.symbol->pp_ID == TP_STDC && c_mode & _C99) {
2204 switch (pp_token.base.symbol->pp_ID) {
2205 case TP_FP_CONTRACT: kind = STDC_FP_CONTRACT; break;
2206 case TP_FENV_ACCESS: kind = STDC_FENV_ACCESS; break;
2207 case TP_CX_LIMITED_RANGE: kind = STDC_CX_LIMITED_RANGE; break;
2210 if (kind != STDC_UNKNOWN) {
2212 stdc_pragma_value_kind_t value;
2213 switch (pp_token.base.symbol->pp_ID) {
2214 case TP_ON: value = STDC_VALUE_ON; break;
2215 case TP_OFF: value = STDC_VALUE_OFF; break;
2216 case TP_DEFAULT: value = STDC_VALUE_DEFAULT; break;
2217 default: value = STDC_VALUE_UNKNOWN; break;
2219 if (value == STDC_VALUE_UNKNOWN) {
2220 kind = STDC_UNKNOWN;
2221 errorf(&pp_token.base.source_position, "bad STDC pragma argument");
2226 if (kind == STDC_UNKNOWN) {
2227 warningf(WARN_UNKNOWN_PRAGMAS, &pp_token.base.source_position,
2228 "encountered unknown #pragma");
2232 static void parse_line_directive(void)
2234 if (pp_token.kind != T_NUMBER) {
2236 parse_error("expected integer");
2239 long const line = strtol(pp_token.literal.string.begin, &end, 0);
2241 /* use offset -1 as this is about the next line */
2242 input.position.lineno = line - 1;
2243 /* force output of line */
2244 input.output_line = input.position.lineno - 20;
2247 errorf(&input.position, "'%S' is not a valid line number",
2248 &pp_token.literal.string);
2252 if (info.at_line_begin)
2255 if (pp_token.kind == T_STRING_LITERAL
2256 && pp_token.literal.string.encoding == STRING_ENCODING_CHAR) {
2257 input.position.input_name = pp_token.literal.string.begin;
2258 input.position.is_system_header = false;
2261 /* attempt to parse numeric flags as outputted by gcc preprocessor */
2262 while (!info.at_line_begin && pp_token.kind == T_NUMBER) {
2264 * 1 - indicates start of a new file
2265 * 2 - indicates return from a file
2266 * 3 - indicates system header
2267 * 4 - indicates implicit extern "C" in C++ mode
2269 * currently we're only interested in "3"
2271 if (streq(pp_token.literal.string.begin, "3")) {
2272 input.position.is_system_header = true;
2281 static void parse_error_directive(void)
2288 bool const old_resolve_escape_sequences = resolve_escape_sequences;
2289 resolve_escape_sequences = false;
2291 source_position_t const pos = pp_token.base.source_position;
2293 if (info.had_whitespace && obstack_object_size(&pp_obstack) != 0)
2294 obstack_1grow(&pp_obstack, ' ');
2296 switch (pp_token.kind) {
2298 string_t const *const str = &pp_token.literal.string;
2299 obstack_grow(&pp_obstack, str->begin, str->size);
2305 case T_STRING_LITERAL: delim = '"'; goto string;
2306 case T_CHARACTER_CONSTANT: delim = '\''; goto string;
2308 string_t const *const str = &pp_token.literal.string;
2309 char const *const enc = get_string_encoding_prefix(str->encoding);
2310 obstack_printf(&pp_obstack, "%s%c%s%c", enc, delim, str->begin, delim);
2315 char const *const str = pp_token.base.symbol->string;
2316 obstack_grow(&pp_obstack, str, strlen(str));
2322 } while (!info.at_line_begin);
2324 resolve_escape_sequences = old_resolve_escape_sequences;
2326 obstack_1grow(&pp_obstack, '\0');
2327 char *const str = obstack_finish(&pp_obstack);
2328 errorf(&pos, "#%s", str);
2329 obstack_free(&pp_obstack, str);
2332 static void parse_preprocessing_directive(void)
2336 if (info.at_line_begin) {
2337 /* empty directive */
2341 if (pp_token.base.symbol) {
2342 switch (pp_token.base.symbol->pp_ID) {
2343 case TP_define: parse_define_directive(); break;
2344 case TP_else: parse_else_directive(); break;
2345 case TP_endif: parse_endif_directive(); break;
2346 case TP_error: parse_error_directive(); break;
2347 case TP_ifdef: parse_ifdef_ifndef_directive(true); break;
2348 case TP_ifndef: parse_ifdef_ifndef_directive(false); break;
2349 case TP_include: parse_include_directive(false); break;
2350 case TP_include_next: parse_include_directive(true); break;
2351 case TP_line: next_input_token(); goto line_directive;
2352 case TP_pragma: parse_pragma_directive(); break;
2353 case TP_undef: parse_undef_directive(); break;
2356 } else if (pp_token.kind == T_NUMBER) {
2358 parse_line_directive();
2362 errorf(&pp_token.base.source_position, "invalid preprocessing directive #%K", &pp_token);
2367 assert(info.at_line_begin);
2370 static void finish_current_argument(void)
2372 if (current_argument == NULL)
2374 size_t size = obstack_object_size(&pp_obstack);
2375 current_argument->list_len = size/sizeof(current_argument->token_list[0]);
2376 current_argument->token_list = obstack_finish(&pp_obstack);
2379 void next_preprocessing_token(void)
2382 if (!expand_next()) {
2385 while (pp_token.kind == '#' && info.at_line_begin) {
2386 parse_preprocessing_directive();
2388 } while (skip_mode && pp_token.kind != T_EOF);
2391 const token_kind_t kind = pp_token.kind;
2392 if (current_call == NULL || argument_expanding != NULL) {
2393 symbol_t *const symbol = pp_token.base.symbol;
2395 if (kind == T_MACRO_PARAMETER) {
2396 assert(current_expansion != NULL);
2397 start_expanding(pp_token.macro_parameter.def);
2401 pp_definition_t *const pp_definition = symbol->pp_definition;
2402 if (pp_definition != NULL && !pp_definition->is_expanding) {
2403 if (pp_definition->has_parameters) {
2405 /* check if next token is a '(' */
2406 whitespace_info_t old_info = info;
2407 token_kind_t next_token = peek_expansion();
2408 if (next_token == T_EOF) {
2409 info.at_line_begin = false;
2410 info.had_whitespace = false;
2412 if (input.c == '(') {
2417 if (next_token == '(') {
2418 if (current_expansion == NULL)
2419 expansion_pos = pp_token.base.source_position;
2420 next_preprocessing_token();
2421 assert(pp_token.kind == '(');
2423 pp_definition->parent_expansion = current_expansion;
2424 current_call = pp_definition;
2425 current_call->expand_pos = 0;
2426 current_call->expand_info = old_info;
2427 if (current_call->n_parameters > 0) {
2428 current_argument = ¤t_call->parameters[0];
2429 assert(argument_brace_count == 0);
2433 /* skip_whitespaces() skipped newlines and whitespace,
2434 * remember results for next token */
2440 if (current_expansion == NULL)
2441 expansion_pos = pp_token.base.source_position;
2442 start_expanding(pp_definition);
2449 if (current_call != NULL) {
2450 /* current_call != NULL */
2452 ++argument_brace_count;
2453 } else if (kind == ')') {
2454 if (argument_brace_count > 0) {
2455 --argument_brace_count;
2457 finish_current_argument();
2458 assert(kind == ')');
2459 start_expanding(current_call);
2460 info = current_call->expand_info;
2461 current_call = NULL;
2462 current_argument = NULL;
2465 } else if (kind == ',' && argument_brace_count == 0) {
2466 finish_current_argument();
2467 current_call->expand_pos++;
2468 if (current_call->expand_pos >= current_call->n_parameters) {
2469 errorf(&pp_token.base.source_position,
2470 "too many arguments passed for macro '%Y'",
2471 current_call->symbol);
2472 current_argument = NULL;
2475 = ¤t_call->parameters[current_call->expand_pos];
2478 } else if (kind == T_MACRO_PARAMETER) {
2479 /* parameters have to be fully expanded before being used as
2480 * parameters for another macro-call */
2481 assert(current_expansion != NULL);
2482 pp_definition_t *argument = pp_token.macro_parameter.def;
2483 argument_expanding = argument;
2484 start_expanding(argument);
2486 } else if (kind == T_EOF) {
2487 errorf(&expansion_pos,
2488 "reached end of file while parsing arguments for '%Y'",
2489 current_call->symbol);
2492 if (current_argument != NULL) {
2493 saved_token_t saved;
2494 saved.token = pp_token;
2495 saved.had_whitespace = info.had_whitespace;
2496 obstack_grow(&pp_obstack, &saved, sizeof(saved));
2503 static void prepend_include_path(const char *path)
2505 searchpath_entry_t *entry = OALLOCZ(&config_obstack, searchpath_entry_t);
2507 entry->next = searchpath;
2511 static void setup_include_path(void)
2513 /* built-in paths */
2514 prepend_include_path("/usr/include");
2516 /* parse environment variable */
2517 const char *cpath = getenv("CPATH");
2518 if (cpath != NULL && *cpath != '\0') {
2519 const char *begin = cpath;
2523 while (*c != '\0' && *c != ':')
2526 size_t len = c-begin;
2528 /* for gcc compatibility (Matze: I would expect that
2529 * nothing happens for an empty entry...) */
2530 prepend_include_path(".");
2532 char *const string = obstack_copy0(&config_obstack, begin, len);
2533 prepend_include_path(string);
2540 } while(*c != '\0');
2544 static void input_error(unsigned const delta_lines, unsigned const delta_cols, char const *const message)
2546 source_position_t pos = pp_token.base.source_position;
2547 pos.lineno += delta_lines;
2548 pos.colno += delta_cols;
2549 errorf(&pos, "%s", message);
2552 void init_preprocessor(void)
2556 obstack_init(&config_obstack);
2557 obstack_init(&pp_obstack);
2558 obstack_init(&input_obstack);
2559 strset_init(&stringset);
2561 setup_include_path();
2563 set_input_error_callback(input_error);
2566 void exit_preprocessor(void)
2568 obstack_free(&input_obstack, NULL);
2569 obstack_free(&pp_obstack, NULL);
2570 obstack_free(&config_obstack, NULL);
2572 strset_destroy(&stringset);
2575 int pptest_main(int argc, char **argv);
2576 int pptest_main(int argc, char **argv)
2578 init_symbol_table();
2579 init_preprocessor();
2582 error_on_unknown_chars = false;
2583 resolve_escape_sequences = false;
2585 /* simplistic commandline parser */
2586 const char *filename = NULL;
2587 const char *output = NULL;
2588 for (int i = 1; i < argc; ++i) {
2589 const char *opt = argv[i];
2590 if (streq(opt, "-I")) {
2591 prepend_include_path(argv[++i]);
2593 } else if (streq(opt, "-E")) {
2595 } else if (streq(opt, "-o")) {
2598 } else if (opt[0] == '-') {
2599 fprintf(stderr, "Unknown option '%s'\n", opt);
2601 if (filename != NULL)
2602 fprintf(stderr, "Multiple inputs not supported\n");
2606 if (filename == NULL) {
2607 fprintf(stderr, "No input specified\n");
2611 if (output == NULL) {
2614 out = fopen(output, "w");
2616 fprintf(stderr, "Couldn't open output '%s'\n", output);
2621 /* just here for gcc compatibility */
2622 fprintf(out, "# 1 \"%s\"\n", filename);
2623 fprintf(out, "# 1 \"<built-in>\"\n");
2624 fprintf(out, "# 1 \"<command-line>\"\n");
2626 FILE *file = fopen(filename, "r");
2628 fprintf(stderr, "Couldn't open input '%s'\n", filename);
2631 switch_pp_input(file, filename, NULL);
2634 next_preprocessing_token();
2635 if (pp_token.kind == T_EOF)
2641 check_unclosed_conditionals();
2642 fclose(close_pp_input());
2647 exit_preprocessor();
2648 exit_symbol_table();