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)
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.linenr;
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.linenr = 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.linenr;
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.linenr = 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.linenr;
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.linenr = 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.linenr;
811 ++input.position.linenr;
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) \
956 } /* end of while(1) */ \
959 #define ELSE(set_type) \
961 pp_token.type = set_type; \
965 static void next_preprocessing_token(void)
967 if(current_expansion != NULL) {
972 pp_token.source_position = input.position;
986 pp_token.type = '\n';
999 parse_string_literal();
1003 parse_character_constant();
1025 MAYBE('.', TP_DOTDOTDOT)
1029 pp_token.type = '.';
1035 MAYBE('&', TP_ANDAND)
1036 MAYBE('=', TP_ANDEQUAL)
1040 MAYBE('=', TP_ASTERISKEQUAL)
1044 MAYBE('+', TP_PLUSPLUS)
1045 MAYBE('=', TP_PLUSEQUAL)
1049 MAYBE('>', TP_MINUSGREATER)
1050 MAYBE('-', TP_MINUSMINUS)
1051 MAYBE('=', TP_MINUSEQUAL)
1055 MAYBE('=', TP_EXCLAMATIONMARKEQUAL)
1059 MAYBE('=', TP_SLASHEQUAL)
1062 skip_multiline_comment();
1066 skip_line_comment();
1072 MAYBE('=', TP_PERCENTEQUAL)
1077 MAYBE(':', TP_HASHHASH)
1081 pp_token.type = '#';
1090 MAYBE('=', TP_LESSEQUAL)
1093 MAYBE('=', TP_LESSLESSEQUAL)
1098 MAYBE('=', TP_GREATEREQUAL)
1101 MAYBE('=', TP_GREATERGREATEREQUAL)
1102 ELSE(TP_GREATERGREATER)
1106 MAYBE('=', TP_CARETEQUAL)
1110 MAYBE('=', TP_PIPEEQUAL)
1111 MAYBE('|', TP_PIPEPIPE)
1119 MAYBE('=', TP_EQUALEQUAL)
1123 MAYBE('#', TP_HASHHASH)
1142 if (input_stack != NULL) {
1144 pop_restore_input();
1145 counted_newlines = 0;
1147 /* hack to output correct line number */
1148 print_line_directive(&input.position, "2");
1149 next_preprocessing_token();
1151 pp_token.type = TP_EOF;
1157 errorf(&pp_token.source_position, "unknown character '%c' found\n", CC);
1158 pp_token.type = TP_ERROR;
1163 static void print_quoted_string(const char *const string)
1166 for (const char *c = string; *c != 0; ++c) {
1168 case '"': fputs("\\\"", out); break;
1169 case '\\': fputs("\\\\", out); break;
1170 case '\a': fputs("\\a", out); break;
1171 case '\b': fputs("\\b", out); break;
1172 case '\f': fputs("\\f", out); break;
1173 case '\n': fputs("\\n", out); break;
1174 case '\r': fputs("\\r", out); break;
1175 case '\t': fputs("\\t", out); break;
1176 case '\v': fputs("\\v", out); break;
1177 case '\?': fputs("\\?", out); break;
1180 fprintf(out, "\\%03o", *c);
1190 static void print_line_directive(const source_position_t *pos, const char *add)
1192 fprintf(out, "# %u ", pos->linenr);
1193 print_quoted_string(pos->input_name);
1200 printed_input_name = pos->input_name;
1203 static void print_spaces(void)
1205 if (counted_newlines >= 9) {
1206 if (input.had_non_space) {
1209 print_line_directive(&pp_token.source_position, NULL);
1210 counted_newlines = 0;
1212 for (unsigned i = 0; i < counted_newlines; ++i)
1214 counted_newlines = 0;
1216 for (unsigned i = 0; i < counted_spaces; ++i)
1221 static void emit_pp_token(void)
1226 if (pp_token.type != '\n') {
1228 input.had_non_space = true;
1231 switch(pp_token.type) {
1233 fputs(pp_token.symbol->string, out);
1236 fputs(pp_token.literal.begin, out);
1238 case TP_STRING_LITERAL:
1240 fputs(pp_token.literal.begin, out);
1246 print_pp_token_type(out, pp_token.type);
1251 static void eat_pp_directive(void)
1253 while(pp_token.type != '\n' && pp_token.type != TP_EOF) {
1254 next_preprocessing_token();
1258 static bool strings_equal(const string_t *string1, const string_t *string2)
1260 size_t size = string1->size;
1261 if(size != string2->size)
1264 const char *c1 = string1->begin;
1265 const char *c2 = string2->begin;
1266 for(size_t i = 0; i < size; ++i, ++c1, ++c2) {
1273 static bool pp_tokens_equal(const token_t *token1, const token_t *token2)
1275 if(token1->type != token2->type)
1278 switch(token1->type) {
1283 return token1->symbol == token2->symbol;
1285 case TP_CHARACTER_CONSTANT:
1286 case TP_STRING_LITERAL:
1287 return strings_equal(&token1->literal, &token2->literal);
1294 static bool pp_definitions_equal(const pp_definition_t *definition1,
1295 const pp_definition_t *definition2)
1297 if(definition1->list_len != definition2->list_len)
1300 size_t len = definition1->list_len;
1301 const token_t *t1 = definition1->token_list;
1302 const token_t *t2 = definition2->token_list;
1303 for(size_t i = 0; i < len; ++i, ++t1, ++t2) {
1304 if(!pp_tokens_equal(t1, t2))
1310 static void parse_define_directive(void)
1313 assert(obstack_object_size(&pp_obstack) == 0);
1315 if (pp_token.type != TP_IDENTIFIER) {
1316 errorf(&pp_token.source_position,
1317 "expected identifier after #define, got '%t'", &pp_token);
1320 symbol_t *symbol = pp_token.symbol;
1322 pp_definition_t *new_definition
1323 = obstack_alloc(&pp_obstack, sizeof(new_definition[0]));
1324 memset(new_definition, 0, sizeof(new_definition[0]));
1325 new_definition->source_position = input.position;
1327 /* this is probably the only place where spaces are significant in the
1328 * lexer (except for the fact that they separate tokens). #define b(x)
1329 * is something else than #define b (x) */
1332 next_preprocessing_token();
1333 /* get next token after '(' */
1334 next_preprocessing_token();
1337 switch (pp_token.type) {
1339 new_definition->is_variadic = true;
1340 next_preprocessing_token();
1341 if (pp_token.type != ')') {
1342 errorf(&input.position,
1343 "'...' not at end of macro argument list");
1348 obstack_ptr_grow(&pp_obstack, pp_token.symbol);
1349 next_preprocessing_token();
1351 if (pp_token.type == ',') {
1352 next_preprocessing_token();
1356 if (pp_token.type != ')') {
1357 errorf(&pp_token.source_position,
1358 "expected ',' or ')' after identifier, got '%t'",
1364 next_preprocessing_token();
1365 goto finish_argument_list;
1367 errorf(&pp_token.source_position,
1368 "expected identifier, '...' or ')' in #define argument list, got '%t'",
1374 finish_argument_list:
1375 new_definition->has_parameters = true;
1376 new_definition->n_parameters
1377 = obstack_object_size(&pp_obstack) / sizeof(new_definition->parameters[0]);
1378 new_definition->parameters = obstack_finish(&pp_obstack);
1380 next_preprocessing_token();
1383 /* construct a new pp_definition on the obstack */
1384 assert(obstack_object_size(&pp_obstack) == 0);
1385 size_t list_len = 0;
1386 while (pp_token.type != '\n' && pp_token.type != TP_EOF) {
1387 obstack_grow(&pp_obstack, &pp_token, sizeof(pp_token));
1389 next_preprocessing_token();
1392 new_definition->list_len = list_len;
1393 new_definition->token_list = obstack_finish(&pp_obstack);
1395 pp_definition_t *old_definition = symbol->pp_definition;
1396 if (old_definition != NULL) {
1397 if (!pp_definitions_equal(old_definition, new_definition)) {
1398 warningf(&input.position, "multiple definition of macro '%Y' (first defined %P)",
1399 symbol, &old_definition->source_position);
1401 /* reuse the old definition */
1402 obstack_free(&pp_obstack, new_definition);
1403 new_definition = old_definition;
1407 symbol->pp_definition = new_definition;
1411 if (obstack_object_size(&pp_obstack) > 0) {
1412 char *ptr = obstack_finish(&pp_obstack);
1413 obstack_free(&pp_obstack, ptr);
1418 static void parse_undef_directive(void)
1422 if(pp_token.type != TP_IDENTIFIER) {
1423 errorf(&input.position,
1424 "expected identifier after #undef, got '%t'", &pp_token);
1429 symbol_t *symbol = pp_token.symbol;
1430 symbol->pp_definition = NULL;
1431 next_preprocessing_token();
1433 if(pp_token.type != '\n') {
1434 warningf(&input.position, "extra tokens at end of #undef directive");
1436 /* eat until '\n' */
1440 static const char *parse_headername(void)
1442 /* behind an #include we can have the special headername lexems.
1443 * They're only allowed behind an #include so they're not recognized
1444 * by the normal next_preprocessing_token. We handle them as a special
1447 /* skip spaces so we reach start of next preprocessing token */
1450 assert(obstack_object_size(&input_obstack) == 0);
1452 /* check wether we have a "... or <... headername */
1455 /* for now until we have proper searchpath handling */
1456 obstack_1grow(&input_obstack, '.');
1457 obstack_1grow(&input_obstack, '/');
1465 parse_error("header name without closing '>'");
1470 goto finished_headername;
1472 obstack_1grow(&input_obstack, (char) CC);
1475 /* we should never be here */
1478 /* for now until we have proper searchpath handling */
1479 obstack_1grow(&input_obstack, '.');
1480 obstack_1grow(&input_obstack, '/');
1488 parse_error("header name without closing '>'");
1493 goto finished_headername;
1495 obstack_1grow(&input_obstack, (char) CC);
1498 /* we should never be here */
1501 /* TODO: do normale pp_token parsing and concatenate results */
1502 panic("pp_token concat include not implemented yet");
1505 finished_headername:
1506 obstack_1grow(&input_obstack, '\0');
1507 char *headername = obstack_finish(&input_obstack);
1509 /* TODO: iterate search-path to find the file */
1511 next_preprocessing_token();
1516 static bool parse_include_directive(void)
1518 /* don't eat the TP_include here!
1519 * we need an alternative parsing for the next token */
1523 const char *headername = parse_headername();
1524 if (headername == NULL) {
1529 if (pp_token.type != '\n' && pp_token.type != TP_EOF) {
1530 warningf(&pp_token.source_position,
1531 "extra tokens at end of #include directive");
1535 if (n_inputs > INCLUDE_LIMIT) {
1536 errorf(&pp_token.source_position, "#include nested too deeply");
1538 next_preprocessing_token();
1542 /* we have to reenable space counting and macro expansion here,
1543 * because it is still disabled in directive parsing,
1544 * but we will trigger a preprocessing token reading of the new file
1545 * now and need expansions/space counting */
1546 do_print_spaces = true;
1547 do_expansions = true;
1551 bool res = open_input(headername);
1553 errorf(&pp_token.source_position,
1554 "failed including '%s': %s", headername, strerror(errno));
1555 pop_restore_input();
1562 static pp_conditional_t *push_conditional(void)
1564 pp_conditional_t *conditional
1565 = obstack_alloc(&pp_obstack, sizeof(*conditional));
1566 memset(conditional, 0, sizeof(*conditional));
1568 conditional->parent = conditional_stack;
1569 conditional_stack = conditional;
1574 static void pop_conditional(void)
1576 assert(conditional_stack != NULL);
1577 conditional_stack = conditional_stack->parent;
1580 static void check_unclosed_conditionals(void)
1582 while (conditional_stack != NULL) {
1583 pp_conditional_t *conditional = conditional_stack;
1585 if (conditional->in_else) {
1586 errorf(&conditional->source_position, "unterminated #else");
1588 errorf(&conditional->source_position, "unterminated condition");
1594 static void parse_ifdef_ifndef_directive(void)
1596 bool is_ifndef = (pp_token.type == TP_ifndef);
1598 next_preprocessing_token();
1602 pp_conditional_t *conditional = push_conditional();
1603 conditional->source_position = pp_token.source_position;
1604 conditional->skip = true;
1608 if (pp_token.type != TP_IDENTIFIER) {
1609 errorf(&pp_token.source_position,
1610 "expected identifier after #%s, got '%t'",
1611 is_ifndef ? "ifndef" : "ifdef", &pp_token);
1614 /* just take the true case in the hope to avoid further errors */
1617 symbol_t *symbol = pp_token.symbol;
1618 pp_definition_t *pp_definition = symbol->pp_definition;
1619 next_preprocessing_token();
1621 if (pp_token.type != '\n') {
1622 errorf(&pp_token.source_position,
1623 "extra tokens at end of #%s",
1624 is_ifndef ? "ifndef" : "ifdef");
1628 /* evaluate wether we are in true or false case */
1629 condition = is_ifndef ? pp_definition == NULL : pp_definition != NULL;
1632 pp_conditional_t *conditional = push_conditional();
1633 conditional->source_position = pp_token.source_position;
1634 conditional->condition = condition;
1641 static void parse_else_directive(void)
1645 if (pp_token.type != '\n') {
1647 warningf(&pp_token.source_position, "extra tokens at end of #else");
1652 pp_conditional_t *conditional = conditional_stack;
1653 if (conditional == NULL) {
1654 errorf(&pp_token.source_position, "#else without prior #if");
1658 if (conditional->in_else) {
1659 errorf(&pp_token.source_position,
1660 "#else after #else (condition started %P)",
1661 conditional->source_position);
1666 conditional->in_else = true;
1667 if (!conditional->skip) {
1668 skip_mode = conditional->condition;
1670 conditional->source_position = pp_token.source_position;
1673 static void parse_endif_directive(void)
1677 if (pp_token.type != '\n') {
1679 warningf(&pp_token.source_position,
1680 "extra tokens at end of #endif");
1685 pp_conditional_t *conditional = conditional_stack;
1686 if (conditional == NULL) {
1687 errorf(&pp_token.source_position, "#endif without prior #if");
1691 if (!conditional->skip) {
1697 static void parse_preprocessing_directive(void)
1699 do_print_spaces = false;
1700 do_expansions = false;
1704 switch(pp_token.type) {
1707 parse_ifdef_ifndef_directive();
1710 parse_else_directive();
1713 parse_endif_directive();
1720 switch(pp_token.type) {
1722 parse_define_directive();
1725 parse_undef_directive();
1729 parse_ifdef_ifndef_directive();
1732 parse_else_directive();
1735 parse_endif_directive();
1738 bool in_new_source = parse_include_directive();
1739 /* no need to do anything if source file switched */
1745 /* the nop directive */
1748 errorf(&pp_token.source_position,
1749 "invalid preprocessing directive #%t", &pp_token);
1755 do_print_spaces = true;
1756 do_expansions = true;
1759 assert(pp_token.type == '\n' || pp_token.type == TP_EOF);
1760 next_preprocessing_token();
1763 #define GCC_COMPAT_MODE
1765 int pptest_main(int argc, char **argv);
1766 int pptest_main(int argc, char **argv)
1768 init_symbol_table();
1771 obstack_init(&pp_obstack);
1772 obstack_init(&input_obstack);
1774 const char *filename = "t.c";
1780 #ifdef GCC_COMPAT_MODE
1781 /* this is here so we can directly compare "gcc -E" output and our output */
1782 fprintf(out, "# 1 \"%s\"\n", filename);
1783 fputs("# 1 \"<built-in>\"\n", out);
1784 fputs("# 1 \"<command-line>\"\n", out);
1787 bool ok = open_input(filename);
1791 /* we're at a line begin */
1792 if(pp_token.type == '#') {
1793 parse_preprocessing_directive();
1795 /* parse+emit a line */
1796 while(pp_token.type != '\n') {
1797 if(pp_token.type == TP_EOF)
1798 goto end_of_main_loop;
1800 next_preprocessing_token();
1803 next_preprocessing_token();
1808 check_unclosed_conditionals();
1811 obstack_free(&input_obstack, NULL);
1812 obstack_free(&pp_obstack, NULL);
1815 exit_symbol_table();