[cfe-dev] Patch: making implicit cast between address space to be illegal

Mon P Wang monping at apple.com
Wed Aug 6 00:00:49 PDT 2008


Hi,

If I remember the state of affairs properly, we decided that implicit  
cast between address spaces should be illegal.  Here is a patch that  
will generate an error if it occurs.  If you have any comments, please  
let me know.

  -- Mon Ping

Index: include/clang/Basic/DiagnosticKinds.def
===================================================================
--- include/clang/Basic/DiagnosticKinds.def	(revision 54405)
+++ include/clang/Basic/DiagnosticKinds.def	(working copy)
@@ -658,6 +658,8 @@
       "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 space")
  DIAG(err_as_qualified_auto_decl, ERROR,
       "automatic variable qualified with an address space")
  DIAG(err_attribute_annotate_no_string, ERROR,
Index: lib/Sema/Sema.cpp
===================================================================
--- lib/Sema/Sema.cpp	(revision 54405)
+++ lib/Sema/Sema.cpp	(working copy)
@@ -113,6 +113,19 @@
    if (Context.getCanonicalType(Expr->getType()) ==
          Context.getCanonicalType(Type)) return;

+  if (Expr->getType().getTypePtr()->isPointerType() &&
+      Type.getTypePtr()->isPointerType()) {
+    QualType ExprBaseType =
+      cast<PointerType>(Expr->getType().getTypePtr())->getPointeeType 
();
+    QualType BaseType =
+      cast<PointerType>(Type.getTypePtr())->getPointeeType();
+    if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
+      // It is illegal to impliclit cast between two pointers of  
different
+      // address place
+      Diag(Expr->getExprLoc(),  
diag::err_implicit_pointer_address_space_cast,
+           Expr->getSourceRange());
+    }
+  }
    if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
      ImpCast->setType(Type);
    else




More information about the cfe-dev mailing list