diff mbox

[C++] Build COND_EXPRs with location (PR c++/67863)

Message ID 20151006172910.GS6184@redhat.com
State New
Headers show

Commit Message

Marek Polacek Oct. 6, 2015, 5:29 p.m. UTC
I've been chasing a bogus -Wtautological-compare warning that only
occurred in the C++ FE.  Turned out that we were building COND_EXPRs
without a location; that means that this code in warn_tautological_cmp
didn't work as planned:

  /* Don't warn for various macro expansions.  */
  if (from_macro_expansion_at (loc) 
      || from_macro_expansion_at (EXPR_LOCATION (lhs))
      || from_macro_expansion_at (EXPR_LOCATION (rhs)))
    return;

If we set the location properly, we're able to detect that either LHS
or RHS comes from a macro expansion.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2015-10-06  Marek Polacek  <polacek@redhat.com>

	PR c++/67863
	* call.c (build_conditional_expr_1): Build the COND_EXPR with
	a location.

	* c-c++-common/Wtautological-compare-4.c: New test.


	Marek

Comments

Jeff Law Oct. 6, 2015, 5:46 p.m. UTC | #1
On 10/06/2015 11:29 AM, Marek Polacek wrote:
> I've been chasing a bogus -Wtautological-compare warning that only
> occurred in the C++ FE.  Turned out that we were building COND_EXPRs
> without a location; that means that this code in warn_tautological_cmp
> didn't work as planned:
>
>    /* Don't warn for various macro expansions.  */
>    if (from_macro_expansion_at (loc)
>        || from_macro_expansion_at (EXPR_LOCATION (lhs))
>        || from_macro_expansion_at (EXPR_LOCATION (rhs)))
>      return;
>
> If we set the location properly, we're able to detect that either LHS
> or RHS comes from a macro expansion.
>
> Bootstrapped/regtested on x86_64-linux, ok for trunk?
>
> 2015-10-06  Marek Polacek  <polacek@redhat.com>
>
> 	PR c++/67863
> 	* call.c (build_conditional_expr_1): Build the COND_EXPR with
> 	a location.
>
> 	* c-c++-common/Wtautological-compare-4.c: New test.
OK.

Related, I'll drop my proposed patch to builtins.c that worked around 
this problem.

Jeff
Marek Polacek Oct. 6, 2015, 6:08 p.m. UTC | #2
On Tue, Oct 06, 2015 at 11:46:33AM -0600, Jeff Law wrote:
> >2015-10-06  Marek Polacek  <polacek@redhat.com>
> >
> >	PR c++/67863
> >	* call.c (build_conditional_expr_1): Build the COND_EXPR with
> >	a location.
> >
> >	* c-c++-common/Wtautological-compare-4.c: New test.
> OK.
 
Committed.

> Related, I'll drop my proposed patch to builtins.c that worked around this
> problem.

Yeah, thanks.

	Marek
diff mbox

Patch

diff --git gcc/cp/call.c gcc/cp/call.c
index 050d045..93e28dc 100644
--- gcc/cp/call.c
+++ gcc/cp/call.c
@@ -5144,7 +5144,7 @@  build_conditional_expr_1 (location_t loc, tree arg1, tree arg2, tree arg3,
     return error_mark_node;
 
  valid_operands:
-  result = build3 (COND_EXPR, result_type, arg1, arg2, arg3);
+  result = build3_loc (loc, COND_EXPR, result_type, arg1, arg2, arg3);
   if (!cp_unevaluated_operand)
     /* Avoid folding within decltype (c++/42013) and noexcept.  */
     result = fold_if_not_in_template (result);
diff --git gcc/testsuite/c-c++-common/Wtautological-compare-4.c gcc/testsuite/c-c++-common/Wtautological-compare-4.c
index e69de29..207c401 100644
--- gcc/testsuite/c-c++-common/Wtautological-compare-4.c
+++ gcc/testsuite/c-c++-common/Wtautological-compare-4.c
@@ -0,0 +1,15 @@ 
+/* PR c++/67863 */
+/* { dg-do compile } */
+/* { dg-options "-Wtautological-compare" } */
+
+extern int e;
+#define A (e ? 4 : 8)
+#define B (e ? 4 : 8)
+
+int
+fn (void)
+{
+  if (A <= B)
+    return 1;
+  return 0;
+}