Can I get clang to use custom classes for Objectiv

2019-06-28 08:55发布

问题:

The gcc documentation makes reference to an -fconstant-string-class argument, that lets you change the class used for @"Objective-C string literal" instances. I find that this doesn't appear to work with clang, using this source:

#import <stdio.h>
#import <objc/runtime.h>

@interface ConstantString
{
    Class isa;
    char *c_string;
    unsigned int len;
}
@end
@implementation ConstantString

- (Class)class {return isa;}

@end

int main(int argc, char *argv[])
{
    fprintf(stderr, "%s\n", class_getName([@"foo" class]));
    return 0;
}

Compiled with this compiler in this way:

$ clang -v

Apple clang version 4.0 (tags/Apple/clang-421.10.60) (based on LLVM 3.1svn)

Target: x86_64-apple-darwin12.0.0

Thread model: posix

$ clang -fconstant-string-class=ConstantString -lobjc constantstring.m

Undefined symbols for architecture x86_64:

"___CFConstantStringClassReference", referenced from:

  CFString in constantstring-C7icsk.o ld: symbol(s) not found for architecture x86_64 

clang: error: linker command failed with exit code 1 (use -v to see invocation)

If I link against Foundation, then I find it's still using the default constant string class:

$ clang -fconstant-string-class=ConstantString -lobjc constantstring.m -framework Foundation

$ ./a.out

__NSCFConstantString

So can you actually define a custom class for string literals using clang? And can the class of array, dictionary and number literals be changed?