This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [C PATCH] Warn if switch has boolean value (PR c/60439)


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]