Index: lib/AST/DeclObjC.cpp =================================================================== --- lib/AST/DeclObjC.cpp +++ lib/AST/DeclObjC.cpp @@ -1741,8 +1741,10 @@ } else if (ObjCCategoryImplDecl *ImplD = dyn_cast_or_null(this)) { - if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier())) + if (ObjCCategoryDecl *CD = + IFace->FindCategoryDeclaration(ImplD->getIdentifier())) { Ctx.setObjCImplementation(CD, ImplD); + } } ClassInterface = IFace; Index: lib/Serialization/ASTReaderDecl.cpp =================================================================== --- lib/Serialization/ASTReaderDecl.cpp +++ lib/Serialization/ASTReaderDecl.cpp @@ -981,17 +981,20 @@ void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) { VisitObjCContainerDecl(D); - D->setClassInterface(ReadDeclAs(Record, Idx)); + // For ObjCCategoryImplDecl, setClassInterface must not be called until the + // category identifier has been deserialized and set. See PR23175. } void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { VisitObjCImplDecl(D); D->setIdentifier(Reader.GetIdentifierInfo(F, Record, Idx)); D->CategoryNameLoc = ReadSourceLocation(Record, Idx); + D->setClassInterface(ReadDeclAs(Record, Idx)); } void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { VisitObjCImplDecl(D); + D->setClassInterface(ReadDeclAs(Record, Idx)); D->setSuperClass(ReadDeclAs(Record, Idx)); D->SuperLoc = ReadSourceLocation(Record, Idx); D->setIvarLBraceLoc(ReadSourceLocation(Record, Idx)); Index: lib/Serialization/ASTWriterDecl.cpp =================================================================== --- lib/Serialization/ASTWriterDecl.cpp +++ lib/Serialization/ASTWriterDecl.cpp @@ -695,7 +695,6 @@ void ASTDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) { VisitObjCContainerDecl(D); - Writer.AddDeclRef(D->getClassInterface(), Record); // Abstract class (no need to define a stable serialization::DECL code). } @@ -703,11 +702,13 @@ VisitObjCImplDecl(D); Writer.AddIdentifierRef(D->getIdentifier(), Record); Writer.AddSourceLocation(D->getCategoryNameLoc(), Record); + Writer.AddDeclRef(D->getClassInterface(), Record); Code = serialization::DECL_OBJC_CATEGORY_IMPL; } void ASTDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { VisitObjCImplDecl(D); + Writer.AddDeclRef(D->getClassInterface(), Record); Writer.AddDeclRef(D->getSuperClass(), Record); Writer.AddSourceLocation(D->getSuperClassLoc(), Record); Writer.AddSourceLocation(D->getIvarLBraceLoc(), Record);