[clang] caf28b0 - [Diagnostics] Diagnose -Wsizeof-array-div for array of pointers

Dávid Bolvanský via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 9 03:56:20 PDT 2020


Author: Dávid Bolvanský
Date: 2020-10-09T12:56:06+02:00
New Revision: caf28b0a1288eb06720acf11d7ad09186b27b74f

URL: https://github.com/llvm/llvm-project/commit/caf28b0a1288eb06720acf11d7ad09186b27b74f
DIFF: https://github.com/llvm/llvm-project/commit/caf28b0a1288eb06720acf11d7ad09186b27b74f.diff

LOG: [Diagnostics] Diagnose  -Wsizeof-array-div for array of pointers

Differential Revision: https://reviews.llvm.org/D87990

Added: 
    

Modified: 
    clang/lib/Sema/SemaExpr.cpp
    clang/test/Sema/div-sizeof-array.cpp
    clang/test/Sema/div-sizeof-ptr.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index dd2e8f8c0d25..26271209b78d 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -10036,7 +10036,7 @@ static void DiagnoseDivisionSizeofPointerOrArray(Sema &S, Expr *LHS, Expr *RHS,
   QualType RHSTy;
 
   if (RUE->isArgumentType())
-    RHSTy = RUE->getArgumentType();
+    RHSTy = RUE->getArgumentType().getNonReferenceType();
   else
     RHSTy = RUE->getArgumentExpr()->IgnoreParens()->getType();
 

diff  --git a/clang/test/Sema/div-sizeof-array.cpp b/clang/test/Sema/div-sizeof-array.cpp
index 898ff42a7bd4..f4d8c2d2a5fb 100644
--- a/clang/test/Sema/div-sizeof-array.cpp
+++ b/clang/test/Sema/div-sizeof-array.cpp
@@ -46,4 +46,8 @@ void test(void) {
   int array[10];
   int narray = sizeof(array) / sizeof(int &);
   int narray2 = sizeof(array) / sizeof(decltype(array[0]));
+
+  int *arrptrs[10];                                          // expected-note {{array 'arrptrs' declared here}}
+  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'}}
+  // expected-note at -1 {{place parentheses around the 'sizeof(decltype(*arrptrs[0]))' expression to silence this warning}}
 }

diff  --git a/clang/test/Sema/div-sizeof-ptr.cpp b/clang/test/Sema/div-sizeof-ptr.cpp
index abb7bbadf0e4..dcb05ccd0162 100644
--- a/clang/test/Sema/div-sizeof-ptr.cpp
+++ b/clang/test/Sema/div-sizeof-ptr.cpp
@@ -7,7 +7,7 @@ int f(Ty (&Array)[N]) {
 
 typedef int int32;
 
-void test(int *p, int **q) {          // expected-note 5 {{pointer 'p' declared here}}
+void test(int *p, int **q) {          // expected-note 6 {{pointer 'p' declared here}}
   const int *r;                       // expected-note {{pointer 'r' declared here}}
   int a1 = sizeof(p) / sizeof(*p);    // expected-warning {{'sizeof (p)' will return the size of the pointer, not the array itself}}
   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
   int a8 = sizeof(d) / sizeof(int);  // expected-warning {{'sizeof (d)' will return the size of the pointer, not the array itself}}
 
   int a9 = sizeof(*q) / sizeof(**q); // expected-warning {{'sizeof (*q)' will return the size of the pointer, not the array itself}}
+  int a10 = sizeof(p) / sizeof(decltype(*p)); // expected-warning {{'sizeof (p)' will return the size of the pointer, not the array itself}}
 
   // Should not warn
   int b1 = sizeof(int *) / sizeof(int);


        


More information about the cfe-commits mailing list