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;
64 static pp_input_t input;
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;
73 static token_t pp_token;
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.lineno = 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.lineno; \
221 ++input.position.lineno; \
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)
343 panic("wrong character given");
348 * Parses an octal character sequence.
350 * @param first_digit the already read first digit
352 static int parse_octal_sequence(const int first_digit)
354 assert(is_octal_digit(first_digit));
355 int value = digit_value(first_digit);
356 if (!is_octal_digit(CC)) return value;
357 value = 8 * value + digit_value(CC);
359 if (!is_octal_digit(CC)) return value;
360 value = 8 * value + digit_value(CC);
364 return (signed char) value;
366 return (unsigned char) value;
371 * Parses a hex character sequence.
373 static int parse_hex_sequence(void)
376 while(isxdigit(CC)) {
377 value = 16 * value + digit_value(CC);
382 return (signed char) value;
384 return (unsigned char) value;
389 * Parse an escape sequence.
391 static int parse_escape_sequence(void)
399 case '"': return '"';
400 case '\'': return '\'';
401 case '\\': return '\\';
402 case '?': return '\?';
403 case 'a': return '\a';
404 case 'b': return '\b';
405 case 'f': return '\f';
406 case 'n': return '\n';
407 case 'r': return '\r';
408 case 't': return '\t';
409 case 'v': return '\v';
411 return parse_hex_sequence();
420 return parse_octal_sequence(ec);
422 parse_error("reached end of file while parsing escape sequence");
425 parse_error("unknown escape sequence");
430 static void parse_string_literal(void)
432 const unsigned start_linenr = input.position.lineno;
440 if(resolve_escape_sequences) {
441 tc = parse_escape_sequence();
442 obstack_1grow(&symbol_obstack, (char) tc);
444 obstack_1grow(&symbol_obstack, (char) CC);
446 obstack_1grow(&symbol_obstack, (char) CC);
452 source_position_t source_position;
453 source_position.input_name = pp_token.source_position.input_name;
454 source_position.lineno = start_linenr;
455 errorf(&source_position, "string has no end");
456 pp_token.type = TP_ERROR;
465 obstack_1grow(&symbol_obstack, (char) CC);
472 /* add finishing 0 to the string */
473 obstack_1grow(&symbol_obstack, '\0');
474 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
475 const char *const string = obstack_finish(&symbol_obstack);
477 #if 0 /* TODO hash */
478 /* check if there is already a copy of the string */
479 result = strset_insert(&stringset, string);
480 if(result != string) {
481 obstack_free(&symbol_obstack, string);
484 const char *const result = string;
487 pp_token.type = TP_STRING_LITERAL;
488 pp_token.literal.begin = result;
489 pp_token.literal.size = size;
492 static void parse_wide_character_constant(void)
500 found_char = parse_escape_sequence();
504 parse_error("newline while parsing character constant");
510 goto end_of_wide_char_constant;
513 parse_error("EOF while parsing character constant");
514 pp_token.type = TP_ERROR;
518 if(found_char != 0) {
519 parse_error("more than 1 characters in character "
521 goto end_of_wide_char_constant;
530 end_of_wide_char_constant:
531 pp_token.type = TP_WIDE_CHARACTER_CONSTANT;
535 static void parse_character_constant(void)
537 const unsigned start_linenr = input.position.lineno;
545 tc = parse_escape_sequence();
546 obstack_1grow(&symbol_obstack, (char) tc);
550 parse_error("newline while parsing character constant");
555 source_position_t source_position;
556 source_position.input_name = pp_token.source_position.input_name;
557 source_position.lineno = start_linenr;
558 errorf(&source_position, "EOF while parsing character constant");
559 pp_token.type = TP_ERROR;
565 goto end_of_char_constant;
568 obstack_1grow(&symbol_obstack, (char) CC);
575 end_of_char_constant:;
576 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
577 const char *const string = obstack_finish(&symbol_obstack);
579 pp_token.type = TP_CHARACTER_CONSTANT;
580 pp_token.literal.begin = string;
581 pp_token.literal.size = size;
584 #define SYMBOL_CHARS_WITHOUT_E_P \
635 #define SYMBOL_CHARS \
636 SYMBOL_CHARS_WITHOUT_E_P \
655 * returns next final token from a preprocessor macro expansion
657 static void expand_next(void)
659 assert(current_expansion != NULL);
661 pp_definition_t *definition = current_expansion;
664 if(definition->list_len == 0
665 || definition->expand_pos >= definition->list_len) {
666 /* we're finished with the current macro, move up 1 level in the
668 pp_definition_t *parent = definition->parent_expansion;
669 definition->parent_expansion = NULL;
670 definition->is_expanding = false;
672 /* it was the outermost expansion, parse normal pptoken */
674 current_expansion = NULL;
675 next_preprocessing_token();
679 current_expansion = definition;
682 pp_token = definition->token_list[definition->expand_pos];
683 ++definition->expand_pos;
685 if(pp_token.type != TP_IDENTIFIER)
688 /* if it was an identifier then we might need to expand again */
689 pp_definition_t *symbol_definition = pp_token.symbol->pp_definition;
690 if(symbol_definition != NULL && !symbol_definition->is_expanding) {
691 symbol_definition->parent_expansion = definition;
692 symbol_definition->expand_pos = 0;
693 symbol_definition->is_expanding = true;
694 definition = symbol_definition;
695 current_expansion = definition;
700 static void skip_line_comment(void)
721 static void skip_multiline_comment(void)
726 unsigned start_linenr = input.position.lineno;
732 /* TODO: nested comment, warn here */
744 if(do_print_spaces) {
752 source_position_t source_position;
753 source_position.input_name = pp_token.source_position.input_name;
754 source_position.lineno = start_linenr;
755 errorf(&source_position, "at end of file while looking for comment end");
766 /* skip spaces advancing at the start of the next preprocessing token */
767 static void skip_spaces(bool skip_newline)
783 } else if (CC == '*') {
785 skip_multiline_comment();
801 ++input.position.lineno;
811 ++input.position.lineno;
822 static void eat_pp(int type)
825 assert(pp_token.type == type);
826 next_preprocessing_token();
829 static void parse_symbol(void)
831 obstack_1grow(&symbol_obstack, (char) CC);
838 obstack_1grow(&symbol_obstack, (char) CC);
848 obstack_1grow(&symbol_obstack, '\0');
849 char *string = obstack_finish(&symbol_obstack);
851 /* might be a wide string or character constant ( L"string"/L'c' ) */
852 if (CC == '"' && string[0] == 'L' && string[1] == '\0') {
853 obstack_free(&symbol_obstack, string);
856 } else if (CC == '\'' && string[0] == 'L' && string[1] == '\0') {
857 obstack_free(&symbol_obstack, string);
858 parse_wide_character_constant();
862 symbol_t *symbol = symbol_table_insert(string);
864 pp_token.type = symbol->pp_ID;
865 pp_token.symbol = symbol;
867 /* we can free the memory from symbol obstack if we already had an entry in
868 * the symbol table */
869 if (symbol->string != string) {
870 obstack_free(&symbol_obstack, string);
875 pp_definition_t *pp_definition = symbol->pp_definition;
876 if (pp_definition == NULL)
879 if (pp_definition->has_parameters) {
881 /* no opening brace -> no expansion */
884 next_preprocessing_token();
887 /* parse arguments (TODO) */
888 while (pp_token.type != TP_EOF && pp_token.type != ')')
889 next_preprocessing_token();
890 next_preprocessing_token();
893 pp_definition->expand_pos = 0;
894 pp_definition->is_expanding = true,
895 current_expansion = pp_definition;
899 static void parse_number(void)
901 obstack_1grow(&symbol_obstack, (char) CC);
908 SYMBOL_CHARS_WITHOUT_E_P
909 obstack_1grow(&symbol_obstack, (char) CC);
917 obstack_1grow(&symbol_obstack, (char) CC);
919 if(CC == '+' || CC == '-') {
920 obstack_1grow(&symbol_obstack, (char) CC);
931 obstack_1grow(&symbol_obstack, '\0');
932 size_t size = obstack_object_size(&symbol_obstack);
933 char *string = obstack_finish(&symbol_obstack);
935 pp_token.type = TP_NUMBER;
936 pp_token.literal.begin = string;
937 pp_token.literal.size = size;
941 #define MAYBE_PROLOG \
946 #define MAYBE(ch, set_type) \
949 pp_token.type = set_type; \
952 #define ELSE_CODE(code) \
957 } /* end of while(1) */ \
959 #define ELSE(set_type) \
961 pp_token.type = set_type; \
964 static void next_preprocessing_token(void)
966 if(current_expansion != NULL) {
971 pp_token.source_position = input.position;
985 pp_token.type = '\n';
998 parse_string_literal();
1002 parse_character_constant();
1024 MAYBE('.', TP_DOTDOTDOT)
1028 pp_token.type = '.';
1033 MAYBE('&', TP_ANDAND)
1034 MAYBE('=', TP_ANDEQUAL)
1038 MAYBE('=', TP_ASTERISKEQUAL)
1042 MAYBE('+', TP_PLUSPLUS)
1043 MAYBE('=', TP_PLUSEQUAL)
1047 MAYBE('>', TP_MINUSGREATER)
1048 MAYBE('-', TP_MINUSMINUS)
1049 MAYBE('=', TP_MINUSEQUAL)
1053 MAYBE('=', TP_EXCLAMATIONMARKEQUAL)
1057 MAYBE('=', TP_SLASHEQUAL)
1060 skip_multiline_comment();
1064 skip_line_comment();
1070 MAYBE('=', TP_PERCENTEQUAL)
1075 MAYBE(':', TP_HASHHASH)
1079 pp_token.type = '#';
1087 MAYBE('=', TP_LESSEQUAL)
1090 MAYBE('=', TP_LESSLESSEQUAL)
1095 MAYBE('=', TP_GREATEREQUAL)
1098 MAYBE('=', TP_GREATERGREATEREQUAL)
1099 ELSE(TP_GREATERGREATER)
1103 MAYBE('=', TP_CARETEQUAL)
1107 MAYBE('=', TP_PIPEEQUAL)
1108 MAYBE('|', TP_PIPEPIPE)
1116 MAYBE('=', TP_EQUALEQUAL)
1120 MAYBE('#', TP_HASHHASH)
1139 if (input_stack != NULL) {
1141 pop_restore_input();
1142 counted_newlines = 0;
1144 /* hack to output correct line number */
1145 print_line_directive(&input.position, "2");
1146 next_preprocessing_token();
1148 pp_token.type = TP_EOF;
1154 errorf(&pp_token.source_position, "unknown character '%c' found\n", CC);
1155 pp_token.type = TP_ERROR;
1160 static void print_quoted_string(const char *const string)
1163 for (const char *c = string; *c != 0; ++c) {
1165 case '"': fputs("\\\"", out); break;
1166 case '\\': fputs("\\\\", out); break;
1167 case '\a': fputs("\\a", out); break;
1168 case '\b': fputs("\\b", out); break;
1169 case '\f': fputs("\\f", out); break;
1170 case '\n': fputs("\\n", out); break;
1171 case '\r': fputs("\\r", out); break;
1172 case '\t': fputs("\\t", out); break;
1173 case '\v': fputs("\\v", out); break;
1174 case '\?': fputs("\\?", out); break;
1177 fprintf(out, "\\%03o", (unsigned)*c);
1187 static void print_line_directive(const source_position_t *pos, const char *add)
1189 fprintf(out, "# %u ", pos->lineno);
1190 print_quoted_string(pos->input_name);
1197 printed_input_name = pos->input_name;
1200 static void print_spaces(void)
1202 if (counted_newlines >= 9) {
1203 if (input.had_non_space) {
1206 print_line_directive(&pp_token.source_position, NULL);
1207 counted_newlines = 0;
1209 for (unsigned i = 0; i < counted_newlines; ++i)
1211 counted_newlines = 0;
1213 for (unsigned i = 0; i < counted_spaces; ++i)
1218 static void emit_pp_token(void)
1223 if (pp_token.type != '\n') {
1225 input.had_non_space = true;
1228 switch(pp_token.type) {
1230 fputs(pp_token.symbol->string, out);
1233 fputs(pp_token.literal.begin, out);
1235 case TP_STRING_LITERAL:
1237 fputs(pp_token.literal.begin, out);
1243 print_pp_token_type(out, pp_token.type);
1248 static void eat_pp_directive(void)
1250 while(pp_token.type != '\n' && pp_token.type != TP_EOF) {
1251 next_preprocessing_token();
1255 static bool strings_equal(const string_t *string1, const string_t *string2)
1257 size_t size = string1->size;
1258 if(size != string2->size)
1261 const char *c1 = string1->begin;
1262 const char *c2 = string2->begin;
1263 for(size_t i = 0; i < size; ++i, ++c1, ++c2) {
1270 static bool pp_tokens_equal(const token_t *token1, const token_t *token2)
1272 if(token1->type != token2->type)
1275 switch(token1->type) {
1280 return token1->symbol == token2->symbol;
1282 case TP_CHARACTER_CONSTANT:
1283 case TP_STRING_LITERAL:
1284 return strings_equal(&token1->literal, &token2->literal);
1291 static bool pp_definitions_equal(const pp_definition_t *definition1,
1292 const pp_definition_t *definition2)
1294 if(definition1->list_len != definition2->list_len)
1297 size_t len = definition1->list_len;
1298 const token_t *t1 = definition1->token_list;
1299 const token_t *t2 = definition2->token_list;
1300 for(size_t i = 0; i < len; ++i, ++t1, ++t2) {
1301 if(!pp_tokens_equal(t1, t2))
1307 static void parse_define_directive(void)
1310 assert(obstack_object_size(&pp_obstack) == 0);
1312 if (pp_token.type != TP_IDENTIFIER) {
1313 errorf(&pp_token.source_position,
1314 "expected identifier after #define, got '%t'", &pp_token);
1317 symbol_t *symbol = pp_token.symbol;
1319 pp_definition_t *new_definition
1320 = obstack_alloc(&pp_obstack, sizeof(new_definition[0]));
1321 memset(new_definition, 0, sizeof(new_definition[0]));
1322 new_definition->source_position = input.position;
1324 /* this is probably the only place where spaces are significant in the
1325 * lexer (except for the fact that they separate tokens). #define b(x)
1326 * is something else than #define b (x) */
1329 next_preprocessing_token();
1330 /* get next token after '(' */
1331 next_preprocessing_token();
1334 switch (pp_token.type) {
1336 new_definition->is_variadic = true;
1337 next_preprocessing_token();
1338 if (pp_token.type != ')') {
1339 errorf(&input.position,
1340 "'...' not at end of macro argument list");
1345 obstack_ptr_grow(&pp_obstack, pp_token.symbol);
1346 next_preprocessing_token();
1348 if (pp_token.type == ',') {
1349 next_preprocessing_token();
1353 if (pp_token.type != ')') {
1354 errorf(&pp_token.source_position,
1355 "expected ',' or ')' after identifier, got '%t'",
1361 next_preprocessing_token();
1362 goto finish_argument_list;
1364 errorf(&pp_token.source_position,
1365 "expected identifier, '...' or ')' in #define argument list, got '%t'",
1371 finish_argument_list:
1372 new_definition->has_parameters = true;
1373 new_definition->n_parameters
1374 = obstack_object_size(&pp_obstack) / sizeof(new_definition->parameters[0]);
1375 new_definition->parameters = obstack_finish(&pp_obstack);
1377 next_preprocessing_token();
1380 /* construct a new pp_definition on the obstack */
1381 assert(obstack_object_size(&pp_obstack) == 0);
1382 size_t list_len = 0;
1383 while (pp_token.type != '\n' && pp_token.type != TP_EOF) {
1384 obstack_grow(&pp_obstack, &pp_token, sizeof(pp_token));
1386 next_preprocessing_token();
1389 new_definition->list_len = list_len;
1390 new_definition->token_list = obstack_finish(&pp_obstack);
1392 pp_definition_t *old_definition = symbol->pp_definition;
1393 if (old_definition != NULL) {
1394 if (!pp_definitions_equal(old_definition, new_definition)) {
1395 warningf(&input.position, "multiple definition of macro '%Y' (first defined %P)",
1396 symbol, &old_definition->source_position);
1398 /* reuse the old definition */
1399 obstack_free(&pp_obstack, new_definition);
1400 new_definition = old_definition;
1404 symbol->pp_definition = new_definition;
1408 if (obstack_object_size(&pp_obstack) > 0) {
1409 char *ptr = obstack_finish(&pp_obstack);
1410 obstack_free(&pp_obstack, ptr);
1415 static void parse_undef_directive(void)
1419 if(pp_token.type != TP_IDENTIFIER) {
1420 errorf(&input.position,
1421 "expected identifier after #undef, got '%t'", &pp_token);
1426 symbol_t *symbol = pp_token.symbol;
1427 symbol->pp_definition = NULL;
1428 next_preprocessing_token();
1430 if(pp_token.type != '\n') {
1431 warningf(&input.position, "extra tokens at end of #undef directive");
1433 /* eat until '\n' */
1437 static const char *parse_headername(void)
1439 /* behind an #include we can have the special headername lexems.
1440 * They're only allowed behind an #include so they're not recognized
1441 * by the normal next_preprocessing_token. We handle them as a special
1444 /* skip spaces so we reach start of next preprocessing token */
1447 assert(obstack_object_size(&input_obstack) == 0);
1449 /* check wether we have a "... or <... headername */
1452 /* for now until we have proper searchpath handling */
1453 obstack_1grow(&input_obstack, '.');
1454 obstack_1grow(&input_obstack, '/');
1462 parse_error("header name without closing '>'");
1467 goto finished_headername;
1469 obstack_1grow(&input_obstack, (char) CC);
1472 /* we should never be here */
1475 /* for now until we have proper searchpath handling */
1476 obstack_1grow(&input_obstack, '.');
1477 obstack_1grow(&input_obstack, '/');
1485 parse_error("header name without closing '>'");
1490 goto finished_headername;
1492 obstack_1grow(&input_obstack, (char) CC);
1495 /* we should never be here */
1498 /* TODO: do normale pp_token parsing and concatenate results */
1499 panic("pp_token concat include not implemented yet");
1502 finished_headername:
1503 obstack_1grow(&input_obstack, '\0');
1504 char *headername = obstack_finish(&input_obstack);
1506 /* TODO: iterate search-path to find the file */
1508 next_preprocessing_token();
1513 static bool parse_include_directive(void)
1515 /* don't eat the TP_include here!
1516 * we need an alternative parsing for the next token */
1520 const char *headername = parse_headername();
1521 if (headername == NULL) {
1526 if (pp_token.type != '\n' && pp_token.type != TP_EOF) {
1527 warningf(&pp_token.source_position,
1528 "extra tokens at end of #include directive");
1532 if (n_inputs > INCLUDE_LIMIT) {
1533 errorf(&pp_token.source_position, "#include nested too deeply");
1535 next_preprocessing_token();
1539 /* we have to reenable space counting and macro expansion here,
1540 * because it is still disabled in directive parsing,
1541 * but we will trigger a preprocessing token reading of the new file
1542 * now and need expansions/space counting */
1543 do_print_spaces = true;
1544 do_expansions = true;
1548 bool res = open_input(headername);
1550 errorf(&pp_token.source_position,
1551 "failed including '%s': %s", headername, strerror(errno));
1552 pop_restore_input();
1559 static pp_conditional_t *push_conditional(void)
1561 pp_conditional_t *conditional
1562 = obstack_alloc(&pp_obstack, sizeof(*conditional));
1563 memset(conditional, 0, sizeof(*conditional));
1565 conditional->parent = conditional_stack;
1566 conditional_stack = conditional;
1571 static void pop_conditional(void)
1573 assert(conditional_stack != NULL);
1574 conditional_stack = conditional_stack->parent;
1577 static void check_unclosed_conditionals(void)
1579 while (conditional_stack != NULL) {
1580 pp_conditional_t *conditional = conditional_stack;
1582 if (conditional->in_else) {
1583 errorf(&conditional->source_position, "unterminated #else");
1585 errorf(&conditional->source_position, "unterminated condition");
1591 static void parse_ifdef_ifndef_directive(void)
1593 bool is_ifndef = (pp_token.type == TP_ifndef);
1595 next_preprocessing_token();
1599 pp_conditional_t *conditional = push_conditional();
1600 conditional->source_position = pp_token.source_position;
1601 conditional->skip = true;
1605 if (pp_token.type != TP_IDENTIFIER) {
1606 errorf(&pp_token.source_position,
1607 "expected identifier after #%s, got '%t'",
1608 is_ifndef ? "ifndef" : "ifdef", &pp_token);
1611 /* just take the true case in the hope to avoid further errors */
1614 symbol_t *symbol = pp_token.symbol;
1615 pp_definition_t *pp_definition = symbol->pp_definition;
1616 next_preprocessing_token();
1618 if (pp_token.type != '\n') {
1619 errorf(&pp_token.source_position,
1620 "extra tokens at end of #%s",
1621 is_ifndef ? "ifndef" : "ifdef");
1625 /* evaluate wether we are in true or false case */
1626 condition = is_ifndef ? pp_definition == NULL : pp_definition != NULL;
1629 pp_conditional_t *conditional = push_conditional();
1630 conditional->source_position = pp_token.source_position;
1631 conditional->condition = condition;
1638 static void parse_else_directive(void)
1642 if (pp_token.type != '\n') {
1644 warningf(&pp_token.source_position, "extra tokens at end of #else");
1649 pp_conditional_t *conditional = conditional_stack;
1650 if (conditional == NULL) {
1651 errorf(&pp_token.source_position, "#else without prior #if");
1655 if (conditional->in_else) {
1656 errorf(&pp_token.source_position,
1657 "#else after #else (condition started %P)",
1658 conditional->source_position);
1663 conditional->in_else = true;
1664 if (!conditional->skip) {
1665 skip_mode = conditional->condition;
1667 conditional->source_position = pp_token.source_position;
1670 static void parse_endif_directive(void)
1674 if (pp_token.type != '\n') {
1676 warningf(&pp_token.source_position,
1677 "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 do_print_spaces = false;
1697 do_expansions = false;
1701 switch(pp_token.type) {
1704 parse_ifdef_ifndef_directive();
1707 parse_else_directive();
1710 parse_endif_directive();
1717 switch(pp_token.type) {
1719 parse_define_directive();
1722 parse_undef_directive();
1726 parse_ifdef_ifndef_directive();
1729 parse_else_directive();
1732 parse_endif_directive();
1735 bool in_new_source = parse_include_directive();
1736 /* no need to do anything if source file switched */
1742 /* the nop directive */
1745 errorf(&pp_token.source_position,
1746 "invalid preprocessing directive #%t", &pp_token);
1752 do_print_spaces = true;
1753 do_expansions = true;
1756 assert(pp_token.type == '\n' || pp_token.type == TP_EOF);
1757 next_preprocessing_token();
1760 #define GCC_COMPAT_MODE
1762 int pptest_main(int argc, char **argv);
1763 int pptest_main(int argc, char **argv)
1765 init_symbol_table();
1768 obstack_init(&pp_obstack);
1769 obstack_init(&input_obstack);
1771 const char *filename = "t.c";
1777 #ifdef GCC_COMPAT_MODE
1778 /* this is here so we can directly compare "gcc -E" output and our output */
1779 fprintf(out, "# 1 \"%s\"\n", filename);
1780 fputs("# 1 \"<built-in>\"\n", out);
1781 fputs("# 1 \"<command-line>\"\n", out);
1784 bool ok = open_input(filename);
1788 /* we're at a line begin */
1789 if(pp_token.type == '#') {
1790 parse_preprocessing_directive();
1792 /* parse+emit a line */
1793 while(pp_token.type != '\n') {
1794 if(pp_token.type == TP_EOF)
1795 goto end_of_main_loop;
1797 next_preprocessing_token();
1800 next_preprocessing_token();
1805 check_unclosed_conditionals();
1808 obstack_free(&input_obstack, NULL);
1809 obstack_free(&pp_obstack, NULL);
1812 exit_symbol_table();