summaryrefslogtreecommitdiff
path: root/gcc/c
diff options
context:
space:
mode:
authormpolacek <mpolacek@138bc75d-0d04-0410-961f-82ee72b054a4>2014-07-06 19:00:10 +0000
committermpolacek <mpolacek@138bc75d-0d04-0410-961f-82ee72b054a4>2014-07-06 19:00:10 +0000
commit87d59e72dfe85065aa3fdefdd01dd538292392ea (patch)
treedbe62f51aeeeb3f4e0e71eea55e603f187e0fdbf /gcc/c
parent36cd680eeb9e824a38efc71f8c3d1af82e971eb1 (diff)
downloadgcc-87d59e72dfe85065aa3fdefdd01dd538292392ea.tar.gz
PR c/6940
* doc/invoke.texi: Document -Wsizeof-array-argument. c-family/ * c.opt (Wsizeof-array-argument): New option. c/ * c-decl.c (grokdeclarator): Set C_ARRAY_PARAMETER. * c-tree.h (C_ARRAY_PARAMETER): Define. * c-typeck.c (c_expr_sizeof_expr): Warn when using sizeof on an array function parameter. cp/ * cp-tree.h (DECL_ARRAY_PARAMETER_P): Define. * decl.c (grokdeclarator): Set DECL_ARRAY_PARAMETER_P. * typeck.c (cxx_sizeof_expr): Warn when using sizeof on an array function parameter. testsuite/ * c-c++-common/Wsizeof-pointer-memaccess1.c: Use -Wno-sizeof-array-argument. * c-c++-common/Wsizeof-pointer-memaccess2.c: Likewise. * g++.dg/warn/Wsizeof-pointer-memaccess-1.C: Likewise. * gcc.dg/Wsizeof-pointer-memaccess1.c: Likewise. * g++.dg/torture/Wsizeof-pointer-memaccess1.C: Likewise. * g++.dg/torture/Wsizeof-pointer-memaccess2.C: Likewise. * gcc.dg/torture/Wsizeof-pointer-memaccess1.c: Likewise. * c-c++-common/sizeof-array-argument.c: New test. * gcc.dg/vla-5.c: Add dg-warnings. ../libgomp/ * testsuite/libgomp.c/appendix-a/a.29.1.c (f): Add dg-warnings. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@212312 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc/c')
-rw-r--r--gcc/c/ChangeLog8
-rw-r--r--gcc/c/c-decl.c3
-rw-r--r--gcc/c/c-tree.h3
-rw-r--r--gcc/c/c-typeck.c10
4 files changed, 24 insertions, 0 deletions
diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog
index 68abca4dfb1..80ad172c596 100644
--- a/gcc/c/ChangeLog
+++ b/gcc/c/ChangeLog
@@ -1,3 +1,11 @@
+2014-07-06 Marek Polacek <polacek@redhat.com>
+
+ PR c/6940
+ * c-decl.c (grokdeclarator): Set C_ARRAY_PARAMETER.
+ * c-tree.h (C_ARRAY_PARAMETER): Define.
+ * c-typeck.c (c_expr_sizeof_expr): Warn when using sizeof on an array
+ function parameter.
+
2014-07-02 Jan Hubicka <hubicka@ucw.cz>
Chen Gang <gang.chen.5i5j@gmail.com>
diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c
index 3dec90b23ed..0ca2e0d58f4 100644
--- a/gcc/c/c-decl.c
+++ b/gcc/c/c-decl.c
@@ -6103,6 +6103,7 @@ grokdeclarator (const struct c_declarator *declarator,
if (decl_context == PARM)
{
tree promoted_type;
+ bool array_parameter_p = false;
/* A parameter declared as an array of T is really a pointer to T.
One declared as a function is really a pointer to a function. */
@@ -6124,6 +6125,7 @@ grokdeclarator (const struct c_declarator *declarator,
"attributes in parameter array declarator ignored");
size_varies = false;
+ array_parameter_p = true;
}
else if (TREE_CODE (type) == FUNCTION_TYPE)
{
@@ -6148,6 +6150,7 @@ grokdeclarator (const struct c_declarator *declarator,
PARM_DECL, declarator->u.id, type);
if (size_varies)
C_DECL_VARIABLE_SIZE (decl) = 1;
+ C_ARRAY_PARAMETER (decl) = array_parameter_p;
/* Compute the type actually passed in the parmlist,
for the case where there is no prototype.
diff --git a/gcc/c/c-tree.h b/gcc/c/c-tree.h
index 133930f4a09..f97d0d5ba9b 100644
--- a/gcc/c/c-tree.h
+++ b/gcc/c/c-tree.h
@@ -66,6 +66,9 @@ along with GCC; see the file COPYING3. If not see
/* For a FUNCTION_DECL, nonzero if it was an implicit declaration. */
#define C_DECL_IMPLICIT(EXP) DECL_LANG_FLAG_2 (EXP)
+/* For a PARM_DECL, nonzero if it was declared as an array. */
+#define C_ARRAY_PARAMETER(NODE) DECL_LANG_FLAG_0 (NODE)
+
/* For FUNCTION_DECLs, evaluates true if the decl is built-in but has
been declared. */
#define C_DECL_DECLARED_BUILTIN(EXP) \
diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c
index 35bfd149876..06fd565f770 100644
--- a/gcc/c/c-typeck.c
+++ b/gcc/c/c-typeck.c
@@ -2731,6 +2731,16 @@ c_expr_sizeof_expr (location_t loc, struct c_expr expr)
else
{
bool expr_const_operands = true;
+
+ if (TREE_CODE (expr.value) == PARM_DECL
+ && C_ARRAY_PARAMETER (expr.value))
+ {
+ if (warning_at (loc, OPT_Wsizeof_array_argument,
+ "%<sizeof%> on array function parameter %qE will "
+ "return size of %qT", expr.value,
+ expr.original_type))
+ inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
+ }
tree folded_expr = c_fully_fold (expr.value, require_constant_value,
&expr_const_operands);
ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));