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);
88 static void print_spaces(void);
90 static bool open_input(const char *filename)
92 FILE *file = fopen(filename, "r");
99 input.had_non_space = false;
100 input.position.input_name = filename;
101 input.position.linenr = 1;
103 /* indicate that we're at a new input */
104 print_line_directive(&input.position, input_stack != NULL ? "1" : NULL);
106 counted_newlines = 0;
109 /* read first char and first token */
111 next_preprocessing_token();
116 static void close_input(void)
118 /* ensure we have a newline at EOF */
119 if (input.had_non_space) {
123 assert(input.file != NULL);
132 static void push_input(void)
134 pp_input_t *saved_input
135 = obstack_alloc(&input_obstack, sizeof(*saved_input));
137 memcpy(saved_input, &input, sizeof(*saved_input));
139 /* adjust buffer positions */
140 if (input.bufpos != NULL)
141 saved_input->bufpos = saved_input->buf + (input.bufpos - input.buf);
142 if (input.bufend != NULL)
143 saved_input->bufend = saved_input->buf + (input.bufend - input.buf);
145 saved_input->parent = input_stack;
146 input_stack = saved_input;
150 static void pop_restore_input(void)
152 assert(n_inputs > 0);
153 assert(input_stack != NULL);
155 pp_input_t *saved_input = input_stack;
157 memcpy(&input, saved_input, sizeof(input));
160 /* adjust buffer positions */
161 if (saved_input->bufpos != NULL)
162 input.bufpos = input.buf + (saved_input->bufpos - saved_input->buf);
163 if (saved_input->bufend != NULL)
164 input.bufend = input.buf + (saved_input->bufend - saved_input->buf);
166 input_stack = saved_input->parent;
167 obstack_free(&input_obstack, saved_input);
172 * Prints a parse error message at the current token.
174 * @param msg the error message
176 static void parse_error(const char *msg)
178 errorf(&pp_token.source_position, "%s", msg);
181 static inline void next_real_char(void)
183 assert(input.bufpos <= input.bufend);
184 if (input.bufpos >= input.bufend) {
185 size_t s = fread(input.buf + MAX_PUTBACK, 1,
186 sizeof(input.buf) - MAX_PUTBACK, input.file);
191 input.bufpos = input.buf + MAX_PUTBACK;
192 input.bufend = input.buf + MAX_PUTBACK + s;
194 CC = *input.bufpos++;
198 * Put a character back into the buffer.
200 * @param pc the character to put back
202 static inline void put_back(int pc)
204 assert(input.bufpos > input.buf);
205 *(--input.bufpos - input.buf + input.buf) = (char) pc;
208 printf("putback '%c'\n", pc);
212 #define MATCH_NEWLINE(code) \
218 ++input.position.linenr; \
222 ++input.position.linenr; \
225 #define eat(c_type) do { assert(CC == c_type); next_char(); } while(0)
227 static void maybe_concat_lines(void)
232 MATCH_NEWLINE(return;)
243 * Set c to the next input character, ie.
244 * after expanding trigraphs.
246 static inline void next_char(void)
250 /* filter trigraphs and concatenated lines */
251 if(UNLIKELY(CC == '\\')) {
252 maybe_concat_lines();
253 goto end_of_next_char;
256 if(LIKELY(CC != '?'))
257 goto end_of_next_char;
260 if(LIKELY(CC != '?')) {
263 goto end_of_next_char;
268 case '=': CC = '#'; break;
269 case '(': CC = '['; break;
270 case '/': CC = '\\'; maybe_concat_lines(); break;
271 case ')': CC = ']'; break;
272 case '\'': CC = '^'; break;
273 case '<': CC = '{'; break;
274 case '!': CC = '|'; break;
275 case '>': CC = '}'; break;
276 case '-': CC = '~'; break;
286 printf("nchar '%c'\n", CC);
293 * Returns true if the given char is a octal digit.
295 * @param char the character to check
297 static inline bool is_octal_digit(int chr)
315 * Returns the value of a digit.
316 * The only portable way to do it ...
318 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.v.string.begin = result;
489 pp_token.v.string.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_wide_string_literal(void)
537 const unsigned start_linenr = input.position.linenr;
545 wchar_rep_t tc = parse_escape_sequence();
546 obstack_grow(&symbol_obstack, &tc, sizeof(tc));
551 source_position_t source_position;
552 source_position.input_name = pp_token.source_position.input_name;
553 source_position.linenr = start_linenr;
554 errorf(&source_position, "string has no end");
555 pp_token.type = TP_ERROR;
565 obstack_grow(&symbol_obstack, &tc, sizeof(tc));
573 /* add finishing 0 to the string */
574 static const wchar_rep_t nul = L'\0';
575 obstack_grow(&symbol_obstack, &nul, sizeof(nul));
578 = (size_t)obstack_object_size(&symbol_obstack) / sizeof(wchar_rep_t);
579 const wchar_rep_t *const string = obstack_finish(&symbol_obstack);
581 #if 0 /* TODO hash */
582 /* check if there is already a copy of the string */
583 const wchar_rep_t *const result = strset_insert(&stringset, string);
584 if(result != string) {
585 obstack_free(&symbol_obstack, string);
588 const wchar_rep_t *const result = string;
591 pp_token.type = TP_WIDE_STRING_LITERAL;
592 pp_token.v.wide_string.begin = result;
593 pp_token.v.wide_string.size = size;
596 static void parse_character_constant(void)
598 const unsigned start_linenr = input.position.linenr;
606 tc = parse_escape_sequence();
607 obstack_1grow(&symbol_obstack, (char) tc);
611 parse_error("newline while parsing character constant");
616 source_position_t source_position;
617 source_position.input_name = pp_token.source_position.input_name;
618 source_position.linenr = start_linenr;
619 errorf(&source_position, "EOF while parsing character constant");
620 pp_token.type = TP_ERROR;
626 goto end_of_char_constant;
629 obstack_1grow(&symbol_obstack, (char) CC);
636 end_of_char_constant:;
637 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
638 const char *const string = obstack_finish(&symbol_obstack);
640 pp_token.type = TP_CHARACTER_CONSTANT;
641 pp_token.v.string.begin = string;
642 pp_token.v.string.size = size;
645 #define SYMBOL_CHARS_WITHOUT_E_P \
696 #define SYMBOL_CHARS \
697 SYMBOL_CHARS_WITHOUT_E_P \
716 * returns next final token from a preprocessor macro expansion
718 static void expand_next(void)
720 assert(current_expansion != NULL);
722 pp_definition_t *definition = current_expansion;
725 if(definition->list_len == 0
726 || definition->expand_pos >= definition->list_len) {
727 /* we're finished with the current macro, move up 1 level in the
729 pp_definition_t *parent = definition->parent_expansion;
730 definition->parent_expansion = NULL;
731 definition->is_expanding = false;
733 /* it was the outermost expansion, parse normal pptoken */
735 current_expansion = NULL;
736 next_preprocessing_token();
740 current_expansion = definition;
743 pp_token = definition->token_list[definition->expand_pos];
744 ++definition->expand_pos;
746 if(pp_token.type != TP_IDENTIFIER)
749 /* if it was an identifier then we might need to expand again */
750 pp_definition_t *symbol_definition = pp_token.v.symbol->pp_definition;
751 if(symbol_definition != NULL && !symbol_definition->is_expanding) {
752 symbol_definition->parent_expansion = definition;
753 symbol_definition->expand_pos = 0;
754 symbol_definition->is_expanding = true;
755 definition = symbol_definition;
756 current_expansion = definition;
761 static void skip_line_comment(void)
782 static void skip_multiline_comment(void)
787 unsigned start_linenr = input.position.linenr;
793 /* TODO: nested comment, warn here */
805 if(do_print_spaces) {
813 source_position_t source_position;
814 source_position.input_name = pp_token.source_position.input_name;
815 source_position.linenr = start_linenr;
816 errorf(&source_position, "at end of file while looking for comment end");
827 /* skip spaces advancing at the start of the next preprocessing token */
828 static void skip_spaces(bool skip_newline)
844 } else if (CC == '*') {
846 skip_multiline_comment();
862 ++input.position.linenr;
872 ++input.position.linenr;
883 static void eat_pp(preprocessor_token_type_t type)
886 assert(pp_token.type == type);
887 next_preprocessing_token();
890 static void parse_symbol(void)
892 obstack_1grow(&symbol_obstack, (char) CC);
899 obstack_1grow(&symbol_obstack, (char) CC);
909 obstack_1grow(&symbol_obstack, '\0');
910 char *string = obstack_finish(&symbol_obstack);
912 /* might be a wide string or character constant ( L"string"/L'c' ) */
913 if (CC == '"' && string[0] == 'L' && string[1] == '\0') {
914 obstack_free(&symbol_obstack, string);
915 parse_wide_string_literal();
917 } else if (CC == '\'' && string[0] == 'L' && string[1] == '\0') {
918 obstack_free(&symbol_obstack, string);
919 parse_wide_character_constant();
923 symbol_t *symbol = symbol_table_insert(string);
925 pp_token.type = symbol->pp_ID;
926 pp_token.v.symbol = symbol;
928 /* we can free the memory from symbol obstack if we already had an entry in
929 * the symbol table */
930 if (symbol->string != string) {
931 obstack_free(&symbol_obstack, string);
936 pp_definition_t *pp_definition = symbol->pp_definition;
937 if (pp_definition == NULL)
940 if (pp_definition->has_parameters) {
942 /* no opening brace -> no expansion */
945 next_preprocessing_token();
948 /* parse arguments (TODO) */
949 while (pp_token.type != TP_EOF && pp_token.type != ')')
950 next_preprocessing_token();
951 next_preprocessing_token();
954 pp_definition->expand_pos = 0;
955 pp_definition->is_expanding = true,
956 current_expansion = pp_definition;
960 static void parse_number(void)
962 obstack_1grow(&symbol_obstack, (char) CC);
969 SYMBOL_CHARS_WITHOUT_E_P
970 obstack_1grow(&symbol_obstack, (char) CC);
978 obstack_1grow(&symbol_obstack, (char) CC);
980 if(CC == '+' || CC == '-') {
981 obstack_1grow(&symbol_obstack, (char) CC);
992 obstack_1grow(&symbol_obstack, '\0');
993 size_t size = obstack_object_size(&symbol_obstack);
994 char *string = obstack_finish(&symbol_obstack);
996 pp_token.type = TP_NUMBER;
997 pp_token.v.string.begin = string;
998 pp_token.v.string.size = size;
1003 #define MAYBE_PROLOG \
1008 #define MAYBE(ch, set_type) \
1011 pp_token.type = set_type; \
1014 #define ELSE_CODE(code) \
1018 } /* end of while(1) */ \
1021 #define ELSE(set_type) \
1023 pp_token.type = set_type; \
1027 static void next_preprocessing_token(void)
1029 if(current_expansion != NULL) {
1034 pp_token.source_position = input.position;
1048 pp_token.type = '\n';
1061 parse_string_literal();
1065 parse_character_constant();
1087 MAYBE('.', TP_DOTDOTDOT)
1091 pp_token.type = '.';
1097 MAYBE('&', TP_ANDAND)
1098 MAYBE('=', TP_ANDEQUAL)
1102 MAYBE('=', TP_ASTERISKEQUAL)
1106 MAYBE('+', TP_PLUSPLUS)
1107 MAYBE('=', TP_PLUSEQUAL)
1111 MAYBE('>', TP_MINUSGREATER)
1112 MAYBE('-', TP_MINUSMINUS)
1113 MAYBE('=', TP_MINUSEQUAL)
1117 MAYBE('=', TP_EXCLAMATIONMARKEQUAL)
1121 MAYBE('=', TP_SLASHEQUAL)
1124 skip_multiline_comment();
1128 skip_line_comment();
1134 MAYBE('=', TP_PERCENTEQUAL)
1139 MAYBE(':', TP_HASHHASH)
1143 pp_token.type = '#';
1152 MAYBE('=', TP_LESSEQUAL)
1155 MAYBE('=', TP_LESSLESSEQUAL)
1160 MAYBE('=', TP_GREATEREQUAL)
1163 MAYBE('=', TP_GREATERGREATEREQUAL)
1164 ELSE(TP_GREATERGREATER)
1168 MAYBE('=', TP_CARETEQUAL)
1172 MAYBE('=', TP_PIPEEQUAL)
1173 MAYBE('|', TP_PIPEPIPE)
1181 MAYBE('=', TP_EQUALEQUAL)
1185 MAYBE('#', TP_HASHHASH)
1204 if (input_stack != NULL) {
1206 pop_restore_input();
1207 counted_newlines = 0;
1209 /* hack to output correct line number */
1210 print_line_directive(&input.position, "2");
1211 next_preprocessing_token();
1213 pp_token.type = TP_EOF;
1219 errorf(&pp_token.source_position, "unknown character '%c' found\n", CC);
1220 pp_token.type = TP_ERROR;
1225 static void print_quoted_string(const char *const string)
1228 for (const char *c = string; *c != 0; ++c) {
1230 case '"': fputs("\\\"", out); break;
1231 case '\\': fputs("\\\\", out); break;
1232 case '\a': fputs("\\a", out); break;
1233 case '\b': fputs("\\b", out); break;
1234 case '\f': fputs("\\f", out); break;
1235 case '\n': fputs("\\n", out); break;
1236 case '\r': fputs("\\r", out); break;
1237 case '\t': fputs("\\t", out); break;
1238 case '\v': fputs("\\v", out); break;
1239 case '\?': fputs("\\?", out); break;
1242 fprintf(out, "\\%03o", *c);
1252 static void print_line_directive(const source_position_t *pos, const char *add)
1254 fprintf(out, "# %d ", pos->linenr);
1255 print_quoted_string(pos->input_name);
1262 printed_input_name = pos->input_name;
1265 static void print_spaces(void)
1267 if (counted_newlines >= 8) {
1268 if (input.had_non_space) {
1271 print_line_directive(&pp_token.source_position, NULL);
1272 counted_newlines = 0;
1274 for (unsigned i = 0; i < counted_newlines; ++i)
1276 counted_newlines = 0;
1278 for (unsigned i = 0; i < counted_spaces; ++i)
1283 static void emit_pp_token(void)
1288 if (pp_token.type != '\n') {
1290 input.had_non_space = true;
1293 switch(pp_token.type) {
1295 fputs(pp_token.v.symbol->string, out);
1298 fputs(pp_token.v.string.begin, out);
1300 case TP_STRING_LITERAL:
1302 fputs(pp_token.v.string.begin, out);
1308 print_pp_token_type(out, pp_token.type);
1313 static void eat_pp_directive(void)
1315 while(pp_token.type != '\n' && pp_token.type != TP_EOF) {
1316 next_preprocessing_token();
1320 static bool strings_equal(const string_t *string1, const string_t *string2)
1322 size_t size = string1->size;
1323 if(size != string2->size)
1326 const char *c1 = string1->begin;
1327 const char *c2 = string2->begin;
1328 for(size_t i = 0; i < size; ++i, ++c1, ++c2) {
1335 static bool wide_strings_equal(const wide_string_t *string1,
1336 const wide_string_t *string2)
1338 size_t size = string1->size;
1339 if(size != string2->size)
1342 const wchar_rep_t *c1 = string1->begin;
1343 const wchar_rep_t *c2 = string2->begin;
1344 for(size_t i = 0; i < size; ++i, ++c1, ++c2) {
1351 static bool pp_tokens_equal(const token_t *token1, const token_t *token2)
1353 if(token1->type != token2->type)
1356 switch(token1->type) {
1361 return token1->v.symbol == token2->v.symbol;
1363 case TP_CHARACTER_CONSTANT:
1364 case TP_STRING_LITERAL:
1365 return strings_equal(&token1->v.string, &token2->v.string);
1367 case TP_WIDE_CHARACTER_CONSTANT:
1368 case TP_WIDE_STRING_LITERAL:
1369 return wide_strings_equal(&token1->v.wide_string,
1370 &token2->v.wide_string);
1376 static bool pp_definitions_equal(const pp_definition_t *definition1,
1377 const pp_definition_t *definition2)
1379 if(definition1->list_len != definition2->list_len)
1382 size_t len = definition1->list_len;
1383 const token_t *t1 = definition1->token_list;
1384 const token_t *t2 = definition2->token_list;
1385 for(size_t i = 0; i < len; ++i, ++t1, ++t2) {
1386 if(!pp_tokens_equal(t1, t2))
1392 static void parse_define_directive(void)
1395 assert(obstack_object_size(&pp_obstack) == 0);
1397 if (pp_token.type != TP_IDENTIFIER) {
1398 errorf(&pp_token.source_position,
1399 "expected identifier after #define, got '%t'", &pp_token);
1402 symbol_t *symbol = pp_token.v.symbol;
1404 pp_definition_t *new_definition
1405 = obstack_alloc(&pp_obstack, sizeof(new_definition[0]));
1406 memset(new_definition, 0, sizeof(new_definition[0]));
1407 new_definition->source_position = input.position;
1409 /* this is probably the only place where spaces are significant in the
1410 * lexer (except for the fact that they separate tokens). #define b(x)
1411 * is something else than #define b (x) */
1414 next_preprocessing_token();
1415 /* get next token after '(' */
1416 next_preprocessing_token();
1419 switch (pp_token.type) {
1421 new_definition->is_variadic = true;
1422 next_preprocessing_token();
1423 if (pp_token.type != ')') {
1424 errorf(&input.position,
1425 "'...' not at end of macro argument list");
1430 obstack_ptr_grow(&pp_obstack, pp_token.v.symbol);
1431 next_preprocessing_token();
1433 if (pp_token.type == ',') {
1434 next_preprocessing_token();
1438 if (pp_token.type != ')') {
1439 errorf(&pp_token.source_position,
1440 "expected ',' or ')' after identifier, got '%t'",
1446 next_preprocessing_token();
1447 goto finish_argument_list;
1449 errorf(&pp_token.source_position,
1450 "expected identifier, '...' or ')' in #define argument list, got '%t'",
1456 finish_argument_list:
1457 new_definition->has_parameters = true;
1458 new_definition->n_parameters
1459 = obstack_object_size(&pp_obstack) / sizeof(new_definition->parameters[0]);
1460 new_definition->parameters = obstack_finish(&pp_obstack);
1462 next_preprocessing_token();
1465 /* construct a new pp_definition on the obstack */
1466 assert(obstack_object_size(&pp_obstack) == 0);
1467 size_t list_len = 0;
1468 while (pp_token.type != '\n' && pp_token.type != TP_EOF) {
1469 obstack_grow(&pp_obstack, &pp_token, sizeof(pp_token));
1471 next_preprocessing_token();
1474 new_definition->list_len = list_len;
1475 new_definition->token_list = obstack_finish(&pp_obstack);
1477 pp_definition_t *old_definition = symbol->pp_definition;
1478 if (old_definition != NULL) {
1479 if (!pp_definitions_equal(old_definition, new_definition)) {
1480 warningf(&input.position, "multiple definition of macro '%Y' (first defined %P)",
1481 symbol, &old_definition->source_position);
1483 /* reuse the old definition */
1484 obstack_free(&pp_obstack, new_definition);
1485 new_definition = old_definition;
1489 symbol->pp_definition = new_definition;
1493 if (obstack_object_size(&pp_obstack) > 0) {
1494 char *ptr = obstack_finish(&pp_obstack);
1495 obstack_free(&pp_obstack, ptr);
1500 static void parse_undef_directive(void)
1504 if(pp_token.type != TP_IDENTIFIER) {
1505 errorf(&input.position,
1506 "expected identifier after #undef, got '%t'", &pp_token);
1511 symbol_t *symbol = pp_token.v.symbol;
1512 symbol->pp_definition = NULL;
1513 next_preprocessing_token();
1515 if(pp_token.type != '\n') {
1516 warningf(&input.position, "extra tokens at end of #undef directive");
1518 /* eat until '\n' */
1522 static const char *parse_headername(void)
1524 /* behind an #include we can have the special headername lexems.
1525 * They're only allowed behind an #include so they're not recognized
1526 * by the normal next_preprocessing_token. We handle them as a special
1529 /* skip spaces so we reach start of next preprocessing token */
1532 assert(obstack_object_size(&input_obstack) == 0);
1534 /* check wether we have a "... or <... headername */
1537 /* for now until we have proper searchpath handling */
1538 obstack_1grow(&input_obstack, '.');
1539 obstack_1grow(&input_obstack, '/');
1547 parse_error("header name without closing '>'");
1552 goto finished_headername;
1554 obstack_1grow(&input_obstack, (char) CC);
1557 /* we should never be here */
1560 /* for now until we have proper searchpath handling */
1561 obstack_1grow(&input_obstack, '.');
1562 obstack_1grow(&input_obstack, '/');
1570 parse_error("header name without closing '>'");
1575 goto finished_headername;
1577 obstack_1grow(&input_obstack, (char) CC);
1580 /* we should never be here */
1583 /* TODO: do normale pp_token parsing and concatenate results */
1584 panic("pp_token concat include not implemented yet");
1587 finished_headername:
1588 obstack_1grow(&input_obstack, '\0');
1589 char *headername = obstack_finish(&input_obstack);
1591 /* TODO: iterate search-path to find the file */
1593 next_preprocessing_token();
1598 static bool parse_include_directive(void)
1600 /* don't eat the TP_include here!
1601 * we need an alternative parsing for the next token */
1605 const char *headername = parse_headername();
1606 if (headername == NULL) {
1611 if (pp_token.type != '\n' && pp_token.type != TP_EOF) {
1612 warningf(&pp_token.source_position,
1613 "extra tokens at end of #include directive");
1617 if (n_inputs > INCLUDE_LIMIT) {
1618 errorf(&pp_token.source_position, "#include nested too deeply");
1620 next_preprocessing_token();
1624 /* we have to reenable space counting and macro expansion here,
1625 * because it is still disabled in directive parsing,
1626 * but we will trigger a preprocessing token reading of the new file
1627 * now and need expansions/space counting */
1628 do_print_spaces = true;
1629 do_expansions = true;
1633 bool res = open_input(headername);
1635 errorf(&pp_token.source_position,
1636 "failed including '%s': %s", headername, strerror(errno));
1637 pop_restore_input();
1644 static pp_conditional_t *push_conditional(void)
1646 pp_conditional_t *conditional
1647 = obstack_alloc(&pp_obstack, sizeof(*conditional));
1648 memset(conditional, 0, sizeof(*conditional));
1650 conditional->parent = conditional_stack;
1651 conditional_stack = conditional;
1656 static void pop_conditional(void)
1658 assert(conditional_stack != NULL);
1659 conditional_stack = conditional_stack->parent;
1662 static void check_unclosed_conditionals(void)
1664 while (conditional_stack != NULL) {
1665 pp_conditional_t *conditional = conditional_stack;
1667 if (conditional->in_else) {
1668 errorf(&conditional->source_position, "unterminated #else");
1670 errorf(&conditional->source_position, "unterminated condition");
1676 static void parse_ifdef_ifndef_directive(void)
1678 bool is_ifndef = (pp_token.type == TP_ifndef);
1680 next_preprocessing_token();
1684 pp_conditional_t *conditional = push_conditional();
1685 conditional->source_position = pp_token.source_position;
1686 conditional->skip = true;
1690 if (pp_token.type != TP_IDENTIFIER) {
1691 errorf(&pp_token.source_position,
1692 "expected identifier after #%s, got '%t'",
1693 is_ifndef ? "ifndef" : "ifdef", &pp_token);
1696 /* just take the true case in the hope to avoid further errors */
1699 symbol_t *symbol = pp_token.v.symbol;
1700 pp_definition_t *pp_definition = symbol->pp_definition;
1701 next_preprocessing_token();
1703 if (pp_token.type != '\n') {
1704 errorf(&pp_token.source_position,
1705 "extra tokens at end of #%s",
1706 is_ifndef ? "ifndef" : "ifdef");
1710 /* evaluate wether we are in true or false case */
1711 condition = is_ifndef ? pp_definition == NULL : pp_definition != NULL;
1714 pp_conditional_t *conditional = push_conditional();
1715 conditional->source_position = pp_token.source_position;
1716 conditional->condition = condition;
1723 static void parse_else_directive(void)
1727 if (pp_token.type != '\n') {
1729 warningf(&pp_token.source_position, "extra tokens at end of #else");
1734 pp_conditional_t *conditional = conditional_stack;
1735 if (conditional == NULL) {
1736 errorf(&pp_token.source_position, "#else without prior #if");
1740 if (conditional->in_else) {
1741 errorf(&pp_token.source_position,
1742 "#else after #else (condition started %P)",
1743 conditional->source_position);
1748 conditional->in_else = true;
1749 if (!conditional->skip) {
1750 skip_mode = conditional->condition;
1752 conditional->source_position = pp_token.source_position;
1755 static void parse_endif_directive(void)
1759 if (pp_token.type != '\n') {
1761 warningf(&pp_token.source_position,
1762 "extra tokens at end of #endif");
1767 pp_conditional_t *conditional = conditional_stack;
1768 if (conditional == NULL) {
1769 errorf(&pp_token.source_position, "#endif without prior #if");
1773 if (!conditional->skip) {
1779 static void parse_preprocessing_directive(void)
1781 do_print_spaces = false;
1782 do_expansions = false;
1786 switch(pp_token.type) {
1789 parse_ifdef_ifndef_directive();
1792 parse_else_directive();
1795 parse_endif_directive();
1802 switch(pp_token.type) {
1804 parse_define_directive();
1807 parse_undef_directive();
1811 parse_ifdef_ifndef_directive();
1814 parse_else_directive();
1817 parse_endif_directive();
1820 bool in_new_source = parse_include_directive();
1821 /* no need to do anything if source file switched */
1827 /* the nop directive */
1830 errorf(&pp_token.source_position,
1831 "invalid preprocessing directive #%t", &pp_token);
1837 do_print_spaces = true;
1838 do_expansions = true;
1841 assert(pp_token.type == '\n' || pp_token.type == TP_EOF);
1842 next_preprocessing_token();
1845 #define GCC_COMPAT_MODE
1847 int pptest_main(int argc, char **argv);
1848 int pptest_main(int argc, char **argv)
1850 init_symbol_table();
1853 obstack_init(&pp_obstack);
1854 obstack_init(&input_obstack);
1856 const char *filename = "t.c";
1862 #ifdef GCC_COMPAT_MODE
1863 /* this is here so we can directly compare "gcc -E" output and our output */
1864 fprintf(out, "# 1 \"%s\"\n", filename);
1865 fputs("# 1 \"<built-in>\"\n", out);
1866 fputs("# 1 \"<command-line>\"\n", out);
1869 bool ok = open_input(filename);
1873 /* we're at a line begin */
1874 if(pp_token.type == '#') {
1875 parse_preprocessing_directive();
1877 /* parse+emit a line */
1878 while(pp_token.type != '\n') {
1879 if(pp_token.type == TP_EOF)
1880 goto end_of_main_loop;
1882 next_preprocessing_token();
1885 next_preprocessing_token();
1890 check_unclosed_conditionals();
1893 obstack_free(&input_obstack, NULL);
1894 obstack_free(&pp_obstack, NULL);
1897 exit_symbol_table();