Hi, an update on this feature:

For a more complicated testing case like the following:

#define MAX_LENGTH 10
int a[MAX_LENGTH];

void __attribute__ ((noinline))  foo (int i, bool is_day, bool is_dollar)
{
  if (i < MAX_LENGTH)
    {
      if (is_day)
        __builtin_printf ("day");
      else
        __builtin_printf ("night");
      if (i == -1)
        {
          if (is_dollar)
            __builtin_printf ("dollar");
          else
            __builtin_printf ("euro");
          a[i] = -1; /* { dg-warning "is below array bounds of" } */
        }
      else
        a[i] = i;
    }
  else
    a[i] = i + 1; /* { dg-warning "is above array bounds of" } */
}

int main ()
{
  for (int i = 0; i < MAX_LENGTH; i++)
    foo (i, false, true);
  return 0;
}


With my latest GCC + -fdiagnostics-show-context=3, we can get the following 
very nice information:

[opc@qinzhao-aarch64-ol8 gcc]$ sh t
/home/opc/Work/GCC/latest-gcc-write/gcc/testsuite/gcc.dg/pr109071_11.c: In 
function ‘foo’:
/home/opc/Work/GCC/latest-gcc-write/gcc/testsuite/gcc.dg/pr109071_11.c:29:6: 
warning: array subscript 10 is above array bounds of ‘int[10]’ [-Warray-bounds=]
   29 |     a[i] = i + 1; /* { dg-warning "is above array bounds of" } */
      |     ~^~~
  ‘foo’: events 1-2
   11 |   if (i < MAX_LENGTH)
      |      ^
      |      |
      |      (1) when the condition is evaluated to false
......
   29 |     a[i] = i + 1; /* { dg-warning "is above array bounds of" } */
      |     ~~~~
      |      |
      |      (2) warning happens here
/home/opc/Work/GCC/latest-gcc-write/gcc/testsuite/gcc.dg/pr109071_11.c:7:5: 
note: while referencing ‘a’
    7 | int a[MAX_LENGTH];
      |     ^
/home/opc/Work/GCC/latest-gcc-write/gcc/testsuite/gcc.dg/pr109071_11.c:23:12: 
warning: array subscript -1 is below array bounds of ‘int[10]’ [-Warray-bounds=]
   23 |           a[i] = -1; /* { dg-warning "is below array bounds of" } */
      |           ~^~~
  ‘foo’: events 1-3
   11 |   if (i < MAX_LENGTH)
      |      ~           |      |
      |      (2) when the condition is evaluated to true
......
   17 |       if (i == -1)
      |          ^
      |          |
      |          (1) when the condition is evaluated to true
......
   23 |           a[i] = -1; /* { dg-warning "is below array bounds of" } */
      |           ~~~~
      |            |
      |            (3) warning happens here
/home/opc/Work/GCC/latest-gcc-write/gcc/testsuite/gcc.dg/pr109071_11.c:7:5: 
note: while referencing ‘a’
    7 | int a[MAX_LENGTH];
      |     ^

I am doing regression testing on this version, hopefully will send out the 
patch very soon for review.

Qing

On Jun 30, 2025, at 12:51, Qing Zhao <qing.z...@oracle.com> wrote:
> 
> 
> 
>> On Jun 30, 2025, at 07:33, Richard Biener <richard.guent...@gmail.com> wrote:
> Yes, immediate dominator should work for such cases. 
>> Maybe a simple heuristic
>> on number of line to not skip too far works, or simply ignore this
>> for now, or simply only use immediate dominators for the first
>> condition to be printed?
> 
> I will experiment a little bit on this.  Do you have any more complicate 
> testing cases? That will be very helpful.
> 
> Thanks a lot for all the help.
> 
> Qing
>> 

Reply via email to