<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br>Le 30 janv. 2012 à 22:10, Eli Friedman a écrit :<br><br><blockquote type="cite">On Mon, Jan 30, 2012 at 11:46 AM, Jean-Daniel Dupas<br><<a href="mailto:devlists@shadowlab.org">devlists@shadowlab.org</a>> wrote:<br><blockquote type="cite">Author: jddupas<br></blockquote><blockquote type="cite">Date: Mon Jan 30 13:46:17 2012<br></blockquote><blockquote type="cite">New Revision: 149268<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">URL: <a href="http://llvm.org/viewvc/llvm-project?rev=149268&view=rev">http://llvm.org/viewvc/llvm-project?rev=149268&view=rev</a><br></blockquote><blockquote type="cite">Log:<br></blockquote><blockquote type="cite">Disable "non literal format string" for NSString that result from a macro expansion.<br></blockquote><blockquote type="cite">This is to prevent diagnostic when using NSLocalizedString or CFCopyLocalizedString<br></blockquote><blockquote type="cite">macros which are usually used in place of NS and CF strings literals.<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">Modified:<br></blockquote><blockquote type="cite">   cfe/trunk/lib/Sema/SemaChecking.cpp<br></blockquote><blockquote type="cite">   cfe/trunk/test/SemaObjC/format-strings-objc.m<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">Modified: cfe/trunk/lib/Sema/SemaChecking.cpp<br></blockquote><blockquote type="cite">URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=149268&r1=149267&r2=149268&view=diff">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=149268&r1=149267&r2=149268&view=diff</a><br></blockquote><blockquote type="cite">==============================================================================<br></blockquote><blockquote type="cite">--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)<br></blockquote><blockquote type="cite">+++ cfe/trunk/lib/Sema/SemaChecking.cpp Mon Jan 30 13:46:17 2012<br></blockquote><blockquote type="cite">@@ -1586,6 +1586,13 @@<br></blockquote><blockquote type="cite">                             format_idx, firstDataArg, Type))<br></blockquote><blockquote type="cite">    return;  // Literal format string found, check done!<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">+  // Do not emit diag when the string param is a macro expansion and the<br></blockquote><blockquote type="cite">+  // format is either NSString or CFString. This is a hack to prevent<br></blockquote><blockquote type="cite">+  // diag when using the NSLocalizedString and CFCopyLocalizedString macros<br></blockquote><blockquote type="cite">+  // which are usually used in place of NS and CF string literals.<br></blockquote><blockquote type="cite">+  if (Type == FST_NSString && Args[format_idx]->getLocStart().isMacroID())<br></blockquote><blockquote type="cite">+    return;<br></blockquote><blockquote type="cite">+<br></blockquote><blockquote type="cite">  // If there are no arguments specified, warn with -Wformat-security, otherwise<br></blockquote><blockquote type="cite">  // warn only with -Wformat-nonliteral.<br></blockquote><blockquote type="cite">  if (NumArgs == format_idx+1)<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">Modified: cfe/trunk/test/SemaObjC/format-strings-objc.m<br></blockquote><blockquote type="cite">URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/format-strings-objc.m?rev=149268&r1=149267&r2=149268&view=diff">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/format-strings-objc.m?rev=149268&r1=149267&r2=149268&view=diff</a><br></blockquote><blockquote type="cite">==============================================================================<br></blockquote><blockquote type="cite">--- cfe/trunk/test/SemaObjC/format-strings-objc.m (original)<br></blockquote><blockquote type="cite">+++ cfe/trunk/test/SemaObjC/format-strings-objc.m Mon Jan 30 13:46:17 2012<br></blockquote><blockquote type="cite">@@ -1,4 +1,4 @@<br></blockquote><blockquote type="cite">-// RUN: %clang_cc1 -fsyntax-only -verify %s<br></blockquote><blockquote type="cite">+// RUN: %clang_cc1 -Wformat-nonliteral -fsyntax-only -verify %s<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite"> //===----------------------------------------------------------------------===//<br></blockquote><blockquote type="cite"> // The following code is reduced using delta-debugging from<br></blockquote><blockquote type="cite">@@ -107,3 +107,11 @@<br></blockquote><blockquote type="cite">  NSString * ns3 = ns1;<br></blockquote><blockquote type="cite">  NSLog(ns3); // expected-warning {{format string is not a string literal}}}<br></blockquote><blockquote type="cite"> }<br></blockquote><blockquote type="cite">+<br></blockquote><blockquote type="cite">+// Do not emit warnings when using NSLocalizedString<br></blockquote><blockquote type="cite">+extern NSString *GetLocalizedString(NSString *str);<br></blockquote><blockquote type="cite">+#define NSLocalizedString(key) GetLocalizedString(key)<br></blockquote><blockquote type="cite">+<br></blockquote><blockquote type="cite">+void check_NSLocalizedString() {<br></blockquote><blockquote type="cite">+  [Foo fooWithFormat:NSLocalizedString(@"format"), @"arg"]; // no-warning<br></blockquote><blockquote type="cite">+}<br></blockquote><br>I'm not sure this is the right approach... see also<br><a href="http://llvm.org/bugs/show_bug.cgi?id=3814">http://llvm.org/bugs/show_bug.cgi?id=3814</a> , which is essentially the<br>same issue.<br><br></blockquote><br>gettext convention are not the same as Cocoa ones. <div><br></div><div>From gettext man page:  "By convention, [msgid] is the English version of the message, with non-ASCII characters replaced by ASCII approximations." which means that the gettext macro argument should be a valid format string when used as printf argument.<div>If so, the gettext functions declaration should use the format_arg() attribute which is already handled in clang.</div><div><br></div><div>Indeed, using a recent version of gettext (which use format_arg), I get the correct behavior:</div><div><br></div><div>---- fmt.c ---</div><div>#include <stdio.h><br>#include <libintl.h><br>#include <gettext-po.h><br><br>int test(int arg) {<br><span class="Apple-tab-span" style="white-space:pre">        </span>return printf(gettext("this is a format string: %s"), arg);<br>}</div><div>---------------</div><div><br></div><div><font class="Apple-style-span" face="Menlo">fmt.c:7:51: warning: format specifies type 'char *' but the argument has type 'int'<br>        return printf(gettext("this is a format string: %s"), arg);</font></div><div><font class="Apple-style-span" face="Menlo">                                                        ~^    ~~~</font></div><div><font class="Apple-style-span" face="Menlo"><br></font></div><div>We may have to improve the support of multiple format_arg attributes, but I'm not sure what the semantic should be in such case.</div><div><br></div><div>The problem with Cocoa localization macro is that there is not such convention, and it is very common to use a key that is not a version of the localized string at all. So even if they were declared using format_arg (which is not the case), it would not help.</div><div><br></div><div><br></div><div>-- Jean-Daniel<br><br><br><br><br></div></div></body></html>