Skip to content

Commit caf28b0

Browse files
[Diagnostics] Diagnose -Wsizeof-array-div for array of pointers
Differential Revision: https://reviews.llvm.org/D87990
1 parent 32cc8f7 commit caf28b0

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

clang/lib/Sema/SemaExpr.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -10036,7 +10036,7 @@ static void DiagnoseDivisionSizeofPointerOrArray(Sema &S, Expr *LHS, Expr *RHS,
1003610036
QualType RHSTy;
1003710037

1003810038
if (RUE->isArgumentType())
10039-
RHSTy = RUE->getArgumentType();
10039+
RHSTy = RUE->getArgumentType().getNonReferenceType();
1004010040
else
1004110041
RHSTy = RUE->getArgumentExpr()->IgnoreParens()->getType();
1004210042

clang/test/Sema/div-sizeof-array.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,8 @@ void test(void) {
4646
int array[10];
4747
int narray = sizeof(array) / sizeof(int &);
4848
int narray2 = sizeof(array) / sizeof(decltype(array[0]));
49+
50+
int *arrptrs[10]; // expected-note {{array 'arrptrs' declared here}}
51+
int len = sizeof(arrptrs) / sizeof(decltype(*arrptrs[0])); // expected-warning {{expression does not compute the number of elements in this array; element type is 'int *', not 'int'}}
52+
// expected-note@-1 {{place parentheses around the 'sizeof(decltype(*arrptrs[0]))' expression to silence this warning}}
4953
}

clang/test/Sema/div-sizeof-ptr.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ int f(Ty (&Array)[N]) {
77

88
typedef int int32;
99

10-
void test(int *p, int **q) { // expected-note 5 {{pointer 'p' declared here}}
10+
void test(int *p, int **q) { // expected-note 6 {{pointer 'p' declared here}}
1111
const int *r; // expected-note {{pointer 'r' declared here}}
1212
int a1 = sizeof(p) / sizeof(*p); // expected-warning {{'sizeof (p)' will return the size of the pointer, not the array itself}}
1313
int a2 = sizeof p / sizeof *p; // expected-warning {{'sizeof p' will return the size of the pointer, not the array itself}}
@@ -21,6 +21,7 @@ void test(int *p, int **q) { // expected-note 5 {{pointer 'p' declared
2121
int a8 = sizeof(d) / sizeof(int); // expected-warning {{'sizeof (d)' will return the size of the pointer, not the array itself}}
2222

2323
int a9 = sizeof(*q) / sizeof(**q); // expected-warning {{'sizeof (*q)' will return the size of the pointer, not the array itself}}
24+
int a10 = sizeof(p) / sizeof(decltype(*p)); // expected-warning {{'sizeof (p)' will return the size of the pointer, not the array itself}}
2425

2526
// Should not warn
2627
int b1 = sizeof(int *) / sizeof(int);

0 commit comments

Comments
 (0)