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

Samuel Antao sfantao at us.ibm.com
Thu May 14 14:00:01 PDT 2015


Hi Hal,

Thanks again for looking into this. Adding a regression test for the issue this patch addresses. Let me know if I created it in the right place.

Thanks,
Samuel


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
@@ -369,6 +369,13 @@
     }
   }
 
+  // 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;
+
   return ImplicitCastExpr::Create(Context, Ty, Kind, E, BasePath, VK);
 }
 
Index: test/Sema/address-space-cast.c
===================================================================
--- /dev/null
+++ test/Sema/address-space-cast.c
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 %s -ast-dump -x c %s | FileCheck -check-prefix=CHKC %s
+// RUN: %clang_cc1 %s -ast-dump -x c++ %s | FileCheck -check-prefix=CHKCXX %s
+
+typedef  __attribute__((address_space(1))) char * AddrSpaceCharType;
+
+void foo() {
+  AddrSpaceCharType p;
+
+  // CHKC:    `-CStyleCastExpr 0x{{[0-9a-f]+}} <col:{{[0-9]+}}, col:{{[0-9]+}}> 'AddrSpaceCharType':'__attribute__((address_space(1))) char *' <AddressSpaceConversion>
+  // CHKC-NEXT: `-ImplicitCastExpr 0x{{[0-9a-f]+}} <col:{{[0-9]+}}> 'char *' <ArrayToPointerDecay>
+  // CHKC-NEXT:   `-StringLiteral 0x{{[0-9a-f]+}} <col:{{[0-9]+}}> 'char [2]' lvalue "a"
+
+  // CHKCXX:    `-CStyleCastExpr 0x{{[0-9a-f]+}} <col:{{[0-9]+}}, col:{{[0-9]+}}> 'AddrSpaceCharType':'__attribute__((address_space(1))) char *' <NoOp>
+  // CHKCXX-NEXT: `-ImplicitCastExpr 0x{{[0-9a-f]+}} <col:{{[0-9]+}}> 'AddrSpaceCharType':'__attribute__((address_space(1))) char *' <AddressSpaceConversion>
+  // CHKCXX-NEXT:   `-ImplicitCastExpr 0x{{[0-9a-f]+}} <col:{{[0-9]+}}> 'const char *' <ArrayToPointerDecay>
+  // CHKCXX-NEXT:     `-StringLiteral 0x{{[0-9a-f]+}} <col:{{[0-9]+}}> 'const char [2]' lvalue "a"
+
+  p = (AddrSpaceCharType)"a";
+}

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


More information about the cfe-commits mailing list