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

Marek Polacek polacek@redhat.com
Tue Oct 6 17:29:00 GMT 2015


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.

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;
+}

	Marek



More information about the Gcc-patches mailing list