bison-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH 2/4] errors: show carets


From: Akim Demaille
Subject: Re: [PATCH 2/4] errors: show carets
Date: Thu, 6 Dec 2012 17:29:01 +0100

Le 6 déc. 2012 à 17:21, Théophile Ranquet <address@hidden> a écrit :

> +  while (caret_info.line < loc.start.line)
> +    if (fgetc (caret_info.source) == '\n')
> +      ++caret_info.line;

FWIW, I would even

  while (caret_info.line < loc.start.line)
    caret_info.line += fgetc (caret_info.source) == '\n';

> +  caret_info.offset = ftell (caret_info.source);
> 
>   /* Read the actual line.  Don't update the offset, so that we keep a pointer
>      to the start of the line.  */
>   {
> -    ssize_t len = 0;
>     char *buf = NULL;
> -    if ((len = getline (&buf, (size_t*) &len, caret_info.source)) != -1)
> +    size_t size = 0;
> +    ssize_t len = getline (&buf, &size, caret_info.source);
> +    if (0 < len)
>       {
>         /* The caret of a multiline location ends with the first line.  */
>         int end = loc.start.line != loc.end.line ? len : loc.end.column;
> 
> -        if (len)
> -          {
> -            int i = loc.start.column;
> -            /* Quote the file, indent by a single column.  */
> -            fputc (' ', out);
> -            fwrite (buf, 1, len, out);
> -
> -            /* Print the caret, with the same indent as above.  */
> -            fputc (' ', out);
> -            fprintf (out, "%*s", loc.start.column - 1, "");
> -            do {
> +        /* Quote the file, indent by a single column.  */
> +        fputc (' ', out);
> +        fwrite (buf, 1, len, out);
> +
> +        /* Print the caret, with the same indent as above.  */
> +        fprintf (out, " %*s", loc.start.column - 1, "");
> +        {
> +          int i = loc.start.column;
> +          do {
>               fputc ('^', out);
> -            } while (++i < end);

You don't need the braces.  And you seem to really dislike putc :)

> -          }
> +          } while (++i < end);
> +        }
>         fputc ('\n', out);
>         free (buf);

Please, pull the free out of the if.

>       }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]