blob: 3101a677960f44fb465040ab4d8479b91d09d798 [file] [log] [blame]
Mehdi Amini06d367c2016-10-24 20:39:34 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
2
3#include <stdarg.h>
4#include <stddef.h>
5#define __need_wint_t
6#include <stddef.h> // For wint_t and wchar_t
7
8int printf(const char *restrict, ...);
9
10@interface NSString
11@end
12
13void test_os_log_format(const char *pc, int i, void *p, void *buf) {
14 __builtin_os_log_format(buf, "");
15 __builtin_os_log_format(buf, "%d"); // expected-warning {{more '%' conversions than data arguments}}
16 __builtin_os_log_format(buf, "%d", i);
17 __builtin_os_log_format(buf, "%P", p); // expected-warning {{using '%P' format specifier without precision}}
18 __builtin_os_log_format(buf, "%.10P", p);
19 __builtin_os_log_format(buf, "%.*P", p); // expected-warning {{field precision should have type 'int', but argument has type 'void *'}}
20 __builtin_os_log_format(buf, "%.*P", i, p);
21 __builtin_os_log_format(buf, "%.*P", i, i); // expected-warning {{format specifies type 'void *' but the argument has type 'int'}}
22 __builtin_os_log_format(buf, "%n"); // expected-error {{os_log() '%n' format specifier is not allowed}}
23 __builtin_os_log_format(buf, pc); // expected-error {{os_log() format argument is not a string constant}}
24
25 printf("%{private}s", pc); // expected-warning {{using 'private' format specifier annotation outside of os_log()/os_trace()}}
26 __builtin_os_log_format(buf, "%{private}s", pc);
27
28 // <rdar://problem/23835805>
29 __builtin_os_log_format_buffer_size("no-args");
30 __builtin_os_log_format(buf, "%s", "hi");
31
32 // <rdar://problem/24828090>
33 wchar_t wc = 'a';
34 __builtin_os_log_format(buf, "%C", wc);
35 printf("%C", wc);
36 wchar_t wcs[] = {'a', 0};
37 __builtin_os_log_format(buf, "%S", wcs);
38 printf("%S", wcs);
39}
40
41// Test os_log_format primitive with ObjC string literal format argument.
42void test_objc(const char *pc, int i, void *p, void *buf, NSString *nss) {
43 __builtin_os_log_format(buf, @"");
44 __builtin_os_log_format(buf, @"%d"); // expected-warning {{more '%' conversions than data arguments}}
45 __builtin_os_log_format(buf, @"%d", i);
46 __builtin_os_log_format(buf, @"%P", p); // expected-warning {{using '%P' format specifier without precision}}
47 __builtin_os_log_format(buf, @"%.10P", p);
48 __builtin_os_log_format(buf, @"%.*P", p); // expected-warning {{field precision should have type 'int', but argument has type 'void *'}}
49 __builtin_os_log_format(buf, @"%.*P", i, p);
50 __builtin_os_log_format(buf, @"%.*P", i, i); // expected-warning {{format specifies type 'void *' but the argument has type 'int'}}
51
52 __builtin_os_log_format(buf, @"%{private}s", pc);
53 __builtin_os_log_format(buf, @"%@", nss);
54}
55
56// Test the os_log format attribute.
57void MyOSLog(const char *format, ...) __attribute__((format(os_log, 1, 2)));
58void test_attribute(void *p) {
59 MyOSLog("%s\n", "Hello");
60 MyOSLog("%d"); // expected-warning {{more '%' conversions than data arguments}}
61 MyOSLog("%P", p); // expected-warning {{using '%P' format specifier without precision}}
62}