[cfe-dev] Attribute for "arc-retain-cycles" check in addition to "add" and "set" method selector prefix convention

Michaël Fortin fortinmike at gmail.com
Mon Oct 14 17:31:44 PDT 2013

Hi there,

I’m new to this mailing list, so let me begin by saying a big thank you to all of the devs working on llvm-clang! The enhancements that were made to Objective-C in the last few years (among other thinks) thanks to your work make our lives much easier.

Now for the actual business: I would like to know if there is currently a way to benefit from the "arc-retain-cycles" warning (Objective-C) for methods that do not begin with “add” and “set”. I’ve checked out the clang source to validate my theory (i.e. that only methods prefixed with “add” and “set” benefit from the check, since I didn’t see any special annotation on the declarations of methods that generated this warning) and from what I’ve seen I was correct (relevant code: SemaChecking.cpp:6634 and the implementation of isSetterLikeSelector() just above that).

I am implementing a few methods in a library that take block arguments and store them for later, repeated execution. However, those methods do not benefit from the arc-retain-cycles warning and it’s pretty easy to create a retain cycle by accident, particularly for novice Objective-C devs. I would like to annotate the methods I know can lead to cycles for inclusion in this check to save others (and myself) from introducing leaks in the future.

If there is a way to do that, I’d love to know how. Else, I’m proposing adding an Objective-C-specific attribute (if that’s the appropriate construct) to indicate that a method’s usages should be checked for potential retain cycles without requiring the method to begin with “add” or “set". Something like:

- (void)doSomethingWithObject:(id)anObject andBlock:(void (^)())aBlock __attribute__((check_retain_cycles));

In addition, it would probably be nice to offer the complementary ability to exclude some “add” and “set”-prefixed methods from the retain cycle check if we know it’s not relevant in a specific case, like the NSOperationQueue “addOperationWithBlock” exclusion that’s currently hard-coded in the isSetterLikeSelector() function (SemaChecking.cpp:6620) but in a dynamic way using an attribute:

- (void)addOperationWithBlock:(void (^)(void))block __attribute__((dont_check_retain_cycles));

What do you think?

Best regards,

Michaël Fortin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20131014/6429088f/attachment.html>

More information about the cfe-dev mailing list