]> granicus.if.org Git - clang/commitdiff
Warn on "void f(int a[10]) { sizeof(a); }"
authorNico Weber <nicolasweber@gmx.de>
Wed, 15 Jun 2011 02:47:03 +0000 (02:47 +0000)
committerNico Weber <nicolasweber@gmx.de>
Wed, 15 Jun 2011 02:47:03 +0000 (02:47 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@133036 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticGroups.td
include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaExpr.cpp
test/Sema/warn-sizeof-arrayarg.c [new file with mode: 0644]

index 9d2bf03862566680497bbdfa85a7396193e143be..98706dd227bd870616bf6355aebaf46c1ff0003c 100644 (file)
@@ -115,6 +115,7 @@ def SignCompare : DiagGroup<"sign-compare">;
 def : DiagGroup<"stack-protector">;
 def : DiagGroup<"switch-default">;
 def : DiagGroup<"synth">;
+def SizeofArrayArgument : DiagGroup<"sizeof-array-argument">;
 def TautologicalCompare : DiagGroup<"tautological-compare">;
 def HeaderHygiene : DiagGroup<"header-hygiene">;
 
@@ -248,6 +249,7 @@ def Most : DiagGroup<"most", [
     ReturnType,
     SelfAssignment,
     Switch,
+    SizeofArrayArgument,
     Trigraphs,
     Uninitialized,
     UnknownPragmas,
index 985f3e4443dc2964061e4ba1da8fcb22b2221a5b..ec23778494225dffb761266ae085930844c2f281 100644 (file)
@@ -2602,6 +2602,10 @@ def warn_self_assignment : Warning<
   "explicitly assigning a variable of type %0 to itself">,
   InGroup<SelfAssignment>, DefaultIgnore;
 
+def warn_sizeof_array_param : Warning<
+  "sizeof on array function parameter will return size of %0 instead of %1">,
+  InGroup<SizeofArrayArgument>;
+
 def err_sizeof_nonfragile_interface : Error<
   "invalid application of '%select{alignof|sizeof}1' to interface %0 in "
   "non-fragile ABI">;
index 9f9c05244e08122f086df7b6bf2b1dda6d5fd2f0..a2ad16145afabd3dc4ae161948c26895d4745c8e 100644 (file)
@@ -3160,6 +3160,20 @@ bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *Op,
                                        Op->getSourceRange(), ExprKind))
     return true;
 
+  if (ExprKind == UETT_SizeOf) {
+    if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(Op->IgnoreParens())) {
+      if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DeclRef->getFoundDecl())) {
+        QualType OType = PVD->getOriginalType();
+        QualType Type = PVD->getType();
+        if (Type->isPointerType() && OType->isArrayType()) {
+          Diag(Op->getExprLoc(), diag::warn_sizeof_array_param)
+            << Type << OType;
+          Diag(PVD->getLocation(), diag::note_declared_at);
+        }
+      }
+    }
+  }
+
   return false;
 }
 
diff --git a/test/Sema/warn-sizeof-arrayarg.c b/test/Sema/warn-sizeof-arrayarg.c
new file mode 100644 (file)
index 0000000..ba8a5fa
--- /dev/null
@@ -0,0 +1,30 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+typedef int Arr[10];
+
+typedef int trungl_int;
+
+void f(int a[10], Arr arr) {  // \
+// expected-note {{declared here}} \
+// expected-note {{declared here}} \
+// expected-note {{declared here}} \
+// expected-note {{declared here}}
+
+  /* Should warn. */
+  (void)sizeof(a);  // \
+      // expected-warning{{sizeof on array function parameter will return size of 'int *' instead of 'int [10]'}}
+  (void)sizeof((((a))));  // \
+      // expected-warning{{sizeof on array function parameter will return size of 'int *' instead of 'int [10]'}}
+  (void)sizeof a;  // \
+      // expected-warning{{sizeof on array function parameter will return size of 'int *' instead of 'int [10]'}}
+  (void)sizeof arr;  // \
+      // expected-warning{{sizeof on array function parameter will return size of 'int *' instead of 'Arr' (aka 'int [10]')}}
+
+  /* Shouldn't warn. */
+  int b[10];
+  (void)sizeof b;
+  Arr brr;
+  (void)sizeof brr;
+  (void)sizeof(Arr);
+  (void)sizeof(int);
+}