12 #include "adt/error.h"
13 #include "lang_features.h"
14 #include "diagnostic.h"
15 #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;
57 utf32 buf[1024+MAX_PUTBACK];
60 source_position_t position;
65 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");
96 input.input = input_from_stream(file, NULL);
99 input.had_non_space = false;
100 input.position.input_name = filename;
101 input.position.lineno = 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 input_free(input.input);
124 assert(input.file != NULL);
134 static void push_input(void)
136 pp_input_t *saved_input
137 = obstack_alloc(&input_obstack, sizeof(*saved_input));
139 memcpy(saved_input, &input, sizeof(*saved_input));
141 /* adjust buffer positions */
142 if (input.bufpos != NULL)
143 saved_input->bufpos = saved_input->buf + (input.bufpos - input.buf);
144 if (input.bufend != NULL)
145 saved_input->bufend = saved_input->buf + (input.bufend - input.buf);
147 saved_input->parent = input_stack;
148 input_stack = saved_input;
152 static void pop_restore_input(void)
154 assert(n_inputs > 0);
155 assert(input_stack != NULL);
157 pp_input_t *saved_input = input_stack;
159 memcpy(&input, saved_input, sizeof(input));
162 /* adjust buffer positions */
163 if (saved_input->bufpos != NULL)
164 input.bufpos = input.buf + (saved_input->bufpos - saved_input->buf);
165 if (saved_input->bufend != NULL)
166 input.bufend = input.buf + (saved_input->bufend - saved_input->buf);
168 input_stack = saved_input->parent;
169 obstack_free(&input_obstack, saved_input);
174 * Prints a parse error message at the current token.
176 * @param msg the error message
178 static void parse_error(const char *msg)
180 errorf(&pp_token.source_position, "%s", msg);
183 static inline void next_real_char(void)
185 assert(input.bufpos <= input.bufend);
186 if (input.bufpos >= input.bufend) {
187 size_t n = decode(input.input, input.buf + MAX_PUTBACK,
188 sizeof(input.buf)/sizeof(input.buf[0]) - MAX_PUTBACK);
193 input.bufpos = input.buf + MAX_PUTBACK;
194 input.bufend = input.bufpos + n;
196 input.c = *input.bufpos++;
197 ++input.position.colno;
201 * Put a character back into the buffer.
203 * @param pc the character to put back
205 static inline void put_back(utf32 const pc)
207 assert(input.bufpos > input.buf);
208 *(--input.bufpos - input.buf + input.buf) = (char) pc;
209 --input.position.colno;
212 #define MATCH_NEWLINE(code) \
215 if(input.c == '\n') { \
218 ++input.position.lineno; \
222 ++input.position.lineno; \
225 #define eat(c_type) (assert(input.c == c_type), next_char())
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(input.c == '\\')) {
252 maybe_concat_lines();
253 goto end_of_next_char;
256 if(LIKELY(input.c != '?'))
257 goto end_of_next_char;
260 if(LIKELY(input.c != '?')) {
263 goto end_of_next_char;
268 case '=': input.c = '#'; break;
269 case '(': input.c = '['; break;
270 case '/': input.c = '\\'; maybe_concat_lines(); break;
271 case ')': input.c = ']'; break;
272 case '\'': input.c = '^'; break;
273 case '<': input.c = '{'; break;
274 case '!': input.c = '|'; break;
275 case '>': input.c = '}'; break;
276 case '-': input.c = '~'; break;
286 printf("nchar '%c'\n", input.c);
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)
344 panic("wrong character given");
349 * Parses an octal character sequence.
351 * @param first_digit the already read first digit
353 static int parse_octal_sequence(const int first_digit)
355 assert(is_octal_digit(first_digit));
356 int value = digit_value(first_digit);
357 if (!is_octal_digit(input.c)) return value;
358 value = 8 * value + digit_value(input.c);
360 if (!is_octal_digit(input.c)) return value;
361 value = 8 * value + digit_value(input.c);
365 return (signed char) value;
367 return (unsigned char) value;
372 * Parses a hex character sequence.
374 static int parse_hex_sequence(void)
377 while(isxdigit(input.c)) {
378 value = 16 * value + digit_value(input.c);
383 return (signed char) value;
385 return (unsigned char) value;
390 * Parse an escape sequence.
392 static int parse_escape_sequence(void)
400 case '"': return '"';
401 case '\'': return '\'';
402 case '\\': return '\\';
403 case '?': return '\?';
404 case 'a': return '\a';
405 case 'b': return '\b';
406 case 'f': return '\f';
407 case 'n': return '\n';
408 case 'r': return '\r';
409 case 't': return '\t';
410 case 'v': return '\v';
412 return parse_hex_sequence();
421 return parse_octal_sequence(ec);
423 parse_error("reached end of file while parsing escape sequence");
426 parse_error("unknown escape sequence");
431 static void parse_string_literal(void)
433 const unsigned start_linenr = input.position.lineno;
441 if(resolve_escape_sequences) {
442 tc = parse_escape_sequence();
443 obstack_1grow(&symbol_obstack, (char) tc);
445 obstack_1grow(&symbol_obstack, (char) input.c);
447 obstack_1grow(&symbol_obstack, (char) input.c);
453 source_position_t source_position;
454 source_position.input_name = pp_token.source_position.input_name;
455 source_position.lineno = start_linenr;
456 errorf(&source_position, "string has no end");
457 pp_token.type = TP_ERROR;
466 obstack_1grow(&symbol_obstack, (char) input.c);
473 /* add finishing 0 to the string */
474 obstack_1grow(&symbol_obstack, '\0');
475 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
476 const char *const string = obstack_finish(&symbol_obstack);
478 #if 0 /* TODO hash */
479 /* check if there is already a copy of the string */
480 result = strset_insert(&stringset, string);
481 if(result != string) {
482 obstack_free(&symbol_obstack, string);
485 const char *const result = string;
488 pp_token.type = TP_STRING_LITERAL;
489 pp_token.literal.begin = result;
490 pp_token.literal.size = size;
493 static void parse_wide_character_constant(void)
501 found_char = parse_escape_sequence();
505 parse_error("newline while parsing character constant");
511 goto end_of_wide_char_constant;
514 parse_error("EOF while parsing character constant");
515 pp_token.type = TP_ERROR;
519 if(found_char != 0) {
520 parse_error("more than 1 characters in character "
522 goto end_of_wide_char_constant;
524 found_char = input.c;
531 end_of_wide_char_constant:
532 pp_token.type = TP_WIDE_CHARACTER_CONSTANT;
536 static void parse_character_constant(void)
538 const unsigned start_linenr = input.position.lineno;
546 tc = parse_escape_sequence();
547 obstack_1grow(&symbol_obstack, (char) tc);
551 parse_error("newline while parsing character constant");
556 source_position_t source_position;
557 source_position.input_name = pp_token.source_position.input_name;
558 source_position.lineno = start_linenr;
559 errorf(&source_position, "EOF while parsing character constant");
560 pp_token.type = TP_ERROR;
566 goto end_of_char_constant;
569 obstack_1grow(&symbol_obstack, (char) input.c);
576 end_of_char_constant:;
577 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
578 const char *const string = obstack_finish(&symbol_obstack);
580 pp_token.type = TP_CHARACTER_CONSTANT;
581 pp_token.literal.begin = string;
582 pp_token.literal.size = size;
585 #define SYMBOL_CHARS_WITHOUT_E_P \
636 #define SYMBOL_CHARS \
637 SYMBOL_CHARS_WITHOUT_E_P \
656 * returns next final token from a preprocessor macro expansion
658 static void expand_next(void)
660 assert(current_expansion != NULL);
662 pp_definition_t *definition = current_expansion;
665 if(definition->list_len == 0
666 || definition->expand_pos >= definition->list_len) {
667 /* we're finished with the current macro, move up 1 level in the
669 pp_definition_t *parent = definition->parent_expansion;
670 definition->parent_expansion = NULL;
671 definition->is_expanding = false;
673 /* it was the outermost expansion, parse normal pptoken */
675 current_expansion = NULL;
676 next_preprocessing_token();
680 current_expansion = definition;
683 pp_token = definition->token_list[definition->expand_pos];
684 ++definition->expand_pos;
686 if(pp_token.type != TP_IDENTIFIER)
689 /* if it was an identifier then we might need to expand again */
690 pp_definition_t *symbol_definition = pp_token.symbol->pp_definition;
691 if(symbol_definition != NULL && !symbol_definition->is_expanding) {
692 symbol_definition->parent_expansion = definition;
693 symbol_definition->expand_pos = 0;
694 symbol_definition->is_expanding = true;
695 definition = symbol_definition;
696 current_expansion = definition;
701 static void skip_line_comment(void)
722 static void skip_multiline_comment(void)
727 unsigned start_linenr = input.position.lineno;
732 if (input.c == '*') {
733 /* TODO: nested comment, warn here */
745 if(do_print_spaces) {
753 source_position_t source_position;
754 source_position.input_name = pp_token.source_position.input_name;
755 source_position.lineno = start_linenr;
756 errorf(&source_position, "at end of file while looking for comment end");
767 /* skip spaces advancing at the start of the next preprocessing token */
768 static void skip_spaces(bool skip_newline)
780 if (input.c == '/') {
784 } else if (input.c == '*') {
786 skip_multiline_comment();
799 if(input.c == '\n') {
802 ++input.position.lineno;
812 ++input.position.lineno;
823 static void eat_pp(int type)
826 assert(pp_token.type == type);
827 next_preprocessing_token();
830 static void parse_symbol(void)
832 obstack_1grow(&symbol_obstack, (char) input.c);
839 obstack_1grow(&symbol_obstack, (char) input.c);
849 obstack_1grow(&symbol_obstack, '\0');
850 char *string = obstack_finish(&symbol_obstack);
852 /* might be a wide string or character constant ( L"string"/L'c' ) */
853 if (input.c == '"' && string[0] == 'L' && string[1] == '\0') {
854 obstack_free(&symbol_obstack, string);
857 } else if (input.c == '\'' && string[0] == 'L' && string[1] == '\0') {
858 obstack_free(&symbol_obstack, string);
859 parse_wide_character_constant();
863 symbol_t *symbol = symbol_table_insert(string);
865 pp_token.type = symbol->pp_ID;
866 pp_token.symbol = symbol;
868 /* we can free the memory from symbol obstack if we already had an entry in
869 * the symbol table */
870 if (symbol->string != string) {
871 obstack_free(&symbol_obstack, string);
876 pp_definition_t *pp_definition = symbol->pp_definition;
877 if (pp_definition == NULL)
880 if (pp_definition->has_parameters) {
882 /* no opening brace -> no expansion */
885 next_preprocessing_token();
888 /* parse arguments (TODO) */
889 while (pp_token.type != TP_EOF && pp_token.type != ')')
890 next_preprocessing_token();
891 next_preprocessing_token();
894 pp_definition->expand_pos = 0;
895 pp_definition->is_expanding = true,
896 current_expansion = pp_definition;
900 static void parse_number(void)
902 obstack_1grow(&symbol_obstack, (char) input.c);
909 SYMBOL_CHARS_WITHOUT_E_P
910 obstack_1grow(&symbol_obstack, (char) input.c);
918 obstack_1grow(&symbol_obstack, (char) input.c);
920 if(input.c == '+' || input.c == '-') {
921 obstack_1grow(&symbol_obstack, (char) input.c);
932 obstack_1grow(&symbol_obstack, '\0');
933 size_t size = obstack_object_size(&symbol_obstack);
934 char *string = obstack_finish(&symbol_obstack);
936 pp_token.type = TP_NUMBER;
937 pp_token.literal.begin = string;
938 pp_token.literal.size = size;
942 #define MAYBE_PROLOG \
947 #define MAYBE(ch, set_type) \
950 pp_token.type = set_type; \
953 #define ELSE_CODE(code) \
958 } /* end of while(1) */ \
960 #define ELSE(set_type) \
962 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 = '.';
1034 MAYBE('&', TP_ANDAND)
1035 MAYBE('=', TP_ANDEQUAL)
1039 MAYBE('=', TP_ASTERISKEQUAL)
1043 MAYBE('+', TP_PLUSPLUS)
1044 MAYBE('=', TP_PLUSEQUAL)
1048 MAYBE('>', TP_MINUSGREATER)
1049 MAYBE('-', TP_MINUSMINUS)
1050 MAYBE('=', TP_MINUSEQUAL)
1054 MAYBE('=', TP_EXCLAMATIONMARKEQUAL)
1058 MAYBE('=', TP_SLASHEQUAL)
1061 skip_multiline_comment();
1065 skip_line_comment();
1071 MAYBE('=', TP_PERCENTEQUAL)
1076 MAYBE(':', TP_HASHHASH)
1080 pp_token.type = '#';
1088 MAYBE('=', TP_LESSEQUAL)
1091 MAYBE('=', TP_LESSLESSEQUAL)
1096 MAYBE('=', TP_GREATEREQUAL)
1099 MAYBE('=', TP_GREATERGREATEREQUAL)
1100 ELSE(TP_GREATERGREATER)
1104 MAYBE('=', TP_CARETEQUAL)
1108 MAYBE('=', TP_PIPEEQUAL)
1109 MAYBE('|', TP_PIPEPIPE)
1117 MAYBE('=', TP_EQUALEQUAL)
1121 MAYBE('#', TP_HASHHASH)
1135 pp_token.type = input.c;
1140 if (input_stack != NULL) {
1142 pop_restore_input();
1143 counted_newlines = 0;
1145 /* hack to output correct line number */
1146 print_line_directive(&input.position, "2");
1147 next_preprocessing_token();
1149 pp_token.type = TP_EOF;
1155 errorf(&pp_token.source_position, "unknown character '%c' found\n",
1157 pp_token.type = TP_ERROR;
1162 static void print_quoted_string(const char *const string)
1165 for (const char *c = string; *c != 0; ++c) {
1167 case '"': fputs("\\\"", out); break;
1168 case '\\': fputs("\\\\", out); break;
1169 case '\a': fputs("\\a", out); break;
1170 case '\b': fputs("\\b", out); break;
1171 case '\f': fputs("\\f", out); break;
1172 case '\n': fputs("\\n", out); break;
1173 case '\r': fputs("\\r", out); break;
1174 case '\t': fputs("\\t", out); break;
1175 case '\v': fputs("\\v", out); break;
1176 case '\?': fputs("\\?", out); break;
1179 fprintf(out, "\\%03o", (unsigned)*c);
1189 static void print_line_directive(const source_position_t *pos, const char *add)
1191 fprintf(out, "# %u ", pos->lineno);
1192 print_quoted_string(pos->input_name);
1199 printed_input_name = pos->input_name;
1202 static void print_spaces(void)
1204 if (counted_newlines >= 9) {
1205 if (input.had_non_space) {
1208 print_line_directive(&pp_token.source_position, NULL);
1209 counted_newlines = 0;
1211 for (unsigned i = 0; i < counted_newlines; ++i)
1213 counted_newlines = 0;
1215 for (unsigned i = 0; i < counted_spaces; ++i)
1220 static void emit_pp_token(void)
1225 if (pp_token.type != '\n') {
1227 input.had_non_space = true;
1230 switch(pp_token.type) {
1232 fputs(pp_token.symbol->string, out);
1235 fputs(pp_token.literal.begin, out);
1237 case TP_STRING_LITERAL:
1239 fputs(pp_token.literal.begin, out);
1245 print_pp_token_type(out, pp_token.type);
1250 static void eat_pp_directive(void)
1252 while(pp_token.type != '\n' && pp_token.type != TP_EOF) {
1253 next_preprocessing_token();
1257 static bool strings_equal(const string_t *string1, const string_t *string2)
1259 size_t size = string1->size;
1260 if(size != string2->size)
1263 const char *c1 = string1->begin;
1264 const char *c2 = string2->begin;
1265 for(size_t i = 0; i < size; ++i, ++c1, ++c2) {
1272 static bool pp_tokens_equal(const token_t *token1, const token_t *token2)
1274 if(token1->type != token2->type)
1277 switch(token1->type) {
1282 return token1->symbol == token2->symbol;
1284 case TP_CHARACTER_CONSTANT:
1285 case TP_STRING_LITERAL:
1286 return strings_equal(&token1->literal, &token2->literal);
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->token_list;
1301 const token_t *t2 = definition2->token_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)
1312 assert(obstack_object_size(&pp_obstack) == 0);
1314 if (pp_token.type != TP_IDENTIFIER) {
1315 errorf(&pp_token.source_position,
1316 "expected identifier after #define, got '%t'", &pp_token);
1319 symbol_t *symbol = pp_token.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 if (input.c == '(') {
1331 next_preprocessing_token();
1332 /* get next token after '(' */
1333 next_preprocessing_token();
1336 switch (pp_token.type) {
1338 new_definition->is_variadic = true;
1339 next_preprocessing_token();
1340 if (pp_token.type != ')') {
1341 errorf(&input.position,
1342 "'...' not at end of macro argument list");
1347 obstack_ptr_grow(&pp_obstack, pp_token.symbol);
1348 next_preprocessing_token();
1350 if (pp_token.type == ',') {
1351 next_preprocessing_token();
1355 if (pp_token.type != ')') {
1356 errorf(&pp_token.source_position,
1357 "expected ',' or ')' after identifier, got '%t'",
1363 next_preprocessing_token();
1364 goto finish_argument_list;
1366 errorf(&pp_token.source_position,
1367 "expected identifier, '...' or ')' in #define argument list, got '%t'",
1373 finish_argument_list:
1374 new_definition->has_parameters = true;
1375 new_definition->n_parameters
1376 = obstack_object_size(&pp_obstack) / sizeof(new_definition->parameters[0]);
1377 new_definition->parameters = obstack_finish(&pp_obstack);
1379 next_preprocessing_token();
1382 /* construct a new pp_definition on the obstack */
1383 assert(obstack_object_size(&pp_obstack) == 0);
1384 size_t list_len = 0;
1385 while (pp_token.type != '\n' && pp_token.type != TP_EOF) {
1386 obstack_grow(&pp_obstack, &pp_token, sizeof(pp_token));
1388 next_preprocessing_token();
1391 new_definition->list_len = list_len;
1392 new_definition->token_list = obstack_finish(&pp_obstack);
1394 pp_definition_t *old_definition = symbol->pp_definition;
1395 if (old_definition != NULL) {
1396 if (!pp_definitions_equal(old_definition, new_definition)) {
1397 warningf(WARN_OTHER, &input.position, "multiple definition of macro '%Y' (first defined %P)", symbol, &old_definition->source_position);
1399 /* reuse the old definition */
1400 obstack_free(&pp_obstack, new_definition);
1401 new_definition = old_definition;
1405 symbol->pp_definition = new_definition;
1409 if (obstack_object_size(&pp_obstack) > 0) {
1410 char *ptr = obstack_finish(&pp_obstack);
1411 obstack_free(&pp_obstack, ptr);
1416 static void parse_undef_directive(void)
1420 if(pp_token.type != TP_IDENTIFIER) {
1421 errorf(&input.position,
1422 "expected identifier after #undef, got '%t'", &pp_token);
1427 symbol_t *symbol = pp_token.symbol;
1428 symbol->pp_definition = NULL;
1429 next_preprocessing_token();
1431 if(pp_token.type != '\n') {
1432 warningf(WARN_OTHER, &input.position, "extra tokens at end of #undef directive");
1434 /* eat until '\n' */
1438 static const char *parse_headername(void)
1440 /* behind an #include we can have the special headername lexems.
1441 * They're only allowed behind an #include so they're not recognized
1442 * by the normal next_preprocessing_token. We handle them as a special
1445 /* skip spaces so we reach start of next preprocessing token */
1448 assert(obstack_object_size(&input_obstack) == 0);
1450 /* check wether we have a "... or <... headername */
1453 /* for now until we have proper searchpath handling */
1454 obstack_1grow(&input_obstack, '.');
1455 obstack_1grow(&input_obstack, '/');
1463 parse_error("header name without closing '>'");
1468 goto finished_headername;
1470 obstack_1grow(&input_obstack, (char) input.c);
1473 /* we should never be here */
1476 /* for now until we have proper searchpath handling */
1477 obstack_1grow(&input_obstack, '.');
1478 obstack_1grow(&input_obstack, '/');
1486 parse_error("header name without closing '>'");
1491 goto finished_headername;
1493 obstack_1grow(&input_obstack, (char) input.c);
1496 /* we should never be here */
1499 /* TODO: do normale pp_token parsing and concatenate results */
1500 panic("pp_token concat include not implemented yet");
1503 finished_headername:
1504 obstack_1grow(&input_obstack, '\0');
1505 char *headername = obstack_finish(&input_obstack);
1507 /* TODO: iterate search-path to find the file */
1509 next_preprocessing_token();
1514 static bool parse_include_directive(void)
1516 /* don't eat the TP_include here!
1517 * we need an alternative parsing for the next token */
1521 const char *headername = parse_headername();
1522 if (headername == NULL) {
1527 if (pp_token.type != '\n' && pp_token.type != TP_EOF) {
1528 warningf(WARN_OTHER, &pp_token.source_position, "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(WARN_OTHER, &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(WARN_OTHER, &pp_token.source_position, "extra tokens at end of #endif");
1681 pp_conditional_t *conditional = conditional_stack;
1682 if (conditional == NULL) {
1683 errorf(&pp_token.source_position, "#endif without prior #if");
1687 if (!conditional->skip) {
1693 static void parse_preprocessing_directive(void)
1695 do_print_spaces = false;
1696 do_expansions = false;
1700 switch(pp_token.type) {
1703 parse_ifdef_ifndef_directive();
1706 parse_else_directive();
1709 parse_endif_directive();
1716 switch(pp_token.type) {
1718 parse_define_directive();
1721 parse_undef_directive();
1725 parse_ifdef_ifndef_directive();
1728 parse_else_directive();
1731 parse_endif_directive();
1734 bool in_new_source = parse_include_directive();
1735 /* no need to do anything if source file switched */
1741 /* the nop directive */
1744 errorf(&pp_token.source_position,
1745 "invalid preprocessing directive #%t", &pp_token);
1751 do_print_spaces = true;
1752 do_expansions = true;
1755 assert(pp_token.type == '\n' || pp_token.type == TP_EOF);
1756 next_preprocessing_token();
1759 #define GCC_COMPAT_MODE
1761 int pptest_main(int argc, char **argv);
1762 int pptest_main(int argc, char **argv)
1764 init_symbol_table();
1767 obstack_init(&pp_obstack);
1768 obstack_init(&input_obstack);
1770 const char *filename = "t.c";
1776 #ifdef GCC_COMPAT_MODE
1777 /* this is here so we can directly compare "gcc -E" output and our output */
1778 fprintf(out, "# 1 \"%s\"\n", filename);
1779 fputs("# 1 \"<built-in>\"\n", out);
1780 fputs("# 1 \"<command-line>\"\n", out);
1783 bool ok = open_input(filename);
1787 /* we're at a line begin */
1788 if(pp_token.type == '#') {
1789 parse_preprocessing_directive();
1791 /* parse+emit a line */
1792 while(pp_token.type != '\n') {
1793 if(pp_token.type == TP_EOF)
1794 goto end_of_main_loop;
1796 next_preprocessing_token();
1799 next_preprocessing_token();
1804 check_unclosed_conditionals();
1807 obstack_free(&input_obstack, NULL);
1808 obstack_free(&pp_obstack, NULL);
1811 exit_symbol_table();