r372600 - [Diagnostics] Avoid -Wsizeof-array-div when dividing the size of a nested array by the size of the deepest base type

David Bolvansky via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 23 05:54:35 PDT 2019


Author: xbolva00
Date: Mon Sep 23 05:54:35 2019
New Revision: 372600

URL: http://llvm.org/viewvc/llvm-project?rev=372600&view=rev
Log:
[Diagnostics] Avoid -Wsizeof-array-div when dividing the size of a nested array by the size of the deepest base type

Modified:
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/Sema/div-sizeof-array.cpp

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=372600&r1=372599&r2=372600&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Mon Sep 23 05:54:35 2019
@@ -9191,7 +9191,8 @@ static void DiagnoseDivisionSizeofPointe
     }
   } 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)

Modified: cfe/trunk/test/Sema/div-sizeof-array.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/div-sizeof-array.cpp?rev=372600&r1=372599&r2=372600&view=diff
==============================================================================
--- cfe/trunk/test/Sema/div-sizeof-array.cpp (original)
+++ cfe/trunk/test/Sema/div-sizeof-array.cpp Mon Sep 23 05:54:35 2019
@@ -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 at -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 at -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 at -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 at -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 at -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);
 }




More information about the cfe-commits mailing list