llvm-project/clang/test/SemaObjC/flexible-array-bounds.m
Bill Wendling b76219b590 [clang][Sema] Use correct array size for diagnostic
The diagnostic was confusing and reporting that an array contains far
more elements than it is defined to have, due to casting.

For example, this code:

  double foo[4096];
  ((char*)foo)[sizeof(foo)];

warns that the "index 32768 is past the end of the array (which contains
32768 elements)."

Reviewed By: serge-sans-paille, aaron.ballman

Differential Revision: https://reviews.llvm.org/D135920
2022-10-20 11:56:49 -07:00

27 lines
571 B
Objective-C

// RUN: %clang_cc1 -fsyntax-only -verify %s
// expected-no-diagnostics
//
// RUN: %clang_cc1 -fsyntax-only -fstrict-flex-arrays=2 -verify=warn %s
@interface Flexible {
@public
char flexible[];
}
@end
@interface Flexible0 {
@public
char flexible[0];
}
@end
@interface Flexible1 {
@public
char flexible[1];
}
@end
char readit(Flexible *p) { return p->flexible[2]; }
char readit0(Flexible0 *p) { return p->flexible[2]; }
char readit1(Flexible1 *p) { return p->flexible[2]; } // warn-warning {{array index 2 is past the end of the array (that has type 'char[1]')}}