This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [C PATCH] Warn if switch has boolean value (PR c/60439)
- From: Jason Merrill <jason at redhat dot com>
- To: Marek Polacek <polacek at redhat dot com>, Jeff Law <law at redhat dot com>
- Cc: Steven Bosscher <stevenb dot gcc at gmail dot com>, GCC Patches <gcc-patches at gcc dot gnu dot org>, "Joseph S. Myers" <joseph at codesourcery dot com>
- Date: Mon, 02 Jun 2014 18:00:04 -0400
- Subject: Re: [C PATCH] Warn if switch has boolean value (PR c/60439)
- Authentication-results: sourceware.org; auth=none
- References: <20140418053021 dot GH20332 at redhat dot com> <CABu31nO7XjjEJCZCf_vLFgfpvuejdxcmeF6YB4++8sAGkHjJ8A at mail dot gmail dot com> <20140418115013 dot GK20332 at redhat dot com> <53632870 dot 6030503 at redhat dot com> <20140524080048 dot GF17600 at redhat dot com>
On 05/24/2014 04:00 AM, Marek Polacek wrote:
+ /* Warn if the condition has boolean value. */
+ tree e = cond;
+ while (TREE_CODE (e) == COMPOUND_EXPR)
+ e = TREE_OPERAND (e, 1);
+
+ if (TREE_CODE (orig_type) == BOOLEAN_TYPE
+ || (truth_value_p (TREE_CODE (e))
+ && TREE_CODE (orig_type) != INTEGER_TYPE))
+ warning_at (input_location, OPT_Wswitch_bool,
+ "switch condition has boolean value");
For C++ it should be "type bool", not "boolean value". And it should be
enough to just check for BOOLEAN_TYPE, without looking through
COMPOUND_EXPR.
2) Since the warning is now enabled even for the C++ FE, it's
exercised during bootstrap. Turned out that gengtype generates
code like
switch (TREE_CODE (...) == INTEGER_TYPE) { ... }
that would mar the bootstrap - so I tweaked it to generate
switch ((int) (TREE_CODE (...) == INTEGER_TYPE) { ... })
instead. Does that make sense?
Makes sense to me.
Jason