[cfe-dev] Objective-C really strong type checking

Jordy Rose jediknil at belkadan.com
Wed Sep 8 14:26:25 PDT 2010


I think this case is probably useful enough that we'd go ahead and
special-case based on naming conventions for -init..., just like the
retain-release checking. The analyzer doesn't care about
implementations, since for non-class methods it can't know about them
anyway. (And after all, if someone wanted to be pathological they could
switch out the implementation of the +allocWithZone: method at runtime.)

On the other hand, factory methods like +[NSArray arrayWithObject:]
pose a bigger problem, since there's not really a checkable convention
for the factory method name. So for completeness' sake, we might want
such an annotation, just like attribute((ns_returns_retained)). But it'd
be nice to have this check automagically work for init methods, even
without annotations (again like ns_returns_retained).

As for the "this method returns the receiver" annotation, this is
important for -retain and -autorelease, but not very common in Cocoa
otherwise. Admittedly, there are libraries other than Cocoa, and the
annotation could be shared with C++ as well. But again, we might want to
special-case -retain and -autorelease (and IIRC we are) since they show
up so often, even when the annotation is "missing".

Of course, right now the handling of -retain and -autorelease is bound
up in CFRefCount.cpp, which needs to be broken down and rewritten to use
the regular Checker interface. I think Ted's planning this eventual
rewrite/refactor.

Jordy


On Wed, 8 Sep 2010 20:31:06 +0100, David Chisnall <csdavec at swan.ac.uk>
wrote:
> On 8 Sep 2010, at 19:50, Fariborz Jahanian wrote:
> 
>> Problem with this is that this is very common form of assignments in
>> objc and checker will get very noisy.
> 
> 
> No it won't.  Assignments to a distinct Objective-C type are very
> rare.  The issue here is that +new (along with (+alloc,
> +allocWithZone:, -init, and so on) is declared on NSObject and not
> overridden.  When you send a +alloc message to almost any class, you
> are calling the implementation in NSObject, with the class as self. 
> This implementation then allocates enough space for an instance of the
> receiver, initalizes the isa pointer, and returns.
> 
> The problem, from an analysis point of view, is that the Objective-C
> type system is not expressive enough to tell you what is really going
> on here.  The +new method always returns an instance of the receiver
> (or, in some special cases such as class clusters, a subclass of the
> receiver).
> 
> Although it would be possible to add a specific hack for +new into
> the analyser, it would be cleaner to solve this in the general case by
> providing two annotations, one saying that the return type of a class
> method is an instance of the receiver and another saying that the
> return type of an instance method is the receiver.
> 
> The latter would also be useful for -retain, -autorelease, and all of
> the various -initWith{something}: methods in Cocoa.  For example, the
> initialisers in NSArray return a mutable array when the receiver is an
> instance of NSMutableArray (or a subclass), while the constructors
> return a mutable array when the receiver is NSMutableArray.
> 
> David
> 
> -- Sent from my STANTEC-ZEBRA
> 
> 
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev




More information about the cfe-dev mailing list