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_kind_t last_token;
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.base.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 const n = decode(input.input, input.buf + MAX_PUTBACK, lengthof(input.buf) - MAX_PUTBACK);
203 input.bufpos = input.buf + MAX_PUTBACK;
204 input.bufend = input.bufpos + n;
206 input.c = *input.bufpos++;
207 ++input.position.colno;
211 * Put a character back into the buffer.
213 * @param pc the character to put back
215 static inline void put_back(utf32 const pc)
217 assert(input.bufpos > input.buf);
218 *(--input.bufpos - input.buf + input.buf) = (char) pc;
219 --input.position.colno;
222 #define MATCH_NEWLINE(code) \
225 if (input.c == '\n') { \
229 info.whitespace = 0; \
230 ++input.position.lineno; \
231 input.position.colno = 1; \
234 #define eat(c_type) (assert(input.c == c_type), next_char())
236 static void maybe_concat_lines(void)
254 * Set c to the next input character, ie.
255 * after expanding trigraphs.
257 static inline void next_char(void)
261 /* filter trigraphs and concatenated lines */
262 if (UNLIKELY(input.c == '\\')) {
263 maybe_concat_lines();
264 goto end_of_next_char;
267 if (LIKELY(input.c != '?'))
268 goto end_of_next_char;
271 if (LIKELY(input.c != '?')) {
274 goto end_of_next_char;
279 case '=': input.c = '#'; break;
280 case '(': input.c = '['; break;
281 case '/': input.c = '\\'; maybe_concat_lines(); break;
282 case ')': input.c = ']'; 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;
297 printf("nchar '%c'\n", input.c);
304 * Returns true if the given char is a octal digit.
306 * @param char the character to check
308 static inline bool is_octal_digit(int chr)
326 * Returns the value of a digit.
327 * The only portable way to do it ...
329 static int digit_value(int digit)
355 panic("wrong character given");
360 * Parses an octal character sequence.
362 * @param first_digit the already read first digit
364 static utf32 parse_octal_sequence(const utf32 first_digit)
366 assert(is_octal_digit(first_digit));
367 utf32 value = digit_value(first_digit);
368 if (!is_octal_digit(input.c)) return value;
369 value = 8 * value + digit_value(input.c);
371 if (!is_octal_digit(input.c)) return value;
372 value = 8 * value + digit_value(input.c);
379 * Parses a hex character sequence.
381 static utf32 parse_hex_sequence(void)
384 while (isxdigit(input.c)) {
385 value = 16 * value + digit_value(input.c);
392 * Parse an escape sequence.
394 static utf32 parse_escape_sequence(void)
398 utf32 const ec = input.c;
402 case '"': return '"';
403 case '\'': return '\'';
404 case '\\': return '\\';
405 case '?': return '\?';
406 case 'a': return '\a';
407 case 'b': return '\b';
408 case 'f': return '\f';
409 case 'n': return '\n';
410 case 'r': return '\r';
411 case 't': return '\t';
412 case 'v': return '\v';
414 return parse_hex_sequence();
423 return parse_octal_sequence(ec);
425 parse_error("reached end of file while parsing escape sequence");
427 /* \E is not documented, but handled, by GCC. It is acceptable according
428 * to §6.11.4, whereas \e is not. */
432 return 27; /* hopefully 27 is ALWAYS the code for ESCAPE */
436 parse_error("universal character parsing not implemented yet");
441 /* §6.4.4.4:8 footnote 64 */
442 parse_error("unknown escape sequence");
446 static const char *identify_string(char *string)
448 const char *result = strset_insert(&stringset, string);
449 if (result != string) {
450 obstack_free(&symbol_obstack, string);
455 static string_t make_string(char *string, size_t len)
457 const char *result = identify_string(string);
458 return (string_t) {result, len};
461 static void parse_string_literal(void)
463 const unsigned start_linenr = input.position.lineno;
471 if (resolve_escape_sequences) {
472 tc = parse_escape_sequence();
473 obstack_1grow(&symbol_obstack, (char) tc);
475 obstack_1grow(&symbol_obstack, (char) input.c);
477 obstack_1grow(&symbol_obstack, (char) input.c);
484 source_position_t source_position;
485 source_position.input_name = pp_token.base.source_position.input_name;
486 source_position.lineno = start_linenr;
487 errorf(&source_position, "string has no end");
496 obstack_grow_symbol(&symbol_obstack, input.c);
503 /* add finishing 0 to the string */
504 obstack_1grow(&symbol_obstack, '\0');
505 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
506 char *const string = obstack_finish(&symbol_obstack);
508 pp_token.kind = TP_STRING_LITERAL;
509 pp_token.string.string = make_string(string, size);
513 * Parse a wide string literal and set lexer_token.
515 static void parse_wide_string_literal(void)
517 parse_string_literal();
518 if (pp_token.kind == TP_STRING_LITERAL)
519 pp_token.kind = TP_WIDE_STRING_LITERAL;
522 static void parse_wide_character_constant(void)
529 const utf32 tc = parse_escape_sequence();
530 obstack_grow_symbol(&symbol_obstack, tc);
535 parse_error("newline while parsing character constant");
541 goto end_of_wide_char_constant;
544 parse_error("EOF while parsing character constant");
545 goto end_of_wide_char_constant;
548 obstack_grow_symbol(&symbol_obstack, input.c);
554 end_of_wide_char_constant:
555 obstack_1grow(&symbol_obstack, '\0');
556 size_t size = (size_t) obstack_object_size(&symbol_obstack)-1;
557 char *string = obstack_finish(&symbol_obstack);
558 pp_token.kind = TP_WIDE_CHARACTER_CONSTANT;
559 pp_token.string.string = make_string(string, size);
562 parse_error("empty character constant");
566 static void parse_character_constant(void)
568 const unsigned start_linenr = input.position.lineno;
576 tc = parse_escape_sequence();
577 obstack_1grow(&symbol_obstack, (char) tc);
581 parse_error("newline while parsing character constant");
586 source_position_t source_position;
587 source_position.input_name = pp_token.base.source_position.input_name;
588 source_position.lineno = start_linenr;
589 errorf(&source_position, "EOF while parsing character constant");
590 goto end_of_char_constant;
595 goto end_of_char_constant;
598 obstack_1grow(&symbol_obstack, (char) input.c);
605 end_of_char_constant:;
606 obstack_1grow(&symbol_obstack, '\0');
607 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
608 char *const string = obstack_finish(&symbol_obstack);
610 pp_token.kind = TP_CHARACTER_CONSTANT;
611 pp_token.string.string = make_string(string, size);
614 parse_error("empty character constant");
618 #define SYMBOL_CHARS_WITHOUT_E_P \
669 #define SYMBOL_CHARS \
670 SYMBOL_CHARS_WITHOUT_E_P \
689 * returns next final token from a preprocessor macro expansion
691 static void expand_next(void)
693 assert(current_expansion != NULL);
695 pp_definition_t *definition = current_expansion;
698 if (definition->list_len == 0
699 || definition->expand_pos >= definition->list_len) {
700 /* we're finished with the current macro, move up 1 level in the
702 pp_definition_t *parent = definition->parent_expansion;
703 definition->parent_expansion = NULL;
704 definition->is_expanding = false;
706 /* it was the outermost expansion, parse normal pptoken */
707 if (parent == NULL) {
708 current_expansion = NULL;
709 next_preprocessing_token();
713 current_expansion = definition;
716 pp_token = definition->token_list[definition->expand_pos];
717 pp_token.base.source_position = expansion_pos;
718 ++definition->expand_pos;
720 if (pp_token.kind != TP_IDENTIFIER)
723 /* if it was an identifier then we might need to expand again */
724 pp_definition_t *const symbol_definition = pp_token.base.symbol->pp_definition;
725 if (symbol_definition != NULL && !symbol_definition->is_expanding) {
726 symbol_definition->parent_expansion = definition;
727 symbol_definition->expand_pos = 0;
728 symbol_definition->is_expanding = true;
729 definition = symbol_definition;
730 current_expansion = definition;
735 static void skip_line_comment(void)
753 static void skip_multiline_comment(void)
755 unsigned start_linenr = input.position.lineno;
760 if (input.c == '*') {
761 /* TODO: nested comment, warn here */
766 if (input.c == '/') {
768 info.whitespace += input.position.colno-1;
774 info.at_line_begin |= !in_pp_directive;
779 source_position_t source_position;
780 source_position.input_name = pp_token.base.source_position.input_name;
781 source_position.lineno = start_linenr;
782 errorf(&source_position, "at end of file while looking for comment end");
793 static void skip_whitespace(void)
803 info.at_line_begin = true;
809 if (input.c == '/') {
813 } else if (input.c == '*') {
815 skip_multiline_comment();
828 static void eat_pp(preprocessor_token_kind_t const type)
831 assert(pp_token.kind == type);
832 next_preprocessing_token();
835 static void parse_symbol(void)
837 obstack_1grow(&symbol_obstack, (char) input.c);
844 obstack_1grow(&symbol_obstack, (char) input.c);
854 obstack_1grow(&symbol_obstack, '\0');
855 char *string = obstack_finish(&symbol_obstack);
857 /* might be a wide string or character constant ( L"string"/L'c' ) */
858 if (input.c == '"' && string[0] == 'L' && string[1] == '\0') {
859 obstack_free(&symbol_obstack, string);
860 parse_wide_string_literal();
862 } else if (input.c == '\'' && string[0] == 'L' && string[1] == '\0') {
863 obstack_free(&symbol_obstack, string);
864 parse_wide_character_constant();
868 symbol_t *symbol = symbol_table_insert(string);
870 pp_token.kind = symbol->pp_ID;
871 pp_token.base.symbol = symbol;
873 /* we can free the memory from symbol obstack if we already had an entry in
874 * the symbol table */
875 if (symbol->string != string) {
876 obstack_free(&symbol_obstack, string);
880 static void parse_number(void)
882 obstack_1grow(&symbol_obstack, (char) input.c);
889 SYMBOL_CHARS_WITHOUT_E_P
890 obstack_1grow(&symbol_obstack, (char) input.c);
898 obstack_1grow(&symbol_obstack, (char) input.c);
900 if (input.c == '+' || input.c == '-') {
901 obstack_1grow(&symbol_obstack, (char) input.c);
912 obstack_1grow(&symbol_obstack, '\0');
913 size_t size = obstack_object_size(&symbol_obstack);
914 char *string = obstack_finish(&symbol_obstack);
916 pp_token.kind = TP_NUMBER;
917 pp_token.number.number = make_string(string, size);
921 #define MAYBE_PROLOG \
926 #define MAYBE(ch, set_type) \
929 pp_token.kind = set_type; \
932 #define ELSE_CODE(code) \
939 #define ELSE(set_type) \
941 pp_token.kind = set_type; \
944 static void next_preprocessing_token(void)
946 if (current_expansion != NULL) {
951 info.at_line_begin = false;
952 info.had_whitespace = false;
954 pp_token.base.source_position = input.position;
955 pp_token.base.symbol = NULL;
961 info.had_whitespace = true;
966 info.at_line_begin = true;
967 info.had_whitespace = true;
980 parse_string_literal();
984 parse_character_constant();
1006 MAYBE('.', TP_DOTDOTDOT)
1010 pp_token.kind = '.';
1015 MAYBE('&', TP_ANDAND)
1016 MAYBE('=', TP_ANDEQUAL)
1020 MAYBE('=', TP_ASTERISKEQUAL)
1024 MAYBE('+', TP_PLUSPLUS)
1025 MAYBE('=', TP_PLUSEQUAL)
1029 MAYBE('>', TP_MINUSGREATER)
1030 MAYBE('-', TP_MINUSMINUS)
1031 MAYBE('=', TP_MINUSEQUAL)
1035 MAYBE('=', TP_EXCLAMATIONMARKEQUAL)
1039 MAYBE('=', TP_SLASHEQUAL)
1042 info.had_whitespace = true;
1043 skip_multiline_comment();
1047 info.had_whitespace = true;
1048 skip_line_comment();
1054 MAYBE('=', TP_PERCENTEQUAL)
1059 MAYBE(':', TP_HASHHASH)
1063 pp_token.kind = '#';
1071 MAYBE('=', TP_LESSEQUAL)
1074 MAYBE('=', TP_LESSLESSEQUAL)
1079 MAYBE('=', TP_GREATEREQUAL)
1082 MAYBE('=', TP_GREATERGREATEREQUAL)
1083 ELSE(TP_GREATERGREATER)
1087 MAYBE('=', TP_CARETEQUAL)
1091 MAYBE('=', TP_PIPEEQUAL)
1092 MAYBE('|', TP_PIPEPIPE)
1100 MAYBE('=', TP_EQUALEQUAL)
1104 MAYBE('#', TP_HASHHASH)
1106 pp_token.kind = '#';
1120 pp_token.kind = input.c;
1125 if (input_stack != NULL) {
1127 pop_restore_input();
1129 print_line_directive(&input.position, "2");
1132 pp_token.base.source_position.lineno++;
1133 info.at_line_begin = true;
1134 pp_token.kind = TP_EOF;
1140 if (!ignore_unknown_chars) {
1141 errorf(&pp_token.base.source_position,
1142 "unknown character '%c' found\n", input.c);
1145 pp_token.kind = input.c;
1151 static void print_quoted_string(const char *const string)
1154 for (const char *c = string; *c != 0; ++c) {
1156 case '"': fputs("\\\"", out); break;
1157 case '\\': fputs("\\\\", out); break;
1158 case '\a': fputs("\\a", out); break;
1159 case '\b': fputs("\\b", out); break;
1160 case '\f': fputs("\\f", out); break;
1161 case '\n': fputs("\\n", out); break;
1162 case '\r': fputs("\\r", out); break;
1163 case '\t': fputs("\\t", out); break;
1164 case '\v': fputs("\\v", out); break;
1165 case '\?': fputs("\\?", out); break;
1168 fprintf(out, "\\%03o", (unsigned)*c);
1178 static void print_line_directive(const source_position_t *pos, const char *add)
1180 fprintf(out, "# %u ", pos->lineno);
1181 print_quoted_string(pos->input_name);
1187 printed_input_name = pos->input_name;
1188 input.output_line = pos->lineno-1;
1191 static void emit_newlines(void)
1193 unsigned delta = pp_token.base.source_position.lineno - input.output_line;
1197 print_line_directive(&pp_token.base.source_position, NULL);
1200 for (unsigned i = 0; i < delta; ++i) {
1204 input.output_line = pp_token.base.source_position.lineno;
1207 static void emit_pp_token(void)
1212 if (info.at_line_begin) {
1215 for (unsigned i = 0; i < info.whitespace; ++i)
1218 } else if (info.had_whitespace ||
1219 tokens_would_paste(last_token, pp_token.kind)) {
1223 switch (pp_token.kind) {
1225 fputs(pp_token.base.symbol->string, out);
1228 fputs(pp_token.number.number.begin, out);
1230 case TP_WIDE_STRING_LITERAL:
1232 case TP_STRING_LITERAL:
1234 fputs(pp_token.string.string.begin, out);
1237 case TP_WIDE_CHARACTER_CONSTANT:
1239 case TP_CHARACTER_CONSTANT:
1241 fputs(pp_token.string.string.begin, out);
1245 print_pp_token_kind(out, pp_token.kind);
1248 last_token = pp_token.kind;
1251 static void eat_pp_directive(void)
1253 while (!info.at_line_begin) {
1254 next_preprocessing_token();
1258 static bool strings_equal(const string_t *string1, const string_t *string2)
1260 size_t size = string1->size;
1261 if (size != string2->size)
1264 const char *c1 = string1->begin;
1265 const char *c2 = string2->begin;
1266 for (size_t i = 0; i < size; ++i, ++c1, ++c2) {
1273 static bool pp_tokens_equal(const token_t *token1, const token_t *token2)
1275 if (token1->kind != token2->kind)
1278 switch (token1->kind) {
1280 return token1->base.symbol == token2->base.symbol;
1283 case TP_CHARACTER_CONSTANT:
1284 case TP_STRING_LITERAL:
1285 return strings_equal(&token1->string.string, &token2->string.string);
1292 static bool pp_definitions_equal(const pp_definition_t *definition1,
1293 const pp_definition_t *definition2)
1295 if (definition1->list_len != definition2->list_len)
1298 size_t len = definition1->list_len;
1299 const token_t *t1 = definition1->token_list;
1300 const token_t *t2 = definition2->token_list;
1301 for (size_t i = 0; i < len; ++i, ++t1, ++t2) {
1302 if (!pp_tokens_equal(t1, t2))
1308 static void parse_define_directive(void)
1311 assert(obstack_object_size(&pp_obstack) == 0);
1313 if (pp_token.kind != TP_IDENTIFIER || info.at_line_begin) {
1314 errorf(&pp_token.base.source_position,
1315 "expected identifier after #define, got '%t'", &pp_token);
1318 symbol_t *const symbol = pp_token.base.symbol;
1320 pp_definition_t *new_definition
1321 = obstack_alloc(&pp_obstack, sizeof(new_definition[0]));
1322 memset(new_definition, 0, sizeof(new_definition[0]));
1323 new_definition->source_position = input.position;
1325 /* this is probably the only place where spaces are significant in the
1326 * lexer (except for the fact that they separate tokens). #define b(x)
1327 * is something else than #define b (x) */
1328 if (input.c == '(') {
1330 next_preprocessing_token();
1331 /* get next token after '(' */
1332 next_preprocessing_token();
1335 switch (pp_token.kind) {
1337 new_definition->is_variadic = true;
1338 next_preprocessing_token();
1339 if (pp_token.kind != ')') {
1340 errorf(&input.position,
1341 "'...' not at end of macro argument list");
1346 obstack_ptr_grow(&pp_obstack, pp_token.base.symbol);
1347 next_preprocessing_token();
1349 if (pp_token.kind == ',') {
1350 next_preprocessing_token();
1354 if (pp_token.kind != ')') {
1355 errorf(&pp_token.base.source_position,
1356 "expected ',' or ')' after identifier, got '%t'",
1362 next_preprocessing_token();
1363 goto finish_argument_list;
1365 errorf(&pp_token.base.source_position,
1366 "expected identifier, '...' or ')' in #define argument list, got '%t'",
1372 finish_argument_list:
1373 new_definition->has_parameters = true;
1374 new_definition->n_parameters
1375 = obstack_object_size(&pp_obstack) / sizeof(new_definition->parameters[0]);
1376 new_definition->parameters = obstack_finish(&pp_obstack);
1378 next_preprocessing_token();
1381 /* construct a new pp_definition on the obstack */
1382 assert(obstack_object_size(&pp_obstack) == 0);
1383 size_t list_len = 0;
1384 while (!info.at_line_begin) {
1385 obstack_grow(&pp_obstack, &pp_token, sizeof(pp_token));
1387 next_preprocessing_token();
1390 new_definition->list_len = list_len;
1391 new_definition->token_list = obstack_finish(&pp_obstack);
1393 pp_definition_t *old_definition = symbol->pp_definition;
1394 if (old_definition != NULL) {
1395 if (!pp_definitions_equal(old_definition, new_definition)) {
1396 warningf(WARN_OTHER, &input.position, "multiple definition of macro '%Y' (first defined %P)", symbol, &old_definition->source_position);
1398 /* reuse the old definition */
1399 obstack_free(&pp_obstack, new_definition);
1400 new_definition = old_definition;
1404 symbol->pp_definition = new_definition;
1408 if (obstack_object_size(&pp_obstack) > 0) {
1409 char *ptr = obstack_finish(&pp_obstack);
1410 obstack_free(&pp_obstack, ptr);
1415 static void parse_undef_directive(void)
1419 if (pp_token.kind != TP_IDENTIFIER) {
1420 errorf(&input.position,
1421 "expected identifier after #undef, got '%t'", &pp_token);
1426 pp_token.base.symbol->pp_definition = NULL;
1427 next_preprocessing_token();
1429 if (!info.at_line_begin) {
1430 warningf(WARN_OTHER, &input.position, "extra tokens at end of #undef directive");
1435 static const char *parse_headername(void)
1437 /* behind an #include we can have the special headername lexems.
1438 * They're only allowed behind an #include so they're not recognized
1439 * by the normal next_preprocessing_token. We handle them as a special
1441 if (info.at_line_begin) {
1442 parse_error("expected headername after #include");
1446 assert(obstack_object_size(&symbol_obstack) == 0);
1448 /* check wether we have a "... or <... headername */
1457 parse_error("header name without closing '>'");
1462 goto finished_headername;
1464 obstack_1grow(&symbol_obstack, (char) input.c);
1467 /* we should never be here */
1476 parse_error("header name without closing '>'");
1481 goto finished_headername;
1483 obstack_1grow(&symbol_obstack, (char) input.c);
1486 /* we should never be here */
1489 /* TODO: do normal pp_token parsing and concatenate results */
1490 panic("pp_token concat include not implemented yet");
1493 finished_headername:
1494 obstack_1grow(&symbol_obstack, '\0');
1495 char *headername = obstack_finish(&symbol_obstack);
1497 /* TODO: iterate search-path to find the file */
1501 return identify_string(headername);
1504 static bool do_include(bool system_include, const char *headername)
1506 size_t headername_len = strlen(headername);
1507 if (!system_include) {
1508 /* put dirname of current input on obstack */
1509 const char *filename = input.position.input_name;
1510 const char *last_slash = strrchr(filename, '/');
1511 if (last_slash != NULL) {
1512 size_t len = last_slash - filename;
1513 obstack_grow(&symbol_obstack, filename, len + 1);
1514 obstack_grow0(&symbol_obstack, headername, headername_len);
1515 char *complete_path = obstack_finish(&symbol_obstack);
1516 headername = identify_string(complete_path);
1519 FILE *file = fopen(headername, "r");
1521 switch_input(file, headername);
1526 assert(obstack_object_size(&symbol_obstack) == 0);
1527 /* check searchpath */
1528 for (searchpath_entry_t *entry = searchpath; entry != NULL;
1529 entry = entry->next) {
1530 const char *path = entry->path;
1531 size_t len = strlen(path);
1532 obstack_grow(&symbol_obstack, path, len);
1533 if (path[len-1] != '/')
1534 obstack_1grow(&symbol_obstack, '/');
1535 obstack_grow(&symbol_obstack, headername, headername_len+1);
1537 char *complete_path = obstack_finish(&symbol_obstack);
1538 FILE *file = fopen(complete_path, "r");
1540 const char *filename = identify_string(complete_path);
1541 switch_input(file, filename);
1544 obstack_free(&symbol_obstack, complete_path);
1551 static bool parse_include_directive(void)
1553 /* don't eat the TP_include here!
1554 * we need an alternative parsing for the next token */
1556 bool system_include = input.c == '<';
1557 const char *headername = parse_headername();
1558 if (headername == NULL) {
1563 if (!info.at_line_begin) {
1564 warningf(WARN_OTHER, &pp_token.base.source_position,
1565 "extra tokens at end of #include directive");
1569 if (n_inputs > INCLUDE_LIMIT) {
1570 errorf(&pp_token.base.source_position, "#include nested too deeply");
1572 next_preprocessing_token();
1576 /* we have to reenable space counting and macro expansion here,
1577 * because it is still disabled in directive parsing,
1578 * but we will trigger a preprocessing token reading of the new file
1579 * now and need expansions/space counting */
1580 in_pp_directive = false;
1585 bool res = do_include(system_include, headername);
1587 errorf(&pp_token.base.source_position,
1588 "failed including '%s': %s", headername, strerror(errno));
1589 pop_restore_input();
1596 static pp_conditional_t *push_conditional(void)
1598 pp_conditional_t *conditional
1599 = obstack_alloc(&pp_obstack, sizeof(*conditional));
1600 memset(conditional, 0, sizeof(*conditional));
1602 conditional->parent = conditional_stack;
1603 conditional_stack = conditional;
1608 static void pop_conditional(void)
1610 assert(conditional_stack != NULL);
1611 conditional_stack = conditional_stack->parent;
1614 static void check_unclosed_conditionals(void)
1616 while (conditional_stack != NULL) {
1617 pp_conditional_t *conditional = conditional_stack;
1619 if (conditional->in_else) {
1620 errorf(&conditional->source_position, "unterminated #else");
1622 errorf(&conditional->source_position, "unterminated condition");
1628 static void parse_ifdef_ifndef_directive(void)
1630 bool is_ifndef = (pp_token.kind == TP_ifndef);
1632 next_preprocessing_token();
1636 pp_conditional_t *conditional = push_conditional();
1637 conditional->source_position = pp_token.base.source_position;
1638 conditional->skip = true;
1642 if (pp_token.kind != TP_IDENTIFIER || info.at_line_begin) {
1643 errorf(&pp_token.base.source_position,
1644 "expected identifier after #%s, got '%t'",
1645 is_ifndef ? "ifndef" : "ifdef", &pp_token);
1648 /* just take the true case in the hope to avoid further errors */
1651 /* evaluate wether we are in true or false case */
1652 condition = !pp_token.base.symbol->pp_definition == is_ifndef;
1654 next_preprocessing_token();
1656 if (!info.at_line_begin) {
1657 errorf(&pp_token.base.source_position,
1658 "extra tokens at end of #%s",
1659 is_ifndef ? "ifndef" : "ifdef");
1664 pp_conditional_t *conditional = push_conditional();
1665 conditional->source_position = pp_token.base.source_position;
1666 conditional->condition = condition;
1673 static void parse_else_directive(void)
1677 if (!info.at_line_begin) {
1679 warningf(WARN_OTHER, &pp_token.base.source_position, "extra tokens at end of #else");
1684 pp_conditional_t *conditional = conditional_stack;
1685 if (conditional == NULL) {
1686 errorf(&pp_token.base.source_position, "#else without prior #if");
1690 if (conditional->in_else) {
1691 errorf(&pp_token.base.source_position,
1692 "#else after #else (condition started %P)",
1693 conditional->source_position);
1698 conditional->in_else = true;
1699 if (!conditional->skip) {
1700 skip_mode = conditional->condition;
1702 conditional->source_position = pp_token.base.source_position;
1705 static void parse_endif_directive(void)
1709 if (!info.at_line_begin) {
1711 warningf(WARN_OTHER, &pp_token.base.source_position, "extra tokens at end of #endif");
1716 pp_conditional_t *conditional = conditional_stack;
1717 if (conditional == NULL) {
1718 errorf(&pp_token.base.source_position, "#endif without prior #if");
1722 if (!conditional->skip) {
1728 static void parse_preprocessing_directive(void)
1730 in_pp_directive = true;
1734 switch (pp_token.kind) {
1737 parse_ifdef_ifndef_directive();
1740 parse_else_directive();
1743 parse_endif_directive();
1750 switch (pp_token.kind) {
1752 parse_define_directive();
1755 parse_undef_directive();
1759 parse_ifdef_ifndef_directive();
1762 parse_else_directive();
1765 parse_endif_directive();
1768 parse_include_directive();
1771 if (info.at_line_begin) {
1772 /* the nop directive "#" */
1775 errorf(&pp_token.base.source_position,
1776 "invalid preprocessing directive #%t", &pp_token);
1782 in_pp_directive = false;
1783 assert(info.at_line_begin);
1786 static void prepend_include_path(const char *path)
1788 searchpath_entry_t *entry = OALLOCZ(&config_obstack, searchpath_entry_t);
1790 entry->next = searchpath;
1794 static void setup_include_path(void)
1796 /* built-in paths */
1797 prepend_include_path("/usr/include");
1799 /* parse environment variable */
1800 const char *cpath = getenv("CPATH");
1801 if (cpath != NULL && *cpath != '\0') {
1802 const char *begin = cpath;
1806 while (*c != '\0' && *c != ':')
1809 size_t len = c-begin;
1811 /* for gcc compatibility (Matze: I would expect that
1812 * nothing happens for an empty entry...) */
1813 prepend_include_path(".");
1815 char *string = obstack_alloc(&config_obstack, len+1);
1816 memcpy(string, begin, len);
1819 prepend_include_path(string);
1826 } while(*c != '\0');
1830 int pptest_main(int argc, char **argv);
1831 int pptest_main(int argc, char **argv)
1833 init_symbol_table();
1836 obstack_init(&config_obstack);
1837 obstack_init(&pp_obstack);
1838 obstack_init(&input_obstack);
1839 strset_init(&stringset);
1841 setup_include_path();
1843 /* simplistic commandline parser */
1844 const char *filename = NULL;
1845 const char *output = NULL;
1846 for (int i = 1; i < argc; ++i) {
1847 const char *opt = argv[i];
1848 if (streq(opt, "-I")) {
1849 prepend_include_path(argv[++i]);
1851 } else if (streq(opt, "-E")) {
1853 } else if (streq(opt, "-o")) {
1856 } else if (opt[0] == '-') {
1857 fprintf(stderr, "Unknown option '%s'\n", opt);
1859 if (filename != NULL)
1860 fprintf(stderr, "Multiple inputs not supported\n");
1864 if (filename == NULL) {
1865 fprintf(stderr, "No input specified\n");
1869 if (output == NULL) {
1872 out = fopen(output, "w");
1874 fprintf(stderr, "Couldn't open output '%s'\n", output);
1879 /* just here for gcc compatibility */
1880 fprintf(out, "# 1 \"%s\"\n", filename);
1881 fprintf(out, "# 1 \"<built-in>\"\n");
1882 fprintf(out, "# 1 \"<command-line>\"\n");
1884 FILE *file = fopen(filename, "r");
1886 fprintf(stderr, "Couldn't open input '%s'\n", filename);
1889 switch_input(file, filename);
1892 if (pp_token.kind == '#' && info.at_line_begin) {
1893 parse_preprocessing_directive();
1895 } else if (pp_token.kind == TP_EOF) {
1896 goto end_of_main_loop;
1897 } else if (pp_token.kind == TP_IDENTIFIER && !in_pp_directive) {
1898 symbol_t *const symbol = pp_token.base.symbol;
1899 pp_definition_t *const pp_definition = symbol->pp_definition;
1900 if (pp_definition != NULL && !pp_definition->is_expanding) {
1901 expansion_pos = pp_token.base.source_position;
1902 if (pp_definition->has_parameters) {
1903 source_position_t position = pp_token.base.source_position;
1904 add_token_info_t old_info = info;
1905 next_preprocessing_token();
1906 add_token_info_t new_info = info;
1908 /* no opening brace -> no expansion */
1909 if (pp_token.kind == '(') {
1912 /* parse arguments (TODO) */
1913 while (pp_token.kind != TP_EOF && pp_token.kind != ')')
1914 next_preprocessing_token();
1916 token_t next_token = pp_token;
1917 /* restore identifier token */
1918 pp_token.kind = TP_IDENTIFIER;
1919 pp_token.base.symbol = symbol;
1920 pp_token.base.source_position = position;
1925 pp_token = next_token;
1930 pp_definition->expand_pos = 0;
1931 pp_definition->is_expanding = true;
1932 current_expansion = pp_definition;
1939 next_preprocessing_token();
1944 check_unclosed_conditionals();
1949 obstack_free(&input_obstack, NULL);
1950 obstack_free(&pp_obstack, NULL);
1951 obstack_free(&config_obstack, NULL);
1953 strset_destroy(&stringset);
1956 exit_symbol_table();