[cfe-commits] [PATCH] format attribute improvements.

Jean-Daniel Dupas devlists at shadowlab.org
Tue Jan 24 16:39:20 PST 2012


Hi,

Here is a series of small patches to improve format attribute handling:

1-type: defer format type checking until we really need it. With this change, clang can report basic error (empty format, non literal format) for all supported formats, and not just printf/scanf.
This patch also remove the special handling of printf0 attribute. As we consider that printf does not imply null format (r113469), this special case is no longer useful.

2-block: A simple change that enable support for multiple format attribute on a single block. It make block's "format attribute" handling on par with functions.

3-macros: In Obj-C and CoreFoundation, the recommended way to localize string are respectively to use NSLocalizedString(key, comment) and CFCopyLocalizedString(key, comment) macros family.
It is common to use these macros as format string, but as they expand to method/function call, clang will warn about "non literal string" used as format string.
So, this patch is a tentative to prevent diagnostic for this common usage. It inhibits the "non literal string format" diagnostic when the format type if NS/CFstring and the format argument is a macro expansion.
Note that while the CFCopyLocalizedString() expands to a function properly tagged with the "format_arg" attribute, we can't rely on it, because interpreting the 'key' parameter as a format string is incorrect IMHO.
It is a common practice to use some kind of descriptive name for the key (i.e. "UNEXPECTED_ERROR_TITLE") instead of the string value ("An unexpected error occurred: %@").
Moreover, NSLocalizedString() does not use the "format_arg" attribute.

If you have a better idea to limit diagnostic for this common pattern, let me know.

4.objc-const.patch: This patch add support for constant NSString pointer as format string (to make NSString on par with c strings).

5.vprintf: This patch implements a long standing FIXME. When the format string variable is an enclosing function parameter, we try to determine if this parameter is itself a format string.

Please, can someone review these patches.
Thanks.

-- Jean-Daniel



-------------- next part --------------
A non-text attachment was scrubbed...
Name: 1-type.patch
Type: application/octet-stream
Size: 11359 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120125/0428c8dc/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 2-block.patch
Type: application/octet-stream
Size: 2341 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120125/0428c8dc/attachment-0001.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 3-macros.patch
Type: application/octet-stream
Size: 1653 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120125/0428c8dc/attachment-0002.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 4-objc-const.patch
Type: application/octet-stream
Size: 2111 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120125/0428c8dc/attachment-0003.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 5-vprintf.patch
Type: application/octet-stream
Size: 4876 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120125/0428c8dc/attachment-0004.obj>


More information about the cfe-commits mailing list