This is an archive of the discontinued LLVM Phabricator instance.

[Sema] -Wformat-pedantic only for NSInteger/NSUInteger %tu/%td on Darwin
ClosedPublic

Authored by arphaman on Jul 2 2018, 2:41 PM.

Details

Summary

The '%tu'/'%td' as formatting specifiers have been used to print out the NSInteger/NSUInteger values for a long time. Typically their ABI matches (i.e. ptrdiff_t = NSInteger), but that's not the case on watchOS (ptrdiff_t = long, NSInteger = int). These specifiers trigger -Wformat warnings only for watchOS builds, which is really inconvenient for cross-platform code.

This patch avoids this -Wformat warning for '%tu'/'%td' and NS[U]Integer only, and instead uses the new -Wformat-pedantic warning that JF introduced in https://reviews.llvm.org/D47290. This is acceptable because Darwin guarantees that, despite the watchOS ABI differences, sizeof(ptrdiff_t) == sizeof(NS[U]Integer), so the warning is therefore noisy for pedantic reasons. Once this is in I'll update public documentation.

rdar://41739204

Diff Detail

Event Timeline

arphaman created this revision.Jul 2 2018, 2:41 PM
jfb accepted this revision.Jul 5 2018, 11:00 AM

LGTM after a few questions.

include/clang/Analysis/Analyses/FormatString.h
265

"unspecified" seems odd because it *is* specified, we just don't care. How about something like "NothingSpecial" or "DontCare"?

test/SemaObjC/format-size-spec-nsinteger.m
4

We use a bunch of different triples to test WatchOS:

5 thumbv7k-apple-watchos2.0
3 i386-apple-watchos4
2 x86_64-apple-watchos
2 thumbv7k-apple-watchos
2 armv7k-apple-watchos3.0.0
2 armv7k-apple-watchos2.0
2 armv7k-apple-watchos
1 x86_64-apple-watchos-simulator
1 thumbv7k-apple-watchos1.0
1 i686-apple-watchos
1 i386-apple-watchos3.0-simulator
1 i386-apple-watchos3
1 i386-apple-watchos2.1
1 i386-apple-watchos2.0-simulator
1 i386-apple-watchos2.0
1 i386-apple-watchos-simulator
1 i386-apple-watchos
1 armv7k-apple-watchos2.1
1 armv7-apple-watchos
1 arm64-apple-watchos
1 aarch64-apple-watchos

Do we care about v7k only for this test?

This revision is now accepted and ready to land.Jul 5 2018, 11:00 AM

This is acceptable because Darwin guarantees that, despite the watchOS ABI differences, sizeof(ptrdiff_t) == sizeof(NS[U]Integer)

Can you describe these ABI differences please? Also, does Darwin guarantee that alignof(ptrdiff_t) == alignof(NS[U]Integer)?

arphaman marked an inline comment as done.Jul 5 2018, 12:52 PM

This is acceptable because Darwin guarantees that, despite the watchOS ABI differences, sizeof(ptrdiff_t) == sizeof(NS[U]Integer)

Can you describe these ABI differences please? Also, does Darwin guarantee that alignof(ptrdiff_t) == alignof(NS[U]Integer)?

The ABI difference boils down to the following:

Regular 32-bit Darwin targets (like armv7) use 'ptrdiff_t' of type 'int', which matches 'NSInteger'.
WatchOS arm iOS target (armv7k) uses 'ptrdiff_t' of type 'long', which doesn't match 'NSInteger' of type 'int'.

Also, does Darwin guarantee that alignof(ptrdiff_t) == alignof(NS[U]Integer)?

Yes.

include/clang/Analysis/Analyses/FormatString.h
265

Thanks. Changed to "DontCare".

test/SemaObjC/format-size-spec-nsinteger.m
4

Yes.
armv7k is used to generate thumbv7k .
i386 is used by simulator, and doesn't have this ABI issue.
We don't support other arches for watches.

arphaman updated this revision to Diff 154294.Jul 5 2018, 12:53 PM
arphaman marked an inline comment as done.

Address review comments.

This is acceptable because Darwin guarantees that, despite the watchOS ABI differences, sizeof(ptrdiff_t) == sizeof(NS[U]Integer)

Can you describe these ABI differences please? Also, does Darwin guarantee that alignof(ptrdiff_t) == alignof(NS[U]Integer)?

The ABI difference boils down to the following:

Regular 32-bit Darwin targets (like armv7) use 'ptrdiff_t' of type 'int', which matches 'NSInteger'.
WatchOS arm iOS target (armv7k) uses 'ptrdiff_t' of type 'long', which doesn't match 'NSInteger' of type 'int'.

Thank you for the explanation, I appreciate it.

This revision was automatically updated to reflect the committed changes.