[cfe-commits] r139403 - in /cfe/trunk: lib/Rewrite/RewriteObjC.cpp test/Rewriter/instancetype-test.mm

Fariborz Jahanian fjahanian at apple.com
Fri Sep 9 13:35:23 PDT 2011


Author: fjahanian
Date: Fri Sep  9 15:35:22 2011
New Revision: 139403

URL: http://llvm.org/viewvc/llvm-project?rev=139403&view=rev
Log:
objc rewriter - Add rewriter test for new instancetype
along with minor rewriter fix to handle that. This
test is still incomplete due to rewriter issues
unrelated to instancetype.

Added:
    cfe/trunk/test/Rewriter/instancetype-test.mm
Modified:
    cfe/trunk/lib/Rewrite/RewriteObjC.cpp

Modified: cfe/trunk/lib/Rewrite/RewriteObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Rewrite/RewriteObjC.cpp?rev=139403&r1=139402&r2=139403&view=diff
==============================================================================
--- cfe/trunk/lib/Rewrite/RewriteObjC.cpp (original)
+++ cfe/trunk/lib/Rewrite/RewriteObjC.cpp Fri Sep  9 15:35:22 2011
@@ -465,6 +465,8 @@
                                    const QualType *args,
                                    unsigned numArgs,
                                    bool variadic = false) {
+      if (result == Context->getObjCInstanceType())
+        result =  Context->getObjCIdType();
       FunctionProtoType::ExtProtoInfo fpi;
       fpi.Variadic = variadic;
       return Context->getFunctionType(result, args, numArgs, fpi);
@@ -970,7 +972,7 @@
 void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) {
   // When method is a synthesized one, such as a getter/setter there is
   // nothing to rewrite.
-  if (Method->isSynthesized())
+  if (Method->isImplicit())
     return;
   SourceLocation LocStart = Method->getLocStart();
   SourceLocation LocEnd = Method->getLocEnd();

Added: cfe/trunk/test/Rewriter/instancetype-test.mm
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Rewriter/instancetype-test.mm?rev=139403&view=auto
==============================================================================
--- cfe/trunk/test/Rewriter/instancetype-test.mm (added)
+++ cfe/trunk/test/Rewriter/instancetype-test.mm Fri Sep  9 15:35:22 2011
@@ -0,0 +1,73 @@
+// RUN: %clang_cc1 -x objective-c++ -Wno-return-type -fblocks -fms-extensions -rewrite-objc %s -o %t-rw.cpp
+
+void *sel_registerName(const char *);
+
+ at interface Root
++ (instancetype)alloc;
+- (instancetype)init; // expected-note{{overridden method is part of the 'init' method family}}
+- (instancetype)self;
+- (Class)class;
+
+ at property (assign) Root *selfProp;
+- (instancetype)selfProp;
+ at end
+
+ at protocol Proto1
+ at optional
+- (instancetype)methodInProto1;
+ at end
+
+ at protocol Proto2
+ at optional
+- (instancetype)methodInProto2; // expected-note{{overridden method returns an instance of its class type}}
+- (instancetype)otherMethodInProto2; // expected-note{{overridden method returns an instance of its class type}}
+ at end
+
+ at interface Subclass1 : Root
+- (instancetype)initSubclass1;
+- (void)methodOnSubclass1;
++ (instancetype)allocSubclass1;
+ at end
+
+ at interface Subclass2 : Root
+- (instancetype)initSubclass2;
+- (void)methodOnSubclass2;
+ at end
+
+// Sanity check: the basic initialization pattern.
+void test_instancetype_alloc_init_simple() {
+  Root *r1 = [[Root alloc] init];
+  Subclass1 *sc1 = [[Subclass1 alloc] init];
+}
+
+// Test that message sends to instancetype methods have the right type.
+void test_instancetype_narrow_method_search() {
+  // instancetype on class methods
+  Subclass1 *sc1 = [[Subclass1 alloc] initSubclass2]; // expected-warning{{'Subclass1' may not respond to 'initSubclass2'}}
+  Subclass2 *sc2 = [[Subclass2 alloc] initSubclass2]; // okay
+
+  // instancetype on instance methods
+  [[[Subclass1 alloc] init] methodOnSubclass2]; // expected-warning{{'Subclass1' may not respond to 'methodOnSubclass2'}}
+  [[[Subclass2 alloc] init] methodOnSubclass2];
+  
+  // instancetype on class methods using protocols
+  [[Subclass1<Proto1> alloc] methodInProto2]; // expected-warning{{method '-methodInProto2' not found (return type defaults to 'id')}}
+  [[Subclass1<Proto2> alloc] methodInProto2];
+
+  // instancetype on instance methods
+  Subclass1<Proto1> *sc1proto1 = 0;
+  [[sc1proto1 self] methodInProto2]; // expected-warning{{method '-methodInProto2' not found (return type defaults to 'id')}}
+  Subclass1<Proto2> *sc1proto2 = 0;
+  [[sc1proto2 self] methodInProto2];
+
+  // Exact type checks
+  // Message sends to Class.
+  Subclass1<Proto1> *sc1proto1_2 = [[[sc1proto1 class] alloc] init];
+
+  // Property access
+  [sc1proto1.self methodInProto2]; // expected-warning{{method '-methodInProto2' not found (return type defaults to 'id')}}
+  [sc1proto2.self methodInProto2];
+
+  [sc1proto1.selfProp methodInProto2]; // expected-warning{{method '-methodInProto2' not found (return type defaults to 'id')}}
+  [sc1proto2.selfProp methodInProto2];
+}





More information about the cfe-commits mailing list