7 #include "lang_features.h"
8 #include "diagnostic.h"
9 #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;
56 char buf[1024+MAX_PUTBACK];
59 source_position_t position;
67 static pp_input_t *input_stack;
68 static unsigned n_inputs;
69 static struct obstack input_obstack;
71 static pp_conditional_t *conditional_stack;
74 static bool resolve_escape_sequences = false;
75 static bool do_print_spaces = true;
76 static bool do_expansions;
77 static bool skip_mode;
79 static struct obstack pp_obstack;
80 static unsigned counted_newlines;
81 static unsigned counted_spaces;
82 static const char *printed_input_name = NULL;
83 static pp_definition_t *current_expansion = NULL;
85 static inline void next_char(void);
86 static void next_preprocessing_token(void);
87 static void print_line_directive(const source_position_t *pos, const char *add);
89 static bool open_input(const char *filename)
91 FILE *file = fopen(filename, "r");
98 input.had_non_space = false;
99 input.position.input_name = filename;
100 input.position.linenr = 1;
102 /* indicate that we're at a new input */
103 print_line_directive(&input.position, input_stack != NULL ? "1" : NULL);
105 counted_newlines = 0;
108 /* read first char and first token */
110 next_preprocessing_token();
115 static void close_input(void)
117 /* ensure we have a newline at EOF */
118 if (input.had_non_space) {
122 assert(input.file != NULL);
131 static void push_input(void)
133 pp_input_t *saved_input
134 = obstack_alloc(&input_obstack, sizeof(*saved_input));
136 memcpy(saved_input, &input, sizeof(*saved_input));
138 /* adjust buffer positions */
139 if (input.bufpos != NULL)
140 saved_input->bufpos = saved_input->buf + (input.bufpos - input.buf);
141 if (input.bufend != NULL)
142 saved_input->bufend = saved_input->buf + (input.bufend - input.buf);
144 saved_input->parent = input_stack;
145 input_stack = saved_input;
149 static void pop_restore_input(void)
151 assert(n_inputs > 0);
152 assert(input_stack != NULL);
154 pp_input_t *saved_input = input_stack;
156 memcpy(&input, saved_input, sizeof(input));
159 /* adjust buffer positions */
160 if (saved_input->bufpos != NULL)
161 input.bufpos = input.buf + (saved_input->bufpos - saved_input->buf);
162 if (saved_input->bufend != NULL)
163 input.bufend = input.buf + (saved_input->bufend - saved_input->buf);
165 input_stack = saved_input->parent;
166 obstack_free(&input_obstack, saved_input);
171 * Prints a parse error message at the current token.
173 * @param msg the error message
175 static void parse_error(const char *msg)
177 errorf(&pp_token.source_position, "%s", msg);
180 static inline void next_real_char(void)
182 assert(input.bufpos <= input.bufend);
183 if (input.bufpos >= input.bufend) {
184 size_t s = fread(input.buf + MAX_PUTBACK, 1,
185 sizeof(input.buf) - MAX_PUTBACK, input.file);
190 input.bufpos = input.buf + MAX_PUTBACK;
191 input.bufend = input.buf + MAX_PUTBACK + s;
193 CC = *input.bufpos++;
197 * Put a character back into the buffer.
199 * @param pc the character to put back
201 static inline void put_back(int pc)
203 assert(input.bufpos > input.buf);
204 *(--input.bufpos - input.buf + input.buf) = (char) pc;
207 printf("putback '%c'\n", pc);
211 #define MATCH_NEWLINE(code) \
217 ++input.position.linenr; \
221 ++input.position.linenr; \
224 #define eat(c_type) do { assert(CC == c_type); next_char(); } while(0)
226 static void maybe_concat_lines(void)
231 MATCH_NEWLINE(return;)
242 * Set c to the next input character, ie.
243 * after expanding trigraphs.
245 static inline void next_char(void)
249 /* filter trigraphs and concatenated lines */
250 if(UNLIKELY(CC == '\\')) {
251 maybe_concat_lines();
252 goto end_of_next_char;
255 if(LIKELY(CC != '?'))
256 goto end_of_next_char;
259 if(LIKELY(CC != '?')) {
262 goto end_of_next_char;
267 case '=': CC = '#'; break;
268 case '(': CC = '['; break;
269 case '/': CC = '\\'; maybe_concat_lines(); break;
270 case ')': CC = ']'; break;
271 case '\'': CC = '^'; break;
272 case '<': CC = '{'; break;
273 case '!': CC = '|'; break;
274 case '>': CC = '}'; break;
275 case '-': CC = '~'; break;
285 printf("nchar '%c'\n", CC);
292 * Returns true if the given char is a octal digit.
294 * @param char the character to check
296 static inline bool is_octal_digit(int chr)
314 * Returns the value of a digit.
315 * The only portable way to do it ...
317 static int digit_value(int digit) {
342 panic("wrong character given");
347 * Parses an octal character sequence.
349 * @param first_digit the already read first digit
351 static int parse_octal_sequence(const int first_digit)
353 assert(is_octal_digit(first_digit));
354 int value = digit_value(first_digit);
355 if (!is_octal_digit(CC)) return value;
356 value = 8 * value + digit_value(CC);
358 if (!is_octal_digit(CC)) return value;
359 value = 8 * value + digit_value(CC);
363 return (signed char) value;
365 return (unsigned char) value;
370 * Parses a hex character sequence.
372 static int parse_hex_sequence(void)
375 while(isxdigit(CC)) {
376 value = 16 * value + digit_value(CC);
381 return (signed char) value;
383 return (unsigned char) value;
388 * Parse an escape sequence.
390 static int parse_escape_sequence(void)
398 case '"': return '"';
399 case '\'': return '\'';
400 case '\\': return '\\';
401 case '?': return '\?';
402 case 'a': return '\a';
403 case 'b': return '\b';
404 case 'f': return '\f';
405 case 'n': return '\n';
406 case 'r': return '\r';
407 case 't': return '\t';
408 case 'v': return '\v';
410 return parse_hex_sequence();
419 return parse_octal_sequence(ec);
421 parse_error("reached end of file while parsing escape sequence");
424 parse_error("unknown escape sequence");
429 static void parse_string_literal(void)
431 const unsigned start_linenr = input.position.linenr;
439 if(resolve_escape_sequences) {
440 tc = parse_escape_sequence();
441 obstack_1grow(&symbol_obstack, (char) tc);
443 obstack_1grow(&symbol_obstack, (char) CC);
445 obstack_1grow(&symbol_obstack, (char) CC);
451 source_position_t source_position;
452 source_position.input_name = pp_token.source_position.input_name;
453 source_position.linenr = start_linenr;
454 errorf(&source_position, "string has no end");
455 pp_token.type = TP_ERROR;
464 obstack_1grow(&symbol_obstack, (char) CC);
471 /* add finishing 0 to the string */
472 obstack_1grow(&symbol_obstack, '\0');
473 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
474 const char *const string = obstack_finish(&symbol_obstack);
476 #if 0 /* TODO hash */
477 /* check if there is already a copy of the string */
478 result = strset_insert(&stringset, string);
479 if(result != string) {
480 obstack_free(&symbol_obstack, string);
483 const char *const result = string;
486 pp_token.type = TP_STRING_LITERAL;
487 pp_token.v.string.begin = result;
488 pp_token.v.string.size = size;
491 static void parse_wide_character_constant(void)
499 found_char = parse_escape_sequence();
503 parse_error("newline while parsing character constant");
509 goto end_of_wide_char_constant;
512 parse_error("EOF while parsing character constant");
513 pp_token.type = TP_ERROR;
517 if(found_char != 0) {
518 parse_error("more than 1 characters in character "
520 goto end_of_wide_char_constant;
529 end_of_wide_char_constant:
530 pp_token.type = TP_WIDE_CHARACTER_CONSTANT;
534 static void parse_wide_string_literal(void)
536 const unsigned start_linenr = input.position.linenr;
544 wchar_rep_t tc = parse_escape_sequence();
545 obstack_grow(&symbol_obstack, &tc, sizeof(tc));
550 source_position_t source_position;
551 source_position.input_name = pp_token.source_position.input_name;
552 source_position.linenr = start_linenr;
553 errorf(&source_position, "string has no end");
554 pp_token.type = TP_ERROR;
564 obstack_grow(&symbol_obstack, &tc, sizeof(tc));
572 /* add finishing 0 to the string */
573 static const wchar_rep_t nul = L'\0';
574 obstack_grow(&symbol_obstack, &nul, sizeof(nul));
577 = (size_t)obstack_object_size(&symbol_obstack) / sizeof(wchar_rep_t);
578 const wchar_rep_t *const string = obstack_finish(&symbol_obstack);
580 #if 0 /* TODO hash */
581 /* check if there is already a copy of the string */
582 const wchar_rep_t *const result = strset_insert(&stringset, string);
583 if(result != string) {
584 obstack_free(&symbol_obstack, string);
587 const wchar_rep_t *const result = string;
590 pp_token.type = TP_WIDE_STRING_LITERAL;
591 pp_token.v.wide_string.begin = result;
592 pp_token.v.wide_string.size = size;
595 static void parse_character_constant(void)
597 const unsigned start_linenr = input.position.linenr;
605 tc = parse_escape_sequence();
606 obstack_1grow(&symbol_obstack, (char) tc);
610 parse_error("newline while parsing character constant");
615 source_position_t source_position;
616 source_position.input_name = pp_token.source_position.input_name;
617 source_position.linenr = start_linenr;
618 errorf(&source_position, "EOF while parsing character constant");
619 pp_token.type = TP_ERROR;
625 goto end_of_char_constant;
628 obstack_1grow(&symbol_obstack, (char) CC);
635 end_of_char_constant:;
636 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
637 const char *const string = obstack_finish(&symbol_obstack);
639 pp_token.type = TP_CHARACTER_CONSTANT;
640 pp_token.v.string.begin = string;
641 pp_token.v.string.size = size;
644 #define SYMBOL_CHARS_WITHOUT_E_P \
695 #define SYMBOL_CHARS \
696 SYMBOL_CHARS_WITHOUT_E_P \
715 * returns next final token from a preprocessor macro expansion
717 static void expand_next(void)
719 assert(current_expansion != NULL);
721 pp_definition_t *definition = current_expansion;
724 if(definition->list_len == 0
725 || definition->expand_pos >= definition->list_len) {
726 /* we're finished with the current macro, move up 1 level in the
728 pp_definition_t *parent = definition->parent_expansion;
729 definition->parent_expansion = NULL;
730 definition->is_expanding = false;
732 /* it was the outermost expansion, parse normal pptoken */
734 current_expansion = NULL;
735 next_preprocessing_token();
739 current_expansion = definition;
742 pp_token = definition->token_list[definition->expand_pos];
743 ++definition->expand_pos;
745 if(pp_token.type != TP_IDENTIFIER)
748 /* if it was an identifier then we might need to expand again */
749 pp_definition_t *symbol_definition = pp_token.v.symbol->pp_definition;
750 if(symbol_definition != NULL && !symbol_definition->is_expanding) {
751 symbol_definition->parent_expansion = definition;
752 symbol_definition->expand_pos = 0;
753 symbol_definition->is_expanding = true;
754 definition = symbol_definition;
755 current_expansion = definition;
760 static void skip_line_comment(void)
781 static void skip_multiline_comment(void)
786 unsigned start_linenr = input.position.linenr;
792 /* TODO: nested comment, warn here */
804 if(do_print_spaces) {
812 source_position_t source_position;
813 source_position.input_name = pp_token.source_position.input_name;
814 source_position.linenr = start_linenr;
815 errorf(&source_position, "at end of file while looking for comment end");
826 /* skip spaces advancing at the start of the next preprocessing token */
827 static void skip_spaces(bool skip_newline)
843 } else if (CC == '*') {
845 skip_multiline_comment();
861 ++input.position.linenr;
871 ++input.position.linenr;
882 static void eat_pp(int type)
885 assert(pp_token.type == type);
886 next_preprocessing_token();
889 static void parse_symbol(void)
891 obstack_1grow(&symbol_obstack, (char) CC);
898 obstack_1grow(&symbol_obstack, (char) CC);
908 obstack_1grow(&symbol_obstack, '\0');
909 char *string = obstack_finish(&symbol_obstack);
911 /* might be a wide string or character constant ( L"string"/L'c' ) */
912 if (CC == '"' && string[0] == 'L' && string[1] == '\0') {
913 obstack_free(&symbol_obstack, string);
914 parse_wide_string_literal();
916 } else if (CC == '\'' && string[0] == 'L' && string[1] == '\0') {
917 obstack_free(&symbol_obstack, string);
918 parse_wide_character_constant();
922 symbol_t *symbol = symbol_table_insert(string);
924 pp_token.type = symbol->pp_ID;
925 pp_token.v.symbol = symbol;
927 /* we can free the memory from symbol obstack if we already had an entry in
928 * the symbol table */
929 if (symbol->string != string) {
930 obstack_free(&symbol_obstack, string);
935 pp_definition_t *pp_definition = symbol->pp_definition;
936 if (pp_definition == NULL)
939 if (pp_definition->has_parameters) {
941 /* no opening brace -> no expansion */
944 next_preprocessing_token();
947 /* parse arguments (TODO) */
948 while (pp_token.type != TP_EOF && pp_token.type != ')')
949 next_preprocessing_token();
950 next_preprocessing_token();
953 pp_definition->expand_pos = 0;
954 pp_definition->is_expanding = true,
955 current_expansion = pp_definition;
959 static void parse_number(void)
961 obstack_1grow(&symbol_obstack, (char) CC);
968 SYMBOL_CHARS_WITHOUT_E_P
969 obstack_1grow(&symbol_obstack, (char) CC);
977 obstack_1grow(&symbol_obstack, (char) CC);
979 if(CC == '+' || CC == '-') {
980 obstack_1grow(&symbol_obstack, (char) CC);
991 obstack_1grow(&symbol_obstack, '\0');
992 size_t size = obstack_object_size(&symbol_obstack);
993 char *string = obstack_finish(&symbol_obstack);
995 pp_token.type = TP_NUMBER;
996 pp_token.v.string.begin = string;
997 pp_token.v.string.size = size;
1002 #define MAYBE_PROLOG \
1007 #define MAYBE(ch, set_type) \
1010 pp_token.type = set_type; \
1013 #define ELSE_CODE(code) \
1017 } /* end of while(1) */ \
1020 #define ELSE(set_type) \
1022 pp_token.type = set_type; \
1026 static void next_preprocessing_token(void)
1028 if(current_expansion != NULL) {
1033 pp_token.source_position = input.position;
1047 pp_token.type = '\n';
1060 parse_string_literal();
1064 parse_character_constant();
1086 MAYBE('.', TP_DOTDOTDOT)
1090 pp_token.type = '.';
1096 MAYBE('&', TP_ANDAND)
1097 MAYBE('=', TP_ANDEQUAL)
1101 MAYBE('=', TP_ASTERISKEQUAL)
1105 MAYBE('+', TP_PLUSPLUS)
1106 MAYBE('=', TP_PLUSEQUAL)
1110 MAYBE('>', TP_MINUSGREATER)
1111 MAYBE('-', TP_MINUSMINUS)
1112 MAYBE('=', TP_MINUSEQUAL)
1116 MAYBE('=', TP_EXCLAMATIONMARKEQUAL)
1120 MAYBE('=', TP_SLASHEQUAL)
1123 skip_multiline_comment();
1127 skip_line_comment();
1133 MAYBE('=', TP_PERCENTEQUAL)
1138 MAYBE(':', TP_HASHHASH)
1142 pp_token.type = '#';
1151 MAYBE('=', TP_LESSEQUAL)
1154 MAYBE('=', TP_LESSLESSEQUAL)
1159 MAYBE('=', TP_GREATEREQUAL)
1162 MAYBE('=', TP_GREATERGREATEREQUAL)
1163 ELSE(TP_GREATERGREATER)
1167 MAYBE('=', TP_CARETEQUAL)
1171 MAYBE('=', TP_PIPEEQUAL)
1172 MAYBE('|', TP_PIPEPIPE)
1180 MAYBE('=', TP_EQUALEQUAL)
1184 MAYBE('#', TP_HASHHASH)
1203 if (input_stack != NULL) {
1205 pop_restore_input();
1206 counted_newlines = 0;
1208 /* hack to output correct line number */
1209 print_line_directive(&input.position, "2");
1210 next_preprocessing_token();
1212 pp_token.type = TP_EOF;
1218 errorf(&pp_token.source_position, "unknown character '%c' found\n", CC);
1219 pp_token.type = TP_ERROR;
1224 static void print_quoted_string(const char *const string)
1227 for (const char *c = string; *c != 0; ++c) {
1229 case '"': fputs("\\\"", out); break;
1230 case '\\': fputs("\\\\", out); break;
1231 case '\a': fputs("\\a", out); break;
1232 case '\b': fputs("\\b", out); break;
1233 case '\f': fputs("\\f", out); break;
1234 case '\n': fputs("\\n", out); break;
1235 case '\r': fputs("\\r", out); break;
1236 case '\t': fputs("\\t", out); break;
1237 case '\v': fputs("\\v", out); break;
1238 case '\?': fputs("\\?", out); break;
1241 fprintf(out, "\\%03o", *c);
1251 static void print_line_directive(const source_position_t *pos, const char *add)
1253 fprintf(out, "# %u ", pos->linenr);
1254 print_quoted_string(pos->input_name);
1261 printed_input_name = pos->input_name;
1264 static void print_spaces(void)
1266 if (counted_newlines >= 9) {
1267 if (input.had_non_space) {
1270 print_line_directive(&pp_token.source_position, NULL);
1271 counted_newlines = 0;
1273 for (unsigned i = 0; i < counted_newlines; ++i)
1275 counted_newlines = 0;
1277 for (unsigned i = 0; i < counted_spaces; ++i)
1282 static void emit_pp_token(void)
1287 if (pp_token.type != '\n') {
1289 input.had_non_space = true;
1292 switch(pp_token.type) {
1294 fputs(pp_token.v.symbol->string, out);
1297 fputs(pp_token.v.string.begin, out);
1299 case TP_STRING_LITERAL:
1301 fputs(pp_token.v.string.begin, out);
1307 print_pp_token_type(out, pp_token.type);
1312 static void eat_pp_directive(void)
1314 while(pp_token.type != '\n' && pp_token.type != TP_EOF) {
1315 next_preprocessing_token();
1319 static bool strings_equal(const string_t *string1, const string_t *string2)
1321 size_t size = string1->size;
1322 if(size != string2->size)
1325 const char *c1 = string1->begin;
1326 const char *c2 = string2->begin;
1327 for(size_t i = 0; i < size; ++i, ++c1, ++c2) {
1334 static bool wide_strings_equal(const wide_string_t *string1,
1335 const wide_string_t *string2)
1337 size_t size = string1->size;
1338 if(size != string2->size)
1341 const wchar_rep_t *c1 = string1->begin;
1342 const wchar_rep_t *c2 = string2->begin;
1343 for(size_t i = 0; i < size; ++i, ++c1, ++c2) {
1350 static bool pp_tokens_equal(const token_t *token1, const token_t *token2)
1352 if(token1->type != token2->type)
1355 switch(token1->type) {
1360 return token1->v.symbol == token2->v.symbol;
1362 case TP_CHARACTER_CONSTANT:
1363 case TP_STRING_LITERAL:
1364 return strings_equal(&token1->v.string, &token2->v.string);
1366 case TP_WIDE_CHARACTER_CONSTANT:
1367 case TP_WIDE_STRING_LITERAL:
1368 return wide_strings_equal(&token1->v.wide_string,
1369 &token2->v.wide_string);
1375 static bool pp_definitions_equal(const pp_definition_t *definition1,
1376 const pp_definition_t *definition2)
1378 if(definition1->list_len != definition2->list_len)
1381 size_t len = definition1->list_len;
1382 const token_t *t1 = definition1->token_list;
1383 const token_t *t2 = definition2->token_list;
1384 for(size_t i = 0; i < len; ++i, ++t1, ++t2) {
1385 if(!pp_tokens_equal(t1, t2))
1391 static void parse_define_directive(void)
1394 assert(obstack_object_size(&pp_obstack) == 0);
1396 if (pp_token.type != TP_IDENTIFIER) {
1397 errorf(&pp_token.source_position,
1398 "expected identifier after #define, got '%t'", &pp_token);
1401 symbol_t *symbol = pp_token.v.symbol;
1403 pp_definition_t *new_definition
1404 = obstack_alloc(&pp_obstack, sizeof(new_definition[0]));
1405 memset(new_definition, 0, sizeof(new_definition[0]));
1406 new_definition->source_position = input.position;
1408 /* this is probably the only place where spaces are significant in the
1409 * lexer (except for the fact that they separate tokens). #define b(x)
1410 * is something else than #define b (x) */
1413 next_preprocessing_token();
1414 /* get next token after '(' */
1415 next_preprocessing_token();
1418 switch (pp_token.type) {
1420 new_definition->is_variadic = true;
1421 next_preprocessing_token();
1422 if (pp_token.type != ')') {
1423 errorf(&input.position,
1424 "'...' not at end of macro argument list");
1429 obstack_ptr_grow(&pp_obstack, pp_token.v.symbol);
1430 next_preprocessing_token();
1432 if (pp_token.type == ',') {
1433 next_preprocessing_token();
1437 if (pp_token.type != ')') {
1438 errorf(&pp_token.source_position,
1439 "expected ',' or ')' after identifier, got '%t'",
1445 next_preprocessing_token();
1446 goto finish_argument_list;
1448 errorf(&pp_token.source_position,
1449 "expected identifier, '...' or ')' in #define argument list, got '%t'",
1455 finish_argument_list:
1456 new_definition->has_parameters = true;
1457 new_definition->n_parameters
1458 = obstack_object_size(&pp_obstack) / sizeof(new_definition->parameters[0]);
1459 new_definition->parameters = obstack_finish(&pp_obstack);
1461 next_preprocessing_token();
1464 /* construct a new pp_definition on the obstack */
1465 assert(obstack_object_size(&pp_obstack) == 0);
1466 size_t list_len = 0;
1467 while (pp_token.type != '\n' && pp_token.type != TP_EOF) {
1468 obstack_grow(&pp_obstack, &pp_token, sizeof(pp_token));
1470 next_preprocessing_token();
1473 new_definition->list_len = list_len;
1474 new_definition->token_list = obstack_finish(&pp_obstack);
1476 pp_definition_t *old_definition = symbol->pp_definition;
1477 if (old_definition != NULL) {
1478 if (!pp_definitions_equal(old_definition, new_definition)) {
1479 warningf(&input.position, "multiple definition of macro '%Y' (first defined %P)",
1480 symbol, &old_definition->source_position);
1482 /* reuse the old definition */
1483 obstack_free(&pp_obstack, new_definition);
1484 new_definition = old_definition;
1488 symbol->pp_definition = new_definition;
1492 if (obstack_object_size(&pp_obstack) > 0) {
1493 char *ptr = obstack_finish(&pp_obstack);
1494 obstack_free(&pp_obstack, ptr);
1499 static void parse_undef_directive(void)
1503 if(pp_token.type != TP_IDENTIFIER) {
1504 errorf(&input.position,
1505 "expected identifier after #undef, got '%t'", &pp_token);
1510 symbol_t *symbol = pp_token.v.symbol;
1511 symbol->pp_definition = NULL;
1512 next_preprocessing_token();
1514 if(pp_token.type != '\n') {
1515 warningf(&input.position, "extra tokens at end of #undef directive");
1517 /* eat until '\n' */
1521 static const char *parse_headername(void)
1523 /* behind an #include we can have the special headername lexems.
1524 * They're only allowed behind an #include so they're not recognized
1525 * by the normal next_preprocessing_token. We handle them as a special
1528 /* skip spaces so we reach start of next preprocessing token */
1531 assert(obstack_object_size(&input_obstack) == 0);
1533 /* check wether we have a "... or <... headername */
1536 /* for now until we have proper searchpath handling */
1537 obstack_1grow(&input_obstack, '.');
1538 obstack_1grow(&input_obstack, '/');
1546 parse_error("header name without closing '>'");
1551 goto finished_headername;
1553 obstack_1grow(&input_obstack, (char) CC);
1556 /* we should never be here */
1559 /* for now until we have proper searchpath handling */
1560 obstack_1grow(&input_obstack, '.');
1561 obstack_1grow(&input_obstack, '/');
1569 parse_error("header name without closing '>'");
1574 goto finished_headername;
1576 obstack_1grow(&input_obstack, (char) CC);
1579 /* we should never be here */
1582 /* TODO: do normale pp_token parsing and concatenate results */
1583 panic("pp_token concat include not implemented yet");
1586 finished_headername:
1587 obstack_1grow(&input_obstack, '\0');
1588 char *headername = obstack_finish(&input_obstack);
1590 /* TODO: iterate search-path to find the file */
1592 next_preprocessing_token();
1597 static bool parse_include_directive(void)
1599 /* don't eat the TP_include here!
1600 * we need an alternative parsing for the next token */
1604 const char *headername = parse_headername();
1605 if (headername == NULL) {
1610 if (pp_token.type != '\n' && pp_token.type != TP_EOF) {
1611 warningf(&pp_token.source_position,
1612 "extra tokens at end of #include directive");
1616 if (n_inputs > INCLUDE_LIMIT) {
1617 errorf(&pp_token.source_position, "#include nested too deeply");
1619 next_preprocessing_token();
1623 /* we have to reenable space counting and macro expansion here,
1624 * because it is still disabled in directive parsing,
1625 * but we will trigger a preprocessing token reading of the new file
1626 * now and need expansions/space counting */
1627 do_print_spaces = true;
1628 do_expansions = true;
1632 bool res = open_input(headername);
1634 errorf(&pp_token.source_position,
1635 "failed including '%s': %s", headername, strerror(errno));
1636 pop_restore_input();
1643 static pp_conditional_t *push_conditional(void)
1645 pp_conditional_t *conditional
1646 = obstack_alloc(&pp_obstack, sizeof(*conditional));
1647 memset(conditional, 0, sizeof(*conditional));
1649 conditional->parent = conditional_stack;
1650 conditional_stack = conditional;
1655 static void pop_conditional(void)
1657 assert(conditional_stack != NULL);
1658 conditional_stack = conditional_stack->parent;
1661 static void check_unclosed_conditionals(void)
1663 while (conditional_stack != NULL) {
1664 pp_conditional_t *conditional = conditional_stack;
1666 if (conditional->in_else) {
1667 errorf(&conditional->source_position, "unterminated #else");
1669 errorf(&conditional->source_position, "unterminated condition");
1675 static void parse_ifdef_ifndef_directive(void)
1677 bool is_ifndef = (pp_token.type == TP_ifndef);
1679 next_preprocessing_token();
1683 pp_conditional_t *conditional = push_conditional();
1684 conditional->source_position = pp_token.source_position;
1685 conditional->skip = true;
1689 if (pp_token.type != TP_IDENTIFIER) {
1690 errorf(&pp_token.source_position,
1691 "expected identifier after #%s, got '%t'",
1692 is_ifndef ? "ifndef" : "ifdef", &pp_token);
1695 /* just take the true case in the hope to avoid further errors */
1698 symbol_t *symbol = pp_token.v.symbol;
1699 pp_definition_t *pp_definition = symbol->pp_definition;
1700 next_preprocessing_token();
1702 if (pp_token.type != '\n') {
1703 errorf(&pp_token.source_position,
1704 "extra tokens at end of #%s",
1705 is_ifndef ? "ifndef" : "ifdef");
1709 /* evaluate wether we are in true or false case */
1710 condition = is_ifndef ? pp_definition == NULL : pp_definition != NULL;
1713 pp_conditional_t *conditional = push_conditional();
1714 conditional->source_position = pp_token.source_position;
1715 conditional->condition = condition;
1722 static void parse_else_directive(void)
1726 if (pp_token.type != '\n') {
1728 warningf(&pp_token.source_position, "extra tokens at end of #else");
1733 pp_conditional_t *conditional = conditional_stack;
1734 if (conditional == NULL) {
1735 errorf(&pp_token.source_position, "#else without prior #if");
1739 if (conditional->in_else) {
1740 errorf(&pp_token.source_position,
1741 "#else after #else (condition started %P)",
1742 conditional->source_position);
1747 conditional->in_else = true;
1748 if (!conditional->skip) {
1749 skip_mode = conditional->condition;
1751 conditional->source_position = pp_token.source_position;
1754 static void parse_endif_directive(void)
1758 if (pp_token.type != '\n') {
1760 warningf(&pp_token.source_position,
1761 "extra tokens at end of #endif");
1766 pp_conditional_t *conditional = conditional_stack;
1767 if (conditional == NULL) {
1768 errorf(&pp_token.source_position, "#endif without prior #if");
1772 if (!conditional->skip) {
1778 static void parse_preprocessing_directive(void)
1780 do_print_spaces = false;
1781 do_expansions = false;
1785 switch(pp_token.type) {
1788 parse_ifdef_ifndef_directive();
1791 parse_else_directive();
1794 parse_endif_directive();
1801 switch(pp_token.type) {
1803 parse_define_directive();
1806 parse_undef_directive();
1810 parse_ifdef_ifndef_directive();
1813 parse_else_directive();
1816 parse_endif_directive();
1819 bool in_new_source = parse_include_directive();
1820 /* no need to do anything if source file switched */
1826 /* the nop directive */
1829 errorf(&pp_token.source_position,
1830 "invalid preprocessing directive #%t", &pp_token);
1836 do_print_spaces = true;
1837 do_expansions = true;
1840 assert(pp_token.type == '\n' || pp_token.type == TP_EOF);
1841 next_preprocessing_token();
1844 #define GCC_COMPAT_MODE
1846 int pptest_main(int argc, char **argv);
1847 int pptest_main(int argc, char **argv)
1849 init_symbol_table();
1852 obstack_init(&pp_obstack);
1853 obstack_init(&input_obstack);
1855 const char *filename = "t.c";
1861 #ifdef GCC_COMPAT_MODE
1862 /* this is here so we can directly compare "gcc -E" output and our output */
1863 fprintf(out, "# 1 \"%s\"\n", filename);
1864 fputs("# 1 \"<built-in>\"\n", out);
1865 fputs("# 1 \"<command-line>\"\n", out);
1868 bool ok = open_input(filename);
1872 /* we're at a line begin */
1873 if(pp_token.type == '#') {
1874 parse_preprocessing_directive();
1876 /* parse+emit a line */
1877 while(pp_token.type != '\n') {
1878 if(pp_token.type == TP_EOF)
1879 goto end_of_main_loop;
1881 next_preprocessing_token();
1884 next_preprocessing_token();
1889 check_unclosed_conditionals();
1892 obstack_free(&input_obstack, NULL);
1893 obstack_free(&pp_obstack, NULL);
1896 exit_symbol_table();