X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=preprocessor.c;h=af95d90c3ea0d2b170ed33f11ba665c1a26008a8;hb=3a75131289ed543970968244180a5b828608b342;hp=a9c10d429ba8fcf2e2a48c68ef20401e283478e0;hpb=778f63bfc216c257ae8194288f77a41f049a3638;p=cparser diff --git a/preprocessor.c b/preprocessor.c index a9c10d4..af95d90 100644 --- a/preprocessor.c +++ b/preprocessor.c @@ -99,7 +99,7 @@ static const char *printed_input_name = NULL; static source_position_t expansion_pos; static pp_definition_t *current_expansion = NULL; static strset_t stringset; -static preprocessor_token_kind_t last_token; +static token_kind_t last_token; static searchpath_entry_t *searchpath; @@ -451,13 +451,18 @@ static const char *identify_string(char *string) return result; } -static string_t make_string(char *string, size_t len) +static string_t sym_make_string(string_encoding_t const enc) { - const char *result = identify_string(string); - return (string_t) {result, len}; + obstack_1grow(&symbol_obstack, '\0'); + size_t const len = obstack_object_size(&symbol_obstack) - 1; + char *const string = obstack_finish(&symbol_obstack); + char const *const result = identify_string(string); + return (string_t){ result, len, enc }; } -static void parse_string(utf32 const delimiter, preprocessor_token_kind_t const kind, string_encoding_t const enc, char const *const context) +static void parse_string(utf32 const delimiter, token_kind_t const kind, + string_encoding_t const enc, + char const *const context) { const unsigned start_linenr = input.position.lineno; @@ -509,14 +514,9 @@ static void parse_string(utf32 const delimiter, preprocessor_token_kind_t const } } -end_of_string:; - obstack_1grow(&symbol_obstack, '\0'); - size_t const size = obstack_object_size(&symbol_obstack) - 1; - char *const string = obstack_finish(&symbol_obstack); - - pp_token.kind = kind; - pp_token.string.encoding = enc; - pp_token.string.string = make_string(string, size); +end_of_string: + pp_token.kind = kind; + pp_token.literal.string = sym_make_string(enc); } static void parse_string_literal(string_encoding_t const enc) @@ -527,13 +527,13 @@ static void parse_string_literal(string_encoding_t const enc) static void parse_character_constant(string_encoding_t const enc) { parse_string('\'', TP_CHARACTER_CONSTANT, enc, "character constant"); - if (pp_token.string.string.size == 0) { + if (pp_token.literal.string.size == 0) { parse_error("empty character constant"); } } -#define SYMBOL_CHARS_WITHOUT_E_P \ - case 'a': \ +#define SYMBOL_CASES_WITHOUT_E_P \ + 'a': \ case 'b': \ case 'c': \ case 'd': \ @@ -581,17 +581,17 @@ static void parse_character_constant(string_encoding_t const enc) case 'X': \ case 'Y': \ case 'Z': \ - case '_': + case '_' -#define SYMBOL_CHARS \ - SYMBOL_CHARS_WITHOUT_E_P \ +#define SYMBOL_CASES \ + SYMBOL_CASES_WITHOUT_E_P: \ case 'e': \ case 'p': \ case 'E': \ - case 'P': + case 'P' -#define DIGITS \ - case '0': \ +#define DIGIT_CASES \ + '0': \ case '1': \ case '2': \ case '3': \ @@ -600,7 +600,7 @@ static void parse_character_constant(string_encoding_t const enc) case '6': \ case '7': \ case '8': \ - case '9': + case '9' /** * returns next final token from a preprocessor macro expansion @@ -740,10 +740,10 @@ static void skip_whitespace(void) } } -static void eat_pp(preprocessor_token_kind_t const type) +static inline void eat_pp(token_kind_t const kind) { - (void) type; - assert(pp_token.kind == type); + assert(pp_token.kind == kind); + (void) kind; next_preprocessing_token(); } @@ -754,8 +754,8 @@ static void parse_symbol(void) while (true) { switch (input.c) { - DIGITS - SYMBOL_CHARS + case DIGIT_CASES: + case SYMBOL_CASES: obstack_1grow(&symbol_obstack, (char) input.c); next_char(); break; @@ -800,8 +800,8 @@ static void parse_number(void) while (true) { switch (input.c) { case '.': - DIGITS - SYMBOL_CHARS_WITHOUT_E_P + case DIGIT_CASES: + case SYMBOL_CASES_WITHOUT_E_P: obstack_1grow(&symbol_obstack, (char) input.c); next_char(); break; @@ -824,12 +824,8 @@ static void parse_number(void) } end_number: - obstack_1grow(&symbol_obstack, '\0'); - size_t size = obstack_object_size(&symbol_obstack); - char *string = obstack_finish(&symbol_obstack); - - pp_token.kind = TP_NUMBER; - pp_token.number.number = make_string(string, size); + pp_token.kind = TP_NUMBER; + pp_token.literal.string = sym_make_string(STRING_ENCODING_CHAR); } @@ -882,11 +878,11 @@ restart: info.had_whitespace = true; goto restart; - SYMBOL_CHARS + case SYMBOL_CASES: parse_symbol(); return; - DIGITS + case DIGIT_CASES: parse_number(); return; @@ -1030,7 +1026,6 @@ restart: case '~': case ';': case ',': - case '\\': pp_token.kind = input.c; next_char(); return; @@ -1139,20 +1134,20 @@ static void emit_pp_token(void) fputs(pp_token.base.symbol->string, out); break; case TP_NUMBER: - fputs(pp_token.number.number.begin, out); + fputs(pp_token.literal.string.begin, out); break; case TP_STRING_LITERAL: - fputs(get_string_encoding_prefix(pp_token.string.encoding), out); + fputs(get_string_encoding_prefix(pp_token.literal.string.encoding), out); fputc('"', out); - fputs(pp_token.string.string.begin, out); + fputs(pp_token.literal.string.begin, out); fputc('"', out); break; case TP_CHARACTER_CONSTANT: - fputs(get_string_encoding_prefix(pp_token.string.encoding), out); + fputs(get_string_encoding_prefix(pp_token.literal.string.encoding), out); fputc('\'', out); - fputs(pp_token.string.string.begin, out); + fputs(pp_token.literal.string.begin, out); fputc('\'', out); break; default: @@ -1196,7 +1191,7 @@ static bool pp_tokens_equal(const token_t *token1, const token_t *token2) case TP_NUMBER: case TP_CHARACTER_CONSTANT: case TP_STRING_LITERAL: - return strings_equal(&token1->string.string, &token2->string.string); + return strings_equal(&token1->literal.string, &token2->literal.string); default: return true; @@ -1346,16 +1341,16 @@ static void parse_undef_directive(void) eat_pp_directive(); } +/** behind an #include we can have the special headername lexems. + * They're only allowed behind an #include so they're not recognized + * by the normal next_preprocessing_token. We handle them as a special + * exception here */ static void parse_headername(void) { const source_position_t start_position = input.position; - string_t string = {NULL, 0}; + string_t string = { NULL, 0, STRING_ENCODING_CHAR }; assert(obstack_object_size(&symbol_obstack) == 0); - /* behind an #include we can have the special headername lexems. - * They're only allowed behind an #include so they're not recognized - * by the normal next_preprocessing_token. We handle them as a special - * exception here */ if (info.at_line_begin) { parse_error("expected headername after #include"); goto finish_error; @@ -1396,15 +1391,12 @@ parse_name: } finished_headername: - obstack_1grow(&symbol_obstack, '\0'); - const size_t size = (size_t)obstack_object_size(&symbol_obstack); - char *const headername = obstack_finish(&symbol_obstack); - string = make_string(headername, size); + string = sym_make_string(STRING_ENCODING_CHAR); finish_error: pp_token.base.source_position = start_position; pp_token.kind = TP_HEADERNAME; - pp_token.string.string = string; + pp_token.literal.string = string; } static bool do_include(bool system_include, const char *headername) @@ -1476,7 +1468,7 @@ static bool parse_include_directive(void) skip_whitespace(); bool system_include = input.c == '<'; parse_headername(); - string_t headername = pp_token.string.string; + string_t headername = pp_token.literal.string; if (headername.begin == NULL) { eat_pp_directive(); return false; @@ -1499,10 +1491,9 @@ static bool parse_include_directive(void) /* switch inputs */ emit_newlines(); push_input(); - bool res = do_include(system_include, pp_token.string.string.begin); + bool res = do_include(system_include, pp_token.literal.string.begin); if (!res) { - errorf(&pp_token.base.source_position, - "failed including '%S': %s", pp_token.string, strerror(errno)); + errorf(&pp_token.base.source_position, "failed including '%S': %s", &pp_token.literal, strerror(errno)); pop_restore_input(); return false; } @@ -1566,7 +1557,7 @@ static void parse_ifdef_ifndef_directive(void) condition = true; } else { /* evaluate wether we are in true or false case */ - condition = !pp_token.base.symbol->pp_definition == is_ifndef; + condition = (bool)!pp_token.base.symbol->pp_definition == is_ifndef; next_preprocessing_token(); @@ -1607,7 +1598,7 @@ static void parse_else_directive(void) if (conditional->in_else) { errorf(&pp_token.base.source_position, "#else after #else (condition started %P)", - conditional->source_position); + &conditional->source_position); skip_mode = true; return; }