On targets where ptrdiff_t is smaller than long, clang crashes (backtrace below) when emitting synthesized getters/setters that call objc_[gs]etProperty. Explicitly emit a zext/trunc of the ivar offset value (which is defined to long) to ptrdiff_t, which objc_[gs]etProperty takes.
Add a test using the AVR target, where ptrdiff_t is smaller than long. Test failed previously and passes now.
Backtrace:
Assertion failed: (castIsValid(op, S, Ty) && "Invalid cast!"), function Create, file /Users/matt/src/llvm/llvm/lib/IR/Instructions.cpp, line 3042. ... 7 libsystem_c.dylib 0x00007fff6ec09ac6 err + 0 8 clang-14 0x000000010acf104c llvm::CastInst::Create(llvm::Instruction::CastOps, llvm::Value*, llvm::Type*, llvm::Twine const&, llvm::Instruction*) + 124 9 clang-14 0x000000010beb140a llvm::IRBuilderBase::CreateCast(llvm::Instruction::CastOps, llvm::Value*, llvm::Type*, llvm::Twine const&) + 234 10 clang-14 0x000000010bebdec2 llvm::IRBuilderBase::CreateZExt(llvm::Value*, llvm::Type*, llvm::Twine const&) + 50 11 clang-14 0x000000010cab50aa clang::CodeGen::CodeGenFunction::EmitCall(clang::CodeGen::CGFunctionInfo const&, clang::CodeGen::CGCallee const&, clang::CodeGen::ReturnValueSlot, clang::CodeGen::CallArgList const&, llvm::CallBase**, bool, clang::SourceLocation) + 8442 12 clang-14 0x000000010ceadeb8 clang::CodeGen::CodeGenFunction::EmitCall(clang::CodeGen::CGFunctionInfo const&, clang::CodeGen::CGCallee const&, clang::CodeGen::ReturnValueSlot, clang::CodeGen::CallArgList const&, llvm::CallBase**, bool) + 216 13 clang-14 0x000000010cc76fe3 clang::CodeGen::CodeGenFunction::generateObjCGetterBody(clang::ObjCImplementationDecl const*, clang::ObjCPropertyImplDecl const*, clang::ObjCMethodDecl const*, llvm::Constant*) + 2595 14 clang-14 0x000000010cc75387 clang::CodeGen::CodeGenFunction::GenerateObjCGetter(clang::ObjCImplementationDecl*, clang::ObjCPropertyImplDecl const*) + 343 15 clang-14 0x000000010cf1c4c0 clang::CodeGen::CodeGenModule::EmitObjCPropertyImplementations(clang::ObjCImplementationDecl const*) + 336 16 clang-14 0x000000010cf17799 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) + 2009 17 clang-14 0x000000010d114bf2 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) + 146 18 clang-14 0x000000010cec0014 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) + 228 19 clang-14 0x0000000110436425 clang::ParseAST(clang::Sema&, bool, bool) + 533