Friday, May 8, 2015

Clang’s New -Wpartial-availability

Clang (via Matt Stevens):

This warns when using decls that are not available on all deployment targets. For example, a call to

- (void)ppartialMethod __attribute__((availability(macosx,introduced=10.8)));

will warn if -mmacosx-version-min is set to less than 10.8.

To silence the warning, one has to explicitly redeclare the method like so:

@interface Whatever(MountainLionAPI)
- (void)ppartialMethod;
@end

This way, one cannot accidentally call a function that isn't available everywhere. Having to add the redeclaration will hopefully remind the user to add an explicit respondsToSelector: call as well.

I’m not sure why this wasn’t added years ago, when Apple stopped supporting older SDKs, but it’s good to finally have it.

Previously: Deploymate 1.0.

Update (2016-11-11): My strategy is to add -Wpartial-availability to the WARNING_CFLAGS, and then wherever I have a runtime test for the OS version I bracket the code with:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wpartial-availability"
// Code that uses partially available APIs.
#pragma clang diagnostic pop

Any warnings that this does not suppress represent code that needs to have a runtime check added. This seems to be more reliable than Deploymate and has the advantage of being checked every time I compile.

Due to problems inside XCTest, it is necessary to not use -Wpartial-availability in test-related code.

Comments RSS · Twitter

Leave a Comment