Skip to content

Commit

Permalink
[Diagnostics] Avoid -Wsizeof-array-div when dividing the size of a ne…
Browse files Browse the repository at this point in the history
…sted array by the size of the deepest base type

llvm-svn: 372600
  • Loading branch information
davidbolvansky committed Sep 23, 2019
1 parent c720f63 commit 116e6cf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
3 changes: 2 additions & 1 deletion clang/lib/Sema/SemaExpr.cpp
Expand Up @@ -9191,7 +9191,8 @@ static void DiagnoseDivisionSizeofPointerOrArray(Sema &S, Expr *LHS, Expr *RHS,
}
} else if (const auto *ArrayTy = S.Context.getAsArrayType(LHSTy)) {
QualType ArrayElemTy = ArrayTy->getElementType();
if (ArrayElemTy->isDependentType() || RHSTy->isDependentType() ||
if (ArrayElemTy != S.Context.getBaseElementType(ArrayTy) ||
ArrayElemTy->isDependentType() || RHSTy->isDependentType() ||
S.Context.getTypeSize(ArrayElemTy) == S.Context.getTypeSize(RHSTy))
return;
S.Diag(Loc, diag::warn_division_sizeof_array)
Expand Down
23 changes: 10 additions & 13 deletions clang/test/Sema/div-sizeof-array.cpp
Expand Up @@ -26,21 +26,18 @@ void test(void) {
int a11 = sizeof(arr2) / (sizeof(unsigned));
int a12 = sizeof(arr) / (sizeof(short));

int arr4[10][12]; // expected-note 3 {{array 'arr4' declared here}}
int b1 = sizeof(arr4) / sizeof(arr2[12]); // expected-warning {{expression does not compute the number of elements in this array; element type is 'int [12]', not 'unsigned long long'}}
// expected-note@-1 {{place parentheses around the 'sizeof (arr2[12])' expression to silence this warning}}
int b2 = sizeof(arr4) / sizeof(int *); // expected-warning {{expression does not compute the number of elements in this array; element type is 'int [12]', not 'int *'}}
// expected-note@-1 {{place parentheses around the 'sizeof(int *)' expression to silence this warning}}
int b3 = sizeof(arr4) / sizeof(short *); // expected-warning {{expression does not compute the number of elements in this array; element type is 'int [12]', not 'short *'}}
// expected-note@-1 {{place parentheses around the 'sizeof(short *)' expression to silence this warning}}

int arr5[][5] = { // expected-note 2 {{array 'arr5' declared here}}
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] = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 0},
};
int c1 = sizeof(arr5) / sizeof(*arr5);
int c2 = sizeof(arr5) / sizeof(**arr5); // expected-warning {{expression does not compute the number of elements in this array; element type is 'int [5]', not 'int'}}
// expected-note@-1 {{place parentheses around the 'sizeof (**arr5)' expression to silence this warning}}
int c3 = sizeof(arr5) / sizeof(int); // expected-warning {{expression does not compute the number of elements in this array; element type is 'int [5]', not 'int'}}
// expected-note@-1 {{place parentheses around the 'sizeof(int)' expression to silence this warning}}
int c2 = sizeof(arr5) / sizeof(**arr5);
int c3 = sizeof(arr5) / sizeof(int);

float m[4][4];
int d1 = sizeof(m) / sizeof(**m);
}

0 comments on commit 116e6cf

Please sign in to comment.