第27月第25天 clang -rewrite-objc main.m
阿新 • • 發佈:2018-12-25
1.clang -rewrite-objc main.m
#import <Foundation/Foundation.h> @interface Person : NSObject //為了方便檢視重寫的程式碼將name改成cjmName @property (nonatomic, copy) NSString *cjmName; @property (nonatomic, assign) NSUInteger age; - (void)showMyself; @end @implementation Person @synthesize cjmName = _cjmName;@synthesize age = _age; - (void)showMyself { NSLog(@"Name: %@ Age: %ld", self.cjmName, self.age); } @end int main(int argc, const char * argv[]) { @autoreleasepool { Person *p = [[Person alloc] init]; [p setValue:@"Jiaming Chen" forKey:@"cjmName"]; [p setValue:@22 forKey:@"age"]; p.cjmName = @"CCCC"; [p showMyself]; } return 0; }
接著使用clang -rewrite-objc main.m
重寫為cpp
檔案,檢視main
函式重寫後的程式碼如下:
int main(int argc, const char * argv[]) { /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool; Person*p = ((Person *(*)(id, SEL))(void *)objc_msgSend)((id)((Person *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("Person"), sel_registerName("alloc")), sel_registerName("init")); ((void (*)(id, SEL, id _Nullable, NSString *))(void *)objc_msgSend)((id)p, sel_registerName("setValue:forKey:"), (id _Nullable)(NSString *)&__NSConstantStringImpl__var_folders_1f_dz4kq57d4b19s4tfmds1mysh0000gn_T_main_080287_mi_1, (NSString *)&__NSConstantStringImpl__var_folders_1f_dz4kq57d4b19s4tfmds1mysh0000gn_T_main_080287_mi_2); ((void (*)(id, SEL, id _Nullable, NSString *))(void *)objc_msgSend)((id)p, sel_registerName("setValue:forKey:"), (id _Nullable)((NSNumber *(*)(Class, SEL, int))(void *)objc_msgSend)(objc_getClass("NSNumber"), sel_registerName("numberWithInt:"), 22), (NSString *)&__NSConstantStringImpl__var_folders_1f_dz4kq57d4b19s4tfmds1mysh0000gn_T_main_080287_mi_3); ((void (*)(id, SEL, NSString *))(void *)objc_msgSend)((id)p, sel_registerName("setCjmName:"), (NSString *)&__NSConstantStringImpl__var_folders_1f_dz4kq57d4b19s4tfmds1mysh0000gn_T_main_080287_mi_4); ((void (*)(id, SEL))(void *)objc_msgSend)((id)p, sel_registerName("showMyself")); } return 0; }
https://www.jianshu.com/p/fa941b769606