On Thu, Apr 30, 2015 at 8:03 PM, grischka <[email protected]> wrote:
> Pip Cet wrote:
>>
>> Does this fix it for you? Even if it doesn't, this should indicate
>> where the code is that goes wrong...
>
>
> Yep, just funny how long it did NOT go wrong...

Well, it happens exclusively when a macro expands recursively to
another macro and we hit EOB at the same time. I'm about to commit
some changes that make tcc read files one character at a time (when
debugging is enabled, of course), which might help find such bugs in
future...

>> ch = tcc_peekc_slow(file);
>
> as in ch = handle_eob();

I think peekc_slow indicates more precisely what we're doing—reading
the next character, whether or not we're at EOB.  handle_eob() sounds
like we're definitely at EOB, which we might not be. However, feel
free to change this if you disagree.

>
> One more, anyone ?
>
>     #define t(x,y,z) x ## y ## z
>
>     int main(int argc, char **argv)
>     {
>         return t(1,,) + t(,2,) + t(,,3);
>
>     }

Does C99 allow concatenating two empty tokens?

#define t(x,y) x ## y

void main(int argc, char **argv)
{
  return t(,);
}

also doesn't do what it probably should.

Here's a patch that makes things work by brute force, and it doesn't
appear to break any of the tests. However, it does make:

#define t(x,y) x ## y
#define A(x,y...) f(x,##y)

A(a,t(,))

produce f(,  ), with the extra comma. That's not really compatible
with A(a,) -> f(x), though GCC has stopped handling this case
specially (see http://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html),
so maybe we should as well... (A(a) still works).
diff --git a/tccpp.c b/tccpp.c
index b44296d..86a3aeb 100644
--- a/tccpp.c
+++ b/tccpp.c
@@ -3039,13 +3039,15 @@ static inline int *macro_twosharps(const int *macro_str)
                 if (tok != TOK_PLCHLDR)
                     cstr_cat(&cstr, get_tok_str(tok, &tokc));
                 n = cstr.size;
-                if (t != TOK_PLCHLDR || tok == TOK_PLCHLDR)
+                if (t != TOK_PLCHLDR)
                     cstr_cat(&cstr, get_tok_str(t, &cval));
                 cstr_ccat(&cstr, '\0');
 
                 tcc_open_bf(tcc_state, ":paste:", cstr.size);
                 memcpy(file->buffer, cstr.data, cstr.size);
                 for (;;) {
+                    if (0 == *file->buf_ptr)
+                        break;
                     next_nomacro1();
                     if (0 == *file->buf_ptr)
                         break;
@@ -3058,7 +3060,8 @@ static inline int *macro_twosharps(const int *macro_str)
             }
         }
         if (tok != TOK_NOSUBST) {
-            tok_str_add2(&macro_str1, tok, &tokc);
+            if (tok != TOK_PLCHLDR)
+                tok_str_add2(&macro_str1, tok, &tokc);
             tok = ' ';
             start_of_nosubsts = -1;
         }
_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to