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

Authored by arphaman on Feb 5 2018, 3:00 PM.



The -Wformat recently started warning for the following code because of the added support for analysis for the '%zi' specifier.

NSInteger i = NSIntegerMax;
NSLog(@"max NSInteger = %zi", i);

The problem is that on armv7 %zi is 'long', and NSInteger is typedefed to 'int' in Foundation. We should avoid this warning as it's inconvenient to our users: it's target specific (happens only on armv7 and not arm64), and breaks their existing code. We should also silence the warning for the '%zu' specifier to ensure consistency.


Diff Detail

rC Clang
arphaman created this revision.Feb 5 2018, 3:00 PM

This seems ... suboptimal. It breaks existing code in the sense that their code was already broken, and the compiler is just diagnosing it correctly now. Note that Apple themselves recommend casting and using a proper printf specifier: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html (under Platform Dependencies).

It sounds to me like the check is working as designed and that the user code was already broken, but it happened to work at runtime because the stars aligned properly for the user. Am I misunderstanding something?

@aaron.ballman, yeah, I believe that the warning is working as intended, it just so happens that at runtime things just happened to work out.

arphaman abandoned this revision.Feb 7 2018, 2:09 PM
arphaman added subscribers: dcoughlin, dexonsmith.

Thanks for your input. You're right, this warning is quite correct (even though it seems like too much).

I discussed this with @dexonsmith and @dcoughlin and we decided to keep the warning as is and to avoid establishing new guidelines. I'm closing this one.