4

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?

Community
  • 1
  • 1
  • There's a similar [SO question here](http://stackoverflow.com/questions/11252392/can-the-new-llvm-objective-c-literals-be-redirected-to-custom-classes) – Paul.s Aug 11 '12 at 19:08
  • 2
    I've definitely seen references to -fconstant-string-class in llvm code, so I believe it's supposed to work. [This](http://www.mentby.com/Group/objc-language/new-literals-hardcoded-to-foundation.html) leads me to believe it's a known bug. – zpasternack Aug 11 '12 at 19:10
  • of course it works, its just not going to link to your code. Its not supposed to. – deleted_user Sep 17 '12 at 15:21
  • A code generation option that is "not going to link to your code" does not sound plausible, @stackmonster. Can you explain more what you mean? What does the option do that means that "it works"? –  Sep 17 '12 at 16:33
  • You have no reason to replace that class and the difficulty you are experiencing is by design. – deleted_user Sep 17 '12 at 16:52
  • @stackmonster the design is supposed to enable swapping the class used for literal constant strings. Evidently the inability to do so is not by design. –  Sep 17 '12 at 18:16

0 Answers0