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 error_on_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 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 symbol_t *symbol_colongreater;
113 static symbol_t *symbol_lesscolon;
114 static symbol_t *symbol_lesspercent;
115 static symbol_t *symbol_percentcolon;
116 static symbol_t *symbol_percentcolonpercentcolon;
117 static symbol_t *symbol_percentgreater;
119 static void init_symbols(void)
121 symbol_colongreater = symbol_table_insert(":>");
122 symbol_lesscolon = symbol_table_insert("<:");
123 symbol_lesspercent = symbol_table_insert("<%");
124 symbol_percentcolon = symbol_table_insert("%:");
125 symbol_percentcolonpercentcolon = symbol_table_insert("%:%:");
126 symbol_percentgreater = symbol_table_insert("%>");
129 static void switch_input(FILE *file, const char *filename)
132 input.input = input_from_stream(file, NULL);
135 input.output_line = 0;
136 input.position.input_name = filename;
137 input.position.lineno = 1;
139 /* indicate that we're at a new input */
140 print_line_directive(&input.position, input_stack != NULL ? "1" : NULL);
142 /* place a virtual '\n' so we realize we're at line begin */
143 input.position.lineno = 0;
145 next_preprocessing_token();
148 static void close_input(void)
150 input_free(input.input);
151 assert(input.file != NULL);
161 static void push_input(void)
163 pp_input_t *saved_input
164 = obstack_alloc(&input_obstack, sizeof(*saved_input));
166 memcpy(saved_input, &input, sizeof(*saved_input));
168 /* adjust buffer positions */
169 if (input.bufpos != NULL)
170 saved_input->bufpos = saved_input->buf + (input.bufpos - input.buf);
171 if (input.bufend != NULL)
172 saved_input->bufend = saved_input->buf + (input.bufend - input.buf);
174 saved_input->parent = input_stack;
175 input_stack = saved_input;
179 static void pop_restore_input(void)
181 assert(n_inputs > 0);
182 assert(input_stack != NULL);
184 pp_input_t *saved_input = input_stack;
186 memcpy(&input, saved_input, sizeof(input));
189 /* adjust buffer positions */
190 if (saved_input->bufpos != NULL)
191 input.bufpos = input.buf + (saved_input->bufpos - saved_input->buf);
192 if (saved_input->bufend != NULL)
193 input.bufend = input.buf + (saved_input->bufend - saved_input->buf);
195 input_stack = saved_input->parent;
196 obstack_free(&input_obstack, saved_input);
201 * Prints a parse error message at the current token.
203 * @param msg the error message
205 static void parse_error(const char *msg)
207 errorf(&pp_token.base.source_position, "%s", msg);
210 static inline void next_real_char(void)
212 assert(input.bufpos <= input.bufend);
213 if (input.bufpos >= input.bufend) {
214 size_t const n = decode(input.input, input.buf + MAX_PUTBACK, lengthof(input.buf) - MAX_PUTBACK);
219 input.bufpos = input.buf + MAX_PUTBACK;
220 input.bufend = input.bufpos + n;
222 input.c = *input.bufpos++;
223 ++input.position.colno;
227 * Put a character back into the buffer.
229 * @param pc the character to put back
231 static inline void put_back(utf32 const pc)
233 assert(input.bufpos > input.buf);
234 *(--input.bufpos - input.buf + input.buf) = (char) pc;
235 --input.position.colno;
241 if (input.c == '\n') { \
245 info.whitespace = 0; \
246 ++input.position.lineno; \
247 input.position.colno = 1; \
249 newline // Let it look like an ordinary case label.
251 #define eat(c_type) (assert(input.c == c_type), next_char())
253 static void maybe_concat_lines(void)
270 * Set c to the next input character, ie.
271 * after expanding trigraphs.
273 static inline void next_char(void)
277 /* filter trigraphs and concatenated lines */
278 if (UNLIKELY(input.c == '\\')) {
279 maybe_concat_lines();
280 goto end_of_next_char;
283 if (LIKELY(input.c != '?'))
284 goto end_of_next_char;
287 if (LIKELY(input.c != '?')) {
290 goto end_of_next_char;
295 case '=': input.c = '#'; break;
296 case '(': input.c = '['; break;
297 case '/': input.c = '\\'; maybe_concat_lines(); break;
298 case ')': input.c = ']'; break;
299 case '\'': input.c = '^'; break;
300 case '<': input.c = '{'; break;
301 case '!': input.c = '|'; break;
302 case '>': input.c = '}'; break;
303 case '-': input.c = '~'; break;
313 printf("nchar '%c'\n", input.c);
320 * Returns true if the given char is a octal digit.
322 * @param char the character to check
324 static inline bool is_octal_digit(int chr)
342 * Returns the value of a digit.
343 * The only portable way to do it ...
345 static int digit_value(int digit)
371 panic("wrong character given");
376 * Parses an octal character sequence.
378 * @param first_digit the already read first digit
380 static utf32 parse_octal_sequence(const utf32 first_digit)
382 assert(is_octal_digit(first_digit));
383 utf32 value = digit_value(first_digit);
384 if (!is_octal_digit(input.c)) return value;
385 value = 8 * value + digit_value(input.c);
387 if (!is_octal_digit(input.c)) return value;
388 value = 8 * value + digit_value(input.c);
395 * Parses a hex character sequence.
397 static utf32 parse_hex_sequence(void)
400 while (isxdigit(input.c)) {
401 value = 16 * value + digit_value(input.c);
408 * Parse an escape sequence.
410 static utf32 parse_escape_sequence(void)
414 utf32 const ec = input.c;
418 case '"': return '"';
419 case '\'': return '\'';
420 case '\\': return '\\';
421 case '?': return '\?';
422 case 'a': return '\a';
423 case 'b': return '\b';
424 case 'f': return '\f';
425 case 'n': return '\n';
426 case 'r': return '\r';
427 case 't': return '\t';
428 case 'v': return '\v';
430 return parse_hex_sequence();
439 return parse_octal_sequence(ec);
441 parse_error("reached end of file while parsing escape sequence");
443 /* \E is not documented, but handled, by GCC. It is acceptable according
444 * to §6.11.4, whereas \e is not. */
448 return 27; /* hopefully 27 is ALWAYS the code for ESCAPE */
452 parse_error("universal character parsing not implemented yet");
457 /* §6.4.4.4:8 footnote 64 */
458 parse_error("unknown escape sequence");
462 static const char *identify_string(char *string)
464 const char *result = strset_insert(&stringset, string);
465 if (result != string) {
466 obstack_free(&symbol_obstack, string);
471 static string_t sym_make_string(string_encoding_t const enc)
473 obstack_1grow(&symbol_obstack, '\0');
474 size_t const len = obstack_object_size(&symbol_obstack) - 1;
475 char *const string = obstack_finish(&symbol_obstack);
476 char const *const result = identify_string(string);
477 return (string_t){ result, len, enc };
480 static void parse_string(utf32 const delimiter, token_kind_t const kind,
481 string_encoding_t const enc,
482 char const *const context)
484 const unsigned start_linenr = input.position.lineno;
491 if (resolve_escape_sequences) {
492 utf32 const tc = parse_escape_sequence();
493 if (enc == STRING_ENCODING_CHAR) {
495 warningf(WARN_OTHER, &pp_token.base.source_position, "escape sequence out of range");
497 obstack_1grow(&symbol_obstack, tc);
499 obstack_grow_utf8(&symbol_obstack, tc);
502 obstack_1grow(&symbol_obstack, (char)input.c);
504 obstack_1grow(&symbol_obstack, (char)input.c);
511 errorf(&pp_token.base.source_position, "newline while parsing %s", context);
515 source_position_t source_position;
516 source_position.input_name = pp_token.base.source_position.input_name;
517 source_position.lineno = start_linenr;
518 errorf(&source_position, "EOF while parsing %s", context);
523 if (input.c == delimiter) {
527 obstack_grow_utf8(&symbol_obstack, input.c);
535 pp_token.kind = kind;
536 pp_token.literal.string = sym_make_string(enc);
539 static void parse_string_literal(string_encoding_t const enc)
541 parse_string('"', T_STRING_LITERAL, enc, "string literal");
544 static void parse_character_constant(string_encoding_t const enc)
546 parse_string('\'', T_CHARACTER_CONSTANT, enc, "character constant");
547 if (pp_token.literal.string.size == 0) {
548 parse_error("empty character constant");
552 #define SYMBOL_CASES_WITHOUT_E_P \
603 #define SYMBOL_CASES \
604 SYMBOL_CASES_WITHOUT_E_P: \
610 #define DIGIT_CASES \
622 static inline void set_punctuator(token_kind_t const kind)
624 pp_token.kind = kind;
625 pp_token.base.symbol = token_symbols[kind];
628 static inline void set_digraph(token_kind_t const kind, symbol_t *const symbol)
630 pp_token.kind = kind;
631 pp_token.base.symbol = symbol;
635 * returns next final token from a preprocessor macro expansion
637 static void expand_next(void)
639 assert(current_expansion != NULL);
641 pp_definition_t *definition = current_expansion;
644 if (definition->list_len == 0
645 || definition->expand_pos >= definition->list_len) {
646 /* we're finished with the current macro, move up 1 level in the
648 pp_definition_t *parent = definition->parent_expansion;
649 definition->parent_expansion = NULL;
650 definition->is_expanding = false;
652 /* it was the outermost expansion, parse normal pptoken */
653 if (parent == NULL) {
654 current_expansion = NULL;
655 next_preprocessing_token();
659 current_expansion = definition;
662 pp_token = definition->token_list[definition->expand_pos];
663 pp_token.base.source_position = expansion_pos;
664 ++definition->expand_pos;
666 if (pp_token.kind != T_IDENTIFIER)
669 /* if it was an identifier then we might need to expand again */
670 pp_definition_t *const symbol_definition = pp_token.base.symbol->pp_definition;
671 if (symbol_definition != NULL && !symbol_definition->is_expanding) {
672 symbol_definition->parent_expansion = definition;
673 symbol_definition->expand_pos = 0;
674 symbol_definition->is_expanding = true;
675 definition = symbol_definition;
676 current_expansion = definition;
681 static void skip_line_comment(void)
699 static void skip_multiline_comment(void)
701 unsigned start_linenr = input.position.lineno;
706 if (input.c == '*') {
707 /* TODO: nested comment, warn here */
712 if (input.c == '/') {
713 if (input.position.lineno != input.output_line)
714 info.whitespace = input.position.colno;
724 source_position_t source_position;
725 source_position.input_name = pp_token.base.source_position.input_name;
726 source_position.lineno = start_linenr;
727 errorf(&source_position, "at end of file while looking for comment end");
738 static void skip_whitespace(void)
748 info.at_line_begin = true;
753 if (input.c == '/') {
757 } else if (input.c == '*') {
759 skip_multiline_comment();
772 static inline void eat_pp(pp_token_kind_t const kind)
774 assert(pp_token.base.symbol->pp_ID == kind);
776 next_preprocessing_token();
779 static inline void eat_token(token_kind_t const kind)
781 assert(pp_token.kind == kind);
783 next_preprocessing_token();
786 static void parse_symbol(void)
788 obstack_1grow(&symbol_obstack, (char) input.c);
795 obstack_1grow(&symbol_obstack, (char) input.c);
805 obstack_1grow(&symbol_obstack, '\0');
806 char *string = obstack_finish(&symbol_obstack);
808 /* might be a wide string or character constant ( L"string"/L'c' ) */
809 if (input.c == '"' && string[0] == 'L' && string[1] == '\0') {
810 obstack_free(&symbol_obstack, string);
811 parse_string_literal(STRING_ENCODING_WIDE);
813 } else if (input.c == '\'' && string[0] == 'L' && string[1] == '\0') {
814 obstack_free(&symbol_obstack, string);
815 parse_character_constant(STRING_ENCODING_WIDE);
819 symbol_t *symbol = symbol_table_insert(string);
821 pp_token.kind = symbol->ID;
822 pp_token.base.symbol = symbol;
824 /* we can free the memory from symbol obstack if we already had an entry in
825 * the symbol table */
826 if (symbol->string != string) {
827 obstack_free(&symbol_obstack, string);
831 static void parse_number(void)
833 obstack_1grow(&symbol_obstack, (char) input.c);
840 case SYMBOL_CASES_WITHOUT_E_P:
841 obstack_1grow(&symbol_obstack, (char) input.c);
849 obstack_1grow(&symbol_obstack, (char) input.c);
851 if (input.c == '+' || input.c == '-') {
852 obstack_1grow(&symbol_obstack, (char) input.c);
863 pp_token.kind = T_NUMBER;
864 pp_token.literal.string = sym_make_string(STRING_ENCODING_CHAR);
867 #define MAYBE_PROLOG \
871 #define MAYBE(ch, kind) \
874 set_punctuator(kind); \
877 #define MAYBE_DIGRAPH(ch, kind, symbol) \
880 set_digraph(kind, symbol); \
883 #define ELSE_CODE(code) \
889 #define ELSE(kind) ELSE_CODE(set_punctuator(kind);)
891 static void next_preprocessing_token(void)
893 if (current_expansion != NULL) {
898 info.at_line_begin = false;
899 info.had_whitespace = false;
901 pp_token.base.source_position = input.position;
902 pp_token.base.symbol = NULL;
908 info.had_whitespace = true;
913 info.at_line_begin = true;
914 info.had_whitespace = true;
926 parse_string_literal(STRING_ENCODING_CHAR);
930 parse_character_constant(STRING_ENCODING_CHAR);
952 MAYBE('.', T_DOTDOTDOT)
962 MAYBE('=', T_ANDEQUAL)
966 MAYBE('=', T_ASTERISKEQUAL)
970 MAYBE('+', T_PLUSPLUS)
971 MAYBE('=', T_PLUSEQUAL)
975 MAYBE('>', T_MINUSGREATER)
976 MAYBE('-', T_MINUSMINUS)
977 MAYBE('=', T_MINUSEQUAL)
981 MAYBE('=', T_EXCLAMATIONMARKEQUAL)
985 MAYBE('=', T_SLASHEQUAL)
988 info.had_whitespace = true;
989 skip_multiline_comment();
993 info.had_whitespace = true;
999 MAYBE_DIGRAPH('>', '}', symbol_percentgreater)
1000 MAYBE('=', T_PERCENTEQUAL)
1005 MAYBE_DIGRAPH(':', T_HASHHASH, symbol_percentcolonpercentcolon)
1009 goto digraph_percentcolon;
1012 digraph_percentcolon:
1013 set_digraph('#', symbol_percentcolon);
1018 MAYBE_DIGRAPH(':', '[', symbol_lesscolon)
1019 MAYBE_DIGRAPH('%', '{', symbol_lesspercent)
1020 MAYBE('=', T_LESSEQUAL)
1023 MAYBE('=', T_LESSLESSEQUAL)
1028 MAYBE('=', T_GREATEREQUAL)
1031 MAYBE('=', T_GREATERGREATEREQUAL)
1032 ELSE(T_GREATERGREATER)
1036 MAYBE('=', T_CARETEQUAL)
1040 MAYBE('=', T_PIPEEQUAL)
1041 MAYBE('|', T_PIPEPIPE)
1045 MAYBE_DIGRAPH('>', ']', symbol_colongreater)
1047 if (c_mode & _CXX) {
1049 set_punctuator(T_COLONCOLON);
1056 MAYBE('=', T_EQUALEQUAL)
1060 MAYBE('#', T_HASHHASH)
1073 set_punctuator(input.c);
1078 if (input_stack != NULL) {
1080 pop_restore_input();
1082 print_line_directive(&input.position, "2");
1085 pp_token.base.source_position.lineno++;
1086 info.at_line_begin = true;
1087 set_punctuator(T_EOF);
1092 if (error_on_unknown_chars) {
1093 errorf(&pp_token.base.source_position,
1094 "unknown character '%lc' found\n", input.c);
1098 assert(obstack_object_size(&symbol_obstack) == 0);
1099 obstack_grow_utf8(&symbol_obstack, input.c);
1100 obstack_1grow(&symbol_obstack, '\0');
1101 char *const string = obstack_finish(&symbol_obstack);
1102 symbol_t *const symbol = symbol_table_insert(string);
1103 if (symbol->string != string)
1104 obstack_free(&symbol_obstack, string);
1106 pp_token.kind = T_UNKNOWN_CHAR;
1107 pp_token.base.symbol = symbol;
1114 static void print_quoted_string(const char *const string)
1117 for (const char *c = string; *c != 0; ++c) {
1119 case '"': fputs("\\\"", out); break;
1120 case '\\': fputs("\\\\", out); break;
1121 case '\a': fputs("\\a", out); break;
1122 case '\b': fputs("\\b", out); break;
1123 case '\f': fputs("\\f", out); break;
1124 case '\n': fputs("\\n", out); break;
1125 case '\r': fputs("\\r", out); break;
1126 case '\t': fputs("\\t", out); break;
1127 case '\v': fputs("\\v", out); break;
1128 case '\?': fputs("\\?", out); break;
1131 fprintf(out, "\\%03o", (unsigned)*c);
1141 static void print_line_directive(const source_position_t *pos, const char *add)
1143 fprintf(out, "# %u ", pos->lineno);
1144 print_quoted_string(pos->input_name);
1150 printed_input_name = pos->input_name;
1151 input.output_line = pos->lineno-1;
1154 static bool emit_newlines(void)
1156 unsigned delta = pp_token.base.source_position.lineno - input.output_line;
1162 print_line_directive(&pp_token.base.source_position, NULL);
1165 for (unsigned i = 0; i < delta; ++i) {
1169 input.output_line = pp_token.base.source_position.lineno;
1171 for (unsigned i = 0; i < info.whitespace; ++i)
1177 static void emit_pp_token(void)
1182 if (!emit_newlines() &&
1183 (info.had_whitespace || tokens_would_paste(last_token, pp_token.kind)))
1186 switch (pp_token.kind) {
1188 fputs(pp_token.literal.string.begin, out);
1191 case T_STRING_LITERAL:
1192 fputs(get_string_encoding_prefix(pp_token.literal.string.encoding), out);
1194 fputs(pp_token.literal.string.begin, out);
1198 case T_CHARACTER_CONSTANT:
1199 fputs(get_string_encoding_prefix(pp_token.literal.string.encoding), out);
1201 fputs(pp_token.literal.string.begin, out);
1206 fputs(pp_token.base.symbol->string, out);
1209 last_token = pp_token.kind;
1212 static void eat_pp_directive(void)
1214 while (!info.at_line_begin) {
1215 next_preprocessing_token();
1219 static bool strings_equal(const string_t *string1, const string_t *string2)
1221 size_t size = string1->size;
1222 if (size != string2->size)
1225 const char *c1 = string1->begin;
1226 const char *c2 = string2->begin;
1227 for (size_t i = 0; i < size; ++i, ++c1, ++c2) {
1234 static bool pp_tokens_equal(const token_t *token1, const token_t *token2)
1236 if (token1->kind != token2->kind)
1239 switch (token1->kind) {
1241 case T_CHARACTER_CONSTANT:
1242 case T_STRING_LITERAL:
1243 return strings_equal(&token1->literal.string, &token2->literal.string);
1246 return token1->base.symbol == token2->base.symbol;
1250 static bool pp_definitions_equal(const pp_definition_t *definition1,
1251 const pp_definition_t *definition2)
1253 if (definition1->list_len != definition2->list_len)
1256 size_t len = definition1->list_len;
1257 const token_t *t1 = definition1->token_list;
1258 const token_t *t2 = definition2->token_list;
1259 for (size_t i = 0; i < len; ++i, ++t1, ++t2) {
1260 if (!pp_tokens_equal(t1, t2))
1266 static void parse_define_directive(void)
1274 assert(obstack_object_size(&pp_obstack) == 0);
1276 if (pp_token.kind != T_IDENTIFIER || info.at_line_begin) {
1277 errorf(&pp_token.base.source_position,
1278 "expected identifier after #define, got %K", &pp_token);
1281 symbol_t *const symbol = pp_token.base.symbol;
1283 pp_definition_t *new_definition
1284 = obstack_alloc(&pp_obstack, sizeof(new_definition[0]));
1285 memset(new_definition, 0, sizeof(new_definition[0]));
1286 new_definition->source_position = input.position;
1288 /* this is probably the only place where spaces are significant in the
1289 * lexer (except for the fact that they separate tokens). #define b(x)
1290 * is something else than #define b (x) */
1291 if (input.c == '(') {
1292 eat_token(T_IDENTIFIER);
1296 switch (pp_token.kind) {
1298 new_definition->is_variadic = true;
1299 eat_token(T_DOTDOTDOT);
1300 if (pp_token.kind != ')') {
1301 errorf(&input.position,
1302 "'...' not at end of macro argument list");
1308 obstack_ptr_grow(&pp_obstack, pp_token.base.symbol);
1309 eat_token(T_IDENTIFIER);
1311 if (pp_token.kind == ',') {
1316 if (pp_token.kind != ')') {
1317 errorf(&pp_token.base.source_position,
1318 "expected ',' or ')' after identifier, got %K",
1326 goto finish_argument_list;
1329 errorf(&pp_token.base.source_position,
1330 "expected identifier, '...' or ')' in #define argument list, got %K",
1336 finish_argument_list:
1337 new_definition->has_parameters = true;
1338 new_definition->n_parameters
1339 = obstack_object_size(&pp_obstack) / sizeof(new_definition->parameters[0]);
1340 new_definition->parameters = obstack_finish(&pp_obstack);
1342 eat_token(T_IDENTIFIER);
1345 /* construct a new pp_definition on the obstack */
1346 assert(obstack_object_size(&pp_obstack) == 0);
1347 size_t list_len = 0;
1348 while (!info.at_line_begin) {
1349 obstack_grow(&pp_obstack, &pp_token, sizeof(pp_token));
1351 next_preprocessing_token();
1354 new_definition->list_len = list_len;
1355 new_definition->token_list = obstack_finish(&pp_obstack);
1357 pp_definition_t *old_definition = symbol->pp_definition;
1358 if (old_definition != NULL) {
1359 if (!pp_definitions_equal(old_definition, new_definition)) {
1360 warningf(WARN_OTHER, &input.position, "multiple definition of macro '%Y' (first defined %P)", symbol, &old_definition->source_position);
1362 /* reuse the old definition */
1363 obstack_free(&pp_obstack, new_definition);
1364 new_definition = old_definition;
1368 symbol->pp_definition = new_definition;
1372 if (obstack_object_size(&pp_obstack) > 0) {
1373 char *ptr = obstack_finish(&pp_obstack);
1374 obstack_free(&pp_obstack, ptr);
1379 static void parse_undef_directive(void)
1387 if (pp_token.kind != T_IDENTIFIER) {
1388 errorf(&input.position,
1389 "expected identifier after #undef, got %K", &pp_token);
1394 pp_token.base.symbol->pp_definition = NULL;
1395 eat_token(T_IDENTIFIER);
1397 if (!info.at_line_begin) {
1398 warningf(WARN_OTHER, &input.position, "extra tokens at end of #undef directive");
1403 /** behind an #include we can have the special headername lexems.
1404 * They're only allowed behind an #include so they're not recognized
1405 * by the normal next_preprocessing_token. We handle them as a special
1407 static void parse_headername(void)
1409 const source_position_t start_position = input.position;
1410 string_t string = { NULL, 0, STRING_ENCODING_CHAR };
1411 assert(obstack_object_size(&symbol_obstack) == 0);
1413 if (info.at_line_begin) {
1414 parse_error("expected headername after #include");
1418 /* check wether we have a "... or <... headername */
1422 case '<': delimiter = '>'; goto parse_name;
1423 case '"': delimiter = '"'; goto parse_name;
1430 errorf(&pp_token.base.source_position, "header name without closing '%c'", (char)delimiter);
1434 if (input.c == delimiter) {
1436 goto finished_headername;
1438 obstack_1grow(&symbol_obstack, (char)input.c);
1444 /* we should never be here */
1448 /* TODO: do normal pp_token parsing and concatenate results */
1449 panic("pp_token concat include not implemented yet");
1452 finished_headername:
1453 string = sym_make_string(STRING_ENCODING_CHAR);
1456 pp_token.base.source_position = start_position;
1457 pp_token.kind = T_HEADERNAME;
1458 pp_token.literal.string = string;
1461 static bool do_include(bool system_include, const char *headername)
1463 size_t headername_len = strlen(headername);
1464 if (!system_include) {
1465 /* put dirname of current input on obstack */
1466 const char *filename = input.position.input_name;
1467 const char *last_slash = strrchr(filename, '/');
1468 if (last_slash != NULL) {
1469 size_t len = last_slash - filename;
1470 obstack_grow(&symbol_obstack, filename, len + 1);
1471 obstack_grow0(&symbol_obstack, headername, headername_len);
1472 char *complete_path = obstack_finish(&symbol_obstack);
1473 headername = identify_string(complete_path);
1476 FILE *file = fopen(headername, "r");
1478 switch_input(file, headername);
1483 assert(obstack_object_size(&symbol_obstack) == 0);
1484 /* check searchpath */
1485 for (searchpath_entry_t *entry = searchpath; entry != NULL;
1486 entry = entry->next) {
1487 const char *path = entry->path;
1488 size_t len = strlen(path);
1489 obstack_grow(&symbol_obstack, path, len);
1490 if (path[len-1] != '/')
1491 obstack_1grow(&symbol_obstack, '/');
1492 obstack_grow(&symbol_obstack, headername, headername_len+1);
1494 char *complete_path = obstack_finish(&symbol_obstack);
1495 FILE *file = fopen(complete_path, "r");
1497 const char *filename = identify_string(complete_path);
1498 switch_input(file, filename);
1501 obstack_free(&symbol_obstack, complete_path);
1508 /* read till next newline character, only for parse_include_directive(),
1509 * use eat_pp_directive() in all other cases */
1510 static void skip_till_newline(void)
1512 /* skip till newline */
1523 static void parse_include_directive(void)
1530 /* don't eat the TP_include here!
1531 * we need an alternative parsing for the next token */
1533 bool system_include = input.c == '<';
1535 string_t headername = pp_token.literal.string;
1536 if (headername.begin == NULL) {
1542 if (!info.at_line_begin) {
1543 warningf(WARN_OTHER, &pp_token.base.source_position,
1544 "extra tokens at end of #include directive");
1545 skip_till_newline();
1548 if (n_inputs > INCLUDE_LIMIT) {
1549 errorf(&pp_token.base.source_position, "#include nested too deeply");
1551 next_preprocessing_token();
1558 bool res = do_include(system_include, pp_token.literal.string.begin);
1560 errorf(&pp_token.base.source_position, "failed including '%S': %s", &pp_token.literal, strerror(errno));
1561 pop_restore_input();
1565 static pp_conditional_t *push_conditional(void)
1567 pp_conditional_t *conditional
1568 = obstack_alloc(&pp_obstack, sizeof(*conditional));
1569 memset(conditional, 0, sizeof(*conditional));
1571 conditional->parent = conditional_stack;
1572 conditional_stack = conditional;
1577 static void pop_conditional(void)
1579 assert(conditional_stack != NULL);
1580 conditional_stack = conditional_stack->parent;
1583 static void check_unclosed_conditionals(void)
1585 while (conditional_stack != NULL) {
1586 pp_conditional_t *conditional = conditional_stack;
1588 if (conditional->in_else) {
1589 errorf(&conditional->source_position, "unterminated #else");
1591 errorf(&conditional->source_position, "unterminated condition");
1597 static void parse_ifdef_ifndef_directive(bool const is_ifdef)
1600 eat_pp(is_ifdef ? TP_ifdef : TP_ifndef);
1604 pp_conditional_t *conditional = push_conditional();
1605 conditional->source_position = pp_token.base.source_position;
1606 conditional->skip = true;
1610 if (pp_token.kind != T_IDENTIFIER || info.at_line_begin) {
1611 errorf(&pp_token.base.source_position,
1612 "expected identifier after #%s, got %K",
1613 is_ifdef ? "ifdef" : "ifndef", &pp_token);
1616 /* just take the true case in the hope to avoid further errors */
1619 /* evaluate wether we are in true or false case */
1620 condition = (bool)pp_token.base.symbol->pp_definition == is_ifdef;
1621 eat_token(T_IDENTIFIER);
1623 if (!info.at_line_begin) {
1624 errorf(&pp_token.base.source_position,
1625 "extra tokens at end of #%s",
1626 is_ifdef ? "ifdef" : "ifndef");
1631 pp_conditional_t *conditional = push_conditional();
1632 conditional->source_position = pp_token.base.source_position;
1633 conditional->condition = condition;
1640 static void parse_else_directive(void)
1644 if (!info.at_line_begin) {
1646 warningf(WARN_OTHER, &pp_token.base.source_position, "extra tokens at end of #else");
1651 pp_conditional_t *conditional = conditional_stack;
1652 if (conditional == NULL) {
1653 errorf(&pp_token.base.source_position, "#else without prior #if");
1657 if (conditional->in_else) {
1658 errorf(&pp_token.base.source_position,
1659 "#else after #else (condition started %P)",
1660 &conditional->source_position);
1665 conditional->in_else = true;
1666 if (!conditional->skip) {
1667 skip_mode = conditional->condition;
1669 conditional->source_position = pp_token.base.source_position;
1672 static void parse_endif_directive(void)
1676 if (!info.at_line_begin) {
1678 warningf(WARN_OTHER, &pp_token.base.source_position, "extra tokens at end of #endif");
1683 pp_conditional_t *conditional = conditional_stack;
1684 if (conditional == NULL) {
1685 errorf(&pp_token.base.source_position, "#endif without prior #if");
1689 if (!conditional->skip) {
1695 static void parse_preprocessing_directive(void)
1699 if (info.at_line_begin) {
1700 /* empty directive */
1704 if (pp_token.base.symbol) {
1705 switch (pp_token.base.symbol->pp_ID) {
1706 case TP_define: parse_define_directive(); break;
1707 case TP_else: parse_else_directive(); break;
1708 case TP_endif: parse_endif_directive(); break;
1709 case TP_ifdef: parse_ifdef_ifndef_directive(true); break;
1710 case TP_ifndef: parse_ifdef_ifndef_directive(false); break;
1711 case TP_include: parse_include_directive(); break;
1712 case TP_undef: parse_undef_directive(); break;
1718 errorf(&pp_token.base.source_position, "invalid preprocessing directive #%K", &pp_token);
1723 assert(info.at_line_begin);
1726 static void prepend_include_path(const char *path)
1728 searchpath_entry_t *entry = OALLOCZ(&config_obstack, searchpath_entry_t);
1730 entry->next = searchpath;
1734 static void setup_include_path(void)
1736 /* built-in paths */
1737 prepend_include_path("/usr/include");
1739 /* parse environment variable */
1740 const char *cpath = getenv("CPATH");
1741 if (cpath != NULL && *cpath != '\0') {
1742 const char *begin = cpath;
1746 while (*c != '\0' && *c != ':')
1749 size_t len = c-begin;
1751 /* for gcc compatibility (Matze: I would expect that
1752 * nothing happens for an empty entry...) */
1753 prepend_include_path(".");
1755 char *string = obstack_alloc(&config_obstack, len+1);
1756 memcpy(string, begin, len);
1759 prepend_include_path(string);
1766 } while(*c != '\0');
1770 int pptest_main(int argc, char **argv);
1771 int pptest_main(int argc, char **argv)
1773 init_symbol_table();
1777 obstack_init(&config_obstack);
1778 obstack_init(&pp_obstack);
1779 obstack_init(&input_obstack);
1780 strset_init(&stringset);
1782 error_on_unknown_chars = false;
1784 setup_include_path();
1786 /* simplistic commandline parser */
1787 const char *filename = NULL;
1788 const char *output = NULL;
1789 for (int i = 1; i < argc; ++i) {
1790 const char *opt = argv[i];
1791 if (streq(opt, "-I")) {
1792 prepend_include_path(argv[++i]);
1794 } else if (streq(opt, "-E")) {
1796 } else if (streq(opt, "-o")) {
1799 } else if (opt[0] == '-') {
1800 fprintf(stderr, "Unknown option '%s'\n", opt);
1802 if (filename != NULL)
1803 fprintf(stderr, "Multiple inputs not supported\n");
1807 if (filename == NULL) {
1808 fprintf(stderr, "No input specified\n");
1812 if (output == NULL) {
1815 out = fopen(output, "w");
1817 fprintf(stderr, "Couldn't open output '%s'\n", output);
1822 /* just here for gcc compatibility */
1823 fprintf(out, "# 1 \"%s\"\n", filename);
1824 fprintf(out, "# 1 \"<built-in>\"\n");
1825 fprintf(out, "# 1 \"<command-line>\"\n");
1827 FILE *file = fopen(filename, "r");
1829 fprintf(stderr, "Couldn't open input '%s'\n", filename);
1832 switch_input(file, filename);
1835 if (pp_token.kind == '#' && info.at_line_begin) {
1836 parse_preprocessing_directive();
1838 } else if (pp_token.kind == T_EOF) {
1839 goto end_of_main_loop;
1840 } else if (pp_token.kind == T_IDENTIFIER) {
1841 symbol_t *const symbol = pp_token.base.symbol;
1842 pp_definition_t *const pp_definition = symbol->pp_definition;
1843 if (pp_definition != NULL && !pp_definition->is_expanding) {
1844 expansion_pos = pp_token.base.source_position;
1845 if (pp_definition->has_parameters) {
1846 source_position_t position = pp_token.base.source_position;
1847 add_token_info_t old_info = info;
1848 eat_token(T_IDENTIFIER);
1849 add_token_info_t new_info = info;
1851 /* no opening brace -> no expansion */
1852 if (pp_token.kind == '(') {
1855 /* parse arguments (TODO) */
1856 while (pp_token.kind != T_EOF && pp_token.kind != ')')
1857 next_preprocessing_token();
1859 token_t next_token = pp_token;
1860 /* restore identifier token */
1861 pp_token.kind = T_IDENTIFIER;
1862 pp_token.base.symbol = symbol;
1863 pp_token.base.source_position = position;
1868 pp_token = next_token;
1873 pp_definition->expand_pos = 0;
1874 pp_definition->is_expanding = true;
1875 current_expansion = pp_definition;
1882 next_preprocessing_token();
1887 check_unclosed_conditionals();
1892 obstack_free(&input_obstack, NULL);
1893 obstack_free(&pp_obstack, NULL);
1894 obstack_free(&config_obstack, NULL);
1896 strset_destroy(&stringset);
1899 exit_symbol_table();