12 #include "adt/error.h"
13 #include "lang_features.h"
14 #include "diagnostic.h"
15 #include "string_rep.h"
19 #define INCLUDE_LIMIT 199 /* 199 is for gcc "compatibility" */
21 struct pp_argument_t {
26 struct pp_definition_t {
28 source_position_t source_position;
29 pp_definition_t *parent_expansion;
32 bool is_expanding : 1;
33 bool has_parameters : 1;
43 typedef struct pp_conditional_t pp_conditional_t;
44 struct pp_conditional_t {
45 source_position_t source_position;
48 bool skip; /**< conditional in skip mode (then+else gets skipped) */
49 pp_conditional_t *parent;
52 typedef struct pp_input_t pp_input_t;
57 utf32 buf[1024+MAX_PUTBACK];
60 source_position_t position;
65 /** additional info about the current token */
66 typedef struct add_token_info_t {
67 /** whitespace from beginning of line to the token */
69 /** there has been any whitespace before the token */
71 /** the token is at the beginning of the line */
75 static pp_input_t input;
77 static pp_input_t *input_stack;
78 static unsigned n_inputs;
79 static struct obstack input_obstack;
81 static pp_conditional_t *conditional_stack;
83 static token_t pp_token;
84 static bool resolve_escape_sequences = false;
85 static bool ignore_unknown_chars = true;
86 static bool in_pp_directive;
87 static bool skip_mode;
89 static struct obstack pp_obstack;
90 static const char *printed_input_name = NULL;
91 static source_position_t expansion_pos;
92 static pp_definition_t *current_expansion = NULL;
93 static preprocessor_token_type_t last_token = TP_ERROR;
95 static add_token_info_t info;
97 static inline void next_char(void);
98 static void next_preprocessing_token(void);
99 static void print_line_directive(const source_position_t *pos, const char *add);
101 static bool open_input(const char *filename)
103 FILE *file = fopen(filename, "r");
108 input.input = input_from_stream(file, NULL);
111 input.output_line = 0;
112 input.position.input_name = filename;
113 input.position.lineno = 1;
115 /* indicate that we're at a new input */
116 print_line_directive(&input.position, input_stack != NULL ? "1" : NULL);
118 /* place a virtual '\n' so we realize we're at line begin */
119 input.position.lineno = 0;
121 next_preprocessing_token();
126 static void close_input(void)
128 input_free(input.input);
129 assert(input.file != NULL);
139 static void push_input(void)
141 pp_input_t *saved_input
142 = obstack_alloc(&input_obstack, sizeof(*saved_input));
144 memcpy(saved_input, &input, sizeof(*saved_input));
146 /* adjust buffer positions */
147 if (input.bufpos != NULL)
148 saved_input->bufpos = saved_input->buf + (input.bufpos - input.buf);
149 if (input.bufend != NULL)
150 saved_input->bufend = saved_input->buf + (input.bufend - input.buf);
152 saved_input->parent = input_stack;
153 input_stack = saved_input;
157 static void pop_restore_input(void)
159 assert(n_inputs > 0);
160 assert(input_stack != NULL);
162 pp_input_t *saved_input = input_stack;
164 memcpy(&input, saved_input, sizeof(input));
167 /* adjust buffer positions */
168 if (saved_input->bufpos != NULL)
169 input.bufpos = input.buf + (saved_input->bufpos - saved_input->buf);
170 if (saved_input->bufend != NULL)
171 input.bufend = input.buf + (saved_input->bufend - saved_input->buf);
173 input_stack = saved_input->parent;
174 obstack_free(&input_obstack, saved_input);
179 * Prints a parse error message at the current token.
181 * @param msg the error message
183 static void parse_error(const char *msg)
185 errorf(&pp_token.source_position, "%s", msg);
188 static inline void next_real_char(void)
190 assert(input.bufpos <= input.bufend);
191 if (input.bufpos >= input.bufend) {
192 size_t n = decode(input.input, input.buf + MAX_PUTBACK,
193 sizeof(input.buf)/sizeof(input.buf[0]) - MAX_PUTBACK);
198 input.bufpos = input.buf + MAX_PUTBACK;
199 input.bufend = input.bufpos + n;
201 input.c = *input.bufpos++;
202 ++input.position.colno;
206 * Put a character back into the buffer.
208 * @param pc the character to put back
210 static inline void put_back(utf32 const pc)
212 assert(input.bufpos > input.buf);
213 *(--input.bufpos - input.buf + input.buf) = (char) pc;
214 --input.position.colno;
217 #define MATCH_NEWLINE(code) \
220 if (input.c == '\n') { \
224 info.whitespace = 0; \
225 ++input.position.lineno; \
226 input.position.colno = 1; \
229 #define eat(c_type) (assert(input.c == c_type), next_char())
231 static void maybe_concat_lines(void)
249 * Set c to the next input character, ie.
250 * after expanding trigraphs.
252 static inline void next_char(void)
256 /* filter trigraphs and concatenated lines */
257 if (UNLIKELY(input.c == '\\')) {
258 maybe_concat_lines();
259 goto end_of_next_char;
262 if (LIKELY(input.c != '?'))
263 goto end_of_next_char;
266 if (LIKELY(input.c != '?')) {
269 goto end_of_next_char;
274 case '=': input.c = '#'; break;
275 case '(': input.c = '['; break;
276 case '/': input.c = '\\'; maybe_concat_lines(); break;
277 case ')': input.c = ']'; break;
278 case '\'': input.c = '^'; break;
279 case '<': input.c = '{'; break;
280 case '!': input.c = '|'; break;
281 case '>': input.c = '}'; break;
282 case '-': input.c = '~'; break;
292 printf("nchar '%c'\n", input.c);
299 * Returns true if the given char is a octal digit.
301 * @param char the character to check
303 static inline bool is_octal_digit(int chr)
321 * Returns the value of a digit.
322 * The only portable way to do it ...
324 static int digit_value(int digit)
350 panic("wrong character given");
355 * Parses an octal character sequence.
357 * @param first_digit the already read first digit
359 static int parse_octal_sequence(const int first_digit)
361 assert(is_octal_digit(first_digit));
362 int value = digit_value(first_digit);
363 if (!is_octal_digit(input.c)) return value;
364 value = 8 * value + digit_value(input.c);
366 if (!is_octal_digit(input.c)) return value;
367 value = 8 * value + digit_value(input.c);
370 if (char_is_signed) {
371 return (signed char) value;
373 return (unsigned char) value;
378 * Parses a hex character sequence.
380 static int parse_hex_sequence(void)
383 while (isxdigit(input.c)) {
384 value = 16 * value + digit_value(input.c);
388 if (char_is_signed) {
389 return (signed char) value;
391 return (unsigned char) value;
396 * Parse an escape sequence.
398 static int parse_escape_sequence(void)
406 case '"': return '"';
407 case '\'': return '\'';
408 case '\\': return '\\';
409 case '?': return '\?';
410 case 'a': return '\a';
411 case 'b': return '\b';
412 case 'f': return '\f';
413 case 'n': return '\n';
414 case 'r': return '\r';
415 case 't': return '\t';
416 case 'v': return '\v';
418 return parse_hex_sequence();
427 return parse_octal_sequence(ec);
429 parse_error("reached end of file while parsing escape sequence");
432 parse_error("unknown escape sequence");
437 static void grow_symbol(utf32 const tc)
439 struct obstack *const o = &symbol_obstack;
441 obstack_1grow(o, tc);
442 } else if (tc < 0x800) {
443 obstack_1grow(o, 0xC0 | (tc >> 6));
444 obstack_1grow(o, 0x80 | (tc & 0x3F));
445 } else if (tc < 0x10000) {
446 obstack_1grow(o, 0xE0 | ( tc >> 12));
447 obstack_1grow(o, 0x80 | ((tc >> 6) & 0x3F));
448 obstack_1grow(o, 0x80 | ( tc & 0x3F));
450 obstack_1grow(o, 0xF0 | ( tc >> 18));
451 obstack_1grow(o, 0x80 | ((tc >> 12) & 0x3F));
452 obstack_1grow(o, 0x80 | ((tc >> 6) & 0x3F));
453 obstack_1grow(o, 0x80 | ( tc & 0x3F));
457 static string_t identify_string(char *string, size_t len)
461 const char *result = strset_insert(&stringset, concat);
462 if (result != concat) {
463 obstack_free(&symbol_obstack, concat);
466 const char *result = string;
468 return (string_t) {result, len};
471 static void parse_string_literal(void)
473 const unsigned start_linenr = input.position.lineno;
481 if (resolve_escape_sequences) {
482 tc = parse_escape_sequence();
483 obstack_1grow(&symbol_obstack, (char) tc);
485 obstack_1grow(&symbol_obstack, (char) input.c);
487 obstack_1grow(&symbol_obstack, (char) input.c);
494 source_position_t source_position;
495 source_position.input_name = pp_token.source_position.input_name;
496 source_position.lineno = start_linenr;
497 errorf(&source_position, "string has no end");
498 pp_token.type = TP_ERROR;
507 grow_symbol(input.c);
514 /* add finishing 0 to the string */
515 obstack_1grow(&symbol_obstack, '\0');
516 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
517 char *const string = obstack_finish(&symbol_obstack);
519 pp_token.type = TP_STRING_LITERAL;
520 pp_token.literal = identify_string(string, size);
524 * Parse a wide string literal and set lexer_token.
526 static void parse_wide_string_literal(void)
528 parse_string_literal();
529 if (pp_token.type == TP_STRING_LITERAL)
530 pp_token.type = TP_WIDE_STRING_LITERAL;
533 static void parse_wide_character_constant(void)
540 const utf32 tc = parse_escape_sequence();
546 parse_error("newline while parsing character constant");
552 goto end_of_wide_char_constant;
555 parse_error("EOF while parsing character constant");
556 pp_token.type = TP_ERROR;
560 grow_symbol(input.c);
566 end_of_wide_char_constant:
567 obstack_1grow(&symbol_obstack, '\0');
568 size_t size = (size_t) obstack_object_size(&symbol_obstack)-1;
569 char *string = obstack_finish(&symbol_obstack);
570 pp_token.type = TP_WIDE_CHARACTER_CONSTANT;
571 pp_token.literal = identify_string(string, size);
574 parse_error("empty character constant");
578 static void parse_character_constant(void)
580 const unsigned start_linenr = input.position.lineno;
588 tc = parse_escape_sequence();
589 obstack_1grow(&symbol_obstack, (char) tc);
593 parse_error("newline while parsing character constant");
598 source_position_t source_position;
599 source_position.input_name = pp_token.source_position.input_name;
600 source_position.lineno = start_linenr;
601 errorf(&source_position, "EOF while parsing character constant");
602 pp_token.type = TP_ERROR;
608 goto end_of_char_constant;
611 obstack_1grow(&symbol_obstack, (char) input.c);
618 end_of_char_constant:;
619 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
620 const char *const string = obstack_finish(&symbol_obstack);
622 pp_token.type = TP_CHARACTER_CONSTANT;
623 pp_token.literal.begin = string;
624 pp_token.literal.size = size;
627 #define SYMBOL_CHARS_WITHOUT_E_P \
678 #define SYMBOL_CHARS \
679 SYMBOL_CHARS_WITHOUT_E_P \
698 * returns next final token from a preprocessor macro expansion
700 static void expand_next(void)
702 assert(current_expansion != NULL);
704 pp_definition_t *definition = current_expansion;
707 if (definition->list_len == 0
708 || definition->expand_pos >= definition->list_len) {
709 /* we're finished with the current macro, move up 1 level in the
711 pp_definition_t *parent = definition->parent_expansion;
712 definition->parent_expansion = NULL;
713 definition->is_expanding = false;
715 /* it was the outermost expansion, parse normal pptoken */
716 if (parent == NULL) {
717 current_expansion = NULL;
718 next_preprocessing_token();
722 current_expansion = definition;
725 pp_token = definition->token_list[definition->expand_pos];
726 pp_token.source_position = expansion_pos;
727 ++definition->expand_pos;
729 if (pp_token.type != TP_IDENTIFIER)
732 /* if it was an identifier then we might need to expand again */
733 pp_definition_t *symbol_definition = pp_token.symbol->pp_definition;
734 if (symbol_definition != NULL && !symbol_definition->is_expanding) {
735 symbol_definition->parent_expansion = definition;
736 symbol_definition->expand_pos = 0;
737 symbol_definition->is_expanding = true;
738 definition = symbol_definition;
739 current_expansion = definition;
744 static void skip_line_comment(void)
762 static void skip_multiline_comment(void)
764 unsigned start_linenr = input.position.lineno;
769 if (input.c == '*') {
770 /* TODO: nested comment, warn here */
775 if (input.c == '/') {
777 info.whitespace += input.position.colno-1;
783 info.at_line_begin |= !in_pp_directive;
788 source_position_t source_position;
789 source_position.input_name = pp_token.source_position.input_name;
790 source_position.lineno = start_linenr;
791 errorf(&source_position, "at end of file while looking for comment end");
802 static void skip_whitespace(void)
812 info.at_line_begin = true;
818 if (input.c == '/') {
822 } else if (input.c == '*') {
824 skip_multiline_comment();
837 static void eat_pp(int type)
840 assert(pp_token.type == type);
841 next_preprocessing_token();
844 static void parse_symbol(void)
846 obstack_1grow(&symbol_obstack, (char) input.c);
853 obstack_1grow(&symbol_obstack, (char) input.c);
863 obstack_1grow(&symbol_obstack, '\0');
864 char *string = obstack_finish(&symbol_obstack);
866 /* might be a wide string or character constant ( L"string"/L'c' ) */
867 if (input.c == '"' && string[0] == 'L' && string[1] == '\0') {
868 obstack_free(&symbol_obstack, string);
869 parse_wide_string_literal();
871 } else if (input.c == '\'' && string[0] == 'L' && string[1] == '\0') {
872 obstack_free(&symbol_obstack, string);
873 parse_wide_character_constant();
877 symbol_t *symbol = symbol_table_insert(string);
879 pp_token.type = symbol->pp_ID;
880 pp_token.symbol = symbol;
882 /* we can free the memory from symbol obstack if we already had an entry in
883 * the symbol table */
884 if (symbol->string != string) {
885 obstack_free(&symbol_obstack, string);
889 static void parse_number(void)
891 obstack_1grow(&symbol_obstack, (char) input.c);
898 SYMBOL_CHARS_WITHOUT_E_P
899 obstack_1grow(&symbol_obstack, (char) input.c);
907 obstack_1grow(&symbol_obstack, (char) input.c);
909 if (input.c == '+' || input.c == '-') {
910 obstack_1grow(&symbol_obstack, (char) input.c);
921 obstack_1grow(&symbol_obstack, '\0');
922 size_t size = obstack_object_size(&symbol_obstack);
923 char *string = obstack_finish(&symbol_obstack);
925 pp_token.type = TP_NUMBER;
926 pp_token.literal.begin = string;
927 pp_token.literal.size = size;
931 #define MAYBE_PROLOG \
936 #define MAYBE(ch, set_type) \
939 pp_token.type = set_type; \
942 #define ELSE_CODE(code) \
949 #define ELSE(set_type) \
951 pp_token.type = set_type; \
954 static void next_preprocessing_token(void)
956 if (current_expansion != NULL) {
961 info.at_line_begin = false;
962 info.had_whitespace = false;
964 pp_token.source_position = input.position;
969 info.had_whitespace = true;
974 info.at_line_begin = true;
975 info.had_whitespace = true;
988 parse_string_literal();
992 parse_character_constant();
1014 MAYBE('.', TP_DOTDOTDOT)
1018 pp_token.type = '.';
1023 MAYBE('&', TP_ANDAND)
1024 MAYBE('=', TP_ANDEQUAL)
1028 MAYBE('=', TP_ASTERISKEQUAL)
1032 MAYBE('+', TP_PLUSPLUS)
1033 MAYBE('=', TP_PLUSEQUAL)
1037 MAYBE('>', TP_MINUSGREATER)
1038 MAYBE('-', TP_MINUSMINUS)
1039 MAYBE('=', TP_MINUSEQUAL)
1043 MAYBE('=', TP_EXCLAMATIONMARKEQUAL)
1047 MAYBE('=', TP_SLASHEQUAL)
1050 info.had_whitespace = true;
1051 skip_multiline_comment();
1055 info.had_whitespace = true;
1056 skip_line_comment();
1062 MAYBE('=', TP_PERCENTEQUAL)
1067 MAYBE(':', TP_HASHHASH)
1071 pp_token.type = '#';
1079 MAYBE('=', TP_LESSEQUAL)
1082 MAYBE('=', TP_LESSLESSEQUAL)
1087 MAYBE('=', TP_GREATEREQUAL)
1090 MAYBE('=', TP_GREATERGREATEREQUAL)
1091 ELSE(TP_GREATERGREATER)
1095 MAYBE('=', TP_CARETEQUAL)
1099 MAYBE('=', TP_PIPEEQUAL)
1100 MAYBE('|', TP_PIPEPIPE)
1108 MAYBE('=', TP_EQUALEQUAL)
1112 MAYBE('#', TP_HASHHASH)
1114 pp_token.type = '#';
1128 pp_token.type = input.c;
1133 if (input_stack != NULL) {
1135 pop_restore_input();
1137 print_line_directive(&input.position, "2");
1140 pp_token.source_position.lineno++;
1141 info.at_line_begin = true;
1142 pp_token.type = TP_EOF;
1148 if (!ignore_unknown_chars) {
1149 errorf(&pp_token.source_position, "unknown character '%c' found\n",
1151 pp_token.type = TP_ERROR;
1153 pp_token.type = input.c;
1159 static void print_quoted_string(const char *const string)
1162 for (const char *c = string; *c != 0; ++c) {
1164 case '"': fputs("\\\"", out); break;
1165 case '\\': fputs("\\\\", out); break;
1166 case '\a': fputs("\\a", out); break;
1167 case '\b': fputs("\\b", out); break;
1168 case '\f': fputs("\\f", out); break;
1169 case '\n': fputs("\\n", out); break;
1170 case '\r': fputs("\\r", out); break;
1171 case '\t': fputs("\\t", out); break;
1172 case '\v': fputs("\\v", out); break;
1173 case '\?': fputs("\\?", out); break;
1176 fprintf(out, "\\%03o", (unsigned)*c);
1186 static void print_line_directive(const source_position_t *pos, const char *add)
1188 fprintf(out, "# %u ", pos->lineno);
1189 print_quoted_string(pos->input_name);
1195 printed_input_name = pos->input_name;
1196 input.output_line = pos->lineno-1;
1199 static void emit_newlines(void)
1201 unsigned delta = pp_token.source_position.lineno - input.output_line;
1205 print_line_directive(&pp_token.source_position, NULL);
1208 for (unsigned i = 0; i < delta; ++i) {
1212 input.output_line = pp_token.source_position.lineno;
1215 static void emit_pp_token(void)
1220 if (info.at_line_begin) {
1223 for (unsigned i = 0; i < info.whitespace; ++i)
1226 } else if (info.had_whitespace ||
1227 tokens_would_paste(last_token, pp_token.type)) {
1231 switch (pp_token.type) {
1233 fputs(pp_token.symbol->string, out);
1236 fputs(pp_token.literal.begin, out);
1238 case TP_WIDE_STRING_LITERAL:
1240 case TP_STRING_LITERAL:
1242 fputs(pp_token.literal.begin, out);
1245 case TP_WIDE_CHARACTER_CONSTANT:
1247 case TP_CHARACTER_CONSTANT:
1249 fputs(pp_token.literal.begin, out);
1253 print_pp_token_type(out, pp_token.type);
1256 last_token = pp_token.type;
1259 static void eat_pp_directive(void)
1261 while (!info.at_line_begin) {
1262 next_preprocessing_token();
1266 static bool strings_equal(const string_t *string1, const string_t *string2)
1268 size_t size = string1->size;
1269 if (size != string2->size)
1272 const char *c1 = string1->begin;
1273 const char *c2 = string2->begin;
1274 for (size_t i = 0; i < size; ++i, ++c1, ++c2) {
1281 static bool pp_tokens_equal(const token_t *token1, const token_t *token2)
1283 if (token1->type != token2->type)
1286 switch (token1->type) {
1291 return token1->symbol == token2->symbol;
1293 case TP_CHARACTER_CONSTANT:
1294 case TP_STRING_LITERAL:
1295 return strings_equal(&token1->literal, &token2->literal);
1302 static bool pp_definitions_equal(const pp_definition_t *definition1,
1303 const pp_definition_t *definition2)
1305 if (definition1->list_len != definition2->list_len)
1308 size_t len = definition1->list_len;
1309 const token_t *t1 = definition1->token_list;
1310 const token_t *t2 = definition2->token_list;
1311 for (size_t i = 0; i < len; ++i, ++t1, ++t2) {
1312 if (!pp_tokens_equal(t1, t2))
1318 static void parse_define_directive(void)
1321 assert(obstack_object_size(&pp_obstack) == 0);
1323 if (pp_token.type != TP_IDENTIFIER || info.at_line_begin) {
1324 errorf(&pp_token.source_position,
1325 "expected identifier after #define, got '%t'", &pp_token);
1328 symbol_t *symbol = pp_token.symbol;
1330 pp_definition_t *new_definition
1331 = obstack_alloc(&pp_obstack, sizeof(new_definition[0]));
1332 memset(new_definition, 0, sizeof(new_definition[0]));
1333 new_definition->source_position = input.position;
1335 /* this is probably the only place where spaces are significant in the
1336 * lexer (except for the fact that they separate tokens). #define b(x)
1337 * is something else than #define b (x) */
1338 if (input.c == '(') {
1340 next_preprocessing_token();
1341 /* get next token after '(' */
1342 next_preprocessing_token();
1345 switch (pp_token.type) {
1347 new_definition->is_variadic = true;
1348 next_preprocessing_token();
1349 if (pp_token.type != ')') {
1350 errorf(&input.position,
1351 "'...' not at end of macro argument list");
1356 obstack_ptr_grow(&pp_obstack, pp_token.symbol);
1357 next_preprocessing_token();
1359 if (pp_token.type == ',') {
1360 next_preprocessing_token();
1364 if (pp_token.type != ')') {
1365 errorf(&pp_token.source_position,
1366 "expected ',' or ')' after identifier, got '%t'",
1372 next_preprocessing_token();
1373 goto finish_argument_list;
1375 errorf(&pp_token.source_position,
1376 "expected identifier, '...' or ')' in #define argument list, got '%t'",
1382 finish_argument_list:
1383 new_definition->has_parameters = true;
1384 new_definition->n_parameters
1385 = obstack_object_size(&pp_obstack) / sizeof(new_definition->parameters[0]);
1386 new_definition->parameters = obstack_finish(&pp_obstack);
1388 next_preprocessing_token();
1391 /* construct a new pp_definition on the obstack */
1392 assert(obstack_object_size(&pp_obstack) == 0);
1393 size_t list_len = 0;
1394 while (!info.at_line_begin) {
1395 obstack_grow(&pp_obstack, &pp_token, sizeof(pp_token));
1397 next_preprocessing_token();
1400 new_definition->list_len = list_len;
1401 new_definition->token_list = obstack_finish(&pp_obstack);
1403 pp_definition_t *old_definition = symbol->pp_definition;
1404 if (old_definition != NULL) {
1405 if (!pp_definitions_equal(old_definition, new_definition)) {
1406 warningf(WARN_OTHER, &input.position, "multiple definition of macro '%Y' (first defined %P)", symbol, &old_definition->source_position);
1408 /* reuse the old definition */
1409 obstack_free(&pp_obstack, new_definition);
1410 new_definition = old_definition;
1414 symbol->pp_definition = new_definition;
1418 if (obstack_object_size(&pp_obstack) > 0) {
1419 char *ptr = obstack_finish(&pp_obstack);
1420 obstack_free(&pp_obstack, ptr);
1425 static void parse_undef_directive(void)
1429 if (pp_token.type != TP_IDENTIFIER) {
1430 errorf(&input.position,
1431 "expected identifier after #undef, got '%t'", &pp_token);
1436 symbol_t *symbol = pp_token.symbol;
1437 symbol->pp_definition = NULL;
1438 next_preprocessing_token();
1440 if (!info.at_line_begin) {
1441 warningf(WARN_OTHER, &input.position, "extra tokens at end of #undef directive");
1446 static const char *parse_headername(void)
1448 /* behind an #include we can have the special headername lexems.
1449 * They're only allowed behind an #include so they're not recognized
1450 * by the normal next_preprocessing_token. We handle them as a special
1454 if (info.at_line_begin) {
1455 parse_error("expected headername after #include");
1459 assert(obstack_object_size(&input_obstack) == 0);
1461 /* check wether we have a "... or <... headername */
1470 parse_error("header name without closing '>'");
1475 goto finished_headername;
1477 obstack_1grow(&input_obstack, (char) input.c);
1480 /* we should never be here */
1489 parse_error("header name without closing '>'");
1494 goto finished_headername;
1496 obstack_1grow(&input_obstack, (char) input.c);
1499 /* we should never be here */
1502 /* TODO: do normal pp_token parsing and concatenate results */
1503 panic("pp_token concat include not implemented yet");
1506 finished_headername:
1507 obstack_1grow(&input_obstack, '\0');
1508 char *headername = obstack_finish(&input_obstack);
1510 /* TODO: iterate search-path to find the file */
1517 static bool parse_include_directive(void)
1519 /* don't eat the TP_include here!
1520 * we need an alternative parsing for the next token */
1522 const char *headername = parse_headername();
1523 if (headername == NULL) {
1528 if (!info.at_line_begin) {
1529 warningf(WARN_OTHER, &pp_token.source_position, "extra tokens at end of #include directive");
1533 if (n_inputs > INCLUDE_LIMIT) {
1534 errorf(&pp_token.source_position, "#include nested too deeply");
1536 next_preprocessing_token();
1540 /* we have to reenable space counting and macro expansion here,
1541 * because it is still disabled in directive parsing,
1542 * but we will trigger a preprocessing token reading of the new file
1543 * now and need expansions/space counting */
1544 in_pp_directive = false;
1549 bool res = open_input(headername);
1551 errorf(&pp_token.source_position,
1552 "failed including '%s': %s", headername, strerror(errno));
1553 pop_restore_input();
1560 static pp_conditional_t *push_conditional(void)
1562 pp_conditional_t *conditional
1563 = obstack_alloc(&pp_obstack, sizeof(*conditional));
1564 memset(conditional, 0, sizeof(*conditional));
1566 conditional->parent = conditional_stack;
1567 conditional_stack = conditional;
1572 static void pop_conditional(void)
1574 assert(conditional_stack != NULL);
1575 conditional_stack = conditional_stack->parent;
1578 static void check_unclosed_conditionals(void)
1580 while (conditional_stack != NULL) {
1581 pp_conditional_t *conditional = conditional_stack;
1583 if (conditional->in_else) {
1584 errorf(&conditional->source_position, "unterminated #else");
1586 errorf(&conditional->source_position, "unterminated condition");
1592 static void parse_ifdef_ifndef_directive(void)
1594 bool is_ifndef = (pp_token.type == TP_ifndef);
1596 next_preprocessing_token();
1600 pp_conditional_t *conditional = push_conditional();
1601 conditional->source_position = pp_token.source_position;
1602 conditional->skip = true;
1606 if (pp_token.type != TP_IDENTIFIER || info.at_line_begin) {
1607 errorf(&pp_token.source_position,
1608 "expected identifier after #%s, got '%t'",
1609 is_ifndef ? "ifndef" : "ifdef", &pp_token);
1612 /* just take the true case in the hope to avoid further errors */
1615 symbol_t *symbol = pp_token.symbol;
1616 pp_definition_t *pp_definition = symbol->pp_definition;
1617 next_preprocessing_token();
1619 if (!info.at_line_begin) {
1620 errorf(&pp_token.source_position,
1621 "extra tokens at end of #%s",
1622 is_ifndef ? "ifndef" : "ifdef");
1626 /* evaluate wether we are in true or false case */
1627 condition = is_ifndef ? pp_definition == NULL : pp_definition != NULL;
1630 pp_conditional_t *conditional = push_conditional();
1631 conditional->source_position = pp_token.source_position;
1632 conditional->condition = condition;
1639 static void parse_else_directive(void)
1643 if (!info.at_line_begin) {
1645 warningf(WARN_OTHER, &pp_token.source_position, "extra tokens at end of #else");
1650 pp_conditional_t *conditional = conditional_stack;
1651 if (conditional == NULL) {
1652 errorf(&pp_token.source_position, "#else without prior #if");
1656 if (conditional->in_else) {
1657 errorf(&pp_token.source_position,
1658 "#else after #else (condition started %P)",
1659 conditional->source_position);
1664 conditional->in_else = true;
1665 if (!conditional->skip) {
1666 skip_mode = conditional->condition;
1668 conditional->source_position = pp_token.source_position;
1671 static void parse_endif_directive(void)
1675 if (!info.at_line_begin) {
1677 warningf(WARN_OTHER, &pp_token.source_position, "extra tokens at end of #endif");
1682 pp_conditional_t *conditional = conditional_stack;
1683 if (conditional == NULL) {
1684 errorf(&pp_token.source_position, "#endif without prior #if");
1688 if (!conditional->skip) {
1694 static void parse_preprocessing_directive(void)
1696 in_pp_directive = true;
1700 switch (pp_token.type) {
1703 parse_ifdef_ifndef_directive();
1706 parse_else_directive();
1709 parse_endif_directive();
1716 switch (pp_token.type) {
1718 parse_define_directive();
1721 parse_undef_directive();
1725 parse_ifdef_ifndef_directive();
1728 parse_else_directive();
1731 parse_endif_directive();
1734 parse_include_directive();
1737 if (info.at_line_begin) {
1738 /* the nop directive "#" */
1741 errorf(&pp_token.source_position,
1742 "invalid preprocessing directive #%t", &pp_token);
1748 in_pp_directive = false;
1749 assert(info.at_line_begin);
1752 int pptest_main(int argc, char **argv);
1753 int pptest_main(int argc, char **argv)
1755 init_symbol_table();
1758 obstack_init(&pp_obstack);
1759 obstack_init(&input_obstack);
1761 const char *filename = "t.c";
1767 /* just here for gcc compatibility */
1768 fprintf(out, "# 1 \"%s\"\n", filename);
1769 fprintf(out, "# 1 \"<built-in>\"\n");
1770 fprintf(out, "# 1 \"<command-line>\"\n");
1772 bool ok = open_input(filename);
1776 if (pp_token.type == '#' && info.at_line_begin) {
1777 parse_preprocessing_directive();
1779 } else if (pp_token.type == TP_EOF) {
1780 goto end_of_main_loop;
1781 } else if (pp_token.type == TP_IDENTIFIER && !in_pp_directive) {
1782 symbol_t *symbol = pp_token.symbol;
1783 pp_definition_t *pp_definition = symbol->pp_definition;
1784 if (pp_definition != NULL && !pp_definition->is_expanding) {
1785 expansion_pos = pp_token.source_position;
1786 if (pp_definition->has_parameters) {
1787 source_position_t position = pp_token.source_position;
1788 add_token_info_t old_info = info;
1789 next_preprocessing_token();
1790 add_token_info_t new_info = info;
1792 /* no opening brace -> no expansion */
1793 if (pp_token.type == '(') {
1796 /* parse arguments (TODO) */
1797 while (pp_token.type != TP_EOF && pp_token.type != ')')
1798 next_preprocessing_token();
1800 token_t next_token = pp_token;
1801 /* restore identifier token */
1802 pp_token.type = TP_IDENTIFIER;
1803 pp_token.symbol = symbol;
1804 pp_token.source_position = position;
1809 pp_token = next_token;
1814 pp_definition->expand_pos = 0;
1815 pp_definition->is_expanding = true;
1816 current_expansion = pp_definition;
1823 next_preprocessing_token();
1828 check_unclosed_conditionals();
1831 obstack_free(&input_obstack, NULL);
1832 obstack_free(&pp_obstack, NULL);
1835 exit_symbol_table();