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]+}} 'AddrSpaceCharType':'__attribute__((address_space(1))) char *' + // CHKC-NEXT: `-ImplicitCastExpr 0x{{[0-9a-f]+}} 'char *' + // CHKC-NEXT: `-StringLiteral 0x{{[0-9a-f]+}} 'char [2]' lvalue "a" + + // CHKCXX: `-CStyleCastExpr 0x{{[0-9a-f]+}} 'AddrSpaceCharType':'__attribute__((address_space(1))) char *' + // CHKCXX-NEXT: `-ImplicitCastExpr 0x{{[0-9a-f]+}} 'AddrSpaceCharType':'__attribute__((address_space(1))) char *' + // CHKCXX-NEXT: `-ImplicitCastExpr 0x{{[0-9a-f]+}} 'const char *' + // CHKCXX-NEXT: `-StringLiteral 0x{{[0-9a-f]+}} 'const char [2]' lvalue "a" + + p = (AddrSpaceCharType)"a"; +}