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;
81 typedef struct searchpath_entry_t searchpath_entry_t;
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)
142 input.input = input_from_stream(file, NULL);
145 input.output_line = 0;
146 input.position.input_name = filename;
147 input.position.lineno = 1;
149 /* indicate that we're at a new input */
150 print_line_directive(&input.position, input_stack != NULL ? "1" : NULL);
152 /* place a virtual '\n' so we realize we're at line begin */
153 input.position.lineno = 0;
157 FILE *close_pp_input(void)
159 input_free(input.input);
161 FILE* const file = input.file;
173 static void push_input(void)
175 pp_input_t *const saved_input = obstack_copy(&input_obstack, &input, sizeof(input));
177 /* adjust buffer positions */
178 if (input.bufpos != NULL)
179 saved_input->bufpos = saved_input->buf + (input.bufpos - input.buf);
180 if (input.bufend != NULL)
181 saved_input->bufend = saved_input->buf + (input.bufend - input.buf);
183 saved_input->parent = input_stack;
184 input_stack = saved_input;
188 static void pop_restore_input(void)
190 assert(n_inputs > 0);
191 assert(input_stack != NULL);
193 pp_input_t *saved_input = input_stack;
195 memcpy(&input, saved_input, sizeof(input));
198 /* adjust buffer positions */
199 if (saved_input->bufpos != NULL)
200 input.bufpos = input.buf + (saved_input->bufpos - saved_input->buf);
201 if (saved_input->bufend != NULL)
202 input.bufend = input.buf + (saved_input->bufend - saved_input->buf);
204 input_stack = saved_input->parent;
205 obstack_free(&input_obstack, saved_input);
210 * Prints a parse error message at the current token.
212 * @param msg the error message
214 static void parse_error(const char *msg)
216 errorf(&pp_token.base.source_position, "%s", msg);
219 static inline void next_real_char(void)
221 assert(input.bufpos <= input.bufend);
222 if (input.bufpos >= input.bufend) {
223 size_t const n = decode(input.input, input.buf + MAX_PUTBACK, lengthof(input.buf) - MAX_PUTBACK);
228 input.bufpos = input.buf + MAX_PUTBACK;
229 input.bufend = input.bufpos + n;
231 input.c = *input.bufpos++;
232 ++input.position.colno;
236 * Put a character back into the buffer.
238 * @param pc the character to put back
240 static inline void put_back(utf32 const pc)
242 assert(input.bufpos > input.buf);
243 *(--input.bufpos - input.buf + input.buf) = (char) pc;
244 --input.position.colno;
250 if (input.c == '\n') { \
254 ++input.position.lineno; \
255 input.position.colno = 1; \
257 newline // Let it look like an ordinary case label.
259 #define eat(c_type) (assert(input.c == c_type), next_char())
261 static void maybe_concat_lines(void)
267 info.whitespace_at_line_begin = 0;
279 * Set c to the next input character, ie.
280 * after expanding trigraphs.
282 static inline void next_char(void)
286 /* filter trigraphs and concatenated lines */
287 if (UNLIKELY(input.c == '\\')) {
288 maybe_concat_lines();
289 goto end_of_next_char;
292 if (LIKELY(input.c != '?'))
293 goto end_of_next_char;
296 if (LIKELY(input.c != '?')) {
299 goto end_of_next_char;
304 case '=': input.c = '#'; break;
305 case '(': input.c = '['; break;
306 case '/': input.c = '\\'; maybe_concat_lines(); break;
307 case ')': input.c = ']'; 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;
322 printf("nchar '%c'\n", input.c);
329 * Returns true if the given char is a octal digit.
331 * @param char the character to check
333 static inline bool is_octal_digit(int chr)
351 * Returns the value of a digit.
352 * The only portable way to do it ...
354 static int digit_value(int digit)
380 panic("wrong character given");
385 * Parses an octal character sequence.
387 * @param first_digit the already read first digit
389 static utf32 parse_octal_sequence(const utf32 first_digit)
391 assert(is_octal_digit(first_digit));
392 utf32 value = digit_value(first_digit);
393 if (!is_octal_digit(input.c)) return value;
394 value = 8 * value + digit_value(input.c);
396 if (!is_octal_digit(input.c)) return value;
397 value = 8 * value + digit_value(input.c);
404 * Parses a hex character sequence.
406 static utf32 parse_hex_sequence(void)
409 while (isxdigit(input.c)) {
410 value = 16 * value + digit_value(input.c);
416 static bool is_universal_char_valid(utf32 const v)
419 if (v < 0xA0U && v != 0x24 && v != 0x40 && v != 0x60)
421 if (0xD800 <= v && v <= 0xDFFF)
426 static utf32 parse_universal_char(unsigned const n_digits)
429 for (unsigned k = n_digits; k != 0; --k) {
430 if (isxdigit(input.c)) {
431 v = 16 * v + digit_value(input.c);
432 if (!resolve_escape_sequences)
433 obstack_1grow(&symbol_obstack, input.c);
436 errorf(&input.position,
437 "short universal character name, expected %u more digits",
442 if (!is_universal_char_valid(v)) {
443 errorf(&input.position,
444 "\\%c%0*X is not a valid universal character name",
445 n_digits == 4 ? 'u' : 'U', (int)n_digits, v);
450 static bool is_universal_char_valid_identifier(utf32 const v)
453 if ( v == 0x000A8) return true;
454 if ( v == 0x000AA) return true;
455 if ( v == 0x000AD) return true;
456 if ( v == 0x000AF) return true;
457 if (0x000B2 <= v && v <= 0x000B5) return true;
458 if (0x000B7 <= v && v <= 0x000BA) return true;
459 if (0x000BC <= v && v <= 0x000BE) return true;
460 if (0x000C0 <= v && v <= 0x000D6) return true;
461 if (0x000D8 <= v && v <= 0x000F6) return true;
462 if (0x000F8 <= v && v <= 0x000FF) return true;
463 if (0x00100 <= v && v <= 0x0167F) return true;
464 if (0x01681 <= v && v <= 0x0180D) return true;
465 if (0x0180F <= v && v <= 0x01FFF) return true;
466 if (0x0200B <= v && v <= 0x0200D) return true;
467 if (0x0202A <= v && v <= 0x0202E) return true;
468 if (0x0203F <= v && v <= 0x02040) return true;
469 if ( v == 0x02054) return true;
470 if (0x02060 <= v && v <= 0x0206F) return true;
471 if (0x02070 <= v && v <= 0x0218F) return true;
472 if (0x02460 <= v && v <= 0x024FF) return true;
473 if (0x02776 <= v && v <= 0x02793) return true;
474 if (0x02C00 <= v && v <= 0x02DFF) return true;
475 if (0x02E80 <= v && v <= 0x02FFF) return true;
476 if (0x03004 <= v && v <= 0x03007) return true;
477 if (0x03021 <= v && v <= 0x0302F) return true;
478 if (0x03031 <= v && v <= 0x0303F) return true;
479 if (0x03040 <= v && v <= 0x0D7FF) return true;
480 if (0x0F900 <= v && v <= 0x0FD3D) return true;
481 if (0x0FD40 <= v && v <= 0x0FDCF) return true;
482 if (0x0FDF0 <= v && v <= 0x0FE44) return true;
483 if (0x0FE47 <= v && v <= 0x0FFFD) return true;
484 if (0x10000 <= v && v <= 0x1FFFD) return true;
485 if (0x20000 <= v && v <= 0x2FFFD) return true;
486 if (0x30000 <= v && v <= 0x3FFFD) return true;
487 if (0x40000 <= v && v <= 0x4FFFD) return true;
488 if (0x50000 <= v && v <= 0x5FFFD) return true;
489 if (0x60000 <= v && v <= 0x6FFFD) return true;
490 if (0x70000 <= v && v <= 0x7FFFD) return true;
491 if (0x80000 <= v && v <= 0x8FFFD) return true;
492 if (0x90000 <= v && v <= 0x9FFFD) return true;
493 if (0xA0000 <= v && v <= 0xAFFFD) return true;
494 if (0xB0000 <= v && v <= 0xBFFFD) return true;
495 if (0xC0000 <= v && v <= 0xCFFFD) return true;
496 if (0xD0000 <= v && v <= 0xDFFFD) return true;
497 if (0xE0000 <= v && v <= 0xEFFFD) return true;
501 static bool is_universal_char_valid_identifier_start(utf32 const v)
504 if (0x0300 <= v && v <= 0x036F) return false;
505 if (0x1DC0 <= v && v <= 0x1DFF) return false;
506 if (0x20D0 <= v && v <= 0x20FF) return false;
507 if (0xFE20 <= v && v <= 0xFE2F) return false;
512 * Parse an escape sequence.
514 static utf32 parse_escape_sequence(void)
518 utf32 const ec = input.c;
522 case '"': return '"';
523 case '\'': return '\'';
524 case '\\': return '\\';
525 case '?': return '\?';
526 case 'a': return '\a';
527 case 'b': return '\b';
528 case 'f': return '\f';
529 case 'n': return '\n';
530 case 'r': return '\r';
531 case 't': return '\t';
532 case 'v': return '\v';
534 return parse_hex_sequence();
543 return parse_octal_sequence(ec);
545 parse_error("reached end of file while parsing escape sequence");
547 /* \E is not documented, but handled, by GCC. It is acceptable according
548 * to §6.11.4, whereas \e is not. */
552 return 27; /* hopefully 27 is ALWAYS the code for ESCAPE */
555 case 'U': return parse_universal_char(8);
556 case 'u': return parse_universal_char(4);
561 /* §6.4.4.4:8 footnote 64 */
562 parse_error("unknown escape sequence");
566 static const char *identify_string(char *string)
568 const char *result = strset_insert(&stringset, string);
569 if (result != string) {
570 obstack_free(&symbol_obstack, string);
575 static string_t sym_make_string(string_encoding_t const enc)
577 obstack_1grow(&symbol_obstack, '\0');
578 size_t const len = obstack_object_size(&symbol_obstack) - 1;
579 char *const string = obstack_finish(&symbol_obstack);
580 char const *const result = identify_string(string);
581 return (string_t){ result, len, enc };
584 string_t make_string(char const *const string)
586 obstack_grow(&symbol_obstack, string, strlen(string));
587 return sym_make_string(STRING_ENCODING_CHAR);
590 static void parse_string(utf32 const delimiter, token_kind_t const kind,
591 string_encoding_t const enc,
592 char const *const context)
594 const unsigned start_linenr = input.position.lineno;
601 if (resolve_escape_sequences) {
602 utf32 const tc = parse_escape_sequence();
603 if (enc == STRING_ENCODING_CHAR) {
605 warningf(WARN_OTHER, &pp_token.base.source_position, "escape sequence out of range");
607 obstack_1grow(&symbol_obstack, tc);
609 obstack_grow_utf8(&symbol_obstack, tc);
612 obstack_1grow(&symbol_obstack, (char)input.c);
614 obstack_1grow(&symbol_obstack, (char)input.c);
621 errorf(&pp_token.base.source_position, "newline while parsing %s", context);
625 source_position_t source_position;
626 source_position.input_name = pp_token.base.source_position.input_name;
627 source_position.lineno = start_linenr;
628 errorf(&source_position, "EOF while parsing %s", context);
633 if (input.c == delimiter) {
637 obstack_grow_utf8(&symbol_obstack, input.c);
645 pp_token.kind = kind;
646 pp_token.literal.string = sym_make_string(enc);
649 static void parse_string_literal(string_encoding_t const enc)
651 parse_string('"', T_STRING_LITERAL, enc, "string literal");
654 static void parse_character_constant(string_encoding_t const enc)
656 parse_string('\'', T_CHARACTER_CONSTANT, enc, "character constant");
657 if (pp_token.literal.string.size == 0) {
658 parse_error("empty character constant");
662 #define SYMBOL_CASES_WITHOUT_E_P \
663 '$': if (!allow_dollar_in_symbol) goto dollar_sign; \
714 #define SYMBOL_CASES \
715 SYMBOL_CASES_WITHOUT_E_P: \
721 #define DIGIT_CASES \
733 static void start_expanding(pp_definition_t *definition)
735 definition->parent_expansion = current_expansion;
736 definition->expand_pos = 0;
737 definition->is_expanding = true;
738 if (definition->list_len > 0) {
739 definition->token_list[0].had_whitespace
740 = info.had_whitespace;
742 current_expansion = definition;
745 static void finished_expanding(pp_definition_t *definition)
747 assert(definition->is_expanding);
748 pp_definition_t *parent = definition->parent_expansion;
749 definition->parent_expansion = NULL;
750 definition->is_expanding = false;
752 /* stop further expanding once we expanded a parameter used in a
754 if (definition == argument_expanding)
755 argument_expanding = NULL;
757 assert(current_expansion == definition);
758 current_expansion = parent;
761 static inline void set_punctuator(token_kind_t const kind)
763 pp_token.kind = kind;
764 pp_token.base.symbol = token_symbols[kind];
767 static inline void set_digraph(token_kind_t const kind, symbol_t *const symbol)
769 pp_token.kind = kind;
770 pp_token.base.symbol = symbol;
774 * returns next final token from a preprocessor macro expansion
776 static bool expand_next(void)
778 if (current_expansion == NULL)
782 size_t pos = current_expansion->expand_pos;
783 if (pos >= current_expansion->list_len) {
784 finished_expanding(current_expansion);
785 /* it was the outermost expansion, parse pptoken normally */
786 if (current_expansion == NULL) {
791 const saved_token_t *saved = ¤t_expansion->token_list[pos++];
792 pp_token = saved->token;
794 if (current_expansion->expand_pos > 0)
795 info.had_whitespace = saved->had_whitespace;
796 pp_token.base.source_position = expansion_pos;
797 ++current_expansion->expand_pos;
803 * Returns the next token kind found when continuing the current expansions
804 * without starting new sub-expansions.
806 static token_kind_t peek_expansion(void)
808 pp_definition_t *expansion = current_expansion;
809 while (expansion != NULL && expansion->expand_pos >= expansion->list_len) {
810 expansion = expansion->parent_expansion;
812 if (expansion == NULL)
814 return expansion->token_list[expansion->expand_pos].token.kind;
817 static void skip_line_comment(void)
819 info.had_whitespace = true;
836 static void skip_multiline_comment(void)
838 info.had_whitespace = true;
840 unsigned start_linenr = input.position.lineno;
845 if (input.c == '*') {
846 /* TODO: nested comment, warn here */
851 if (input.c == '/') {
852 if (input.position.lineno != input.output_line)
853 info.whitespace_at_line_begin = input.position.colno;
863 source_position_t source_position;
864 source_position.input_name = pp_token.base.source_position.input_name;
865 source_position.lineno = start_linenr;
866 errorf(&source_position, "at end of file while looking for comment end");
877 static bool skip_till_newline(bool stop_at_non_whitespace)
889 if (input.c == '/') {
893 } else if (input.c == '*') {
895 skip_multiline_comment();
907 if (stop_at_non_whitespace)
916 static void skip_whitespace(void)
922 ++info.whitespace_at_line_begin;
923 info.had_whitespace = true;
928 info.at_line_begin = true;
929 info.had_whitespace = true;
930 info.whitespace_at_line_begin = 0;
935 if (input.c == '/') {
939 } else if (input.c == '*') {
941 skip_multiline_comment();
955 static inline void eat_pp(pp_token_kind_t const kind)
957 assert(pp_token.base.symbol->pp_ID == kind);
962 static inline void eat_token(token_kind_t const kind)
964 assert(pp_token.kind == kind);
969 static void parse_symbol(void)
971 assert(obstack_object_size(&symbol_obstack) == 0);
976 obstack_1grow(&symbol_obstack, (char) input.c);
985 case 'U': n = 8; goto universal;
986 case 'u': n = 4; goto universal;
988 if (!resolve_escape_sequences) {
989 obstack_1grow(&symbol_obstack, '\\');
990 obstack_1grow(&symbol_obstack, input.c);
993 utf32 const v = parse_universal_char(n);
994 if (!is_universal_char_valid_identifier(v)) {
995 if (is_universal_char_valid(v)) {
996 errorf(&input.position,
997 "universal character \\%c%0*X is not valid in an identifier",
998 n == 4 ? 'u' : 'U', (int)n, v);
1000 } else if (obstack_object_size(&symbol_obstack) == 0 && !is_universal_char_valid_identifier_start(v)) {
1001 errorf(&input.position,
1002 "universal character \\%c%0*X is not valid as start of an identifier",
1003 n == 4 ? 'u' : 'U', (int)n, v);
1004 } else if (resolve_escape_sequences) {
1005 obstack_grow_utf8(&symbol_obstack, v);
1023 obstack_1grow(&symbol_obstack, '\0');
1024 char *string = obstack_finish(&symbol_obstack);
1026 /* might be a wide string or character constant ( L"string"/L'c' ) */
1027 if (input.c == '"' && string[0] == 'L' && string[1] == '\0') {
1028 obstack_free(&symbol_obstack, string);
1029 parse_string_literal(STRING_ENCODING_WIDE);
1031 } else if (input.c == '\'' && string[0] == 'L' && string[1] == '\0') {
1032 obstack_free(&symbol_obstack, string);
1033 parse_character_constant(STRING_ENCODING_WIDE);
1037 symbol_t *symbol = symbol_table_insert(string);
1039 pp_token.kind = symbol->ID;
1040 pp_token.base.symbol = symbol;
1042 /* we can free the memory from symbol obstack if we already had an entry in
1043 * the symbol table */
1044 if (symbol->string != string) {
1045 obstack_free(&symbol_obstack, string);
1049 static void parse_number(void)
1051 obstack_1grow(&symbol_obstack, (char) input.c);
1058 case SYMBOL_CASES_WITHOUT_E_P:
1059 obstack_1grow(&symbol_obstack, (char) input.c);
1067 obstack_1grow(&symbol_obstack, (char) input.c);
1069 if (input.c == '+' || input.c == '-') {
1070 obstack_1grow(&symbol_obstack, (char) input.c);
1082 pp_token.kind = T_NUMBER;
1083 pp_token.literal.string = sym_make_string(STRING_ENCODING_CHAR);
1086 #define MAYBE_PROLOG \
1090 #define MAYBE(ch, kind) \
1093 set_punctuator(kind); \
1096 #define MAYBE_DIGRAPH(ch, kind, symbol) \
1099 set_digraph(kind, symbol); \
1102 #define ELSE_CODE(code) \
1107 #define ELSE(kind) ELSE_CODE(set_punctuator(kind); return;)
1109 /** identifies and returns the next preprocessing token contained in the
1110 * input stream. No macro expansion is performed. */
1111 static void next_input_token(void)
1113 if (next_info.had_whitespace) {
1115 next_info.had_whitespace = false;
1117 info.at_line_begin = false;
1118 info.had_whitespace = false;
1121 pp_token.base.source_position = input.position;
1122 pp_token.base.symbol = NULL;
1127 info.whitespace_at_line_begin++;
1128 info.had_whitespace = true;
1133 info.at_line_begin = true;
1134 info.had_whitespace = true;
1135 info.whitespace_at_line_begin = 0;
1147 parse_string_literal(STRING_ENCODING_CHAR);
1151 parse_character_constant(STRING_ENCODING_CHAR);
1173 MAYBE('.', T_DOTDOTDOT)
1177 set_punctuator('.');
1183 MAYBE('&', T_ANDAND)
1184 MAYBE('=', T_ANDEQUAL)
1188 MAYBE('=', T_ASTERISKEQUAL)
1192 MAYBE('+', T_PLUSPLUS)
1193 MAYBE('=', T_PLUSEQUAL)
1197 MAYBE('>', T_MINUSGREATER)
1198 MAYBE('-', T_MINUSMINUS)
1199 MAYBE('=', T_MINUSEQUAL)
1203 MAYBE('=', T_EXCLAMATIONMARKEQUAL)
1207 MAYBE('=', T_SLASHEQUAL)
1210 skip_multiline_comment();
1214 skip_line_comment();
1219 MAYBE_DIGRAPH('>', '}', symbol_percentgreater)
1220 MAYBE('=', T_PERCENTEQUAL)
1225 MAYBE_DIGRAPH(':', T_HASHHASH, symbol_percentcolonpercentcolon)
1229 goto digraph_percentcolon;
1232 digraph_percentcolon:
1233 set_digraph('#', symbol_percentcolon);
1239 MAYBE_DIGRAPH(':', '[', symbol_lesscolon)
1240 MAYBE_DIGRAPH('%', '{', symbol_lesspercent)
1241 MAYBE('=', T_LESSEQUAL)
1244 MAYBE('=', T_LESSLESSEQUAL)
1249 MAYBE('=', T_GREATEREQUAL)
1252 MAYBE('=', T_GREATERGREATEREQUAL)
1253 ELSE(T_GREATERGREATER)
1257 MAYBE('=', T_CARETEQUAL)
1261 MAYBE('=', T_PIPEEQUAL)
1262 MAYBE('|', T_PIPEPIPE)
1266 MAYBE_DIGRAPH('>', ']', symbol_colongreater)
1268 if (c_mode & _CXX) {
1270 set_punctuator(T_COLONCOLON);
1277 MAYBE('=', T_EQUALEQUAL)
1281 MAYBE('#', T_HASHHASH)
1294 set_punctuator(input.c);
1299 if (input_stack != NULL) {
1300 fclose(close_pp_input());
1301 pop_restore_input();
1303 if (input.c == (utf32)EOF)
1304 --input.position.lineno;
1305 print_line_directive(&input.position, "2");
1308 info.at_line_begin = true;
1309 set_punctuator(T_EOF);
1315 int next_c = input.c;
1318 if (next_c == 'U' || next_c == 'u') {
1325 if (error_on_unknown_chars) {
1326 errorf(&pp_token.base.source_position,
1327 "unknown character '%lc' found\n", input.c);
1331 assert(obstack_object_size(&symbol_obstack) == 0);
1332 obstack_grow_utf8(&symbol_obstack, input.c);
1333 obstack_1grow(&symbol_obstack, '\0');
1334 char *const string = obstack_finish(&symbol_obstack);
1335 symbol_t *const symbol = symbol_table_insert(string);
1336 if (symbol->string != string)
1337 obstack_free(&symbol_obstack, string);
1339 pp_token.kind = T_UNKNOWN_CHAR;
1340 pp_token.base.symbol = symbol;
1347 static void print_quoted_string(const char *const string)
1350 for (const char *c = string; *c != 0; ++c) {
1352 case '"': fputs("\\\"", out); break;
1353 case '\\': fputs("\\\\", out); break;
1354 case '\a': fputs("\\a", out); break;
1355 case '\b': fputs("\\b", out); break;
1356 case '\f': fputs("\\f", out); break;
1357 case '\n': fputs("\\n", out); break;
1358 case '\r': fputs("\\r", out); break;
1359 case '\t': fputs("\\t", out); break;
1360 case '\v': fputs("\\v", out); break;
1361 case '\?': fputs("\\?", out); break;
1364 fprintf(out, "\\%03o", (unsigned)*c);
1374 static void print_line_directive(const source_position_t *pos, const char *add)
1379 fprintf(out, "# %u ", pos->lineno);
1380 print_quoted_string(pos->input_name);
1386 printed_input_name = pos->input_name;
1387 input.output_line = pos->lineno-1;
1390 static bool emit_newlines(void)
1392 unsigned delta = pp_token.base.source_position.lineno - input.output_line;
1398 print_line_directive(&pp_token.base.source_position, NULL);
1401 for (unsigned i = 0; i < delta; ++i) {
1405 input.output_line = pp_token.base.source_position.lineno;
1407 for (unsigned i = 0; i < info.whitespace_at_line_begin; ++i)
1413 void set_preprocessor_output(FILE *output)
1417 error_on_unknown_chars = false;
1418 resolve_escape_sequences = false;
1420 error_on_unknown_chars = true;
1421 resolve_escape_sequences = true;
1425 void emit_pp_token(void)
1427 if (!emit_newlines() &&
1428 (info.had_whitespace || tokens_would_paste(last_token, pp_token.kind)))
1431 switch (pp_token.kind) {
1433 fputs(pp_token.literal.string.begin, out);
1436 case T_STRING_LITERAL:
1437 fputs(get_string_encoding_prefix(pp_token.literal.string.encoding), out);
1439 fputs(pp_token.literal.string.begin, out);
1443 case T_CHARACTER_CONSTANT:
1444 fputs(get_string_encoding_prefix(pp_token.literal.string.encoding), out);
1446 fputs(pp_token.literal.string.begin, out);
1450 case T_MACRO_PARAMETER:
1451 panic("macro parameter not expanded");
1454 fputs(pp_token.base.symbol->string, out);
1457 last_token = pp_token.kind;
1460 static void eat_pp_directive(void)
1462 while (!info.at_line_begin) {
1467 static bool strings_equal(const string_t *string1, const string_t *string2)
1469 size_t size = string1->size;
1470 if (size != string2->size)
1473 const char *c1 = string1->begin;
1474 const char *c2 = string2->begin;
1475 for (size_t i = 0; i < size; ++i, ++c1, ++c2) {
1482 static bool pp_tokens_equal(const token_t *token1, const token_t *token2)
1484 if (token1->kind != token2->kind)
1487 switch (token1->kind) {
1489 case T_CHARACTER_CONSTANT:
1490 case T_STRING_LITERAL:
1491 return strings_equal(&token1->literal.string, &token2->literal.string);
1493 case T_MACRO_PARAMETER:
1494 return token1->macro_parameter.def->symbol
1495 == token2->macro_parameter.def->symbol;
1498 return token1->base.symbol == token2->base.symbol;
1502 static bool pp_definitions_equal(const pp_definition_t *definition1,
1503 const pp_definition_t *definition2)
1505 if (definition1->list_len != definition2->list_len)
1508 size_t len = definition1->list_len;
1509 const saved_token_t *t1 = definition1->token_list;
1510 const saved_token_t *t2 = definition2->token_list;
1511 for (size_t i = 0; i < len; ++i, ++t1, ++t2) {
1512 if (!pp_tokens_equal(&t1->token, &t2->token))
1518 static void parse_define_directive(void)
1526 assert(obstack_object_size(&pp_obstack) == 0);
1528 if (pp_token.kind != T_IDENTIFIER || info.at_line_begin) {
1529 errorf(&pp_token.base.source_position,
1530 "expected identifier after #define, got %K", &pp_token);
1533 symbol_t *const symbol = pp_token.base.symbol;
1535 pp_definition_t *new_definition
1536 = obstack_alloc(&pp_obstack, sizeof(new_definition[0]));
1537 memset(new_definition, 0, sizeof(new_definition[0]));
1538 new_definition->symbol = symbol;
1539 new_definition->source_position = input.position;
1541 /* this is probably the only place where spaces are significant in the
1542 * lexer (except for the fact that they separate tokens). #define b(x)
1543 * is something else than #define b (x) */
1544 if (input.c == '(') {
1545 eat_token(T_IDENTIFIER);
1549 switch (pp_token.kind) {
1551 new_definition->is_variadic = true;
1552 eat_token(T_DOTDOTDOT);
1553 if (pp_token.kind != ')') {
1554 errorf(&input.position,
1555 "'...' not at end of macro argument list");
1560 case T_IDENTIFIER: {
1561 pp_definition_t parameter;
1562 memset(¶meter, 0, sizeof(parameter));
1563 parameter.source_position = pp_token.base.source_position;
1564 parameter.symbol = pp_token.base.symbol;
1565 parameter.is_parameter = true;
1566 obstack_grow(&pp_obstack, ¶meter, sizeof(parameter));
1567 eat_token(T_IDENTIFIER);
1569 if (pp_token.kind == ',') {
1574 if (pp_token.kind != ')') {
1575 errorf(&pp_token.base.source_position,
1576 "expected ',' or ')' after identifier, got %K",
1585 goto finish_argument_list;
1588 errorf(&pp_token.base.source_position,
1589 "expected identifier, '...' or ')' in #define argument list, got %K",
1595 finish_argument_list:
1596 new_definition->has_parameters = true;
1597 size_t size = obstack_object_size(&pp_obstack);
1598 new_definition->n_parameters
1599 = size / sizeof(new_definition->parameters[0]);
1600 new_definition->parameters = obstack_finish(&pp_obstack);
1601 for (size_t i = 0; i < new_definition->n_parameters; ++i) {
1602 pp_definition_t *param = &new_definition->parameters[i];
1603 symbol_t *symbol = param->symbol;
1604 pp_definition_t *previous = symbol->pp_definition;
1605 if (previous != NULL
1606 && previous->function_definition == new_definition) {
1607 errorf(¶m->source_position,
1608 "duplicate macro parameter '%Y'", symbol);
1609 param->symbol = sym_anonymous;
1612 param->parent_expansion = previous;
1613 param->function_definition = new_definition;
1614 symbol->pp_definition = param;
1617 eat_token(T_IDENTIFIER);
1620 /* construct token list */
1621 assert(obstack_object_size(&pp_obstack) == 0);
1622 while (!info.at_line_begin) {
1623 if (pp_token.kind == T_IDENTIFIER) {
1624 const symbol_t *symbol = pp_token.base.symbol;
1625 pp_definition_t *definition = symbol->pp_definition;
1626 if (definition != NULL
1627 && definition->function_definition == new_definition) {
1628 pp_token.kind = T_MACRO_PARAMETER;
1629 pp_token.macro_parameter.def = definition;
1632 saved_token_t saved_token;
1633 saved_token.token = pp_token;
1634 saved_token.had_whitespace = info.had_whitespace;
1635 obstack_grow(&pp_obstack, &saved_token, sizeof(saved_token));
1639 new_definition->list_len = obstack_object_size(&pp_obstack)
1640 / sizeof(new_definition->token_list[0]);
1641 new_definition->token_list = obstack_finish(&pp_obstack);
1643 if (new_definition->has_parameters) {
1644 for (size_t i = 0; i < new_definition->n_parameters; ++i) {
1645 pp_definition_t *param = &new_definition->parameters[i];
1646 symbol_t *symbol = param->symbol;
1647 if (symbol == sym_anonymous)
1649 assert(symbol->pp_definition == param);
1650 assert(param->function_definition == new_definition);
1651 symbol->pp_definition = param->parent_expansion;
1652 param->parent_expansion = NULL;
1656 pp_definition_t *old_definition = symbol->pp_definition;
1657 if (old_definition != NULL) {
1658 if (!pp_definitions_equal(old_definition, new_definition)) {
1659 warningf(WARN_OTHER, &input.position, "multiple definition of macro '%Y' (first defined %P)", symbol, &old_definition->source_position);
1661 /* reuse the old definition */
1662 obstack_free(&pp_obstack, new_definition);
1663 new_definition = old_definition;
1667 symbol->pp_definition = new_definition;
1671 if (obstack_object_size(&pp_obstack) > 0) {
1672 char *ptr = obstack_finish(&pp_obstack);
1673 obstack_free(&pp_obstack, ptr);
1678 static void parse_undef_directive(void)
1686 if (pp_token.kind != T_IDENTIFIER) {
1687 errorf(&input.position,
1688 "expected identifier after #undef, got %K", &pp_token);
1693 pp_token.base.symbol->pp_definition = NULL;
1694 eat_token(T_IDENTIFIER);
1696 if (!info.at_line_begin) {
1697 warningf(WARN_OTHER, &input.position, "extra tokens at end of #undef directive");
1702 /** behind an #include we can have the special headername lexems.
1703 * They're only allowed behind an #include so they're not recognized
1704 * by the normal next_preprocessing_token. We handle them as a special
1706 static void parse_headername(void)
1708 const source_position_t start_position = input.position;
1709 string_t string = { NULL, 0, STRING_ENCODING_CHAR };
1710 assert(obstack_object_size(&symbol_obstack) == 0);
1712 if (info.at_line_begin) {
1713 parse_error("expected headername after #include");
1717 /* check wether we have a "... or <... headername */
1721 case '<': delimiter = '>'; goto parse_name;
1722 case '"': delimiter = '"'; goto parse_name;
1729 errorf(&pp_token.base.source_position, "header name without closing '%c'", (char)delimiter);
1733 if (input.c == delimiter) {
1735 goto finished_headername;
1737 obstack_1grow(&symbol_obstack, (char)input.c);
1743 /* we should never be here */
1747 /* TODO: do normal pp_token parsing and concatenate results */
1748 panic("pp_token concat include not implemented yet");
1751 finished_headername:
1752 string = sym_make_string(STRING_ENCODING_CHAR);
1755 pp_token.base.source_position = start_position;
1756 pp_token.kind = T_HEADERNAME;
1757 pp_token.literal.string = string;
1760 static bool do_include(bool system_include, char const *const headername)
1762 size_t headername_len = strlen(headername);
1763 if (!system_include) {
1764 /* put dirname of current input on obstack */
1765 const char *filename = input.position.input_name;
1766 const char *last_slash = strrchr(filename, '/');
1767 const char *full_name;
1768 if (last_slash != NULL) {
1769 size_t len = last_slash - filename;
1770 obstack_grow(&symbol_obstack, filename, len + 1);
1771 obstack_grow0(&symbol_obstack, headername, headername_len);
1772 char *complete_path = obstack_finish(&symbol_obstack);
1773 full_name = identify_string(complete_path);
1775 full_name = headername;
1778 FILE *file = fopen(full_name, "r");
1780 switch_pp_input(file, full_name);
1785 assert(obstack_object_size(&symbol_obstack) == 0);
1786 /* check searchpath */
1787 for (searchpath_entry_t *entry = searchpath; entry != NULL;
1788 entry = entry->next) {
1789 const char *path = entry->path;
1790 size_t len = strlen(path);
1791 obstack_grow(&symbol_obstack, path, len);
1792 if (path[len-1] != '/')
1793 obstack_1grow(&symbol_obstack, '/');
1794 obstack_grow(&symbol_obstack, headername, headername_len+1);
1796 char *complete_path = obstack_finish(&symbol_obstack);
1797 FILE *file = fopen(complete_path, "r");
1799 const char *filename = identify_string(complete_path);
1800 switch_pp_input(file, filename);
1803 obstack_free(&symbol_obstack, complete_path);
1810 static void parse_include_directive(void)
1817 /* don't eat the TP_include here!
1818 * we need an alternative parsing for the next token */
1819 skip_till_newline(true);
1820 bool system_include = input.c == '<';
1822 string_t headername = pp_token.literal.string;
1823 if (headername.begin == NULL) {
1828 bool had_nonwhitespace = skip_till_newline(false);
1829 if (had_nonwhitespace) {
1830 warningf(WARN_OTHER, &pp_token.base.source_position,
1831 "extra tokens at end of #include directive");
1834 if (n_inputs > INCLUDE_LIMIT) {
1835 errorf(&pp_token.base.source_position, "#include nested too deeply");
1842 info.whitespace_at_line_begin = 0;
1843 info.had_whitespace = false;
1844 info.at_line_begin = true;
1847 bool res = do_include(system_include, pp_token.literal.string.begin);
1851 errorf(&pp_token.base.source_position, "failed including '%S': %s", &pp_token.literal.string, strerror(errno));
1852 pop_restore_input();
1856 static pp_conditional_t *push_conditional(void)
1858 pp_conditional_t *conditional
1859 = obstack_alloc(&pp_obstack, sizeof(*conditional));
1860 memset(conditional, 0, sizeof(*conditional));
1862 conditional->parent = conditional_stack;
1863 conditional_stack = conditional;
1868 static void pop_conditional(void)
1870 assert(conditional_stack != NULL);
1871 conditional_stack = conditional_stack->parent;
1874 void check_unclosed_conditionals(void)
1876 while (conditional_stack != NULL) {
1877 pp_conditional_t *conditional = conditional_stack;
1879 if (conditional->in_else) {
1880 errorf(&conditional->source_position, "unterminated #else");
1882 errorf(&conditional->source_position, "unterminated condition");
1888 static void parse_ifdef_ifndef_directive(bool const is_ifdef)
1891 eat_pp(is_ifdef ? TP_ifdef : TP_ifndef);
1895 pp_conditional_t *conditional = push_conditional();
1896 conditional->source_position = pp_token.base.source_position;
1897 conditional->skip = true;
1901 if (pp_token.kind != T_IDENTIFIER || info.at_line_begin) {
1902 errorf(&pp_token.base.source_position,
1903 "expected identifier after #%s, got %K",
1904 is_ifdef ? "ifdef" : "ifndef", &pp_token);
1907 /* just take the true case in the hope to avoid further errors */
1910 /* evaluate wether we are in true or false case */
1911 condition = (bool)pp_token.base.symbol->pp_definition == is_ifdef;
1912 eat_token(T_IDENTIFIER);
1914 if (!info.at_line_begin) {
1915 errorf(&pp_token.base.source_position,
1916 "extra tokens at end of #%s",
1917 is_ifdef ? "ifdef" : "ifndef");
1922 pp_conditional_t *conditional = push_conditional();
1923 conditional->source_position = pp_token.base.source_position;
1924 conditional->condition = condition;
1931 static void parse_else_directive(void)
1935 if (!info.at_line_begin) {
1937 warningf(WARN_OTHER, &pp_token.base.source_position, "extra tokens at end of #else");
1942 pp_conditional_t *conditional = conditional_stack;
1943 if (conditional == NULL) {
1944 errorf(&pp_token.base.source_position, "#else without prior #if");
1948 if (conditional->in_else) {
1949 errorf(&pp_token.base.source_position,
1950 "#else after #else (condition started %P)",
1951 &conditional->source_position);
1956 conditional->in_else = true;
1957 if (!conditional->skip) {
1958 skip_mode = conditional->condition;
1960 conditional->source_position = pp_token.base.source_position;
1963 static void parse_endif_directive(void)
1967 if (!info.at_line_begin) {
1969 warningf(WARN_OTHER, &pp_token.base.source_position, "extra tokens at end of #endif");
1974 pp_conditional_t *conditional = conditional_stack;
1975 if (conditional == NULL) {
1976 errorf(&pp_token.base.source_position, "#endif without prior #if");
1980 if (!conditional->skip) {
1986 typedef enum stdc_pragma_kind_t {
1990 STDC_CX_LIMITED_RANGE
1991 } stdc_pragma_kind_t;
1993 typedef enum stdc_pragma_value_kind_t {
1998 } stdc_pragma_value_kind_t;
2000 static void parse_pragma_directive(void)
2004 if (pp_token.kind != T_IDENTIFIER) {
2005 warningf(WARN_UNKNOWN_PRAGMAS, &pp_token.base.source_position,
2006 "expected identifier after #pragma");
2011 stdc_pragma_kind_t kind = STDC_UNKNOWN;
2012 if (pp_token.base.symbol->pp_ID == TP_STDC && c_mode & _C99) {
2016 switch (pp_token.base.symbol->pp_ID) {
2017 case TP_FP_CONTRACT: kind = STDC_FP_CONTRACT; break;
2018 case TP_FENV_ACCESS: kind = STDC_FENV_ACCESS; break;
2019 case TP_CX_LIMITED_RANGE: kind = STDC_CX_LIMITED_RANGE; break;
2022 if (kind != STDC_UNKNOWN) {
2024 stdc_pragma_value_kind_t value;
2025 switch (pp_token.base.symbol->pp_ID) {
2026 case TP_ON: value = STDC_VALUE_ON; break;
2027 case TP_OFF: value = STDC_VALUE_OFF; break;
2028 case TP_DEFAULT: value = STDC_VALUE_DEFAULT; break;
2029 default: value = STDC_VALUE_UNKNOWN; break;
2031 if (value == STDC_VALUE_UNKNOWN) {
2032 kind = STDC_UNKNOWN;
2033 errorf(&pp_token.base.source_position, "bad STDC pragma argument");
2038 if (kind == STDC_UNKNOWN) {
2039 warningf(WARN_UNKNOWN_PRAGMAS, &pp_token.base.source_position,
2040 "encountered unknown #pragma");
2044 static void parse_line_directive(void)
2046 if (pp_token.kind != T_NUMBER) {
2048 parse_error("expected integer");
2051 long const line = strtol(pp_token.literal.string.begin, &end, 0);
2053 /* use offset -1 as this is about the next line */
2054 input.position.lineno = line - 1;
2055 /* force output of line */
2056 input.output_line = input.position.lineno - 20;
2059 errorf(&input.position, "'%S' is not a valid line number",
2060 &pp_token.literal.string);
2064 if (info.at_line_begin)
2067 if (pp_token.kind == T_STRING_LITERAL
2068 && pp_token.literal.string.encoding == STRING_ENCODING_CHAR) {
2069 input.position.input_name = pp_token.literal.string.begin;
2070 input.position.is_system_header = false;
2073 /* attempt to parse numeric flags as outputted by gcc preprocessor */
2074 while (!info.at_line_begin && pp_token.kind == T_NUMBER) {
2076 * 1 - indicates start of a new file
2077 * 2 - indicates return from a file
2078 * 3 - indicates system header
2079 * 4 - indicates implicit extern "C" in C++ mode
2081 * currently we're only interested in "3"
2083 if (streq(pp_token.literal.string.begin, "3")) {
2084 input.position.is_system_header = true;
2093 static void parse_error_directive(void)
2100 bool const old_resolve_escape_sequences = resolve_escape_sequences;
2101 resolve_escape_sequences = false;
2103 source_position_t const pos = pp_token.base.source_position;
2105 if (info.had_whitespace && obstack_object_size(&pp_obstack) != 0)
2106 obstack_1grow(&pp_obstack, ' ');
2108 switch (pp_token.kind) {
2110 string_t const *const str = &pp_token.literal.string;
2111 obstack_grow(&pp_obstack, str->begin, str->size);
2117 case T_STRING_LITERAL: delim = '"'; goto string;
2118 case T_CHARACTER_CONSTANT: delim = '\''; goto string;
2120 string_t const *const str = &pp_token.literal.string;
2121 char const *const enc = get_string_encoding_prefix(str->encoding);
2122 obstack_printf(&pp_obstack, "%s%c%s%c", enc, delim, str->begin, delim);
2127 char const *const str = pp_token.base.symbol->string;
2128 obstack_grow(&pp_obstack, str, strlen(str));
2134 } while (!info.at_line_begin);
2136 resolve_escape_sequences = old_resolve_escape_sequences;
2138 obstack_1grow(&pp_obstack, '\0');
2139 char *const str = obstack_finish(&pp_obstack);
2140 errorf(&pos, "#%s", str);
2141 obstack_free(&pp_obstack, str);
2144 static void parse_preprocessing_directive(void)
2148 if (info.at_line_begin) {
2149 /* empty directive */
2153 if (pp_token.base.symbol) {
2154 switch (pp_token.base.symbol->pp_ID) {
2155 case TP_define: parse_define_directive(); break;
2156 case TP_else: parse_else_directive(); break;
2157 case TP_endif: parse_endif_directive(); break;
2158 case TP_error: parse_error_directive(); break;
2159 case TP_ifdef: parse_ifdef_ifndef_directive(true); break;
2160 case TP_ifndef: parse_ifdef_ifndef_directive(false); break;
2161 case TP_include: parse_include_directive(); break;
2162 case TP_line: next_input_token(); goto line_directive;
2163 case TP_pragma: parse_pragma_directive(); break;
2164 case TP_undef: parse_undef_directive(); break;
2167 } else if (pp_token.kind == T_NUMBER) {
2169 parse_line_directive();
2173 errorf(&pp_token.base.source_position, "invalid preprocessing directive #%K", &pp_token);
2178 assert(info.at_line_begin);
2181 static void finish_current_argument(void)
2183 if (current_argument == NULL)
2185 size_t size = obstack_object_size(&pp_obstack);
2186 current_argument->list_len = size/sizeof(current_argument->token_list[0]);
2187 current_argument->token_list = obstack_finish(&pp_obstack);
2190 void next_preprocessing_token(void)
2193 if (!expand_next()) {
2196 while (pp_token.kind == '#' && info.at_line_begin) {
2197 parse_preprocessing_directive();
2199 } while (skip_mode && pp_token.kind != T_EOF);
2202 const token_kind_t kind = pp_token.kind;
2203 if (current_call == NULL || argument_expanding != NULL) {
2204 if (kind == T_IDENTIFIER) {
2205 symbol_t *const symbol = pp_token.base.symbol;
2206 pp_definition_t *const pp_definition = symbol->pp_definition;
2207 if (pp_definition != NULL && !pp_definition->is_expanding) {
2208 if (pp_definition->has_parameters) {
2210 /* check if next token is a '(' */
2211 whitespace_info_t old_info = info;
2212 token_kind_t next_token = peek_expansion();
2213 if (next_token == T_EOF) {
2214 info.at_line_begin = false;
2215 info.had_whitespace = false;
2217 if (input.c == '(') {
2222 if (next_token == '(') {
2223 if (current_expansion == NULL)
2224 expansion_pos = pp_token.base.source_position;
2225 next_preprocessing_token();
2226 assert(pp_token.kind == '(');
2228 pp_definition->parent_expansion = current_expansion;
2229 current_call = pp_definition;
2230 current_call->expand_pos = 0;
2231 current_call->expand_info = old_info;
2232 if (current_call->n_parameters > 0) {
2233 current_argument = ¤t_call->parameters[0];
2234 assert(argument_brace_count == 0);
2238 /* skip_whitespaces() skipped newlines and whitespace,
2239 * remember results for next token */
2245 if (current_expansion == NULL)
2246 expansion_pos = pp_token.base.source_position;
2247 start_expanding(pp_definition);
2251 } else if (kind == T_MACRO_PARAMETER) {
2252 assert(current_expansion != NULL);
2253 start_expanding(pp_token.macro_parameter.def);
2258 if (current_call != NULL) {
2259 /* current_call != NULL */
2261 ++argument_brace_count;
2262 } else if (kind == ')') {
2263 if (argument_brace_count > 0) {
2264 --argument_brace_count;
2266 finish_current_argument();
2267 assert(kind == ')');
2268 start_expanding(current_call);
2269 info = current_call->expand_info;
2270 current_call = NULL;
2271 current_argument = NULL;
2274 } else if (kind == ',' && argument_brace_count == 0) {
2275 finish_current_argument();
2276 current_call->expand_pos++;
2277 if (current_call->expand_pos >= current_call->n_parameters) {
2278 errorf(&pp_token.base.source_position,
2279 "too many arguments passed for macro '%Y'",
2280 current_call->symbol);
2281 current_argument = NULL;
2284 = ¤t_call->parameters[current_call->expand_pos];
2287 } else if (kind == T_MACRO_PARAMETER) {
2288 /* parameters have to be fully expanded before being used as
2289 * parameters for another macro-call */
2290 assert(current_expansion != NULL);
2291 pp_definition_t *argument = pp_token.macro_parameter.def;
2292 argument_expanding = argument;
2293 start_expanding(argument);
2295 } else if (kind == T_EOF) {
2296 errorf(&expansion_pos,
2297 "reached end of file while parsing arguments for '%Y'",
2298 current_call->symbol);
2301 if (current_argument != NULL) {
2302 saved_token_t saved;
2303 saved.token = pp_token;
2304 saved.had_whitespace = info.had_whitespace;
2305 obstack_grow(&pp_obstack, &saved, sizeof(saved));
2312 static void prepend_include_path(const char *path)
2314 searchpath_entry_t *entry = OALLOCZ(&config_obstack, searchpath_entry_t);
2316 entry->next = searchpath;
2320 static void setup_include_path(void)
2322 /* built-in paths */
2323 prepend_include_path("/usr/include");
2325 /* parse environment variable */
2326 const char *cpath = getenv("CPATH");
2327 if (cpath != NULL && *cpath != '\0') {
2328 const char *begin = cpath;
2332 while (*c != '\0' && *c != ':')
2335 size_t len = c-begin;
2337 /* for gcc compatibility (Matze: I would expect that
2338 * nothing happens for an empty entry...) */
2339 prepend_include_path(".");
2341 char *const string = obstack_copy0(&config_obstack, begin, len);
2342 prepend_include_path(string);
2349 } while(*c != '\0');
2353 void init_preprocessor(void)
2357 obstack_init(&config_obstack);
2358 obstack_init(&pp_obstack);
2359 obstack_init(&input_obstack);
2360 strset_init(&stringset);
2362 setup_include_path();
2365 void exit_preprocessor(void)
2367 obstack_free(&input_obstack, NULL);
2368 obstack_free(&pp_obstack, NULL);
2369 obstack_free(&config_obstack, NULL);
2371 strset_destroy(&stringset);
2374 int pptest_main(int argc, char **argv);
2375 int pptest_main(int argc, char **argv)
2377 init_symbol_table();
2378 init_preprocessor();
2381 error_on_unknown_chars = false;
2382 resolve_escape_sequences = false;
2384 /* simplistic commandline parser */
2385 const char *filename = NULL;
2386 const char *output = NULL;
2387 for (int i = 1; i < argc; ++i) {
2388 const char *opt = argv[i];
2389 if (streq(opt, "-I")) {
2390 prepend_include_path(argv[++i]);
2392 } else if (streq(opt, "-E")) {
2394 } else if (streq(opt, "-o")) {
2397 } else if (opt[0] == '-') {
2398 fprintf(stderr, "Unknown option '%s'\n", opt);
2400 if (filename != NULL)
2401 fprintf(stderr, "Multiple inputs not supported\n");
2405 if (filename == NULL) {
2406 fprintf(stderr, "No input specified\n");
2410 if (output == NULL) {
2413 out = fopen(output, "w");
2415 fprintf(stderr, "Couldn't open output '%s'\n", output);
2420 /* just here for gcc compatibility */
2421 fprintf(out, "# 1 \"%s\"\n", filename);
2422 fprintf(out, "# 1 \"<built-in>\"\n");
2423 fprintf(out, "# 1 \"<command-line>\"\n");
2425 FILE *file = fopen(filename, "r");
2427 fprintf(stderr, "Couldn't open input '%s'\n", filename);
2430 switch_pp_input(file, filename);
2433 next_preprocessing_token();
2434 if (pp_token.kind == T_EOF)
2440 check_unclosed_conditionals();
2441 fclose(close_pp_input());
2446 exit_preprocessor();
2447 exit_symbol_table();