xref: /minix3/external/bsd/llvm/dist/clang/test/SemaObjC/format-cstrings-warning.m (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc// RUN: %clang_cc1 -Wcstring-format-directive -verify -fsyntax-only %s
2*0a6a1f1dSLionel Sambuc// rdar://18182443
3*0a6a1f1dSLionel Sambuc
4*0a6a1f1dSLionel Sambuctypedef __builtin_va_list __darwin_va_list;
5*0a6a1f1dSLionel Sambuctypedef __builtin_va_list va_list;
6*0a6a1f1dSLionel Sambuc
7*0a6a1f1dSLionel Sambuc@interface NSString
8*0a6a1f1dSLionel Sambuc@end
9*0a6a1f1dSLionel Sambuc
10*0a6a1f1dSLionel Sambucva_list argList;
11*0a6a1f1dSLionel Sambuc
12*0a6a1f1dSLionel Sambuc@interface NSString (NSStringExtensionMethods)
13*0a6a1f1dSLionel Sambuc- (NSString *)stringByAppendingFormat:(NSString *)format, ... __attribute__((format(__NSString__, 1, 2)));
14*0a6a1f1dSLionel Sambuc- (instancetype)initWithFormat:(NSString *)format, ... __attribute__((format(__NSString__, 1, 2))); // expected-note 2 {{method 'initWithFormat:' declared here}}
15*0a6a1f1dSLionel Sambuc- (instancetype)initWithFormat:(NSString *)format arguments:(va_list)argList __attribute__((format(__NSString__, 1, 0)));
16*0a6a1f1dSLionel Sambuc- (instancetype)initWithFormat:(NSString *)format locale:(id)locale, ... __attribute__((format(__NSString__, 1, 3)));
17*0a6a1f1dSLionel Sambuc- (instancetype)initWithFormat:(NSString *)format locale:(id)locale arguments:(va_list)argList __attribute__((format(__NSString__, 1, 0)));
18*0a6a1f1dSLionel Sambuc+ (instancetype)stringWithFormat:(NSString *)format, ... __attribute__((format(__NSString__, 1, 2))); // expected-note {{method 'stringWithFormat:' declared here}}
19*0a6a1f1dSLionel Sambuc+ (instancetype)localizedStringWithFormat:(NSString *)format, ... __attribute__((format(__NSString__, 1, 2))); // expected-note {{method 'localizedStringWithFormat:' declared here}}
20*0a6a1f1dSLionel Sambuc- (void)MyRandomMethod:(NSString *)format locale:(id)locale arguments:(va_list)argList __attribute__((format(__NSString__, 1, 0))); // expected-note {{method 'MyRandomMethod:locale:arguments:' declared here}}
21*0a6a1f1dSLionel Sambuc@end
22*0a6a1f1dSLionel Sambuc
23*0a6a1f1dSLionel Sambuc@interface NSMutableString : NSString
24*0a6a1f1dSLionel Sambuc@end
25*0a6a1f1dSLionel Sambuc
26*0a6a1f1dSLionel Sambuc@interface NSMutableString (NSMutableStringExtensionMethods)
27*0a6a1f1dSLionel Sambuc
28*0a6a1f1dSLionel Sambuc- (void)appendFormat:(NSString *)format, ... __attribute__((format(__NSString__, 1, 2)));
29*0a6a1f1dSLionel Sambuc
30*0a6a1f1dSLionel Sambuc@end
31*0a6a1f1dSLionel Sambuc
32*0a6a1f1dSLionel SambucNSString *ns(NSString *pns) {
33*0a6a1f1dSLionel Sambuc  [pns initWithFormat: @"Number %d length %c name %s", 1, 'a', "something"]; // expected-warning {{using %s directive in NSString which is being passed as a formatting argument to the formatting method}}
34*0a6a1f1dSLionel Sambuc  [NSString localizedStringWithFormat : @"Hello%s", " There"]; // expected-warning {{using %s directive in NSString which is being passed as a formatting argument to the formatting method}}
35*0a6a1f1dSLionel Sambuc  [pns initWithFormat : @"Hello%s %d %d", "Hello", 1, 2]; // expected-warning {{using %s directive in NSString which is being passed as a formatting argument to the formatting method}}
36*0a6a1f1dSLionel Sambuc  [pns MyRandomMethod : @"Hello%s %d %d" locale:0 arguments: argList];  // expected-warning {{using %s directive in NSString which is being passed as a formatting argument to the formatting method}}
37*0a6a1f1dSLionel Sambuc  return [NSString stringWithFormat : @"Hello%s", " There"]; // expected-warning {{using %s directive in NSString which is being passed as a formatting argument to the formatting method}}
38*0a6a1f1dSLionel Sambuc}
39*0a6a1f1dSLionel Sambuc
40*0a6a1f1dSLionel Sambuc
41*0a6a1f1dSLionel Sambuctypedef const struct __CFString * CFStringRef;
42*0a6a1f1dSLionel Sambuctypedef struct __CFString * CFMutableStringRef;
43*0a6a1f1dSLionel Sambuctypedef const struct __CFAllocator * CFAllocatorRef;
44*0a6a1f1dSLionel Sambuc
45*0a6a1f1dSLionel Sambuc
46*0a6a1f1dSLionel Sambuctypedef const struct __CFDictionary * CFDictionaryRef;
47*0a6a1f1dSLionel Sambuc
48*0a6a1f1dSLionel Sambuc
49*0a6a1f1dSLionel Sambucextern
50*0a6a1f1dSLionel SambucCFStringRef CFStringCreateWithFormat(CFAllocatorRef alloc, CFDictionaryRef formatOptions, CFStringRef format, ...) __attribute__((format(CFString, 3, 4)));
51*0a6a1f1dSLionel Sambuc
52*0a6a1f1dSLionel Sambucextern
53*0a6a1f1dSLionel SambucCFStringRef CFStringCreateWithFormatAndArguments(CFAllocatorRef alloc, CFDictionaryRef formatOptions, CFStringRef format, va_list arguments) __attribute__((format(CFString, 3, 0))); // expected-note {{'CFStringCreateWithFormatAndArguments' declared here}}
54*0a6a1f1dSLionel Sambuc
55*0a6a1f1dSLionel Sambucextern
56*0a6a1f1dSLionel Sambucvoid CFStringAppendFormat(CFMutableStringRef theString, CFDictionaryRef formatOptions, CFStringRef format, ...) __attribute__((format(CFString, 3, 4)));
57*0a6a1f1dSLionel Sambuc
58*0a6a1f1dSLionel Sambucextern
59*0a6a1f1dSLionel Sambucvoid CFStringAppendFormatAndArguments(CFMutableStringRef theString, CFDictionaryRef formatOptions, CFStringRef format, va_list arguments) __attribute__((format(CFString, 3, 0))); // expected-note {{'CFStringAppendFormatAndArguments' declared here}}
60*0a6a1f1dSLionel Sambuc
61*0a6a1f1dSLionel Sambucvoid Test1(va_list argList) {
62*0a6a1f1dSLionel Sambuc  CFAllocatorRef alloc;
63*0a6a1f1dSLionel Sambuc  CFStringCreateWithFormatAndArguments (alloc, 0, (CFStringRef)@"%s\n", argList); // expected-warning {{using %s directive in CFString which is being passed as a formatting argument to the formatting CFfunction}}
64*0a6a1f1dSLionel Sambuc  CFStringAppendFormatAndArguments ((CFMutableStringRef)@"AAAA", 0, (CFStringRef)"Hello %s there %d\n", argList); // expected-warning {{using %s directive in CFString which is being passed as a formatting argument to the formatting CFfunction}}
65*0a6a1f1dSLionel Sambuc  CFStringCreateWithFormatAndArguments (alloc, 0, (CFStringRef)@"%c\n", argList);
66*0a6a1f1dSLionel Sambuc  CFStringAppendFormatAndArguments ((CFMutableStringRef)@"AAAA", 0, (CFStringRef)"%d\n", argList);
67*0a6a1f1dSLionel Sambuc}
68*0a6a1f1dSLionel Sambuc
69*0a6a1f1dSLionel Sambucextern void MyNSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); // expected-note {{'MyNSLog' declared here}}
70*0a6a1f1dSLionel Sambucextern void MyCFStringCreateWithFormat(CFStringRef format, ...) __attribute__((format(__CFString__, 1, 2))); // expected-note {{'MyCFStringCreateWithFormat' declared here}}
71*0a6a1f1dSLionel Sambucextern void XMyNSLog(int, NSString *format, ...) __attribute__((format(__NSString__, 2, 3))); // expected-note {{'XMyNSLog' declared here}}
72*0a6a1f1dSLionel Sambuc
73*0a6a1f1dSLionel Sambucvoid Test2() {
74*0a6a1f1dSLionel Sambuc  MyNSLog(@"%s\n", "Hello"); // expected-warning {{using %s directive in CFString which is being passed as a formatting argument to the formatting CFfunction}}
75*0a6a1f1dSLionel Sambuc
76*0a6a1f1dSLionel Sambuc  MyCFStringCreateWithFormat((CFStringRef)@"%s", "Hello"); // expected-warning {{using %s directive in CFString which is being passed as a formatting argument to the formatting CFfunction}}
77*0a6a1f1dSLionel Sambuc  XMyNSLog(4, @"%s\n", "Hello"); // expected-warning {{using %s directive in CFString which is being passed as a formatting argument to the formatting CFfunction}}
78*0a6a1f1dSLionel Sambuc}
79*0a6a1f1dSLionel Sambuc
80