diff options
author | Mehdi Amini <mehdi.amini@apple.com> | 2016-10-24 20:39:34 +0000 |
---|---|---|
committer | Mehdi Amini <mehdi.amini@apple.com> | 2016-10-24 20:39:34 +0000 |
commit | 06d367c6c6851b272f56f4e6af63da1f71ceb0d3 (patch) | |
tree | f00dac755362c7010b9676aa676efae25c44b905 /clang/test/SemaObjC | |
parent | 06ca6ea6c38f5ec8dcc3ec010176d61f992ae15c (diff) | |
download | bcm5719-llvm-06d367c6c6851b272f56f4e6af63da1f71ceb0d3.tar.gz bcm5719-llvm-06d367c6c6851b272f56f4e6af63da1f71ceb0d3.zip |
Add support for __builtin_os_log_format[_buffer_size]
This reverts commit r285007 and reapply r284990, with a fix for the
opencl test that I broke. Original commit message follows:
These new builtins support a mechanism for logging OS events, using a
printf-like format string to specify the layout of data in a buffer.
The _buffer_size version of the builtin can be used to determine the size
of the buffer to allocate to hold the data, and then __builtin_os_log_format
can write data into that buffer. This implements format checking to report
mismatches between the format string and the data arguments. Most of this
code was written by Chris Willmore.
Differential Revision: https://reviews.llvm.org/D25888
llvm-svn: 285019
Diffstat (limited to 'clang/test/SemaObjC')
-rw-r--r-- | clang/test/SemaObjC/format-strings-oslog.m | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/clang/test/SemaObjC/format-strings-oslog.m b/clang/test/SemaObjC/format-strings-oslog.m new file mode 100644 index 00000000000..3101a677960 --- /dev/null +++ b/clang/test/SemaObjC/format-strings-oslog.m @@ -0,0 +1,62 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +#include <stdarg.h> +#include <stddef.h> +#define __need_wint_t +#include <stddef.h> // For wint_t and wchar_t + +int printf(const char *restrict, ...); + +@interface NSString +@end + +void test_os_log_format(const char *pc, int i, void *p, void *buf) { + __builtin_os_log_format(buf, ""); + __builtin_os_log_format(buf, "%d"); // expected-warning {{more '%' conversions than data arguments}} + __builtin_os_log_format(buf, "%d", i); + __builtin_os_log_format(buf, "%P", p); // expected-warning {{using '%P' format specifier without precision}} + __builtin_os_log_format(buf, "%.10P", p); + __builtin_os_log_format(buf, "%.*P", p); // expected-warning {{field precision should have type 'int', but argument has type 'void *'}} + __builtin_os_log_format(buf, "%.*P", i, p); + __builtin_os_log_format(buf, "%.*P", i, i); // expected-warning {{format specifies type 'void *' but the argument has type 'int'}} + __builtin_os_log_format(buf, "%n"); // expected-error {{os_log() '%n' format specifier is not allowed}} + __builtin_os_log_format(buf, pc); // expected-error {{os_log() format argument is not a string constant}} + + printf("%{private}s", pc); // expected-warning {{using 'private' format specifier annotation outside of os_log()/os_trace()}} + __builtin_os_log_format(buf, "%{private}s", pc); + + // <rdar://problem/23835805> + __builtin_os_log_format_buffer_size("no-args"); + __builtin_os_log_format(buf, "%s", "hi"); + + // <rdar://problem/24828090> + wchar_t wc = 'a'; + __builtin_os_log_format(buf, "%C", wc); + printf("%C", wc); + wchar_t wcs[] = {'a', 0}; + __builtin_os_log_format(buf, "%S", wcs); + printf("%S", wcs); +} + +// Test os_log_format primitive with ObjC string literal format argument. +void test_objc(const char *pc, int i, void *p, void *buf, NSString *nss) { + __builtin_os_log_format(buf, @""); + __builtin_os_log_format(buf, @"%d"); // expected-warning {{more '%' conversions than data arguments}} + __builtin_os_log_format(buf, @"%d", i); + __builtin_os_log_format(buf, @"%P", p); // expected-warning {{using '%P' format specifier without precision}} + __builtin_os_log_format(buf, @"%.10P", p); + __builtin_os_log_format(buf, @"%.*P", p); // expected-warning {{field precision should have type 'int', but argument has type 'void *'}} + __builtin_os_log_format(buf, @"%.*P", i, p); + __builtin_os_log_format(buf, @"%.*P", i, i); // expected-warning {{format specifies type 'void *' but the argument has type 'int'}} + + __builtin_os_log_format(buf, @"%{private}s", pc); + __builtin_os_log_format(buf, @"%@", nss); +} + +// Test the os_log format attribute. +void MyOSLog(const char *format, ...) __attribute__((format(os_log, 1, 2))); +void test_attribute(void *p) { + MyOSLog("%s\n", "Hello"); + MyOSLog("%d"); // expected-warning {{more '%' conversions than data arguments}} + MyOSLog("%P", p); // expected-warning {{using '%P' format specifier without precision}} +} |