mirror of
git://gcc.gnu.org/git/gcc.git
synced 2025-03-26 09:50:40 +08:00
re PR c/6940 (taking sizeof array parameter should trigger a warning)
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. From-SVN: r212312
This commit is contained in:
parent
dc044bfb72
commit
773ec47fe6
@ -1,3 +1,8 @@
|
||||
2014-07-06 Marek Polacek <polacek@redhat.com>
|
||||
|
||||
PR c/6940
|
||||
* doc/invoke.texi: Document -Wsizeof-array-argument.
|
||||
|
||||
2014-07-05 Gerald Pfeifer <gerald@pfeifer.com>
|
||||
|
||||
* wide-int.h (wide_int_storage): Change declaration from struct
|
||||
|
@ -1,3 +1,8 @@
|
||||
2014-07-06 Marek Polacek <polacek@redhat.com>
|
||||
|
||||
PR c/6940
|
||||
* c.opt (Wsizeof-array-argument): New option.
|
||||
|
||||
2014-07-03 Jakub Jelinek <jakub@redhat.com>
|
||||
|
||||
* c-ada-spec.c (dump_ada_nodes): Don't call qsort if
|
||||
|
@ -534,6 +534,10 @@ Wsizeof-pointer-memaccess
|
||||
C ObjC C++ ObjC++ Var(warn_sizeof_pointer_memaccess) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall)
|
||||
Warn about suspicious length parameters to certain string functions if the argument uses sizeof
|
||||
|
||||
Wsizeof-array-argument
|
||||
C ObjC C++ ObjC++ Var(warn_sizeof_array_argument) Warning Init(1)
|
||||
Warn when sizeof is applied on a parameter declared as an array
|
||||
|
||||
Wsuggest-attribute=format
|
||||
C ObjC C++ ObjC++ Var(warn_suggest_attribute_format) Warning
|
||||
Warn about functions which might be candidates for format attributes
|
||||
|
@ -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>
|
||||
|
||||
|
@ -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.
|
||||
|
@ -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) \
|
||||
|
@ -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));
|
||||
|
@ -1,3 +1,11 @@
|
||||
2014-07-06 Marek Polacek <polacek@redhat.com>
|
||||
|
||||
PR c/6940
|
||||
* 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.
|
||||
|
||||
2014-07-02 Paolo Carlini <paolo.carlini@oracle.com>
|
||||
|
||||
* pt.c (convert_template_argument): Use inform instead of error in
|
||||
|
@ -146,6 +146,7 @@ c-common.h, not after.
|
||||
DECL_MEMBER_TEMPLATE_P (in TEMPLATE_DECL)
|
||||
USING_DECL_TYPENAME_P (in USING_DECL)
|
||||
DECL_VLA_CAPTURE_P (in FIELD_DECL)
|
||||
DECL_ARRAY_PARAMETER_P (in PARM_DECL)
|
||||
2: DECL_THIS_EXTERN (in VAR_DECL or FUNCTION_DECL).
|
||||
DECL_IMPLICIT_TYPEDEF_P (in a TYPE_DECL)
|
||||
3: DECL_IN_AGGR_P.
|
||||
@ -3681,6 +3682,11 @@ more_aggr_init_expr_args_p (const aggr_init_expr_arg_iterator *iter)
|
||||
#define DECL_VLA_CAPTURE_P(NODE) \
|
||||
DECL_LANG_FLAG_1 (FIELD_DECL_CHECK (NODE))
|
||||
|
||||
/* Nonzero for PARM_DECL node means that this is an array function
|
||||
parameter, i.e, a[] rather than *a. */
|
||||
#define DECL_ARRAY_PARAMETER_P(NODE) \
|
||||
DECL_LANG_FLAG_1 (PARM_DECL_CHECK (NODE))
|
||||
|
||||
/* Nonzero for FIELD_DECL node means that this field is a base class
|
||||
of the parent object, as opposed to a member field. */
|
||||
#define DECL_FIELD_IS_BASE(NODE) \
|
||||
|
@ -8816,6 +8816,7 @@ grokdeclarator (const cp_declarator *declarator,
|
||||
bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
|
||||
bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
|
||||
bool late_return_type_p = false;
|
||||
bool array_parameter_p = false;
|
||||
source_location saved_loc = input_location;
|
||||
const char *errmsg;
|
||||
|
||||
@ -10454,6 +10455,7 @@ grokdeclarator (const cp_declarator *declarator,
|
||||
/* Transfer const-ness of array into that of type pointed to. */
|
||||
type = build_pointer_type (TREE_TYPE (type));
|
||||
type_quals = TYPE_UNQUALIFIED;
|
||||
array_parameter_p = true;
|
||||
}
|
||||
else if (TREE_CODE (type) == FUNCTION_TYPE)
|
||||
type = build_pointer_type (type);
|
||||
@ -10474,6 +10476,7 @@ grokdeclarator (const cp_declarator *declarator,
|
||||
if (decl_context == PARM)
|
||||
{
|
||||
decl = cp_build_parm_decl (unqualified_id, type);
|
||||
DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
|
||||
|
||||
bad_specifiers (decl, BSP_PARM, virtualp,
|
||||
memfn_quals != TYPE_UNQUALIFIED,
|
||||
|
@ -1614,6 +1614,15 @@ cxx_sizeof_expr (tree e, tsubst_flags_t complain)
|
||||
&& DECL_TEMPLATE_INSTANTIATION (e))
|
||||
instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
|
||||
|
||||
if (TREE_CODE (e) == PARM_DECL
|
||||
&& DECL_ARRAY_PARAMETER_P (e)
|
||||
&& (complain & tf_warning))
|
||||
{
|
||||
if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
|
||||
"parameter %qE will return size of %qT", e, TREE_TYPE (e)))
|
||||
inform (DECL_SOURCE_LOCATION (e), "declared here");
|
||||
}
|
||||
|
||||
e = mark_type_use (e);
|
||||
|
||||
if (TREE_CODE (e) == COMPONENT_REF
|
||||
|
@ -266,7 +266,7 @@ Objective-C and Objective-C++ Dialects}.
|
||||
-Wredundant-decls -Wno-return-local-addr @gol
|
||||
-Wreturn-type -Wsequence-point -Wshadow -Wno-shadow-ivar @gol
|
||||
-Wsign-compare -Wsign-conversion -Wfloat-conversion @gol
|
||||
-Wsizeof-pointer-memaccess @gol
|
||||
-Wsizeof-pointer-memaccess -Wsizeof-array-argument @gol
|
||||
-Wstack-protector -Wstack-usage=@var{len} -Wstrict-aliasing @gol
|
||||
-Wstrict-aliasing=n @gol -Wstrict-overflow -Wstrict-overflow=@var{n} @gol
|
||||
-Wsuggest-attribute=@r{[}pure@r{|}const@r{|}noreturn@r{|}format@r{]} @gol
|
||||
@ -4676,6 +4676,13 @@ but a pointer, and suggests a possible fix, or about
|
||||
@code{memcpy (&foo, ptr, sizeof (&foo));}. This warning is enabled by
|
||||
@option{-Wall}.
|
||||
|
||||
@item -Wsizeof-array-argument
|
||||
@opindex Wsizeof-array-argument
|
||||
@opindex Wno-sizeof-array-argument
|
||||
Warn when the @code{sizeof} operator is applied to a parameter that is
|
||||
declared as an array in a function definition. This warning is enabled by
|
||||
default for C and C++ programs.
|
||||
|
||||
@item -Waddress
|
||||
@opindex Waddress
|
||||
@opindex Wno-address
|
||||
|
@ -1,3 +1,17 @@
|
||||
2014-07-06 Marek Polacek <polacek@redhat.com>
|
||||
|
||||
PR c/6940
|
||||
* 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.
|
||||
|
||||
2014-07-05 Jan Hubicka <hubicka@ucw.cz>
|
||||
|
||||
* g++.dg/ipa/devirt-26.C: Update testcase.
|
||||
|
@ -1,6 +1,6 @@
|
||||
/* Test -Wsizeof-pointer-memaccess warnings. */
|
||||
/* { dg-do compile } */
|
||||
/* { dg-options "-Wall" } */
|
||||
/* { dg-options "-Wall -Wno-sizeof-array-argument" } */
|
||||
|
||||
typedef __SIZE_TYPE__ size_t;
|
||||
#ifdef __cplusplus
|
||||
|
@ -1,6 +1,6 @@
|
||||
/* Test -Wsizeof-pointer-memaccess warnings. */
|
||||
/* { dg-do compile } */
|
||||
/* { dg-options "-Wall -O2" } */
|
||||
/* { dg-options "-Wall -O2 -Wno-sizeof-array-argument" } */
|
||||
|
||||
#define bos(ptr) __builtin_object_size (ptr, 1)
|
||||
#define bos0(ptr) __builtin_object_size (ptr, 0)
|
||||
|
100
gcc/testsuite/c-c++-common/sizeof-array-argument.c
Normal file
100
gcc/testsuite/c-c++-common/sizeof-array-argument.c
Normal file
@ -0,0 +1,100 @@
|
||||
/* PR c/6940 */
|
||||
/* { dg-do compile } */
|
||||
|
||||
/* Test -Wsizeof-array-argument warning. */
|
||||
|
||||
typedef int T[2][2];
|
||||
|
||||
int
|
||||
fn1 (int a[])
|
||||
{
|
||||
return sizeof a; /* { dg-warning "on array function parameter" } */
|
||||
}
|
||||
|
||||
int
|
||||
fn2 (int x, int b[3])
|
||||
{
|
||||
return x + sizeof b; /* { dg-warning "on array function parameter" } */
|
||||
}
|
||||
|
||||
int
|
||||
fn3 (int *p)
|
||||
{
|
||||
return sizeof p;
|
||||
}
|
||||
|
||||
int fn4 (int *p);
|
||||
int
|
||||
fn4 (int p[])
|
||||
{
|
||||
return sizeof p; /* { dg-warning "on array function parameter" } */
|
||||
}
|
||||
|
||||
int fn5 (int x[]);
|
||||
int
|
||||
fn5 (int *x)
|
||||
{
|
||||
return sizeof x;
|
||||
}
|
||||
|
||||
#ifndef __cplusplus
|
||||
/* C++ doesn't know VLA unspec. */
|
||||
int fn6 (int x[*]);
|
||||
int
|
||||
fn6 (int x[])
|
||||
{
|
||||
return sizeof x; /* { dg-warning "on array function parameter" "" { target c } } */
|
||||
}
|
||||
#endif
|
||||
|
||||
int
|
||||
fn7 (int x[][2])
|
||||
{
|
||||
return sizeof x; /* { dg-warning "on array function parameter" } */
|
||||
}
|
||||
|
||||
int
|
||||
fn8 (char *x[])
|
||||
{
|
||||
return sizeof x; /* { dg-warning "on array function parameter" } */
|
||||
}
|
||||
|
||||
int
|
||||
fn9 (char **x)
|
||||
{
|
||||
return sizeof x;
|
||||
}
|
||||
|
||||
#ifndef __cplusplus
|
||||
int
|
||||
fn10 (int a, char x[static sizeof a])
|
||||
{
|
||||
return sizeof x; /* { dg-warning "on array function parameter" "" { target c } } */
|
||||
}
|
||||
|
||||
int
|
||||
fn11 (a)
|
||||
char a[];
|
||||
{
|
||||
return sizeof a; /* { dg-warning "on array function parameter" "" { target c } } */
|
||||
}
|
||||
|
||||
int
|
||||
fn12 (a)
|
||||
char *a;
|
||||
{
|
||||
return sizeof a;
|
||||
}
|
||||
#endif
|
||||
|
||||
int
|
||||
fn13 (char (*x)[2])
|
||||
{
|
||||
return sizeof x;
|
||||
}
|
||||
|
||||
int
|
||||
fn14 (T t)
|
||||
{
|
||||
return sizeof t; /* { dg-warning "on array function parameter" } */
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
// Test -Wsizeof-pointer-memaccess warnings.
|
||||
// { dg-do compile }
|
||||
// { dg-options "-Wall" }
|
||||
// { dg-options "-Wall -Wno-sizeof-array-argument" }
|
||||
// Test just twice, once with -O0 non-fortified, once with -O2 fortified.
|
||||
// { dg-skip-if "" { *-*-* } { "*" } { "-O0" "-O2" } }
|
||||
// { dg-skip-if "" { *-*-* } { "-flto" } { "" } }
|
||||
|
@ -1,6 +1,6 @@
|
||||
// Test -Wsizeof-pointer-memaccess warnings.
|
||||
// { dg-do compile }
|
||||
// { dg-options "-Wall" }
|
||||
// { dg-options "-Wall -Wno-sizeof-array-argument" }
|
||||
// Test just twice, once with -O0 non-fortified, once with -O2 fortified.
|
||||
// { dg-skip-if "" { *-*-* } { "*" } { "-O0" "-O2" } }
|
||||
// { dg-skip-if "" { *-*-* } { "-flto" } { "" } }
|
||||
|
@ -1,6 +1,6 @@
|
||||
// Test -Wsizeof-pointer-memaccess warnings.
|
||||
// { dg-do compile }
|
||||
// { dg-options "-Wall" }
|
||||
// { dg-options "-Wall -Wno-sizeof-array-argument" }
|
||||
|
||||
typedef __SIZE_TYPE__ size_t;
|
||||
extern "C" void *memset (void *, int, size_t);
|
||||
|
@ -1,6 +1,6 @@
|
||||
/* Test -Wsizeof-pointer-memaccess warnings. */
|
||||
/* { dg-do compile } */
|
||||
/* { dg-options "-Wall" } */
|
||||
/* { dg-options "-Wall -Wno-sizeof-array-argument" } */
|
||||
|
||||
typedef __SIZE_TYPE__ size_t;
|
||||
extern void bzero (void *, size_t);
|
||||
|
@ -1,6 +1,6 @@
|
||||
/* Test -Wsizeof-pointer-memaccess warnings. */
|
||||
/* { dg-do compile } */
|
||||
/* { dg-options "-Wall" } */
|
||||
/* { dg-options "-Wall -Wno-sizeof-array-argument" } */
|
||||
/* Test just twice, once with -O0 non-fortified, once with -O2 fortified. */
|
||||
/* { dg-skip-if "" { *-*-* } { "*" } { "-O0" "-O2" } } */
|
||||
/* { dg-skip-if "" { *-*-* } { "-flto" } { "" } } */
|
||||
|
@ -13,12 +13,12 @@ void foo4(int j, int a[j]) {
|
||||
|
||||
int foo5(int a, int b[*][*], int c[static sizeof(*b)]);
|
||||
int foo5(int a, int b[10][10], int c[400]) {
|
||||
return sizeof (c);
|
||||
return sizeof (c); /* { dg-warning "on array function parameter" } */
|
||||
}
|
||||
|
||||
int foo6(int a, int b[*][*], int c[static sizeof(*b)]);
|
||||
int foo6(int a, int b[a][a], int c[sizeof(*b)]) {
|
||||
return sizeof (c);
|
||||
return sizeof (c); /* { dg-warning "on array function parameter" } */
|
||||
}
|
||||
|
||||
void foo7(__typeof__ (int (*)(int o[*])) i);
|
||||
|
@ -1,3 +1,8 @@
|
||||
2014-07-06 Marek Polacek <polacek@redhat.com>
|
||||
|
||||
PR c/6940
|
||||
* testsuite/libgomp.c/appendix-a/a.29.1.c (f): Add dg-warnings.
|
||||
|
||||
2014-07-03 Jakub Jelinek <jakub@redhat.com>
|
||||
|
||||
* testsuite/lib/libgomp.exp (libgomp_target_compile): If $source
|
||||
|
@ -11,8 +11,8 @@ f (int n, int B[n][n], int C[])
|
||||
E[1][1] = 4;
|
||||
#pragma omp parallel firstprivate(B, C, D, E)
|
||||
{
|
||||
assert (sizeof (B) == sizeof (int (*)[n]));
|
||||
assert (sizeof (C) == sizeof (int *));
|
||||
assert (sizeof (B) == sizeof (int (*)[n])); /* { dg-warning "on array function parameter" } */
|
||||
assert (sizeof (C) == sizeof (int *)); /* { dg-warning "on array function parameter" } */
|
||||
assert (sizeof (D) == 4 * sizeof (int));
|
||||
assert (sizeof (E) == n * n * sizeof (int));
|
||||
/* Private B and C have values of original B and C. */
|
||||
|
Loading…
x
Reference in New Issue
Block a user