diff options
-rw-r--r-- | clang/lib/Sema/SemaChecking.cpp | 3 | ||||
-rw-r--r-- | clang/test/SemaObjC/format-strings-objc.m | 13 | ||||
-rw-r--r-- | clang/test/SemaObjC/format-strings-system.h | 10 |
3 files changed, 23 insertions, 3 deletions
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 44f4ac671f3..d538fcf36d6 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/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/clang/test/SemaObjC/format-strings-objc.m b/clang/test/SemaObjC/format-strings-objc.m index 987889bc232..c1285b2e13d 100644 --- a/clang/test/SemaObjC/format-strings-objc.m +++ b/clang/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/clang/test/SemaObjC/format-strings-system.h b/clang/test/SemaObjC/format-strings-system.h new file mode 100644 index 00000000000..73b776845cb --- /dev/null +++ b/clang/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) |