From: Jean-Daniel Dupas Date: Fri, 4 May 2012 21:08:08 +0000 (+0000) Subject: Inhibit ObjC format warning only in system headers (NSLocalizedString). X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=dc1702001964c3314f7090e6a4af889b5771d884;p=clang Inhibit ObjC format warning only in system headers (NSLocalizedString). 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 --- diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index 44f4ac671f..d538fcf36d 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -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 diff --git a/test/SemaObjC/format-strings-objc.m b/test/SemaObjC/format-strings-objc.m index 987889bc23..c1285b2e13 100644 --- a/test/SemaObjC/format-strings-objc.m +++ b/test/SemaObjC/format-strings-objc.m @@ -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 index 0000000000..73b776845c --- /dev/null +++ b/test/SemaObjC/format-strings-system.h @@ -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)