xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/enable_if.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1 // RUN: %clang_cc1 %s -verify
2 // RUN: %clang_cc1 %s -DCODEGEN -emit-llvm -o - | FileCheck %s
3 
4 #define O_CREAT 0x100
5 typedef int mode_t;
6 typedef unsigned long size_t;
7 
8 int open(const char *pathname, int flags) __attribute__((enable_if(!(flags & O_CREAT), "must specify mode when using O_CREAT"))) __attribute__((overloadable));  // expected-note{{candidate disabled: must specify mode when using O_CREAT}}
9 int open(const char *pathname, int flags, mode_t mode) __attribute__((overloadable));  // expected-note{{candidate function not viable: requires 3 arguments, but 2 were provided}}
10 
test1()11 void test1() {
12 #ifndef CODEGEN
13   open("path", O_CREAT);  // expected-error{{no matching function for call to 'open'}}
14 #endif
15   open("path", O_CREAT, 0660);
16   open("path", 0);
17   open("path", 0, 0);
18 }
19 
20 size_t __strnlen_chk(const char *s, size_t requested_amount, size_t s_len);
21 
22 size_t strnlen(const char *s, size_t maxlen)  // expected-note{{candidate function}}
23   __attribute__((overloadable))
24   __asm__("strnlen_real1");
25 
26 __attribute__((always_inline))
strnlen(const char * s,size_t maxlen)27 inline size_t strnlen(const char *s, size_t maxlen)  // expected-note{{candidate function}}
28   __attribute__((overloadable))
29   __attribute__((enable_if(__builtin_object_size(s, 0) != -1,
30                            "chosen when target buffer size is known")))
31 {
32   return __strnlen_chk(s, maxlen, __builtin_object_size(s, 0));
33 }
34 
35 size_t strnlen(const char *s, size_t maxlen)  // expected-note{{candidate disabled: chosen when 'maxlen' is known to be less than or equal to the buffer size}}
36   __attribute__((overloadable))
37   __attribute__((enable_if(__builtin_object_size(s, 0) != -1,
38                            "chosen when target buffer size is known")))
39   __attribute__((enable_if(maxlen <= __builtin_object_size(s, 0),
40                            "chosen when 'maxlen' is known to be less than or equal to the buffer size")))
41   __asm__("strnlen_real2");
42 
43 size_t strnlen(const char *s, size_t maxlen)  // expected-note{{candidate function has been explicitly made unavailable}}
44   __attribute__((overloadable))
45   __attribute__((enable_if(__builtin_object_size(s, 0) != -1,
46                            "chosen when target buffer size is known")))
47   __attribute__((enable_if(maxlen > __builtin_object_size(s, 0),
48                            "chosen when 'maxlen' is larger than the buffer size")))
49   __attribute__((unavailable("'maxlen' is larger than the buffer size")));
50 
test2(const char * s,int i)51 void test2(const char *s, int i) {
52 // CHECK: define void @test2
53   const char c[123];
54   strnlen(s, i);
55 // CHECK: call {{.*}}strnlen_real1
56   strnlen(s, 999);
57 // CHECK: call {{.*}}strnlen_real1
58   strnlen(c, 1);
59 // CHECK: call {{.*}}strnlen_real2
60   strnlen(c, i);
61 // CHECK: call {{.*}}strnlen_chk
62 #ifndef CODEGEN
63   strnlen(c, 999);  // expected-error{{call to unavailable function 'strnlen': 'maxlen' is larger than the buffer size}}
64 #endif
65 }
66 
67 int isdigit(int c) __attribute__((overloadable));  // expected-note{{candidate function}}
68 int isdigit(int c) __attribute__((overloadable))  // expected-note{{candidate function has been explicitly made unavailable}}
69   __attribute__((enable_if(c <= -1 || c > 255, "'c' must have the value of an unsigned char or EOF")))
70   __attribute__((unavailable("'c' must have the value of an unsigned char or EOF")));
71 
test3(int c)72 void test3(int c) {
73   isdigit(c);
74   isdigit(10);
75 #ifndef CODEGEN
76   isdigit(-10);  // expected-error{{call to unavailable function 'isdigit': 'c' must have the value of an unsigned char or EOF}}
77 #endif
78 }
79 
80 #ifndef CODEGEN
81 __attribute__((enable_if(n == 0, "chosen when 'n' is zero"))) void f1(int n); // expected-error{{use of undeclared identifier 'n'}}
82 
83 int n __attribute__((enable_if(1, "always chosen"))); // expected-warning{{'enable_if' attribute only applies to functions}}
84 
85 void f(int n) __attribute__((enable_if("chosen when 'n' is zero", n == 0)));  // expected-error{{'enable_if' attribute requires a string}}
86 
87 void f(int n) __attribute__((enable_if()));  // expected-error{{'enable_if' attribute requires exactly 2 arguments}}
88 
89 void f(int n) __attribute__((enable_if(unresolvedid, "chosen when 'unresolvedid' is non-zero")));  // expected-error{{use of undeclared identifier 'unresolvedid'}}
90 
91 int global;
92 void f(int n) __attribute__((enable_if(global == 0, "chosen when 'global' is zero")));  // expected-error{{'enable_if' attribute expression never produces a constant expression}}  // expected-note{{subexpression not valid in a constant expression}}
93 
94 const int cst = 7;
95 void return_cst(void) __attribute__((overloadable)) __attribute__((enable_if(cst == 7, "chosen when 'cst' is 7")));
test_return_cst()96 void test_return_cst() { return_cst(); }
97 #endif
98