[flang] remove use of undefined behavior
authorEric Schweitz <eschweitz@nvidia.com>
Tue, 26 Mar 2019 01:56:44 +0000 (18:56 -0700)
committerEric Schweitz <eschweitz@nvidia.com>
Tue, 26 Mar 2019 02:16:41 +0000 (19:16 -0700)
Original-commit: flang-compiler/f18@d7c5879f2c86ede4304f98a4e16af6c97b2a09b9
Reviewed-on: https://github.com/flang-compiler/f18/pull/354
Tree-same-pre-rewrite: false

flang/lib/FIR/statements.h

index 6078e91..8afd598 100644 (file)
@@ -557,14 +557,14 @@ public:
   }
   std::string dump() const;
 
-  static std::size_t offsetof_impl() {
-    return reinterpret_cast<std::size_t>(&static_cast<Statement *>(nullptr)->u);
-  }
+  // g++/clang++ will optimize this to a simple register copy
   static Statement *From(Stmt_impl *stmt) {
-    char *cp{reinterpret_cast<char *>(stmt)};
-    auto adjust{Statement::offsetof_impl()};
-    CHECK(adjust == 0);
-    return reinterpret_cast<Statement *>(cp - adjust);
+    static Statement s{nullptr, UnreachableStmt::Create()};
+    auto *result{reinterpret_cast<Statement *>(reinterpret_cast<char *>(stmt) -
+        (reinterpret_cast<char *>(&s.u) - reinterpret_cast<char *>(&s)))};
+    CHECK(result == reinterpret_cast<Statement *>(stmt) &&
+        "expecting pointers to be equal");
+    return result;
   }
 };