c-common.c (match_case_to_enum_1): Add appropriate OPT_W* parameter to warning.

2008-02-26  Manuel Lopez-Ibanez <manu@gcc.gnu.org>

	* c-common.c (match_case_to_enum_1): Add appropriate
	OPT_W* parameter to warning.
	(c_do_switch_warnings): Likewise.
	* c-typeck.c (warning_init): Add one more parameter following
	'warning' function.
	(push_init_level): Update call to warning_init.
	(pop_init_level): Likewise.
	(add_pending_init): Likewise.
	(output_init_element: Likewise.
cp/
	* typeck.c (build_class_member_access_expr): Add appropriate
	OPT_W* parameter to warning.
	(build_reinterpret_cast_1): Likewise.
	* name-lookup.c (push_overloaded_decl): Likewise.
testsuite/
	* gcc.dg/Wswitch-enum-error.c: New.
	* gcc.dg/Wswitch-error.c: New.
	* gcc.dg/20011021-1.c: Use two directives to match two different
	messages.

From-SVN: r132649
parent 369dcbd9
2008-02-26 Manuel Lopez-Ibanez <manu@gcc.gnu.org>
* c-common.c (match_case_to_enum_1): Add appropriate
OPT_W* parameter to warning.
(c_do_switch_warnings): Likewise.
* c-typeck.c (warning_init): Add one more parameter following
'warning' function.
(push_init_level): Update call to warning_init.
(pop_init_level): Likewise.
(add_pending_init): Likewise.
(output_init_element: Likewise.
2008-02-26 Manuel Lopez-Ibanez <manu@gcc.gnu.org> 2008-02-26 Manuel Lopez-Ibanez <manu@gcc.gnu.org>
PR 28322 PR 28322
......
...@@ -4329,10 +4329,12 @@ match_case_to_enum_1 (tree key, tree type, tree label) ...@@ -4329,10 +4329,12 @@ match_case_to_enum_1 (tree key, tree type, tree label)
TREE_INT_CST_HIGH (key), TREE_INT_CST_LOW (key)); TREE_INT_CST_HIGH (key), TREE_INT_CST_LOW (key));
if (TYPE_NAME (type) == 0) if (TYPE_NAME (type) == 0)
warning (0, "%Jcase value %qs not in enumerated type", warning (warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
"%Jcase value %qs not in enumerated type",
CASE_LABEL (label), buf); CASE_LABEL (label), buf);
else else
warning (0, "%Jcase value %qs not in enumerated type %qT", warning (warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
"%Jcase value %qs not in enumerated type %qT",
CASE_LABEL (label), buf, type); CASE_LABEL (label), buf, type);
} }
...@@ -4384,6 +4386,7 @@ c_do_switch_warnings (splay_tree cases, location_t switch_location, ...@@ -4384,6 +4386,7 @@ c_do_switch_warnings (splay_tree cases, location_t switch_location,
splay_tree_node default_node; splay_tree_node default_node;
splay_tree_node node; splay_tree_node node;
tree chain; tree chain;
int saved_warn_switch;
if (!warn_switch && !warn_switch_enum && !warn_switch_default) if (!warn_switch && !warn_switch_enum && !warn_switch_default)
return; return;
...@@ -4453,7 +4456,13 @@ c_do_switch_warnings (splay_tree cases, location_t switch_location, ...@@ -4453,7 +4456,13 @@ c_do_switch_warnings (splay_tree cases, location_t switch_location,
if (cond && tree_int_cst_compare (cond, value)) if (cond && tree_int_cst_compare (cond, value))
continue; continue;
warning (0, "%Henumeration value %qE not handled in switch", /* If there is a default_node, the only relevant option is
Wswitch-enum. Otherwise, if both are enabled then we prefer
to warn using -Wswitch because -Wswitch is enabled by -Wall
while -Wswitch-enum is explicit. */
warning ((default_node || !warn_switch)
? OPT_Wswitch_enum : OPT_Wswitch,
"%Henumeration value %qE not handled in switch",
&switch_location, TREE_PURPOSE (chain)); &switch_location, TREE_PURPOSE (chain));
} }
...@@ -4465,7 +4474,17 @@ c_do_switch_warnings (splay_tree cases, location_t switch_location, ...@@ -4465,7 +4474,17 @@ c_do_switch_warnings (splay_tree cases, location_t switch_location,
we should have marked both the lower bound and upper bound of we should have marked both the lower bound and upper bound of
every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
above. This scan also resets those fields. */ above. This scan also resets those fields. */
/* If there is a default_node, the only relevant option is
Wswitch-enum. Otherwise, if both are enabled then we prefer
to warn using -Wswitch because -Wswitch is enabled by -Wall
while -Wswitch-enum is explicit. */
saved_warn_switch = warn_switch;
if (default_node)
warn_switch = 0;
splay_tree_foreach (cases, match_case_to_enum, type); splay_tree_foreach (cases, match_case_to_enum, type);
warn_switch = saved_warn_switch;
} }
/* Finish an expression taking the address of LABEL (an /* Finish an expression taking the address of LABEL (an
......
...@@ -91,7 +91,7 @@ static void push_string (const char *); ...@@ -91,7 +91,7 @@ static void push_string (const char *);
static void push_member_name (tree); static void push_member_name (tree);
static int spelling_length (void); static int spelling_length (void);
static char *print_spelling (char *); static char *print_spelling (char *);
static void warning_init (const char *); static void warning_init (int, const char *);
static tree digest_init (tree, tree, bool, int); static tree digest_init (tree, tree, bool, int);
static void output_init_element (tree, bool, tree, tree, int); static void output_init_element (tree, bool, tree, tree, int);
static void output_pending_init_elements (int); static void output_pending_init_elements (int);
...@@ -4636,19 +4636,21 @@ pedwarn_init (const char *msgid) ...@@ -4636,19 +4636,21 @@ pedwarn_init (const char *msgid)
pedwarn ("(near initialization for %qs)", ofwhat); pedwarn ("(near initialization for %qs)", ofwhat);
} }
/* Issue a warning for a bad initializer component. /* Issue a warning for a bad initializer component.
MSGID identifies the message.
The component name is taken from the spelling stack. */ OPT is the OPT_W* value corresponding to the warning option that
controls this warning. MSGID identifies the message. The
component name is taken from the spelling stack. */
static void static void
warning_init (const char *msgid) warning_init (int opt, const char *msgid)
{ {
char *ofwhat; char *ofwhat;
warning (0, "%s", _(msgid)); warning (opt, "%s", _(msgid));
ofwhat = print_spelling ((char *) alloca (spelling_length () + 1)); ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
if (*ofwhat) if (*ofwhat)
warning (0, "(near initialization for %qs)", ofwhat); warning (opt, "(near initialization for %qs)", ofwhat);
} }
/* If TYPE is an array type and EXPR is a parenthesized string /* If TYPE is an array type and EXPR is a parenthesized string
...@@ -5415,7 +5417,7 @@ push_init_level (int implicit) ...@@ -5415,7 +5417,7 @@ push_init_level (int implicit)
if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned) if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
{ {
missing_braces_mentioned = 1; missing_braces_mentioned = 1;
warning_init ("missing braces around initializer"); warning_init (OPT_Wmissing_braces, "missing braces around initializer");
} }
if (TREE_CODE (constructor_type) == RECORD_TYPE if (TREE_CODE (constructor_type) == RECORD_TYPE
...@@ -5476,7 +5478,7 @@ push_init_level (int implicit) ...@@ -5476,7 +5478,7 @@ push_init_level (int implicit)
else else
{ {
if (constructor_type != error_mark_node) if (constructor_type != error_mark_node)
warning_init ("braces around scalar initializer"); warning_init (0, "braces around scalar initializer");
constructor_fields = constructor_type; constructor_fields = constructor_type;
constructor_unfilled_fields = constructor_type; constructor_unfilled_fields = constructor_type;
} }
...@@ -5562,7 +5564,8 @@ pop_init_level (int implicit) ...@@ -5562,7 +5564,8 @@ pop_init_level (int implicit)
if (constructor_unfilled_fields && !constructor_designated) if (constructor_unfilled_fields && !constructor_designated)
{ {
push_member_name (constructor_unfilled_fields); push_member_name (constructor_unfilled_fields);
warning_init ("missing initializer"); warning_init (OPT_Wmissing_field_initializers,
"missing initializer");
RESTORE_SPELLING_DEPTH (constructor_depth); RESTORE_SPELLING_DEPTH (constructor_depth);
} }
} }
...@@ -5846,9 +5849,9 @@ add_pending_init (tree purpose, tree value) ...@@ -5846,9 +5849,9 @@ add_pending_init (tree purpose, tree value)
else else
{ {
if (TREE_SIDE_EFFECTS (p->value)) if (TREE_SIDE_EFFECTS (p->value))
warning_init ("initialized field with side-effects overwritten"); warning_init (0, "initialized field with side-effects overwritten");
else if (warn_override_init) else if (warn_override_init)
warning_init ("initialized field overwritten"); warning_init (OPT_Woverride_init, "initialized field overwritten");
p->value = value; p->value = value;
return; return;
} }
...@@ -5869,9 +5872,9 @@ add_pending_init (tree purpose, tree value) ...@@ -5869,9 +5872,9 @@ add_pending_init (tree purpose, tree value)
else else
{ {
if (TREE_SIDE_EFFECTS (p->value)) if (TREE_SIDE_EFFECTS (p->value))
warning_init ("initialized field with side-effects overwritten"); warning_init (0, "initialized field with side-effects overwritten");
else if (warn_override_init) else if (warn_override_init)
warning_init ("initialized field overwritten"); warning_init (OPT_Woverride_init, "initialized field overwritten");
p->value = value; p->value = value;
return; return;
} }
...@@ -6343,9 +6346,9 @@ output_init_element (tree value, bool strict_string, tree type, tree field, ...@@ -6343,9 +6346,9 @@ output_init_element (tree value, bool strict_string, tree type, tree field,
{ {
if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt, if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
constructor_elements)->value)) constructor_elements)->value))
warning_init ("initialized field with side-effects overwritten"); warning_init (0, "initialized field with side-effects overwritten");
else if (warn_override_init) else if (warn_override_init)
warning_init ("initialized field overwritten"); warning_init (OPT_Woverride_init, "initialized field overwritten");
/* We can have just one union field set. */ /* We can have just one union field set. */
constructor_elements = 0; constructor_elements = 0;
......
2008-02-26 Manuel Lopez-Ibanez <manu@gcc.gnu.org>
* typeck.c (build_class_member_access_expr): Add appropriate
OPT_W* parameter to warning.
(build_reinterpret_cast_1): Likewise.
* name-lookup.c (push_overloaded_decl): Likewise.
2008-02-25 Paolo Carlini <pcarlini@suse.de> 2008-02-25 Paolo Carlini <pcarlini@suse.de>
PR c++/35333 PR c++/35333
......
...@@ -1919,7 +1919,7 @@ push_overloaded_decl (tree decl, int flags, bool is_friend) ...@@ -1919,7 +1919,7 @@ push_overloaded_decl (tree decl, int flags, bool is_friend)
if (IS_AGGR_TYPE (t) && warn_shadow if (IS_AGGR_TYPE (t) && warn_shadow
&& (! DECL_IN_SYSTEM_HEADER (decl) && (! DECL_IN_SYSTEM_HEADER (decl)
|| ! DECL_IN_SYSTEM_HEADER (old))) || ! DECL_IN_SYSTEM_HEADER (old)))
warning (0, "%q#D hides constructor for %q#T", decl, t); warning (OPT_Wshadow, "%q#D hides constructor for %q#T", decl, t);
old = NULL_TREE; old = NULL_TREE;
} }
else if (is_overloaded_fn (old)) else if (is_overloaded_fn (old))
......
...@@ -1934,9 +1934,11 @@ build_class_member_access_expr (tree object, tree member, ...@@ -1934,9 +1934,11 @@ build_class_member_access_expr (tree object, tree member,
&& !DECL_FIELD_IS_BASE (member) && !DECL_FIELD_IS_BASE (member)
&& !skip_evaluation) && !skip_evaluation)
{ {
warning (0, "invalid access to non-static data member %qD of NULL object", warning (OPT_Winvalid_offsetof,
member); "invalid access to non-static data member %qD "
warning (0, "(perhaps the %<offsetof%> macro was used incorrectly)"); " of NULL object", member);
warning (OPT_Winvalid_offsetof,
"(perhaps the %<offsetof%> macro was used incorrectly)");
} }
/* If MEMBER is from an anonymous aggregate, we have converted /* If MEMBER is from an anonymous aggregate, we have converted
...@@ -5337,9 +5339,8 @@ build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p, ...@@ -5337,9 +5339,8 @@ build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
&& COMPLETE_TYPE_P (TREE_TYPE (type)) && COMPLETE_TYPE_P (TREE_TYPE (type))
&& COMPLETE_TYPE_P (TREE_TYPE (intype)) && COMPLETE_TYPE_P (TREE_TYPE (intype))
&& TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype))) && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
warning (0, "cast from %qT to %qT increases required alignment of " warning (OPT_Wcast_align, "cast from %qT to %qT "
"target type", "increases required alignment of target type", intype, type);
intype, type);
/* We need to strip nops here, because the front end likes to /* We need to strip nops here, because the front end likes to
create (int *)&a for array-to-pointer decay, instead of &a[0]. */ create (int *)&a for array-to-pointer decay, instead of &a[0]. */
......
2008-02-26 Manuel Lopez-Ibanez <manu@gcc.gnu.org> 2008-02-26 Manuel Lopez-Ibanez <manu@gcc.gnu.org>
* gcc.dg/Wswitch-enum-error.c: New.
* gcc.dg/Wswitch-error.c: New.
* gcc.dg/20011021-1.c: Use two directives to match two different
messages.
2008-02-26 Manuel Lopez-Ibanez <manu@gcc.gnu.org>
PR 28322 PR 28322
* gcc.dg/pr28322.c: New. * gcc.dg/pr28322.c: New.
...@@ -27,7 +27,8 @@ struct multilevel ...@@ -27,7 +27,8 @@ struct multilevel
char *f; char *f;
}; };
struct t T0 = { 1 }; /* { dg-warning "(missing|near) init" } */ struct t T0 = { 1 }; /* { dg-warning "missing init" } */
/* { dg-warning "near init" "near init" { target *-*-* } 30 } */
struct t T1 = { .a = 1 }; /* { dg-bogus "(missing|near) init" } */ struct t T1 = { .a = 1 }; /* { dg-bogus "(missing|near) init" } */
union u U0 = { 1 }; /* { dg-warning "initialization of union" } */ union u U0 = { 1 }; /* { dg-warning "initialization of union" } */
...@@ -36,8 +37,9 @@ union u U1 = { .i = 1 }; /* { dg-bogus "initialization of union" } */ ...@@ -36,8 +37,9 @@ union u U1 = { .i = 1 }; /* { dg-bogus "initialization of union" } */
struct multilevel M = struct multilevel M =
{ {
12, 12,
{ .b = 3 }, /* { dg-bogus "(missing|near) init" } */ { .b = 3 }, /* { dg-bogus "missing init" } */
{ 4 }, /* { dg-warning "initialization of union" } */ { 4 }, /* { dg-warning "initialization of union" } */
{ .n = 9 }, /* { dg-bogus "initialization of union" } */ { .n = 9 }, /* { dg-bogus "initialization of union" } */
/* "string here" */ /* "string here" */
}; /* { dg-warning "(missing|near) init" } */ }; /* { dg-warning "missing init" } */
/* { dg-warning "near init" "near init" { target *-*-* } 44 } */
/* { dg-do compile } */
/* { dg-options "-Werror=switch-enum -Wswitch" } */
enum e { e1, e2 };
int
foo (int i, int j, enum e ei, enum e ej, enum e ek, enum e el,
enum e em, enum e en, enum e eo, enum e ep)
{
switch (i)
{
case 1: return 1;
case 2: return 2;
}
switch (j)
{
case 3: return 4;
case 4: return 3;
default: break;
}
switch (ei) /* { dg-warning "enumeration value 'e1' not handled in switch" "enum e1" } */
{ /* { dg-warning "enumeration value 'e2' not handled in switch" "enum e2" { target *-*-* } 22 } */
}
switch (ej) /* { dg-error "enumeration value 'e1' not handled in switch" "enum e1" } */
{ /* { dg-error "enumeration value 'e2' not handled in switch" "enum e2" { target *-*-* } 25 } */
default: break;
}
switch (ek) /* { dg-warning "enumeration value 'e2' not handled in switch" "enum e2" } */
{
case e1: return 1;
}
switch (el) /* { dg-error "enumeration value 'e2' not handled in switch" "enum e2" } */
{
case e1: return 1;
default: break;
}
switch (em)
{
case e1: return 1;
case e2: return 2;
}
switch (en)
{
case e1: return 1;
case e2: return 2;
default: break;
}
switch (eo)
{
case e1: return 1;
case e2: return 2;
case 3: return 3; /* { dg-warning "case value '3' not in enumerated type 'enum e'" "excess 3" } */
}
switch (ep)
{
case e1: return 1;
case e2: return 2;
case 3: return 3; /* { dg-error "case value '3' not in enumerated type 'enum e'" "excess 3" } */
default: break;
}
return 0;
}
/* { dg-do compile } */
/* { dg-options "-Werror=switch -Wswitch-enum" } */
enum e { e1, e2 };
int
foo (int i, int j, enum e ei, enum e ej, enum e ek, enum e el,
enum e em, enum e en, enum e eo, enum e ep)
{
switch (i)
{
case 1: return 1;
case 2: return 2;
}
switch (j)
{
case 3: return 4;
case 4: return 3;
default: break;
}
switch (ei) /* { dg-error "enumeration value 'e1' not handled in switch" "enum e1" } */
{ /* { dg-error "enumeration value 'e2' not handled in switch" "enum e2" { target *-*-* } 22 } */
}
switch (ej) /* { dg-warning "enumeration value 'e1' not handled in switch" "enum e1" } */
{ /* { dg-warning "enumeration value 'e2' not handled in switch" "enum e2" { target *-*-* } 25 } */
default: break;
}
switch (ek) /* { dg-error "enumeration value 'e2' not handled in switch" "enum e2" } */
{
case e1: return 1;
}
switch (el) /* { dg-warning "enumeration value 'e2' not handled in switch" "enum e2" } */
{
case e1: return 1;
default: break;
}
switch (em)
{
case e1: return 1;
case e2: return 2;
}
switch (en)
{
case e1: return 1;
case e2: return 2;
default: break;
}
switch (eo)
{
case e1: return 1;
case e2: return 2;
case 3: return 3; /* { dg-error "case value '3' not in enumerated type 'enum e'" "excess 3" } */
}
switch (ep)
{
case e1: return 1;
case e2: return 2;
case 3: return 3; /* { dg-warning "case value '3' not in enumerated type 'enum e'" "excess 3" } */
default: break;
}
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment