diff mbox

[PATCHv3,resent] Add a warning for suspicious use of conditional expressions in boolean context

Message ID AM4PR0701MB216281F16F5011350CC97D67E4F60@AM4PR0701MB2162.eurprd07.prod.outlook.com
State New
Headers show

Commit Message

Bernd Edlinger Sept. 21, 2016, 7:52 p.m. UTC
On 09/21/16 21:03, Jason Merrill wrote:
> On Wed, Sep 21, 2016 at 11:43 AM, Bernd Edlinger
> <bernd.edlinger@hotmail.de> wrote:
>> On 09/21/16 17:00, Jason Merrill wrote:
>>> On 09/20/2016 02:40 PM, Bernd Edlinger wrote:
>>>> On 09/20/16 16:51, Jason Merrill wrote:
>>>>> On Tue, Sep 20, 2016 at 10:11 AM, Bernd Edlinger
>>>>> <bernd.edlinger@hotmail.de> wrote:
>>>>>> I think I will have to suppress the warning if the conditional is in
>>>>>> a macro somehow.
>>>>>
>>>>> from_macro_expansion_at will help with that.
>>>>>
>>>>> Though it seems to me that the issue here is more that (TARGET_FP16 ?
>>>>> 14 : 12) is not in a boolean context, it's in an integer context; only
>>>>> the outer ?: is in a boolean context.
>>>>>
>>>> +      if (warn_int_in_bool_context
>>>> +  && !from_macro_expansion_at (EXPR_LOCATION (expr)))
>>>>
>>>> But this seems to suppress all such warnings from an assert
>>>> macro too.  Like for instance "assert(a?1:2)".
>>>>
>>>> Sure, we would not be interested in a ?: that is part of the assert
>>>> macro itself, but the expression that is evaluated by the macro should
>>>> be checked, but that is no longer done, because the macro parameter is
>>>> now also from the macro expansion.  But it is initially from the
>>>> macro invocation point.  Ideas?
>>>
>>> This should fix that, though I haven't run regression tests yet:
>>>
>>
>> Yes.  I think that goes in the right direction,
>> but it does not work yet.
>>
>> #define XXX (a ? 2 : 3)
>>
>> if (XXX) // prints a warning, but it should not.
>
> Indeed, that was too simplistic.  This patch adds a
> from_macro_definition_at test that ought to do what you want:
>

Yes, this works for me as well.

Here is an updated warning patch, using the new
from_macro_definition_at.


Bernd.

Comments

Jason Merrill Sept. 21, 2016, 8 p.m. UTC | #1
On Wed, Sep 21, 2016 at 3:52 PM, Bernd Edlinger
<bernd.edlinger@hotmail.de> wrote:
> On 09/21/16 21:03, Jason Merrill wrote:
>> On Wed, Sep 21, 2016 at 11:43 AM, Bernd Edlinger
>> <bernd.edlinger@hotmail.de> wrote:
>>> On 09/21/16 17:00, Jason Merrill wrote:
>>>> On 09/20/2016 02:40 PM, Bernd Edlinger wrote:
>>>>> On 09/20/16 16:51, Jason Merrill wrote:
>>>>>> On Tue, Sep 20, 2016 at 10:11 AM, Bernd Edlinger
>>>>>> <bernd.edlinger@hotmail.de> wrote:
>>>>>>> I think I will have to suppress the warning if the conditional is in
>>>>>>> a macro somehow.
>>>>>>
>>>>>> from_macro_expansion_at will help with that.
>>>>>>
>>>>>> Though it seems to me that the issue here is more that (TARGET_FP16 ?
>>>>>> 14 : 12) is not in a boolean context, it's in an integer context; only
>>>>>> the outer ?: is in a boolean context.
>>>>>>
>>>>> +      if (warn_int_in_bool_context
>>>>> +  && !from_macro_expansion_at (EXPR_LOCATION (expr)))
>>>>>
>>>>> But this seems to suppress all such warnings from an assert
>>>>> macro too.  Like for instance "assert(a?1:2)".
>>>>>
>>>>> Sure, we would not be interested in a ?: that is part of the assert
>>>>> macro itself, but the expression that is evaluated by the macro should
>>>>> be checked, but that is no longer done, because the macro parameter is
>>>>> now also from the macro expansion.  But it is initially from the
>>>>> macro invocation point.  Ideas?
>>>>
>>>> This should fix that, though I haven't run regression tests yet:
>>>>
>>>
>>> Yes.  I think that goes in the right direction,
>>> but it does not work yet.
>>>
>>> #define XXX (a ? 2 : 3)
>>>
>>> if (XXX) // prints a warning, but it should not.
>>
>> Indeed, that was too simplistic.  This patch adds a
>> from_macro_definition_at test that ought to do what you want:
>>
>
> Yes, this works for me as well.
>
> Here is an updated warning patch, using the new
> from_macro_definition_at.

OK.

Jason
diff mbox

Patch

2016-09-21  Bernd Edlinger  <bernd.edlinger@hotmail.de>

	* c-common.c (c_common_truthvalue_conversion): Inhibit
	Wint-in-bool-context warning with from_macro_definition_at.
	Mention the expression will always evaluate to true.

Index: gcc/c-family/c-common.c
===================================================================
--- gcc/c-family/c-common.c	(Revision 240313)
+++ gcc/c-family/c-common.c	(Arbeitskopie)
@@ -4652,7 +4652,8 @@  c_common_truthvalue_conversion (location_t locatio
 					       TREE_OPERAND (expr, 0));
 
     case COND_EXPR:
-      if (warn_int_in_bool_context)
+      if (warn_int_in_bool_context
+	  && !from_macro_definition_at (EXPR_LOCATION (expr)))
 	{
 	  tree val1 = fold_for_warn (TREE_OPERAND (expr, 1));
 	  tree val2 = fold_for_warn (TREE_OPERAND (expr, 2));
@@ -4663,7 +4664,8 @@  c_common_truthvalue_conversion (location_t locatio
 	      && (!integer_onep (val1)
 		  || !integer_onep (val2)))
 	    warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
-		        "?: using integer constants in boolean context");
+			"?: using integer constants in boolean context, "
+			"the expression will always evaluate to %<true%>");
 	}
       /* Distribute the conversion into the arms of a COND_EXPR.  */
       if (c_dialect_cxx ())