[PATCH] Fix adress cast for C++ in SEMA

Samuel Antao sfantao at us.ibm.com
Tue Jun 9 17:28:57 PDT 2015


Refactor the patch following rsmith requests.


http://reviews.llvm.org/D7606

Files:
  lib/Sema/Sema.cpp
  test/Sema/address-space-cast.c

Index: lib/Sema/Sema.cpp
===================================================================
--- lib/Sema/Sema.cpp
+++ lib/Sema/Sema.cpp
@@ -362,6 +362,13 @@
   if (ExprTy == TypeTy)
     return E;
 
+  // If we are casting pointers, we need to make sure we deal with address
+  // spaces properly.
+  if (Kind == CK_NoOp && ExprTy->isPointerType() && TypeTy->isPointerType() &&
+      ExprTy->getPointeeType().getAddressSpace() !=
+          TypeTy->getPointeeType().getAddressSpace())
+    Kind = CK_AddressSpaceConversion;
+
   if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(E)) {
     if (ImpCast->getCastKind() == Kind && (!BasePath || BasePath->empty())) {
       ImpCast->setType(Ty);
Index: test/Sema/address-space-cast.c
===================================================================
--- /dev/null
+++ test/Sema/address-space-cast.c
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -emit-llvm -o - -x c %s | FileCheck -check-prefix=CHECK %s
+// RUN: %clang_cc1 -emit-llvm -o - -x c++ %s | FileCheck -check-prefix=CHECK-CXX %s
+
+typedef  __attribute__((address_space(1))) char * AddrSpaceCharType;
+
+// CHECK-LABEL: @foo()
+// CHECK-CXX-LABEL: @_Z3foov()
+void foo() {
+  // CHECK: %p = alloca i8 addrspace(1)*
+  // CHECK-CXX: %p = alloca i8 addrspace(1)*
+  AddrSpaceCharType p;
+
+  // CHECK: store i8 addrspace(1)* addrspacecast ({{.*}} to i8 addrspace(1)*), i8 addrspace(1)** %p
+  // CHECK-CXX: store i8 addrspace(1)* addrspacecast ({{.*}} to i8 addrspace(1)*), i8 addrspace(1)** %p
+  p = (AddrSpaceCharType)"a";
+}

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D7606.27420.patch
Type: text/x-patch
Size: 1538 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150610/e2142491/attachment.bin>


More information about the cfe-commits mailing list