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)
1049 MAYBE('=', T_EQUALEQUAL)
1053 MAYBE('#', T_HASHHASH)
1066 set_punctuator(input.c);
1071 if (input_stack != NULL) {
1073 pop_restore_input();
1075 print_line_directive(&input.position, "2");
1078 pp_token.base.source_position.lineno++;
1079 info.at_line_begin = true;
1080 set_punctuator(T_EOF);
1085 if (error_on_unknown_chars) {
1086 errorf(&pp_token.base.source_position,
1087 "unknown character '%lc' found\n", input.c);
1091 assert(obstack_object_size(&symbol_obstack) == 0);
1092 obstack_grow_utf8(&symbol_obstack, input.c);
1093 obstack_1grow(&symbol_obstack, '\0');
1094 char *const string = obstack_finish(&symbol_obstack);
1095 symbol_t *const symbol = symbol_table_insert(string);
1096 if (symbol->string != string)
1097 obstack_free(&symbol_obstack, string);
1099 pp_token.kind = T_UNKNOWN_CHAR;
1100 pp_token.base.symbol = symbol;
1107 static void print_quoted_string(const char *const string)
1110 for (const char *c = string; *c != 0; ++c) {
1112 case '"': fputs("\\\"", out); break;
1113 case '\\': fputs("\\\\", out); break;
1114 case '\a': fputs("\\a", out); break;
1115 case '\b': fputs("\\b", out); break;
1116 case '\f': fputs("\\f", out); break;
1117 case '\n': fputs("\\n", out); break;
1118 case '\r': fputs("\\r", out); break;
1119 case '\t': fputs("\\t", out); break;
1120 case '\v': fputs("\\v", out); break;
1121 case '\?': fputs("\\?", out); break;
1124 fprintf(out, "\\%03o", (unsigned)*c);
1134 static void print_line_directive(const source_position_t *pos, const char *add)
1136 fprintf(out, "# %u ", pos->lineno);
1137 print_quoted_string(pos->input_name);
1143 printed_input_name = pos->input_name;
1144 input.output_line = pos->lineno-1;
1147 static bool emit_newlines(void)
1149 unsigned delta = pp_token.base.source_position.lineno - input.output_line;
1155 print_line_directive(&pp_token.base.source_position, NULL);
1158 for (unsigned i = 0; i < delta; ++i) {
1162 input.output_line = pp_token.base.source_position.lineno;
1164 for (unsigned i = 0; i < info.whitespace; ++i)
1170 static void emit_pp_token(void)
1175 if (!emit_newlines() &&
1176 (info.had_whitespace || tokens_would_paste(last_token, pp_token.kind)))
1179 switch (pp_token.kind) {
1181 fputs(pp_token.literal.string.begin, out);
1184 case T_STRING_LITERAL:
1185 fputs(get_string_encoding_prefix(pp_token.literal.string.encoding), out);
1187 fputs(pp_token.literal.string.begin, out);
1191 case T_CHARACTER_CONSTANT:
1192 fputs(get_string_encoding_prefix(pp_token.literal.string.encoding), out);
1194 fputs(pp_token.literal.string.begin, out);
1199 fputs(pp_token.base.symbol->string, out);
1202 last_token = pp_token.kind;
1205 static void eat_pp_directive(void)
1207 while (!info.at_line_begin) {
1208 next_preprocessing_token();
1212 static bool strings_equal(const string_t *string1, const string_t *string2)
1214 size_t size = string1->size;
1215 if (size != string2->size)
1218 const char *c1 = string1->begin;
1219 const char *c2 = string2->begin;
1220 for (size_t i = 0; i < size; ++i, ++c1, ++c2) {
1227 static bool pp_tokens_equal(const token_t *token1, const token_t *token2)
1229 if (token1->kind != token2->kind)
1232 switch (token1->kind) {
1234 case T_CHARACTER_CONSTANT:
1235 case T_STRING_LITERAL:
1236 return strings_equal(&token1->literal.string, &token2->literal.string);
1239 return token1->base.symbol == token2->base.symbol;
1243 static bool pp_definitions_equal(const pp_definition_t *definition1,
1244 const pp_definition_t *definition2)
1246 if (definition1->list_len != definition2->list_len)
1249 size_t len = definition1->list_len;
1250 const token_t *t1 = definition1->token_list;
1251 const token_t *t2 = definition2->token_list;
1252 for (size_t i = 0; i < len; ++i, ++t1, ++t2) {
1253 if (!pp_tokens_equal(t1, t2))
1259 static void parse_define_directive(void)
1267 assert(obstack_object_size(&pp_obstack) == 0);
1269 if (pp_token.kind != T_IDENTIFIER || info.at_line_begin) {
1270 errorf(&pp_token.base.source_position,
1271 "expected identifier after #define, got %K", &pp_token);
1274 symbol_t *const symbol = pp_token.base.symbol;
1276 pp_definition_t *new_definition
1277 = obstack_alloc(&pp_obstack, sizeof(new_definition[0]));
1278 memset(new_definition, 0, sizeof(new_definition[0]));
1279 new_definition->source_position = input.position;
1281 /* this is probably the only place where spaces are significant in the
1282 * lexer (except for the fact that they separate tokens). #define b(x)
1283 * is something else than #define b (x) */
1284 if (input.c == '(') {
1285 eat_token(T_IDENTIFIER);
1289 switch (pp_token.kind) {
1291 new_definition->is_variadic = true;
1292 eat_token(T_DOTDOTDOT);
1293 if (pp_token.kind != ')') {
1294 errorf(&input.position,
1295 "'...' not at end of macro argument list");
1301 obstack_ptr_grow(&pp_obstack, pp_token.base.symbol);
1302 eat_token(T_IDENTIFIER);
1304 if (pp_token.kind == ',') {
1309 if (pp_token.kind != ')') {
1310 errorf(&pp_token.base.source_position,
1311 "expected ',' or ')' after identifier, got %K",
1319 goto finish_argument_list;
1322 errorf(&pp_token.base.source_position,
1323 "expected identifier, '...' or ')' in #define argument list, got %K",
1329 finish_argument_list:
1330 new_definition->has_parameters = true;
1331 new_definition->n_parameters
1332 = obstack_object_size(&pp_obstack) / sizeof(new_definition->parameters[0]);
1333 new_definition->parameters = obstack_finish(&pp_obstack);
1335 eat_token(T_IDENTIFIER);
1338 /* construct a new pp_definition on the obstack */
1339 assert(obstack_object_size(&pp_obstack) == 0);
1340 size_t list_len = 0;
1341 while (!info.at_line_begin) {
1342 obstack_grow(&pp_obstack, &pp_token, sizeof(pp_token));
1344 next_preprocessing_token();
1347 new_definition->list_len = list_len;
1348 new_definition->token_list = obstack_finish(&pp_obstack);
1350 pp_definition_t *old_definition = symbol->pp_definition;
1351 if (old_definition != NULL) {
1352 if (!pp_definitions_equal(old_definition, new_definition)) {
1353 warningf(WARN_OTHER, &input.position, "multiple definition of macro '%Y' (first defined %P)", symbol, &old_definition->source_position);
1355 /* reuse the old definition */
1356 obstack_free(&pp_obstack, new_definition);
1357 new_definition = old_definition;
1361 symbol->pp_definition = new_definition;
1365 if (obstack_object_size(&pp_obstack) > 0) {
1366 char *ptr = obstack_finish(&pp_obstack);
1367 obstack_free(&pp_obstack, ptr);
1372 static void parse_undef_directive(void)
1380 if (pp_token.kind != T_IDENTIFIER) {
1381 errorf(&input.position,
1382 "expected identifier after #undef, got %K", &pp_token);
1387 pp_token.base.symbol->pp_definition = NULL;
1388 eat_token(T_IDENTIFIER);
1390 if (!info.at_line_begin) {
1391 warningf(WARN_OTHER, &input.position, "extra tokens at end of #undef directive");
1396 /** behind an #include we can have the special headername lexems.
1397 * They're only allowed behind an #include so they're not recognized
1398 * by the normal next_preprocessing_token. We handle them as a special
1400 static void parse_headername(void)
1402 const source_position_t start_position = input.position;
1403 string_t string = { NULL, 0, STRING_ENCODING_CHAR };
1404 assert(obstack_object_size(&symbol_obstack) == 0);
1406 if (info.at_line_begin) {
1407 parse_error("expected headername after #include");
1411 /* check wether we have a "... or <... headername */
1415 case '<': delimiter = '>'; goto parse_name;
1416 case '"': delimiter = '"'; goto parse_name;
1423 errorf(&pp_token.base.source_position, "header name without closing '%c'", (char)delimiter);
1427 if (input.c == delimiter) {
1429 goto finished_headername;
1431 obstack_1grow(&symbol_obstack, (char)input.c);
1437 /* we should never be here */
1441 /* TODO: do normal pp_token parsing and concatenate results */
1442 panic("pp_token concat include not implemented yet");
1445 finished_headername:
1446 string = sym_make_string(STRING_ENCODING_CHAR);
1449 pp_token.base.source_position = start_position;
1450 pp_token.kind = T_HEADERNAME;
1451 pp_token.literal.string = string;
1454 static bool do_include(bool system_include, const char *headername)
1456 size_t headername_len = strlen(headername);
1457 if (!system_include) {
1458 /* put dirname of current input on obstack */
1459 const char *filename = input.position.input_name;
1460 const char *last_slash = strrchr(filename, '/');
1461 if (last_slash != NULL) {
1462 size_t len = last_slash - filename;
1463 obstack_grow(&symbol_obstack, filename, len + 1);
1464 obstack_grow0(&symbol_obstack, headername, headername_len);
1465 char *complete_path = obstack_finish(&symbol_obstack);
1466 headername = identify_string(complete_path);
1469 FILE *file = fopen(headername, "r");
1471 switch_input(file, headername);
1476 assert(obstack_object_size(&symbol_obstack) == 0);
1477 /* check searchpath */
1478 for (searchpath_entry_t *entry = searchpath; entry != NULL;
1479 entry = entry->next) {
1480 const char *path = entry->path;
1481 size_t len = strlen(path);
1482 obstack_grow(&symbol_obstack, path, len);
1483 if (path[len-1] != '/')
1484 obstack_1grow(&symbol_obstack, '/');
1485 obstack_grow(&symbol_obstack, headername, headername_len+1);
1487 char *complete_path = obstack_finish(&symbol_obstack);
1488 FILE *file = fopen(complete_path, "r");
1490 const char *filename = identify_string(complete_path);
1491 switch_input(file, filename);
1494 obstack_free(&symbol_obstack, complete_path);
1501 /* read till next newline character, only for parse_include_directive(),
1502 * use eat_pp_directive() in all other cases */
1503 static void skip_till_newline(void)
1505 /* skip till newline */
1516 static void parse_include_directive(void)
1523 /* don't eat the TP_include here!
1524 * we need an alternative parsing for the next token */
1526 bool system_include = input.c == '<';
1528 string_t headername = pp_token.literal.string;
1529 if (headername.begin == NULL) {
1535 if (!info.at_line_begin) {
1536 warningf(WARN_OTHER, &pp_token.base.source_position,
1537 "extra tokens at end of #include directive");
1538 skip_till_newline();
1541 if (n_inputs > INCLUDE_LIMIT) {
1542 errorf(&pp_token.base.source_position, "#include nested too deeply");
1544 next_preprocessing_token();
1551 bool res = do_include(system_include, pp_token.literal.string.begin);
1553 errorf(&pp_token.base.source_position, "failed including '%S': %s", &pp_token.literal, strerror(errno));
1554 pop_restore_input();
1558 static pp_conditional_t *push_conditional(void)
1560 pp_conditional_t *conditional
1561 = obstack_alloc(&pp_obstack, sizeof(*conditional));
1562 memset(conditional, 0, sizeof(*conditional));
1564 conditional->parent = conditional_stack;
1565 conditional_stack = conditional;
1570 static void pop_conditional(void)
1572 assert(conditional_stack != NULL);
1573 conditional_stack = conditional_stack->parent;
1576 static void check_unclosed_conditionals(void)
1578 while (conditional_stack != NULL) {
1579 pp_conditional_t *conditional = conditional_stack;
1581 if (conditional->in_else) {
1582 errorf(&conditional->source_position, "unterminated #else");
1584 errorf(&conditional->source_position, "unterminated condition");
1590 static void parse_ifdef_ifndef_directive(bool const is_ifdef)
1593 eat_pp(is_ifdef ? TP_ifdef : TP_ifndef);
1597 pp_conditional_t *conditional = push_conditional();
1598 conditional->source_position = pp_token.base.source_position;
1599 conditional->skip = true;
1603 if (pp_token.kind != T_IDENTIFIER || info.at_line_begin) {
1604 errorf(&pp_token.base.source_position,
1605 "expected identifier after #%s, got %K",
1606 is_ifdef ? "ifdef" : "ifndef", &pp_token);
1609 /* just take the true case in the hope to avoid further errors */
1612 /* evaluate wether we are in true or false case */
1613 condition = (bool)pp_token.base.symbol->pp_definition == is_ifdef;
1614 eat_token(T_IDENTIFIER);
1616 if (!info.at_line_begin) {
1617 errorf(&pp_token.base.source_position,
1618 "extra tokens at end of #%s",
1619 is_ifdef ? "ifdef" : "ifndef");
1624 pp_conditional_t *conditional = push_conditional();
1625 conditional->source_position = pp_token.base.source_position;
1626 conditional->condition = condition;
1633 static void parse_else_directive(void)
1637 if (!info.at_line_begin) {
1639 warningf(WARN_OTHER, &pp_token.base.source_position, "extra tokens at end of #else");
1644 pp_conditional_t *conditional = conditional_stack;
1645 if (conditional == NULL) {
1646 errorf(&pp_token.base.source_position, "#else without prior #if");
1650 if (conditional->in_else) {
1651 errorf(&pp_token.base.source_position,
1652 "#else after #else (condition started %P)",
1653 &conditional->source_position);
1658 conditional->in_else = true;
1659 if (!conditional->skip) {
1660 skip_mode = conditional->condition;
1662 conditional->source_position = pp_token.base.source_position;
1665 static void parse_endif_directive(void)
1669 if (!info.at_line_begin) {
1671 warningf(WARN_OTHER, &pp_token.base.source_position, "extra tokens at end of #endif");
1676 pp_conditional_t *conditional = conditional_stack;
1677 if (conditional == NULL) {
1678 errorf(&pp_token.base.source_position, "#endif without prior #if");
1682 if (!conditional->skip) {
1688 static void parse_preprocessing_directive(void)
1692 if (info.at_line_begin) {
1693 /* empty directive */
1697 if (pp_token.base.symbol) {
1698 switch (pp_token.base.symbol->pp_ID) {
1699 case TP_define: parse_define_directive(); break;
1700 case TP_else: parse_else_directive(); break;
1701 case TP_endif: parse_endif_directive(); break;
1702 case TP_ifdef: parse_ifdef_ifndef_directive(true); break;
1703 case TP_ifndef: parse_ifdef_ifndef_directive(false); break;
1704 case TP_include: parse_include_directive(); break;
1705 case TP_undef: parse_undef_directive(); break;
1711 errorf(&pp_token.base.source_position, "invalid preprocessing directive #%K", &pp_token);
1716 assert(info.at_line_begin);
1719 static void prepend_include_path(const char *path)
1721 searchpath_entry_t *entry = OALLOCZ(&config_obstack, searchpath_entry_t);
1723 entry->next = searchpath;
1727 static void setup_include_path(void)
1729 /* built-in paths */
1730 prepend_include_path("/usr/include");
1732 /* parse environment variable */
1733 const char *cpath = getenv("CPATH");
1734 if (cpath != NULL && *cpath != '\0') {
1735 const char *begin = cpath;
1739 while (*c != '\0' && *c != ':')
1742 size_t len = c-begin;
1744 /* for gcc compatibility (Matze: I would expect that
1745 * nothing happens for an empty entry...) */
1746 prepend_include_path(".");
1748 char *string = obstack_alloc(&config_obstack, len+1);
1749 memcpy(string, begin, len);
1752 prepend_include_path(string);
1759 } while(*c != '\0');
1763 int pptest_main(int argc, char **argv);
1764 int pptest_main(int argc, char **argv)
1766 init_symbol_table();
1770 obstack_init(&config_obstack);
1771 obstack_init(&pp_obstack);
1772 obstack_init(&input_obstack);
1773 strset_init(&stringset);
1775 error_on_unknown_chars = false;
1777 setup_include_path();
1779 /* simplistic commandline parser */
1780 const char *filename = NULL;
1781 const char *output = NULL;
1782 for (int i = 1; i < argc; ++i) {
1783 const char *opt = argv[i];
1784 if (streq(opt, "-I")) {
1785 prepend_include_path(argv[++i]);
1787 } else if (streq(opt, "-E")) {
1789 } else if (streq(opt, "-o")) {
1792 } else if (opt[0] == '-') {
1793 fprintf(stderr, "Unknown option '%s'\n", opt);
1795 if (filename != NULL)
1796 fprintf(stderr, "Multiple inputs not supported\n");
1800 if (filename == NULL) {
1801 fprintf(stderr, "No input specified\n");
1805 if (output == NULL) {
1808 out = fopen(output, "w");
1810 fprintf(stderr, "Couldn't open output '%s'\n", output);
1815 /* just here for gcc compatibility */
1816 fprintf(out, "# 1 \"%s\"\n", filename);
1817 fprintf(out, "# 1 \"<built-in>\"\n");
1818 fprintf(out, "# 1 \"<command-line>\"\n");
1820 FILE *file = fopen(filename, "r");
1822 fprintf(stderr, "Couldn't open input '%s'\n", filename);
1825 switch_input(file, filename);
1828 if (pp_token.kind == '#' && info.at_line_begin) {
1829 parse_preprocessing_directive();
1831 } else if (pp_token.kind == T_EOF) {
1832 goto end_of_main_loop;
1833 } else if (pp_token.kind == T_IDENTIFIER) {
1834 symbol_t *const symbol = pp_token.base.symbol;
1835 pp_definition_t *const pp_definition = symbol->pp_definition;
1836 if (pp_definition != NULL && !pp_definition->is_expanding) {
1837 expansion_pos = pp_token.base.source_position;
1838 if (pp_definition->has_parameters) {
1839 source_position_t position = pp_token.base.source_position;
1840 add_token_info_t old_info = info;
1841 eat_token(T_IDENTIFIER);
1842 add_token_info_t new_info = info;
1844 /* no opening brace -> no expansion */
1845 if (pp_token.kind == '(') {
1848 /* parse arguments (TODO) */
1849 while (pp_token.kind != T_EOF && pp_token.kind != ')')
1850 next_preprocessing_token();
1852 token_t next_token = pp_token;
1853 /* restore identifier token */
1854 pp_token.kind = T_IDENTIFIER;
1855 pp_token.base.symbol = symbol;
1856 pp_token.base.source_position = position;
1861 pp_token = next_token;
1866 pp_definition->expand_pos = 0;
1867 pp_definition->is_expanding = true;
1868 current_expansion = pp_definition;
1875 next_preprocessing_token();
1880 check_unclosed_conditionals();
1885 obstack_free(&input_obstack, NULL);
1886 obstack_free(&pp_obstack, NULL);
1887 obstack_free(&config_obstack, NULL);
1889 strset_destroy(&stringset);
1892 exit_symbol_table();