https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121750

            Bug ID: 121750
           Summary: Diagnostic output: Improve column number in location
                    data (and use more ranges)
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: burnus at gcc dot gnu.org
  Target Milestone: ---

Created attachment 62261
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=62261&action=edit
Patch to improve the expression location (works - but requires testsuite
changed + is not ideal in some cases)

There are several issues with the current location data:

* In several cases, the expr->where is rather arbitrarily set to
  gfc_current_locus

In particular,
* white-space is not honored - such that it does not point at the
  expression but elsewhere (usually at the whitespace before the
  expression) - but it might also point at the last whitespace after
  the expression!

* As the current location points to the location before the character,
  there is often an off-by one error. For %C, that's addressed in
  the error print function, but not for most diagnostic.

* Better error locations might get overridden by later parsing steps.


In principle, it would be nice to have range based errors - like with
  gfc_get_location_range
However, calling that function with a range may lead to three new hash
entries in the 'location_t' table: For beginning, end, and range.

That's fine if gfortran would just parse the file once, but the method
of try if that matches, discard, try something else, etc. would lead to
a huge number of bogus entries. That's slow, increases the memory usage
and might cause problems with large files, albeit since
r15-6016-gd9cdc500c1e86b
location_t is now a 63bit instead of 31bit integer (upper bit 64 not used,
permitting to take differences).

Still, we should consider handling ranges somehow. Either nonetheless
using location_t ranges also during parsing - or only selectively (but
that's not easy). Or:

I wonder whether it makes more sense to add a 'locus end_where' to gfc_expr.
For the error call, we can then convert it to a location_t range - either
always or only when not buffering the output. For this, one can create a
function:
  const locus *gfc_expr_loc(gfc_expr *e)
  {
    /* Already location_t or no range.  */
    if (e->where->nextc == (gfc_char_t *) -1
        || (e->where->nextc == 0 && loc->u.lb == NULL))
      return &e->where;
    e->where = gfc_get_location_range (NULL, 0, e->where, 0, e->end);
    return e->where;
  }

* * *

Albeit the off-by-one issue is not quite solved here.
BTW: regarding the patch, I think an inline function that conditionally bumps
the location is better than the nextc++ implemented here. The condition is
  *loc->nextc != '\0'  For the attach patch, it was done unconditionally as
the condition should be false, given that the match was successful.

* * *

Attached is a patch that fixes some error locations - but it causes issues
like:

   realvar = 0.23_8 &
             *cmplxvar
             ^............ warning for ignoring cmplx part is here
   → warn_conversion_6.f90

where the complx -> real conversion warning (imag) is now at '*' - which is the
proper location as that's the outermost expression, i.e. the RHS is actually:

   0.23_8 * cmplxvar
   ~~~~~1~~~~~~~~~

writing as range-based location - but currently it looks as if the 'cmplxvar'
is the issue (and the '1' is misplaced under '*').

However, IMHO, the proper location would be at '=' as that's the conversion
location, the RHS itself is OK (or rather has its own warning in the mentioned
test case as one is real(8) and cmplx is complex(4) in warn_conversion_6.f90).


Due to such changes, the current patch has several testsuite fails.


BTW: IMHO, we also should add more tests that take care of the line number -
but only after checking that the line number makes sense.

Reply via email to