From 8630f50feeb78d4a69eaeabf520b734a67bfe32b Mon Sep 17 00:00:00 2001
From: Eric Botcazou <ebotcazou@adacore.com>
Date: Sun, 16 Jan 2022 14:09:20 +0100
Subject: [PATCH] [Ada] Preserve unchecked conversion of string constant

This makes it possible to pass the result to a C function directly.

gcc/ada/

	* gcc-interface/utils.cc (unchecked_convert): Do not fold a string
	constant if the target type is pointer to character.
---
 gcc/ada/gcc-interface/utils.cc | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/gcc/ada/gcc-interface/utils.cc b/gcc/ada/gcc-interface/utils.cc
index 5722ed2611db..98bcb7063e33 100644
--- a/gcc/ada/gcc-interface/utils.cc
+++ b/gcc/ada/gcc-interface/utils.cc
@@ -5637,6 +5637,13 @@ unchecked_convert (tree type, tree expr, bool notrunc_p)
       return unchecked_convert (type, expr, notrunc_p);
     }
 
+  /* If we are converting a string constant to a pointer to character, make
+     sure that the string is not folded into an integer constant.  */
+  else if (TREE_CODE (expr) == STRING_CST
+	   && POINTER_TYPE_P (type)
+	   && TYPE_STRING_FLAG (TREE_TYPE (type)))
+    expr = build1 (VIEW_CONVERT_EXPR, type, expr);
+
   /* Otherwise, just build a VIEW_CONVERT_EXPR of the expression.  */
   else
     {
-- 
GitLab