[PATCH] D42933: [Sema] Avoid -Wformat warning for NSInteger/NSUInteger 'int' values with %zu/%zi long specifiers

John McCall via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 8 18:39:02 PDT 2018


rjmccall added a comment.

In https://reviews.llvm.org/D42933#1092077, @smeenai wrote:

> Apple's current recommendation for using printf with the NSInteger types is casting to a long, per https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html. Are you suggesting that recommendation would change to using `%zd` instead?


I'm not saying that the recommendation would change, but `long` being pointer-sized is a pretty universal assumption on Darwin, I'm afraid.

In https://reviews.llvm.org/D42933#1092154, @jfb wrote:

> I don't disagree with the original intent, but AFAICT that's exactly the intent of the warning I'd like to get rid of. I'm making a very narrow point: there is no portability problem with the warning I'm specifically trying to silence (using `%z` with `NS[U]Integer` on Darwin). If we decide that `-Wformat` shouldn't emit portability warnings then my point is moot, so let's see if people agree to that. If not, then my point still stands.


Agreed.

>>> - There are always established idioms for solving portability problems.  In this case, a certain set of important typedefs from the C standard have been blessed with dedicated format modifiers like "z", but every other typedef in the world has to find some other solution, either by asserting that some existing format is good enough (as NSInteger does) or by introducing a macro that expands to an appropriate format (as some things in POSIX do).  A proper format-portability warning would have to know about all of these conventions (in order to check that e.g. part of the format string came from the right macro), which just seems wildly out-of-scope for a compiler warning.
> 
> We could provide a macro for `NS[U]Integer`, but it's been long enough and there's enough existing code with `%z`. Not sure the code churn on user is worth it, if we can instead say "`%z` works".

Right.  I'm not trying to argue that we should add a macro for NSInteger, I'm saying that that's the sort of thing that a portability warning would have to consider.  Portability warnings are... always tempting, but they're very, very difficult in C.  Since we're not going to do that sort of work, we should back off from doing a portability warning.

John.


Repository:
  rC Clang

https://reviews.llvm.org/D42933





More information about the cfe-commits mailing list