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_definition_t {
23 source_position_t source_position;
24 pp_definition_t *parent_expansion;
27 bool is_expanding : 1;
28 size_t argument_count;
31 token_t *replacement_list;
34 typedef struct pp_conditional_t pp_conditional_t;
35 struct pp_conditional_t {
36 source_position_t source_position;
39 bool skip; /**< conditional in skip mode (then+else gets skipped) */
40 pp_conditional_t *parent;
43 typedef struct pp_input_t pp_input_t;
47 char buf[1024+MAX_PUTBACK];
50 source_position_t position;
58 static pp_input_t *input_stack;
59 static unsigned n_inputs;
60 static struct obstack input_obstack;
62 static pp_conditional_t *conditional_stack;
65 static bool resolve_escape_sequences = false;
66 static bool do_print_spaces = true;
67 static bool do_expansions;
68 static bool skip_mode;
70 static struct obstack pp_obstack;
71 static unsigned counted_newlines;
72 static unsigned counted_spaces;
73 static const char *printed_input_name = NULL;
74 static pp_definition_t *current_expansion = NULL;
76 static inline void next_char(void);
77 static void next_preprocessing_token(void);
78 static void print_line_directive(const source_position_t *pos, const char *add);
79 static void print_spaces(void);
81 static bool open_input(const char *filename)
83 FILE *file = fopen(filename, "r");
90 input.had_non_space = false;
91 input.position.input_name = filename;
92 input.position.linenr = 1;
94 /* indicate that we're at a new input */
95 print_line_directive(&input.position, input_stack != NULL ? "1" : NULL);
100 /* read first char and first token */
102 next_preprocessing_token();
107 static void close_input(void)
109 /* ensure we have a newline at EOF */
110 if (input.had_non_space) {
114 assert(input.file != NULL);
123 static void push_input(void)
125 pp_input_t *saved_input
126 = obstack_alloc(&input_obstack, sizeof(*saved_input));
128 memcpy(saved_input, &input, sizeof(*saved_input));
130 /* adjust buffer positions */
131 if (input.bufpos != NULL)
132 saved_input->bufpos = saved_input->buf + (input.bufpos - input.buf);
133 if (input.bufend != NULL)
134 saved_input->bufend = saved_input->buf + (input.bufend - input.buf);
136 saved_input->parent = input_stack;
137 input_stack = saved_input;
141 static void pop_restore_input(void)
143 assert(n_inputs > 0);
144 assert(input_stack != NULL);
146 pp_input_t *saved_input = input_stack;
148 memcpy(&input, saved_input, sizeof(input));
151 /* adjust buffer positions */
152 if (saved_input->bufpos != NULL)
153 input.bufpos = input.buf + (saved_input->bufpos - saved_input->buf);
154 if (saved_input->bufend != NULL)
155 input.bufend = input.buf + (saved_input->bufend - saved_input->buf);
157 input_stack = saved_input->parent;
158 obstack_free(&input_obstack, saved_input);
163 * Prints a parse error message at the current token.
165 * @param msg the error message
167 static void parse_error(const char *msg)
169 errorf(&pp_token.source_position, "%s", msg);
172 static inline void next_real_char(void)
174 assert(input.bufpos <= input.bufend);
175 if (input.bufpos >= input.bufend) {
176 size_t s = fread(input.buf + MAX_PUTBACK, 1,
177 sizeof(input.buf) - MAX_PUTBACK, input.file);
182 input.bufpos = input.buf + MAX_PUTBACK;
183 input.bufend = input.buf + MAX_PUTBACK + s;
185 CC = *input.bufpos++;
189 * Put a character back into the buffer.
191 * @param pc the character to put back
193 static inline void put_back(int pc)
195 assert(input.bufpos > input.buf);
196 *(--input.bufpos - input.buf + input.buf) = (char) pc;
199 printf("putback '%c'\n", pc);
203 #define MATCH_NEWLINE(code) \
209 ++input.position.linenr; \
213 ++input.position.linenr; \
216 #define eat(c_type) do { assert(CC == c_type); next_char(); } while(0)
218 static void maybe_concat_lines(void)
223 MATCH_NEWLINE(return;)
234 * Set c to the next input character, ie.
235 * after expanding trigraphs.
237 static inline void next_char(void)
241 /* filter trigraphs and concatenated lines */
242 if(UNLIKELY(CC == '\\')) {
243 maybe_concat_lines();
244 goto end_of_next_char;
247 if(LIKELY(CC != '?'))
248 goto end_of_next_char;
251 if(LIKELY(CC != '?')) {
254 goto end_of_next_char;
259 case '=': CC = '#'; break;
260 case '(': CC = '['; break;
261 case '/': CC = '\\'; maybe_concat_lines(); break;
262 case ')': CC = ']'; break;
263 case '\'': CC = '^'; break;
264 case '<': CC = '{'; break;
265 case '!': CC = '|'; break;
266 case '>': CC = '}'; break;
267 case '-': CC = '~'; break;
277 printf("nchar '%c'\n", CC);
284 * Returns true if the given char is a octal digit.
286 * @param char the character to check
288 static inline bool is_octal_digit(int chr)
306 * Returns the value of a digit.
307 * The only portable way to do it ...
309 static int digit_value(int digit) {
334 panic("wrong character given");
339 * Parses an octal character sequence.
341 * @param first_digit the already read first digit
343 static int parse_octal_sequence(const int first_digit)
345 assert(is_octal_digit(first_digit));
346 int value = digit_value(first_digit);
347 if (!is_octal_digit(CC)) return value;
348 value = 8 * value + digit_value(CC);
350 if (!is_octal_digit(CC)) return value;
351 value = 8 * value + digit_value(CC);
355 return (signed char) value;
357 return (unsigned char) value;
362 * Parses a hex character sequence.
364 static int parse_hex_sequence(void)
367 while(isxdigit(CC)) {
368 value = 16 * value + digit_value(CC);
373 return (signed char) value;
375 return (unsigned char) value;
380 * Parse an escape sequence.
382 static int parse_escape_sequence(void)
390 case '"': return '"';
391 case '\'': return '\'';
392 case '\\': return '\\';
393 case '?': return '\?';
394 case 'a': return '\a';
395 case 'b': return '\b';
396 case 'f': return '\f';
397 case 'n': return '\n';
398 case 'r': return '\r';
399 case 't': return '\t';
400 case 'v': return '\v';
402 return parse_hex_sequence();
411 return parse_octal_sequence(ec);
413 parse_error("reached end of file while parsing escape sequence");
416 parse_error("unknown escape sequence");
421 static void parse_string_literal(void)
423 const unsigned start_linenr = input.position.linenr;
431 if(resolve_escape_sequences) {
432 tc = parse_escape_sequence();
433 obstack_1grow(&symbol_obstack, (char) tc);
435 obstack_1grow(&symbol_obstack, (char) CC);
437 obstack_1grow(&symbol_obstack, (char) CC);
443 source_position_t source_position;
444 source_position.input_name = pp_token.source_position.input_name;
445 source_position.linenr = start_linenr;
446 errorf(&source_position, "string has no end");
447 pp_token.type = TP_ERROR;
456 obstack_1grow(&symbol_obstack, (char) CC);
463 /* add finishing 0 to the string */
464 obstack_1grow(&symbol_obstack, '\0');
465 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
466 const char *const string = obstack_finish(&symbol_obstack);
468 #if 0 /* TODO hash */
469 /* check if there is already a copy of the string */
470 result = strset_insert(&stringset, string);
471 if(result != string) {
472 obstack_free(&symbol_obstack, string);
475 const char *const result = string;
478 pp_token.type = TP_STRING_LITERAL;
479 pp_token.v.string.begin = result;
480 pp_token.v.string.size = size;
483 static void parse_wide_character_constant(void)
491 found_char = parse_escape_sequence();
495 parse_error("newline while parsing character constant");
501 goto end_of_wide_char_constant;
504 parse_error("EOF while parsing character constant");
505 pp_token.type = TP_ERROR;
509 if(found_char != 0) {
510 parse_error("more than 1 characters in character "
512 goto end_of_wide_char_constant;
521 end_of_wide_char_constant:
522 pp_token.type = TP_WIDE_CHARACTER_CONSTANT;
526 static void parse_wide_string_literal(void)
528 const unsigned start_linenr = input.position.linenr;
536 wchar_rep_t tc = parse_escape_sequence();
537 obstack_grow(&symbol_obstack, &tc, sizeof(tc));
542 source_position_t source_position;
543 source_position.input_name = pp_token.source_position.input_name;
544 source_position.linenr = start_linenr;
545 errorf(&source_position, "string has no end");
546 pp_token.type = TP_ERROR;
556 obstack_grow(&symbol_obstack, &tc, sizeof(tc));
564 /* add finishing 0 to the string */
565 static const wchar_rep_t nul = L'\0';
566 obstack_grow(&symbol_obstack, &nul, sizeof(nul));
569 = (size_t)obstack_object_size(&symbol_obstack) / sizeof(wchar_rep_t);
570 const wchar_rep_t *const string = obstack_finish(&symbol_obstack);
572 #if 0 /* TODO hash */
573 /* check if there is already a copy of the string */
574 const wchar_rep_t *const result = strset_insert(&stringset, string);
575 if(result != string) {
576 obstack_free(&symbol_obstack, string);
579 const wchar_rep_t *const result = string;
582 pp_token.type = TP_WIDE_STRING_LITERAL;
583 pp_token.v.wide_string.begin = result;
584 pp_token.v.wide_string.size = size;
587 static void parse_character_constant(void)
589 const unsigned start_linenr = input.position.linenr;
597 tc = parse_escape_sequence();
598 obstack_1grow(&symbol_obstack, (char) tc);
602 parse_error("newline while parsing character constant");
607 source_position_t source_position;
608 source_position.input_name = pp_token.source_position.input_name;
609 source_position.linenr = start_linenr;
610 errorf(&source_position, "EOF while parsing character constant");
611 pp_token.type = TP_ERROR;
617 goto end_of_char_constant;
620 obstack_1grow(&symbol_obstack, (char) CC);
627 end_of_char_constant:;
628 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
629 const char *const string = obstack_finish(&symbol_obstack);
631 pp_token.type = TP_CHARACTER_CONSTANT;
632 pp_token.v.string.begin = string;
633 pp_token.v.string.size = size;
636 #define SYMBOL_CHARS_WITHOUT_E_P \
687 #define SYMBOL_CHARS \
688 SYMBOL_CHARS_WITHOUT_E_P \
707 * returns next final token from a preprocessor macro expansion
709 static void expand_next(void)
711 assert(current_expansion != NULL);
713 pp_definition_t *definition = current_expansion;
716 if(definition->list_len == 0
717 || definition->expand_pos >= definition->list_len) {
718 /* we're finished with the current macro, move up 1 level in the
720 pp_definition_t *parent = definition->parent_expansion;
721 definition->parent_expansion = NULL;
722 definition->is_expanding = false;
724 /* it was the outermost expansion, parse normal pptoken */
726 current_expansion = NULL;
727 next_preprocessing_token();
731 current_expansion = definition;
734 pp_token = definition->replacement_list[definition->expand_pos];
735 ++definition->expand_pos;
737 if(pp_token.type != TP_IDENTIFIER)
740 /* if it was an identifier then we might need to expand again */
741 pp_definition_t *symbol_definition = pp_token.v.symbol->pp_definition;
742 if(symbol_definition != NULL && !symbol_definition->is_expanding) {
743 symbol_definition->parent_expansion = definition;
744 symbol_definition->expand_pos = 0;
745 symbol_definition->is_expanding = true;
746 definition = symbol_definition;
747 current_expansion = definition;
752 static void parse_symbol(void)
754 obstack_1grow(&symbol_obstack, (char) CC);
761 obstack_1grow(&symbol_obstack, (char) CC);
771 obstack_1grow(&symbol_obstack, '\0');
772 char *string = obstack_finish(&symbol_obstack);
774 /* might be a wide string or character constant ( L"string"/L'c' ) */
775 if(CC == '"' && string[0] == 'L' && string[1] == '\0') {
776 obstack_free(&symbol_obstack, string);
777 parse_wide_string_literal();
779 } else if(CC == '\'' && string[0] == 'L' && string[1] == '\0') {
780 obstack_free(&symbol_obstack, string);
781 parse_wide_character_constant();
785 symbol_t *symbol = symbol_table_insert(string);
787 pp_token.type = symbol->pp_ID;
788 pp_token.v.symbol = symbol;
790 /* we can free the memory from symbol obstack if we already had an entry in
791 * the symbol table */
792 if(symbol->string != string) {
793 obstack_free(&symbol_obstack, string);
796 pp_definition_t *pp_definition = symbol->pp_definition;
797 if(do_expansions && pp_definition != NULL) {
798 pp_definition->expand_pos = 0;
799 pp_definition->is_expanding = true,
800 current_expansion = pp_definition;
805 static void parse_number(void)
807 obstack_1grow(&symbol_obstack, (char) CC);
814 SYMBOL_CHARS_WITHOUT_E_P
815 obstack_1grow(&symbol_obstack, (char) CC);
823 obstack_1grow(&symbol_obstack, (char) CC);
825 if(CC == '+' || CC == '-') {
826 obstack_1grow(&symbol_obstack, (char) CC);
837 obstack_1grow(&symbol_obstack, '\0');
838 size_t size = obstack_object_size(&symbol_obstack);
839 char *string = obstack_finish(&symbol_obstack);
841 pp_token.type = TP_NUMBER;
842 pp_token.v.string.begin = string;
843 pp_token.v.string.size = size;
846 static void skip_multiline_comment(void)
848 unsigned start_linenr = input.position.linenr;
855 /* TODO: nested comment, warn here */
867 if(do_print_spaces) {
875 source_position_t source_position;
876 source_position.input_name = pp_token.source_position.input_name;
877 source_position.linenr = start_linenr;
878 errorf(&source_position, "at end of file while looking for comment end");
889 static void skip_line_comment(void)
909 #define MAYBE_PROLOG \
914 #define MAYBE(ch, set_type) \
917 pp_token.type = set_type; \
920 #define ELSE_CODE(code) \
924 } /* end of while(1) */ \
927 #define ELSE(set_type) \
929 pp_token.type = set_type; \
933 static void next_preprocessing_token(void)
935 if(current_expansion != NULL) {
940 pp_token.source_position = input.position;
954 pp_token.type = '\n';
967 parse_string_literal();
971 parse_character_constant();
993 MAYBE('.', TP_DOTDOTDOT)
1003 MAYBE('&', TP_ANDAND)
1004 MAYBE('=', TP_ANDEQUAL)
1008 MAYBE('=', TP_ASTERISKEQUAL)
1012 MAYBE('+', TP_PLUSPLUS)
1013 MAYBE('=', TP_PLUSEQUAL)
1017 MAYBE('>', TP_MINUSGREATER)
1018 MAYBE('-', TP_MINUSMINUS)
1019 MAYBE('=', TP_MINUSEQUAL)
1023 MAYBE('=', TP_EXCLAMATIONMARKEQUAL)
1027 MAYBE('=', TP_SLASHEQUAL)
1030 skip_multiline_comment();
1036 skip_line_comment();
1044 MAYBE('=', TP_PERCENTEQUAL)
1049 MAYBE(':', TP_HASHHASH)
1053 pp_token.type = '#';
1062 MAYBE('=', TP_LESSEQUAL)
1065 MAYBE('=', TP_LESSLESSEQUAL)
1070 MAYBE('=', TP_GREATEREQUAL)
1073 MAYBE('=', TP_GREATERGREATEREQUAL)
1074 ELSE(TP_GREATERGREATER)
1078 MAYBE('=', TP_CARETEQUAL)
1082 MAYBE('=', TP_PIPEEQUAL)
1083 MAYBE('|', TP_PIPEPIPE)
1091 MAYBE('=', TP_EQUALEQUAL)
1095 MAYBE('#', TP_HASHHASH)
1114 if (input_stack != NULL) {
1116 pop_restore_input();
1117 counted_newlines = 0;
1119 /* hack to output correct line number */
1120 print_line_directive(&input.position, "2");
1121 next_preprocessing_token();
1123 pp_token.type = TP_EOF;
1129 errorf(&pp_token.source_position, "unknown character '%c' found\n", CC);
1130 pp_token.type = TP_ERROR;
1135 static void print_quoted_string(const char *const string)
1138 for (const char *c = string; *c != 0; ++c) {
1140 case '"': fputs("\\\"", out); break;
1141 case '\\': fputs("\\\\", out); break;
1142 case '\a': fputs("\\a", out); break;
1143 case '\b': fputs("\\b", out); break;
1144 case '\f': fputs("\\f", out); break;
1145 case '\n': fputs("\\n", out); break;
1146 case '\r': fputs("\\r", out); break;
1147 case '\t': fputs("\\t", out); break;
1148 case '\v': fputs("\\v", out); break;
1149 case '\?': fputs("\\?", out); break;
1152 fprintf(out, "\\%03o", *c);
1162 static void print_line_directive(const source_position_t *pos, const char *add)
1164 fprintf(out, "# %d ", pos->linenr);
1165 print_quoted_string(pos->input_name);
1172 printed_input_name = pos->input_name;
1175 static void print_spaces(void)
1177 if (counted_newlines >= 9) {
1178 if (input.had_non_space) {
1181 print_line_directive(&pp_token.source_position, NULL);
1182 counted_newlines = 0;
1184 for (unsigned i = 0; i < counted_newlines; ++i)
1186 counted_newlines = 0;
1188 for (unsigned i = 0; i < counted_spaces; ++i)
1193 static void emit_pp_token(void)
1198 if (pp_token.type != '\n') {
1200 input.had_non_space = true;
1203 switch(pp_token.type) {
1205 fputs(pp_token.v.symbol->string, out);
1208 fputs(pp_token.v.string.begin, out);
1210 case TP_STRING_LITERAL:
1212 fputs(pp_token.v.string.begin, out);
1218 print_pp_token_type(out, pp_token.type);
1223 static void eat_pp(preprocessor_token_type_t type)
1226 assert(pp_token.type == type);
1227 next_preprocessing_token();
1230 static void eat_pp_directive(void)
1232 while(pp_token.type != '\n' && pp_token.type != TP_EOF) {
1233 next_preprocessing_token();
1237 static bool strings_equal(const string_t *string1, const string_t *string2)
1239 size_t size = string1->size;
1240 if(size != string2->size)
1243 const char *c1 = string1->begin;
1244 const char *c2 = string2->begin;
1245 for(size_t i = 0; i < size; ++i, ++c1, ++c2) {
1252 static bool wide_strings_equal(const wide_string_t *string1,
1253 const wide_string_t *string2)
1255 size_t size = string1->size;
1256 if(size != string2->size)
1259 const wchar_rep_t *c1 = string1->begin;
1260 const wchar_rep_t *c2 = string2->begin;
1261 for(size_t i = 0; i < size; ++i, ++c1, ++c2) {
1268 static bool pp_tokens_equal(const token_t *token1, const token_t *token2)
1270 if(token1->type != token2->type)
1273 switch(token1->type) {
1278 return token1->v.symbol == token2->v.symbol;
1280 case TP_CHARACTER_CONSTANT:
1281 case TP_STRING_LITERAL:
1282 return strings_equal(&token1->v.string, &token2->v.string);
1284 case TP_WIDE_CHARACTER_CONSTANT:
1285 case TP_WIDE_STRING_LITERAL:
1286 return wide_strings_equal(&token1->v.wide_string,
1287 &token2->v.wide_string);
1293 static bool pp_definitions_equal(const pp_definition_t *definition1,
1294 const pp_definition_t *definition2)
1296 if(definition1->list_len != definition2->list_len)
1299 size_t len = definition1->list_len;
1300 const token_t *t1 = definition1->replacement_list;
1301 const token_t *t2 = definition2->replacement_list;
1302 for(size_t i = 0; i < len; ++i, ++t1, ++t2) {
1303 if(!pp_tokens_equal(t1, t2))
1309 static void parse_define_directive(void)
1313 if(pp_token.type != TP_IDENTIFIER) {
1314 errorf(&pp_token.source_position,
1315 "expected identifier after #define, got '%T'", &pp_token);
1319 symbol_t *symbol = pp_token.v.symbol;
1321 pp_definition_t *new_definition
1322 = obstack_alloc(&pp_obstack, sizeof(new_definition[0]));
1323 memset(new_definition, 0, sizeof(new_definition[0]));
1324 new_definition->source_position = input.position;
1326 /* this is probably the only place where spaces are significant in the
1327 * lexer (except for the fact that they separate tokens). #define b(x)
1328 * is something else than #define b (x) */
1329 //token_t *arguments = NULL;
1331 next_preprocessing_token();
1332 while(pp_token.type != ')') {
1333 if(pp_token.type == TP_DOTDOTDOT) {
1334 new_definition->is_variadic = true;
1335 next_preprocessing_token();
1336 if(pp_token.type != ')') {
1337 errorf(&input.position,
1338 "'...' not at end of macro argument list");
1341 } else if(pp_token.type != TP_IDENTIFIER) {
1342 next_preprocessing_token();
1346 next_preprocessing_token();
1349 /* construct a new pp_definition on the obstack */
1350 assert(obstack_object_size(&pp_obstack) == 0);
1351 size_t list_len = 0;
1352 while(pp_token.type != '\n' && pp_token.type != TP_EOF) {
1353 obstack_grow(&pp_obstack, &pp_token, sizeof(pp_token));
1355 next_preprocessing_token();
1358 new_definition->list_len = list_len;
1359 new_definition->replacement_list = obstack_finish(&pp_obstack);
1361 pp_definition_t *old_definition = symbol->pp_definition;
1362 if(old_definition != NULL) {
1363 if(!pp_definitions_equal(old_definition, new_definition)) {
1364 warningf(&input.position, "multiple definition of macro '%Y' (first defined %P)",
1365 symbol, &old_definition->source_position);
1367 /* reuse the old definition */
1368 obstack_free(&pp_obstack, new_definition);
1369 new_definition = old_definition;
1373 symbol->pp_definition = new_definition;
1376 static void parse_undef_directive(void)
1380 if(pp_token.type != TP_IDENTIFIER) {
1381 errorf(&input.position,
1382 "expected identifier after #undef, got '%T'", &pp_token);
1387 symbol_t *symbol = pp_token.v.symbol;
1388 symbol->pp_definition = NULL;
1389 next_preprocessing_token();
1391 if(pp_token.type != '\n') {
1392 warningf(&input.position, "extra tokens at end of #undef directive");
1394 /* eat until '\n' */
1398 static const char *parse_headername(void)
1400 /* behind an #include we can have the special headername lexems, check
1404 while (CC == ' ' || CC == '\t') {
1408 assert(obstack_object_size(&input_obstack) == 0);
1412 /* for now until we have proper searchpath handling */
1413 obstack_1grow(&input_obstack, '.');
1414 obstack_1grow(&input_obstack, '/');
1422 parse_error("header name without closing '>'");
1427 goto finished_headername;
1429 obstack_1grow(&input_obstack, (char) CC);
1432 /* we should never be here */
1435 /* for now until we have proper searchpath handling */
1436 obstack_1grow(&input_obstack, '.');
1437 obstack_1grow(&input_obstack, '/');
1445 parse_error("header name without closing '>'");
1450 goto finished_headername;
1452 obstack_1grow(&input_obstack, (char) CC);
1455 /* we should never be here */
1458 /* TODO: do normale pp_token parsing and concatenate results */
1459 panic("pp_token concat include not implemented yet");
1462 finished_headername:
1463 obstack_1grow(&input_obstack, '\0');
1464 char *headername = obstack_finish(&input_obstack);
1466 /* TODO: iterate search-path to find the file */
1468 next_preprocessing_token();
1473 static void parse_include_directive(void)
1475 /* don't eat the TP_include here!
1476 * we need an alternative parsing for the next token */
1480 const char *headername = parse_headername();
1481 if (headername == NULL) {
1486 if (pp_token.type != '\n' && pp_token.type != TP_EOF) {
1487 warningf(&pp_token.source_position,
1488 "extra tokens at end of #include directive");
1492 if (n_inputs > INCLUDE_LIMIT) {
1493 errorf(&pp_token.source_position, "#include nested too deeply");
1495 next_preprocessing_token();
1501 bool res = open_input(headername);
1503 errorf(&pp_token.source_position,
1504 "failed including '%s': %s", headername, strerror(errno));
1505 pop_restore_input();
1510 static pp_conditional_t *push_conditional(void)
1512 pp_conditional_t *conditional
1513 = obstack_alloc(&pp_obstack, sizeof(*conditional));
1514 memset(conditional, 0, sizeof(*conditional));
1516 conditional->parent = conditional_stack;
1517 conditional_stack = conditional;
1522 static void pop_conditional(void)
1524 assert(conditional_stack != NULL);
1525 conditional_stack = conditional_stack->parent;
1528 static void check_unclosed_conditionals(void)
1530 while (conditional_stack != NULL) {
1531 pp_conditional_t *conditional = conditional_stack;
1533 if (conditional->in_else) {
1534 errorf(&conditional->source_position, "unterminated #else");
1536 errorf(&conditional->source_position, "unterminated condition");
1542 static void parse_ifdef_ifndef_directive(void)
1544 bool is_ifndef = (pp_token.type == TP_ifndef);
1546 next_preprocessing_token();
1550 pp_conditional_t *conditional = push_conditional();
1551 conditional->source_position = pp_token.source_position;
1552 conditional->skip = true;
1556 if (pp_token.type != TP_IDENTIFIER) {
1557 errorf(&pp_token.source_position,
1558 "expected identifier after #%s, got '%T'",
1559 is_ifndef ? "ifndef" : "ifdef", &pp_token);
1562 /* just take the true case in the hope to avoid further errors */
1565 symbol_t *symbol = pp_token.v.symbol;
1566 pp_definition_t *pp_definition = symbol->pp_definition;
1567 next_preprocessing_token();
1569 if (pp_token.type != '\n') {
1570 errorf(&pp_token.source_position,
1571 "extra tokens at end of #%s",
1572 is_ifndef ? "ifndef" : "ifdef");
1576 /* evaluate wether we are in true or false case */
1577 condition = is_ifndef ? pp_definition == NULL : pp_definition != NULL;
1580 pp_conditional_t *conditional = push_conditional();
1581 conditional->source_position = pp_token.source_position;
1582 conditional->condition = condition;
1589 static void parse_else_directive(void)
1593 if (pp_token.type != '\n') {
1595 warningf(&pp_token.source_position, "extra tokens at end of #else");
1600 pp_conditional_t *conditional = conditional_stack;
1601 if (conditional == NULL) {
1602 errorf(&pp_token.source_position, "#else without prior #if");
1606 if (conditional->in_else) {
1607 errorf(&pp_token.source_position,
1608 "#else after #else (condition started %P)",
1609 conditional->source_position);
1614 conditional->in_else = true;
1615 if (!conditional->skip) {
1616 skip_mode = conditional->condition;
1618 conditional->source_position = pp_token.source_position;
1621 static void parse_endif_directive(void)
1625 if (pp_token.type != '\n') {
1627 warningf(&pp_token.source_position,
1628 "extra tokens at end of #endif");
1633 pp_conditional_t *conditional = conditional_stack;
1634 if (conditional == NULL) {
1635 errorf(&pp_token.source_position, "#endif without prior #if");
1639 if (!conditional->skip) {
1645 static void parse_preprocessing_directive(void)
1647 do_print_spaces = false;
1648 do_expansions = false;
1652 switch(pp_token.type) {
1655 parse_ifdef_ifndef_directive();
1658 parse_else_directive();
1661 parse_endif_directive();
1668 switch(pp_token.type) {
1670 parse_define_directive();
1673 parse_undef_directive();
1677 parse_ifdef_ifndef_directive();
1680 parse_else_directive();
1683 parse_endif_directive();
1686 parse_include_directive();
1687 /* no need to parse ending '\n' */
1688 do_print_spaces = true;
1689 do_expansions = true;
1692 errorf(&pp_token.source_position,
1693 "invalid preprocessing directive #%T", &pp_token);
1699 do_print_spaces = true;
1700 do_expansions = true;
1703 assert(pp_token.type == '\n' || pp_token.type == TP_EOF);
1704 next_preprocessing_token();
1707 #define GCC_COMPAT_MODE
1709 int pptest_main(int argc, char **argv);
1710 int pptest_main(int argc, char **argv)
1712 init_symbol_table();
1715 obstack_init(&pp_obstack);
1716 obstack_init(&input_obstack);
1718 const char *filename = "t.c";
1724 #ifdef GCC_COMPAT_MODE
1725 /* this is here so we can directly compare "gcc -E" output and our output */
1726 fprintf(out, "# 1 \"%s\"\n", filename);
1727 fputs("# 1 \"<built-in>\"\n", out);
1728 fputs("# 1 \"<command-line>\"\n", out);
1731 bool ok = open_input(filename);
1735 /* we're at a line begin */
1736 if(pp_token.type == '#') {
1737 parse_preprocessing_directive();
1739 /* parse+emit a line */
1740 while(pp_token.type != '\n') {
1741 if(pp_token.type == TP_EOF)
1742 goto end_of_main_loop;
1744 next_preprocessing_token();
1747 next_preprocessing_token();
1752 check_unclosed_conditionals();
1755 obstack_free(&input_obstack, NULL);
1756 obstack_free(&pp_obstack, NULL);
1759 exit_symbol_table();