Sorry about that. This should definitely fix Sergey's issue.
Unified · Split
| @@ -2890,7 +2890,7 @@ static int macro_subst_tok(TokenString *tok_str, · 2889 unmodified lines | ||
| 2890 | 2890 | p = macro_ptr; |
| 2891 | 2891 | while (is_space(t = *p) || TOK_LINEFEED == t) { |
| 2892 | 2892 | if (saved_parse_flags & PARSE_FLAG_SPACES) |
| 2893 | − tok_str_add(&ws_str, t); | |
| 2893 | + tok_str_add(&ws_str, t); | |
| 2894 | 2894 | ++p; |
| 2895 | 2895 | } |
| 2896 | 2896 | if (t == 0 && can_read_stream) { |
| @@ -2927,7 +2927,8 @@ static int macro_subst_tok(TokenString *tok_str, · 30 unmodified lines | ||
| 2927 | 2927 | break; |
| 2928 | 2928 | ch = ' '; |
| 2929 | 2929 | } |
| 2930 | − tok_str_add(&ws_str, ch); | |
| 2930 | + if (saved_parse_flags & PARSE_FLAG_SPACES) | |
| 2931 | + tok_str_add(&ws_str, ch); | |
| 2931 | 2932 | cinp(); |
| 2932 | 2933 | } |
| 2933 | 2934 | t = ch; |
| @@ -2995,8 +2996,30 @@ static int macro_subst_tok(TokenString *tok_str, · 61 unmodified lines | ||
| 2995 | 2996 | else |
| 2996 | 2997 | break; |
| 2997 | 2998 | } |
| 2998 | − if (tok != ',') | |
| 2999 | − expect(","); | |
| 2999 | + /* | |
| 3000 | + * #define f(a) f(a) | |
| 3001 | + * #define h f(r | |
| 3002 | + * h 5) | |
| 3003 | + */ | |
| 3004 | + if (tok != ',') { | |
| 3005 | + /* Argh! Not a macro invocation after all, at this | |
| 3006 | + * point, so put everything back onto mstr that's | |
| 3007 | + * been skipped since we saw the '(' )*/ | |
| 3008 | + tok_str_new(&str); | |
| 3009 | + tok_str_add(&str, mtok); | |
| 3010 | + tok_str_add(&str, '('); | |
| 3011 | + for (sa = s->next; sa; sa = sa->next) { | |
| 3012 | + int *p = sa->d; | |
| 3013 | + while (p && *p) { | |
| 3014 | + tok_str_add(&str, *p); | |
| 3015 | + p++; | |
| 3016 | + } | |
| 3017 | + mstr = str.str; | |
| 3018 | + /* leak memory */; | |
| 3019 | + mstr_allocated = 0; | |
| 3020 | + goto free_memory; | |
| 3021 | + } | |
| 3022 | + } | |
| 3000 | 3023 | next_nomacro(); |
| 3001 | 3024 | } |
| 3002 | 3025 | if (sa) { |
| @@ -3006,6 +3029,7 @@ static int macro_subst_tok(TokenString *tok_str, · 3 unmodified lines | ||
| 3006 | 3029 | |
| 3007 | 3030 | /* now subst each arg */ |
| 3008 | 3031 | mstr = macro_arg_subst(nested_list, mstr, args); |
| 3032 | + free_memory: | |
| 3009 | 3033 | /* free memory */ |
| 3010 | 3034 | sa = args; |
| 3011 | 3035 | while (sa) { |
| @@ -3057,8 +3081,6 @@ static inline int *macro_twosharps(const int *macro_str) · 45 unmodified lines | ||
| 3057 | 3081 | TOK_GET(&tok, &ptr, &tokc); |
| 3058 | 3082 | if (tok == 0) |
| 3059 | 3083 | break; |
| 3060 | − if (tok == TOK_TWOSHARPS) | |
| 3061 | − continue; | |
| 3062 | 3084 | if (tok == TOK_NOSUBST && start_of_nosubsts < 0) |
| 3063 | 3085 | start_of_nosubsts = macro_str1.len; |
| 3064 | 3086 | while (*ptr == TOK_TWOSHARPS) { |
| @@ -3078,13 +3100,15 @@ static inline int *macro_twosharps(const int *macro_str) · 13 unmodified lines | ||
| 3078 | 3100 | if (tok != TOK_PLCHLDR) |
| 3079 | 3101 | cstr_cat(&cstr, get_tok_str(tok, &tokc)); |
| 3080 | 3102 | n = cstr.size; |
| 3081 | − if (t != TOK_PLCHLDR || tok == TOK_PLCHLDR) | |
| 3103 | + if (t != TOK_PLCHLDR) | |
| 3082 | 3104 | cstr_cat(&cstr, get_tok_str(t, &cval)); |
| 3083 | 3105 | cstr_ccat(&cstr, '\0'); |
| 3084 | 3106 | |
| 3085 | 3107 | tcc_open_bf(tcc_state, ":paste:", cstr.size); |
| 3086 | 3108 | memcpy(file->buffer, cstr.data, cstr.size); |
| 3087 | 3109 | for (;;) { |
| 3110 | + if (0 == *file->buf_ptr) | |
| 3111 | + break; | |
| 3088 | 3112 | next_nomacro1(); |
| 3089 | 3113 | if (0 == *file->buf_ptr) |
| 3090 | 3114 | break; |
| @@ -3095,13 +3119,22 @@ static inline int *macro_twosharps(const int *macro_str) · 4 unmodified lines | ||
| 3095 | 3119 | tcc_close(); |
| 3096 | 3120 | cstr_free(&cstr); |
| 3097 | 3121 | } |
| 3122 | + if (tok == TOK_TWOSHARPS) { | |
| 3123 | + /* two sharps twosharped together tokenize to two | |
| 3124 | + * sharp tokens, not a twosharp token. */ | |
| 3125 | + /* That's fun to say, but is it actually true? GCC | |
| 3126 | + * stringifies #define a # ## # ## # to "## #" (and a | |
| 3127 | + * warning), while we produce "###" (no warning) */ | |
| 3128 | + tok_str_add(¯o_str1, '#'); | |
| 3129 | + tok = '#'; | |
| 3130 | + } | |
| 3098 | 3131 | } |
| 3099 | 3132 | if (tok != TOK_NOSUBST) { |
| 3100 | 3133 | tok_str_add2(¯o_str1, tok, &tokc); |
| 3101 | 3134 | tok = ' '; |
| 3102 | 3135 | start_of_nosubsts = -1; |
| 3103 | − } | |
| 3104 | − tok_str_add2(¯o_str1, tok, &tokc); | |
| 3136 | + } else | |
| 3137 | + tok_str_add2(¯o_str1, tok, &tokc); | |
| 3105 | 3138 | } |
| 3106 | 3139 | tok_str_add(¯o_str1, 0); |
| 3107 | 3140 | return macro_str1.str; |
| @@ -3120,7 +3153,6 @@ static void macro_subst(TokenString *tok_str, Sym **nested_list, · 12 unmodified lines | ||
| 3120 | 3153 | int t, spc; |
| 3121 | 3154 | CValue cval; |
| 3122 | 3155 | struct macro_level ml; |
| 3123 | − int force_blank; | |
| 3124 | 3156 | int gnucomma_index = -1; |
| 3125 | 3157 | |
| 3126 | 3158 | /* first scan for '##' operator handling */ |
| @@ -3130,7 +3162,6 @@ static void macro_subst(TokenString *tok_str, Sym **nested_list, · 3 unmodified lines | ||
| 3130 | 3162 | if (macro_str1) |
| 3131 | 3163 | ptr = macro_str1; |
| 3132 | 3164 | spc = 0; |
| 3133 | − force_blank = 0; | |
| 3134 | 3165 | |
| 3135 | 3166 | while (1) { |
| 3136 | 3167 | /* NOTE: ptr == NULL can only happen if tokens are read from |
| @@ -3176,18 +3207,11 @@ static void macro_subst(TokenString *tok_str, Sym **nested_list, · 39 unmodified lines | ||
| 3176 | 3207 | macro_ptr = ml.p; |
| 3177 | 3208 | if (can_read_stream && *can_read_stream == &ml) |
| 3178 | 3209 | *can_read_stream = ml.prev; |
| 3179 | − if (parse_flags & PARSE_FLAG_SPACES) | |
| 3180 | − force_blank = 1; | |
| 3181 | 3210 | if (old_len == tok_str->len) |
| 3182 | 3211 | tok_str_add(tok_str, TOK_PLCHLDR); |
| 3183 | 3212 | } else { |
| 3184 | 3213 | no_subst: |
| 3185 | − if (force_blank) { | |
| 3186 | − tok_str_add(tok_str, ' '); | |
| 3187 | − spc = 1; | |
| 3188 | − force_blank = 0; | |
| 3189 | − } | |
| 3190 | − if (!check_space(t, &spc)) | |
| 3214 | + if (!check_space(t, &spc)) | |
| 3191 | 3215 | tok_str_add2(tok_str, t, &cval); |
| 3192 | 3216 | } |
| 3193 | 3217 | if (gnucomma_index != -1) { |