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(string_encoding_t const enc)
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.encoding = enc;
509 pp_token.string.string = make_string(string, size);
512 static void parse_wide_character_constant(void)
519 const utf32 tc = parse_escape_sequence();
520 obstack_grow_symbol(&symbol_obstack, tc);
525 parse_error("newline while parsing character constant");
531 goto end_of_wide_char_constant;
534 parse_error("EOF while parsing character constant");
535 goto end_of_wide_char_constant;
538 obstack_grow_symbol(&symbol_obstack, input.c);
544 end_of_wide_char_constant:
545 obstack_1grow(&symbol_obstack, '\0');
546 size_t size = (size_t) obstack_object_size(&symbol_obstack)-1;
547 char *string = obstack_finish(&symbol_obstack);
548 pp_token.kind = TP_WIDE_CHARACTER_CONSTANT;
549 pp_token.string.string = make_string(string, size);
552 parse_error("empty character constant");
556 static void parse_character_constant(void)
558 const unsigned start_linenr = input.position.lineno;
566 tc = parse_escape_sequence();
567 obstack_1grow(&symbol_obstack, (char) tc);
571 parse_error("newline while parsing character constant");
576 source_position_t source_position;
577 source_position.input_name = pp_token.base.source_position.input_name;
578 source_position.lineno = start_linenr;
579 errorf(&source_position, "EOF while parsing character constant");
580 goto end_of_char_constant;
585 goto end_of_char_constant;
588 obstack_1grow(&symbol_obstack, (char) input.c);
595 end_of_char_constant:;
596 obstack_1grow(&symbol_obstack, '\0');
597 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
598 char *const string = obstack_finish(&symbol_obstack);
600 pp_token.kind = TP_CHARACTER_CONSTANT;
601 pp_token.string.string = make_string(string, size);
604 parse_error("empty character constant");
608 #define SYMBOL_CHARS_WITHOUT_E_P \
659 #define SYMBOL_CHARS \
660 SYMBOL_CHARS_WITHOUT_E_P \
679 * returns next final token from a preprocessor macro expansion
681 static void expand_next(void)
683 assert(current_expansion != NULL);
685 pp_definition_t *definition = current_expansion;
688 if (definition->list_len == 0
689 || definition->expand_pos >= definition->list_len) {
690 /* we're finished with the current macro, move up 1 level in the
692 pp_definition_t *parent = definition->parent_expansion;
693 definition->parent_expansion = NULL;
694 definition->is_expanding = false;
696 /* it was the outermost expansion, parse normal pptoken */
697 if (parent == NULL) {
698 current_expansion = NULL;
699 next_preprocessing_token();
703 current_expansion = definition;
706 pp_token = definition->token_list[definition->expand_pos];
707 pp_token.base.source_position = expansion_pos;
708 ++definition->expand_pos;
710 if (pp_token.kind != TP_IDENTIFIER)
713 /* if it was an identifier then we might need to expand again */
714 pp_definition_t *const symbol_definition = pp_token.base.symbol->pp_definition;
715 if (symbol_definition != NULL && !symbol_definition->is_expanding) {
716 symbol_definition->parent_expansion = definition;
717 symbol_definition->expand_pos = 0;
718 symbol_definition->is_expanding = true;
719 definition = symbol_definition;
720 current_expansion = definition;
725 static void skip_line_comment(void)
743 static void skip_multiline_comment(void)
745 unsigned start_linenr = input.position.lineno;
750 if (input.c == '*') {
751 /* TODO: nested comment, warn here */
756 if (input.c == '/') {
757 if (input.position.lineno != input.output_line)
758 info.whitespace = input.position.colno;
769 source_position_t source_position;
770 source_position.input_name = pp_token.base.source_position.input_name;
771 source_position.lineno = start_linenr;
772 errorf(&source_position, "at end of file while looking for comment end");
783 static void skip_whitespace(void)
793 info.at_line_begin = true;
799 if (input.c == '/') {
803 } else if (input.c == '*') {
805 skip_multiline_comment();
818 static void eat_pp(preprocessor_token_kind_t const type)
821 assert(pp_token.kind == type);
822 next_preprocessing_token();
825 static void parse_symbol(void)
827 obstack_1grow(&symbol_obstack, (char) input.c);
834 obstack_1grow(&symbol_obstack, (char) input.c);
844 obstack_1grow(&symbol_obstack, '\0');
845 char *string = obstack_finish(&symbol_obstack);
847 /* might be a wide string or character constant ( L"string"/L'c' ) */
848 if (input.c == '"' && string[0] == 'L' && string[1] == '\0') {
849 obstack_free(&symbol_obstack, string);
850 parse_string_literal(STRING_ENCODING_WIDE);
852 } else if (input.c == '\'' && string[0] == 'L' && string[1] == '\0') {
853 obstack_free(&symbol_obstack, string);
854 parse_wide_character_constant();
858 symbol_t *symbol = symbol_table_insert(string);
860 pp_token.kind = symbol->pp_ID;
861 pp_token.base.symbol = symbol;
863 /* we can free the memory from symbol obstack if we already had an entry in
864 * the symbol table */
865 if (symbol->string != string) {
866 obstack_free(&symbol_obstack, string);
870 static void parse_number(void)
872 obstack_1grow(&symbol_obstack, (char) input.c);
879 SYMBOL_CHARS_WITHOUT_E_P
880 obstack_1grow(&symbol_obstack, (char) input.c);
888 obstack_1grow(&symbol_obstack, (char) input.c);
890 if (input.c == '+' || input.c == '-') {
891 obstack_1grow(&symbol_obstack, (char) input.c);
902 obstack_1grow(&symbol_obstack, '\0');
903 size_t size = obstack_object_size(&symbol_obstack);
904 char *string = obstack_finish(&symbol_obstack);
906 pp_token.kind = TP_NUMBER;
907 pp_token.number.number = make_string(string, size);
911 #define MAYBE_PROLOG \
916 #define MAYBE(ch, set_type) \
919 pp_token.kind = set_type; \
922 #define ELSE_CODE(code) \
929 #define ELSE(set_type) \
931 pp_token.kind = set_type; \
934 static void next_preprocessing_token(void)
936 if (current_expansion != NULL) {
941 info.at_line_begin = false;
942 info.had_whitespace = false;
944 pp_token.base.source_position = input.position;
945 pp_token.base.symbol = NULL;
951 info.had_whitespace = true;
956 info.at_line_begin = true;
957 info.had_whitespace = true;
970 parse_string_literal(STRING_ENCODING_CHAR);
974 parse_character_constant();
996 MAYBE('.', TP_DOTDOTDOT)
1000 pp_token.kind = '.';
1005 MAYBE('&', TP_ANDAND)
1006 MAYBE('=', TP_ANDEQUAL)
1010 MAYBE('=', TP_ASTERISKEQUAL)
1014 MAYBE('+', TP_PLUSPLUS)
1015 MAYBE('=', TP_PLUSEQUAL)
1019 MAYBE('>', TP_MINUSGREATER)
1020 MAYBE('-', TP_MINUSMINUS)
1021 MAYBE('=', TP_MINUSEQUAL)
1025 MAYBE('=', TP_EXCLAMATIONMARKEQUAL)
1029 MAYBE('=', TP_SLASHEQUAL)
1032 info.had_whitespace = true;
1033 skip_multiline_comment();
1037 info.had_whitespace = true;
1038 skip_line_comment();
1044 MAYBE('=', TP_PERCENTEQUAL)
1049 MAYBE(':', TP_HASHHASH)
1053 pp_token.kind = '#';
1061 MAYBE('=', TP_LESSEQUAL)
1064 MAYBE('=', TP_LESSLESSEQUAL)
1069 MAYBE('=', TP_GREATEREQUAL)
1072 MAYBE('=', TP_GREATERGREATEREQUAL)
1073 ELSE(TP_GREATERGREATER)
1077 MAYBE('=', TP_CARETEQUAL)
1081 MAYBE('=', TP_PIPEEQUAL)
1082 MAYBE('|', TP_PIPEPIPE)
1090 MAYBE('=', TP_EQUALEQUAL)
1094 MAYBE('#', TP_HASHHASH)
1096 pp_token.kind = '#';
1110 pp_token.kind = input.c;
1115 if (input_stack != NULL) {
1117 pop_restore_input();
1119 print_line_directive(&input.position, "2");
1122 pp_token.base.source_position.lineno++;
1123 info.at_line_begin = true;
1124 pp_token.kind = TP_EOF;
1130 if (!ignore_unknown_chars) {
1131 errorf(&pp_token.base.source_position,
1132 "unknown character '%c' found\n", input.c);
1135 pp_token.kind = input.c;
1141 static void print_quoted_string(const char *const string)
1144 for (const char *c = string; *c != 0; ++c) {
1146 case '"': fputs("\\\"", out); break;
1147 case '\\': fputs("\\\\", out); break;
1148 case '\a': fputs("\\a", out); break;
1149 case '\b': fputs("\\b", out); break;
1150 case '\f': fputs("\\f", out); break;
1151 case '\n': fputs("\\n", out); break;
1152 case '\r': fputs("\\r", out); break;
1153 case '\t': fputs("\\t", out); break;
1154 case '\v': fputs("\\v", out); break;
1155 case '\?': fputs("\\?", out); break;
1158 fprintf(out, "\\%03o", (unsigned)*c);
1168 static void print_line_directive(const source_position_t *pos, const char *add)
1170 fprintf(out, "# %u ", pos->lineno);
1171 print_quoted_string(pos->input_name);
1177 printed_input_name = pos->input_name;
1178 input.output_line = pos->lineno-1;
1181 static bool emit_newlines(void)
1183 unsigned delta = pp_token.base.source_position.lineno - input.output_line;
1189 print_line_directive(&pp_token.base.source_position, NULL);
1192 for (unsigned i = 0; i < delta; ++i) {
1196 input.output_line = pp_token.base.source_position.lineno;
1198 for (unsigned i = 0; i < info.whitespace; ++i)
1204 static void emit_pp_token(void)
1209 if (!emit_newlines() &&
1210 (info.had_whitespace || tokens_would_paste(last_token, pp_token.kind)))
1213 switch (pp_token.kind) {
1215 fputs(pp_token.base.symbol->string, out);
1218 fputs(pp_token.number.number.begin, out);
1221 case TP_STRING_LITERAL:
1222 fputs(get_string_encoding_prefix(pp_token.string.encoding), out);
1224 fputs(pp_token.string.string.begin, out);
1227 case TP_WIDE_CHARACTER_CONSTANT:
1229 case TP_CHARACTER_CONSTANT:
1231 fputs(pp_token.string.string.begin, out);
1235 print_pp_token_kind(out, pp_token.kind);
1238 last_token = pp_token.kind;
1241 static void eat_pp_directive(void)
1243 while (!info.at_line_begin) {
1244 next_preprocessing_token();
1248 static bool strings_equal(const string_t *string1, const string_t *string2)
1250 size_t size = string1->size;
1251 if (size != string2->size)
1254 const char *c1 = string1->begin;
1255 const char *c2 = string2->begin;
1256 for (size_t i = 0; i < size; ++i, ++c1, ++c2) {
1263 static bool pp_tokens_equal(const token_t *token1, const token_t *token2)
1265 if (token1->kind != token2->kind)
1268 switch (token1->kind) {
1270 return token1->base.symbol == token2->base.symbol;
1273 case TP_CHARACTER_CONSTANT:
1274 case TP_STRING_LITERAL:
1275 return strings_equal(&token1->string.string, &token2->string.string);
1282 static bool pp_definitions_equal(const pp_definition_t *definition1,
1283 const pp_definition_t *definition2)
1285 if (definition1->list_len != definition2->list_len)
1288 size_t len = definition1->list_len;
1289 const token_t *t1 = definition1->token_list;
1290 const token_t *t2 = definition2->token_list;
1291 for (size_t i = 0; i < len; ++i, ++t1, ++t2) {
1292 if (!pp_tokens_equal(t1, t2))
1298 static void parse_define_directive(void)
1301 assert(obstack_object_size(&pp_obstack) == 0);
1303 if (pp_token.kind != TP_IDENTIFIER || info.at_line_begin) {
1304 errorf(&pp_token.base.source_position,
1305 "expected identifier after #define, got '%t'", &pp_token);
1308 symbol_t *const symbol = pp_token.base.symbol;
1310 pp_definition_t *new_definition
1311 = obstack_alloc(&pp_obstack, sizeof(new_definition[0]));
1312 memset(new_definition, 0, sizeof(new_definition[0]));
1313 new_definition->source_position = input.position;
1315 /* this is probably the only place where spaces are significant in the
1316 * lexer (except for the fact that they separate tokens). #define b(x)
1317 * is something else than #define b (x) */
1318 if (input.c == '(') {
1320 next_preprocessing_token();
1321 /* get next token after '(' */
1322 next_preprocessing_token();
1325 switch (pp_token.kind) {
1327 new_definition->is_variadic = true;
1328 next_preprocessing_token();
1329 if (pp_token.kind != ')') {
1330 errorf(&input.position,
1331 "'...' not at end of macro argument list");
1336 obstack_ptr_grow(&pp_obstack, pp_token.base.symbol);
1337 next_preprocessing_token();
1339 if (pp_token.kind == ',') {
1340 next_preprocessing_token();
1344 if (pp_token.kind != ')') {
1345 errorf(&pp_token.base.source_position,
1346 "expected ',' or ')' after identifier, got '%t'",
1352 next_preprocessing_token();
1353 goto finish_argument_list;
1355 errorf(&pp_token.base.source_position,
1356 "expected identifier, '...' or ')' in #define argument list, got '%t'",
1362 finish_argument_list:
1363 new_definition->has_parameters = true;
1364 new_definition->n_parameters
1365 = obstack_object_size(&pp_obstack) / sizeof(new_definition->parameters[0]);
1366 new_definition->parameters = obstack_finish(&pp_obstack);
1368 next_preprocessing_token();
1371 /* construct a new pp_definition on the obstack */
1372 assert(obstack_object_size(&pp_obstack) == 0);
1373 size_t list_len = 0;
1374 while (!info.at_line_begin) {
1375 obstack_grow(&pp_obstack, &pp_token, sizeof(pp_token));
1377 next_preprocessing_token();
1380 new_definition->list_len = list_len;
1381 new_definition->token_list = obstack_finish(&pp_obstack);
1383 pp_definition_t *old_definition = symbol->pp_definition;
1384 if (old_definition != NULL) {
1385 if (!pp_definitions_equal(old_definition, new_definition)) {
1386 warningf(WARN_OTHER, &input.position, "multiple definition of macro '%Y' (first defined %P)", symbol, &old_definition->source_position);
1388 /* reuse the old definition */
1389 obstack_free(&pp_obstack, new_definition);
1390 new_definition = old_definition;
1394 symbol->pp_definition = new_definition;
1398 if (obstack_object_size(&pp_obstack) > 0) {
1399 char *ptr = obstack_finish(&pp_obstack);
1400 obstack_free(&pp_obstack, ptr);
1405 static void parse_undef_directive(void)
1409 if (pp_token.kind != TP_IDENTIFIER) {
1410 errorf(&input.position,
1411 "expected identifier after #undef, got '%t'", &pp_token);
1416 pp_token.base.symbol->pp_definition = NULL;
1417 next_preprocessing_token();
1419 if (!info.at_line_begin) {
1420 warningf(WARN_OTHER, &input.position, "extra tokens at end of #undef directive");
1425 static void parse_headername(void)
1427 const source_position_t start_position = input.position;
1428 string_t string = {NULL, 0};
1429 assert(obstack_object_size(&symbol_obstack) == 0);
1431 /* behind an #include we can have the special headername lexems.
1432 * They're only allowed behind an #include so they're not recognized
1433 * by the normal next_preprocessing_token. We handle them as a special
1435 if (info.at_line_begin) {
1436 parse_error("expected headername after #include");
1440 /* check wether we have a "... or <... headername */
1449 parse_error("header name without closing '>'");
1454 goto finished_headername;
1456 obstack_1grow(&symbol_obstack, (char) input.c);
1459 /* we should never be here */
1468 parse_error("header name without closing '>'");
1473 goto finished_headername;
1475 obstack_1grow(&symbol_obstack, (char) input.c);
1478 /* we should never be here */
1481 /* TODO: do normal pp_token parsing and concatenate results */
1482 panic("pp_token concat include not implemented yet");
1485 finished_headername:
1486 obstack_1grow(&symbol_obstack, '\0');
1487 const size_t size = (size_t)obstack_object_size(&symbol_obstack);
1488 char *const headername = obstack_finish(&symbol_obstack);
1489 string = make_string(headername, size);
1492 pp_token.base.source_position = start_position;
1493 pp_token.kind = TP_HEADERNAME;
1494 pp_token.string.string = string;
1497 static bool do_include(bool system_include, const char *headername)
1499 size_t headername_len = strlen(headername);
1500 if (!system_include) {
1501 /* put dirname of current input on obstack */
1502 const char *filename = input.position.input_name;
1503 const char *last_slash = strrchr(filename, '/');
1504 if (last_slash != NULL) {
1505 size_t len = last_slash - filename;
1506 obstack_grow(&symbol_obstack, filename, len + 1);
1507 obstack_grow0(&symbol_obstack, headername, headername_len);
1508 char *complete_path = obstack_finish(&symbol_obstack);
1509 headername = identify_string(complete_path);
1512 FILE *file = fopen(headername, "r");
1514 switch_input(file, headername);
1519 assert(obstack_object_size(&symbol_obstack) == 0);
1520 /* check searchpath */
1521 for (searchpath_entry_t *entry = searchpath; entry != NULL;
1522 entry = entry->next) {
1523 const char *path = entry->path;
1524 size_t len = strlen(path);
1525 obstack_grow(&symbol_obstack, path, len);
1526 if (path[len-1] != '/')
1527 obstack_1grow(&symbol_obstack, '/');
1528 obstack_grow(&symbol_obstack, headername, headername_len+1);
1530 char *complete_path = obstack_finish(&symbol_obstack);
1531 FILE *file = fopen(complete_path, "r");
1533 const char *filename = identify_string(complete_path);
1534 switch_input(file, filename);
1537 obstack_free(&symbol_obstack, complete_path);
1544 /* read till next newline character, only for parse_include_directive(),
1545 * use eat_pp_directive() in all other cases */
1546 static void skip_till_newline(void)
1548 /* skip till newline */
1561 static bool parse_include_directive(void)
1563 /* don't eat the TP_include here!
1564 * we need an alternative parsing for the next token */
1566 bool system_include = input.c == '<';
1568 string_t headername = pp_token.string.string;
1569 if (headername.begin == NULL) {
1575 if (!info.at_line_begin) {
1576 warningf(WARN_OTHER, &pp_token.base.source_position,
1577 "extra tokens at end of #include directive");
1578 skip_till_newline();
1581 if (n_inputs > INCLUDE_LIMIT) {
1582 errorf(&pp_token.base.source_position, "#include nested too deeply");
1584 next_preprocessing_token();
1591 bool res = do_include(system_include, pp_token.string.string.begin);
1593 errorf(&pp_token.base.source_position,
1594 "failed including '%S': %s", pp_token.string, strerror(errno));
1595 pop_restore_input();
1602 static pp_conditional_t *push_conditional(void)
1604 pp_conditional_t *conditional
1605 = obstack_alloc(&pp_obstack, sizeof(*conditional));
1606 memset(conditional, 0, sizeof(*conditional));
1608 conditional->parent = conditional_stack;
1609 conditional_stack = conditional;
1614 static void pop_conditional(void)
1616 assert(conditional_stack != NULL);
1617 conditional_stack = conditional_stack->parent;
1620 static void check_unclosed_conditionals(void)
1622 while (conditional_stack != NULL) {
1623 pp_conditional_t *conditional = conditional_stack;
1625 if (conditional->in_else) {
1626 errorf(&conditional->source_position, "unterminated #else");
1628 errorf(&conditional->source_position, "unterminated condition");
1634 static void parse_ifdef_ifndef_directive(void)
1636 bool is_ifndef = (pp_token.kind == TP_ifndef);
1638 next_preprocessing_token();
1642 pp_conditional_t *conditional = push_conditional();
1643 conditional->source_position = pp_token.base.source_position;
1644 conditional->skip = true;
1648 if (pp_token.kind != TP_IDENTIFIER || info.at_line_begin) {
1649 errorf(&pp_token.base.source_position,
1650 "expected identifier after #%s, got '%t'",
1651 is_ifndef ? "ifndef" : "ifdef", &pp_token);
1654 /* just take the true case in the hope to avoid further errors */
1657 /* evaluate wether we are in true or false case */
1658 condition = !pp_token.base.symbol->pp_definition == is_ifndef;
1660 next_preprocessing_token();
1662 if (!info.at_line_begin) {
1663 errorf(&pp_token.base.source_position,
1664 "extra tokens at end of #%s",
1665 is_ifndef ? "ifndef" : "ifdef");
1670 pp_conditional_t *conditional = push_conditional();
1671 conditional->source_position = pp_token.base.source_position;
1672 conditional->condition = condition;
1679 static void parse_else_directive(void)
1683 if (!info.at_line_begin) {
1685 warningf(WARN_OTHER, &pp_token.base.source_position, "extra tokens at end of #else");
1690 pp_conditional_t *conditional = conditional_stack;
1691 if (conditional == NULL) {
1692 errorf(&pp_token.base.source_position, "#else without prior #if");
1696 if (conditional->in_else) {
1697 errorf(&pp_token.base.source_position,
1698 "#else after #else (condition started %P)",
1699 conditional->source_position);
1704 conditional->in_else = true;
1705 if (!conditional->skip) {
1706 skip_mode = conditional->condition;
1708 conditional->source_position = pp_token.base.source_position;
1711 static void parse_endif_directive(void)
1715 if (!info.at_line_begin) {
1717 warningf(WARN_OTHER, &pp_token.base.source_position, "extra tokens at end of #endif");
1722 pp_conditional_t *conditional = conditional_stack;
1723 if (conditional == NULL) {
1724 errorf(&pp_token.base.source_position, "#endif without prior #if");
1728 if (!conditional->skip) {
1734 static void parse_preprocessing_directive(void)
1738 if (info.at_line_begin) {
1739 /* empty directive */
1744 switch (pp_token.kind) {
1747 parse_ifdef_ifndef_directive();
1750 parse_else_directive();
1753 parse_endif_directive();
1760 switch (pp_token.kind) {
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.base.source_position,
1786 "invalid preprocessing directive #%t", &pp_token);
1792 assert(info.at_line_begin);
1795 static void prepend_include_path(const char *path)
1797 searchpath_entry_t *entry = OALLOCZ(&config_obstack, searchpath_entry_t);
1799 entry->next = searchpath;
1803 static void setup_include_path(void)
1805 /* built-in paths */
1806 prepend_include_path("/usr/include");
1808 /* parse environment variable */
1809 const char *cpath = getenv("CPATH");
1810 if (cpath != NULL && *cpath != '\0') {
1811 const char *begin = cpath;
1815 while (*c != '\0' && *c != ':')
1818 size_t len = c-begin;
1820 /* for gcc compatibility (Matze: I would expect that
1821 * nothing happens for an empty entry...) */
1822 prepend_include_path(".");
1824 char *string = obstack_alloc(&config_obstack, len+1);
1825 memcpy(string, begin, len);
1828 prepend_include_path(string);
1835 } while(*c != '\0');
1839 int pptest_main(int argc, char **argv);
1840 int pptest_main(int argc, char **argv)
1842 init_symbol_table();
1845 obstack_init(&config_obstack);
1846 obstack_init(&pp_obstack);
1847 obstack_init(&input_obstack);
1848 strset_init(&stringset);
1850 setup_include_path();
1852 /* simplistic commandline parser */
1853 const char *filename = NULL;
1854 const char *output = 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 (streq(opt, "-o")) {
1865 } else if (opt[0] == '-') {
1866 fprintf(stderr, "Unknown option '%s'\n", opt);
1868 if (filename != NULL)
1869 fprintf(stderr, "Multiple inputs not supported\n");
1873 if (filename == NULL) {
1874 fprintf(stderr, "No input specified\n");
1878 if (output == NULL) {
1881 out = fopen(output, "w");
1883 fprintf(stderr, "Couldn't open output '%s'\n", output);
1888 /* just here for gcc compatibility */
1889 fprintf(out, "# 1 \"%s\"\n", filename);
1890 fprintf(out, "# 1 \"<built-in>\"\n");
1891 fprintf(out, "# 1 \"<command-line>\"\n");
1893 FILE *file = fopen(filename, "r");
1895 fprintf(stderr, "Couldn't open input '%s'\n", filename);
1898 switch_input(file, filename);
1901 if (pp_token.kind == '#' && info.at_line_begin) {
1902 parse_preprocessing_directive();
1904 } else if (pp_token.kind == TP_EOF) {
1905 goto end_of_main_loop;
1906 } else if (pp_token.kind == TP_IDENTIFIER) {
1907 symbol_t *const symbol = pp_token.base.symbol;
1908 pp_definition_t *const pp_definition = symbol->pp_definition;
1909 if (pp_definition != NULL && !pp_definition->is_expanding) {
1910 expansion_pos = pp_token.base.source_position;
1911 if (pp_definition->has_parameters) {
1912 source_position_t position = pp_token.base.source_position;
1913 add_token_info_t old_info = info;
1914 next_preprocessing_token();
1915 add_token_info_t new_info = info;
1917 /* no opening brace -> no expansion */
1918 if (pp_token.kind == '(') {
1921 /* parse arguments (TODO) */
1922 while (pp_token.kind != TP_EOF && pp_token.kind != ')')
1923 next_preprocessing_token();
1925 token_t next_token = pp_token;
1926 /* restore identifier token */
1927 pp_token.kind = TP_IDENTIFIER;
1928 pp_token.base.symbol = symbol;
1929 pp_token.base.source_position = position;
1934 pp_token = next_token;
1939 pp_definition->expand_pos = 0;
1940 pp_definition->is_expanding = true;
1941 current_expansion = pp_definition;
1948 next_preprocessing_token();
1953 check_unclosed_conditionals();
1958 obstack_free(&input_obstack, NULL);
1959 obstack_free(&pp_obstack, NULL);
1960 obstack_free(&config_obstack, NULL);
1962 strset_destroy(&stringset);
1965 exit_symbol_table();