12 #include "adt/error.h"
13 #include "adt/strutil.h"
14 #include "adt/strset.h"
15 #include "lang_features.h"
16 #include "diagnostic.h"
17 #include "string_rep.h"
21 #define INCLUDE_LIMIT 199 /* 199 is for gcc "compatibility" */
23 struct pp_argument_t {
28 struct pp_definition_t {
30 source_position_t source_position;
31 pp_definition_t *parent_expansion;
34 bool is_expanding : 1;
35 bool has_parameters : 1;
45 typedef struct pp_conditional_t pp_conditional_t;
46 struct pp_conditional_t {
47 source_position_t source_position;
50 bool skip; /**< conditional in skip mode (then+else gets skipped) */
51 pp_conditional_t *parent;
54 typedef struct pp_input_t pp_input_t;
59 utf32 buf[1024+MAX_PUTBACK];
62 source_position_t position;
67 /** additional info about the current token */
68 typedef struct add_token_info_t {
69 /** whitespace from beginning of line to the token */
71 /** there has been any whitespace before the token */
73 /** the token is at the beginning of the line */
77 typedef struct searchpath_entry_t searchpath_entry_t;
78 struct searchpath_entry_t {
80 searchpath_entry_t *next;
83 static pp_input_t input;
85 static pp_input_t *input_stack;
86 static unsigned n_inputs;
87 static struct obstack input_obstack;
89 static pp_conditional_t *conditional_stack;
91 static token_t pp_token;
92 static bool resolve_escape_sequences = false;
93 static bool ignore_unknown_chars = true;
94 static bool in_pp_directive;
95 static bool skip_mode;
97 static struct obstack pp_obstack;
98 static struct obstack config_obstack;
99 static const char *printed_input_name = NULL;
100 static source_position_t expansion_pos;
101 static pp_definition_t *current_expansion = NULL;
102 static strset_t stringset;
103 static preprocessor_token_type_t last_token = TP_ERROR;
105 static searchpath_entry_t *searchpath;
107 static add_token_info_t info;
109 static inline void next_char(void);
110 static void next_preprocessing_token(void);
111 static void print_line_directive(const source_position_t *pos, const char *add);
113 static void switch_input(FILE *file, const char *filename)
116 input.input = input_from_stream(file, NULL);
119 input.output_line = 0;
120 input.position.input_name = filename;
121 input.position.lineno = 1;
123 /* indicate that we're at a new input */
124 print_line_directive(&input.position, input_stack != NULL ? "1" : NULL);
126 /* place a virtual '\n' so we realize we're at line begin */
127 input.position.lineno = 0;
129 next_preprocessing_token();
132 static void close_input(void)
134 input_free(input.input);
135 assert(input.file != NULL);
145 static void push_input(void)
147 pp_input_t *saved_input
148 = obstack_alloc(&input_obstack, sizeof(*saved_input));
150 memcpy(saved_input, &input, sizeof(*saved_input));
152 /* adjust buffer positions */
153 if (input.bufpos != NULL)
154 saved_input->bufpos = saved_input->buf + (input.bufpos - input.buf);
155 if (input.bufend != NULL)
156 saved_input->bufend = saved_input->buf + (input.bufend - input.buf);
158 saved_input->parent = input_stack;
159 input_stack = saved_input;
163 static void pop_restore_input(void)
165 assert(n_inputs > 0);
166 assert(input_stack != NULL);
168 pp_input_t *saved_input = input_stack;
170 memcpy(&input, saved_input, sizeof(input));
173 /* adjust buffer positions */
174 if (saved_input->bufpos != NULL)
175 input.bufpos = input.buf + (saved_input->bufpos - saved_input->buf);
176 if (saved_input->bufend != NULL)
177 input.bufend = input.buf + (saved_input->bufend - saved_input->buf);
179 input_stack = saved_input->parent;
180 obstack_free(&input_obstack, saved_input);
185 * Prints a parse error message at the current token.
187 * @param msg the error message
189 static void parse_error(const char *msg)
191 errorf(&pp_token.source_position, "%s", msg);
194 static inline void next_real_char(void)
196 assert(input.bufpos <= input.bufend);
197 if (input.bufpos >= input.bufend) {
198 size_t n = decode(input.input, input.buf + MAX_PUTBACK,
199 sizeof(input.buf)/sizeof(input.buf[0]) - MAX_PUTBACK);
204 input.bufpos = input.buf + MAX_PUTBACK;
205 input.bufend = input.bufpos + n;
207 input.c = *input.bufpos++;
208 ++input.position.colno;
212 * Put a character back into the buffer.
214 * @param pc the character to put back
216 static inline void put_back(utf32 const pc)
218 assert(input.bufpos > input.buf);
219 *(--input.bufpos - input.buf + input.buf) = (char) pc;
220 --input.position.colno;
223 #define MATCH_NEWLINE(code) \
226 if (input.c == '\n') { \
230 info.whitespace = 0; \
231 ++input.position.lineno; \
232 input.position.colno = 1; \
235 #define eat(c_type) (assert(input.c == c_type), next_char())
237 static void maybe_concat_lines(void)
255 * Set c to the next input character, ie.
256 * after expanding trigraphs.
258 static inline void next_char(void)
262 /* filter trigraphs and concatenated lines */
263 if (UNLIKELY(input.c == '\\')) {
264 maybe_concat_lines();
265 goto end_of_next_char;
268 if (LIKELY(input.c != '?'))
269 goto end_of_next_char;
272 if (LIKELY(input.c != '?')) {
275 goto end_of_next_char;
280 case '=': input.c = '#'; break;
281 case '(': input.c = '['; break;
282 case '/': input.c = '\\'; maybe_concat_lines(); break;
283 case ')': input.c = ']'; break;
284 case '\'': input.c = '^'; break;
285 case '<': input.c = '{'; break;
286 case '!': input.c = '|'; break;
287 case '>': input.c = '}'; break;
288 case '-': input.c = '~'; break;
298 printf("nchar '%c'\n", input.c);
305 * Returns true if the given char is a octal digit.
307 * @param char the character to check
309 static inline bool is_octal_digit(int chr)
327 * Returns the value of a digit.
328 * The only portable way to do it ...
330 static int digit_value(int digit)
356 panic("wrong character given");
361 * Parses an octal character sequence.
363 * @param first_digit the already read first digit
365 static int parse_octal_sequence(const int first_digit)
367 assert(is_octal_digit(first_digit));
368 int value = digit_value(first_digit);
369 if (!is_octal_digit(input.c)) return value;
370 value = 8 * value + digit_value(input.c);
372 if (!is_octal_digit(input.c)) return value;
373 value = 8 * value + digit_value(input.c);
376 if (char_is_signed) {
377 return (signed char) value;
379 return (unsigned char) value;
384 * Parses a hex character sequence.
386 static int parse_hex_sequence(void)
389 while (isxdigit(input.c)) {
390 value = 16 * value + digit_value(input.c);
394 if (char_is_signed) {
395 return (signed char) value;
397 return (unsigned char) value;
402 * Parse an escape sequence.
404 static int parse_escape_sequence(void)
412 case '"': return '"';
413 case '\'': return '\'';
414 case '\\': return '\\';
415 case '?': return '\?';
416 case 'a': return '\a';
417 case 'b': return '\b';
418 case 'f': return '\f';
419 case 'n': return '\n';
420 case 'r': return '\r';
421 case 't': return '\t';
422 case 'v': return '\v';
424 return parse_hex_sequence();
433 return parse_octal_sequence(ec);
435 parse_error("reached end of file while parsing escape sequence");
438 parse_error("unknown escape sequence");
443 static void grow_symbol(utf32 const tc)
445 struct obstack *const o = &symbol_obstack;
447 obstack_1grow(o, tc);
448 } else if (tc < 0x800) {
449 obstack_1grow(o, 0xC0 | (tc >> 6));
450 obstack_1grow(o, 0x80 | (tc & 0x3F));
451 } else if (tc < 0x10000) {
452 obstack_1grow(o, 0xE0 | ( tc >> 12));
453 obstack_1grow(o, 0x80 | ((tc >> 6) & 0x3F));
454 obstack_1grow(o, 0x80 | ( tc & 0x3F));
456 obstack_1grow(o, 0xF0 | ( tc >> 18));
457 obstack_1grow(o, 0x80 | ((tc >> 12) & 0x3F));
458 obstack_1grow(o, 0x80 | ((tc >> 6) & 0x3F));
459 obstack_1grow(o, 0x80 | ( tc & 0x3F));
463 static const char *identify_string(char *string)
465 const char *result = strset_insert(&stringset, string);
466 if (result != string) {
467 obstack_free(&symbol_obstack, string);
472 static string_t make_string(char *string, size_t len)
474 const char *result = identify_string(string);
475 return (string_t) {result, len};
478 static void parse_string_literal(void)
480 const unsigned start_linenr = input.position.lineno;
488 if (resolve_escape_sequences) {
489 tc = parse_escape_sequence();
490 obstack_1grow(&symbol_obstack, (char) tc);
492 obstack_1grow(&symbol_obstack, (char) input.c);
494 obstack_1grow(&symbol_obstack, (char) input.c);
501 source_position_t source_position;
502 source_position.input_name = pp_token.source_position.input_name;
503 source_position.lineno = start_linenr;
504 errorf(&source_position, "string has no end");
505 pp_token.type = TP_ERROR;
514 grow_symbol(input.c);
521 /* add finishing 0 to the string */
522 obstack_1grow(&symbol_obstack, '\0');
523 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
524 char *const string = obstack_finish(&symbol_obstack);
526 pp_token.type = TP_STRING_LITERAL;
527 pp_token.literal = make_string(string, size);
531 * Parse a wide string literal and set lexer_token.
533 static void parse_wide_string_literal(void)
535 parse_string_literal();
536 if (pp_token.type == TP_STRING_LITERAL)
537 pp_token.type = TP_WIDE_STRING_LITERAL;
540 static void parse_wide_character_constant(void)
547 const utf32 tc = parse_escape_sequence();
553 parse_error("newline while parsing character constant");
559 goto end_of_wide_char_constant;
562 parse_error("EOF while parsing character constant");
563 pp_token.type = TP_ERROR;
567 grow_symbol(input.c);
573 end_of_wide_char_constant:
574 obstack_1grow(&symbol_obstack, '\0');
575 size_t size = (size_t) obstack_object_size(&symbol_obstack)-1;
576 char *string = obstack_finish(&symbol_obstack);
577 pp_token.type = TP_WIDE_CHARACTER_CONSTANT;
578 pp_token.literal = make_string(string, size);
581 parse_error("empty character constant");
585 static void parse_character_constant(void)
587 const unsigned start_linenr = input.position.lineno;
595 tc = parse_escape_sequence();
596 obstack_1grow(&symbol_obstack, (char) tc);
600 parse_error("newline while parsing character constant");
605 source_position_t source_position;
606 source_position.input_name = pp_token.source_position.input_name;
607 source_position.lineno = start_linenr;
608 errorf(&source_position, "EOF while parsing character constant");
609 pp_token.type = TP_ERROR;
615 goto end_of_char_constant;
618 obstack_1grow(&symbol_obstack, (char) input.c);
625 end_of_char_constant:;
626 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
627 const char *const string = obstack_finish(&symbol_obstack);
629 pp_token.type = TP_CHARACTER_CONSTANT;
630 pp_token.literal.begin = string;
631 pp_token.literal.size = size;
634 #define SYMBOL_CHARS_WITHOUT_E_P \
685 #define SYMBOL_CHARS \
686 SYMBOL_CHARS_WITHOUT_E_P \
705 * returns next final token from a preprocessor macro expansion
707 static void expand_next(void)
709 assert(current_expansion != NULL);
711 pp_definition_t *definition = current_expansion;
714 if (definition->list_len == 0
715 || definition->expand_pos >= definition->list_len) {
716 /* we're finished with the current macro, move up 1 level in the
718 pp_definition_t *parent = definition->parent_expansion;
719 definition->parent_expansion = NULL;
720 definition->is_expanding = false;
722 /* it was the outermost expansion, parse normal pptoken */
723 if (parent == NULL) {
724 current_expansion = NULL;
725 next_preprocessing_token();
729 current_expansion = definition;
732 pp_token = definition->token_list[definition->expand_pos];
733 pp_token.source_position = expansion_pos;
734 ++definition->expand_pos;
736 if (pp_token.type != TP_IDENTIFIER)
739 /* if it was an identifier then we might need to expand again */
740 pp_definition_t *symbol_definition = pp_token.symbol->pp_definition;
741 if (symbol_definition != NULL && !symbol_definition->is_expanding) {
742 symbol_definition->parent_expansion = definition;
743 symbol_definition->expand_pos = 0;
744 symbol_definition->is_expanding = true;
745 definition = symbol_definition;
746 current_expansion = definition;
751 static void skip_line_comment(void)
769 static void skip_multiline_comment(void)
771 unsigned start_linenr = input.position.lineno;
776 if (input.c == '*') {
777 /* TODO: nested comment, warn here */
782 if (input.c == '/') {
784 info.whitespace += input.position.colno-1;
790 info.at_line_begin |= !in_pp_directive;
795 source_position_t source_position;
796 source_position.input_name = pp_token.source_position.input_name;
797 source_position.lineno = start_linenr;
798 errorf(&source_position, "at end of file while looking for comment end");
809 static void skip_whitespace(void)
819 info.at_line_begin = true;
825 if (input.c == '/') {
829 } else if (input.c == '*') {
831 skip_multiline_comment();
844 static void eat_pp(int type)
847 assert(pp_token.type == type);
848 next_preprocessing_token();
851 static void parse_symbol(void)
853 obstack_1grow(&symbol_obstack, (char) input.c);
860 obstack_1grow(&symbol_obstack, (char) input.c);
870 obstack_1grow(&symbol_obstack, '\0');
871 char *string = obstack_finish(&symbol_obstack);
873 /* might be a wide string or character constant ( L"string"/L'c' ) */
874 if (input.c == '"' && string[0] == 'L' && string[1] == '\0') {
875 obstack_free(&symbol_obstack, string);
876 parse_wide_string_literal();
878 } else if (input.c == '\'' && string[0] == 'L' && string[1] == '\0') {
879 obstack_free(&symbol_obstack, string);
880 parse_wide_character_constant();
884 symbol_t *symbol = symbol_table_insert(string);
886 pp_token.type = symbol->pp_ID;
887 pp_token.symbol = symbol;
889 /* we can free the memory from symbol obstack if we already had an entry in
890 * the symbol table */
891 if (symbol->string != string) {
892 obstack_free(&symbol_obstack, string);
896 static void parse_number(void)
898 obstack_1grow(&symbol_obstack, (char) input.c);
905 SYMBOL_CHARS_WITHOUT_E_P
906 obstack_1grow(&symbol_obstack, (char) input.c);
914 obstack_1grow(&symbol_obstack, (char) input.c);
916 if (input.c == '+' || input.c == '-') {
917 obstack_1grow(&symbol_obstack, (char) input.c);
928 obstack_1grow(&symbol_obstack, '\0');
929 size_t size = obstack_object_size(&symbol_obstack);
930 char *string = obstack_finish(&symbol_obstack);
932 pp_token.type = TP_NUMBER;
933 pp_token.literal.begin = string;
934 pp_token.literal.size = size;
938 #define MAYBE_PROLOG \
943 #define MAYBE(ch, set_type) \
946 pp_token.type = set_type; \
949 #define ELSE_CODE(code) \
956 #define ELSE(set_type) \
958 pp_token.type = set_type; \
961 static void next_preprocessing_token(void)
963 if (current_expansion != NULL) {
968 info.at_line_begin = false;
969 info.had_whitespace = false;
971 pp_token.source_position = input.position;
976 info.had_whitespace = true;
981 info.at_line_begin = true;
982 info.had_whitespace = true;
995 parse_string_literal();
999 parse_character_constant();
1021 MAYBE('.', TP_DOTDOTDOT)
1025 pp_token.type = '.';
1030 MAYBE('&', TP_ANDAND)
1031 MAYBE('=', TP_ANDEQUAL)
1035 MAYBE('=', TP_ASTERISKEQUAL)
1039 MAYBE('+', TP_PLUSPLUS)
1040 MAYBE('=', TP_PLUSEQUAL)
1044 MAYBE('>', TP_MINUSGREATER)
1045 MAYBE('-', TP_MINUSMINUS)
1046 MAYBE('=', TP_MINUSEQUAL)
1050 MAYBE('=', TP_EXCLAMATIONMARKEQUAL)
1054 MAYBE('=', TP_SLASHEQUAL)
1057 info.had_whitespace = true;
1058 skip_multiline_comment();
1062 info.had_whitespace = true;
1063 skip_line_comment();
1069 MAYBE('=', TP_PERCENTEQUAL)
1074 MAYBE(':', TP_HASHHASH)
1078 pp_token.type = '#';
1086 MAYBE('=', TP_LESSEQUAL)
1089 MAYBE('=', TP_LESSLESSEQUAL)
1094 MAYBE('=', TP_GREATEREQUAL)
1097 MAYBE('=', TP_GREATERGREATEREQUAL)
1098 ELSE(TP_GREATERGREATER)
1102 MAYBE('=', TP_CARETEQUAL)
1106 MAYBE('=', TP_PIPEEQUAL)
1107 MAYBE('|', TP_PIPEPIPE)
1115 MAYBE('=', TP_EQUALEQUAL)
1119 MAYBE('#', TP_HASHHASH)
1121 pp_token.type = '#';
1135 pp_token.type = input.c;
1140 if (input_stack != NULL) {
1142 pop_restore_input();
1144 print_line_directive(&input.position, "2");
1147 pp_token.source_position.lineno++;
1148 info.at_line_begin = true;
1149 pp_token.type = TP_EOF;
1155 if (!ignore_unknown_chars) {
1156 errorf(&pp_token.source_position, "unknown character '%c' found\n",
1158 pp_token.type = TP_ERROR;
1160 pp_token.type = input.c;
1166 static void print_quoted_string(const char *const string)
1169 for (const char *c = string; *c != 0; ++c) {
1171 case '"': fputs("\\\"", out); break;
1172 case '\\': fputs("\\\\", out); break;
1173 case '\a': fputs("\\a", out); break;
1174 case '\b': fputs("\\b", out); break;
1175 case '\f': fputs("\\f", out); break;
1176 case '\n': fputs("\\n", out); break;
1177 case '\r': fputs("\\r", out); break;
1178 case '\t': fputs("\\t", out); break;
1179 case '\v': fputs("\\v", out); break;
1180 case '\?': fputs("\\?", out); break;
1183 fprintf(out, "\\%03o", (unsigned)*c);
1193 static void print_line_directive(const source_position_t *pos, const char *add)
1195 fprintf(out, "# %u ", pos->lineno);
1196 print_quoted_string(pos->input_name);
1202 printed_input_name = pos->input_name;
1203 input.output_line = pos->lineno-1;
1206 static void emit_newlines(void)
1208 unsigned delta = pp_token.source_position.lineno - input.output_line;
1212 print_line_directive(&pp_token.source_position, NULL);
1215 for (unsigned i = 0; i < delta; ++i) {
1219 input.output_line = pp_token.source_position.lineno;
1222 static void emit_pp_token(void)
1227 if (info.at_line_begin) {
1230 for (unsigned i = 0; i < info.whitespace; ++i)
1233 } else if (info.had_whitespace ||
1234 tokens_would_paste(last_token, pp_token.type)) {
1238 switch (pp_token.type) {
1240 fputs(pp_token.symbol->string, out);
1243 fputs(pp_token.literal.begin, out);
1245 case TP_WIDE_STRING_LITERAL:
1247 case TP_STRING_LITERAL:
1249 fputs(pp_token.literal.begin, out);
1252 case TP_WIDE_CHARACTER_CONSTANT:
1254 case TP_CHARACTER_CONSTANT:
1256 fputs(pp_token.literal.begin, out);
1260 print_pp_token_type(out, pp_token.type);
1263 last_token = pp_token.type;
1266 static void eat_pp_directive(void)
1268 while (!info.at_line_begin) {
1269 next_preprocessing_token();
1273 static bool strings_equal(const string_t *string1, const string_t *string2)
1275 size_t size = string1->size;
1276 if (size != string2->size)
1279 const char *c1 = string1->begin;
1280 const char *c2 = string2->begin;
1281 for (size_t i = 0; i < size; ++i, ++c1, ++c2) {
1288 static bool pp_tokens_equal(const token_t *token1, const token_t *token2)
1290 if (token1->type != token2->type)
1293 switch (token1->type) {
1298 return token1->symbol == token2->symbol;
1300 case TP_CHARACTER_CONSTANT:
1301 case TP_STRING_LITERAL:
1302 return strings_equal(&token1->literal, &token2->literal);
1309 static bool pp_definitions_equal(const pp_definition_t *definition1,
1310 const pp_definition_t *definition2)
1312 if (definition1->list_len != definition2->list_len)
1315 size_t len = definition1->list_len;
1316 const token_t *t1 = definition1->token_list;
1317 const token_t *t2 = definition2->token_list;
1318 for (size_t i = 0; i < len; ++i, ++t1, ++t2) {
1319 if (!pp_tokens_equal(t1, t2))
1325 static void parse_define_directive(void)
1328 assert(obstack_object_size(&pp_obstack) == 0);
1330 if (pp_token.type != TP_IDENTIFIER || info.at_line_begin) {
1331 errorf(&pp_token.source_position,
1332 "expected identifier after #define, got '%t'", &pp_token);
1335 symbol_t *symbol = pp_token.symbol;
1337 pp_definition_t *new_definition
1338 = obstack_alloc(&pp_obstack, sizeof(new_definition[0]));
1339 memset(new_definition, 0, sizeof(new_definition[0]));
1340 new_definition->source_position = input.position;
1342 /* this is probably the only place where spaces are significant in the
1343 * lexer (except for the fact that they separate tokens). #define b(x)
1344 * is something else than #define b (x) */
1345 if (input.c == '(') {
1347 next_preprocessing_token();
1348 /* get next token after '(' */
1349 next_preprocessing_token();
1352 switch (pp_token.type) {
1354 new_definition->is_variadic = true;
1355 next_preprocessing_token();
1356 if (pp_token.type != ')') {
1357 errorf(&input.position,
1358 "'...' not at end of macro argument list");
1363 obstack_ptr_grow(&pp_obstack, pp_token.symbol);
1364 next_preprocessing_token();
1366 if (pp_token.type == ',') {
1367 next_preprocessing_token();
1371 if (pp_token.type != ')') {
1372 errorf(&pp_token.source_position,
1373 "expected ',' or ')' after identifier, got '%t'",
1379 next_preprocessing_token();
1380 goto finish_argument_list;
1382 errorf(&pp_token.source_position,
1383 "expected identifier, '...' or ')' in #define argument list, got '%t'",
1389 finish_argument_list:
1390 new_definition->has_parameters = true;
1391 new_definition->n_parameters
1392 = obstack_object_size(&pp_obstack) / sizeof(new_definition->parameters[0]);
1393 new_definition->parameters = obstack_finish(&pp_obstack);
1395 next_preprocessing_token();
1398 /* construct a new pp_definition on the obstack */
1399 assert(obstack_object_size(&pp_obstack) == 0);
1400 size_t list_len = 0;
1401 while (!info.at_line_begin) {
1402 obstack_grow(&pp_obstack, &pp_token, sizeof(pp_token));
1404 next_preprocessing_token();
1407 new_definition->list_len = list_len;
1408 new_definition->token_list = obstack_finish(&pp_obstack);
1410 pp_definition_t *old_definition = symbol->pp_definition;
1411 if (old_definition != NULL) {
1412 if (!pp_definitions_equal(old_definition, new_definition)) {
1413 warningf(WARN_OTHER, &input.position, "multiple definition of macro '%Y' (first defined %P)", symbol, &old_definition->source_position);
1415 /* reuse the old definition */
1416 obstack_free(&pp_obstack, new_definition);
1417 new_definition = old_definition;
1421 symbol->pp_definition = new_definition;
1425 if (obstack_object_size(&pp_obstack) > 0) {
1426 char *ptr = obstack_finish(&pp_obstack);
1427 obstack_free(&pp_obstack, ptr);
1432 static void parse_undef_directive(void)
1436 if (pp_token.type != TP_IDENTIFIER) {
1437 errorf(&input.position,
1438 "expected identifier after #undef, got '%t'", &pp_token);
1443 symbol_t *symbol = pp_token.symbol;
1444 symbol->pp_definition = NULL;
1445 next_preprocessing_token();
1447 if (!info.at_line_begin) {
1448 warningf(WARN_OTHER, &input.position, "extra tokens at end of #undef directive");
1453 static const char *parse_headername(void)
1455 /* behind an #include we can have the special headername lexems.
1456 * They're only allowed behind an #include so they're not recognized
1457 * by the normal next_preprocessing_token. We handle them as a special
1459 if (info.at_line_begin) {
1460 parse_error("expected headername after #include");
1464 assert(obstack_object_size(&symbol_obstack) == 0);
1466 /* check wether we have a "... or <... headername */
1475 parse_error("header name without closing '>'");
1480 goto finished_headername;
1482 obstack_1grow(&symbol_obstack, (char) input.c);
1485 /* we should never be here */
1494 parse_error("header name without closing '>'");
1499 goto finished_headername;
1501 obstack_1grow(&symbol_obstack, (char) input.c);
1504 /* we should never be here */
1507 /* TODO: do normal pp_token parsing and concatenate results */
1508 panic("pp_token concat include not implemented yet");
1511 finished_headername:
1512 obstack_1grow(&symbol_obstack, '\0');
1513 char *headername = obstack_finish(&symbol_obstack);
1515 /* TODO: iterate search-path to find the file */
1519 return identify_string(headername);
1522 static bool do_include(bool system_include, const char *headername)
1524 if (!system_include) {
1525 /* for "bla" includes first try current dir
1526 * TODO: this isn't correct, should be the directory of the source file
1528 FILE *file = fopen(headername, "r");
1530 switch_input(file, headername);
1535 size_t headername_len = strlen(headername);
1536 assert(obstack_object_size(&pp_obstack) == 0);
1537 /* check searchpath */
1538 for (searchpath_entry_t *entry = searchpath; entry != NULL;
1539 entry = entry->next) {
1540 const char *path = entry->path;
1541 size_t len = strlen(path);
1542 obstack_grow(&pp_obstack, path, len);
1543 if (path[len-1] != '/')
1544 obstack_1grow(&pp_obstack, '/');
1545 obstack_grow(&pp_obstack, headername, headername_len+1);
1547 char *complete_path = obstack_finish(&pp_obstack);
1548 FILE *file = fopen(complete_path, "r");
1550 const char *filename = identify_string(complete_path);
1551 switch_input(file, filename);
1554 obstack_free(&pp_obstack, complete_path);
1560 static bool parse_include_directive(void)
1562 /* don't eat the TP_include here!
1563 * we need an alternative parsing for the next token */
1565 bool system_include = input.c == '<';
1566 const char *headername = parse_headername();
1567 if (headername == NULL) {
1572 if (!info.at_line_begin) {
1573 warningf(WARN_OTHER, &pp_token.source_position, "extra tokens at end of #include directive");
1577 if (n_inputs > INCLUDE_LIMIT) {
1578 errorf(&pp_token.source_position, "#include nested too deeply");
1580 next_preprocessing_token();
1584 /* we have to reenable space counting and macro expansion here,
1585 * because it is still disabled in directive parsing,
1586 * but we will trigger a preprocessing token reading of the new file
1587 * now and need expansions/space counting */
1588 in_pp_directive = false;
1593 bool res = do_include(system_include, headername);
1595 errorf(&pp_token.source_position,
1596 "failed including '%s': %s", headername, strerror(errno));
1597 pop_restore_input();
1604 static pp_conditional_t *push_conditional(void)
1606 pp_conditional_t *conditional
1607 = obstack_alloc(&pp_obstack, sizeof(*conditional));
1608 memset(conditional, 0, sizeof(*conditional));
1610 conditional->parent = conditional_stack;
1611 conditional_stack = conditional;
1616 static void pop_conditional(void)
1618 assert(conditional_stack != NULL);
1619 conditional_stack = conditional_stack->parent;
1622 static void check_unclosed_conditionals(void)
1624 while (conditional_stack != NULL) {
1625 pp_conditional_t *conditional = conditional_stack;
1627 if (conditional->in_else) {
1628 errorf(&conditional->source_position, "unterminated #else");
1630 errorf(&conditional->source_position, "unterminated condition");
1636 static void parse_ifdef_ifndef_directive(void)
1638 bool is_ifndef = (pp_token.type == TP_ifndef);
1640 next_preprocessing_token();
1644 pp_conditional_t *conditional = push_conditional();
1645 conditional->source_position = pp_token.source_position;
1646 conditional->skip = true;
1650 if (pp_token.type != TP_IDENTIFIER || info.at_line_begin) {
1651 errorf(&pp_token.source_position,
1652 "expected identifier after #%s, got '%t'",
1653 is_ifndef ? "ifndef" : "ifdef", &pp_token);
1656 /* just take the true case in the hope to avoid further errors */
1659 symbol_t *symbol = pp_token.symbol;
1660 pp_definition_t *pp_definition = symbol->pp_definition;
1661 next_preprocessing_token();
1663 if (!info.at_line_begin) {
1664 errorf(&pp_token.source_position,
1665 "extra tokens at end of #%s",
1666 is_ifndef ? "ifndef" : "ifdef");
1670 /* evaluate wether we are in true or false case */
1671 condition = is_ifndef ? pp_definition == NULL : pp_definition != NULL;
1674 pp_conditional_t *conditional = push_conditional();
1675 conditional->source_position = pp_token.source_position;
1676 conditional->condition = condition;
1683 static void parse_else_directive(void)
1687 if (!info.at_line_begin) {
1689 warningf(WARN_OTHER, &pp_token.source_position, "extra tokens at end of #else");
1694 pp_conditional_t *conditional = conditional_stack;
1695 if (conditional == NULL) {
1696 errorf(&pp_token.source_position, "#else without prior #if");
1700 if (conditional->in_else) {
1701 errorf(&pp_token.source_position,
1702 "#else after #else (condition started %P)",
1703 conditional->source_position);
1708 conditional->in_else = true;
1709 if (!conditional->skip) {
1710 skip_mode = conditional->condition;
1712 conditional->source_position = pp_token.source_position;
1715 static void parse_endif_directive(void)
1719 if (!info.at_line_begin) {
1721 warningf(WARN_OTHER, &pp_token.source_position, "extra tokens at end of #endif");
1726 pp_conditional_t *conditional = conditional_stack;
1727 if (conditional == NULL) {
1728 errorf(&pp_token.source_position, "#endif without prior #if");
1732 if (!conditional->skip) {
1738 static void parse_preprocessing_directive(void)
1740 in_pp_directive = true;
1744 switch (pp_token.type) {
1747 parse_ifdef_ifndef_directive();
1750 parse_else_directive();
1753 parse_endif_directive();
1760 switch (pp_token.type) {
1762 parse_define_directive();
1765 parse_undef_directive();
1769 parse_ifdef_ifndef_directive();
1772 parse_else_directive();
1775 parse_endif_directive();
1778 parse_include_directive();
1781 if (info.at_line_begin) {
1782 /* the nop directive "#" */
1785 errorf(&pp_token.source_position,
1786 "invalid preprocessing directive #%t", &pp_token);
1792 in_pp_directive = false;
1793 assert(info.at_line_begin);
1796 static void prepend_include_path(const char *path)
1798 searchpath_entry_t *entry = OALLOCZ(&config_obstack, searchpath_entry_t);
1800 entry->next = searchpath;
1804 static void setup_include_path(void)
1806 /* built-in paths */
1807 prepend_include_path("/usr/include");
1809 /* parse environment variable */
1810 const char *cpath = getenv("CPATH");
1811 if (cpath != NULL && *cpath != '\0') {
1812 const char *begin = cpath;
1816 while (*c != '\0' && *c != ':')
1819 size_t len = c-begin;
1821 /* for gcc compatibility (Matze: I would expect that
1822 * nothing happens for an empty entry...) */
1823 prepend_include_path(".");
1825 char *string = obstack_alloc(&config_obstack, len+1);
1826 memcpy(string, begin, len);
1829 prepend_include_path(string);
1836 } while(*c != '\0');
1840 int pptest_main(int argc, char **argv);
1841 int pptest_main(int argc, char **argv)
1843 init_symbol_table();
1846 obstack_init(&config_obstack);
1847 obstack_init(&pp_obstack);
1848 obstack_init(&input_obstack);
1849 strset_init(&stringset);
1851 setup_include_path();
1853 /* simplistic commandline parser */
1854 const char *filename = NULL;
1855 for (int i = 1; i < argc; ++i) {
1856 const char *opt = argv[i];
1857 if (streq(opt, "-I")) {
1858 prepend_include_path(argv[++i]);
1860 } else if (streq(opt, "-E")) {
1862 } else if (opt[0] == '-') {
1863 fprintf(stderr, "Unknown option '%s'\n", opt);
1865 if (filename != NULL)
1866 fprintf(stderr, "Multiple inputs not supported\n");
1870 if (filename == NULL) {
1871 fprintf(stderr, "No input specified\n");
1877 /* just here for gcc compatibility */
1878 fprintf(out, "# 1 \"%s\"\n", filename);
1879 fprintf(out, "# 1 \"<built-in>\"\n");
1880 fprintf(out, "# 1 \"<command-line>\"\n");
1882 FILE *file = fopen(filename, "r");
1884 fprintf(stderr, "Couldn't open input '%s'\n", filename);
1887 switch_input(file, filename);
1890 if (pp_token.type == '#' && info.at_line_begin) {
1891 parse_preprocessing_directive();
1893 } else if (pp_token.type == TP_EOF) {
1894 goto end_of_main_loop;
1895 } else if (pp_token.type == TP_IDENTIFIER && !in_pp_directive) {
1896 symbol_t *symbol = pp_token.symbol;
1897 pp_definition_t *pp_definition = symbol->pp_definition;
1898 if (pp_definition != NULL && !pp_definition->is_expanding) {
1899 expansion_pos = pp_token.source_position;
1900 if (pp_definition->has_parameters) {
1901 source_position_t position = pp_token.source_position;
1902 add_token_info_t old_info = info;
1903 next_preprocessing_token();
1904 add_token_info_t new_info = info;
1906 /* no opening brace -> no expansion */
1907 if (pp_token.type == '(') {
1910 /* parse arguments (TODO) */
1911 while (pp_token.type != TP_EOF && pp_token.type != ')')
1912 next_preprocessing_token();
1914 token_t next_token = pp_token;
1915 /* restore identifier token */
1916 pp_token.type = TP_IDENTIFIER;
1917 pp_token.symbol = symbol;
1918 pp_token.source_position = position;
1923 pp_token = next_token;
1928 pp_definition->expand_pos = 0;
1929 pp_definition->is_expanding = true;
1930 current_expansion = pp_definition;
1937 next_preprocessing_token();
1942 check_unclosed_conditionals();
1945 obstack_free(&input_obstack, NULL);
1946 obstack_free(&pp_obstack, NULL);
1947 obstack_free(&config_obstack, NULL);
1949 strset_destroy(&stringset);
1952 exit_symbol_table();