This is an archive of the discontinued LLVM Phabricator instance.

[ObjC] Add an attribute that "clamps" signed char BOOLs to {0,1}
Needs ReviewPublic

Authored by erik.pilkington on Jul 11 2019, 3:11 PM.

Details

Summary

This attribute can be applied to typedefs of BOOL in Objective-C. It causes loads, stores, and casts to BOOL to clamp into {0,1}. This is useful for us because a lot of code is going to be moving from a platform where BOOL is a typedef for a native bool type (iOS) to a platform where its a typedef for signed char (macOS) because of macCatalyst. We can't change the type of the BOOL, since its ABI.

rdar://6510042

Thanks for taking a look!
Erik

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptJul 11 2019, 3:11 PM
aaron.ballman added inline comments.Jul 12 2019, 10:44 AM
clang/include/clang/Basic/Attr.td
3279

Is there a desire for similar functionality for C's _Bool datatype, or other, custom datatypes (perhaps BOOL from the Win32 APIs)? It seems like this attribute could be generalized to apply to any typedef to an integral type.

erik.pilkington marked 2 inline comments as done.

Address review comments.

clang/include/clang/Basic/Attr.td
3279

Sure, I guess there isn't anything Objective-C specific about this. The new patches renames it to clamping_integral_bool (and permits it on any integral type).

dexonsmith added inline comments.Jul 12 2019, 3:18 PM
clang/include/clang/Basic/Attr.td
3279

Given that "clamping" isn't exactly what's going on -- since -1 becomes 1, not 0 -- is there another word we can use here?

aaron.ballman added inline comments.Jul 18 2019, 11:36 AM
clang/include/clang/Basic/Attr.td
3279

Given that "clamping" isn't exactly what's going on -- since -1 becomes 1, not 0 -- is there another word we can use here?

converting_integral_bool is a bit closer to what it's doing. Or mapping. But neither of those names really strike me as more amazing than Erik's current name. boolify.

clang/include/clang/Basic/AttrDocs.td
4153

Its -> It's or It is, your call.

clang/include/clang/Basic/DiagnosticSemaKinds.td
2712

"'clamping_integral_bool' attribute only applies to a typedef to an integral type"

clang/lib/CodeGen/CodeGenFunction.h
3400

Please give the parameter a name.