[cfe-commits] r133036 - in /cfe/trunk: include/clang/Basic/DiagnosticGroups.td include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExpr.cpp test/Sema/warn-sizeof-arrayarg.c
Nico Weber
nicolasweber at gmx.de
Tue Jun 14 19:47:04 PDT 2011
Author: nico
Date: Tue Jun 14 21:47:03 2011
New Revision: 133036
URL: http://llvm.org/viewvc/llvm-project?rev=133036&view=rev
Log:
Warn on "void f(int a[10]) { sizeof(a); }"
Added:
cfe/trunk/test/Sema/warn-sizeof-arrayarg.c
Modified:
cfe/trunk/include/clang/Basic/DiagnosticGroups.td
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/lib/Sema/SemaExpr.cpp
Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=133036&r1=133035&r2=133036&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Tue Jun 14 21:47:03 2011
@@ -115,6 +115,7 @@
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 @@
ReturnType,
SelfAssignment,
Switch,
+ SizeofArrayArgument,
Trigraphs,
Uninitialized,
UnknownPragmas,
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=133036&r1=133035&r2=133036&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Jun 14 21:47:03 2011
@@ -2602,6 +2602,10 @@
"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">;
Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=133036&r1=133035&r2=133036&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Tue Jun 14 21:47:03 2011
@@ -3160,6 +3160,20 @@
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;
}
Added: cfe/trunk/test/Sema/warn-sizeof-arrayarg.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-sizeof-arrayarg.c?rev=133036&view=auto
==============================================================================
--- cfe/trunk/test/Sema/warn-sizeof-arrayarg.c (added)
+++ cfe/trunk/test/Sema/warn-sizeof-arrayarg.c Tue Jun 14 21:47:03 2011
@@ -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);
+}
More information about the cfe-commits
mailing list