[PATCH] D7606: Fix adress cast for C++ in SEMA
Samuel Antao via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 6 18:44:05 PST 2015
sfantao updated this revision to Diff 39619.
sfantao added a comment.
Rebase and add check to make sure the pointee of the pointers being casted match.
In a previous review, it was suggested by Richard Smith to move the check to the end of `TryReinterpretCast`. However, that does not solve the issue given that it is possible to have a successful cast try before the end of `TryReinterpretCast` is reached. The testcase in this patch is one of those cases.
http://reviews.llvm.org/D7606
Files:
lib/Sema/Sema.cpp
lib/Sema/SemaCast.cpp
test/CodeGen/address-space-explicit-cast.c
Index: test/CodeGen/address-space-explicit-cast.c
===================================================================
--- /dev/null
+++ test/CodeGen/address-space-explicit-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";
+}
Index: lib/Sema/SemaCast.cpp
===================================================================
--- lib/Sema/SemaCast.cpp
+++ lib/Sema/SemaCast.cpp
@@ -2036,7 +2036,7 @@
<< OpRange;
return TC_Success;
}
-
+
// C++ 5.2.10p7: A pointer to an object can be explicitly converted to
// a pointer to an object of different type.
// Void pointers are not specified, but supported by every compiler out there.
@@ -2104,6 +2104,22 @@
return;
}
+ // If we are casting pointers, we need to check whether this refers to an
+ // address space cast.
+ if (DestType->isPointerType() && SrcExpr.get()->getType()->isPointerType()) {
+ QualType DTy =
+ Self.getASTContext().getCanonicalType(DestType->getPointeeType());
+ QualType STy = Self.getASTContext().getCanonicalType(
+ SrcExpr.get()->getType()->getPointeeType());
+ // If the pointer point to the same type in different address spaces, this
+ // is an address-space cast.
+ if (STy.getTypePtr() == DTy.getTypePtr() &&
+ STy.getAddressSpace() != DTy.getAddressSpace()) {
+ Kind = CK_AddressSpaceConversion;
+ return;
+ }
+ }
+
// C++ [expr.cast]p5: The conversions performed by
// - a const_cast,
// - a static_cast,
Index: lib/Sema/Sema.cpp
===================================================================
--- lib/Sema/Sema.cpp
+++ lib/Sema/Sema.cpp
@@ -392,6 +392,14 @@
if (ExprTy == TypeTy)
return E;
+ // In the event an address space cast is requested, the kind passed from the
+ // caller should not be CK_NoOp.
+ assert((Kind != CK_NoOp ||
+ !(ExprTy->isPointerType() && TypeTy->isPointerType() &&
+ ExprTy->getPointeeType().getAddressSpace() !=
+ TypeTy->getPointeeType().getAddressSpace())) &&
+ "NoOp is not a valid kind for and address cast");
+
if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(E)) {
if (ImpCast->getCastKind() == Kind && (!BasePath || BasePath->empty())) {
ImpCast->setType(Ty);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D7606.39619.patch
Type: text/x-patch
Size: 2898 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20151107/30299ab2/attachment.bin>
More information about the cfe-commits
mailing list