[PATCH] Sema: Warn on sizeof on binary ops on decayed arrays.

Benjamin Kramer benny.kra at gmail.com
Sun Mar 24 08:22:50 PDT 2013


  Extend and enhance comments.

http://llvm-reviews.chandlerc.com/D571

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D571?vs=1378&id=1380#toc

Files:
  include/clang/Basic/DiagnosticGroups.td
  include/clang/Basic/DiagnosticSemaKinds.td
  lib/Sema/SemaExpr.cpp
  test/Sema/expr-comma-c99.c
  test/Sema/expr-comma.c
  test/Sema/warn-sizeof-array-decay.c

Index: include/clang/Basic/DiagnosticGroups.td
===================================================================
--- include/clang/Basic/DiagnosticGroups.td
+++ include/clang/Basic/DiagnosticGroups.td
@@ -214,6 +214,7 @@
 def : DiagGroup<"switch-default">;
 def : DiagGroup<"synth">;
 def SizeofArrayArgument : DiagGroup<"sizeof-array-argument">;
+def SizeofArrayDecay : DiagGroup<"sizeof-array-decay">;
 def SizeofPointerMemaccess : DiagGroup<"sizeof-pointer-memaccess">;
 def StaticInInline : DiagGroup<"static-in-inline">;
 def GNUStaticFloatInit : DiagGroup<"gnu-static-float-init">;
@@ -420,6 +421,7 @@
     ReturnType,
     SelfAssignment,
     SizeofArrayArgument,
+    SizeofArrayDecay,
     StringPlusInt,
     Trigraphs,
     Uninitialized,
Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td
+++ include/clang/Basic/DiagnosticSemaKinds.td
@@ -4033,6 +4033,10 @@
   "sizeof on array function parameter will return size of %0 instead of %1">,
   InGroup<SizeofArrayArgument>;
 
+def warn_sizeof_array_decay : Warning<
+  "sizeof on pointer operation will return size of %0 instead of %1">,
+  InGroup<SizeofArrayDecay>;
+
 def err_sizeof_nonfragile_interface : Error<
   "application of '%select{alignof|sizeof}1' to interface %0 is "
   "not supported on this architecture and platform">;
Index: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp
+++ lib/Sema/SemaExpr.cpp
@@ -3098,6 +3098,27 @@
         }
       }
     }
+
+    // Warn on "sizeof(array op x)" and "sizeof(x op array)", where the array
+    // decays into a pointer and returns an unintended result. This is most
+    // likely a typo for "sizeof(array) op x". We don't warn when both sides are
+    // decayed arrays, sizeof(array - array) is a legitimate way to get the size
+    // of ptrdiff_t.
+    if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E->IgnoreParens())) {
+      ImplicitCastExpr *LHSICE = dyn_cast<ImplicitCastExpr>(BO->getLHS());
+      ImplicitCastExpr *RHSICE = dyn_cast<ImplicitCastExpr>(BO->getRHS());
+      bool LHSDecay = LHSICE && LHSICE->getCastKind() == CK_ArrayToPointerDecay;
+      bool RHSDecay = RHSICE && RHSICE->getCastKind() == CK_ArrayToPointerDecay;
+
+      if (LHSDecay && !RHSDecay && BO->getType() == LHSICE->getType())
+        Diag(BO->getOperatorLoc(), diag::warn_sizeof_array_decay)
+          << LHSICE->getSourceRange()
+          << LHSICE->getType() << LHSICE->getSubExpr()->getType();
+      else if (!LHSDecay && RHSDecay && BO->getType() == RHSICE->getType())
+        Diag(BO->getOperatorLoc(), diag::warn_sizeof_array_decay)
+          << RHSICE->getSourceRange()
+          << RHSICE->getType() << RHSICE->getSubExpr()->getType();
+    }
   }
 
   return false;
Index: test/Sema/expr-comma-c99.c
===================================================================
--- test/Sema/expr-comma-c99.c
+++ test/Sema/expr-comma-c99.c
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 %s -fsyntax-only -verify -std=c99
+// RUN: %clang_cc1 %s -fsyntax-only -verify -std=c99 -Wno-sizeof-array-decay
 // expected-no-diagnostics
 // rdar://6095180
 
Index: test/Sema/expr-comma.c
===================================================================
--- test/Sema/expr-comma.c
+++ test/Sema/expr-comma.c
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 %s -fsyntax-only -verify -std=c89
+// RUN: %clang_cc1 %s -fsyntax-only -verify -std=c89 -Wno-sizeof-array-decay
 // expected-no-diagnostics
 // rdar://6095180
 
Index: test/Sema/warn-sizeof-array-decay.c
===================================================================
--- /dev/null
+++ test/Sema/warn-sizeof-array-decay.c
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+void f(int x) {
+  char foo[10];
+  int bar[20];
+  char qux[30];
+
+  (void)sizeof(bar + 10); // expected-warning{{sizeof on pointer operation will return size of 'int *' instead of 'int [20]'}}
+  (void)sizeof(foo - 20); // expected-warning{{sizeof on pointer operation will return size of 'char *' instead of 'char [10]'}}
+  (void)sizeof(bar - x); // expected-warning{{sizeof on pointer operation will return size of 'int *' instead of 'int [20]'}}
+  (void)sizeof(foo + x); // expected-warning{{sizeof on pointer operation will return size of 'char *' instead of 'char [10]'}}
+
+  // This is ptrdiff_t.
+  (void)sizeof(foo - qux); // no-warning
+
+  (void)sizeof(foo, x); // no-warning
+  (void)sizeof(x, foo); // expected-warning{{sizeof on pointer operation will return size of 'char *' instead of 'char [10]'}}
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D571.3.patch
Type: text/x-patch
Size: 4643 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130324/ccb161c5/attachment.bin>


More information about the cfe-commits mailing list