From 116e6cf36e4ec06cf4eb7116d7521415236ab9d1 Mon Sep 17 00:00:00 2001 From: David Bolvansky Date: Mon, 23 Sep 2019 12:54:35 +0000 Subject: [PATCH] [Diagnostics] Avoid -Wsizeof-array-div when dividing the size of a nested array by the size of the deepest base type llvm-svn: 372600 --- clang/lib/Sema/SemaExpr.cpp | 3 ++- clang/test/Sema/div-sizeof-array.cpp | 23 ++++++++++------------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 18de1dc8a2aa0..13463dc149feb 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -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) diff --git a/clang/test/Sema/div-sizeof-array.cpp b/clang/test/Sema/div-sizeof-array.cpp index cd9b307451f88..7c76a5265b85e 100644 --- a/clang/test/Sema/div-sizeof-array.cpp +++ b/clang/test/Sema/div-sizeof-array.cpp @@ -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); }