2019-09-12 02:55:56 +08:00
// RUN: %clang_cc1 %s -verify -Wsizeof-array-div -fsyntax-only -triple=x86_64-linux-gnu
// RUN: %clang_cc1 %s -verify -fsyntax-only -triple=x86_64-linux-gnu
2019-09-11 18:59:47 +08:00
template < typename Ty , int N >
int f ( Ty ( & Array ) [ N ] ) {
return sizeof ( Array ) / sizeof ( Ty ) ; // Should not warn
}
typedef int int32 ;
void test ( void ) {
2019-09-15 03:38:55 +08:00
int arr [ 12 ] ; // expected-note 2 {{array 'arr' declared here}}
2019-09-11 18:59:47 +08:00
unsigned long long arr2 [ 4 ] ;
int * p = & arr [ 0 ] ;
int a1 = sizeof ( arr ) / sizeof ( * arr ) ;
int a2 = sizeof arr / sizeof p ; // expected-warning {{expression does not compute the number of elements in this array; element type is 'int', not 'int *'}}
2019-09-15 03:38:55 +08:00
// expected-note@-1 {{place parentheses around the 'sizeof p' expression to silence this warning}}
2019-09-11 18:59:47 +08:00
int a4 = sizeof arr2 / sizeof p ;
int a5 = sizeof ( arr ) / sizeof ( short ) ; // expected-warning {{expression does not compute the number of elements in this array; element type is 'int', not 'short'}}
2019-09-15 03:38:55 +08:00
// expected-note@-1 {{place parentheses around the 'sizeof(short)' expression to silence this warning}}
2019-09-11 18:59:47 +08:00
int a6 = sizeof ( arr ) / sizeof ( int32 ) ;
int a7 = sizeof ( arr ) / sizeof ( int ) ;
int a9 = sizeof ( arr ) / sizeof ( unsigned int ) ;
const char arr3 [ 2 ] = " A " ;
int a10 = sizeof ( arr3 ) / sizeof ( char ) ;
2019-09-15 03:38:55 +08:00
int a11 = sizeof ( arr2 ) / ( sizeof ( unsigned ) ) ;
int a12 = sizeof ( arr ) / ( sizeof ( short ) ) ;
[Diagnostics] Silence -Wsizeof-array-div for character buffers
Summary:
Character buffers are sometimes used to represent a pool of memory that
contains non-character objects, due to them being synonymous with a stream of
bytes on almost all modern architectures. Often, when interacting with hardware
devices, byte buffers are therefore used as an intermediary and so we can end
Character buffers are sometimes used to represent a pool of memory that
contains non-character objects, due to them being synonymous with a stream of
bytes on almost all modern architectures. Often, when interacting with hardware
devices, byte buffers are therefore used as an intermediary and so we can end
up generating lots of false-positives.
Moreover, due to the ability of character pointers to alias non-character
pointers, the strict aliasing violations that would generally be implied by the
calculations caught by the warning (if the calculation itself is in fact
correct) do not apply here, and so although the length calculation may be
wrong, that is the only possible issue.
Reviewers: rsmith, xbolva00, thakis
Reviewed By: xbolva00, thakis
Subscribers: thakis, lebedev.ri, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D68526
llvm-svn: 374035
2019-10-08 19:34:02 +08:00
int a13 = sizeof ( arr3 ) / sizeof ( p ) ;
int a14 = sizeof ( arr3 ) / sizeof ( int ) ;
2019-09-11 18:59:47 +08:00
2019-09-23 20:54:35 +08:00
int arr4 [ 10 ] [ 12 ] ;
int b1 = sizeof ( arr4 ) / sizeof ( arr2 [ 12 ] ) ;
int b2 = sizeof ( arr4 ) / sizeof ( int * ) ;
int b3 = sizeof ( arr4 ) / sizeof ( short * ) ;
int arr5 [ ] [ 5 ] = {
2019-09-15 03:38:55 +08:00
{ 1 , 2 , 3 , 4 , 5 } ,
{ 6 , 7 , 8 , 9 , 0 } ,
} ;
int c1 = sizeof ( arr5 ) / sizeof ( * arr5 ) ;
2019-09-23 20:54:35 +08:00
int c2 = sizeof ( arr5 ) / sizeof ( * * arr5 ) ;
int c3 = sizeof ( arr5 ) / sizeof ( int ) ;
float m [ 4 ] [ 4 ] ;
int d1 = sizeof ( m ) / sizeof ( * * m ) ;
2019-09-11 18:59:47 +08:00
}