]> granicus.if.org Git - clang/commitdiff
Inhibit ObjC format warning only in system headers (NSLocalizedString).
authorJean-Daniel Dupas <devlists@shadowlab.org>
Fri, 4 May 2012 21:08:08 +0000 (21:08 +0000)
committerJean-Daniel Dupas <devlists@shadowlab.org>
Fri, 4 May 2012 21:08:08 +0000 (21:08 +0000)
Add a test case for the related NSAssert workaround.

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

lib/Sema/SemaChecking.cpp
test/SemaObjC/format-strings-objc.m
test/SemaObjC/format-strings-system.h [new file with mode: 0644]

index 44f4ac671f3de8ef67f346a1bd288707e7897ac6..d538fcf36d63fd4766c3ebffa1332fe1fa0f25f5 100644 (file)
@@ -1749,7 +1749,8 @@ void Sema::CheckFormatArguments(Expr **Args, unsigned NumArgs,
   // 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())
+  if (Type == FST_NSString &&
+      SourceMgr.isInSystemMacro(Args[format_idx]->getLocStart()))
     return;
 
   // If there are no arguments specified, warn with -Wformat-security, otherwise
index 987889bc232ff355d6dfaf988ee7c68afba4e4aa..c1285b2e13d41896bdaac7e41c5cb5ac5ae0f3c2 100644 (file)
@@ -111,11 +111,20 @@ NSString *test_literal_propagation(void) {
 }
 
 // Do not emit warnings when using NSLocalizedString
-extern NSString *GetLocalizedString(NSString *str);
-#define NSLocalizedString(key) GetLocalizedString(key)
+#include "format-strings-system.h"
+
+// Test it inhibits diag only for macros in system headers
+#define MyNSLocalizedString(key) GetLocalizedString(key)
+#define MyNSAssert(fmt, arg) NSLog(fmt, arg, 0, 0)
 
 void check_NSLocalizedString() {
   [Foo fooWithFormat:NSLocalizedString(@"format"), @"arg"]; // no-warning
+  [Foo fooWithFormat:MyNSLocalizedString(@"format"), @"arg"]; // expected-warning {{format string is not a string literal}}}
+}
+
+void check_NSAssert() {
+  NSAssert(@"Hello %@", @"World"); // no-warning
+  MyNSAssert(@"Hello %@", @"World"); // expected-warning  {{data argument not used by format string}}
 }
 
 typedef __WCHAR_TYPE__ wchar_t;
diff --git a/test/SemaObjC/format-strings-system.h b/test/SemaObjC/format-strings-system.h
new file mode 100644 (file)
index 0000000..73b7768
--- /dev/null
@@ -0,0 +1,10 @@
+
+#pragma clang system_header
+
+@class NSString;
+
+// Do not emit warnings when using NSLocalizedString
+extern NSString *GetLocalizedString(NSString *str);
+#define NSLocalizedString(key) GetLocalizedString(key)
+
+#define NSAssert(fmt, arg) NSLog(fmt, arg, 0, 0)