]> granicus.if.org Git - clang/commitdiff
Disable "non literal format string" for NSString that result from a macro expansion.
authorJean-Daniel Dupas <devlists@shadowlab.org>
Mon, 30 Jan 2012 19:46:17 +0000 (19:46 +0000)
committerJean-Daniel Dupas <devlists@shadowlab.org>
Mon, 30 Jan 2012 19:46:17 +0000 (19:46 +0000)
This is to prevent diagnostic when using NSLocalizedString or CFCopyLocalizedString
macros which are usually used in place of NS and CF strings literals.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@149268 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaChecking.cpp
test/SemaObjC/format-strings-objc.m

index b31a5b8a84da3825b4398d10d874534f522e0317..7a852aefad1360332ff59aff3f5253dae2dc4fbc 100644 (file)
@@ -1586,6 +1586,13 @@ void Sema::CheckFormatArguments(Expr **Args, unsigned NumArgs,
                              format_idx, firstDataArg, Type))
     return;  // Literal format string found, check done!
 
+  // Do not emit diag when the string param is a macro expansion and the
+  // format is either NSString or CFString. This is a hack to prevent
+  // diag when using the NSLocalizedString and CFCopyLocalizedString macros
+  // which are usually used in place of NS and CF string literals.
+  if (Type == FST_NSString && Args[format_idx]->getLocStart().isMacroID())
+    return;
+
   // If there are no arguments specified, warn with -Wformat-security, otherwise
   // warn only with -Wformat-nonliteral.
   if (NumArgs == format_idx+1)
index e130b1803fdf0c522f101c56a1d9d0cff03655bd..5ab4d8bf3ba41c193ec726f34eba143173693822 100644 (file)
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -Wformat-nonliteral -fsyntax-only -verify %s
 
 //===----------------------------------------------------------------------===//
 // The following code is reduced using delta-debugging from
@@ -107,3 +107,11 @@ NSString *test_literal_propagation(void) {
   NSString * ns3 = ns1;
   NSLog(ns3); // expected-warning {{format string is not a string literal}}}
 }
+
+// Do not emit warnings when using NSLocalizedString
+extern NSString *GetLocalizedString(NSString *str);
+#define NSLocalizedString(key) GetLocalizedString(key)
+
+void check_NSLocalizedString() {
+  [Foo fooWithFormat:NSLocalizedString(@"format"), @"arg"]; // no-warning
+}