Generate error if we try to implicit cast between different address
spaces
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@55765 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/Sema.cpp b/lib/Sema/Sema.cpp
index 65f1d68..b288c62 100644
--- a/lib/Sema/Sema.cpp
+++ b/lib/Sema/Sema.cpp
@@ -114,14 +114,29 @@
/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
/// If there is already an implicit cast, merge into the existing one.
-void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) {
- if (Context.getCanonicalType(Expr->getType()) ==
- Context.getCanonicalType(Type)) return;
+void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty) {
+ QualType ExprTy = Context.getCanonicalType(Expr->getType());
+ QualType TypeTy = Context.getCanonicalType(Ty);
+
+ if (ExprTy == TypeTy)
+ return;
+
+ if (Expr->getType().getTypePtr()->isPointerType() &&
+ Ty.getTypePtr()->isPointerType()) {
+ QualType ExprBaseType =
+ cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType();
+ QualType BaseType =
+ cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType();
+ if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
+ Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast,
+ Expr->getSourceRange());
+ }
+ }
if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
- ImpCast->setType(Type);
+ ImpCast->setType(Ty);
else
- Expr = new ImplicitCastExpr(Type, Expr);
+ Expr = new ImplicitCastExpr(Ty, Expr);
}
void Sema::DeleteExpr(ExprTy *E) {