diff --git a/clang/lib/Rewrite/RewriteModernObjC.cpp b/clang/lib/Rewrite/RewriteModernObjC.cpp index d3d8622b701c..aee89f11b2b6 100644 --- a/clang/lib/Rewrite/RewriteModernObjC.cpp +++ b/clang/lib/Rewrite/RewriteModernObjC.cpp @@ -3171,6 +3171,14 @@ void RewriteModernObjC::RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl, llvm_unreachable("struct already synthesize- RewriteObjCInternalStruct"); } +static void WriteInternalIvarName(ObjCInterfaceDecl *IDecl, + ObjCIvarDecl *IvarDecl, std::string &Result) { + Result += "OBJC_IVAR_$_"; + Result += IDecl->getName(); + Result += "$"; + Result += IvarDecl->getName(); +} + /// RewriteIvarOffsetSymbols - Rewrite ivar offset symbols of those ivars which /// have been referenced in an ivar access expression. void RewriteModernObjC::RewriteIvarOffsetSymbols(ObjCInterfaceDecl *CDecl, @@ -3193,9 +3201,9 @@ void RewriteModernObjC::RewriteIvarOffsetSymbols(ObjCInterfaceDecl *CDecl, if (CDecl->getImplementation()) Result += "__declspec(dllexport) "; } - Result += "extern unsigned long OBJC_IVAR_$_"; - Result += CDecl->getName(); Result += "_"; - Result += IvarDecl->getName(); Result += ";"; + Result += "extern unsigned long "; + WriteInternalIvarName(CDecl, IvarDecl, Result); + Result += ";"; } } @@ -5746,8 +5754,7 @@ static void Write__extendedMethodTypes_initializer(RewriteModernObjC &RewriteObj static void Write_IvarOffsetVar(ASTContext *Context, std::string &Result, ArrayRef Ivars, - StringRef VarName, - StringRef ClassName) { + ObjCInterfaceDecl *CDecl) { // FIXME. visibilty of offset symbols may have to be set; for Darwin // this is what happens: /** @@ -5771,10 +5778,7 @@ static void Write_IvarOffsetVar(ASTContext *Context, Result += "unsigned long int "; else Result += "__declspec(dllexport) unsigned long int "; - - Result += VarName; - Result += ClassName; Result += "_"; - Result += IvarDecl->getName(); + WriteInternalIvarName(CDecl, IvarDecl, Result); Result += " __attribute__ ((used, section (\"__DATA,__objc_ivar\")))"; Result += " = "; if (IvarDecl->isBitField()) { @@ -5784,7 +5788,7 @@ static void Write_IvarOffsetVar(ASTContext *Context, } else { Result += "__OFFSETOFIVAR__(struct "; - Result += ClassName; + Result += CDecl->getNameAsString(); Result += "_IMPL, "; Result += IvarDecl->getName(); Result += ");\n"; } @@ -5795,14 +5799,14 @@ static void Write__ivar_list_t_initializer(RewriteModernObjC &RewriteObj, ASTContext *Context, std::string &Result, ArrayRef Ivars, StringRef VarName, - StringRef ClassName) { + ObjCInterfaceDecl *CDecl) { if (Ivars.size() > 0) { - Write_IvarOffsetVar(Context, Result, Ivars, "OBJC_IVAR_$_", ClassName); + Write_IvarOffsetVar(Context, Result, Ivars, CDecl); Result += "\nstatic "; Write__ivar_list_t_TypeDecl(Result, Ivars.size()); Result += " "; Result += VarName; - Result += ClassName; + Result += CDecl->getNameAsString(); Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n"; Result += "\t"; Result += "sizeof(_ivar_t)"; Result += ",\n"; Result += "\t"; Result += utostr(Ivars.size()); Result += ",\n"; @@ -5812,9 +5816,8 @@ static void Write__ivar_list_t_initializer(RewriteModernObjC &RewriteObj, Result += "\t{{"; else Result += "\t {"; - - Result += "(unsigned long int *)&OBJC_IVAR_$_"; - Result += ClassName; Result += "_"; Result += IvarDecl->getName(); + Result += "(unsigned long int *)&"; + WriteInternalIvarName(CDecl, IvarDecl, Result); Result += ", "; Result += "\""; Result += IvarDecl->getName(); Result += "\", "; @@ -6088,7 +6091,7 @@ void RewriteModernObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, Write__ivar_list_t_initializer(*this, Context, Result, IVars, "_OBJC_$_INSTANCE_VARIABLES_", - CDecl->getNameAsString()); + CDecl); // Build _objc_method_list for class's instance methods if needed SmallVector @@ -6523,10 +6526,9 @@ Stmt *RewriteModernObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) { assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); // Build name of symbol holding ivar offset. - std::string IvarOffsetName = "OBJC_IVAR_$_"; - IvarOffsetName += clsDeclared->getIdentifier()->getName(); - IvarOffsetName += "_"; - IvarOffsetName += D->getName(); + std::string IvarOffsetName; + WriteInternalIvarName(clsDeclared, D, IvarOffsetName); + ReferencedIvars[clsDeclared].insert(D); // cast offset to "char *". diff --git a/clang/test/Rewriter/objc-modern-ivar-receiver-1.mm b/clang/test/Rewriter/objc-modern-ivar-receiver-1.mm index 537530b664a3..5ffcdd49698e 100644 --- a/clang/test/Rewriter/objc-modern-ivar-receiver-1.mm +++ b/clang/test/Rewriter/objc-modern-ivar-receiver-1.mm @@ -25,4 +25,4 @@ void *sel_registerName(const char *); } @end -// CHECK: id obj = (*(id *)((char *)newInv + OBJC_IVAR_$_NSInvocation__container)); +// CHECK: id obj = (*(id *)((char *)newInv + OBJC_IVAR_$_NSInvocation$_container)); diff --git a/clang/test/Rewriter/rewrite-modern-block-ivar-call.mm b/clang/test/Rewriter/rewrite-modern-block-ivar-call.mm index 0fe2d543ee00..1bafbdd15bab 100644 --- a/clang/test/Rewriter/rewrite-modern-block-ivar-call.mm +++ b/clang/test/Rewriter/rewrite-modern-block-ivar-call.mm @@ -14,4 +14,4 @@ } @end -// CHECK: ((void (*)(struct __block_impl *))((struct __block_impl *)(*(void (**)(void))((char *)self + OBJC_IVAR_$_Foo__block)))->FuncPtr)((struct __block_impl *)(*(void (**)(void))((char *)self + OBJC_IVAR_$_Foo__block))); +// CHECK: ((void (*)(struct __block_impl *))((struct __block_impl *)(*(void (**)(void))((char *)self + OBJC_IVAR_$_Foo$_block)))->FuncPtr)((struct __block_impl *)(*(void (**)(void))((char *)self + OBJC_IVAR_$_Foo$_block))); diff --git a/clang/test/Rewriter/rewrite-modern-nested-ivar.mm b/clang/test/Rewriter/rewrite-modern-nested-ivar.mm index e2b5ea245f73..13e31cd01ead 100644 --- a/clang/test/Rewriter/rewrite-modern-nested-ivar.mm +++ b/clang/test/Rewriter/rewrite-modern-nested-ivar.mm @@ -30,4 +30,4 @@ } @end -// CHECK: (*(NSURLResponse **)((char *)(*(NSURLResponse **)((char *)(*(NSCachedURLResponseInternal **)((char *)self + OBJC_IVAR_$_NSCachedURLResponse__internal)) + OBJC_IVAR_$_NSCachedURLResponseInternal_response)) + OBJC_IVAR_$_NSURLResponse_InnerResponse)) = 0; +// CHECK: (*(NSURLResponse **)((char *)(*(NSURLResponse **)((char *)(*(NSCachedURLResponseInternal **)((char *)self + OBJC_IVAR_$_NSCachedURLResponse$_internal)) + OBJC_IVAR_$_NSCachedURLResponseInternal$response)) + OBJC_IVAR_$_NSURLResponse$InnerResponse)) = 0; diff --git a/clang/test/Rewriter/rewrite-modern-struct-ivar.mm b/clang/test/Rewriter/rewrite-modern-struct-ivar.mm index 5ab02d438989..09c02dffab19 100644 --- a/clang/test/Rewriter/rewrite-modern-struct-ivar.mm +++ b/clang/test/Rewriter/rewrite-modern-struct-ivar.mm @@ -21,4 +21,4 @@ struct S { - (struct S) dMeth{ return struct_ivar; } @end -// CHECK: return (*(struct S *)((char *)self + OBJC_IVAR_$_I_struct_ivar)); +// CHECK: return (*(struct S *)((char *)self + OBJC_IVAR_$_I$struct_ivar));