From 5cd6070393ce9466dd872f52357f9c4d4a17b996 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Thu, 1 Nov 2018 11:14:24 -0400
Subject: [PATCH 58/83] FIXME: fixups

---
 gcc/convert.c | 94 ++++++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 58 insertions(+), 36 deletions(-)

diff --git a/gcc/convert.c b/gcc/convert.c
index 99a04c4..043a5d0 100644
--- a/gcc/convert.c
+++ b/gcc/convert.c
@@ -985,7 +985,7 @@ convert_to_integer_1 (tree type, tree expr, bool dofold)
 
       /* When parsing long initializers, we might end up with a lot of casts.
 	 Shortcut this.  */
-      if (TREE_CODE (expr) == INTEGER_CST)
+      if (TREE_CODE (tree_strip_any_location_wrapper (expr)) == INTEGER_CST)
 	return fold_convert (type, expr);
       return build1 (CONVERT_EXPR, type, expr);
 
@@ -1210,46 +1210,68 @@ namespace selftest {
 
 /* Selftests for conversions.  */
 
+static void
+test_convert_to_integer_maybe_fold (tree orig_type, tree new_type)
+{
+  /* Calling convert_to_integer_maybe_fold on an INTEGER_CST.  */
+
+  tree orig_cst = build_int_cst (orig_type, 42);
+
+  /* Verify that convert_to_integer_maybe_fold on a constant returns a new
+     constant of the new type, unless the types are the same, in which
+     case verify it's a no-op.  */
+  {
+    tree result = convert_to_integer_maybe_fold (new_type,
+						 orig_cst, false);
+    if (orig_type != new_type)
+      {
+	ASSERT_EQ (TREE_TYPE (result), new_type);
+	ASSERT_EQ (TREE_CODE (result), INTEGER_CST);
+      }
+    else
+      ASSERT_EQ (result, orig_cst);
+  }
+
+  /* Calling convert_to_integer_maybe_fold on a location wrapper around
+     an INTEGER_CST.
+
+     Verify that convert_to_integer_maybe_fold on a location wrapper
+     around a constant returns a new location wrapper around an equivalent
+     constant, both of the new type, unless the types are the same,
+     in which case the original wrapper should be returned.   */
+  {
+    const location_t loc = BUILTINS_LOCATION;
+    tree wrapped_orig_cst = maybe_wrap_with_location (orig_cst, loc);
+    tree result
+      = convert_to_integer_maybe_fold (new_type, wrapped_orig_cst, false);
+    ASSERT_EQ (TREE_TYPE (result), new_type);
+    ASSERT_EQ (EXPR_LOCATION (result), loc);
+    ASSERT_TRUE (location_wrapper_p (result));
+    ASSERT_EQ (TREE_TYPE (TREE_OPERAND (result, 0)), new_type);
+    ASSERT_EQ (TREE_CODE (TREE_OPERAND (result, 0)), INTEGER_CST);
+
+    if (orig_type == new_type)
+      ASSERT_EQ (result, wrapped_orig_cst);
+  }
+}
+
 /* Verify that convert_to_integer_maybe_fold preserves locations.  */
 
 static void
 test_convert_to_integer_maybe_fold ()
 {
-  location_t loc = BUILTINS_LOCATION;
-
-  /* char -> long.
-     Verify that convert_to_integer_maybe_fold on a constant returns a new
-     constant of the new type.   */
-  tree int_cst = build_int_cst (char_type_node, 42);
-  tree result = convert_to_integer_maybe_fold (long_integer_type_node,
-					       int_cst, false);
-  ASSERT_EQ (TREE_TYPE (result), long_integer_type_node);
-  ASSERT_EQ (TREE_CODE (result), INTEGER_CST);
-
-  /* char -> long.
-     Verify that convert_to_integer_maybe_fold on a location wrapper
-     around a constant returns a new location wrapper around an equivalent
-     constant, both of the new type.   */
-  tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
-  result = convert_to_integer_maybe_fold (long_integer_type_node,
-					  wrapped_int_cst, false);
-  ASSERT_EQ (TREE_TYPE (result), long_integer_type_node);
-  ASSERT_EQ (EXPR_LOCATION (result), loc);
-  ASSERT_TRUE (location_wrapper_p (result));
-  ASSERT_EQ (TREE_TYPE (TREE_OPERAND (result, 0)), long_integer_type_node);
-  ASSERT_EQ (TREE_CODE (TREE_OPERAND (result, 0)), INTEGER_CST);
-
-  /* char -> char.
-     Verify that a no-op conversion on a constant is indeed a no-op.  */
-  result = convert_to_integer_maybe_fold (char_type_node,
-					  int_cst, false);
-  ASSERT_EQ (result, int_cst);
-
-  /* char -> char.
-     Verify that a no-op conversion on a wrapped constant is indeed a no-op.  */
-  result = convert_to_integer_maybe_fold (char_type_node,
-					  wrapped_int_cst, false);
-  ASSERT_EQ (result, wrapped_int_cst);
+  /* char -> long.  */
+  test_convert_to_integer_maybe_fold (char_type_node, long_integer_type_node);
+
+  /* char -> char.  */
+  test_convert_to_integer_maybe_fold (char_type_node, char_type_node);
+
+  /* long -> char.  */
+  test_convert_to_integer_maybe_fold (char_type_node, long_integer_type_node);
+
+  /* long -> long.  */
+  test_convert_to_integer_maybe_fold (long_integer_type_node,
+				      long_integer_type_node);
 }
 
 /* Run all of the selftests within this file.  */
-- 
1.8.5.3

