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;
224 if (input.c == '\n') { \
228 info.whitespace = 0; \
229 ++input.position.lineno; \
230 input.position.colno = 1; \
232 newline // Let it look like an ordinary case label.
234 #define eat(c_type) (assert(input.c == c_type), next_char())
236 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 sym_make_string(string_encoding_t const enc)
456 obstack_1grow(&symbol_obstack, '\0');
457 size_t const len = obstack_object_size(&symbol_obstack) - 1;
458 char *const string = obstack_finish(&symbol_obstack);
459 char const *const result = identify_string(string);
460 return (string_t){ result, len, enc };
463 static void parse_string(utf32 const delimiter, preprocessor_token_kind_t const kind, string_encoding_t const enc, char const *const context)
465 const unsigned start_linenr = input.position.lineno;
472 if (resolve_escape_sequences) {
473 utf32 const tc = parse_escape_sequence();
474 if (enc == STRING_ENCODING_CHAR) {
476 warningf(WARN_OTHER, &pp_token.base.source_position, "escape sequence out of range");
478 obstack_1grow(&symbol_obstack, tc);
480 obstack_grow_symbol(&symbol_obstack, tc);
483 obstack_1grow(&symbol_obstack, (char)input.c);
485 obstack_1grow(&symbol_obstack, (char)input.c);
492 errorf(&pp_token.base.source_position, "newline while parsing %s", context);
496 source_position_t source_position;
497 source_position.input_name = pp_token.base.source_position.input_name;
498 source_position.lineno = start_linenr;
499 errorf(&source_position, "EOF while parsing %s", context);
504 if (input.c == delimiter) {
508 obstack_grow_symbol(&symbol_obstack, input.c);
516 pp_token.kind = kind;
517 pp_token.literal.string = sym_make_string(enc);
520 static void parse_string_literal(string_encoding_t const enc)
522 parse_string('"', TP_STRING_LITERAL, enc, "string literal");
525 static void parse_character_constant(string_encoding_t const enc)
527 parse_string('\'', TP_CHARACTER_CONSTANT, enc, "character constant");
528 if (pp_token.literal.string.size == 0) {
529 parse_error("empty character constant");
533 #define SYMBOL_CASES_WITHOUT_E_P \
584 #define SYMBOL_CASES \
585 SYMBOL_CASES_WITHOUT_E_P: \
591 #define DIGIT_CASES \
604 * returns next final token from a preprocessor macro expansion
606 static void expand_next(void)
608 assert(current_expansion != NULL);
610 pp_definition_t *definition = current_expansion;
613 if (definition->list_len == 0
614 || definition->expand_pos >= definition->list_len) {
615 /* we're finished with the current macro, move up 1 level in the
617 pp_definition_t *parent = definition->parent_expansion;
618 definition->parent_expansion = NULL;
619 definition->is_expanding = false;
621 /* it was the outermost expansion, parse normal pptoken */
622 if (parent == NULL) {
623 current_expansion = NULL;
624 next_preprocessing_token();
628 current_expansion = definition;
631 pp_token = definition->token_list[definition->expand_pos];
632 pp_token.base.source_position = expansion_pos;
633 ++definition->expand_pos;
635 if (pp_token.kind != TP_IDENTIFIER)
638 /* if it was an identifier then we might need to expand again */
639 pp_definition_t *const symbol_definition = pp_token.base.symbol->pp_definition;
640 if (symbol_definition != NULL && !symbol_definition->is_expanding) {
641 symbol_definition->parent_expansion = definition;
642 symbol_definition->expand_pos = 0;
643 symbol_definition->is_expanding = true;
644 definition = symbol_definition;
645 current_expansion = definition;
650 static void skip_line_comment(void)
668 static void skip_multiline_comment(void)
670 unsigned start_linenr = input.position.lineno;
675 if (input.c == '*') {
676 /* TODO: nested comment, warn here */
681 if (input.c == '/') {
682 if (input.position.lineno != input.output_line)
683 info.whitespace = input.position.colno;
693 source_position_t source_position;
694 source_position.input_name = pp_token.base.source_position.input_name;
695 source_position.lineno = start_linenr;
696 errorf(&source_position, "at end of file while looking for comment end");
707 static void skip_whitespace(void)
717 info.at_line_begin = true;
722 if (input.c == '/') {
726 } else if (input.c == '*') {
728 skip_multiline_comment();
741 static void eat_pp(preprocessor_token_kind_t const type)
744 assert(pp_token.kind == type);
745 next_preprocessing_token();
748 static void parse_symbol(void)
750 obstack_1grow(&symbol_obstack, (char) input.c);
757 obstack_1grow(&symbol_obstack, (char) input.c);
767 obstack_1grow(&symbol_obstack, '\0');
768 char *string = obstack_finish(&symbol_obstack);
770 /* might be a wide string or character constant ( L"string"/L'c' ) */
771 if (input.c == '"' && string[0] == 'L' && string[1] == '\0') {
772 obstack_free(&symbol_obstack, string);
773 parse_string_literal(STRING_ENCODING_WIDE);
775 } else if (input.c == '\'' && string[0] == 'L' && string[1] == '\0') {
776 obstack_free(&symbol_obstack, string);
777 parse_character_constant(STRING_ENCODING_WIDE);
781 symbol_t *symbol = symbol_table_insert(string);
783 pp_token.kind = symbol->pp_ID;
784 pp_token.base.symbol = symbol;
786 /* we can free the memory from symbol obstack if we already had an entry in
787 * the symbol table */
788 if (symbol->string != string) {
789 obstack_free(&symbol_obstack, string);
793 static void parse_number(void)
795 obstack_1grow(&symbol_obstack, (char) input.c);
802 case SYMBOL_CASES_WITHOUT_E_P:
803 obstack_1grow(&symbol_obstack, (char) input.c);
811 obstack_1grow(&symbol_obstack, (char) input.c);
813 if (input.c == '+' || input.c == '-') {
814 obstack_1grow(&symbol_obstack, (char) input.c);
825 pp_token.kind = TP_NUMBER;
826 pp_token.literal.string = sym_make_string(STRING_ENCODING_CHAR);
830 #define MAYBE_PROLOG \
835 #define MAYBE(ch, set_type) \
838 pp_token.kind = set_type; \
841 #define ELSE_CODE(code) \
848 #define ELSE(set_type) \
850 pp_token.kind = set_type; \
853 static void next_preprocessing_token(void)
855 if (current_expansion != NULL) {
860 info.at_line_begin = false;
861 info.had_whitespace = false;
863 pp_token.base.source_position = input.position;
864 pp_token.base.symbol = NULL;
870 info.had_whitespace = true;
875 info.at_line_begin = true;
876 info.had_whitespace = true;
888 parse_string_literal(STRING_ENCODING_CHAR);
892 parse_character_constant(STRING_ENCODING_CHAR);
914 MAYBE('.', TP_DOTDOTDOT)
923 MAYBE('&', TP_ANDAND)
924 MAYBE('=', TP_ANDEQUAL)
928 MAYBE('=', TP_ASTERISKEQUAL)
932 MAYBE('+', TP_PLUSPLUS)
933 MAYBE('=', TP_PLUSEQUAL)
937 MAYBE('>', TP_MINUSGREATER)
938 MAYBE('-', TP_MINUSMINUS)
939 MAYBE('=', TP_MINUSEQUAL)
943 MAYBE('=', TP_EXCLAMATIONMARKEQUAL)
947 MAYBE('=', TP_SLASHEQUAL)
950 info.had_whitespace = true;
951 skip_multiline_comment();
955 info.had_whitespace = true;
962 MAYBE('=', TP_PERCENTEQUAL)
967 MAYBE(':', TP_HASHHASH)
979 MAYBE('=', TP_LESSEQUAL)
982 MAYBE('=', TP_LESSLESSEQUAL)
987 MAYBE('=', TP_GREATEREQUAL)
990 MAYBE('=', TP_GREATERGREATEREQUAL)
991 ELSE(TP_GREATERGREATER)
995 MAYBE('=', TP_CARETEQUAL)
999 MAYBE('=', TP_PIPEEQUAL)
1000 MAYBE('|', TP_PIPEPIPE)
1008 MAYBE('=', TP_EQUALEQUAL)
1012 MAYBE('#', TP_HASHHASH)
1014 pp_token.kind = '#';
1027 pp_token.kind = input.c;
1032 if (input_stack != NULL) {
1034 pop_restore_input();
1036 print_line_directive(&input.position, "2");
1039 pp_token.base.source_position.lineno++;
1040 info.at_line_begin = true;
1041 pp_token.kind = TP_EOF;
1047 if (!ignore_unknown_chars) {
1048 errorf(&pp_token.base.source_position,
1049 "unknown character '%c' found\n", input.c);
1052 pp_token.kind = input.c;
1058 static void print_quoted_string(const char *const string)
1061 for (const char *c = string; *c != 0; ++c) {
1063 case '"': fputs("\\\"", out); break;
1064 case '\\': fputs("\\\\", out); break;
1065 case '\a': fputs("\\a", out); break;
1066 case '\b': fputs("\\b", out); break;
1067 case '\f': fputs("\\f", out); break;
1068 case '\n': fputs("\\n", out); break;
1069 case '\r': fputs("\\r", out); break;
1070 case '\t': fputs("\\t", out); break;
1071 case '\v': fputs("\\v", out); break;
1072 case '\?': fputs("\\?", out); break;
1075 fprintf(out, "\\%03o", (unsigned)*c);
1085 static void print_line_directive(const source_position_t *pos, const char *add)
1087 fprintf(out, "# %u ", pos->lineno);
1088 print_quoted_string(pos->input_name);
1094 printed_input_name = pos->input_name;
1095 input.output_line = pos->lineno-1;
1098 static bool emit_newlines(void)
1100 unsigned delta = pp_token.base.source_position.lineno - input.output_line;
1106 print_line_directive(&pp_token.base.source_position, NULL);
1109 for (unsigned i = 0; i < delta; ++i) {
1113 input.output_line = pp_token.base.source_position.lineno;
1115 for (unsigned i = 0; i < info.whitespace; ++i)
1121 static void emit_pp_token(void)
1126 if (!emit_newlines() &&
1127 (info.had_whitespace || tokens_would_paste(last_token, pp_token.kind)))
1130 switch (pp_token.kind) {
1132 fputs(pp_token.base.symbol->string, out);
1135 fputs(pp_token.literal.string.begin, out);
1138 case TP_STRING_LITERAL:
1139 fputs(get_string_encoding_prefix(pp_token.literal.string.encoding), out);
1141 fputs(pp_token.literal.string.begin, out);
1145 case TP_CHARACTER_CONSTANT:
1146 fputs(get_string_encoding_prefix(pp_token.literal.string.encoding), out);
1148 fputs(pp_token.literal.string.begin, out);
1152 print_pp_token_kind(out, pp_token.kind);
1155 last_token = pp_token.kind;
1158 static void eat_pp_directive(void)
1160 while (!info.at_line_begin) {
1161 next_preprocessing_token();
1165 static bool strings_equal(const string_t *string1, const string_t *string2)
1167 size_t size = string1->size;
1168 if (size != string2->size)
1171 const char *c1 = string1->begin;
1172 const char *c2 = string2->begin;
1173 for (size_t i = 0; i < size; ++i, ++c1, ++c2) {
1180 static bool pp_tokens_equal(const token_t *token1, const token_t *token2)
1182 if (token1->kind != token2->kind)
1185 switch (token1->kind) {
1187 return token1->base.symbol == token2->base.symbol;
1190 case TP_CHARACTER_CONSTANT:
1191 case TP_STRING_LITERAL:
1192 return strings_equal(&token1->literal.string, &token2->literal.string);
1199 static bool pp_definitions_equal(const pp_definition_t *definition1,
1200 const pp_definition_t *definition2)
1202 if (definition1->list_len != definition2->list_len)
1205 size_t len = definition1->list_len;
1206 const token_t *t1 = definition1->token_list;
1207 const token_t *t2 = definition2->token_list;
1208 for (size_t i = 0; i < len; ++i, ++t1, ++t2) {
1209 if (!pp_tokens_equal(t1, t2))
1215 static void parse_define_directive(void)
1218 assert(obstack_object_size(&pp_obstack) == 0);
1220 if (pp_token.kind != TP_IDENTIFIER || info.at_line_begin) {
1221 errorf(&pp_token.base.source_position,
1222 "expected identifier after #define, got '%t'", &pp_token);
1225 symbol_t *const symbol = pp_token.base.symbol;
1227 pp_definition_t *new_definition
1228 = obstack_alloc(&pp_obstack, sizeof(new_definition[0]));
1229 memset(new_definition, 0, sizeof(new_definition[0]));
1230 new_definition->source_position = input.position;
1232 /* this is probably the only place where spaces are significant in the
1233 * lexer (except for the fact that they separate tokens). #define b(x)
1234 * is something else than #define b (x) */
1235 if (input.c == '(') {
1237 next_preprocessing_token();
1238 /* get next token after '(' */
1239 next_preprocessing_token();
1242 switch (pp_token.kind) {
1244 new_definition->is_variadic = true;
1245 next_preprocessing_token();
1246 if (pp_token.kind != ')') {
1247 errorf(&input.position,
1248 "'...' not at end of macro argument list");
1253 obstack_ptr_grow(&pp_obstack, pp_token.base.symbol);
1254 next_preprocessing_token();
1256 if (pp_token.kind == ',') {
1257 next_preprocessing_token();
1261 if (pp_token.kind != ')') {
1262 errorf(&pp_token.base.source_position,
1263 "expected ',' or ')' after identifier, got '%t'",
1269 next_preprocessing_token();
1270 goto finish_argument_list;
1272 errorf(&pp_token.base.source_position,
1273 "expected identifier, '...' or ')' in #define argument list, got '%t'",
1279 finish_argument_list:
1280 new_definition->has_parameters = true;
1281 new_definition->n_parameters
1282 = obstack_object_size(&pp_obstack) / sizeof(new_definition->parameters[0]);
1283 new_definition->parameters = obstack_finish(&pp_obstack);
1285 next_preprocessing_token();
1288 /* construct a new pp_definition on the obstack */
1289 assert(obstack_object_size(&pp_obstack) == 0);
1290 size_t list_len = 0;
1291 while (!info.at_line_begin) {
1292 obstack_grow(&pp_obstack, &pp_token, sizeof(pp_token));
1294 next_preprocessing_token();
1297 new_definition->list_len = list_len;
1298 new_definition->token_list = obstack_finish(&pp_obstack);
1300 pp_definition_t *old_definition = symbol->pp_definition;
1301 if (old_definition != NULL) {
1302 if (!pp_definitions_equal(old_definition, new_definition)) {
1303 warningf(WARN_OTHER, &input.position, "multiple definition of macro '%Y' (first defined %P)", symbol, &old_definition->source_position);
1305 /* reuse the old definition */
1306 obstack_free(&pp_obstack, new_definition);
1307 new_definition = old_definition;
1311 symbol->pp_definition = new_definition;
1315 if (obstack_object_size(&pp_obstack) > 0) {
1316 char *ptr = obstack_finish(&pp_obstack);
1317 obstack_free(&pp_obstack, ptr);
1322 static void parse_undef_directive(void)
1326 if (pp_token.kind != TP_IDENTIFIER) {
1327 errorf(&input.position,
1328 "expected identifier after #undef, got '%t'", &pp_token);
1333 pp_token.base.symbol->pp_definition = NULL;
1334 next_preprocessing_token();
1336 if (!info.at_line_begin) {
1337 warningf(WARN_OTHER, &input.position, "extra tokens at end of #undef directive");
1342 /** behind an #include we can have the special headername lexems.
1343 * They're only allowed behind an #include so they're not recognized
1344 * by the normal next_preprocessing_token. We handle them as a special
1346 static void parse_headername(void)
1348 const source_position_t start_position = input.position;
1349 string_t string = { NULL, 0, STRING_ENCODING_CHAR };
1350 assert(obstack_object_size(&symbol_obstack) == 0);
1352 if (info.at_line_begin) {
1353 parse_error("expected headername after #include");
1357 /* check wether we have a "... or <... headername */
1361 case '<': delimiter = '>'; goto parse_name;
1362 case '"': delimiter = '"'; goto parse_name;
1369 errorf(&pp_token.base.source_position, "header name without closing '%c'", (char)delimiter);
1373 if (input.c == delimiter) {
1375 goto finished_headername;
1377 obstack_1grow(&symbol_obstack, (char)input.c);
1383 /* we should never be here */
1387 /* TODO: do normal pp_token parsing and concatenate results */
1388 panic("pp_token concat include not implemented yet");
1391 finished_headername:
1392 string = sym_make_string(STRING_ENCODING_CHAR);
1395 pp_token.base.source_position = start_position;
1396 pp_token.kind = TP_HEADERNAME;
1397 pp_token.literal.string = string;
1400 static bool do_include(bool system_include, const char *headername)
1402 size_t headername_len = strlen(headername);
1403 if (!system_include) {
1404 /* put dirname of current input on obstack */
1405 const char *filename = input.position.input_name;
1406 const char *last_slash = strrchr(filename, '/');
1407 if (last_slash != NULL) {
1408 size_t len = last_slash - filename;
1409 obstack_grow(&symbol_obstack, filename, len + 1);
1410 obstack_grow0(&symbol_obstack, headername, headername_len);
1411 char *complete_path = obstack_finish(&symbol_obstack);
1412 headername = identify_string(complete_path);
1415 FILE *file = fopen(headername, "r");
1417 switch_input(file, headername);
1422 assert(obstack_object_size(&symbol_obstack) == 0);
1423 /* check searchpath */
1424 for (searchpath_entry_t *entry = searchpath; entry != NULL;
1425 entry = entry->next) {
1426 const char *path = entry->path;
1427 size_t len = strlen(path);
1428 obstack_grow(&symbol_obstack, path, len);
1429 if (path[len-1] != '/')
1430 obstack_1grow(&symbol_obstack, '/');
1431 obstack_grow(&symbol_obstack, headername, headername_len+1);
1433 char *complete_path = obstack_finish(&symbol_obstack);
1434 FILE *file = fopen(complete_path, "r");
1436 const char *filename = identify_string(complete_path);
1437 switch_input(file, filename);
1440 obstack_free(&symbol_obstack, complete_path);
1447 /* read till next newline character, only for parse_include_directive(),
1448 * use eat_pp_directive() in all other cases */
1449 static void skip_till_newline(void)
1451 /* skip till newline */
1462 static bool parse_include_directive(void)
1464 /* don't eat the TP_include here!
1465 * we need an alternative parsing for the next token */
1467 bool system_include = input.c == '<';
1469 string_t headername = pp_token.literal.string;
1470 if (headername.begin == NULL) {
1476 if (!info.at_line_begin) {
1477 warningf(WARN_OTHER, &pp_token.base.source_position,
1478 "extra tokens at end of #include directive");
1479 skip_till_newline();
1482 if (n_inputs > INCLUDE_LIMIT) {
1483 errorf(&pp_token.base.source_position, "#include nested too deeply");
1485 next_preprocessing_token();
1492 bool res = do_include(system_include, pp_token.literal.string.begin);
1494 errorf(&pp_token.base.source_position, "failed including '%S': %s", &pp_token.literal, strerror(errno));
1495 pop_restore_input();
1502 static pp_conditional_t *push_conditional(void)
1504 pp_conditional_t *conditional
1505 = obstack_alloc(&pp_obstack, sizeof(*conditional));
1506 memset(conditional, 0, sizeof(*conditional));
1508 conditional->parent = conditional_stack;
1509 conditional_stack = conditional;
1514 static void pop_conditional(void)
1516 assert(conditional_stack != NULL);
1517 conditional_stack = conditional_stack->parent;
1520 static void check_unclosed_conditionals(void)
1522 while (conditional_stack != NULL) {
1523 pp_conditional_t *conditional = conditional_stack;
1525 if (conditional->in_else) {
1526 errorf(&conditional->source_position, "unterminated #else");
1528 errorf(&conditional->source_position, "unterminated condition");
1534 static void parse_ifdef_ifndef_directive(void)
1536 bool is_ifndef = (pp_token.kind == TP_ifndef);
1538 next_preprocessing_token();
1542 pp_conditional_t *conditional = push_conditional();
1543 conditional->source_position = pp_token.base.source_position;
1544 conditional->skip = true;
1548 if (pp_token.kind != TP_IDENTIFIER || info.at_line_begin) {
1549 errorf(&pp_token.base.source_position,
1550 "expected identifier after #%s, got '%t'",
1551 is_ifndef ? "ifndef" : "ifdef", &pp_token);
1554 /* just take the true case in the hope to avoid further errors */
1557 /* evaluate wether we are in true or false case */
1558 condition = (bool)!pp_token.base.symbol->pp_definition == is_ifndef;
1560 next_preprocessing_token();
1562 if (!info.at_line_begin) {
1563 errorf(&pp_token.base.source_position,
1564 "extra tokens at end of #%s",
1565 is_ifndef ? "ifndef" : "ifdef");
1570 pp_conditional_t *conditional = push_conditional();
1571 conditional->source_position = pp_token.base.source_position;
1572 conditional->condition = condition;
1579 static void parse_else_directive(void)
1583 if (!info.at_line_begin) {
1585 warningf(WARN_OTHER, &pp_token.base.source_position, "extra tokens at end of #else");
1590 pp_conditional_t *conditional = conditional_stack;
1591 if (conditional == NULL) {
1592 errorf(&pp_token.base.source_position, "#else without prior #if");
1596 if (conditional->in_else) {
1597 errorf(&pp_token.base.source_position,
1598 "#else after #else (condition started %P)",
1599 &conditional->source_position);
1604 conditional->in_else = true;
1605 if (!conditional->skip) {
1606 skip_mode = conditional->condition;
1608 conditional->source_position = pp_token.base.source_position;
1611 static void parse_endif_directive(void)
1615 if (!info.at_line_begin) {
1617 warningf(WARN_OTHER, &pp_token.base.source_position, "extra tokens at end of #endif");
1622 pp_conditional_t *conditional = conditional_stack;
1623 if (conditional == NULL) {
1624 errorf(&pp_token.base.source_position, "#endif without prior #if");
1628 if (!conditional->skip) {
1634 static void parse_preprocessing_directive(void)
1638 if (info.at_line_begin) {
1639 /* empty directive */
1644 switch (pp_token.kind) {
1647 parse_ifdef_ifndef_directive();
1650 parse_else_directive();
1653 parse_endif_directive();
1660 switch (pp_token.kind) {
1662 parse_define_directive();
1665 parse_undef_directive();
1669 parse_ifdef_ifndef_directive();
1672 parse_else_directive();
1675 parse_endif_directive();
1678 parse_include_directive();
1681 if (info.at_line_begin) {
1682 /* the nop directive "#" */
1685 errorf(&pp_token.base.source_position,
1686 "invalid preprocessing directive #%t", &pp_token);
1692 assert(info.at_line_begin);
1695 static void prepend_include_path(const char *path)
1697 searchpath_entry_t *entry = OALLOCZ(&config_obstack, searchpath_entry_t);
1699 entry->next = searchpath;
1703 static void setup_include_path(void)
1705 /* built-in paths */
1706 prepend_include_path("/usr/include");
1708 /* parse environment variable */
1709 const char *cpath = getenv("CPATH");
1710 if (cpath != NULL && *cpath != '\0') {
1711 const char *begin = cpath;
1715 while (*c != '\0' && *c != ':')
1718 size_t len = c-begin;
1720 /* for gcc compatibility (Matze: I would expect that
1721 * nothing happens for an empty entry...) */
1722 prepend_include_path(".");
1724 char *string = obstack_alloc(&config_obstack, len+1);
1725 memcpy(string, begin, len);
1728 prepend_include_path(string);
1735 } while(*c != '\0');
1739 int pptest_main(int argc, char **argv);
1740 int pptest_main(int argc, char **argv)
1742 init_symbol_table();
1745 obstack_init(&config_obstack);
1746 obstack_init(&pp_obstack);
1747 obstack_init(&input_obstack);
1748 strset_init(&stringset);
1750 setup_include_path();
1752 /* simplistic commandline parser */
1753 const char *filename = NULL;
1754 const char *output = NULL;
1755 for (int i = 1; i < argc; ++i) {
1756 const char *opt = argv[i];
1757 if (streq(opt, "-I")) {
1758 prepend_include_path(argv[++i]);
1760 } else if (streq(opt, "-E")) {
1762 } else if (streq(opt, "-o")) {
1765 } else if (opt[0] == '-') {
1766 fprintf(stderr, "Unknown option '%s'\n", opt);
1768 if (filename != NULL)
1769 fprintf(stderr, "Multiple inputs not supported\n");
1773 if (filename == NULL) {
1774 fprintf(stderr, "No input specified\n");
1778 if (output == NULL) {
1781 out = fopen(output, "w");
1783 fprintf(stderr, "Couldn't open output '%s'\n", output);
1788 /* just here for gcc compatibility */
1789 fprintf(out, "# 1 \"%s\"\n", filename);
1790 fprintf(out, "# 1 \"<built-in>\"\n");
1791 fprintf(out, "# 1 \"<command-line>\"\n");
1793 FILE *file = fopen(filename, "r");
1795 fprintf(stderr, "Couldn't open input '%s'\n", filename);
1798 switch_input(file, filename);
1801 if (pp_token.kind == '#' && info.at_line_begin) {
1802 parse_preprocessing_directive();
1804 } else if (pp_token.kind == TP_EOF) {
1805 goto end_of_main_loop;
1806 } else if (pp_token.kind == TP_IDENTIFIER) {
1807 symbol_t *const symbol = pp_token.base.symbol;
1808 pp_definition_t *const pp_definition = symbol->pp_definition;
1809 if (pp_definition != NULL && !pp_definition->is_expanding) {
1810 expansion_pos = pp_token.base.source_position;
1811 if (pp_definition->has_parameters) {
1812 source_position_t position = pp_token.base.source_position;
1813 add_token_info_t old_info = info;
1814 next_preprocessing_token();
1815 add_token_info_t new_info = info;
1817 /* no opening brace -> no expansion */
1818 if (pp_token.kind == '(') {
1821 /* parse arguments (TODO) */
1822 while (pp_token.kind != TP_EOF && pp_token.kind != ')')
1823 next_preprocessing_token();
1825 token_t next_token = pp_token;
1826 /* restore identifier token */
1827 pp_token.kind = TP_IDENTIFIER;
1828 pp_token.base.symbol = symbol;
1829 pp_token.base.source_position = position;
1834 pp_token = next_token;
1839 pp_definition->expand_pos = 0;
1840 pp_definition->is_expanding = true;
1841 current_expansion = pp_definition;
1848 next_preprocessing_token();
1853 check_unclosed_conditionals();
1858 obstack_free(&input_obstack, NULL);
1859 obstack_free(&pp_obstack, NULL);
1860 obstack_free(&config_obstack, NULL);
1862 strset_destroy(&stringset);
1865 exit_symbol_table();