-Wswitch -Wno-switch-bool -Wswitch-default -Wswitch-enum @gol
-Wno-switch-outside-range -Wno-switch-unreachable -Wsync-nand @gol
-Wsystem-headers -Wtautological-compare -Wtrampolines -Wtrigraphs @gol
--Wtsan -Wtype-limits -Wundef @gol
+-Wtrivial-auto-var-init -Wtsan -Wtype-limits -Wundef @gol
-Wuninitialized -Wunknown-pragmas @gol
-Wunsuffixed-float-constants -Wunused @gol
-Wunused-but-set-parameter -Wunused-but-set-variable @gol
Warn when @code{__sync_fetch_and_nand} and @code{__sync_nand_and_fetch}
built-in functions are used. These functions changed semantics in GCC 4.4.
+@item -Wtrivial-auto-var-init
+@opindex Wtrivial-auto-var-init
+@opindex Wno-trivial-auto-var-init
+Warn when @code{-ftrivial-auto-var-init} cannot initialize the automatic
+variable. A common situation is an automatic variable that is declared
+between the controlling expression and the first case label of a @code{switch}
+statement.
+
@item -Wunused-but-set-parameter
@opindex Wunused-but-set-parameter
@opindex Wno-unused-but-set-parameter
warning messages on such automatic variables.
With this option, GCC will also initialize any padding of automatic variables
that have structure or union types to zeroes.
+However, the current implementation cannot initialize automatic variables that
+are declared between the controlling expression and the first case of a
+@code{switch} statement. Using @option{-Wtrivial-auto-var-init} to report all
+such cases.
The three values of @var{choice} are:
return GS_ALL_DONE;
}
+
+/* Emit warning for the unreachable statment STMT if needed.
+ Return the gimple itself when the warning is emitted, otherwise
+ return NULL. */
+static gimple *
+emit_warn_switch_unreachable (gimple *stmt)
+{
+ if (gimple_code (stmt) == GIMPLE_GOTO
+ && TREE_CODE (gimple_goto_dest (stmt)) == LABEL_DECL
+ && DECL_ARTIFICIAL (gimple_goto_dest (stmt)))
+ /* Don't warn for compiler-generated gotos. These occur
+ in Duff's devices, for example. */
+ return NULL;
+ else if ((flag_auto_var_init > AUTO_INIT_UNINITIALIZED)
+ && ((gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))
+ || (gimple_call_builtin_p (stmt, BUILT_IN_CLEAR_PADDING)
+ && (bool) TREE_INT_CST_LOW (gimple_call_arg (stmt, 1)))
+ || (is_gimple_assign (stmt)
+ && gimple_assign_single_p (stmt)
+ && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
+ && gimple_call_internal_p (
+ SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt)),
+ IFN_DEFERRED_INIT))))
+ /* Don't warn for compiler-generated initializations for
+ -ftrivial-auto-var-init.
+ There are 3 cases:
+ case 1: a call to .DEFERRED_INIT;
+ case 2: a call to __builtin_clear_padding with the 2nd argument is
+ present and non-zero;
+ case 3: a gimple assign store right after the call to .DEFERRED_INIT
+ that has the LHS of .DEFERRED_INIT as the RHS as following:
+ _1 = .DEFERRED_INIT (4, 2, &"i1"[0]);
+ i1 = _1. */
+ return NULL;
+ else
+ warning_at (gimple_location (stmt), OPT_Wswitch_unreachable,
+ "statement will never be executed");
+ return stmt;
+}
+
/* Callback for walk_gimple_seq. */
static tree
-warn_switch_unreachable_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
- struct walk_stmt_info *wi)
+warn_switch_unreachable_and_auto_init_r (gimple_stmt_iterator *gsi_p,
+ bool *handled_ops_p,
+ struct walk_stmt_info *wi)
{
gimple *stmt = gsi_stmt (*gsi_p);
+ bool unreachable_issued = wi->info != NULL;
*handled_ops_p = true;
switch (gimple_code (stmt))
worse location info. */
if (gimple_try_eval (stmt) == NULL)
{
- wi->info = stmt;
- return integer_zero_node;
+ if (warn_switch_unreachable && !unreachable_issued)
+ wi->info = emit_warn_switch_unreachable (stmt);
+
+ /* Stop when auto var init warning is not on. */
+ if (!warn_trivial_auto_var_init)
+ return integer_zero_node;
}
/* Fall through. */
case GIMPLE_BIND:
there will be non-debug stmts too, and we'll catch those. */
break;
+ case GIMPLE_LABEL:
+ /* Stop till the first Label. */
+ return integer_zero_node;
case GIMPLE_CALL:
if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
{
*handled_ops_p = false;
break;
}
+ if (warn_trivial_auto_var_init
+ && flag_auto_var_init > AUTO_INIT_UNINITIALIZED
+ && gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))
+ {
+ /* Get the variable name from the 3rd argument of call. */
+ tree var_name = gimple_call_arg (stmt, 2);
+ var_name = TREE_OPERAND (TREE_OPERAND (var_name, 0), 0);
+ const char *var_name_str = TREE_STRING_POINTER (var_name);
+
+ warning_at (gimple_location (stmt), OPT_Wtrivial_auto_var_init,
+ "%qs cannot be initialized with"
+ "%<-ftrivial-auto-var_init%>",
+ var_name_str);
+ break;
+ }
+
/* Fall through. */
default:
- /* Save the first "real" statement (not a decl/lexical scope/...). */
- wi->info = stmt;
- return integer_zero_node;
+ /* check the first "real" statement (not a decl/lexical scope/...), issue
+ warning if needed. */
+ if (warn_switch_unreachable && !unreachable_issued)
+ wi->info = emit_warn_switch_unreachable (stmt);
+ /* Stop when auto var init warning is not on. */
+ if (!warn_trivial_auto_var_init)
+ return integer_zero_node;
+ break;
}
return NULL_TREE;
}
+
/* Possibly warn about unreachable statements between switch's controlling
- expression and the first case. SEQ is the body of a switch expression. */
+ expression and the first case. Also warn about -ftrivial-auto-var-init
+ cannot initialize the auto variable under such situation.
+ SEQ is the body of a switch expression. */
static void
-maybe_warn_switch_unreachable (gimple_seq seq)
+maybe_warn_switch_unreachable_and_auto_init (gimple_seq seq)
{
- if (!warn_switch_unreachable
+ if ((!warn_switch_unreachable && !warn_trivial_auto_var_init)
/* This warning doesn't play well with Fortran when optimizations
are on. */
|| lang_GNU_Fortran ()
return;
struct walk_stmt_info wi;
- memset (&wi, 0, sizeof (wi));
- walk_gimple_seq (seq, warn_switch_unreachable_r, NULL, &wi);
- gimple *stmt = (gimple *) wi.info;
- if (stmt && gimple_code (stmt) != GIMPLE_LABEL)
- {
- if (gimple_code (stmt) == GIMPLE_GOTO
- && TREE_CODE (gimple_goto_dest (stmt)) == LABEL_DECL
- && DECL_ARTIFICIAL (gimple_goto_dest (stmt)))
- /* Don't warn for compiler-generated gotos. These occur
- in Duff's devices, for example. */;
- else
- warning_at (gimple_location (stmt), OPT_Wswitch_unreachable,
- "statement will never be executed");
- }
+ memset (&wi, 0, sizeof (wi));
+ walk_gimple_seq (seq, warn_switch_unreachable_and_auto_init_r, NULL, &wi);
}
gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
gimplify_ctxp->in_switch_expr = old_in_switch_expr;
- maybe_warn_switch_unreachable (switch_body_seq);
+ maybe_warn_switch_unreachable_and_auto_init (switch_body_seq);
maybe_warn_implicit_fallthrough (switch_body_seq);
/* Only do this for the outermost GIMPLE_SWITCH. */
if (!gimplify_ctxp->in_switch_expr)