xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/builtin-object-size.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -triple x86_64-apple-darwin9 -verify %s
3f4a2713aSLionel Sambuc 
4f4a2713aSLionel Sambuc int a[10];
5f4a2713aSLionel Sambuc 
f0()6f4a2713aSLionel Sambuc int f0() {
7f4a2713aSLionel Sambuc   return __builtin_object_size(&a); // expected-error {{too few arguments to function}}
8f4a2713aSLionel Sambuc }
f1()9f4a2713aSLionel Sambuc int f1() {
10f4a2713aSLionel Sambuc   return (__builtin_object_size(&a, 0) +
11f4a2713aSLionel Sambuc           __builtin_object_size(&a, 1) +
12f4a2713aSLionel Sambuc           __builtin_object_size(&a, 2) +
13f4a2713aSLionel Sambuc           __builtin_object_size(&a, 3));
14f4a2713aSLionel Sambuc }
f2()15f4a2713aSLionel Sambuc int f2() {
16f4a2713aSLionel Sambuc   return __builtin_object_size(&a, -1); // expected-error {{argument should be a value from 0 to 3}}
17f4a2713aSLionel Sambuc }
f3()18f4a2713aSLionel Sambuc int f3() {
19f4a2713aSLionel Sambuc   return __builtin_object_size(&a, 4); // expected-error {{argument should be a value from 0 to 3}}
20f4a2713aSLionel Sambuc }
21f4a2713aSLionel Sambuc 
22f4a2713aSLionel Sambuc 
23f4a2713aSLionel Sambuc // rdar://6252231 - cannot call vsnprintf with va_list on x86_64
f4(const char * fmt,...)24f4a2713aSLionel Sambuc void f4(const char *fmt, ...) {
25f4a2713aSLionel Sambuc  __builtin_va_list args;
26*0a6a1f1dSLionel Sambuc  __builtin___vsnprintf_chk (0, 42, 0, 11, fmt, args); // expected-warning {{'__builtin___vsnprintf_chk' will always overflow destination buffer}}
27f4a2713aSLionel Sambuc }
28f4a2713aSLionel Sambuc 
29*0a6a1f1dSLionel Sambuc // rdar://18334276
30*0a6a1f1dSLionel Sambuc typedef __typeof__(sizeof(int)) size_t;
31*0a6a1f1dSLionel Sambuc void * memcset(void *restrict dst, int src, size_t n);
32*0a6a1f1dSLionel Sambuc void * memcpy(void *restrict dst, const void *restrict src, size_t n);
33*0a6a1f1dSLionel Sambuc 
34*0a6a1f1dSLionel Sambuc #define memset(dest, src, len) __builtin___memset_chk(dest, src, len, __builtin_object_size(dest, 0))
35*0a6a1f1dSLionel Sambuc #define memcpy(dest, src, len) __builtin___memcpy_chk(dest, src, len, __builtin_object_size(dest, 0))
36*0a6a1f1dSLionel Sambuc #define memcpy1(dest, src, len) __builtin___memcpy_chk(dest, src, len, __builtin_object_size(dest, 4))
37*0a6a1f1dSLionel Sambuc #define NULL ((void *)0)
38*0a6a1f1dSLionel Sambuc 
f5(void)39*0a6a1f1dSLionel Sambuc void f5(void)
40*0a6a1f1dSLionel Sambuc {
41*0a6a1f1dSLionel Sambuc   char buf[10];
42*0a6a1f1dSLionel Sambuc   memset((void *)0x100000000ULL, 0, 0x1000);
43*0a6a1f1dSLionel Sambuc   memcpy((char *)NULL + 0x10000, buf, 0x10);
44*0a6a1f1dSLionel Sambuc   memcpy1((char *)NULL + 0x10000, buf, 0x10); // expected-error {{argument should be a value from 0 to 3}}
45*0a6a1f1dSLionel Sambuc }
46*0a6a1f1dSLionel Sambuc 
47*0a6a1f1dSLionel Sambuc // rdar://18431336
f6(void)48*0a6a1f1dSLionel Sambuc void f6(void)
49*0a6a1f1dSLionel Sambuc {
50*0a6a1f1dSLionel Sambuc   char b[5];
51*0a6a1f1dSLionel Sambuc   char buf[10];
52*0a6a1f1dSLionel Sambuc   __builtin___memccpy_chk (buf, b, '\0', sizeof(b), __builtin_object_size (buf, 0));
53*0a6a1f1dSLionel Sambuc   __builtin___memccpy_chk (b, buf, '\0', sizeof(buf), __builtin_object_size (b, 0));  // expected-warning {{'__builtin___memccpy_chk' will always overflow destination buffer}}
54*0a6a1f1dSLionel Sambuc }
55