diff options
| -rw-r--r-- | clang/include/clang/Basic/DiagnosticKinds.def | 2 | ||||
| -rw-r--r-- | clang/lib/Sema/Sema.cpp | 25 |
2 files changed, 22 insertions, 5 deletions
diff --git a/clang/include/clang/Basic/DiagnosticKinds.def b/clang/include/clang/Basic/DiagnosticKinds.def index e6cc8ef772c..2fe462a9a0c 100644 --- a/clang/include/clang/Basic/DiagnosticKinds.def +++ b/clang/include/clang/Basic/DiagnosticKinds.def @@ -670,6 +670,8 @@ DIAG(err_attribute_address_space_not_int, ERROR, "address space attribute requires an integer constant") DIAG(err_attribute_address_multiple_qualifiers, ERROR, "multiple address spaces specified for type") +DIAG(err_implicit_pointer_address_space_cast, ERROR, + "illegal implicit cast between two pointers with different address spaces") DIAG(err_as_qualified_auto_decl, ERROR, "automatic variable qualified with an address space") DIAG(err_attribute_annotate_no_string, ERROR, diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp index 65f1d6812b6..b288c627872 100644 --- a/clang/lib/Sema/Sema.cpp +++ b/clang/lib/Sema/Sema.cpp @@ -114,14 +114,29 @@ Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer) /// 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) { |

