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 skip_mode;
96 static struct obstack pp_obstack;
97 static struct obstack config_obstack;
98 static const char *printed_input_name = NULL;
99 static source_position_t expansion_pos;
100 static pp_definition_t *current_expansion = NULL;
101 static strset_t stringset;
102 static preprocessor_token_kind_t last_token;
104 static searchpath_entry_t *searchpath;
106 static add_token_info_t info;
108 static inline void next_char(void);
109 static void next_preprocessing_token(void);
110 static void print_line_directive(const source_position_t *pos, const char *add);
112 static void switch_input(FILE *file, const char *filename)
115 input.input = input_from_stream(file, NULL);
118 input.output_line = 0;
119 input.position.input_name = filename;
120 input.position.lineno = 1;
122 /* indicate that we're at a new input */
123 print_line_directive(&input.position, input_stack != NULL ? "1" : NULL);
125 /* place a virtual '\n' so we realize we're at line begin */
126 input.position.lineno = 0;
128 next_preprocessing_token();
131 static void close_input(void)
133 input_free(input.input);
134 assert(input.file != NULL);
144 static void push_input(void)
146 pp_input_t *saved_input
147 = obstack_alloc(&input_obstack, sizeof(*saved_input));
149 memcpy(saved_input, &input, sizeof(*saved_input));
151 /* adjust buffer positions */
152 if (input.bufpos != NULL)
153 saved_input->bufpos = saved_input->buf + (input.bufpos - input.buf);
154 if (input.bufend != NULL)
155 saved_input->bufend = saved_input->buf + (input.bufend - input.buf);
157 saved_input->parent = input_stack;
158 input_stack = saved_input;
162 static void pop_restore_input(void)
164 assert(n_inputs > 0);
165 assert(input_stack != NULL);
167 pp_input_t *saved_input = input_stack;
169 memcpy(&input, saved_input, sizeof(input));
172 /* adjust buffer positions */
173 if (saved_input->bufpos != NULL)
174 input.bufpos = input.buf + (saved_input->bufpos - saved_input->buf);
175 if (saved_input->bufend != NULL)
176 input.bufend = input.buf + (saved_input->bufend - saved_input->buf);
178 input_stack = saved_input->parent;
179 obstack_free(&input_obstack, saved_input);
184 * Prints a parse error message at the current token.
186 * @param msg the error message
188 static void parse_error(const char *msg)
190 errorf(&pp_token.base.source_position, "%s", msg);
193 static inline void next_real_char(void)
195 assert(input.bufpos <= input.bufend);
196 if (input.bufpos >= input.bufend) {
197 size_t const n = decode(input.input, input.buf + MAX_PUTBACK, lengthof(input.buf) - MAX_PUTBACK);
202 input.bufpos = input.buf + MAX_PUTBACK;
203 input.bufend = input.bufpos + n;
205 input.c = *input.bufpos++;
206 ++input.position.colno;
210 * Put a character back into the buffer.
212 * @param pc the character to put back
214 static inline void put_back(utf32 const pc)
216 assert(input.bufpos > input.buf);
217 *(--input.bufpos - input.buf + input.buf) = (char) pc;
218 --input.position.colno;
221 #define MATCH_NEWLINE(code) \
224 if (input.c == '\n') { \
228 info.whitespace = 0; \
229 ++input.position.lineno; \
230 input.position.colno = 1; \
233 #define eat(c_type) (assert(input.c == c_type), next_char())
235 static void maybe_concat_lines(void)
253 * Set c to the next input character, ie.
254 * after expanding trigraphs.
256 static inline void next_char(void)
260 /* filter trigraphs and concatenated lines */
261 if (UNLIKELY(input.c == '\\')) {
262 maybe_concat_lines();
263 goto end_of_next_char;
266 if (LIKELY(input.c != '?'))
267 goto end_of_next_char;
270 if (LIKELY(input.c != '?')) {
273 goto end_of_next_char;
278 case '=': input.c = '#'; break;
279 case '(': input.c = '['; break;
280 case '/': input.c = '\\'; maybe_concat_lines(); break;
281 case ')': input.c = ']'; 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;
296 printf("nchar '%c'\n", input.c);
303 * Returns true if the given char is a octal digit.
305 * @param char the character to check
307 static inline bool is_octal_digit(int chr)
325 * Returns the value of a digit.
326 * The only portable way to do it ...
328 static int digit_value(int digit)
354 panic("wrong character given");
359 * Parses an octal character sequence.
361 * @param first_digit the already read first digit
363 static utf32 parse_octal_sequence(const utf32 first_digit)
365 assert(is_octal_digit(first_digit));
366 utf32 value = digit_value(first_digit);
367 if (!is_octal_digit(input.c)) return value;
368 value = 8 * value + digit_value(input.c);
370 if (!is_octal_digit(input.c)) return value;
371 value = 8 * value + digit_value(input.c);
378 * Parses a hex character sequence.
380 static utf32 parse_hex_sequence(void)
383 while (isxdigit(input.c)) {
384 value = 16 * value + digit_value(input.c);
391 * Parse an escape sequence.
393 static utf32 parse_escape_sequence(void)
397 utf32 const ec = input.c;
401 case '"': return '"';
402 case '\'': return '\'';
403 case '\\': return '\\';
404 case '?': return '\?';
405 case 'a': return '\a';
406 case 'b': return '\b';
407 case 'f': return '\f';
408 case 'n': return '\n';
409 case 'r': return '\r';
410 case 't': return '\t';
411 case 'v': return '\v';
413 return parse_hex_sequence();
422 return parse_octal_sequence(ec);
424 parse_error("reached end of file while parsing escape sequence");
426 /* \E is not documented, but handled, by GCC. It is acceptable according
427 * to §6.11.4, whereas \e is not. */
431 return 27; /* hopefully 27 is ALWAYS the code for ESCAPE */
435 parse_error("universal character parsing not implemented yet");
440 /* §6.4.4.4:8 footnote 64 */
441 parse_error("unknown escape sequence");
445 static const char *identify_string(char *string)
447 const char *result = strset_insert(&stringset, string);
448 if (result != string) {
449 obstack_free(&symbol_obstack, string);
454 static string_t make_string(char *string, size_t len)
456 const char *result = identify_string(string);
457 return (string_t) {result, len};
460 static void parse_string_literal(void)
462 const unsigned start_linenr = input.position.lineno;
470 if (resolve_escape_sequences) {
471 tc = parse_escape_sequence();
472 obstack_1grow(&symbol_obstack, (char) tc);
474 obstack_1grow(&symbol_obstack, (char) input.c);
476 obstack_1grow(&symbol_obstack, (char) input.c);
483 source_position_t source_position;
484 source_position.input_name = pp_token.base.source_position.input_name;
485 source_position.lineno = start_linenr;
486 errorf(&source_position, "string has no end");
495 obstack_grow_symbol(&symbol_obstack, input.c);
502 /* add finishing 0 to the string */
503 obstack_1grow(&symbol_obstack, '\0');
504 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
505 char *const string = obstack_finish(&symbol_obstack);
507 pp_token.kind = TP_STRING_LITERAL;
508 pp_token.string.string = make_string(string, size);
512 * Parse a wide string literal and set lexer_token.
514 static void parse_wide_string_literal(void)
516 parse_string_literal();
517 if (pp_token.kind == TP_STRING_LITERAL)
518 pp_token.kind = TP_WIDE_STRING_LITERAL;
521 static void parse_wide_character_constant(void)
528 const utf32 tc = parse_escape_sequence();
529 obstack_grow_symbol(&symbol_obstack, tc);
534 parse_error("newline while parsing character constant");
540 goto end_of_wide_char_constant;
543 parse_error("EOF while parsing character constant");
544 goto end_of_wide_char_constant;
547 obstack_grow_symbol(&symbol_obstack, input.c);
553 end_of_wide_char_constant:
554 obstack_1grow(&symbol_obstack, '\0');
555 size_t size = (size_t) obstack_object_size(&symbol_obstack)-1;
556 char *string = obstack_finish(&symbol_obstack);
557 pp_token.kind = TP_WIDE_CHARACTER_CONSTANT;
558 pp_token.string.string = make_string(string, size);
561 parse_error("empty character constant");
565 static void parse_character_constant(void)
567 const unsigned start_linenr = input.position.lineno;
575 tc = parse_escape_sequence();
576 obstack_1grow(&symbol_obstack, (char) tc);
580 parse_error("newline while parsing character constant");
585 source_position_t source_position;
586 source_position.input_name = pp_token.base.source_position.input_name;
587 source_position.lineno = start_linenr;
588 errorf(&source_position, "EOF while parsing character constant");
589 goto end_of_char_constant;
594 goto end_of_char_constant;
597 obstack_1grow(&symbol_obstack, (char) input.c);
604 end_of_char_constant:;
605 obstack_1grow(&symbol_obstack, '\0');
606 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
607 char *const string = obstack_finish(&symbol_obstack);
609 pp_token.kind = TP_CHARACTER_CONSTANT;
610 pp_token.string.string = make_string(string, size);
613 parse_error("empty character constant");
617 #define SYMBOL_CHARS_WITHOUT_E_P \
668 #define SYMBOL_CHARS \
669 SYMBOL_CHARS_WITHOUT_E_P \
688 * returns next final token from a preprocessor macro expansion
690 static void expand_next(void)
692 assert(current_expansion != NULL);
694 pp_definition_t *definition = current_expansion;
697 if (definition->list_len == 0
698 || definition->expand_pos >= definition->list_len) {
699 /* we're finished with the current macro, move up 1 level in the
701 pp_definition_t *parent = definition->parent_expansion;
702 definition->parent_expansion = NULL;
703 definition->is_expanding = false;
705 /* it was the outermost expansion, parse normal pptoken */
706 if (parent == NULL) {
707 current_expansion = NULL;
708 next_preprocessing_token();
712 current_expansion = definition;
715 pp_token = definition->token_list[definition->expand_pos];
716 pp_token.base.source_position = expansion_pos;
717 ++definition->expand_pos;
719 if (pp_token.kind != TP_IDENTIFIER)
722 /* if it was an identifier then we might need to expand again */
723 pp_definition_t *const symbol_definition = pp_token.base.symbol->pp_definition;
724 if (symbol_definition != NULL && !symbol_definition->is_expanding) {
725 symbol_definition->parent_expansion = definition;
726 symbol_definition->expand_pos = 0;
727 symbol_definition->is_expanding = true;
728 definition = symbol_definition;
729 current_expansion = definition;
734 static void skip_line_comment(void)
752 static void skip_multiline_comment(void)
754 unsigned start_linenr = input.position.lineno;
759 if (input.c == '*') {
760 /* TODO: nested comment, warn here */
765 if (input.c == '/') {
766 if (input.position.lineno != input.output_line)
767 info.whitespace = input.position.colno;
778 source_position_t source_position;
779 source_position.input_name = pp_token.base.source_position.input_name;
780 source_position.lineno = start_linenr;
781 errorf(&source_position, "at end of file while looking for comment end");
792 static void skip_whitespace(void)
802 info.at_line_begin = true;
808 if (input.c == '/') {
812 } else if (input.c == '*') {
814 skip_multiline_comment();
827 static void eat_pp(preprocessor_token_kind_t const type)
830 assert(pp_token.kind == type);
831 next_preprocessing_token();
834 static void parse_symbol(void)
836 obstack_1grow(&symbol_obstack, (char) input.c);
843 obstack_1grow(&symbol_obstack, (char) input.c);
853 obstack_1grow(&symbol_obstack, '\0');
854 char *string = obstack_finish(&symbol_obstack);
856 /* might be a wide string or character constant ( L"string"/L'c' ) */
857 if (input.c == '"' && string[0] == 'L' && string[1] == '\0') {
858 obstack_free(&symbol_obstack, string);
859 parse_wide_string_literal();
861 } else if (input.c == '\'' && string[0] == 'L' && string[1] == '\0') {
862 obstack_free(&symbol_obstack, string);
863 parse_wide_character_constant();
867 symbol_t *symbol = symbol_table_insert(string);
869 pp_token.kind = symbol->pp_ID;
870 pp_token.base.symbol = symbol;
872 /* we can free the memory from symbol obstack if we already had an entry in
873 * the symbol table */
874 if (symbol->string != string) {
875 obstack_free(&symbol_obstack, string);
879 static void parse_number(void)
881 obstack_1grow(&symbol_obstack, (char) input.c);
888 SYMBOL_CHARS_WITHOUT_E_P
889 obstack_1grow(&symbol_obstack, (char) input.c);
897 obstack_1grow(&symbol_obstack, (char) input.c);
899 if (input.c == '+' || input.c == '-') {
900 obstack_1grow(&symbol_obstack, (char) input.c);
911 obstack_1grow(&symbol_obstack, '\0');
912 size_t size = obstack_object_size(&symbol_obstack);
913 char *string = obstack_finish(&symbol_obstack);
915 pp_token.kind = TP_NUMBER;
916 pp_token.number.number = make_string(string, size);
920 #define MAYBE_PROLOG \
925 #define MAYBE(ch, set_type) \
928 pp_token.kind = set_type; \
931 #define ELSE_CODE(code) \
938 #define ELSE(set_type) \
940 pp_token.kind = set_type; \
943 static void next_preprocessing_token(void)
945 if (current_expansion != NULL) {
950 info.at_line_begin = false;
951 info.had_whitespace = false;
953 pp_token.base.source_position = input.position;
954 pp_token.base.symbol = NULL;
960 info.had_whitespace = true;
965 info.at_line_begin = true;
966 info.had_whitespace = true;
979 parse_string_literal();
983 parse_character_constant();
1005 MAYBE('.', TP_DOTDOTDOT)
1009 pp_token.kind = '.';
1014 MAYBE('&', TP_ANDAND)
1015 MAYBE('=', TP_ANDEQUAL)
1019 MAYBE('=', TP_ASTERISKEQUAL)
1023 MAYBE('+', TP_PLUSPLUS)
1024 MAYBE('=', TP_PLUSEQUAL)
1028 MAYBE('>', TP_MINUSGREATER)
1029 MAYBE('-', TP_MINUSMINUS)
1030 MAYBE('=', TP_MINUSEQUAL)
1034 MAYBE('=', TP_EXCLAMATIONMARKEQUAL)
1038 MAYBE('=', TP_SLASHEQUAL)
1041 info.had_whitespace = true;
1042 skip_multiline_comment();
1046 info.had_whitespace = true;
1047 skip_line_comment();
1053 MAYBE('=', TP_PERCENTEQUAL)
1058 MAYBE(':', TP_HASHHASH)
1062 pp_token.kind = '#';
1070 MAYBE('=', TP_LESSEQUAL)
1073 MAYBE('=', TP_LESSLESSEQUAL)
1078 MAYBE('=', TP_GREATEREQUAL)
1081 MAYBE('=', TP_GREATERGREATEREQUAL)
1082 ELSE(TP_GREATERGREATER)
1086 MAYBE('=', TP_CARETEQUAL)
1090 MAYBE('=', TP_PIPEEQUAL)
1091 MAYBE('|', TP_PIPEPIPE)
1099 MAYBE('=', TP_EQUALEQUAL)
1103 MAYBE('#', TP_HASHHASH)
1105 pp_token.kind = '#';
1119 pp_token.kind = input.c;
1124 if (input_stack != NULL) {
1126 pop_restore_input();
1128 print_line_directive(&input.position, "2");
1131 pp_token.base.source_position.lineno++;
1132 info.at_line_begin = true;
1133 pp_token.kind = TP_EOF;
1139 if (!ignore_unknown_chars) {
1140 errorf(&pp_token.base.source_position,
1141 "unknown character '%c' found\n", input.c);
1144 pp_token.kind = input.c;
1150 static void print_quoted_string(const char *const string)
1153 for (const char *c = string; *c != 0; ++c) {
1155 case '"': fputs("\\\"", out); break;
1156 case '\\': fputs("\\\\", out); break;
1157 case '\a': fputs("\\a", out); break;
1158 case '\b': fputs("\\b", out); break;
1159 case '\f': fputs("\\f", out); break;
1160 case '\n': fputs("\\n", out); break;
1161 case '\r': fputs("\\r", out); break;
1162 case '\t': fputs("\\t", out); break;
1163 case '\v': fputs("\\v", out); break;
1164 case '\?': fputs("\\?", out); break;
1167 fprintf(out, "\\%03o", (unsigned)*c);
1177 static void print_line_directive(const source_position_t *pos, const char *add)
1179 fprintf(out, "# %u ", pos->lineno);
1180 print_quoted_string(pos->input_name);
1186 printed_input_name = pos->input_name;
1187 input.output_line = pos->lineno-1;
1190 static bool emit_newlines(void)
1192 unsigned delta = pp_token.base.source_position.lineno - input.output_line;
1198 print_line_directive(&pp_token.base.source_position, NULL);
1201 for (unsigned i = 0; i < delta; ++i) {
1205 input.output_line = pp_token.base.source_position.lineno;
1207 for (unsigned i = 0; i < info.whitespace; ++i)
1213 static void emit_pp_token(void)
1218 if (!emit_newlines() &&
1219 (info.had_whitespace || tokens_would_paste(last_token, pp_token.kind)))
1222 switch (pp_token.kind) {
1224 fputs(pp_token.base.symbol->string, out);
1227 fputs(pp_token.number.number.begin, out);
1229 case TP_WIDE_STRING_LITERAL:
1231 case TP_STRING_LITERAL:
1233 fputs(pp_token.string.string.begin, out);
1236 case TP_WIDE_CHARACTER_CONSTANT:
1238 case TP_CHARACTER_CONSTANT:
1240 fputs(pp_token.string.string.begin, out);
1244 print_pp_token_kind(out, pp_token.kind);
1247 last_token = pp_token.kind;
1250 static void eat_pp_directive(void)
1252 while (!info.at_line_begin) {
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->kind != token2->kind)
1277 switch (token1->kind) {
1279 return token1->base.symbol == token2->base.symbol;
1282 case TP_CHARACTER_CONSTANT:
1283 case TP_STRING_LITERAL:
1284 return strings_equal(&token1->string.string, &token2->string.string);
1291 static bool pp_definitions_equal(const pp_definition_t *definition1,
1292 const pp_definition_t *definition2)
1294 if (definition1->list_len != definition2->list_len)
1297 size_t len = definition1->list_len;
1298 const token_t *t1 = definition1->token_list;
1299 const token_t *t2 = definition2->token_list;
1300 for (size_t i = 0; i < len; ++i, ++t1, ++t2) {
1301 if (!pp_tokens_equal(t1, t2))
1307 static void parse_define_directive(void)
1310 assert(obstack_object_size(&pp_obstack) == 0);
1312 if (pp_token.kind != TP_IDENTIFIER || info.at_line_begin) {
1313 errorf(&pp_token.base.source_position,
1314 "expected identifier after #define, got '%t'", &pp_token);
1317 symbol_t *const symbol = pp_token.base.symbol;
1319 pp_definition_t *new_definition
1320 = obstack_alloc(&pp_obstack, sizeof(new_definition[0]));
1321 memset(new_definition, 0, sizeof(new_definition[0]));
1322 new_definition->source_position = input.position;
1324 /* this is probably the only place where spaces are significant in the
1325 * lexer (except for the fact that they separate tokens). #define b(x)
1326 * is something else than #define b (x) */
1327 if (input.c == '(') {
1329 next_preprocessing_token();
1330 /* get next token after '(' */
1331 next_preprocessing_token();
1334 switch (pp_token.kind) {
1336 new_definition->is_variadic = true;
1337 next_preprocessing_token();
1338 if (pp_token.kind != ')') {
1339 errorf(&input.position,
1340 "'...' not at end of macro argument list");
1345 obstack_ptr_grow(&pp_obstack, pp_token.base.symbol);
1346 next_preprocessing_token();
1348 if (pp_token.kind == ',') {
1349 next_preprocessing_token();
1353 if (pp_token.kind != ')') {
1354 errorf(&pp_token.base.source_position,
1355 "expected ',' or ')' after identifier, got '%t'",
1361 next_preprocessing_token();
1362 goto finish_argument_list;
1364 errorf(&pp_token.base.source_position,
1365 "expected identifier, '...' or ')' in #define argument list, got '%t'",
1371 finish_argument_list:
1372 new_definition->has_parameters = true;
1373 new_definition->n_parameters
1374 = obstack_object_size(&pp_obstack) / sizeof(new_definition->parameters[0]);
1375 new_definition->parameters = obstack_finish(&pp_obstack);
1377 next_preprocessing_token();
1380 /* construct a new pp_definition on the obstack */
1381 assert(obstack_object_size(&pp_obstack) == 0);
1382 size_t list_len = 0;
1383 while (!info.at_line_begin) {
1384 obstack_grow(&pp_obstack, &pp_token, sizeof(pp_token));
1386 next_preprocessing_token();
1389 new_definition->list_len = list_len;
1390 new_definition->token_list = obstack_finish(&pp_obstack);
1392 pp_definition_t *old_definition = symbol->pp_definition;
1393 if (old_definition != NULL) {
1394 if (!pp_definitions_equal(old_definition, new_definition)) {
1395 warningf(WARN_OTHER, &input.position, "multiple definition of macro '%Y' (first defined %P)", symbol, &old_definition->source_position);
1397 /* reuse the old definition */
1398 obstack_free(&pp_obstack, new_definition);
1399 new_definition = old_definition;
1403 symbol->pp_definition = new_definition;
1407 if (obstack_object_size(&pp_obstack) > 0) {
1408 char *ptr = obstack_finish(&pp_obstack);
1409 obstack_free(&pp_obstack, ptr);
1414 static void parse_undef_directive(void)
1418 if (pp_token.kind != TP_IDENTIFIER) {
1419 errorf(&input.position,
1420 "expected identifier after #undef, got '%t'", &pp_token);
1425 pp_token.base.symbol->pp_definition = NULL;
1426 next_preprocessing_token();
1428 if (!info.at_line_begin) {
1429 warningf(WARN_OTHER, &input.position, "extra tokens at end of #undef directive");
1434 static void parse_headername(void)
1436 const source_position_t start_position = input.position;
1437 string_t string = {NULL, 0};
1438 assert(obstack_object_size(&symbol_obstack) == 0);
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
1444 if (info.at_line_begin) {
1445 parse_error("expected headername after #include");
1449 /* check wether we have a "... or <... headername */
1458 parse_error("header name without closing '>'");
1463 goto finished_headername;
1465 obstack_1grow(&symbol_obstack, (char) input.c);
1468 /* we should never be here */
1477 parse_error("header name without closing '>'");
1482 goto finished_headername;
1484 obstack_1grow(&symbol_obstack, (char) input.c);
1487 /* we should never be here */
1490 /* TODO: do normal pp_token parsing and concatenate results */
1491 panic("pp_token concat include not implemented yet");
1494 finished_headername:
1495 obstack_1grow(&symbol_obstack, '\0');
1496 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
1497 char *const headername = obstack_finish(&symbol_obstack);
1498 string = make_string(headername, size);
1501 pp_token.base.source_position = start_position;
1502 pp_token.kind = TP_HEADERNAME;
1503 pp_token.string.string = string;
1506 static bool do_include(bool system_include, const char *headername)
1508 size_t headername_len = strlen(headername);
1509 if (!system_include) {
1510 /* put dirname of current input on obstack */
1511 const char *filename = input.position.input_name;
1512 const char *last_slash = strrchr(filename, '/');
1513 if (last_slash != NULL) {
1514 size_t len = last_slash - filename;
1515 obstack_grow(&symbol_obstack, filename, len + 1);
1516 obstack_grow0(&symbol_obstack, headername, headername_len);
1517 char *complete_path = obstack_finish(&symbol_obstack);
1518 headername = identify_string(complete_path);
1521 FILE *file = fopen(headername, "r");
1523 switch_input(file, headername);
1528 assert(obstack_object_size(&symbol_obstack) == 0);
1529 /* check searchpath */
1530 for (searchpath_entry_t *entry = searchpath; entry != NULL;
1531 entry = entry->next) {
1532 const char *path = entry->path;
1533 size_t len = strlen(path);
1534 obstack_grow(&symbol_obstack, path, len);
1535 if (path[len-1] != '/')
1536 obstack_1grow(&symbol_obstack, '/');
1537 obstack_grow(&symbol_obstack, headername, headername_len+1);
1539 char *complete_path = obstack_finish(&symbol_obstack);
1540 FILE *file = fopen(complete_path, "r");
1542 const char *filename = identify_string(complete_path);
1543 switch_input(file, filename);
1546 obstack_free(&symbol_obstack, complete_path);
1553 /* read till next newline character, only for parse_include_directive(),
1554 * use eat_pp_directive() in all other cases */
1555 static void skip_till_newline(void)
1557 /* skip till newline */
1570 static bool parse_include_directive(void)
1572 /* don't eat the TP_include here!
1573 * we need an alternative parsing for the next token */
1575 bool system_include = input.c == '<';
1577 string_t headername = pp_token.string.string;
1578 if (headername.begin == NULL) {
1584 if (!info.at_line_begin) {
1585 warningf(WARN_OTHER, &pp_token.base.source_position,
1586 "extra tokens at end of #include directive");
1587 skip_till_newline();
1590 if (n_inputs > INCLUDE_LIMIT) {
1591 errorf(&pp_token.base.source_position, "#include nested too deeply");
1593 next_preprocessing_token();
1600 bool res = do_include(system_include, pp_token.string.string.begin);
1602 errorf(&pp_token.base.source_position,
1603 "failed including '%S': %s", pp_token.string, strerror(errno));
1604 pop_restore_input();
1611 static pp_conditional_t *push_conditional(void)
1613 pp_conditional_t *conditional
1614 = obstack_alloc(&pp_obstack, sizeof(*conditional));
1615 memset(conditional, 0, sizeof(*conditional));
1617 conditional->parent = conditional_stack;
1618 conditional_stack = conditional;
1623 static void pop_conditional(void)
1625 assert(conditional_stack != NULL);
1626 conditional_stack = conditional_stack->parent;
1629 static void check_unclosed_conditionals(void)
1631 while (conditional_stack != NULL) {
1632 pp_conditional_t *conditional = conditional_stack;
1634 if (conditional->in_else) {
1635 errorf(&conditional->source_position, "unterminated #else");
1637 errorf(&conditional->source_position, "unterminated condition");
1643 static void parse_ifdef_ifndef_directive(void)
1645 bool is_ifndef = (pp_token.kind == TP_ifndef);
1647 next_preprocessing_token();
1651 pp_conditional_t *conditional = push_conditional();
1652 conditional->source_position = pp_token.base.source_position;
1653 conditional->skip = true;
1657 if (pp_token.kind != TP_IDENTIFIER || info.at_line_begin) {
1658 errorf(&pp_token.base.source_position,
1659 "expected identifier after #%s, got '%t'",
1660 is_ifndef ? "ifndef" : "ifdef", &pp_token);
1663 /* just take the true case in the hope to avoid further errors */
1666 /* evaluate wether we are in true or false case */
1667 condition = !pp_token.base.symbol->pp_definition == is_ifndef;
1669 next_preprocessing_token();
1671 if (!info.at_line_begin) {
1672 errorf(&pp_token.base.source_position,
1673 "extra tokens at end of #%s",
1674 is_ifndef ? "ifndef" : "ifdef");
1679 pp_conditional_t *conditional = push_conditional();
1680 conditional->source_position = pp_token.base.source_position;
1681 conditional->condition = condition;
1688 static void parse_else_directive(void)
1692 if (!info.at_line_begin) {
1694 warningf(WARN_OTHER, &pp_token.base.source_position, "extra tokens at end of #else");
1699 pp_conditional_t *conditional = conditional_stack;
1700 if (conditional == NULL) {
1701 errorf(&pp_token.base.source_position, "#else without prior #if");
1705 if (conditional->in_else) {
1706 errorf(&pp_token.base.source_position,
1707 "#else after #else (condition started %P)",
1708 conditional->source_position);
1713 conditional->in_else = true;
1714 if (!conditional->skip) {
1715 skip_mode = conditional->condition;
1717 conditional->source_position = pp_token.base.source_position;
1720 static void parse_endif_directive(void)
1724 if (!info.at_line_begin) {
1726 warningf(WARN_OTHER, &pp_token.base.source_position, "extra tokens at end of #endif");
1731 pp_conditional_t *conditional = conditional_stack;
1732 if (conditional == NULL) {
1733 errorf(&pp_token.base.source_position, "#endif without prior #if");
1737 if (!conditional->skip) {
1743 static void parse_preprocessing_directive(void)
1747 if (info.at_line_begin) {
1748 /* empty directive */
1753 switch (pp_token.kind) {
1756 parse_ifdef_ifndef_directive();
1759 parse_else_directive();
1762 parse_endif_directive();
1769 switch (pp_token.kind) {
1771 parse_define_directive();
1774 parse_undef_directive();
1778 parse_ifdef_ifndef_directive();
1781 parse_else_directive();
1784 parse_endif_directive();
1787 parse_include_directive();
1790 if (info.at_line_begin) {
1791 /* the nop directive "#" */
1794 errorf(&pp_token.base.source_position,
1795 "invalid preprocessing directive #%t", &pp_token);
1801 assert(info.at_line_begin);
1804 static void prepend_include_path(const char *path)
1806 searchpath_entry_t *entry = OALLOCZ(&config_obstack, searchpath_entry_t);
1808 entry->next = searchpath;
1812 static void setup_include_path(void)
1814 /* built-in paths */
1815 prepend_include_path("/usr/include");
1817 /* parse environment variable */
1818 const char *cpath = getenv("CPATH");
1819 if (cpath != NULL && *cpath != '\0') {
1820 const char *begin = cpath;
1824 while (*c != '\0' && *c != ':')
1827 size_t len = c-begin;
1829 /* for gcc compatibility (Matze: I would expect that
1830 * nothing happens for an empty entry...) */
1831 prepend_include_path(".");
1833 char *string = obstack_alloc(&config_obstack, len+1);
1834 memcpy(string, begin, len);
1837 prepend_include_path(string);
1844 } while(*c != '\0');
1848 int pptest_main(int argc, char **argv);
1849 int pptest_main(int argc, char **argv)
1851 init_symbol_table();
1854 obstack_init(&config_obstack);
1855 obstack_init(&pp_obstack);
1856 obstack_init(&input_obstack);
1857 strset_init(&stringset);
1859 setup_include_path();
1861 /* simplistic commandline parser */
1862 const char *filename = NULL;
1863 const char *output = NULL;
1864 for (int i = 1; i < argc; ++i) {
1865 const char *opt = argv[i];
1866 if (streq(opt, "-I")) {
1867 prepend_include_path(argv[++i]);
1869 } else if (streq(opt, "-E")) {
1871 } else if (streq(opt, "-o")) {
1874 } else if (opt[0] == '-') {
1875 fprintf(stderr, "Unknown option '%s'\n", opt);
1877 if (filename != NULL)
1878 fprintf(stderr, "Multiple inputs not supported\n");
1882 if (filename == NULL) {
1883 fprintf(stderr, "No input specified\n");
1887 if (output == NULL) {
1890 out = fopen(output, "w");
1892 fprintf(stderr, "Couldn't open output '%s'\n", output);
1897 /* just here for gcc compatibility */
1898 fprintf(out, "# 1 \"%s\"\n", filename);
1899 fprintf(out, "# 1 \"<built-in>\"\n");
1900 fprintf(out, "# 1 \"<command-line>\"\n");
1902 FILE *file = fopen(filename, "r");
1904 fprintf(stderr, "Couldn't open input '%s'\n", filename);
1907 switch_input(file, filename);
1910 if (pp_token.kind == '#' && info.at_line_begin) {
1911 parse_preprocessing_directive();
1913 } else if (pp_token.kind == TP_EOF) {
1914 goto end_of_main_loop;
1915 } else if (pp_token.kind == TP_IDENTIFIER) {
1916 symbol_t *const symbol = pp_token.base.symbol;
1917 pp_definition_t *const pp_definition = symbol->pp_definition;
1918 if (pp_definition != NULL && !pp_definition->is_expanding) {
1919 expansion_pos = pp_token.base.source_position;
1920 if (pp_definition->has_parameters) {
1921 source_position_t position = pp_token.base.source_position;
1922 add_token_info_t old_info = info;
1923 next_preprocessing_token();
1924 add_token_info_t new_info = info;
1926 /* no opening brace -> no expansion */
1927 if (pp_token.kind == '(') {
1930 /* parse arguments (TODO) */
1931 while (pp_token.kind != TP_EOF && pp_token.kind != ')')
1932 next_preprocessing_token();
1934 token_t next_token = pp_token;
1935 /* restore identifier token */
1936 pp_token.kind = TP_IDENTIFIER;
1937 pp_token.base.symbol = symbol;
1938 pp_token.base.source_position = position;
1943 pp_token = next_token;
1948 pp_definition->expand_pos = 0;
1949 pp_definition->is_expanding = true;
1950 current_expansion = pp_definition;
1957 next_preprocessing_token();
1962 check_unclosed_conditionals();
1967 obstack_free(&input_obstack, NULL);
1968 obstack_free(&pp_obstack, NULL);
1969 obstack_free(&config_obstack, NULL);
1971 strset_destroy(&stringset);
1974 exit_symbol_table();