[flang] Replace access through union with reinterpret_cast.
authorDavid Truby <david.truby@arm.com>
Mon, 30 Sep 2019 11:41:34 +0000 (12:41 +0100)
committerDavid Truby <david.truby@arm.com>
Mon, 30 Sep 2019 11:41:34 +0000 (12:41 +0100)
This avoids undefined behaviour.

Original-commit: flang-compiler/f18@e289bbfa83ebb818971a688b019be697fd900d62
Reviewed-on: https://github.com/flang-compiler/f18/pull/767
Tree-same-pre-rewrite: false

flang/lib/evaluate/intrinsics-library-templates.h
flang/lib/evaluate/intrinsics-library.h

index 73f51b4..0d4f48c 100644 (file)
@@ -129,23 +129,13 @@ template<typename TR, typename... ArgInfo> struct CallableHostWrapper {
 template<typename TR, typename... TA>
 inline GenericFunctionPointer ToGenericFunctionPointer(
     FuncPointer<TR, TA...> f) {
-  union {
-    GenericFunctionPointer gp;
-    FuncPointer<TR, TA...> fp;
-  } u;
-  u.fp = f;
-  return u.gp;
+  return reinterpret_cast<GenericFunctionPointer>(f);
 }
 
 template<typename TR, typename... TA>
 inline FuncPointer<TR, TA...> FromGenericFunctionPointer(
     GenericFunctionPointer g) {
-  union {
-    GenericFunctionPointer gp;
-    FuncPointer<TR, TA...> fp;
-  } u;
-  u.gp = g;
-  return u.fp;
+  return reinterpret_cast<FuncPointer<TR, TA...>>(g);
 }
 
 template<typename TR, typename... ArgInfo>
index 10aeeb5..28a83e4 100644 (file)
@@ -35,7 +35,7 @@ class FoldingContext;
 using TypeCode = unsigned char;
 
 template<typename TR, typename... TA> using FuncPointer = TR (*)(TA...);
-using GenericFunctionPointer = FuncPointer<void *>;
+using GenericFunctionPointer = void (*) (void);
 
 enum class PassBy { Ref, Val };
 template<typename TA, PassBy Pass = PassBy::Ref> struct ArgumentInfo {