Unified · Split
| @@ -223,7 +223,7 @@ typedef struct BufferedFile { · 222 unmodified lines | |||
| 223 | uint8_t *buf_ptr; | 223 | uint8_t *buf_ptr; |
| 224 | uint8_t *buf_end; | 224 | uint8_t *buf_end; |
| 225 | int fd; | 225 | int fd; |
| 226 | int line_num; /* current line number - here to simply code */ | 226 | int line_num; /* current line number - here to simplify code */ |
| 227 | int ifndef_macro; /* #ifndef macro / #endif search */ | 227 | int ifndef_macro; /* #ifndef macro / #endif search */ |
| 228 | int ifndef_macro_saved; /* saved ifndef_macro */ | 228 | int ifndef_macro_saved; /* saved ifndef_macro */ |
| 229 | int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */ | 229 | int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */ |
| @@ -1738,6 +1738,18 @@ static int handle_stray1(uint8_t *p) · 1508 unmodified lines | |||
| 1738 | return c; | 1738 | return c; |
| 1739 | } | 1739 | } |
| 1740 | 1740 | ||
| 1741 | /* handle just the EOB case, but not stray */ | ||
| 1742 | #define PEEKC_EOB(c, p)\ | ||
| 1743 | {\ | ||
| 1744 | p++;\ | ||
| 1745 | c = *p;\ | ||
| 1746 | if (c == '\\') {\ | ||
| 1747 | file->buf_ptr = p;\ | ||
| 1748 | c = handle_eob();\ | ||
| 1749 | p = file->buf_ptr;\ | ||
| 1750 | }\ | ||
| 1751 | } | ||
| 1752 | |||
| 1741 | /* handle the complicated stray case */ | 1753 | /* handle the complicated stray case */ |
| 1742 | #define PEEKC(c, p)\ | 1754 | #define PEEKC(c, p)\ |
| 1743 | {\ | 1755 | {\ |
| @@ -1862,11 +1874,73 @@ static inline void skip_spaces(void) · 118 unmodified lines | |||
| 1862 | cinp(); | 1874 | cinp(); |
| 1863 | } | 1875 | } |
| 1864 | 1876 | ||
| 1877 | /* parse a string without interpreting escapes */ | ||
| 1878 | static uint8_t *parse_pp_string(uint8_t *p, | ||
| 1879 | int sep, CString *str) | ||
| 1880 | { | ||
| 1881 | int c; | ||
| 1882 | p++; | ||
| 1883 | for(;;) { | ||
| 1884 | c = *p; | ||
| 1885 | if (c == sep) { | ||
| 1886 | break; | ||
| 1887 | } else if (c == '\\') { | ||
| 1888 | file->buf_ptr = p; | ||
| 1889 | c = handle_eob(); | ||
| 1890 | p = file->buf_ptr; | ||
| 1891 | if (c == CH_EOF) { | ||
| 1892 | unterminated_string: | ||
| 1893 | /* XXX: indicate line number of start of string */ | ||
| 1894 | error("missing terminating %c character", sep); | ||
| 1895 | } else if (c == '\\') { | ||
| 1896 | /* escape : just skip \[\r]\n */ | ||
| 1897 | PEEKC_EOB(c, p); | ||
| 1898 | if (c == '\n') { | ||
| 1899 | file->line_num++; | ||
| 1900 | p++; | ||
| 1901 | } else if (c == '\r') { | ||
| 1902 | PEEKC_EOB(c, p); | ||
| 1903 | if (c != '\n') | ||
| 1904 | expect("'\n' after '\r'"); | ||
| 1905 | file->line_num++; | ||
| 1906 | p++; | ||
| 1907 | } else if (c == CH_EOF) { | ||
| 1908 | goto unterminated_string; | ||
| 1909 | } else { | ||
| 1910 | if (str) { | ||
| 1911 | cstr_ccat(str, '\\'); | ||
| 1912 | cstr_ccat(str, c); | ||
| 1913 | } | ||
| 1914 | p++; | ||
| 1915 | } | ||
| 1916 | } | ||
| 1917 | } else if (c == '\n') { | ||
| 1918 | file->line_num++; | ||
| 1919 | goto add_char; | ||
| 1920 | } else if (c == '\r') { | ||
| 1921 | PEEKC_EOB(c, p); | ||
| 1922 | if (c != '\n') { | ||
| 1923 | cstr_ccat(str, '\r'); | ||
| 1924 | } else { | ||
| 1925 | file->line_num++; | ||
| 1926 | goto add_char; | ||
| 1927 | } | ||
| 1928 | } else { | ||
| 1929 | add_char: | ||
| 1930 | if (str) | ||
| 1931 | cstr_ccat(str, c); | ||
| 1932 | p++; | ||
| 1933 | } | ||
| 1934 | } | ||
| 1935 | p++; | ||
| 1936 | return p; | ||
| 1937 | } | ||
| 1938 | |||
| 1865 | /* skip block of text until #else, #elif or #endif. skip also pairs of | 1939 | /* skip block of text until #else, #elif or #endif. skip also pairs of |
| 1866 | #if/#endif */ | 1940 | #if/#endif */ |
| 1867 | void preprocess_skip(void) | 1941 | void preprocess_skip(void) |
| 1868 | { | 1942 | { |
| 1869 | int a, start_of_line, sep, c; | 1943 | int a, start_of_line, c; |
| 1870 | uint8_t *p; | 1944 | uint8_t *p; |
| 1871 | 1945 | ||
| 1872 | p = file->buf_ptr; | 1946 | p = file->buf_ptr; |
| @@ -1903,41 +1977,7 @@ void preprocess_skip(void) · 30 unmodified lines | |||
| 1903 | /* skip strings */ | 1977 | /* skip strings */ |
| 1904 | case '\"': | 1978 | case '\"': |
| 1905 | case '\'': | 1979 | case '\'': |
| 1906 | sep = c; | 1980 | p = parse_pp_string(p, c, NULL); |
| 1907 | p++; | ||
| 1908 | for(;;) { | ||
| 1909 | c = *p; | ||
| 1910 | if (c == sep) { | ||
| 1911 | break; | ||
| 1912 | } else if (c == '\\') { | ||
| 1913 | file->buf_ptr = p; | ||
| 1914 | c = handle_eob(); | ||
| 1915 | p = file->buf_ptr; | ||
| 1916 | if (c == CH_EOF) { | ||
| 1917 | /* XXX: better error message */ | ||
| 1918 | error("unterminated string"); | ||
| 1919 | } else if (c == '\\') { | ||
| 1920 | /* ignore next char */ | ||
| 1921 | p++; | ||
| 1922 | c = *p; | ||
| 1923 | if (c == '\\') { | ||
| 1924 | file->buf_ptr = p; | ||
| 1925 | c = handle_eob(); | ||
| 1926 | p = file->buf_ptr; | ||
| 1927 | } | ||
| 1928 | if (c == '\n') | ||
| 1929 | file->line_num++; | ||
| 1930 | else if (c != CH_EOF) | ||
| 1931 | p++; | ||
| 1932 | } | ||
| 1933 | } else if (c == '\n') { | ||
| 1934 | file->line_num++; | ||
| 1935 | p++; | ||
| 1936 | } else { | ||
| 1937 | p++; | ||
| 1938 | } | ||
| 1939 | } | ||
| 1940 | p++; | ||
| 1941 | break; | 1981 | break; |
| 1942 | /* skip comments */ | 1982 | /* skip comments */ |
| 1943 | case '/': | 1983 | case '/': |
| @@ -2724,106 +2764,105 @@ static void preprocess(int is_bof) · 780 unmodified lines | |||
| 2724 | parse_flags = saved_parse_flags; | 2764 | parse_flags = saved_parse_flags; |
| 2725 | } | 2765 | } |
| 2726 | 2766 | ||
| 2727 | /* read a number in base b */ | 2767 | /* evaluate escape codes in a string. */ |
| 2728 | static int getn(int b) | 2768 | static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long) |
| 2729 | { | ||
| 2730 | int n, t; | ||
| 2731 | n = 0; | ||
| 2732 | while (1) { | ||
| 2733 | if (ch >= 'a' && ch <= 'f') | ||
| 2734 | t = ch - 'a' + 10; | ||
| 2735 | else if (ch >= 'A' && ch <= 'F') | ||
| 2736 | t = ch - 'A' + 10; | ||
| 2737 | else if (isnum(ch)) | ||
| 2738 | t = ch - '0'; | ||
| 2739 | else | ||
| 2740 | break; | ||
| 2741 | if (t < 0 || t >= b) | ||
| 2742 | break; | ||
| 2743 | n = n * b + t; | ||
| 2744 | inp(); | ||
| 2745 | } | ||
| 2746 | return n; | ||
| 2747 | } | ||
| 2748 | |||
| 2749 | /* read a character for string or char constant and eval escape codes */ | ||
| 2750 | static int getq(void) | ||
| 2751 | { | 2769 | { |
| 2752 | int c; | 2770 | int c, n; |
| 2771 | const char *p; | ||
| 2753 | 2772 | ||
| 2754 | redo: | 2773 | p = buf; |
| 2755 | c = ch; | 2774 | for(;;) { |
| 2756 | inp(); | 2775 | c = *p; |
| 2757 | if (c == '\\') { | 2776 | if (c == '\0') |
| 2758 | switch(ch) { | 2777 | break; |
| 2759 | case '0': case '1': case '2': case '3': | 2778 | if (c == '\\') { |
| 2760 | case '4': case '5': case '6': case '7': | 2779 | p++; |
| 2761 | /* at most three octal digits */ | 2780 | /* escape */ |
| 2762 | c = ch - '0'; | 2781 | c = *p; |
| 2763 | inp(); | 2782 | switch(c) { |
| 2764 | if (isoct(ch)) { | 2783 | case '0': case '1': case '2': case '3': |
| 2765 | c = c * 8 + ch - '0'; | 2784 | case '4': case '5': case '6': case '7': |
| 2766 | inp(); | 2785 | /* at most three octal digits */ |
| 2767 | if (isoct(ch)) { | 2786 | n = c - '0'; |
| 2768 | c = c * 8 + ch - '0'; | 2787 | p++; |
| 2769 | inp(); | 2788 | c = *p; |
| 2789 | if (isoct(c)) { | ||
| 2790 | n = n * 8 + c - '0'; | ||
| 2791 | p++; | ||
| 2792 | c = *p; | ||
| 2793 | if (isoct(c)) { | ||
| 2794 | n = n * 8 + c - '0'; | ||
| 2795 | p++; | ||
| 2796 | } | ||
| 2797 | } | ||
| 2798 | c = n; | ||
| 2799 | goto add_char_nonext; | ||
| 2800 | case 'x': | ||
| 2801 | p++; | ||
| 2802 | n = 0; | ||
| 2803 | for(;;) { | ||
| 2804 | c = *p; | ||
| 2805 | if (c >= 'a' && c <= 'f') | ||
| 2806 | c = c - 'a' + 10; | ||
| 2807 | else if (c >= 'A' && c <= 'F') | ||
| 2808 | c = c - 'A' + 10; | ||
| 2809 | else if (isnum(c)) | ||
| 2810 | c = c - '0'; | ||
| 2811 | else | ||
| 2812 | break; | ||
| 2813 | n = n * 16 + c; | ||
| 2814 | p++; | ||
| 2770 | } | 2815 | } |
| 2816 | c = n; | ||
| 2817 | goto add_char_nonext; | ||
| 2818 | case 'a': | ||
| 2819 | c = '\a'; | ||
| 2820 | break; | ||
| 2821 | case 'b': | ||
| 2822 | c = '\b'; | ||
| 2823 | break; | ||
| 2824 | case 'f': | ||
| 2825 | c = '\f'; | ||
| 2826 | break; | ||
| 2827 | case 'n': | ||
| 2828 | c = '\n'; | ||
| 2829 | break; | ||
| 2830 | case 'r': | ||
| 2831 | c = '\r'; | ||
| 2832 | break; | ||
| 2833 | case 't': | ||
| 2834 | c = '\t'; | ||
| 2835 | break; | ||
| 2836 | case 'v': | ||
| 2837 | c = '\v'; | ||
| 2838 | break; | ||
| 2839 | case 'e': | ||
| 2840 | if (!gnu_ext) | ||
| 2841 | goto invalid_escape; | ||
| 2842 | c = 27; | ||
| 2843 | break; | ||
| 2844 | case '\'': | ||
| 2845 | case '\"': | ||
| 2846 | case '\\': | ||
| 2847 | case '?': | ||
| 2848 | break; | ||
| 2849 | default: | ||
| 2850 | invalid_escape: | ||
| 2851 | error("invalid escaped char"); | ||
| 2771 | } | 2852 | } |
| 2772 | return c; | ||
| 2773 | case 'x': | ||
| 2774 | inp(); | ||
| 2775 | return getn(16); | ||
| 2776 | case 'a': | ||
| 2777 | c = '\a'; | ||
| 2778 | break; | ||
| 2779 | case 'b': | ||
| 2780 | c = '\b'; | ||
| 2781 | break; | ||
| 2782 | case 'f': | ||
| 2783 | c = '\f'; | ||
| 2784 | break; | ||
| 2785 | case 'n': | ||
| 2786 | c = '\n'; | ||
| 2787 | break; | ||
| 2788 | case 'r': | ||
| 2789 | c = '\r'; | ||
| 2790 | break; | ||
| 2791 | case 't': | ||
| 2792 | c = '\t'; | ||
| 2793 | break; | ||
| 2794 | case 'v': | ||
| 2795 | c = '\v'; | ||
| 2796 | break; | ||
| 2797 | case 'e': | ||
| 2798 | if (!gnu_ext) | ||
| 2799 | goto invalid_escape; | ||
| 2800 | c = 27; | ||
| 2801 | break; | ||
| 2802 | case '\'': | ||
| 2803 | case '\"': | ||
| 2804 | case '\\': | ||
| 2805 | case '?': | ||
| 2806 | c = ch; | ||
| 2807 | break; | ||
| 2808 | case '\n': | ||
| 2809 | inp(); | ||
| 2810 | goto redo; | ||
| 2811 | case '\r': | ||
| 2812 | inp(); | ||
| 2813 | if (ch != '\n') | ||
| 2814 | goto invalid_escape; | ||
| 2815 | inp(); | ||
| 2816 | goto redo; | ||
| 2817 | default: | ||
| 2818 | invalid_escape: | ||
| 2819 | error("invalid escaped char"); | ||
| 2820 | } | 2853 | } |
| 2821 | inp(); | 2854 | p++; |
| 2822 | } else if (c == '\r' && ch == '\n') { | 2855 | add_char_nonext: |
| 2823 | inp(); | 2856 | if (!is_long) |
| 2824 | c = '\n'; | 2857 | cstr_ccat(outstr, c); |
| 2858 | else | ||
| 2859 | cstr_wccat(outstr, c); | ||
| 2825 | } | 2860 | } |
| 2826 | return c; | 2861 | /* add a trailing '\0' */ |
| 2862 | if (!is_long) | ||
| 2863 | cstr_ccat(outstr, '\0'); | ||
| 2864 | else | ||
| 2865 | cstr_wccat(outstr, '\0'); | ||
| 2827 | } | 2866 | } |
| 2828 | 2867 | ||
| 2829 | /* we use 64 bit numbers */ | 2868 | /* we use 64 bit numbers */ |
| @@ -3132,7 +3171,7 @@ void parse_number(const char *p) · 302 unmodified lines | |||
| 3132 | /* return next token without macro substitution */ | 3171 | /* return next token without macro substitution */ |
| 3133 | static inline void next_nomacro1(void) | 3172 | static inline void next_nomacro1(void) |
| 3134 | { | 3173 | { |
| 3135 | int b, t, c; | 3174 | int t, c, is_long; |
| 3136 | TokenSym *ts; | 3175 | TokenSym *ts; |
| 3137 | uint8_t *p, *p1; | 3176 | uint8_t *p, *p1; |
| 3138 | unsigned int h; | 3177 | unsigned int h; |
| @@ -3304,11 +3343,8 @@ static inline void next_nomacro1(void) · 165 unmodified lines | |||
| 3304 | goto parse_ident_fast; | 3343 | goto parse_ident_fast; |
| 3305 | } else { | 3344 | } else { |
| 3306 | PEEKC(c, p); | 3345 | PEEKC(c, p); |
| 3307 | if (c == '\'') { | 3346 | if (c == '\'' || c == '\"') { |
| 3308 | tok = TOK_LCHAR; | 3347 | is_long = 1; |
| 3309 | goto char_const; | ||
| 3310 | } else if (c == '\"') { | ||
| 3311 | tok = TOK_LSTR; | ||
| 3312 | goto str_const; | 3348 | goto str_const; |
| 3313 | } else { | 3349 | } else { |
| 3314 | cstr_reset(&tokcstr); | 3350 | cstr_reset(&tokcstr); |
| @@ -3357,42 +3393,51 @@ static inline void next_nomacro1(void) · 42 unmodified lines | |||
| 3357 | } | 3393 | } |
| 3358 | break; | 3394 | break; |
| 3359 | case '\'': | 3395 | case '\'': |
| 3360 | tok = TOK_CCHAR; | ||
| 3361 | char_const: | ||
| 3362 | file->buf_ptr = p; | ||
| 3363 | inp(); | ||
| 3364 | b = getq(); | ||
| 3365 | /* this cast is needed if >= 128 */ | ||
| 3366 | if (tok == TOK_CCHAR) | ||
| 3367 | b = (char)b; | ||
| 3368 | tokc.i = b; | ||
| 3369 | if (ch != '\'') | ||
| 3370 | error("unterminated character constant"); | ||
| 3371 | p = file->buf_ptr; | ||
| 3372 | p++; | ||
| 3373 | break; | ||
| 3374 | case '\"': | 3396 | case '\"': |
| 3375 | tok = TOK_STR; | 3397 | is_long = 0; |
| 3376 | str_const: | 3398 | str_const: |
| 3377 | file->buf_ptr = p; | 3399 | { |
| 3378 | inp(); | 3400 | CString str; |
| 3379 | cstr_reset(&tokcstr); | 3401 | int sep; |
| 3380 | while (ch != '\"') { | 3402 | |
| 3381 | b = getq(); | 3403 | sep = c; |
| 3382 | if (ch == CH_EOF) | 3404 | |
| 3383 | error("unterminated string"); | 3405 | /* parse the string */ |
| 3384 | if (tok == TOK_STR) | 3406 | cstr_new(&str); |
| 3385 | cstr_ccat(&tokcstr, b); | 3407 | p = parse_pp_string(p, sep, &str); |
| 3386 | else | 3408 | cstr_ccat(&str, '\0'); |
| 3387 | cstr_wccat(&tokcstr, b); | 3409 | |
| 3410 | /* eval the escape (should be done as TOK_PPNUM) */ | ||
| 3411 | cstr_reset(&tokcstr); | ||
| 3412 | parse_escape_string(&tokcstr, str.data, is_long); | ||
| 3413 | cstr_free(&str); | ||
| 3414 | |||
| 3415 | if (sep == '\'') { | ||
| 3416 | int char_size; | ||
| 3417 | /* XXX: make it portable */ | ||
| 3418 | if (!is_long) | ||
| 3419 | char_size = 1; | ||
| 3420 | else | ||
| 3421 | char_size = sizeof(int); | ||
| 3422 | if (tokcstr.size <= char_size) | ||
| 3423 | error("empty character constant"); | ||
| 3424 | if (tokcstr.size > 2 * char_size) | ||
| 3425 | warning("multi-character character constant"); | ||
| 3426 | if (!is_long) { | ||
| 3427 | tokc.i = *(int8_t *)tokcstr.data; | ||
| 3428 | tok = TOK_CCHAR; | ||
| 3429 | } else { | ||
| 3430 | tokc.i = *(int *)tokcstr.data; | ||
| 3431 | tok = TOK_LCHAR; | ||
| 3432 | } | ||
| 3433 | } else { | ||
| 3434 | tokc.cstr = &tokcstr; | ||
| 3435 | if (!is_long) | ||
| 3436 | tok = TOK_STR; | ||
| 3437 | else | ||
| 3438 | tok = TOK_LSTR; | ||
| 3439 | } | ||
| 3388 | } | 3440 | } |
| 3389 | if (tok == TOK_STR) | ||
| 3390 | cstr_ccat(&tokcstr, '\0'); | ||
| 3391 | else | ||
| 3392 | cstr_wccat(&tokcstr, '\0'); | ||
| 3393 | tokc.cstr = &tokcstr; | ||
| 3394 | p = file->buf_ptr; | ||
| 3395 | p++; | ||
| 3396 | break; | 3441 | break; |
| 3397 | 3442 | ||
| 3398 | case '<': | 3443 | case '<': |