[PATCH] D128556: Make Objective-C++ match Objective-C's behavior on implicit ivar access when `self` is shadowed

Michael Wyman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 27 10:50:12 PDT 2022


mwyman updated this revision to Diff 440311.
mwyman added a comment.

Added codegen test.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D128556/new/

https://reviews.llvm.org/D128556

Files:
  clang/lib/Sema/SemaLookup.cpp
  clang/test/CodeGenObjC/ivar-implicit-self-shadow.m


Index: clang/test/CodeGenObjC/ivar-implicit-self-shadow.m
===================================================================
--- /dev/null
+++ clang/test/CodeGenObjC/ivar-implicit-self-shadow.m
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -triple x86_64-apple-iossimulator -fobjc-arc -x objective-c -emit-llvm -o - %s | FileCheck %s -check-prefix=CHECK
+// RUN: %clang_cc1 -triple x86_64-apple-iossimulator -fobjc-arc -x objective-c++ -emit-llvm -o - %s | FileCheck %s -check-prefix=CHECK
+
+__attribute__((objc_root_class))
+ at interface Foo
+ at end
+
+extern Foo *CreateObject(void) __attribute__((ns_returns_retained));
+
+ at implementation Foo {
+  int _ivar;
+}
+
+// CHECK-LABEL: define{{.*}}-[Foo foo]"(ptr noundef %self, ptr noundef %_cmd) #0 {
+- (void)foo {
+  {
+    Foo *self = CreateObject();
+    _ivar = 42;
+    // CHECK:       [[PTR:%.*]] = load ptr, ptr %self.addr, align 8
+    // CHECK-NEXT:  [[IVAR:%.*]] = load i64, ptr @"OBJC_IVAR_$_Foo._ivar", align 8, {{.*}}
+    // CHECK-NEXT:  [[OFFSET:%.*]] = getelementptr inbounds i8, ptr [[PTR]], i64 [[IVAR]]
+    // CHECK-NEXT:  store i32 42, ptr [[OFFSET]]
+  }
+}
+ at end
+
Index: clang/lib/Sema/SemaLookup.cpp
===================================================================
--- clang/lib/Sema/SemaLookup.cpp
+++ clang/lib/Sema/SemaLookup.cpp
@@ -1313,6 +1313,9 @@
             R.setShadowed();
             continue;
           }
+        } else if (NameKind == LookupObjCImplicitSelfParam &&
+                   !isa<ImplicitParamDecl>(*I)) {
+          continue;
         } else {
           // We found something in this scope, we should not look at the
           // namespace scope


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128556.440311.patch
Type: text/x-patch
Size: 1646 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220627/1e0de48c/attachment.bin>


More information about the cfe-commits mailing list