<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/56952>56952</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[GNU ObjC] Clang 14 regression: class methods always return nil object with ARC and optimization
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
triplef
</td>
</tr>
</table>
<pre>
Using using Automatic Reference Counting and `-O1` or better optimization using Clang 14.0.5 or 15.0 rc1 causes class methods returning an ObjC object type to always return a null pointer.
This works correctly with Clang 13, or when not using ARC, or when using `-O0` optimization. Methods returning C primitive types are not affected.
The following code reproduces the issue:
```objc
#include <stdio.h>
#import "objc/runtime.h"
__attribute__((objc_root_class))
@interface TestObj { id isa; }
+ (Class)class;
+ (id)testObj;
+ (const char *)testStr;
+ (int)testNum;
@end
@implementation TestObj
+ (Class)class {
return self;
}
+ (id)testObj {
id testObj = @"Test";
printf("returning testObj: 0x%p\n", testObj);
return testObj;
}
+ (const char *)testStr {
return "forty two";
}
+ (int)testNum {
return 42;
}
@end
int main() {
id testClass = [TestObj class];
printf("testClass: %s (%p)\n", class_getName(testClass), testClass);
TestObj *testObj = [TestObj testObj];
printf("testObj: 0x%p\n", testObj);
const char *testStr = [TestObj testStr];
printf("testStr: %s\n", testStr);
int testNum = [TestObj testNum];
printf("testNum: %d\n", testNum);
return 0;
}
```
Build command:
```
"C:\Program Files\LLVM-14\bin\clang" -IC:\libobjc2\include -LC:\libobjc2\lib -fobjc-runtime=gnustep-2.0 -fobjc-arc -fuse-ld=lld -lobjc -O2 -o test.exe test.m
```
Output:
```
testClass: TestObj (00007FF7CD7F1158)
returning testObj: 0xA9979F4000000024
testObj: 0x0000000000000000 // <<<< should be 0xA9979F4000000024
testStr: forty two
testNum: 42
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyVVl-PozYQ_zTkZQQCA5vwkIcs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